Updated documentation for findCaller() to indicate that a 3-tuple is now returned...
[python.git] / Modules / cPickle.c
blob4f7d1f198afb84edd1f5eb815d8def0220b22861
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,
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_CLEAR(*p);
201 self->length = clearto;
203 return 0;
206 static int
207 Pdata_grow(Pdata *self)
209 int bigger;
210 size_t nbytes;
211 PyObject **tmp;
213 bigger = self->size << 1;
214 if (bigger <= 0) /* was 0, or new value overflows */
215 goto nomemory;
216 if ((int)(size_t)bigger != bigger)
217 goto nomemory;
218 nbytes = (size_t)bigger * sizeof(PyObject *);
219 if (nbytes / sizeof(PyObject *) != (size_t)bigger)
220 goto nomemory;
221 tmp = realloc(self->data, nbytes);
222 if (tmp == NULL)
223 goto nomemory;
224 self->data = tmp;
225 self->size = bigger;
226 return 0;
228 nomemory:
229 PyErr_NoMemory();
230 return -1;
233 /* D is a Pdata*. Pop the topmost element and store it into V, which
234 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
235 * is raised and V is set to NULL. D and V may be evaluated several times.
237 #define PDATA_POP(D, V) { \
238 if ((D)->length) \
239 (V) = (D)->data[--((D)->length)]; \
240 else { \
241 PyErr_SetString(UnpicklingError, "bad pickle data"); \
242 (V) = NULL; \
246 /* PDATA_PUSH and PDATA_APPEND both push rvalue PyObject* O on to Pdata*
247 * D. If the Pdata stack can't be grown to hold the new value, both
248 * raise MemoryError and execute "return ER". The difference is in ownership
249 * of O after: _PUSH transfers ownership of O from the caller to the stack
250 * (no incref of O is done, and in case of error O is decrefed), while
251 * _APPEND pushes a new reference.
254 /* Push O on stack D, giving ownership of O to the stack. */
255 #define PDATA_PUSH(D, O, ER) { \
256 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
257 Pdata_grow((Pdata*)(D)) < 0) { \
258 Py_DECREF(O); \
259 return ER; \
261 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
264 /* Push O on stack D, pushing a new reference. */
265 #define PDATA_APPEND(D, O, ER) { \
266 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
267 Pdata_grow((Pdata*)(D)) < 0) \
268 return ER; \
269 Py_INCREF(O); \
270 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
274 static PyObject *
275 Pdata_popTuple(Pdata *self, int start)
277 PyObject *r;
278 int i, j, l;
280 l = self->length-start;
281 r = PyTuple_New(l);
282 if (r == NULL)
283 return NULL;
284 for (i = start, j = 0 ; j < l; i++, j++)
285 PyTuple_SET_ITEM(r, j, self->data[i]);
287 self->length = start;
288 return r;
291 static PyObject *
292 Pdata_popList(Pdata *self, int start)
294 PyObject *r;
295 int i, j, l;
297 l=self->length-start;
298 if (!( r=PyList_New(l))) return NULL;
299 for (i=start, j=0 ; j < l; i++, j++)
300 PyList_SET_ITEM(r, j, self->data[i]);
302 self->length=start;
303 return r;
306 /*************************************************************************/
308 #define ARG_TUP(self, o) { \
309 if (self->arg || (self->arg=PyTuple_New(1))) { \
310 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
311 PyTuple_SET_ITEM(self->arg,0,o); \
313 else { \
314 Py_DECREF(o); \
318 #define FREE_ARG_TUP(self) { \
319 if (self->arg->ob_refcnt > 1) { \
320 Py_DECREF(self->arg); \
321 self->arg=NULL; \
325 typedef struct Picklerobject {
326 PyObject_HEAD
327 FILE *fp;
328 PyObject *write;
329 PyObject *file;
330 PyObject *memo;
331 PyObject *arg;
332 PyObject *pers_func;
333 PyObject *inst_pers_func;
335 /* pickle protocol number, >= 0 */
336 int proto;
338 /* bool, true if proto > 0 */
339 int bin;
341 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
342 int nesting;
343 int (*write_func)(struct Picklerobject *, const char *, Py_ssize_t);
344 char *write_buf;
345 int buf_size;
346 PyObject *dispatch_table;
347 int fast_container; /* count nested container dumps */
348 PyObject *fast_memo;
349 } Picklerobject;
351 #ifndef PY_CPICKLE_FAST_LIMIT
352 #define PY_CPICKLE_FAST_LIMIT 50
353 #endif
355 static PyTypeObject Picklertype;
357 typedef struct Unpicklerobject {
358 PyObject_HEAD
359 FILE *fp;
360 PyObject *file;
361 PyObject *readline;
362 PyObject *read;
363 PyObject *memo;
364 PyObject *arg;
365 Pdata *stack;
366 PyObject *mark;
367 PyObject *pers_func;
368 PyObject *last_string;
369 int *marks;
370 int num_marks;
371 int marks_size;
372 Py_ssize_t (*read_func)(struct Unpicklerobject *, char **, Py_ssize_t);
373 Py_ssize_t (*readline_func)(struct Unpicklerobject *, char **);
374 int buf_size;
375 char *buf;
376 PyObject *find_class;
377 } Unpicklerobject;
379 static PyTypeObject Unpicklertype;
381 /* Forward decls that need the above structs */
382 static int save(Picklerobject *, PyObject *, int);
383 static int put2(Picklerobject *, PyObject *);
385 static
386 PyObject *
387 cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
389 va_list va;
390 PyObject *args=0, *retval=0;
391 va_start(va, format);
393 if (format) args = Py_VaBuildValue(format, va);
394 va_end(va);
395 if (format && ! args) return NULL;
396 if (stringformat && !(retval=PyString_FromString(stringformat)))
397 return NULL;
399 if (retval) {
400 if (args) {
401 PyObject *v;
402 v=PyString_Format(retval, args);
403 Py_DECREF(retval);
404 Py_DECREF(args);
405 if (! v) return NULL;
406 retval=v;
409 else
410 if (args) retval=args;
411 else {
412 PyErr_SetObject(ErrType,Py_None);
413 return NULL;
415 PyErr_SetObject(ErrType,retval);
416 Py_DECREF(retval);
417 return NULL;
420 static int
421 write_file(Picklerobject *self, const char *s, Py_ssize_t n)
423 size_t nbyteswritten;
425 if (s == NULL) {
426 return 0;
429 if (n > INT_MAX) {
430 /* String too large */
431 return -1;
434 Py_BEGIN_ALLOW_THREADS
435 nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
436 Py_END_ALLOW_THREADS
437 if (nbyteswritten != (size_t)n) {
438 PyErr_SetFromErrno(PyExc_IOError);
439 return -1;
442 return (int)n;
445 static int
446 write_cStringIO(Picklerobject *self, const char *s, Py_ssize_t n)
448 if (s == NULL) {
449 return 0;
452 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
453 return -1;
456 return (int)n;
459 static int
460 write_none(Picklerobject *self, const char *s, Py_ssize_t n)
462 if (s == NULL) return 0;
463 if (n > INT_MAX) return -1;
464 return (int)n;
467 static int
468 write_other(Picklerobject *self, const char *s, Py_ssize_t _n)
470 PyObject *py_str = 0, *junk = 0;
471 int n;
473 if (_n > INT_MAX)
474 return -1;
475 n = (int)_n;
476 if (s == NULL) {
477 if (!( self->buf_size )) return 0;
478 py_str = PyString_FromStringAndSize(self->write_buf,
479 self->buf_size);
480 if (!py_str)
481 return -1;
483 else {
484 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
485 if (write_other(self, NULL, 0) < 0)
486 return -1;
489 if (n > WRITE_BUF_SIZE) {
490 if (!( py_str =
491 PyString_FromStringAndSize(s, n)))
492 return -1;
494 else {
495 memcpy(self->write_buf + self->buf_size, s, n);
496 self->buf_size += n;
497 return n;
501 if (self->write) {
502 /* object with write method */
503 ARG_TUP(self, py_str);
504 if (self->arg) {
505 junk = PyObject_Call(self->write, self->arg, NULL);
506 FREE_ARG_TUP(self);
508 if (junk) Py_DECREF(junk);
509 else return -1;
511 else
512 PDATA_PUSH(self->file, py_str, -1);
514 self->buf_size = 0;
515 return n;
519 static Py_ssize_t
520 read_file(Unpicklerobject *self, char **s, Py_ssize_t n)
522 size_t nbytesread;
524 if (self->buf_size == 0) {
525 int size;
527 size = ((n < 32) ? 32 : n);
528 if (!( self->buf = (char *)malloc(size))) {
529 PyErr_NoMemory();
530 return -1;
533 self->buf_size = size;
535 else if (n > self->buf_size) {
536 self->buf = (char *)realloc(self->buf, n);
537 if (!self->buf) {
538 PyErr_NoMemory();
539 return -1;
541 self->buf_size = n;
544 Py_BEGIN_ALLOW_THREADS
545 nbytesread = fread(self->buf, sizeof(char), n, self->fp);
546 Py_END_ALLOW_THREADS
547 if (nbytesread != (size_t)n) {
548 if (feof(self->fp)) {
549 PyErr_SetNone(PyExc_EOFError);
550 return -1;
553 PyErr_SetFromErrno(PyExc_IOError);
554 return -1;
557 *s = self->buf;
559 return n;
563 static Py_ssize_t
564 readline_file(Unpicklerobject *self, char **s)
566 int i;
568 if (self->buf_size == 0) {
569 if (!( self->buf = (char *)malloc(40))) {
570 PyErr_NoMemory();
571 return -1;
573 self->buf_size = 40;
576 i = 0;
577 while (1) {
578 int bigger;
579 for (; i < (self->buf_size - 1); i++) {
580 if (feof(self->fp) ||
581 (self->buf[i] = getc(self->fp)) == '\n') {
582 self->buf[i + 1] = '\0';
583 *s = self->buf;
584 return i + 1;
587 bigger = self->buf_size << 1;
588 if (bigger <= 0) { /* overflow */
589 PyErr_NoMemory();
590 return -1;
592 self->buf = (char *)realloc(self->buf, bigger);
593 if (!self->buf) {
594 PyErr_NoMemory();
595 return -1;
597 self->buf_size = bigger;
602 static Py_ssize_t
603 read_cStringIO(Unpicklerobject *self, char **s, Py_ssize_t n)
605 char *ptr;
607 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
608 PyErr_SetNone(PyExc_EOFError);
609 return -1;
612 *s = ptr;
614 return n;
618 static Py_ssize_t
619 readline_cStringIO(Unpicklerobject *self, char **s)
621 Py_ssize_t n;
622 char *ptr;
624 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
625 return -1;
628 *s = ptr;
630 return n;
634 static Py_ssize_t
635 read_other(Unpicklerobject *self, char **s, Py_ssize_t n)
637 PyObject *bytes, *str=0;
639 if (!( bytes = PyInt_FromSsize_t(n))) return -1;
641 ARG_TUP(self, bytes);
642 if (self->arg) {
643 str = PyObject_Call(self->read, self->arg, NULL);
644 FREE_ARG_TUP(self);
646 if (! str) return -1;
648 Py_XDECREF(self->last_string);
649 self->last_string = str;
651 if (! (*s = PyString_AsString(str))) return -1;
652 return n;
656 static Py_ssize_t
657 readline_other(Unpicklerobject *self, char **s)
659 PyObject *str;
660 Py_ssize_t str_size;
662 if (!( str = PyObject_CallObject(self->readline, empty_tuple))) {
663 return -1;
666 if ((str_size = PyString_Size(str)) < 0)
667 return -1;
669 Py_XDECREF(self->last_string);
670 self->last_string = str;
672 if (! (*s = PyString_AsString(str)))
673 return -1;
675 return str_size;
678 /* Copy the first n bytes from s into newly malloc'ed memory, plus a
679 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
680 * The caller is responsible for free()'ing the return value.
682 static char *
683 pystrndup(const char *s, int n)
685 char *r = (char *)malloc(n+1);
686 if (r == NULL)
687 return (char*)PyErr_NoMemory();
688 memcpy(r, s, n);
689 r[n] = 0;
690 return r;
694 static int
695 get(Picklerobject *self, PyObject *id)
697 PyObject *value, *mv;
698 long c_value;
699 char s[30];
700 size_t len;
702 if (!( mv = PyDict_GetItem(self->memo, id))) {
703 PyErr_SetObject(PyExc_KeyError, id);
704 return -1;
707 if (!( value = PyTuple_GetItem(mv, 0)))
708 return -1;
710 if (!( PyInt_Check(value))) {
711 PyErr_SetString(PicklingError, "no int where int expected in memo");
712 return -1;
714 c_value = PyInt_AS_LONG((PyIntObject*)value);
716 if (!self->bin) {
717 s[0] = GET;
718 PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value);
719 len = strlen(s);
721 else if (Pdata_Check(self->file)) {
722 if (write_other(self, NULL, 0) < 0) return -1;
723 PDATA_APPEND(self->file, mv, -1);
724 return 0;
726 else {
727 if (c_value < 256) {
728 s[0] = BINGET;
729 s[1] = (int)(c_value & 0xff);
730 len = 2;
732 else {
733 s[0] = LONG_BINGET;
734 s[1] = (int)(c_value & 0xff);
735 s[2] = (int)((c_value >> 8) & 0xff);
736 s[3] = (int)((c_value >> 16) & 0xff);
737 s[4] = (int)((c_value >> 24) & 0xff);
738 len = 5;
742 if (self->write_func(self, s, len) < 0)
743 return -1;
745 return 0;
749 static int
750 put(Picklerobject *self, PyObject *ob)
752 if (ob->ob_refcnt < 2 || self->fast)
753 return 0;
755 return put2(self, ob);
759 static int
760 put2(Picklerobject *self, PyObject *ob)
762 char c_str[30];
763 int p;
764 size_t len;
765 int res = -1;
766 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
768 if (self->fast)
769 return 0;
771 if ((p = PyDict_Size(self->memo)) < 0)
772 goto finally;
774 /* Make sure memo keys are positive! */
775 /* XXX Why?
776 * XXX And does "positive" really mean non-negative?
777 * XXX pickle.py starts with PUT index 0, not 1. This makes for
778 * XXX gratuitous differences between the pickling modules.
780 p++;
782 if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
783 goto finally;
785 if (!( memo_len = PyInt_FromLong(p)))
786 goto finally;
788 if (!( t = PyTuple_New(2)))
789 goto finally;
791 PyTuple_SET_ITEM(t, 0, memo_len);
792 Py_INCREF(memo_len);
793 PyTuple_SET_ITEM(t, 1, ob);
794 Py_INCREF(ob);
796 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
797 goto finally;
799 if (!self->bin) {
800 c_str[0] = PUT;
801 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p);
802 len = strlen(c_str);
804 else if (Pdata_Check(self->file)) {
805 if (write_other(self, NULL, 0) < 0) return -1;
806 PDATA_APPEND(self->file, memo_len, -1);
807 res=0; /* Job well done ;) */
808 goto finally;
810 else {
811 if (p >= 256) {
812 c_str[0] = LONG_BINPUT;
813 c_str[1] = (int)(p & 0xff);
814 c_str[2] = (int)((p >> 8) & 0xff);
815 c_str[3] = (int)((p >> 16) & 0xff);
816 c_str[4] = (int)((p >> 24) & 0xff);
817 len = 5;
819 else {
820 c_str[0] = BINPUT;
821 c_str[1] = p;
822 len = 2;
826 if (self->write_func(self, c_str, len) < 0)
827 goto finally;
829 res = 0;
831 finally:
832 Py_XDECREF(py_ob_id);
833 Py_XDECREF(memo_len);
834 Py_XDECREF(t);
836 return res;
839 static PyObject *
840 whichmodule(PyObject *global, PyObject *global_name)
842 Py_ssize_t i, j;
843 PyObject *module = 0, *modules_dict = 0,
844 *global_name_attr = 0, *name = 0;
846 module = PyObject_GetAttrString(global, "__module__");
847 if (module)
848 return module;
849 if (PyErr_ExceptionMatches(PyExc_AttributeError))
850 PyErr_Clear();
851 else
852 return NULL;
854 if (!( modules_dict = PySys_GetObject("modules")))
855 return NULL;
857 i = 0;
858 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
860 if (PyObject_Compare(name, __main___str)==0) continue;
862 global_name_attr = PyObject_GetAttr(module, global_name);
863 if (!global_name_attr) {
864 if (PyErr_ExceptionMatches(PyExc_AttributeError))
865 PyErr_Clear();
866 else
867 return NULL;
868 continue;
871 if (global_name_attr != global) {
872 Py_DECREF(global_name_attr);
873 continue;
876 Py_DECREF(global_name_attr);
878 break;
881 /* The following implements the rule in pickle.py added in 1.5
882 that used __main__ if no module is found. I don't actually
883 like this rule. jlf
885 if (!j) {
886 j=1;
887 name=__main___str;
890 Py_INCREF(name);
891 return name;
895 static int
896 fast_save_enter(Picklerobject *self, PyObject *obj)
898 /* if fast_container < 0, we're doing an error exit. */
899 if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
900 PyObject *key = NULL;
901 if (self->fast_memo == NULL) {
902 self->fast_memo = PyDict_New();
903 if (self->fast_memo == NULL) {
904 self->fast_container = -1;
905 return 0;
908 key = PyLong_FromVoidPtr(obj);
909 if (key == NULL)
910 return 0;
911 if (PyDict_GetItem(self->fast_memo, key)) {
912 Py_DECREF(key);
913 PyErr_Format(PyExc_ValueError,
914 "fast mode: can't pickle cyclic objects "
915 "including object type %s at %p",
916 obj->ob_type->tp_name, obj);
917 self->fast_container = -1;
918 return 0;
920 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
921 Py_DECREF(key);
922 self->fast_container = -1;
923 return 0;
925 Py_DECREF(key);
927 return 1;
931 fast_save_leave(Picklerobject *self, PyObject *obj)
933 if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
934 PyObject *key = PyLong_FromVoidPtr(obj);
935 if (key == NULL)
936 return 0;
937 if (PyDict_DelItem(self->fast_memo, key) < 0) {
938 Py_DECREF(key);
939 return 0;
941 Py_DECREF(key);
943 return 1;
946 static int
947 save_none(Picklerobject *self, PyObject *args)
949 static char none = NONE;
950 if (self->write_func(self, &none, 1) < 0)
951 return -1;
953 return 0;
956 static int
957 save_bool(Picklerobject *self, PyObject *args)
959 static const char *buf[2] = {FALSE, TRUE};
960 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
961 long l = PyInt_AS_LONG((PyIntObject *)args);
963 if (self->proto >= 2) {
964 char opcode = l ? NEWTRUE : NEWFALSE;
965 if (self->write_func(self, &opcode, 1) < 0)
966 return -1;
968 else if (self->write_func(self, buf[l], len[l]) < 0)
969 return -1;
970 return 0;
973 static int
974 save_int(Picklerobject *self, PyObject *args)
976 char c_str[32];
977 long l = PyInt_AS_LONG((PyIntObject *)args);
978 int len = 0;
980 if (!self->bin
981 #if SIZEOF_LONG > 4
982 || l > 0x7fffffffL
983 || l < -0x80000000L
984 #endif
986 /* Text-mode pickle, or long too big to fit in the 4-byte
987 * signed BININT format: store as a string.
989 c_str[0] = INT;
990 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
991 if (self->write_func(self, c_str, strlen(c_str)) < 0)
992 return -1;
994 else {
995 /* Binary pickle and l fits in a signed 4-byte int. */
996 c_str[1] = (int)( l & 0xff);
997 c_str[2] = (int)((l >> 8) & 0xff);
998 c_str[3] = (int)((l >> 16) & 0xff);
999 c_str[4] = (int)((l >> 24) & 0xff);
1001 if ((c_str[4] == 0) && (c_str[3] == 0)) {
1002 if (c_str[2] == 0) {
1003 c_str[0] = BININT1;
1004 len = 2;
1006 else {
1007 c_str[0] = BININT2;
1008 len = 3;
1011 else {
1012 c_str[0] = BININT;
1013 len = 5;
1016 if (self->write_func(self, c_str, len) < 0)
1017 return -1;
1020 return 0;
1024 static int
1025 save_long(Picklerobject *self, PyObject *args)
1027 Py_ssize_t size;
1028 int res = -1;
1029 PyObject *repr = NULL;
1031 static char l = LONG;
1033 if (self->proto >= 2) {
1034 /* Linear-time pickling. */
1035 size_t nbits;
1036 size_t nbytes;
1037 unsigned char *pdata;
1038 char c_str[5];
1039 int i;
1040 int sign = _PyLong_Sign(args);
1042 if (sign == 0) {
1043 /* It's 0 -- an empty bytestring. */
1044 c_str[0] = LONG1;
1045 c_str[1] = 0;
1046 i = self->write_func(self, c_str, 2);
1047 if (i < 0) goto finally;
1048 res = 0;
1049 goto finally;
1051 nbits = _PyLong_NumBits(args);
1052 if (nbits == (size_t)-1 && PyErr_Occurred())
1053 goto finally;
1054 /* How many bytes do we need? There are nbits >> 3 full
1055 * bytes of data, and nbits & 7 leftover bits. If there
1056 * are any leftover bits, then we clearly need another
1057 * byte. Wnat's not so obvious is that we *probably*
1058 * need another byte even if there aren't any leftovers:
1059 * the most-significant bit of the most-significant byte
1060 * acts like a sign bit, and it's usually got a sense
1061 * opposite of the one we need. The exception is longs
1062 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1063 * its own 256's-complement, so has the right sign bit
1064 * even without the extra byte. That's a pain to check
1065 * for in advance, though, so we always grab an extra
1066 * byte at the start, and cut it back later if possible.
1068 nbytes = (nbits >> 3) + 1;
1069 if (nbytes > INT_MAX) {
1070 PyErr_SetString(PyExc_OverflowError, "long too large "
1071 "to pickle");
1072 goto finally;
1074 repr = PyString_FromStringAndSize(NULL, (int)nbytes);
1075 if (repr == NULL) goto finally;
1076 pdata = (unsigned char *)PyString_AS_STRING(repr);
1077 i = _PyLong_AsByteArray((PyLongObject *)args,
1078 pdata, nbytes,
1079 1 /* little endian */, 1 /* signed */);
1080 if (i < 0) goto finally;
1081 /* If the long is negative, this may be a byte more than
1082 * needed. This is so iff the MSB is all redundant sign
1083 * bits.
1085 if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
1086 (pdata[nbytes - 2] & 0x80) != 0)
1087 --nbytes;
1089 if (nbytes < 256) {
1090 c_str[0] = LONG1;
1091 c_str[1] = (char)nbytes;
1092 size = 2;
1094 else {
1095 c_str[0] = LONG4;
1096 size = (int)nbytes;
1097 for (i = 1; i < 5; i++) {
1098 c_str[i] = (char)(size & 0xff);
1099 size >>= 8;
1101 size = 5;
1103 i = self->write_func(self, c_str, size);
1104 if (i < 0) goto finally;
1105 i = self->write_func(self, (char *)pdata, (int)nbytes);
1106 if (i < 0) goto finally;
1107 res = 0;
1108 goto finally;
1111 /* proto < 2: write the repr and newline. This is quadratic-time
1112 * (in the number of digits), in both directions.
1114 if (!( repr = PyObject_Repr(args)))
1115 goto finally;
1117 if ((size = PyString_Size(repr)) < 0)
1118 goto finally;
1120 if (self->write_func(self, &l, 1) < 0)
1121 goto finally;
1123 if (self->write_func(self,
1124 PyString_AS_STRING((PyStringObject *)repr),
1125 size) < 0)
1126 goto finally;
1128 if (self->write_func(self, "\n", 1) < 0)
1129 goto finally;
1131 res = 0;
1133 finally:
1134 Py_XDECREF(repr);
1135 return res;
1139 static int
1140 save_float(Picklerobject *self, PyObject *args)
1142 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
1144 if (self->bin) {
1145 char str[9];
1146 str[0] = BINFLOAT;
1147 if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0)
1148 return -1;
1149 if (self->write_func(self, str, 9) < 0)
1150 return -1;
1152 else {
1153 char c_str[250];
1154 c_str[0] = FLOAT;
1155 PyOS_ascii_formatd(c_str + 1, sizeof(c_str) - 2, "%.17g", x);
1156 /* Extend the formatted string with a newline character */
1157 strcat(c_str, "\n");
1159 if (self->write_func(self, c_str, strlen(c_str)) < 0)
1160 return -1;
1163 return 0;
1167 static int
1168 save_string(Picklerobject *self, PyObject *args, int doput)
1170 int size, len;
1171 PyObject *repr=0;
1173 if ((size = PyString_Size(args)) < 0)
1174 return -1;
1176 if (!self->bin) {
1177 char *repr_str;
1179 static char string = STRING;
1181 if (!( repr = PyObject_Repr(args)))
1182 return -1;
1184 if ((len = PyString_Size(repr)) < 0)
1185 goto err;
1186 repr_str = PyString_AS_STRING((PyStringObject *)repr);
1188 if (self->write_func(self, &string, 1) < 0)
1189 goto err;
1191 if (self->write_func(self, repr_str, len) < 0)
1192 goto err;
1194 if (self->write_func(self, "\n", 1) < 0)
1195 goto err;
1197 Py_XDECREF(repr);
1199 else {
1200 int i;
1201 char c_str[5];
1203 if ((size = PyString_Size(args)) < 0)
1204 return -1;
1206 if (size < 256) {
1207 c_str[0] = SHORT_BINSTRING;
1208 c_str[1] = size;
1209 len = 2;
1211 else if (size <= INT_MAX) {
1212 c_str[0] = BINSTRING;
1213 for (i = 1; i < 5; i++)
1214 c_str[i] = (int)(size >> ((i - 1) * 8));
1215 len = 5;
1217 else
1218 return -1; /* string too large */
1220 if (self->write_func(self, c_str, len) < 0)
1221 return -1;
1223 if (size > 128 && Pdata_Check(self->file)) {
1224 if (write_other(self, NULL, 0) < 0) return -1;
1225 PDATA_APPEND(self->file, args, -1);
1227 else {
1228 if (self->write_func(self,
1229 PyString_AS_STRING(
1230 (PyStringObject *)args),
1231 size) < 0)
1232 return -1;
1236 if (doput)
1237 if (put(self, args) < 0)
1238 return -1;
1240 return 0;
1242 err:
1243 Py_XDECREF(repr);
1244 return -1;
1248 #ifdef Py_USING_UNICODE
1249 /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1250 backslash and newline characters to \uXXXX escapes. */
1251 static PyObject *
1252 modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, int size)
1254 PyObject *repr;
1255 char *p;
1256 char *q;
1258 static const char *hexdigit = "0123456789ABCDEF";
1260 repr = PyString_FromStringAndSize(NULL, 6 * size);
1261 if (repr == NULL)
1262 return NULL;
1263 if (size == 0)
1264 return repr;
1266 p = q = PyString_AS_STRING(repr);
1267 while (size-- > 0) {
1268 Py_UNICODE ch = *s++;
1269 /* Map 16-bit characters to '\uxxxx' */
1270 if (ch >= 256 || ch == '\\' || ch == '\n') {
1271 *p++ = '\\';
1272 *p++ = 'u';
1273 *p++ = hexdigit[(ch >> 12) & 0xf];
1274 *p++ = hexdigit[(ch >> 8) & 0xf];
1275 *p++ = hexdigit[(ch >> 4) & 0xf];
1276 *p++ = hexdigit[ch & 15];
1278 /* Copy everything else as-is */
1279 else
1280 *p++ = (char) ch;
1282 *p = '\0';
1283 _PyString_Resize(&repr, p - q);
1284 return repr;
1288 static int
1289 save_unicode(Picklerobject *self, PyObject *args, int doput)
1291 Py_ssize_t size, len;
1292 PyObject *repr=0;
1294 if (!PyUnicode_Check(args))
1295 return -1;
1297 if (!self->bin) {
1298 char *repr_str;
1299 static char string = UNICODE;
1301 repr = modified_EncodeRawUnicodeEscape(
1302 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
1303 if (!repr)
1304 return -1;
1306 if ((len = PyString_Size(repr)) < 0)
1307 goto err;
1308 repr_str = PyString_AS_STRING((PyStringObject *)repr);
1310 if (self->write_func(self, &string, 1) < 0)
1311 goto err;
1313 if (self->write_func(self, repr_str, len) < 0)
1314 goto err;
1316 if (self->write_func(self, "\n", 1) < 0)
1317 goto err;
1319 Py_XDECREF(repr);
1321 else {
1322 int i;
1323 char c_str[5];
1325 if (!( repr = PyUnicode_AsUTF8String(args)))
1326 return -1;
1328 if ((size = PyString_Size(repr)) < 0)
1329 goto err;
1330 if (size > INT_MAX)
1331 return -1; /* string too large */
1333 c_str[0] = BINUNICODE;
1334 for (i = 1; i < 5; i++)
1335 c_str[i] = (int)(size >> ((i - 1) * 8));
1336 len = 5;
1338 if (self->write_func(self, c_str, len) < 0)
1339 goto err;
1341 if (size > 128 && Pdata_Check(self->file)) {
1342 if (write_other(self, NULL, 0) < 0)
1343 goto err;
1344 PDATA_APPEND(self->file, repr, -1);
1346 else {
1347 if (self->write_func(self, PyString_AS_STRING(repr),
1348 size) < 0)
1349 goto err;
1352 Py_DECREF(repr);
1355 if (doput)
1356 if (put(self, args) < 0)
1357 return -1;
1359 return 0;
1361 err:
1362 Py_XDECREF(repr);
1363 return -1;
1365 #endif
1367 /* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1368 static int
1369 store_tuple_elements(Picklerobject *self, PyObject *t, int len)
1371 int i;
1372 int res = -1; /* guilty until proved innocent */
1374 assert(PyTuple_Size(t) == len);
1376 for (i = 0; i < len; i++) {
1377 PyObject *element = PyTuple_GET_ITEM(t, i);
1379 if (element == NULL)
1380 goto finally;
1381 if (save(self, element, 0) < 0)
1382 goto finally;
1384 res = 0;
1386 finally:
1387 return res;
1390 /* Tuples are ubiquitous in the pickle protocols, so many techniques are
1391 * used across protocols to minimize the space needed to pickle them.
1392 * Tuples are also the only builtin immutable type that can be recursive
1393 * (a tuple can be reached from itself), and that requires some subtle
1394 * magic so that it works in all cases. IOW, this is a long routine.
1396 static int
1397 save_tuple(Picklerobject *self, PyObject *args)
1399 PyObject *py_tuple_id = NULL;
1400 int len, i;
1401 int res = -1;
1403 static char tuple = TUPLE;
1404 static char pop = POP;
1405 static char pop_mark = POP_MARK;
1406 static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
1408 if ((len = PyTuple_Size(args)) < 0)
1409 goto finally;
1411 if (len == 0) {
1412 char c_str[2];
1414 if (self->proto) {
1415 c_str[0] = EMPTY_TUPLE;
1416 len = 1;
1418 else {
1419 c_str[0] = MARK;
1420 c_str[1] = TUPLE;
1421 len = 2;
1423 if (self->write_func(self, c_str, len) >= 0)
1424 res = 0;
1425 /* Don't memoize an empty tuple. */
1426 goto finally;
1429 /* A non-empty tuple. */
1431 /* id(tuple) isn't in the memo now. If it shows up there after
1432 * saving the tuple elements, the tuple must be recursive, in
1433 * which case we'll pop everything we put on the stack, and fetch
1434 * its value from the memo.
1436 py_tuple_id = PyLong_FromVoidPtr(args);
1437 if (py_tuple_id == NULL)
1438 goto finally;
1440 if (len <= 3 && self->proto >= 2) {
1441 /* Use TUPLE{1,2,3} opcodes. */
1442 if (store_tuple_elements(self, args, len) < 0)
1443 goto finally;
1444 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1445 /* pop the len elements */
1446 for (i = 0; i < len; ++i)
1447 if (self->write_func(self, &pop, 1) < 0)
1448 goto finally;
1449 /* fetch from memo */
1450 if (get(self, py_tuple_id) < 0)
1451 goto finally;
1452 res = 0;
1453 goto finally;
1455 /* Not recursive. */
1456 if (self->write_func(self, len2opcode + len, 1) < 0)
1457 goto finally;
1458 goto memoize;
1461 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1462 * Generate MARK elt1 elt2 ... TUPLE
1464 if (self->write_func(self, &MARKv, 1) < 0)
1465 goto finally;
1467 if (store_tuple_elements(self, args, len) < 0)
1468 goto finally;
1470 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1471 /* pop the stack stuff we pushed */
1472 if (self->bin) {
1473 if (self->write_func(self, &pop_mark, 1) < 0)
1474 goto finally;
1476 else {
1477 /* Note that we pop one more than len, to remove
1478 * the MARK too.
1480 for (i = 0; i <= len; i++)
1481 if (self->write_func(self, &pop, 1) < 0)
1482 goto finally;
1484 /* fetch from memo */
1485 if (get(self, py_tuple_id) >= 0)
1486 res = 0;
1487 goto finally;
1490 /* Not recursive. */
1491 if (self->write_func(self, &tuple, 1) < 0)
1492 goto finally;
1494 memoize:
1495 if (put(self, args) >= 0)
1496 res = 0;
1498 finally:
1499 Py_XDECREF(py_tuple_id);
1500 return res;
1503 /* iter is an iterator giving items, and we batch up chunks of
1504 * MARK item item ... item APPENDS
1505 * opcode sequences. Calling code should have arranged to first create an
1506 * empty list, or list-like object, for the APPENDS to operate on.
1507 * Returns 0 on success, <0 on error.
1509 static int
1510 batch_list(Picklerobject *self, PyObject *iter)
1512 PyObject *obj;
1513 PyObject *slice[BATCHSIZE];
1514 int i, n;
1516 static char append = APPEND;
1517 static char appends = APPENDS;
1519 assert(iter != NULL);
1521 if (self->proto == 0) {
1522 /* APPENDS isn't available; do one at a time. */
1523 for (;;) {
1524 obj = PyIter_Next(iter);
1525 if (obj == NULL) {
1526 if (PyErr_Occurred())
1527 return -1;
1528 break;
1530 i = save(self, obj, 0);
1531 Py_DECREF(obj);
1532 if (i < 0)
1533 return -1;
1534 if (self->write_func(self, &append, 1) < 0)
1535 return -1;
1537 return 0;
1540 /* proto > 0: write in batches of BATCHSIZE. */
1541 do {
1542 /* Get next group of (no more than) BATCHSIZE elements. */
1543 for (n = 0; n < BATCHSIZE; ++n) {
1544 obj = PyIter_Next(iter);
1545 if (obj == NULL) {
1546 if (PyErr_Occurred())
1547 goto BatchFailed;
1548 break;
1550 slice[n] = obj;
1553 if (n > 1) {
1554 /* Pump out MARK, slice[0:n], APPENDS. */
1555 if (self->write_func(self, &MARKv, 1) < 0)
1556 goto BatchFailed;
1557 for (i = 0; i < n; ++i) {
1558 if (save(self, slice[i], 0) < 0)
1559 goto BatchFailed;
1561 if (self->write_func(self, &appends, 1) < 0)
1562 goto BatchFailed;
1564 else if (n == 1) {
1565 if (save(self, slice[0], 0) < 0)
1566 goto BatchFailed;
1567 if (self->write_func(self, &append, 1) < 0)
1568 goto BatchFailed;
1571 for (i = 0; i < n; ++i) {
1572 Py_DECREF(slice[i]);
1574 } while (n == BATCHSIZE);
1575 return 0;
1577 BatchFailed:
1578 while (--n >= 0) {
1579 Py_DECREF(slice[n]);
1581 return -1;
1584 static int
1585 save_list(Picklerobject *self, PyObject *args)
1587 int res = -1;
1588 char s[3];
1589 int len;
1590 PyObject *iter;
1592 if (self->fast && !fast_save_enter(self, args))
1593 goto finally;
1595 /* Create an empty list. */
1596 if (self->bin) {
1597 s[0] = EMPTY_LIST;
1598 len = 1;
1600 else {
1601 s[0] = MARK;
1602 s[1] = LIST;
1603 len = 2;
1606 if (self->write_func(self, s, len) < 0)
1607 goto finally;
1609 /* Get list length, and bow out early if empty. */
1610 if ((len = PyList_Size(args)) < 0)
1611 goto finally;
1613 /* Memoize. */
1614 if (len == 0) {
1615 if (put(self, args) >= 0)
1616 res = 0;
1617 goto finally;
1619 if (put2(self, args) < 0)
1620 goto finally;
1622 /* Materialize the list elements. */
1623 iter = PyObject_GetIter(args);
1624 if (iter == NULL)
1625 goto finally;
1626 res = batch_list(self, iter);
1627 Py_DECREF(iter);
1629 finally:
1630 if (self->fast && !fast_save_leave(self, args))
1631 res = -1;
1633 return res;
1637 /* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1638 * MARK key value ... key value SETITEMS
1639 * opcode sequences. Calling code should have arranged to first create an
1640 * empty dict, or dict-like object, for the SETITEMS to operate on.
1641 * Returns 0 on success, <0 on error.
1643 * This is very much like batch_list(). The difference between saving
1644 * elements directly, and picking apart two-tuples, is so long-winded at
1645 * the C level, though, that attempts to combine these routines were too
1646 * ugly to bear.
1648 static int
1649 batch_dict(Picklerobject *self, PyObject *iter)
1651 PyObject *p;
1652 PyObject *slice[BATCHSIZE];
1653 int i, n;
1655 static char setitem = SETITEM;
1656 static char setitems = SETITEMS;
1658 assert(iter != NULL);
1660 if (self->proto == 0) {
1661 /* SETITEMS isn't available; do one at a time. */
1662 for (;;) {
1663 p = PyIter_Next(iter);
1664 if (p == NULL) {
1665 if (PyErr_Occurred())
1666 return -1;
1667 break;
1669 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1670 PyErr_SetString(PyExc_TypeError, "dict items "
1671 "iterator must return 2-tuples");
1672 return -1;
1674 i = save(self, PyTuple_GET_ITEM(p, 0), 0);
1675 if (i >= 0)
1676 i = save(self, PyTuple_GET_ITEM(p, 1), 0);
1677 Py_DECREF(p);
1678 if (i < 0)
1679 return -1;
1680 if (self->write_func(self, &setitem, 1) < 0)
1681 return -1;
1683 return 0;
1686 /* proto > 0: write in batches of BATCHSIZE. */
1687 do {
1688 /* Get next group of (no more than) BATCHSIZE elements. */
1689 for (n = 0; n < BATCHSIZE; ++n) {
1690 p = PyIter_Next(iter);
1691 if (p == NULL) {
1692 if (PyErr_Occurred())
1693 goto BatchFailed;
1694 break;
1696 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1697 PyErr_SetString(PyExc_TypeError, "dict items "
1698 "iterator must return 2-tuples");
1699 goto BatchFailed;
1701 slice[n] = p;
1704 if (n > 1) {
1705 /* Pump out MARK, slice[0:n], SETITEMS. */
1706 if (self->write_func(self, &MARKv, 1) < 0)
1707 goto BatchFailed;
1708 for (i = 0; i < n; ++i) {
1709 p = slice[i];
1710 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1711 goto BatchFailed;
1712 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1713 goto BatchFailed;
1715 if (self->write_func(self, &setitems, 1) < 0)
1716 goto BatchFailed;
1718 else if (n == 1) {
1719 p = slice[0];
1720 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1721 goto BatchFailed;
1722 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1723 goto BatchFailed;
1724 if (self->write_func(self, &setitem, 1) < 0)
1725 goto BatchFailed;
1728 for (i = 0; i < n; ++i) {
1729 Py_DECREF(slice[i]);
1731 } while (n == BATCHSIZE);
1732 return 0;
1734 BatchFailed:
1735 while (--n >= 0) {
1736 Py_DECREF(slice[n]);
1738 return -1;
1741 static int
1742 save_dict(Picklerobject *self, PyObject *args)
1744 int res = -1;
1745 char s[3];
1746 int len;
1747 PyObject *iter;
1749 if (self->fast && !fast_save_enter(self, args))
1750 goto finally;
1752 /* Create an empty dict. */
1753 if (self->bin) {
1754 s[0] = EMPTY_DICT;
1755 len = 1;
1757 else {
1758 s[0] = MARK;
1759 s[1] = DICT;
1760 len = 2;
1763 if (self->write_func(self, s, len) < 0)
1764 goto finally;
1766 /* Get dict size, and bow out early if empty. */
1767 if ((len = PyDict_Size(args)) < 0)
1768 goto finally;
1770 if (len == 0) {
1771 if (put(self, args) >= 0)
1772 res = 0;
1773 goto finally;
1775 if (put2(self, args) < 0)
1776 goto finally;
1778 /* Materialize the dict items. */
1779 iter = PyObject_CallMethod(args, "iteritems", "()");
1780 if (iter == NULL)
1781 goto finally;
1782 res = batch_dict(self, iter);
1783 Py_DECREF(iter);
1785 finally:
1786 if (self->fast && !fast_save_leave(self, args))
1787 res = -1;
1789 return res;
1793 static int
1794 save_inst(Picklerobject *self, PyObject *args)
1796 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1797 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1798 char *module_str, *name_str;
1799 int module_size, name_size, res = -1;
1801 static char inst = INST, obj = OBJ, build = BUILD;
1803 if (self->fast && !fast_save_enter(self, args))
1804 goto finally;
1806 if (self->write_func(self, &MARKv, 1) < 0)
1807 goto finally;
1809 if (!( class = PyObject_GetAttr(args, __class___str)))
1810 goto finally;
1812 if (self->bin) {
1813 if (save(self, class, 0) < 0)
1814 goto finally;
1817 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
1818 PyObject *element = 0;
1819 int i, len;
1821 if (!( class_args =
1822 PyObject_Call(getinitargs_func, empty_tuple, NULL)))
1823 goto finally;
1825 if ((len = PyObject_Size(class_args)) < 0)
1826 goto finally;
1828 for (i = 0; i < len; i++) {
1829 if (!( element = PySequence_GetItem(class_args, i)))
1830 goto finally;
1832 if (save(self, element, 0) < 0) {
1833 Py_DECREF(element);
1834 goto finally;
1837 Py_DECREF(element);
1840 else {
1841 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1842 PyErr_Clear();
1843 else
1844 goto finally;
1847 if (!self->bin) {
1848 if (!( name = ((PyClassObject *)class)->cl_name )) {
1849 PyErr_SetString(PicklingError, "class has no name");
1850 goto finally;
1853 if (!( module = whichmodule(class, name)))
1854 goto finally;
1857 if ((module_size = PyString_Size(module)) < 0 ||
1858 (name_size = PyString_Size(name)) < 0)
1859 goto finally;
1861 module_str = PyString_AS_STRING((PyStringObject *)module);
1862 name_str = PyString_AS_STRING((PyStringObject *)name);
1864 if (self->write_func(self, &inst, 1) < 0)
1865 goto finally;
1867 if (self->write_func(self, module_str, module_size) < 0)
1868 goto finally;
1870 if (self->write_func(self, "\n", 1) < 0)
1871 goto finally;
1873 if (self->write_func(self, name_str, name_size) < 0)
1874 goto finally;
1876 if (self->write_func(self, "\n", 1) < 0)
1877 goto finally;
1879 else if (self->write_func(self, &obj, 1) < 0) {
1880 goto finally;
1883 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
1884 state = PyObject_Call(getstate_func, empty_tuple, NULL);
1885 if (!state)
1886 goto finally;
1888 else {
1889 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1890 PyErr_Clear();
1891 else
1892 goto finally;
1894 if (!( state = PyObject_GetAttr(args, __dict___str))) {
1895 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1896 PyErr_Clear();
1897 else
1898 goto finally;
1899 res = 0;
1900 goto finally;
1904 if (!PyDict_Check(state)) {
1905 if (put2(self, args) < 0)
1906 goto finally;
1908 else {
1909 if (put(self, args) < 0)
1910 goto finally;
1913 if (save(self, state, 0) < 0)
1914 goto finally;
1916 if (self->write_func(self, &build, 1) < 0)
1917 goto finally;
1919 res = 0;
1921 finally:
1922 if (self->fast && !fast_save_leave(self, args))
1923 res = -1;
1925 Py_XDECREF(module);
1926 Py_XDECREF(class);
1927 Py_XDECREF(state);
1928 Py_XDECREF(getinitargs_func);
1929 Py_XDECREF(getstate_func);
1930 Py_XDECREF(class_args);
1932 return res;
1936 static int
1937 save_global(Picklerobject *self, PyObject *args, PyObject *name)
1939 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
1940 char *name_str, *module_str;
1941 int module_size, name_size, res = -1;
1943 static char global = GLOBAL;
1945 if (name) {
1946 global_name = name;
1947 Py_INCREF(global_name);
1949 else {
1950 if (!( global_name = PyObject_GetAttr(args, __name___str)))
1951 goto finally;
1954 if (!( module = whichmodule(args, global_name)))
1955 goto finally;
1957 if ((module_size = PyString_Size(module)) < 0 ||
1958 (name_size = PyString_Size(global_name)) < 0)
1959 goto finally;
1961 module_str = PyString_AS_STRING((PyStringObject *)module);
1962 name_str = PyString_AS_STRING((PyStringObject *)global_name);
1964 /* XXX This can be doing a relative import. Clearly it shouldn't,
1965 but I don't know how to stop it. :-( */
1966 mod = PyImport_ImportModule(module_str);
1967 if (mod == NULL) {
1968 cPickle_ErrFormat(PicklingError,
1969 "Can't pickle %s: import of module %s "
1970 "failed",
1971 "OS", args, module);
1972 goto finally;
1974 klass = PyObject_GetAttrString(mod, name_str);
1975 if (klass == NULL) {
1976 cPickle_ErrFormat(PicklingError,
1977 "Can't pickle %s: attribute lookup %s.%s "
1978 "failed",
1979 "OSS", args, module, global_name);
1980 goto finally;
1982 if (klass != args) {
1983 Py_DECREF(klass);
1984 cPickle_ErrFormat(PicklingError,
1985 "Can't pickle %s: it's not the same object "
1986 "as %s.%s",
1987 "OSS", args, module, global_name);
1988 goto finally;
1990 Py_DECREF(klass);
1992 if (self->proto >= 2) {
1993 /* See whether this is in the extension registry, and if
1994 * so generate an EXT opcode.
1996 PyObject *py_code; /* extension code as Python object */
1997 long code; /* extension code as C value */
1998 char c_str[5];
1999 int n;
2001 PyTuple_SET_ITEM(two_tuple, 0, module);
2002 PyTuple_SET_ITEM(two_tuple, 1, global_name);
2003 py_code = PyDict_GetItem(extension_registry, two_tuple);
2004 if (py_code == NULL)
2005 goto gen_global; /* not registered */
2007 /* Verify py_code has the right type and value. */
2008 if (!PyInt_Check(py_code)) {
2009 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2010 "extension code %s isn't an integer",
2011 "OO", args, py_code);
2012 goto finally;
2014 code = PyInt_AS_LONG(py_code);
2015 if (code <= 0 || code > 0x7fffffffL) {
2016 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2017 "extension code %ld is out of range",
2018 "Ol", args, code);
2019 goto finally;
2022 /* Generate an EXT opcode. */
2023 if (code <= 0xff) {
2024 c_str[0] = EXT1;
2025 c_str[1] = (char)code;
2026 n = 2;
2028 else if (code <= 0xffff) {
2029 c_str[0] = EXT2;
2030 c_str[1] = (char)(code & 0xff);
2031 c_str[2] = (char)((code >> 8) & 0xff);
2032 n = 3;
2034 else {
2035 c_str[0] = EXT4;
2036 c_str[1] = (char)(code & 0xff);
2037 c_str[2] = (char)((code >> 8) & 0xff);
2038 c_str[3] = (char)((code >> 16) & 0xff);
2039 c_str[4] = (char)((code >> 24) & 0xff);
2040 n = 5;
2043 if (self->write_func(self, c_str, n) >= 0)
2044 res = 0;
2045 goto finally; /* and don't memoize */
2048 gen_global:
2049 if (self->write_func(self, &global, 1) < 0)
2050 goto finally;
2052 if (self->write_func(self, module_str, module_size) < 0)
2053 goto finally;
2055 if (self->write_func(self, "\n", 1) < 0)
2056 goto finally;
2058 if (self->write_func(self, name_str, name_size) < 0)
2059 goto finally;
2061 if (self->write_func(self, "\n", 1) < 0)
2062 goto finally;
2064 if (put(self, args) < 0)
2065 goto finally;
2067 res = 0;
2069 finally:
2070 Py_XDECREF(module);
2071 Py_XDECREF(global_name);
2072 Py_XDECREF(mod);
2074 return res;
2077 static int
2078 save_pers(Picklerobject *self, PyObject *args, PyObject *f)
2080 PyObject *pid = 0;
2081 int size, res = -1;
2083 static char persid = PERSID, binpersid = BINPERSID;
2085 Py_INCREF(args);
2086 ARG_TUP(self, args);
2087 if (self->arg) {
2088 pid = PyObject_Call(f, self->arg, NULL);
2089 FREE_ARG_TUP(self);
2091 if (! pid) return -1;
2093 if (pid != Py_None) {
2094 if (!self->bin) {
2095 if (!PyString_Check(pid)) {
2096 PyErr_SetString(PicklingError,
2097 "persistent id must be string");
2098 goto finally;
2101 if (self->write_func(self, &persid, 1) < 0)
2102 goto finally;
2104 if ((size = PyString_Size(pid)) < 0)
2105 goto finally;
2107 if (self->write_func(self,
2108 PyString_AS_STRING(
2109 (PyStringObject *)pid),
2110 size) < 0)
2111 goto finally;
2113 if (self->write_func(self, "\n", 1) < 0)
2114 goto finally;
2116 res = 1;
2117 goto finally;
2119 else if (save(self, pid, 1) >= 0) {
2120 if (self->write_func(self, &binpersid, 1) < 0)
2121 res = -1;
2122 else
2123 res = 1;
2126 goto finally;
2129 res = 0;
2131 finally:
2132 Py_XDECREF(pid);
2134 return res;
2137 /* We're saving ob, and args is the 2-thru-5 tuple returned by the
2138 * appropriate __reduce__ method for ob.
2140 static int
2141 save_reduce(Picklerobject *self, PyObject *args, PyObject *ob)
2143 PyObject *callable;
2144 PyObject *argtup;
2145 PyObject *state = NULL;
2146 PyObject *listitems = NULL;
2147 PyObject *dictitems = NULL;
2149 int use_newobj = self->proto >= 2;
2151 static char reduce = REDUCE;
2152 static char build = BUILD;
2153 static char newobj = NEWOBJ;
2155 if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2156 &callable,
2157 &argtup,
2158 &state,
2159 &listitems,
2160 &dictitems))
2161 return -1;
2163 if (!PyTuple_Check(argtup)) {
2164 PyErr_SetString(PicklingError,
2165 "args from reduce() should be a tuple");
2166 return -1;
2169 if (state == Py_None)
2170 state = NULL;
2171 if (listitems == Py_None)
2172 listitems = NULL;
2173 if (dictitems == Py_None)
2174 dictitems = NULL;
2176 /* Protocol 2 special case: if callable's name is __newobj__, use
2177 * NEWOBJ. This consumes a lot of code.
2179 if (use_newobj) {
2180 PyObject *temp = PyObject_GetAttr(callable, __name___str);
2182 if (temp == NULL) {
2183 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2184 PyErr_Clear();
2185 else
2186 return -1;
2187 use_newobj = 0;
2189 else {
2190 use_newobj = PyString_Check(temp) &&
2191 strcmp(PyString_AS_STRING(temp),
2192 "__newobj__") == 0;
2193 Py_DECREF(temp);
2196 if (use_newobj) {
2197 PyObject *cls;
2198 PyObject *newargtup;
2199 int n, i;
2201 /* Sanity checks. */
2202 n = PyTuple_Size(argtup);
2203 if (n < 1) {
2204 PyErr_SetString(PicklingError, "__newobj__ arglist "
2205 "is empty");
2206 return -1;
2209 cls = PyTuple_GET_ITEM(argtup, 0);
2210 if (! PyObject_HasAttrString(cls, "__new__")) {
2211 PyErr_SetString(PicklingError, "args[0] from "
2212 "__newobj__ args has no __new__");
2213 return -1;
2216 /* XXX How could ob be NULL? */
2217 if (ob != NULL) {
2218 PyObject *ob_dot_class;
2220 ob_dot_class = PyObject_GetAttr(ob, __class___str);
2221 if (ob_dot_class == NULL) {
2222 if (PyErr_ExceptionMatches(
2223 PyExc_AttributeError))
2224 PyErr_Clear();
2225 else
2226 return -1;
2228 i = ob_dot_class != cls; /* true iff a problem */
2229 Py_XDECREF(ob_dot_class);
2230 if (i) {
2231 PyErr_SetString(PicklingError, "args[0] from "
2232 "__newobj__ args has the wrong class");
2233 return -1;
2237 /* Save the class and its __new__ arguments. */
2238 if (save(self, cls, 0) < 0)
2239 return -1;
2241 newargtup = PyTuple_New(n-1); /* argtup[1:] */
2242 if (newargtup == NULL)
2243 return -1;
2244 for (i = 1; i < n; ++i) {
2245 PyObject *temp = PyTuple_GET_ITEM(argtup, i);
2246 Py_INCREF(temp);
2247 PyTuple_SET_ITEM(newargtup, i-1, temp);
2249 i = save(self, newargtup, 0) < 0;
2250 Py_DECREF(newargtup);
2251 if (i < 0)
2252 return -1;
2254 /* Add NEWOBJ opcode. */
2255 if (self->write_func(self, &newobj, 1) < 0)
2256 return -1;
2258 else {
2259 /* Not using NEWOBJ. */
2260 if (save(self, callable, 0) < 0 ||
2261 save(self, argtup, 0) < 0 ||
2262 self->write_func(self, &reduce, 1) < 0)
2263 return -1;
2266 /* Memoize. */
2267 /* XXX How can ob be NULL? */
2268 if (ob != NULL) {
2269 if (state && !PyDict_Check(state)) {
2270 if (put2(self, ob) < 0)
2271 return -1;
2273 else if (put(self, ob) < 0)
2274 return -1;
2278 if (listitems && batch_list(self, listitems) < 0)
2279 return -1;
2281 if (dictitems && batch_dict(self, dictitems) < 0)
2282 return -1;
2284 if (state) {
2285 if (save(self, state, 0) < 0 ||
2286 self->write_func(self, &build, 1) < 0)
2287 return -1;
2290 return 0;
2293 static int
2294 save(Picklerobject *self, PyObject *args, int pers_save)
2296 PyTypeObject *type;
2297 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
2298 PyObject *arg_tup;
2299 int res = -1;
2300 int tmp, size;
2302 if (self->nesting++ > Py_GetRecursionLimit()){
2303 PyErr_SetString(PyExc_RuntimeError,
2304 "maximum recursion depth exceeded");
2305 goto finally;
2308 if (!pers_save && self->pers_func) {
2309 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2310 res = tmp;
2311 goto finally;
2315 if (args == Py_None) {
2316 res = save_none(self, args);
2317 goto finally;
2320 type = args->ob_type;
2322 switch (type->tp_name[0]) {
2323 case 'b':
2324 if (args == Py_False || args == Py_True) {
2325 res = save_bool(self, args);
2326 goto finally;
2328 break;
2329 case 'i':
2330 if (type == &PyInt_Type) {
2331 res = save_int(self, args);
2332 goto finally;
2334 break;
2336 case 'l':
2337 if (type == &PyLong_Type) {
2338 res = save_long(self, args);
2339 goto finally;
2341 break;
2343 case 'f':
2344 if (type == &PyFloat_Type) {
2345 res = save_float(self, args);
2346 goto finally;
2348 break;
2350 case 't':
2351 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2352 res = save_tuple(self, args);
2353 goto finally;
2355 break;
2357 case 's':
2358 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
2359 res = save_string(self, args, 0);
2360 goto finally;
2363 #ifdef Py_USING_UNICODE
2364 case 'u':
2365 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
2366 res = save_unicode(self, args, 0);
2367 goto finally;
2369 #endif
2372 if (args->ob_refcnt > 1) {
2373 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
2374 goto finally;
2376 if (PyDict_GetItem(self->memo, py_ob_id)) {
2377 if (get(self, py_ob_id) < 0)
2378 goto finally;
2380 res = 0;
2381 goto finally;
2385 switch (type->tp_name[0]) {
2386 case 's':
2387 if (type == &PyString_Type) {
2388 res = save_string(self, args, 1);
2389 goto finally;
2391 break;
2393 #ifdef Py_USING_UNICODE
2394 case 'u':
2395 if (type == &PyUnicode_Type) {
2396 res = save_unicode(self, args, 1);
2397 goto finally;
2399 break;
2400 #endif
2402 case 't':
2403 if (type == &PyTuple_Type) {
2404 res = save_tuple(self, args);
2405 goto finally;
2407 if (type == &PyType_Type) {
2408 res = save_global(self, args, NULL);
2409 goto finally;
2411 break;
2413 case 'l':
2414 if (type == &PyList_Type) {
2415 res = save_list(self, args);
2416 goto finally;
2418 break;
2420 case 'd':
2421 if (type == &PyDict_Type) {
2422 res = save_dict(self, args);
2423 goto finally;
2425 break;
2427 case 'i':
2428 if (type == &PyInstance_Type) {
2429 res = save_inst(self, args);
2430 goto finally;
2432 break;
2434 case 'c':
2435 if (type == &PyClass_Type) {
2436 res = save_global(self, args, NULL);
2437 goto finally;
2439 break;
2441 case 'f':
2442 if (type == &PyFunction_Type) {
2443 res = save_global(self, args, NULL);
2444 if (res && PyErr_ExceptionMatches(PickleError)) {
2445 /* fall back to reduce */
2446 PyErr_Clear();
2447 break;
2449 goto finally;
2451 break;
2453 case 'b':
2454 if (type == &PyCFunction_Type) {
2455 res = save_global(self, args, NULL);
2456 goto finally;
2460 if (!pers_save && self->inst_pers_func) {
2461 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2462 res = tmp;
2463 goto finally;
2467 if (PyType_IsSubtype(type, &PyType_Type)) {
2468 res = save_global(self, args, NULL);
2469 goto finally;
2472 /* Get a reduction callable, and call it. This may come from
2473 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
2474 * or the object's __reduce__ method.
2476 __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2477 if (__reduce__ != NULL) {
2478 Py_INCREF(__reduce__);
2479 Py_INCREF(args);
2480 ARG_TUP(self, args);
2481 if (self->arg) {
2482 t = PyObject_Call(__reduce__, self->arg, NULL);
2483 FREE_ARG_TUP(self);
2486 else {
2487 /* Check for a __reduce_ex__ method. */
2488 __reduce__ = PyObject_GetAttr(args, __reduce_ex___str);
2489 if (__reduce__ != NULL) {
2490 t = PyInt_FromLong(self->proto);
2491 if (t != NULL) {
2492 ARG_TUP(self, t);
2493 t = NULL;
2494 if (self->arg) {
2495 t = PyObject_Call(__reduce__,
2496 self->arg, NULL);
2497 FREE_ARG_TUP(self);
2501 else {
2502 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2503 PyErr_Clear();
2504 else
2505 goto finally;
2506 /* Check for a __reduce__ method. */
2507 __reduce__ = PyObject_GetAttr(args, __reduce___str);
2508 if (__reduce__ != NULL) {
2509 t = PyObject_Call(__reduce__,
2510 empty_tuple, NULL);
2512 else {
2513 PyErr_SetObject(UnpickleableError, args);
2514 goto finally;
2519 if (t == NULL)
2520 goto finally;
2522 if (PyString_Check(t)) {
2523 res = save_global(self, args, t);
2524 goto finally;
2527 if (! PyTuple_Check(t)) {
2528 cPickle_ErrFormat(PicklingError, "Value returned by "
2529 "%s must be string or tuple",
2530 "O", __reduce__);
2531 goto finally;
2534 size = PyTuple_Size(t);
2535 if (size < 2 || size > 5) {
2536 cPickle_ErrFormat(PicklingError, "tuple returned by "
2537 "%s must contain 2 through 5 elements",
2538 "O", __reduce__);
2539 goto finally;
2542 arg_tup = PyTuple_GET_ITEM(t, 1);
2543 if (!(PyTuple_Check(arg_tup) || arg_tup == Py_None)) {
2544 cPickle_ErrFormat(PicklingError, "Second element of "
2545 "tuple returned by %s must be a tuple",
2546 "O", __reduce__);
2547 goto finally;
2550 res = save_reduce(self, t, args);
2552 finally:
2553 self->nesting--;
2554 Py_XDECREF(py_ob_id);
2555 Py_XDECREF(__reduce__);
2556 Py_XDECREF(t);
2558 return res;
2562 static int
2563 dump(Picklerobject *self, PyObject *args)
2565 static char stop = STOP;
2567 if (self->proto >= 2) {
2568 char bytes[2];
2570 bytes[0] = PROTO;
2571 assert(self->proto >= 0 && self->proto < 256);
2572 bytes[1] = (char)self->proto;
2573 if (self->write_func(self, bytes, 2) < 0)
2574 return -1;
2577 if (save(self, args, 0) < 0)
2578 return -1;
2580 if (self->write_func(self, &stop, 1) < 0)
2581 return -1;
2583 if (self->write_func(self, NULL, 0) < 0)
2584 return -1;
2586 return 0;
2589 static PyObject *
2590 Pickle_clear_memo(Picklerobject *self, PyObject *args)
2592 if (self->memo)
2593 PyDict_Clear(self->memo);
2594 Py_INCREF(Py_None);
2595 return Py_None;
2598 static PyObject *
2599 Pickle_getvalue(Picklerobject *self, PyObject *args)
2601 int l, i, rsize, ssize, clear=1, lm;
2602 long ik;
2603 PyObject *k, *r;
2604 char *s, *p, *have_get;
2605 Pdata *data;
2607 /* Can be called by Python code or C code */
2608 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
2609 return NULL;
2611 /* Check to make sure we are based on a list */
2612 if (! Pdata_Check(self->file)) {
2613 PyErr_SetString(PicklingError,
2614 "Attempt to getvalue() a non-list-based pickler");
2615 return NULL;
2618 /* flush write buffer */
2619 if (write_other(self, NULL, 0) < 0) return NULL;
2621 data=(Pdata*)self->file;
2622 l=data->length;
2624 /* set up an array to hold get/put status */
2625 lm = PyDict_Size(self->memo);
2626 if (lm < 0) return NULL;
2627 lm++;
2628 have_get = malloc(lm);
2629 if (have_get == NULL) return PyErr_NoMemory();
2630 memset(have_get, 0, lm);
2632 /* Scan for gets. */
2633 for (rsize = 0, i = l; --i >= 0; ) {
2634 k = data->data[i];
2636 if (PyString_Check(k))
2637 rsize += PyString_GET_SIZE(k);
2639 else if (PyInt_Check(k)) { /* put */
2640 ik = PyInt_AS_LONG((PyIntObject*)k);
2641 if (ik >= lm || ik == 0) {
2642 PyErr_SetString(PicklingError,
2643 "Invalid get data");
2644 goto err;
2646 if (have_get[ik]) /* with matching get */
2647 rsize += ik < 256 ? 2 : 5;
2650 else if (! (PyTuple_Check(k) &&
2651 PyTuple_GET_SIZE(k) == 2 &&
2652 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
2654 PyErr_SetString(PicklingError,
2655 "Unexpected data in internal list");
2656 goto err;
2659 else { /* put */
2660 ik = PyInt_AS_LONG((PyIntObject *)k);
2661 if (ik >= lm || ik == 0) {
2662 PyErr_SetString(PicklingError,
2663 "Invalid get data");
2664 return NULL;
2666 have_get[ik] = 1;
2667 rsize += ik < 256 ? 2 : 5;
2671 /* Now generate the result */
2672 r = PyString_FromStringAndSize(NULL, rsize);
2673 if (r == NULL) goto err;
2674 s = PyString_AS_STRING((PyStringObject *)r);
2676 for (i = 0; i < l; i++) {
2677 k = data->data[i];
2679 if (PyString_Check(k)) {
2680 ssize = PyString_GET_SIZE(k);
2681 if (ssize) {
2682 p=PyString_AS_STRING((PyStringObject *)k);
2683 while (--ssize >= 0)
2684 *s++ = *p++;
2688 else if (PyTuple_Check(k)) { /* get */
2689 ik = PyInt_AS_LONG((PyIntObject *)
2690 PyTuple_GET_ITEM(k, 0));
2691 if (ik < 256) {
2692 *s++ = BINGET;
2693 *s++ = (int)(ik & 0xff);
2695 else {
2696 *s++ = LONG_BINGET;
2697 *s++ = (int)(ik & 0xff);
2698 *s++ = (int)((ik >> 8) & 0xff);
2699 *s++ = (int)((ik >> 16) & 0xff);
2700 *s++ = (int)((ik >> 24) & 0xff);
2704 else { /* put */
2705 ik = PyInt_AS_LONG((PyIntObject*)k);
2707 if (have_get[ik]) { /* with matching get */
2708 if (ik < 256) {
2709 *s++ = BINPUT;
2710 *s++ = (int)(ik & 0xff);
2712 else {
2713 *s++ = LONG_BINPUT;
2714 *s++ = (int)(ik & 0xff);
2715 *s++ = (int)((ik >> 8) & 0xff);
2716 *s++ = (int)((ik >> 16) & 0xff);
2717 *s++ = (int)((ik >> 24) & 0xff);
2723 if (clear) {
2724 PyDict_Clear(self->memo);
2725 Pdata_clear(data, 0);
2728 free(have_get);
2729 return r;
2730 err:
2731 free(have_get);
2732 return NULL;
2735 static PyObject *
2736 Pickler_dump(Picklerobject *self, PyObject *args)
2738 PyObject *ob;
2739 int get=0;
2741 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
2742 return NULL;
2744 if (dump(self, ob) < 0)
2745 return NULL;
2747 if (get) return Pickle_getvalue(self, NULL);
2749 /* XXX Why does dump() return self? */
2750 Py_INCREF(self);
2751 return (PyObject*)self;
2755 static struct PyMethodDef Pickler_methods[] =
2757 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
2758 PyDoc_STR("dump(object) -- "
2759 "Write an object in pickle format to the object's pickle stream")},
2760 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
2761 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
2762 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
2763 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
2764 {NULL, NULL} /* sentinel */
2768 static Picklerobject *
2769 newPicklerobject(PyObject *file, int proto)
2771 Picklerobject *self;
2773 if (proto < 0)
2774 proto = HIGHEST_PROTOCOL;
2775 if (proto > HIGHEST_PROTOCOL) {
2776 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
2777 "the highest available protocol is %d",
2778 proto, HIGHEST_PROTOCOL);
2779 return NULL;
2782 self = PyObject_GC_New(Picklerobject, &Picklertype);
2783 if (self == NULL)
2784 return NULL;
2785 self->proto = proto;
2786 self->bin = proto > 0;
2787 self->fp = NULL;
2788 self->write = NULL;
2789 self->memo = NULL;
2790 self->arg = NULL;
2791 self->pers_func = NULL;
2792 self->inst_pers_func = NULL;
2793 self->write_buf = NULL;
2794 self->fast = 0;
2795 self->nesting = 0;
2796 self->fast_container = 0;
2797 self->fast_memo = NULL;
2798 self->buf_size = 0;
2799 self->dispatch_table = NULL;
2801 self->file = NULL;
2802 if (file)
2803 Py_INCREF(file);
2804 else {
2805 file = Pdata_New();
2806 if (file == NULL)
2807 goto err;
2809 self->file = file;
2811 if (!( self->memo = PyDict_New()))
2812 goto err;
2814 if (PyFile_Check(file)) {
2815 self->fp = PyFile_AsFile(file);
2816 if (self->fp == NULL) {
2817 PyErr_SetString(PyExc_ValueError,
2818 "I/O operation on closed file");
2819 goto err;
2821 self->write_func = write_file;
2823 else if (PycStringIO_OutputCheck(file)) {
2824 self->write_func = write_cStringIO;
2826 else if (file == Py_None) {
2827 self->write_func = write_none;
2829 else {
2830 self->write_func = write_other;
2832 if (! Pdata_Check(file)) {
2833 self->write = PyObject_GetAttr(file, write_str);
2834 if (!self->write) {
2835 PyErr_Clear();
2836 PyErr_SetString(PyExc_TypeError,
2837 "argument must have 'write' "
2838 "attribute");
2839 goto err;
2843 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
2844 if (self->write_buf == NULL) {
2845 PyErr_NoMemory();
2846 goto err;
2850 if (PyEval_GetRestricted()) {
2851 /* Restricted execution, get private tables */
2852 PyObject *m = PyImport_Import(copy_reg_str);
2854 if (m == NULL)
2855 goto err;
2856 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
2857 Py_DECREF(m);
2858 if (self->dispatch_table == NULL)
2859 goto err;
2861 else {
2862 self->dispatch_table = dispatch_table;
2863 Py_INCREF(dispatch_table);
2865 PyObject_GC_Track(self);
2867 return self;
2869 err:
2870 Py_DECREF(self);
2871 return NULL;
2875 static PyObject *
2876 get_Pickler(PyObject *self, PyObject *args, PyObject *kwds)
2878 static char *kwlist[] = {"file", "protocol", NULL};
2879 PyObject *file = NULL;
2880 int proto = 0;
2882 /* XXX
2883 * The documented signature is Pickler(file, protocol=0), but this
2884 * accepts Pickler() and Pickler(integer) too. The meaning then
2885 * is clear as mud, undocumented, and not supported by pickle.py.
2886 * I'm told Zope uses this, but I haven't traced into this code
2887 * far enough to figure out what it means.
2889 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
2890 PyErr_Clear();
2891 proto = 0;
2892 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler",
2893 kwlist, &file, &proto))
2894 return NULL;
2896 return (PyObject *)newPicklerobject(file, proto);
2900 static void
2901 Pickler_dealloc(Picklerobject *self)
2903 PyObject_GC_UnTrack(self);
2904 Py_XDECREF(self->write);
2905 Py_XDECREF(self->memo);
2906 Py_XDECREF(self->fast_memo);
2907 Py_XDECREF(self->arg);
2908 Py_XDECREF(self->file);
2909 Py_XDECREF(self->pers_func);
2910 Py_XDECREF(self->inst_pers_func);
2911 Py_XDECREF(self->dispatch_table);
2912 PyMem_Free(self->write_buf);
2913 self->ob_type->tp_free((PyObject *)self);
2916 static int
2917 Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)
2919 Py_VISIT(self->write);
2920 Py_VISIT(self->memo);
2921 Py_VISIT(self->fast_memo);
2922 Py_VISIT(self->arg);
2923 Py_VISIT(self->file);
2924 Py_VISIT(self->pers_func);
2925 Py_VISIT(self->inst_pers_func);
2926 Py_VISIT(self->dispatch_table);
2927 return 0;
2930 static int
2931 Pickler_clear(Picklerobject *self)
2933 Py_CLEAR(self->write);
2934 Py_CLEAR(self->memo);
2935 Py_CLEAR(self->fast_memo);
2936 Py_CLEAR(self->arg);
2937 Py_CLEAR(self->file);
2938 Py_CLEAR(self->pers_func);
2939 Py_CLEAR(self->inst_pers_func);
2940 Py_CLEAR(self->dispatch_table);
2941 return 0;
2944 static PyObject *
2945 Pickler_get_pers_func(Picklerobject *p)
2947 if (p->pers_func == NULL)
2948 PyErr_SetString(PyExc_AttributeError, "persistent_id");
2949 else
2950 Py_INCREF(p->pers_func);
2951 return p->pers_func;
2954 static int
2955 Pickler_set_pers_func(Picklerobject *p, PyObject *v)
2957 if (v == NULL) {
2958 PyErr_SetString(PyExc_TypeError,
2959 "attribute deletion is not supported");
2960 return -1;
2962 Py_XDECREF(p->pers_func);
2963 Py_INCREF(v);
2964 p->pers_func = v;
2965 return 0;
2968 static int
2969 Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
2971 if (v == NULL) {
2972 PyErr_SetString(PyExc_TypeError,
2973 "attribute deletion is not supported");
2974 return -1;
2976 Py_XDECREF(p->inst_pers_func);
2977 Py_INCREF(v);
2978 p->inst_pers_func = v;
2979 return 0;
2982 static PyObject *
2983 Pickler_get_memo(Picklerobject *p)
2985 if (p->memo == NULL)
2986 PyErr_SetString(PyExc_AttributeError, "memo");
2987 else
2988 Py_INCREF(p->memo);
2989 return p->memo;
2992 static int
2993 Pickler_set_memo(Picklerobject *p, PyObject *v)
2995 if (v == NULL) {
2996 PyErr_SetString(PyExc_TypeError,
2997 "attribute deletion is not supported");
2998 return -1;
3000 if (!PyDict_Check(v)) {
3001 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
3002 return -1;
3004 Py_XDECREF(p->memo);
3005 Py_INCREF(v);
3006 p->memo = v;
3007 return 0;
3010 static PyObject *
3011 Pickler_get_error(Picklerobject *p)
3013 /* why is this an attribute on the Pickler? */
3014 Py_INCREF(PicklingError);
3015 return PicklingError;
3018 static PyMemberDef Pickler_members[] = {
3019 {"binary", T_INT, offsetof(Picklerobject, bin)},
3020 {"fast", T_INT, offsetof(Picklerobject, fast)},
3021 {NULL}
3024 static PyGetSetDef Pickler_getsets[] = {
3025 {"persistent_id", (getter)Pickler_get_pers_func,
3026 (setter)Pickler_set_pers_func},
3027 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
3028 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
3029 {"PicklingError", (getter)Pickler_get_error, NULL},
3030 {NULL}
3033 PyDoc_STRVAR(Picklertype__doc__,
3034 "Objects that know how to pickle objects\n");
3036 static PyTypeObject Picklertype = {
3037 PyObject_HEAD_INIT(NULL)
3038 0, /*ob_size*/
3039 "cPickle.Pickler", /*tp_name*/
3040 sizeof(Picklerobject), /*tp_basicsize*/
3042 (destructor)Pickler_dealloc, /* tp_dealloc */
3043 0, /* tp_print */
3044 0, /* tp_getattr */
3045 0, /* tp_setattr */
3046 0, /* tp_compare */
3047 0, /* tp_repr */
3048 0, /* tp_as_number */
3049 0, /* tp_as_sequence */
3050 0, /* tp_as_mapping */
3051 0, /* tp_hash */
3052 0, /* tp_call */
3053 0, /* tp_str */
3054 PyObject_GenericGetAttr, /* tp_getattro */
3055 PyObject_GenericSetAttr, /* tp_setattro */
3056 0, /* tp_as_buffer */
3057 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
3058 Picklertype__doc__, /* tp_doc */
3059 (traverseproc)Pickler_traverse, /* tp_traverse */
3060 (inquiry)Pickler_clear, /* tp_clear */
3061 0, /* tp_richcompare */
3062 0, /* tp_weaklistoffset */
3063 0, /* tp_iter */
3064 0, /* tp_iternext */
3065 Pickler_methods, /* tp_methods */
3066 Pickler_members, /* tp_members */
3067 Pickler_getsets, /* tp_getset */
3070 static PyObject *
3071 find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
3073 PyObject *global = 0, *module;
3075 if (fc) {
3076 if (fc==Py_None) {
3077 PyErr_SetString(UnpicklingError, "Global and instance "
3078 "pickles are not supported.");
3079 return NULL;
3081 return PyObject_CallFunctionObjArgs(fc, py_module_name,
3082 py_global_name, NULL);
3085 module = PySys_GetObject("modules");
3086 if (module == NULL)
3087 return NULL;
3089 module = PyDict_GetItem(module, py_module_name);
3090 if (module == NULL) {
3091 module = PyImport_Import(py_module_name);
3092 if (!module)
3093 return NULL;
3094 global = PyObject_GetAttr(module, py_global_name);
3095 Py_DECREF(module);
3097 else
3098 global = PyObject_GetAttr(module, py_global_name);
3099 return global;
3102 static int
3103 marker(Unpicklerobject *self)
3105 if (self->num_marks < 1) {
3106 PyErr_SetString(UnpicklingError, "could not find MARK");
3107 return -1;
3110 return self->marks[--self->num_marks];
3114 static int
3115 load_none(Unpicklerobject *self)
3117 PDATA_APPEND(self->stack, Py_None, -1);
3118 return 0;
3121 static int
3122 bad_readline(void)
3124 PyErr_SetString(UnpicklingError, "pickle data was truncated");
3125 return -1;
3128 static int
3129 load_int(Unpicklerobject *self)
3131 PyObject *py_int = 0;
3132 char *endptr, *s;
3133 int len, res = -1;
3134 long l;
3136 if ((len = self->readline_func(self, &s)) < 0) return -1;
3137 if (len < 2) return bad_readline();
3138 if (!( s=pystrndup(s,len))) return -1;
3140 errno = 0;
3141 l = strtol(s, &endptr, 0);
3143 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
3144 /* Hm, maybe we've got something long. Let's try reading
3145 it as a Python long object. */
3146 errno = 0;
3147 py_int = PyLong_FromString(s, NULL, 0);
3148 if (py_int == NULL) {
3149 PyErr_SetString(PyExc_ValueError,
3150 "could not convert string to int");
3151 goto finally;
3154 else {
3155 if (len == 3 && (l == 0 || l == 1)) {
3156 if (!( py_int = PyBool_FromLong(l))) goto finally;
3158 else {
3159 if (!( py_int = PyInt_FromLong(l))) goto finally;
3163 free(s);
3164 PDATA_PUSH(self->stack, py_int, -1);
3165 return 0;
3167 finally:
3168 free(s);
3170 return res;
3173 static int
3174 load_bool(Unpicklerobject *self, PyObject *boolean)
3176 assert(boolean == Py_True || boolean == Py_False);
3177 PDATA_APPEND(self->stack, boolean, -1);
3178 return 0;
3181 /* s contains x bytes of a little-endian integer. Return its value as a
3182 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3183 * int, but when x is 4 it's a signed one. This is an historical source
3184 * of x-platform bugs.
3186 static long
3187 calc_binint(char *s, int x)
3189 unsigned char c;
3190 int i;
3191 long l;
3193 for (i = 0, l = 0L; i < x; i++) {
3194 c = (unsigned char)s[i];
3195 l |= (long)c << (i * 8);
3197 #if SIZEOF_LONG > 4
3198 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3199 * is signed, so on a box with longs bigger than 4 bytes we need
3200 * to extend a BININT's sign bit to the full width.
3202 if (x == 4 && l & (1L << 31))
3203 l |= (~0L) << 32;
3204 #endif
3205 return l;
3209 static int
3210 load_binintx(Unpicklerobject *self, char *s, int x)
3212 PyObject *py_int = 0;
3213 long l;
3215 l = calc_binint(s, x);
3217 if (!( py_int = PyInt_FromLong(l)))
3218 return -1;
3220 PDATA_PUSH(self->stack, py_int, -1);
3221 return 0;
3225 static int
3226 load_binint(Unpicklerobject *self)
3228 char *s;
3230 if (self->read_func(self, &s, 4) < 0)
3231 return -1;
3233 return load_binintx(self, s, 4);
3237 static int
3238 load_binint1(Unpicklerobject *self)
3240 char *s;
3242 if (self->read_func(self, &s, 1) < 0)
3243 return -1;
3245 return load_binintx(self, s, 1);
3249 static int
3250 load_binint2(Unpicklerobject *self)
3252 char *s;
3254 if (self->read_func(self, &s, 2) < 0)
3255 return -1;
3257 return load_binintx(self, s, 2);
3260 static int
3261 load_long(Unpicklerobject *self)
3263 PyObject *l = 0;
3264 char *end, *s;
3265 int len, res = -1;
3267 if ((len = self->readline_func(self, &s)) < 0) return -1;
3268 if (len < 2) return bad_readline();
3269 if (!( s=pystrndup(s,len))) return -1;
3271 if (!( l = PyLong_FromString(s, &end, 0)))
3272 goto finally;
3274 free(s);
3275 PDATA_PUSH(self->stack, l, -1);
3276 return 0;
3278 finally:
3279 free(s);
3281 return res;
3284 /* 'size' bytes contain the # of bytes of little-endian 256's-complement
3285 * data following.
3287 static int
3288 load_counted_long(Unpicklerobject *self, int size)
3290 Py_ssize_t i;
3291 char *nbytes;
3292 unsigned char *pdata;
3293 PyObject *along;
3295 assert(size == 1 || size == 4);
3296 i = self->read_func(self, &nbytes, size);
3297 if (i < 0) return -1;
3299 size = calc_binint(nbytes, size);
3300 if (size < 0) {
3301 /* Corrupt or hostile pickle -- we never write one like
3302 * this.
3304 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
3305 "byte count");
3306 return -1;
3309 if (size == 0)
3310 along = PyLong_FromLong(0L);
3311 else {
3312 /* Read the raw little-endian bytes & convert. */
3313 i = self->read_func(self, (char **)&pdata, size);
3314 if (i < 0) return -1;
3315 along = _PyLong_FromByteArray(pdata, (size_t)size,
3316 1 /* little endian */, 1 /* signed */);
3318 if (along == NULL)
3319 return -1;
3320 PDATA_PUSH(self->stack, along, -1);
3321 return 0;
3324 static int
3325 load_float(Unpicklerobject *self)
3327 PyObject *py_float = 0;
3328 char *endptr, *s;
3329 int len, res = -1;
3330 double d;
3332 if ((len = self->readline_func(self, &s)) < 0) return -1;
3333 if (len < 2) return bad_readline();
3334 if (!( s=pystrndup(s,len))) return -1;
3336 errno = 0;
3337 d = PyOS_ascii_strtod(s, &endptr);
3339 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
3340 PyErr_SetString(PyExc_ValueError,
3341 "could not convert string to float");
3342 goto finally;
3345 if (!( py_float = PyFloat_FromDouble(d)))
3346 goto finally;
3348 free(s);
3349 PDATA_PUSH(self->stack, py_float, -1);
3350 return 0;
3352 finally:
3353 free(s);
3355 return res;
3358 static int
3359 load_binfloat(Unpicklerobject *self)
3361 PyObject *py_float;
3362 double x;
3363 char *p;
3365 if (self->read_func(self, &p, 8) < 0)
3366 return -1;
3368 x = _PyFloat_Unpack8((unsigned char *)p, 0);
3369 if (x == -1.0 && PyErr_Occurred())
3370 return -1;
3372 py_float = PyFloat_FromDouble(x);
3373 if (py_float == NULL)
3374 return -1;
3376 PDATA_PUSH(self->stack, py_float, -1);
3377 return 0;
3380 static int
3381 load_string(Unpicklerobject *self)
3383 PyObject *str = 0;
3384 int len, res = -1;
3385 char *s, *p;
3387 if ((len = self->readline_func(self, &s)) < 0) return -1;
3388 if (len < 2) return bad_readline();
3389 if (!( s=pystrndup(s,len))) return -1;
3392 /* Strip outermost quotes */
3393 while (s[len-1] <= ' ')
3394 len--;
3395 if(s[0]=='"' && s[len-1]=='"'){
3396 s[len-1] = '\0';
3397 p = s + 1 ;
3398 len -= 2;
3399 } else if(s[0]=='\'' && s[len-1]=='\''){
3400 s[len-1] = '\0';
3401 p = s + 1 ;
3402 len -= 2;
3403 } else
3404 goto insecure;
3405 /********************************************/
3407 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
3408 free(s);
3409 if (str) {
3410 PDATA_PUSH(self->stack, str, -1);
3411 res = 0;
3413 return res;
3415 insecure:
3416 free(s);
3417 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3418 return -1;
3422 static int
3423 load_binstring(Unpicklerobject *self)
3425 PyObject *py_string = 0;
3426 long l;
3427 char *s;
3429 if (self->read_func(self, &s, 4) < 0) return -1;
3431 l = calc_binint(s, 4);
3433 if (self->read_func(self, &s, l) < 0)
3434 return -1;
3436 if (!( py_string = PyString_FromStringAndSize(s, l)))
3437 return -1;
3439 PDATA_PUSH(self->stack, py_string, -1);
3440 return 0;
3444 static int
3445 load_short_binstring(Unpicklerobject *self)
3447 PyObject *py_string = 0;
3448 unsigned char l;
3449 char *s;
3451 if (self->read_func(self, &s, 1) < 0)
3452 return -1;
3454 l = (unsigned char)s[0];
3456 if (self->read_func(self, &s, l) < 0) return -1;
3458 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
3460 PDATA_PUSH(self->stack, py_string, -1);
3461 return 0;
3465 #ifdef Py_USING_UNICODE
3466 static int
3467 load_unicode(Unpicklerobject *self)
3469 PyObject *str = 0;
3470 int len, res = -1;
3471 char *s;
3473 if ((len = self->readline_func(self, &s)) < 0) return -1;
3474 if (len < 1) return bad_readline();
3476 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
3477 goto finally;
3479 PDATA_PUSH(self->stack, str, -1);
3480 return 0;
3482 finally:
3483 return res;
3485 #endif
3488 #ifdef Py_USING_UNICODE
3489 static int
3490 load_binunicode(Unpicklerobject *self)
3492 PyObject *unicode;
3493 long l;
3494 char *s;
3496 if (self->read_func(self, &s, 4) < 0) return -1;
3498 l = calc_binint(s, 4);
3500 if (self->read_func(self, &s, l) < 0)
3501 return -1;
3503 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
3504 return -1;
3506 PDATA_PUSH(self->stack, unicode, -1);
3507 return 0;
3509 #endif
3512 static int
3513 load_tuple(Unpicklerobject *self)
3515 PyObject *tup;
3516 int i;
3518 if ((i = marker(self)) < 0) return -1;
3519 if (!( tup=Pdata_popTuple(self->stack, i))) return -1;
3520 PDATA_PUSH(self->stack, tup, -1);
3521 return 0;
3524 static int
3525 load_counted_tuple(Unpicklerobject *self, int len)
3527 PyObject *tup = PyTuple_New(len);
3529 if (tup == NULL)
3530 return -1;
3532 while (--len >= 0) {
3533 PyObject *element;
3535 PDATA_POP(self->stack, element);
3536 if (element == NULL)
3537 return -1;
3538 PyTuple_SET_ITEM(tup, len, element);
3540 PDATA_PUSH(self->stack, tup, -1);
3541 return 0;
3544 static int
3545 load_empty_list(Unpicklerobject *self)
3547 PyObject *list;
3549 if (!( list=PyList_New(0))) return -1;
3550 PDATA_PUSH(self->stack, list, -1);
3551 return 0;
3554 static int
3555 load_empty_dict(Unpicklerobject *self)
3557 PyObject *dict;
3559 if (!( dict=PyDict_New())) return -1;
3560 PDATA_PUSH(self->stack, dict, -1);
3561 return 0;
3565 static int
3566 load_list(Unpicklerobject *self)
3568 PyObject *list = 0;
3569 int i;
3571 if ((i = marker(self)) < 0) return -1;
3572 if (!( list=Pdata_popList(self->stack, i))) return -1;
3573 PDATA_PUSH(self->stack, list, -1);
3574 return 0;
3577 static int
3578 load_dict(Unpicklerobject *self)
3580 PyObject *dict, *key, *value;
3581 int i, j, k;
3583 if ((i = marker(self)) < 0) return -1;
3584 j=self->stack->length;
3586 if (!( dict = PyDict_New())) return -1;
3588 for (k = i+1; k < j; k += 2) {
3589 key =self->stack->data[k-1];
3590 value=self->stack->data[k ];
3591 if (PyDict_SetItem(dict, key, value) < 0) {
3592 Py_DECREF(dict);
3593 return -1;
3596 Pdata_clear(self->stack, i);
3597 PDATA_PUSH(self->stack, dict, -1);
3598 return 0;
3601 static PyObject *
3602 Instance_New(PyObject *cls, PyObject *args)
3604 PyObject *r = 0;
3606 if (PyClass_Check(cls)) {
3607 int l;
3609 if ((l=PyObject_Size(args)) < 0) goto err;
3610 if (!( l )) {
3611 PyObject *__getinitargs__;
3613 __getinitargs__ = PyObject_GetAttr(cls,
3614 __getinitargs___str);
3615 if (!__getinitargs__) {
3616 /* We have a class with no __getinitargs__,
3617 so bypass usual construction */
3618 PyObject *inst;
3620 PyErr_Clear();
3621 if (!( inst=PyInstance_NewRaw(cls, NULL)))
3622 goto err;
3623 return inst;
3625 Py_DECREF(__getinitargs__);
3628 if ((r=PyInstance_New(cls, args, NULL))) return r;
3629 else goto err;
3632 if ((r=PyObject_CallObject(cls, args))) return r;
3634 err:
3636 PyObject *tp, *v, *tb, *tmp_value;
3638 PyErr_Fetch(&tp, &v, &tb);
3639 tmp_value = v;
3640 /* NULL occurs when there was a KeyboardInterrupt */
3641 if (tmp_value == NULL)
3642 tmp_value = Py_None;
3643 if ((r = PyTuple_Pack(3, tmp_value, cls, args))) {
3644 Py_XDECREF(v);
3645 v=r;
3647 PyErr_Restore(tp,v,tb);
3649 return NULL;
3653 static int
3654 load_obj(Unpicklerobject *self)
3656 PyObject *class, *tup, *obj=0;
3657 int i;
3659 if ((i = marker(self)) < 0) return -1;
3660 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3661 PDATA_POP(self->stack, class);
3662 if (class) {
3663 obj = Instance_New(class, tup);
3664 Py_DECREF(class);
3666 Py_DECREF(tup);
3668 if (! obj) return -1;
3669 PDATA_PUSH(self->stack, obj, -1);
3670 return 0;
3674 static int
3675 load_inst(Unpicklerobject *self)
3677 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
3678 int i, len;
3679 char *s;
3681 if ((i = marker(self)) < 0) return -1;
3683 if ((len = self->readline_func(self, &s)) < 0) return -1;
3684 if (len < 2) return bad_readline();
3685 module_name = PyString_FromStringAndSize(s, len - 1);
3686 if (!module_name) return -1;
3688 if ((len = self->readline_func(self, &s)) >= 0) {
3689 if (len < 2) return bad_readline();
3690 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
3691 class = find_class(module_name, class_name,
3692 self->find_class);
3693 Py_DECREF(class_name);
3696 Py_DECREF(module_name);
3698 if (! class) return -1;
3700 if ((tup=Pdata_popTuple(self->stack, i))) {
3701 obj = Instance_New(class, tup);
3702 Py_DECREF(tup);
3704 Py_DECREF(class);
3706 if (! obj) return -1;
3708 PDATA_PUSH(self->stack, obj, -1);
3709 return 0;
3712 static int
3713 load_newobj(Unpicklerobject *self)
3715 PyObject *args = NULL;
3716 PyObject *clsraw = NULL;
3717 PyTypeObject *cls; /* clsraw cast to its true type */
3718 PyObject *obj;
3720 /* Stack is ... cls argtuple, and we want to call
3721 * cls.__new__(cls, *argtuple).
3723 PDATA_POP(self->stack, args);
3724 if (args == NULL) goto Fail;
3725 if (! PyTuple_Check(args)) {
3726 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
3727 "tuple.");
3728 goto Fail;
3731 PDATA_POP(self->stack, clsraw);
3732 cls = (PyTypeObject *)clsraw;
3733 if (cls == NULL) goto Fail;
3734 if (! PyType_Check(cls)) {
3735 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3736 "isn't a type object");
3737 goto Fail;
3739 if (cls->tp_new == NULL) {
3740 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3741 "has NULL tp_new");
3742 goto Fail;
3745 /* Call __new__. */
3746 obj = cls->tp_new(cls, args, NULL);
3747 if (obj == NULL) goto Fail;
3749 Py_DECREF(args);
3750 Py_DECREF(clsraw);
3751 PDATA_PUSH(self->stack, obj, -1);
3752 return 0;
3754 Fail:
3755 Py_XDECREF(args);
3756 Py_XDECREF(clsraw);
3757 return -1;
3760 static int
3761 load_global(Unpicklerobject *self)
3763 PyObject *class = 0, *module_name = 0, *class_name = 0;
3764 int len;
3765 char *s;
3767 if ((len = self->readline_func(self, &s)) < 0) return -1;
3768 if (len < 2) return bad_readline();
3769 module_name = PyString_FromStringAndSize(s, len - 1);
3770 if (!module_name) return -1;
3772 if ((len = self->readline_func(self, &s)) >= 0) {
3773 if (len < 2) {
3774 Py_DECREF(module_name);
3775 return bad_readline();
3777 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
3778 class = find_class(module_name, class_name,
3779 self->find_class);
3780 Py_DECREF(class_name);
3783 Py_DECREF(module_name);
3785 if (! class) return -1;
3786 PDATA_PUSH(self->stack, class, -1);
3787 return 0;
3791 static int
3792 load_persid(Unpicklerobject *self)
3794 PyObject *pid = 0;
3795 int len;
3796 char *s;
3798 if (self->pers_func) {
3799 if ((len = self->readline_func(self, &s)) < 0) return -1;
3800 if (len < 2) return bad_readline();
3802 pid = PyString_FromStringAndSize(s, len - 1);
3803 if (!pid) return -1;
3805 if (PyList_Check(self->pers_func)) {
3806 if (PyList_Append(self->pers_func, pid) < 0) {
3807 Py_DECREF(pid);
3808 return -1;
3811 else {
3812 ARG_TUP(self, pid);
3813 if (self->arg) {
3814 pid = PyObject_Call(self->pers_func, self->arg,
3815 NULL);
3816 FREE_ARG_TUP(self);
3820 if (! pid) return -1;
3822 PDATA_PUSH(self->stack, pid, -1);
3823 return 0;
3825 else {
3826 PyErr_SetString(UnpicklingError,
3827 "A load persistent id instruction was encountered,\n"
3828 "but no persistent_load function was specified.");
3829 return -1;
3833 static int
3834 load_binpersid(Unpicklerobject *self)
3836 PyObject *pid = 0;
3838 if (self->pers_func) {
3839 PDATA_POP(self->stack, pid);
3840 if (! pid) return -1;
3842 if (PyList_Check(self->pers_func)) {
3843 if (PyList_Append(self->pers_func, pid) < 0) {
3844 Py_DECREF(pid);
3845 return -1;
3848 else {
3849 ARG_TUP(self, pid);
3850 if (self->arg) {
3851 pid = PyObject_Call(self->pers_func, self->arg,
3852 NULL);
3853 FREE_ARG_TUP(self);
3855 if (! pid) return -1;
3858 PDATA_PUSH(self->stack, pid, -1);
3859 return 0;
3861 else {
3862 PyErr_SetString(UnpicklingError,
3863 "A load persistent id instruction was encountered,\n"
3864 "but no persistent_load function was specified.");
3865 return -1;
3870 static int
3871 load_pop(Unpicklerobject *self)
3873 int len;
3875 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
3877 /* Note that we split the (pickle.py) stack into two stacks,
3878 an object stack and a mark stack. We have to be clever and
3879 pop the right one. We do this by looking at the top of the
3880 mark stack.
3883 if ((self->num_marks > 0) &&
3884 (self->marks[self->num_marks - 1] == len))
3885 self->num_marks--;
3886 else {
3887 len--;
3888 Py_DECREF(self->stack->data[len]);
3889 self->stack->length=len;
3892 return 0;
3896 static int
3897 load_pop_mark(Unpicklerobject *self)
3899 int i;
3901 if ((i = marker(self)) < 0)
3902 return -1;
3904 Pdata_clear(self->stack, i);
3906 return 0;
3910 static int
3911 load_dup(Unpicklerobject *self)
3913 PyObject *last;
3914 int len;
3916 if ((len = self->stack->length) <= 0) return stackUnderflow();
3917 last=self->stack->data[len-1];
3918 Py_INCREF(last);
3919 PDATA_PUSH(self->stack, last, -1);
3920 return 0;
3924 static int
3925 load_get(Unpicklerobject *self)
3927 PyObject *py_str = 0, *value = 0;
3928 int len;
3929 char *s;
3930 int rc;
3932 if ((len = self->readline_func(self, &s)) < 0) return -1;
3933 if (len < 2) return bad_readline();
3935 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
3937 value = PyDict_GetItem(self->memo, py_str);
3938 if (! value) {
3939 PyErr_SetObject(BadPickleGet, py_str);
3940 rc = -1;
3942 else {
3943 PDATA_APPEND(self->stack, value, -1);
3944 rc = 0;
3947 Py_DECREF(py_str);
3948 return rc;
3952 static int
3953 load_binget(Unpicklerobject *self)
3955 PyObject *py_key = 0, *value = 0;
3956 unsigned char key;
3957 char *s;
3958 int rc;
3960 if (self->read_func(self, &s, 1) < 0) return -1;
3962 key = (unsigned char)s[0];
3963 if (!( py_key = PyInt_FromLong((long)key))) return -1;
3965 value = PyDict_GetItem(self->memo, py_key);
3966 if (! value) {
3967 PyErr_SetObject(BadPickleGet, py_key);
3968 rc = -1;
3970 else {
3971 PDATA_APPEND(self->stack, value, -1);
3972 rc = 0;
3975 Py_DECREF(py_key);
3976 return rc;
3980 static int
3981 load_long_binget(Unpicklerobject *self)
3983 PyObject *py_key = 0, *value = 0;
3984 unsigned char c;
3985 char *s;
3986 long key;
3987 int rc;
3989 if (self->read_func(self, &s, 4) < 0) return -1;
3991 c = (unsigned char)s[0];
3992 key = (long)c;
3993 c = (unsigned char)s[1];
3994 key |= (long)c << 8;
3995 c = (unsigned char)s[2];
3996 key |= (long)c << 16;
3997 c = (unsigned char)s[3];
3998 key |= (long)c << 24;
4000 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4002 value = PyDict_GetItem(self->memo, py_key);
4003 if (! value) {
4004 PyErr_SetObject(BadPickleGet, py_key);
4005 rc = -1;
4007 else {
4008 PDATA_APPEND(self->stack, value, -1);
4009 rc = 0;
4012 Py_DECREF(py_key);
4013 return rc;
4016 /* Push an object from the extension registry (EXT[124]). nbytes is
4017 * the number of bytes following the opcode, holding the index (code) value.
4019 static int
4020 load_extension(Unpicklerobject *self, int nbytes)
4022 char *codebytes; /* the nbytes bytes after the opcode */
4023 long code; /* calc_binint returns long */
4024 PyObject *py_code; /* code as a Python int */
4025 PyObject *obj; /* the object to push */
4026 PyObject *pair; /* (module_name, class_name) */
4027 PyObject *module_name, *class_name;
4029 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4030 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4031 code = calc_binint(codebytes, nbytes);
4032 if (code <= 0) { /* note that 0 is forbidden */
4033 /* Corrupt or hostile pickle. */
4034 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
4035 return -1;
4038 /* Look for the code in the cache. */
4039 py_code = PyInt_FromLong(code);
4040 if (py_code == NULL) return -1;
4041 obj = PyDict_GetItem(extension_cache, py_code);
4042 if (obj != NULL) {
4043 /* Bingo. */
4044 Py_DECREF(py_code);
4045 PDATA_APPEND(self->stack, obj, -1);
4046 return 0;
4049 /* Look up the (module_name, class_name) pair. */
4050 pair = PyDict_GetItem(inverted_registry, py_code);
4051 if (pair == NULL) {
4052 Py_DECREF(py_code);
4053 PyErr_Format(PyExc_ValueError, "unregistered extension "
4054 "code %ld", code);
4055 return -1;
4057 /* Since the extension registry is manipulable via Python code,
4058 * confirm that pair is really a 2-tuple of strings.
4060 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
4061 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
4062 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
4063 Py_DECREF(py_code);
4064 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
4065 "isn't a 2-tuple of strings", code);
4066 return -1;
4068 /* Load the object. */
4069 obj = find_class(module_name, class_name, self->find_class);
4070 if (obj == NULL) {
4071 Py_DECREF(py_code);
4072 return -1;
4074 /* Cache code -> obj. */
4075 code = PyDict_SetItem(extension_cache, py_code, obj);
4076 Py_DECREF(py_code);
4077 if (code < 0) {
4078 Py_DECREF(obj);
4079 return -1;
4081 PDATA_PUSH(self->stack, obj, -1);
4082 return 0;
4085 static int
4086 load_put(Unpicklerobject *self)
4088 PyObject *py_str = 0, *value = 0;
4089 int len, l;
4090 char *s;
4092 if ((l = self->readline_func(self, &s)) < 0) return -1;
4093 if (l < 2) return bad_readline();
4094 if (!( len=self->stack->length )) return stackUnderflow();
4095 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
4096 value=self->stack->data[len-1];
4097 l=PyDict_SetItem(self->memo, py_str, value);
4098 Py_DECREF(py_str);
4099 return l;
4103 static int
4104 load_binput(Unpicklerobject *self)
4106 PyObject *py_key = 0, *value = 0;
4107 unsigned char key;
4108 char *s;
4109 int len;
4111 if (self->read_func(self, &s, 1) < 0) return -1;
4112 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
4114 key = (unsigned char)s[0];
4116 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4117 value=self->stack->data[len-1];
4118 len=PyDict_SetItem(self->memo, py_key, value);
4119 Py_DECREF(py_key);
4120 return len;
4124 static int
4125 load_long_binput(Unpicklerobject *self)
4127 PyObject *py_key = 0, *value = 0;
4128 long key;
4129 unsigned char c;
4130 char *s;
4131 int len;
4133 if (self->read_func(self, &s, 4) < 0) return -1;
4134 if (!( len=self->stack->length )) return stackUnderflow();
4136 c = (unsigned char)s[0];
4137 key = (long)c;
4138 c = (unsigned char)s[1];
4139 key |= (long)c << 8;
4140 c = (unsigned char)s[2];
4141 key |= (long)c << 16;
4142 c = (unsigned char)s[3];
4143 key |= (long)c << 24;
4145 if (!( py_key = PyInt_FromLong(key))) return -1;
4146 value=self->stack->data[len-1];
4147 len=PyDict_SetItem(self->memo, py_key, value);
4148 Py_DECREF(py_key);
4149 return len;
4153 static int
4154 do_append(Unpicklerobject *self, int x)
4156 PyObject *value = 0, *list = 0, *append_method = 0;
4157 int len, i;
4159 len=self->stack->length;
4160 if (!( len >= x && x > 0 )) return stackUnderflow();
4161 /* nothing to do */
4162 if (len==x) return 0;
4164 list=self->stack->data[x-1];
4166 if (PyList_Check(list)) {
4167 PyObject *slice;
4168 int list_len;
4170 slice=Pdata_popList(self->stack, x);
4171 if (! slice) return -1;
4172 list_len = PyList_GET_SIZE(list);
4173 i=PyList_SetSlice(list, list_len, list_len, slice);
4174 Py_DECREF(slice);
4175 return i;
4177 else {
4179 if (!( append_method = PyObject_GetAttr(list, append_str)))
4180 return -1;
4182 for (i = x; i < len; i++) {
4183 PyObject *junk;
4185 value=self->stack->data[i];
4186 junk=0;
4187 ARG_TUP(self, value);
4188 if (self->arg) {
4189 junk = PyObject_Call(append_method, self->arg,
4190 NULL);
4191 FREE_ARG_TUP(self);
4193 if (! junk) {
4194 Pdata_clear(self->stack, i+1);
4195 self->stack->length=x;
4196 Py_DECREF(append_method);
4197 return -1;
4199 Py_DECREF(junk);
4201 self->stack->length=x;
4202 Py_DECREF(append_method);
4205 return 0;
4209 static int
4210 load_append(Unpicklerobject *self)
4212 return do_append(self, self->stack->length - 1);
4216 static int
4217 load_appends(Unpicklerobject *self)
4219 return do_append(self, marker(self));
4223 static int
4224 do_setitems(Unpicklerobject *self, int x)
4226 PyObject *value = 0, *key = 0, *dict = 0;
4227 int len, i, r=0;
4229 if (!( (len=self->stack->length) >= x
4230 && x > 0 )) return stackUnderflow();
4232 dict=self->stack->data[x-1];
4234 for (i = x+1; i < len; i += 2) {
4235 key =self->stack->data[i-1];
4236 value=self->stack->data[i ];
4237 if (PyObject_SetItem(dict, key, value) < 0) {
4238 r=-1;
4239 break;
4243 Pdata_clear(self->stack, x);
4245 return r;
4249 static int
4250 load_setitem(Unpicklerobject *self)
4252 return do_setitems(self, self->stack->length - 2);
4255 static int
4256 load_setitems(Unpicklerobject *self)
4258 return do_setitems(self, marker(self));
4262 static int
4263 load_build(Unpicklerobject *self)
4265 PyObject *state, *inst, *slotstate;
4266 PyObject *__setstate__;
4267 PyObject *d_key, *d_value;
4268 Py_ssize_t i;
4269 int res = -1;
4271 /* Stack is ... instance, state. We want to leave instance at
4272 * the stack top, possibly mutated via instance.__setstate__(state).
4274 if (self->stack->length < 2)
4275 return stackUnderflow();
4276 PDATA_POP(self->stack, state);
4277 if (state == NULL)
4278 return -1;
4279 inst = self->stack->data[self->stack->length - 1];
4281 __setstate__ = PyObject_GetAttr(inst, __setstate___str);
4282 if (__setstate__ != NULL) {
4283 PyObject *junk = NULL;
4285 /* The explicit __setstate__ is responsible for everything. */
4286 ARG_TUP(self, state);
4287 if (self->arg) {
4288 junk = PyObject_Call(__setstate__, self->arg, NULL);
4289 FREE_ARG_TUP(self);
4291 Py_DECREF(__setstate__);
4292 if (junk == NULL)
4293 return -1;
4294 Py_DECREF(junk);
4295 return 0;
4297 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4298 return -1;
4299 PyErr_Clear();
4301 /* A default __setstate__. First see whether state embeds a
4302 * slot state dict too (a proto 2 addition).
4304 if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
4305 PyObject *temp = state;
4306 state = PyTuple_GET_ITEM(temp, 0);
4307 slotstate = PyTuple_GET_ITEM(temp, 1);
4308 Py_INCREF(state);
4309 Py_INCREF(slotstate);
4310 Py_DECREF(temp);
4312 else
4313 slotstate = NULL;
4315 /* Set inst.__dict__ from the state dict (if any). */
4316 if (state != Py_None) {
4317 PyObject *dict;
4318 if (! PyDict_Check(state)) {
4319 PyErr_SetString(UnpicklingError, "state is not a "
4320 "dictionary");
4321 goto finally;
4323 dict = PyObject_GetAttr(inst, __dict___str);
4324 if (dict == NULL)
4325 goto finally;
4327 i = 0;
4328 while (PyDict_Next(state, &i, &d_key, &d_value)) {
4329 if (PyObject_SetItem(dict, d_key, d_value) < 0)
4330 goto finally;
4332 Py_DECREF(dict);
4335 /* Also set instance attributes from the slotstate dict (if any). */
4336 if (slotstate != NULL) {
4337 if (! PyDict_Check(slotstate)) {
4338 PyErr_SetString(UnpicklingError, "slot state is not "
4339 "a dictionary");
4340 goto finally;
4342 i = 0;
4343 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
4344 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
4345 goto finally;
4348 res = 0;
4350 finally:
4351 Py_DECREF(state);
4352 Py_XDECREF(slotstate);
4353 return res;
4357 static int
4358 load_mark(Unpicklerobject *self)
4360 int s;
4362 /* Note that we split the (pickle.py) stack into two stacks, an
4363 object stack and a mark stack. Here we push a mark onto the
4364 mark stack.
4367 if ((self->num_marks + 1) >= self->marks_size) {
4368 s=self->marks_size+20;
4369 if (s <= self->num_marks) s=self->num_marks + 1;
4370 if (self->marks == NULL)
4371 self->marks=(int *)malloc(s * sizeof(int));
4372 else
4373 self->marks=(int *)realloc(self->marks,
4374 s * sizeof(int));
4375 if (! self->marks) {
4376 PyErr_NoMemory();
4377 return -1;
4379 self->marks_size = s;
4382 self->marks[self->num_marks++] = self->stack->length;
4384 return 0;
4387 static int
4388 load_reduce(Unpicklerobject *self)
4390 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
4392 PDATA_POP(self->stack, arg_tup);
4393 if (! arg_tup) return -1;
4394 PDATA_POP(self->stack, callable);
4395 if (callable) {
4396 ob = Instance_New(callable, arg_tup);
4397 Py_DECREF(callable);
4399 Py_DECREF(arg_tup);
4401 if (! ob) return -1;
4403 PDATA_PUSH(self->stack, ob, -1);
4404 return 0;
4407 /* Just raises an error if we don't know the protocol specified. PROTO
4408 * is the first opcode for protocols >= 2.
4410 static int
4411 load_proto(Unpicklerobject *self)
4413 int i;
4414 char *protobyte;
4416 i = self->read_func(self, &protobyte, 1);
4417 if (i < 0)
4418 return -1;
4420 i = calc_binint(protobyte, 1);
4421 /* No point checking for < 0, since calc_binint returns an unsigned
4422 * int when chewing on 1 byte.
4424 assert(i >= 0);
4425 if (i <= HIGHEST_PROTOCOL)
4426 return 0;
4428 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4429 return -1;
4432 static PyObject *
4433 load(Unpicklerobject *self)
4435 PyObject *err = 0, *val = 0;
4436 char *s;
4438 self->num_marks = 0;
4439 if (self->stack->length) Pdata_clear(self->stack, 0);
4441 while (1) {
4442 if (self->read_func(self, &s, 1) < 0)
4443 break;
4445 switch (s[0]) {
4446 case NONE:
4447 if (load_none(self) < 0)
4448 break;
4449 continue;
4451 case BININT:
4452 if (load_binint(self) < 0)
4453 break;
4454 continue;
4456 case BININT1:
4457 if (load_binint1(self) < 0)
4458 break;
4459 continue;
4461 case BININT2:
4462 if (load_binint2(self) < 0)
4463 break;
4464 continue;
4466 case INT:
4467 if (load_int(self) < 0)
4468 break;
4469 continue;
4471 case LONG:
4472 if (load_long(self) < 0)
4473 break;
4474 continue;
4476 case LONG1:
4477 if (load_counted_long(self, 1) < 0)
4478 break;
4479 continue;
4481 case LONG4:
4482 if (load_counted_long(self, 4) < 0)
4483 break;
4484 continue;
4486 case FLOAT:
4487 if (load_float(self) < 0)
4488 break;
4489 continue;
4491 case BINFLOAT:
4492 if (load_binfloat(self) < 0)
4493 break;
4494 continue;
4496 case BINSTRING:
4497 if (load_binstring(self) < 0)
4498 break;
4499 continue;
4501 case SHORT_BINSTRING:
4502 if (load_short_binstring(self) < 0)
4503 break;
4504 continue;
4506 case STRING:
4507 if (load_string(self) < 0)
4508 break;
4509 continue;
4511 #ifdef Py_USING_UNICODE
4512 case UNICODE:
4513 if (load_unicode(self) < 0)
4514 break;
4515 continue;
4517 case BINUNICODE:
4518 if (load_binunicode(self) < 0)
4519 break;
4520 continue;
4521 #endif
4523 case EMPTY_TUPLE:
4524 if (load_counted_tuple(self, 0) < 0)
4525 break;
4526 continue;
4528 case TUPLE1:
4529 if (load_counted_tuple(self, 1) < 0)
4530 break;
4531 continue;
4533 case TUPLE2:
4534 if (load_counted_tuple(self, 2) < 0)
4535 break;
4536 continue;
4538 case TUPLE3:
4539 if (load_counted_tuple(self, 3) < 0)
4540 break;
4541 continue;
4543 case TUPLE:
4544 if (load_tuple(self) < 0)
4545 break;
4546 continue;
4548 case EMPTY_LIST:
4549 if (load_empty_list(self) < 0)
4550 break;
4551 continue;
4553 case LIST:
4554 if (load_list(self) < 0)
4555 break;
4556 continue;
4558 case EMPTY_DICT:
4559 if (load_empty_dict(self) < 0)
4560 break;
4561 continue;
4563 case DICT:
4564 if (load_dict(self) < 0)
4565 break;
4566 continue;
4568 case OBJ:
4569 if (load_obj(self) < 0)
4570 break;
4571 continue;
4573 case INST:
4574 if (load_inst(self) < 0)
4575 break;
4576 continue;
4578 case NEWOBJ:
4579 if (load_newobj(self) < 0)
4580 break;
4581 continue;
4583 case GLOBAL:
4584 if (load_global(self) < 0)
4585 break;
4586 continue;
4588 case APPEND:
4589 if (load_append(self) < 0)
4590 break;
4591 continue;
4593 case APPENDS:
4594 if (load_appends(self) < 0)
4595 break;
4596 continue;
4598 case BUILD:
4599 if (load_build(self) < 0)
4600 break;
4601 continue;
4603 case DUP:
4604 if (load_dup(self) < 0)
4605 break;
4606 continue;
4608 case BINGET:
4609 if (load_binget(self) < 0)
4610 break;
4611 continue;
4613 case LONG_BINGET:
4614 if (load_long_binget(self) < 0)
4615 break;
4616 continue;
4618 case GET:
4619 if (load_get(self) < 0)
4620 break;
4621 continue;
4623 case EXT1:
4624 if (load_extension(self, 1) < 0)
4625 break;
4626 continue;
4628 case EXT2:
4629 if (load_extension(self, 2) < 0)
4630 break;
4631 continue;
4633 case EXT4:
4634 if (load_extension(self, 4) < 0)
4635 break;
4636 continue;
4637 case MARK:
4638 if (load_mark(self) < 0)
4639 break;
4640 continue;
4642 case BINPUT:
4643 if (load_binput(self) < 0)
4644 break;
4645 continue;
4647 case LONG_BINPUT:
4648 if (load_long_binput(self) < 0)
4649 break;
4650 continue;
4652 case PUT:
4653 if (load_put(self) < 0)
4654 break;
4655 continue;
4657 case POP:
4658 if (load_pop(self) < 0)
4659 break;
4660 continue;
4662 case POP_MARK:
4663 if (load_pop_mark(self) < 0)
4664 break;
4665 continue;
4667 case SETITEM:
4668 if (load_setitem(self) < 0)
4669 break;
4670 continue;
4672 case SETITEMS:
4673 if (load_setitems(self) < 0)
4674 break;
4675 continue;
4677 case STOP:
4678 break;
4680 case PERSID:
4681 if (load_persid(self) < 0)
4682 break;
4683 continue;
4685 case BINPERSID:
4686 if (load_binpersid(self) < 0)
4687 break;
4688 continue;
4690 case REDUCE:
4691 if (load_reduce(self) < 0)
4692 break;
4693 continue;
4695 case PROTO:
4696 if (load_proto(self) < 0)
4697 break;
4698 continue;
4700 case NEWTRUE:
4701 if (load_bool(self, Py_True) < 0)
4702 break;
4703 continue;
4705 case NEWFALSE:
4706 if (load_bool(self, Py_False) < 0)
4707 break;
4708 continue;
4710 case '\0':
4711 /* end of file */
4712 PyErr_SetNone(PyExc_EOFError);
4713 break;
4715 default:
4716 cPickle_ErrFormat(UnpicklingError,
4717 "invalid load key, '%s'.",
4718 "c", s[0]);
4719 return NULL;
4722 break;
4725 if ((err = PyErr_Occurred())) {
4726 if (err == PyExc_EOFError) {
4727 PyErr_SetNone(PyExc_EOFError);
4729 return NULL;
4732 PDATA_POP(self->stack, val);
4733 return val;
4737 /* No-load functions to support noload, which is used to
4738 find persistent references. */
4740 static int
4741 noload_obj(Unpicklerobject *self)
4743 int i;
4745 if ((i = marker(self)) < 0) return -1;
4746 return Pdata_clear(self->stack, i+1);
4750 static int
4751 noload_inst(Unpicklerobject *self)
4753 int i;
4754 char *s;
4756 if ((i = marker(self)) < 0) return -1;
4757 Pdata_clear(self->stack, i);
4758 if (self->readline_func(self, &s) < 0) return -1;
4759 if (self->readline_func(self, &s) < 0) return -1;
4760 PDATA_APPEND(self->stack, Py_None, -1);
4761 return 0;
4764 static int
4765 noload_newobj(Unpicklerobject *self)
4767 PyObject *obj;
4769 PDATA_POP(self->stack, obj); /* pop argtuple */
4770 if (obj == NULL) return -1;
4771 Py_DECREF(obj);
4773 PDATA_POP(self->stack, obj); /* pop cls */
4774 if (obj == NULL) return -1;
4775 Py_DECREF(obj);
4777 PDATA_APPEND(self->stack, Py_None, -1);
4778 return 0;
4781 static int
4782 noload_global(Unpicklerobject *self)
4784 char *s;
4786 if (self->readline_func(self, &s) < 0) return -1;
4787 if (self->readline_func(self, &s) < 0) return -1;
4788 PDATA_APPEND(self->stack, Py_None,-1);
4789 return 0;
4792 static int
4793 noload_reduce(Unpicklerobject *self)
4796 if (self->stack->length < 2) return stackUnderflow();
4797 Pdata_clear(self->stack, self->stack->length-2);
4798 PDATA_APPEND(self->stack, Py_None,-1);
4799 return 0;
4802 static int
4803 noload_build(Unpicklerobject *self) {
4805 if (self->stack->length < 1) return stackUnderflow();
4806 Pdata_clear(self->stack, self->stack->length-1);
4807 return 0;
4810 static int
4811 noload_extension(Unpicklerobject *self, int nbytes)
4813 char *codebytes;
4815 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4816 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4817 PDATA_APPEND(self->stack, Py_None, -1);
4818 return 0;
4822 static PyObject *
4823 noload(Unpicklerobject *self)
4825 PyObject *err = 0, *val = 0;
4826 char *s;
4828 self->num_marks = 0;
4829 Pdata_clear(self->stack, 0);
4831 while (1) {
4832 if (self->read_func(self, &s, 1) < 0)
4833 break;
4835 switch (s[0]) {
4836 case NONE:
4837 if (load_none(self) < 0)
4838 break;
4839 continue;
4841 case BININT:
4842 if (load_binint(self) < 0)
4843 break;
4844 continue;
4846 case BININT1:
4847 if (load_binint1(self) < 0)
4848 break;
4849 continue;
4851 case BININT2:
4852 if (load_binint2(self) < 0)
4853 break;
4854 continue;
4856 case INT:
4857 if (load_int(self) < 0)
4858 break;
4859 continue;
4861 case LONG:
4862 if (load_long(self) < 0)
4863 break;
4864 continue;
4866 case LONG1:
4867 if (load_counted_long(self, 1) < 0)
4868 break;
4869 continue;
4871 case LONG4:
4872 if (load_counted_long(self, 4) < 0)
4873 break;
4874 continue;
4876 case FLOAT:
4877 if (load_float(self) < 0)
4878 break;
4879 continue;
4881 case BINFLOAT:
4882 if (load_binfloat(self) < 0)
4883 break;
4884 continue;
4886 case BINSTRING:
4887 if (load_binstring(self) < 0)
4888 break;
4889 continue;
4891 case SHORT_BINSTRING:
4892 if (load_short_binstring(self) < 0)
4893 break;
4894 continue;
4896 case STRING:
4897 if (load_string(self) < 0)
4898 break;
4899 continue;
4901 #ifdef Py_USING_UNICODE
4902 case UNICODE:
4903 if (load_unicode(self) < 0)
4904 break;
4905 continue;
4907 case BINUNICODE:
4908 if (load_binunicode(self) < 0)
4909 break;
4910 continue;
4911 #endif
4913 case EMPTY_TUPLE:
4914 if (load_counted_tuple(self, 0) < 0)
4915 break;
4916 continue;
4918 case TUPLE1:
4919 if (load_counted_tuple(self, 1) < 0)
4920 break;
4921 continue;
4923 case TUPLE2:
4924 if (load_counted_tuple(self, 2) < 0)
4925 break;
4926 continue;
4928 case TUPLE3:
4929 if (load_counted_tuple(self, 3) < 0)
4930 break;
4931 continue;
4933 case TUPLE:
4934 if (load_tuple(self) < 0)
4935 break;
4936 continue;
4938 case EMPTY_LIST:
4939 if (load_empty_list(self) < 0)
4940 break;
4941 continue;
4943 case LIST:
4944 if (load_list(self) < 0)
4945 break;
4946 continue;
4948 case EMPTY_DICT:
4949 if (load_empty_dict(self) < 0)
4950 break;
4951 continue;
4953 case DICT:
4954 if (load_dict(self) < 0)
4955 break;
4956 continue;
4958 case OBJ:
4959 if (noload_obj(self) < 0)
4960 break;
4961 continue;
4963 case INST:
4964 if (noload_inst(self) < 0)
4965 break;
4966 continue;
4968 case NEWOBJ:
4969 if (noload_newobj(self) < 0)
4970 break;
4971 continue;
4973 case GLOBAL:
4974 if (noload_global(self) < 0)
4975 break;
4976 continue;
4978 case APPEND:
4979 if (load_append(self) < 0)
4980 break;
4981 continue;
4983 case APPENDS:
4984 if (load_appends(self) < 0)
4985 break;
4986 continue;
4988 case BUILD:
4989 if (noload_build(self) < 0)
4990 break;
4991 continue;
4993 case DUP:
4994 if (load_dup(self) < 0)
4995 break;
4996 continue;
4998 case BINGET:
4999 if (load_binget(self) < 0)
5000 break;
5001 continue;
5003 case LONG_BINGET:
5004 if (load_long_binget(self) < 0)
5005 break;
5006 continue;
5008 case GET:
5009 if (load_get(self) < 0)
5010 break;
5011 continue;
5013 case EXT1:
5014 if (noload_extension(self, 1) < 0)
5015 break;
5016 continue;
5018 case EXT2:
5019 if (noload_extension(self, 2) < 0)
5020 break;
5021 continue;
5023 case EXT4:
5024 if (noload_extension(self, 4) < 0)
5025 break;
5026 continue;
5028 case MARK:
5029 if (load_mark(self) < 0)
5030 break;
5031 continue;
5033 case BINPUT:
5034 if (load_binput(self) < 0)
5035 break;
5036 continue;
5038 case LONG_BINPUT:
5039 if (load_long_binput(self) < 0)
5040 break;
5041 continue;
5043 case PUT:
5044 if (load_put(self) < 0)
5045 break;
5046 continue;
5048 case POP:
5049 if (load_pop(self) < 0)
5050 break;
5051 continue;
5053 case POP_MARK:
5054 if (load_pop_mark(self) < 0)
5055 break;
5056 continue;
5058 case SETITEM:
5059 if (load_setitem(self) < 0)
5060 break;
5061 continue;
5063 case SETITEMS:
5064 if (load_setitems(self) < 0)
5065 break;
5066 continue;
5068 case STOP:
5069 break;
5071 case PERSID:
5072 if (load_persid(self) < 0)
5073 break;
5074 continue;
5076 case BINPERSID:
5077 if (load_binpersid(self) < 0)
5078 break;
5079 continue;
5081 case REDUCE:
5082 if (noload_reduce(self) < 0)
5083 break;
5084 continue;
5086 case PROTO:
5087 if (load_proto(self) < 0)
5088 break;
5089 continue;
5091 case NEWTRUE:
5092 if (load_bool(self, Py_True) < 0)
5093 break;
5094 continue;
5096 case NEWFALSE:
5097 if (load_bool(self, Py_False) < 0)
5098 break;
5099 continue;
5100 default:
5101 cPickle_ErrFormat(UnpicklingError,
5102 "invalid load key, '%s'.",
5103 "c", s[0]);
5104 return NULL;
5107 break;
5110 if ((err = PyErr_Occurred())) {
5111 if (err == PyExc_EOFError) {
5112 PyErr_SetNone(PyExc_EOFError);
5114 return NULL;
5117 PDATA_POP(self->stack, val);
5118 return val;
5122 static PyObject *
5123 Unpickler_load(Unpicklerobject *self, PyObject *unused)
5125 return load(self);
5128 static PyObject *
5129 Unpickler_noload(Unpicklerobject *self, PyObject *unused)
5131 return noload(self);
5135 static struct PyMethodDef Unpickler_methods[] = {
5136 {"load", (PyCFunction)Unpickler_load, METH_NOARGS,
5137 PyDoc_STR("load() -- Load a pickle")
5139 {"noload", (PyCFunction)Unpickler_noload, METH_NOARGS,
5140 PyDoc_STR(
5141 "noload() -- not load a pickle, but go through most of the motions\n"
5142 "\n"
5143 "This function can be used to read past a pickle without instantiating\n"
5144 "any objects or importing any modules. It can also be used to find all\n"
5145 "persistent references without instantiating any objects or importing\n"
5146 "any modules.\n")
5148 {NULL, NULL} /* sentinel */
5152 static Unpicklerobject *
5153 newUnpicklerobject(PyObject *f)
5155 Unpicklerobject *self;
5157 if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype)))
5158 return NULL;
5160 self->file = NULL;
5161 self->arg = NULL;
5162 self->stack = (Pdata*)Pdata_New();
5163 self->pers_func = NULL;
5164 self->last_string = NULL;
5165 self->marks = NULL;
5166 self->num_marks = 0;
5167 self->marks_size = 0;
5168 self->buf_size = 0;
5169 self->read = NULL;
5170 self->readline = NULL;
5171 self->find_class = NULL;
5173 if (!( self->memo = PyDict_New()))
5174 goto err;
5176 if (!self->stack)
5177 goto err;
5179 Py_INCREF(f);
5180 self->file = f;
5182 /* Set read, readline based on type of f */
5183 if (PyFile_Check(f)) {
5184 self->fp = PyFile_AsFile(f);
5185 if (self->fp == NULL) {
5186 PyErr_SetString(PyExc_ValueError,
5187 "I/O operation on closed file");
5188 goto err;
5190 self->read_func = read_file;
5191 self->readline_func = readline_file;
5193 else if (PycStringIO_InputCheck(f)) {
5194 self->fp = NULL;
5195 self->read_func = read_cStringIO;
5196 self->readline_func = readline_cStringIO;
5198 else {
5200 self->fp = NULL;
5201 self->read_func = read_other;
5202 self->readline_func = readline_other;
5204 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5205 (self->read = PyObject_GetAttr(f, read_str)))) {
5206 PyErr_Clear();
5207 PyErr_SetString( PyExc_TypeError,
5208 "argument must have 'read' and "
5209 "'readline' attributes" );
5210 goto err;
5213 PyObject_GC_Track(self);
5215 return self;
5217 err:
5218 Py_DECREF((PyObject *)self);
5219 return NULL;
5223 static PyObject *
5224 get_Unpickler(PyObject *self, PyObject *file)
5226 return (PyObject *)newUnpicklerobject(file);
5230 static void
5231 Unpickler_dealloc(Unpicklerobject *self)
5233 PyObject_GC_UnTrack((PyObject *)self);
5234 Py_XDECREF(self->readline);
5235 Py_XDECREF(self->read);
5236 Py_XDECREF(self->file);
5237 Py_XDECREF(self->memo);
5238 Py_XDECREF(self->stack);
5239 Py_XDECREF(self->pers_func);
5240 Py_XDECREF(self->arg);
5241 Py_XDECREF(self->last_string);
5242 Py_XDECREF(self->find_class);
5244 if (self->marks) {
5245 free(self->marks);
5248 if (self->buf_size) {
5249 free(self->buf);
5252 self->ob_type->tp_free((PyObject *)self);
5255 static int
5256 Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg)
5258 Py_VISIT(self->readline);
5259 Py_VISIT(self->read);
5260 Py_VISIT(self->file);
5261 Py_VISIT(self->memo);
5262 Py_VISIT(self->stack);
5263 Py_VISIT(self->pers_func);
5264 Py_VISIT(self->arg);
5265 Py_VISIT(self->last_string);
5266 Py_VISIT(self->find_class);
5267 return 0;
5270 static int
5271 Unpickler_clear(Unpicklerobject *self)
5273 Py_CLEAR(self->readline);
5274 Py_CLEAR(self->read);
5275 Py_CLEAR(self->file);
5276 Py_CLEAR(self->memo);
5277 Py_CLEAR(self->stack);
5278 Py_CLEAR(self->pers_func);
5279 Py_CLEAR(self->arg);
5280 Py_CLEAR(self->last_string);
5281 Py_CLEAR(self->find_class);
5282 return 0;
5285 static PyObject *
5286 Unpickler_getattr(Unpicklerobject *self, char *name)
5288 if (!strcmp(name, "persistent_load")) {
5289 if (!self->pers_func) {
5290 PyErr_SetString(PyExc_AttributeError, name);
5291 return NULL;
5294 Py_INCREF(self->pers_func);
5295 return self->pers_func;
5298 if (!strcmp(name, "find_global")) {
5299 if (!self->find_class) {
5300 PyErr_SetString(PyExc_AttributeError, name);
5301 return NULL;
5304 Py_INCREF(self->find_class);
5305 return self->find_class;
5308 if (!strcmp(name, "memo")) {
5309 if (!self->memo) {
5310 PyErr_SetString(PyExc_AttributeError, name);
5311 return NULL;
5314 Py_INCREF(self->memo);
5315 return self->memo;
5318 if (!strcmp(name, "UnpicklingError")) {
5319 Py_INCREF(UnpicklingError);
5320 return UnpicklingError;
5323 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
5327 static int
5328 Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
5331 if (!strcmp(name, "persistent_load")) {
5332 Py_XDECREF(self->pers_func);
5333 self->pers_func = value;
5334 Py_XINCREF(value);
5335 return 0;
5338 if (!strcmp(name, "find_global")) {
5339 Py_XDECREF(self->find_class);
5340 self->find_class = value;
5341 Py_XINCREF(value);
5342 return 0;
5345 if (! value) {
5346 PyErr_SetString(PyExc_TypeError,
5347 "attribute deletion is not supported");
5348 return -1;
5351 if (strcmp(name, "memo") == 0) {
5352 if (!PyDict_Check(value)) {
5353 PyErr_SetString(PyExc_TypeError,
5354 "memo must be a dictionary");
5355 return -1;
5357 Py_XDECREF(self->memo);
5358 self->memo = value;
5359 Py_INCREF(value);
5360 return 0;
5363 PyErr_SetString(PyExc_AttributeError, name);
5364 return -1;
5367 /* ---------------------------------------------------------------------------
5368 * Module-level functions.
5371 /* dump(obj, file, protocol=0). */
5372 static PyObject *
5373 cpm_dump(PyObject *self, PyObject *args, PyObject *kwds)
5375 static char *kwlist[] = {"obj", "file", "protocol", NULL};
5376 PyObject *ob, *file, *res = NULL;
5377 Picklerobject *pickler = 0;
5378 int proto = 0;
5380 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist,
5381 &ob, &file, &proto)))
5382 goto finally;
5384 if (!( pickler = newPicklerobject(file, proto)))
5385 goto finally;
5387 if (dump(pickler, ob) < 0)
5388 goto finally;
5390 Py_INCREF(Py_None);
5391 res = Py_None;
5393 finally:
5394 Py_XDECREF(pickler);
5396 return res;
5400 /* dumps(obj, protocol=0). */
5401 static PyObject *
5402 cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds)
5404 static char *kwlist[] = {"obj", "protocol", NULL};
5405 PyObject *ob, *file = 0, *res = NULL;
5406 Picklerobject *pickler = 0;
5407 int proto = 0;
5409 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist,
5410 &ob, &proto)))
5411 goto finally;
5413 if (!( file = PycStringIO->NewOutput(128)))
5414 goto finally;
5416 if (!( pickler = newPicklerobject(file, proto)))
5417 goto finally;
5419 if (dump(pickler, ob) < 0)
5420 goto finally;
5422 res = PycStringIO->cgetvalue(file);
5424 finally:
5425 Py_XDECREF(pickler);
5426 Py_XDECREF(file);
5428 return res;
5432 /* load(fileobj). */
5433 static PyObject *
5434 cpm_load(PyObject *self, PyObject *ob)
5436 Unpicklerobject *unpickler = 0;
5437 PyObject *res = NULL;
5439 if (!( unpickler = newUnpicklerobject(ob)))
5440 goto finally;
5442 res = load(unpickler);
5444 finally:
5445 Py_XDECREF(unpickler);
5447 return res;
5451 /* loads(string) */
5452 static PyObject *
5453 cpm_loads(PyObject *self, PyObject *args)
5455 PyObject *ob, *file = 0, *res = NULL;
5456 Unpicklerobject *unpickler = 0;
5458 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
5459 goto finally;
5461 if (!( file = PycStringIO->NewInput(ob)))
5462 goto finally;
5464 if (!( unpickler = newUnpicklerobject(file)))
5465 goto finally;
5467 res = load(unpickler);
5469 finally:
5470 Py_XDECREF(file);
5471 Py_XDECREF(unpickler);
5473 return res;
5477 PyDoc_STRVAR(Unpicklertype__doc__,
5478 "Objects that know how to unpickle");
5480 static PyTypeObject Unpicklertype = {
5481 PyObject_HEAD_INIT(NULL)
5482 0, /*ob_size*/
5483 "cPickle.Unpickler", /*tp_name*/
5484 sizeof(Unpicklerobject), /*tp_basicsize*/
5486 (destructor)Unpickler_dealloc, /* tp_dealloc */
5487 0, /* tp_print */
5488 (getattrfunc)Unpickler_getattr, /* tp_getattr */
5489 (setattrfunc)Unpickler_setattr, /* tp_setattr */
5490 0, /* tp_compare */
5491 0, /* tp_repr */
5492 0, /* tp_as_number */
5493 0, /* tp_as_sequence */
5494 0, /* tp_as_mapping */
5495 0, /* tp_hash */
5496 0, /* tp_call */
5497 0, /* tp_str */
5498 0, /* tp_getattro */
5499 0, /* tp_setattro */
5500 0, /* tp_as_buffer */
5501 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
5502 Unpicklertype__doc__, /* tp_doc */
5503 (traverseproc)Unpickler_traverse, /* tp_traverse */
5504 (inquiry)Unpickler_clear, /* tp_clear */
5507 static struct PyMethodDef cPickle_methods[] = {
5508 {"dump", (PyCFunction)cpm_dump, METH_VARARGS | METH_KEYWORDS,
5509 PyDoc_STR("dump(obj, file, protocol=0) -- "
5510 "Write an object in pickle format to the given file.\n"
5511 "\n"
5512 "See the Pickler docstring for the meaning of optional argument proto.")
5515 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS | METH_KEYWORDS,
5516 PyDoc_STR("dumps(obj, protocol=0) -- "
5517 "Return a string containing an object in pickle format.\n"
5518 "\n"
5519 "See the Pickler docstring for the meaning of optional argument proto.")
5522 {"load", (PyCFunction)cpm_load, METH_O,
5523 PyDoc_STR("load(file) -- Load a pickle from the given file")},
5525 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
5526 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
5528 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS | METH_KEYWORDS,
5529 PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n"
5530 "\n"
5531 "This takes a file-like object for writing a pickle data stream.\n"
5532 "The optional proto argument tells the pickler to use the given\n"
5533 "protocol; supported protocols are 0, 1, 2. The default\n"
5534 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5535 "only protocol that can be written to a file opened in text\n"
5536 "mode and read back successfully. When using a protocol higher\n"
5537 "than 0, make sure the file is opened in binary mode, both when\n"
5538 "pickling and unpickling.)\n"
5539 "\n"
5540 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5541 "more efficient than protocol 1.\n"
5542 "\n"
5543 "Specifying a negative protocol version selects the highest\n"
5544 "protocol version supported. The higher the protocol used, the\n"
5545 "more recent the version of Python needed to read the pickle\n"
5546 "produced.\n"
5547 "\n"
5548 "The file parameter must have a write() method that accepts a single\n"
5549 "string argument. It can thus be an open file object, a StringIO\n"
5550 "object, or any other custom object that meets this interface.\n")
5553 {"Unpickler", (PyCFunction)get_Unpickler, METH_O,
5554 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5556 { NULL, NULL }
5559 static int
5560 init_stuff(PyObject *module_dict)
5562 PyObject *copy_reg, *t, *r;
5564 #define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
5566 if (PyType_Ready(&Unpicklertype) < 0)
5567 return -1;
5568 if (PyType_Ready(&Picklertype) < 0)
5569 return -1;
5571 INIT_STR(__class__);
5572 INIT_STR(__getinitargs__);
5573 INIT_STR(__dict__);
5574 INIT_STR(__getstate__);
5575 INIT_STR(__setstate__);
5576 INIT_STR(__name__);
5577 INIT_STR(__main__);
5578 INIT_STR(__reduce__);
5579 INIT_STR(__reduce_ex__);
5580 INIT_STR(write);
5581 INIT_STR(append);
5582 INIT_STR(read);
5583 INIT_STR(readline);
5584 INIT_STR(copy_reg);
5585 INIT_STR(dispatch_table);
5587 if (!( copy_reg = PyImport_ImportModule("copy_reg")))
5588 return -1;
5590 /* This is special because we want to use a different
5591 one in restricted mode. */
5592 dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str);
5593 if (!dispatch_table) return -1;
5595 extension_registry = PyObject_GetAttrString(copy_reg,
5596 "_extension_registry");
5597 if (!extension_registry) return -1;
5599 inverted_registry = PyObject_GetAttrString(copy_reg,
5600 "_inverted_registry");
5601 if (!inverted_registry) return -1;
5603 extension_cache = PyObject_GetAttrString(copy_reg,
5604 "_extension_cache");
5605 if (!extension_cache) return -1;
5607 Py_DECREF(copy_reg);
5609 if (!(empty_tuple = PyTuple_New(0)))
5610 return -1;
5612 two_tuple = PyTuple_New(2);
5613 if (two_tuple == NULL)
5614 return -1;
5615 /* We use this temp container with no regard to refcounts, or to
5616 * keeping containees alive. Exempt from GC, because we don't
5617 * want anything looking at two_tuple() by magic.
5619 PyObject_GC_UnTrack(two_tuple);
5621 /* Ugh */
5622 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5623 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5624 return -1;
5626 if (!( t=PyDict_New())) return -1;
5627 if (!( r=PyRun_String(
5628 "def __str__(self):\n"
5629 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5630 Py_file_input,
5631 module_dict, t) )) return -1;
5632 Py_DECREF(r);
5634 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
5635 if (!PickleError)
5636 return -1;
5638 Py_DECREF(t);
5640 PicklingError = PyErr_NewException("cPickle.PicklingError",
5641 PickleError, NULL);
5642 if (!PicklingError)
5643 return -1;
5645 if (!( t=PyDict_New())) return -1;
5646 if (!( r=PyRun_String(
5647 "def __str__(self):\n"
5648 " a=self.args\n"
5649 " a=a and type(a[0]) or '(what)'\n"
5650 " return 'Cannot pickle %s objects' % a\n"
5651 , Py_file_input,
5652 module_dict, t) )) return -1;
5653 Py_DECREF(r);
5655 if (!( UnpickleableError = PyErr_NewException(
5656 "cPickle.UnpickleableError", PicklingError, t)))
5657 return -1;
5659 Py_DECREF(t);
5661 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
5662 PickleError, NULL)))
5663 return -1;
5665 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5666 UnpicklingError, NULL)))
5667 return -1;
5669 if (PyDict_SetItemString(module_dict, "PickleError",
5670 PickleError) < 0)
5671 return -1;
5673 if (PyDict_SetItemString(module_dict, "PicklingError",
5674 PicklingError) < 0)
5675 return -1;
5677 if (PyDict_SetItemString(module_dict, "UnpicklingError",
5678 UnpicklingError) < 0)
5679 return -1;
5681 if (PyDict_SetItemString(module_dict, "UnpickleableError",
5682 UnpickleableError) < 0)
5683 return -1;
5685 if (PyDict_SetItemString(module_dict, "BadPickleGet",
5686 BadPickleGet) < 0)
5687 return -1;
5689 PycString_IMPORT;
5691 return 0;
5694 #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5695 #define PyMODINIT_FUNC void
5696 #endif
5697 PyMODINIT_FUNC
5698 initcPickle(void)
5700 PyObject *m, *d, *di, *v, *k;
5701 Py_ssize_t i;
5702 char *rev = "1.71"; /* XXX when does this change? */
5703 PyObject *format_version;
5704 PyObject *compatible_formats;
5706 Picklertype.ob_type = &PyType_Type;
5707 Unpicklertype.ob_type = &PyType_Type;
5708 PdataType.ob_type = &PyType_Type;
5710 /* Initialize some pieces. We need to do this before module creation,
5711 * so we're forced to use a temporary dictionary. :(
5713 di = PyDict_New();
5714 if (!di) return;
5715 if (init_stuff(di) < 0) return;
5717 /* Create the module and add the functions */
5718 m = Py_InitModule4("cPickle", cPickle_methods,
5719 cPickle_module_documentation,
5720 (PyObject*)NULL,PYTHON_API_VERSION);
5721 if (m == NULL)
5722 return;
5724 /* Add some symbolic constants to the module */
5725 d = PyModule_GetDict(m);
5726 v = PyString_FromString(rev);
5727 PyDict_SetItemString(d, "__version__", v);
5728 Py_XDECREF(v);
5730 /* Copy data from di. Waaa. */
5731 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
5732 if (PyObject_SetItem(d, k, v) < 0) {
5733 Py_DECREF(di);
5734 return;
5737 Py_DECREF(di);
5739 i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
5740 if (i < 0)
5741 return;
5743 /* These are purely informational; no code uses them. */
5744 /* File format version we write. */
5745 format_version = PyString_FromString("2.0");
5746 /* Format versions we can read. */
5747 compatible_formats = Py_BuildValue("[sssss]",
5748 "1.0", /* Original protocol 0 */
5749 "1.1", /* Protocol 0 + INST */
5750 "1.2", /* Original protocol 1 */
5751 "1.3", /* Protocol 1 + BINFLOAT */
5752 "2.0"); /* Original protocol 2 */
5753 PyDict_SetItemString(d, "format_version", format_version);
5754 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
5755 Py_XDECREF(format_version);
5756 Py_XDECREF(compatible_formats);