Manual py3k backport: [svn r74158] Issue #6218: Make io.BytesIO and io.StringIO pickl...
[python.git] / Modules / cPickle.c
blobbbc14bf4d49eb2f3dd67158dab2e4b3f7b3d0e3d
1 #include "Python.h"
2 #include "cStringIO.h"
3 #include "structmember.h"
5 PyDoc_STRVAR(cPickle_module_documentation,
6 "C implementation and optimization of the Python pickle module.");
8 #ifndef Py_eval_input
9 #include <graminit.h>
10 #define Py_eval_input eval_input
11 #endif /* Py_eval_input */
13 #define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL))
15 #define WRITE_BUF_SIZE 256
17 /* Bump this when new opcodes are added to the pickle protocol. */
18 #define HIGHEST_PROTOCOL 2
21 * Note: The UNICODE macro controls the TCHAR meaning of the win32 API. Since
22 * all headers have already been included here, we can safely redefine it.
24 #ifdef UNICODE
25 # undef UNICODE
26 #endif
29 * Pickle opcodes. These must be kept in synch with pickle.py. Extensive
30 * docs are in pickletools.py.
32 #define MARK '('
33 #define STOP '.'
34 #define POP '0'
35 #define POP_MARK '1'
36 #define DUP '2'
37 #define FLOAT 'F'
38 #define BINFLOAT 'G'
39 #define INT 'I'
40 #define BININT 'J'
41 #define BININT1 'K'
42 #define LONG 'L'
43 #define BININT2 'M'
44 #define NONE 'N'
45 #define PERSID 'P'
46 #define BINPERSID 'Q'
47 #define REDUCE 'R'
48 #define STRING 'S'
49 #define BINSTRING 'T'
50 #define SHORT_BINSTRING 'U'
51 #define UNICODE 'V'
52 #define BINUNICODE 'X'
53 #define APPEND 'a'
54 #define BUILD 'b'
55 #define GLOBAL 'c'
56 #define DICT 'd'
57 #define EMPTY_DICT '}'
58 #define APPENDS 'e'
59 #define GET 'g'
60 #define BINGET 'h'
61 #define INST 'i'
62 #define LONG_BINGET 'j'
63 #define LIST 'l'
64 #define EMPTY_LIST ']'
65 #define OBJ 'o'
66 #define PUT 'p'
67 #define BINPUT 'q'
68 #define LONG_BINPUT 'r'
69 #define SETITEM 's'
70 #define TUPLE 't'
71 #define EMPTY_TUPLE ')'
72 #define SETITEMS 'u'
74 /* Protocol 2. */
75 #define PROTO '\x80' /* identify pickle protocol */
76 #define NEWOBJ '\x81' /* build object by applying cls.__new__ to argtuple */
77 #define EXT1 '\x82' /* push object from extension registry; 1-byte index */
78 #define EXT2 '\x83' /* ditto, but 2-byte index */
79 #define EXT4 '\x84' /* ditto, but 4-byte index */
80 #define TUPLE1 '\x85' /* build 1-tuple from stack top */
81 #define TUPLE2 '\x86' /* build 2-tuple from two topmost stack items */
82 #define TUPLE3 '\x87' /* build 3-tuple from three topmost stack items */
83 #define NEWTRUE '\x88' /* push True */
84 #define NEWFALSE '\x89' /* push False */
85 #define LONG1 '\x8a' /* push long from < 256 bytes */
86 #define LONG4 '\x8b' /* push really big long */
88 /* There aren't opcodes -- they're ways to pickle bools before protocol 2,
89 * so that unpicklers written before bools were introduced unpickle them
90 * as ints, but unpicklers after can recognize that bools were intended.
91 * Note that protocol 2 added direct ways to pickle bools.
93 #undef TRUE
94 #define TRUE "I01\n"
95 #undef FALSE
96 #define FALSE "I00\n"
98 /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
99 * batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
100 * break if this gets out of synch with pickle.py, but it's unclear that
101 * would help anything either.
103 #define BATCHSIZE 1000
105 static char MARKv = MARK;
107 static PyObject *PickleError;
108 static PyObject *PicklingError;
109 static PyObject *UnpickleableError;
110 static PyObject *UnpicklingError;
111 static PyObject *BadPickleGet;
113 /* As the name says, an empty tuple. */
114 static PyObject *empty_tuple;
116 /* copy_reg.dispatch_table, {type_object: pickling_function} */
117 static PyObject *dispatch_table;
119 /* For EXT[124] opcodes. */
120 /* copy_reg._extension_registry, {(module_name, function_name): code} */
121 static PyObject *extension_registry;
122 /* copy_reg._inverted_registry, {code: (module_name, function_name)} */
123 static PyObject *inverted_registry;
124 /* copy_reg._extension_cache, {code: object} */
125 static PyObject *extension_cache;
127 /* For looking up name pairs in copy_reg._extension_registry. */
128 static PyObject *two_tuple;
130 static PyObject *__class___str, *__getinitargs___str, *__dict___str,
131 *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
132 *__reduce_ex___str,
133 *write_str, *append_str,
134 *read_str, *readline_str, *__main___str,
135 *copyreg_str, *dispatch_table_str;
137 /*************************************************************************
138 Internal Data type for pickle data. */
140 typedef struct {
141 PyObject_HEAD
142 int length; /* number of initial slots in data currently used */
143 int size; /* number of slots in data allocated */
144 PyObject **data;
145 } Pdata;
147 static void
148 Pdata_dealloc(Pdata *self)
150 int i;
151 PyObject **p;
153 for (i = self->length, p = self->data; --i >= 0; p++) {
154 Py_DECREF(*p);
156 if (self->data)
157 free(self->data);
158 PyObject_Del(self);
161 static PyTypeObject PdataType = {
162 PyVarObject_HEAD_INIT(NULL, 0) "cPickle.Pdata", sizeof(Pdata), 0,
163 (destructor)Pdata_dealloc,
164 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
167 #define Pdata_Check(O) (Py_TYPE(O) == &PdataType)
169 static PyObject *
170 Pdata_New(void)
172 Pdata *self;
174 if (!(self = PyObject_New(Pdata, &PdataType)))
175 return NULL;
176 self->size = 8;
177 self->length = 0;
178 self->data = malloc(self->size * sizeof(PyObject*));
179 if (self->data)
180 return (PyObject*)self;
181 Py_DECREF(self);
182 return PyErr_NoMemory();
185 static int
186 stackUnderflow(void)
188 PyErr_SetString(UnpicklingError, "unpickling stack underflow");
189 return -1;
192 /* Retain only the initial clearto items. If clearto >= the current
193 * number of items, this is a (non-erroneous) NOP.
195 static int
196 Pdata_clear(Pdata *self, int clearto)
198 int i;
199 PyObject **p;
201 if (clearto < 0) return stackUnderflow();
202 if (clearto >= self->length) return 0;
204 for (i = self->length, p = self->data + clearto;
205 --i >= clearto;
206 p++) {
207 Py_CLEAR(*p);
209 self->length = clearto;
211 return 0;
214 static int
215 Pdata_grow(Pdata *self)
217 int bigger;
218 size_t nbytes;
219 PyObject **tmp;
221 bigger = self->size << 1;
222 if (bigger <= 0) /* was 0, or new value overflows */
223 goto nomemory;
224 if ((int)(size_t)bigger != bigger)
225 goto nomemory;
226 nbytes = (size_t)bigger * sizeof(PyObject *);
227 if (nbytes / sizeof(PyObject *) != (size_t)bigger)
228 goto nomemory;
229 tmp = realloc(self->data, nbytes);
230 if (tmp == NULL)
231 goto nomemory;
232 self->data = tmp;
233 self->size = bigger;
234 return 0;
236 nomemory:
237 PyErr_NoMemory();
238 return -1;
241 /* D is a Pdata*. Pop the topmost element and store it into V, which
242 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
243 * is raised and V is set to NULL. D and V may be evaluated several times.
245 #define PDATA_POP(D, V) { \
246 if ((D)->length) \
247 (V) = (D)->data[--((D)->length)]; \
248 else { \
249 PyErr_SetString(UnpicklingError, "bad pickle data"); \
250 (V) = NULL; \
254 /* PDATA_PUSH and PDATA_APPEND both push rvalue PyObject* O on to Pdata*
255 * D. If the Pdata stack can't be grown to hold the new value, both
256 * raise MemoryError and execute "return ER". The difference is in ownership
257 * of O after: _PUSH transfers ownership of O from the caller to the stack
258 * (no incref of O is done, and in case of error O is decrefed), while
259 * _APPEND pushes a new reference.
262 /* Push O on stack D, giving ownership of O to the stack. */
263 #define PDATA_PUSH(D, O, ER) { \
264 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
265 Pdata_grow((Pdata*)(D)) < 0) { \
266 Py_DECREF(O); \
267 return ER; \
269 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
272 /* Push O on stack D, pushing a new reference. */
273 #define PDATA_APPEND(D, O, ER) { \
274 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
275 Pdata_grow((Pdata*)(D)) < 0) \
276 return ER; \
277 Py_INCREF(O); \
278 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
282 static PyObject *
283 Pdata_popTuple(Pdata *self, int start)
285 PyObject *r;
286 int i, j, l;
288 l = self->length-start;
289 r = PyTuple_New(l);
290 if (r == NULL)
291 return NULL;
292 for (i = start, j = 0 ; j < l; i++, j++)
293 PyTuple_SET_ITEM(r, j, self->data[i]);
295 self->length = start;
296 return r;
299 static PyObject *
300 Pdata_popList(Pdata *self, int start)
302 PyObject *r;
303 int i, j, l;
305 l=self->length-start;
306 if (!( r=PyList_New(l))) return NULL;
307 for (i=start, j=0 ; j < l; i++, j++)
308 PyList_SET_ITEM(r, j, self->data[i]);
310 self->length=start;
311 return r;
314 /*************************************************************************/
316 #define ARG_TUP(self, o) { \
317 if (self->arg || (self->arg=PyTuple_New(1))) { \
318 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
319 PyTuple_SET_ITEM(self->arg,0,o); \
321 else { \
322 Py_DECREF(o); \
326 #define FREE_ARG_TUP(self) { \
327 if (Py_REFCNT(self->arg) > 1) { \
328 Py_DECREF(self->arg); \
329 self->arg=NULL; \
333 typedef struct Picklerobject {
334 PyObject_HEAD
335 FILE *fp;
336 PyObject *write;
337 PyObject *file;
338 PyObject *memo;
339 PyObject *arg;
340 PyObject *pers_func;
341 PyObject *inst_pers_func;
343 /* pickle protocol number, >= 0 */
344 int proto;
346 /* bool, true if proto > 0 */
347 int bin;
349 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
350 int (*write_func)(struct Picklerobject *, const char *, Py_ssize_t);
351 char *write_buf;
352 int buf_size;
353 PyObject *dispatch_table;
354 int fast_container; /* count nested container dumps */
355 PyObject *fast_memo;
356 } Picklerobject;
358 #ifndef PY_CPICKLE_FAST_LIMIT
359 #define PY_CPICKLE_FAST_LIMIT 50
360 #endif
362 static PyTypeObject Picklertype;
364 typedef struct Unpicklerobject {
365 PyObject_HEAD
366 FILE *fp;
367 PyObject *file;
368 PyObject *readline;
369 PyObject *read;
370 PyObject *memo;
371 PyObject *arg;
372 Pdata *stack;
373 PyObject *mark;
374 PyObject *pers_func;
375 PyObject *last_string;
376 int *marks;
377 int num_marks;
378 int marks_size;
379 Py_ssize_t (*read_func)(struct Unpicklerobject *, char **, Py_ssize_t);
380 Py_ssize_t (*readline_func)(struct Unpicklerobject *, char **);
381 int buf_size;
382 char *buf;
383 PyObject *find_class;
384 } Unpicklerobject;
386 static PyTypeObject Unpicklertype;
388 /* Forward decls that need the above structs */
389 static int save(Picklerobject *, PyObject *, int);
390 static int put2(Picklerobject *, PyObject *);
392 static
393 PyObject *
394 cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
396 va_list va;
397 PyObject *args=0, *retval=0;
398 va_start(va, format);
400 if (format) args = Py_VaBuildValue(format, va);
401 va_end(va);
402 if (format && ! args) return NULL;
403 if (stringformat && !(retval=PyString_FromString(stringformat)))
404 return NULL;
406 if (retval) {
407 if (args) {
408 PyObject *v;
409 v=PyString_Format(retval, args);
410 Py_DECREF(retval);
411 Py_DECREF(args);
412 if (! v) return NULL;
413 retval=v;
416 else
417 if (args) retval=args;
418 else {
419 PyErr_SetObject(ErrType,Py_None);
420 return NULL;
422 PyErr_SetObject(ErrType,retval);
423 Py_DECREF(retval);
424 return NULL;
427 static int
428 write_file(Picklerobject *self, const char *s, Py_ssize_t n)
430 size_t nbyteswritten;
432 if (s == NULL) {
433 return 0;
436 if (n > INT_MAX) {
437 /* String too large */
438 return -1;
441 PyFile_IncUseCount((PyFileObject *)self->file);
442 Py_BEGIN_ALLOW_THREADS
443 nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
444 Py_END_ALLOW_THREADS
445 PyFile_DecUseCount((PyFileObject *)self->file);
446 if (nbyteswritten != (size_t)n) {
447 PyErr_SetFromErrno(PyExc_IOError);
448 return -1;
451 return (int)n;
454 static int
455 write_cStringIO(Picklerobject *self, const char *s, Py_ssize_t n)
457 if (s == NULL) {
458 return 0;
461 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
462 return -1;
465 return (int)n;
468 static int
469 write_none(Picklerobject *self, const char *s, Py_ssize_t n)
471 if (s == NULL) return 0;
472 if (n > INT_MAX) return -1;
473 return (int)n;
476 static int
477 write_other(Picklerobject *self, const char *s, Py_ssize_t _n)
479 PyObject *py_str = 0, *junk = 0;
480 int n;
482 if (_n > INT_MAX)
483 return -1;
484 n = (int)_n;
485 if (s == NULL) {
486 if (!( self->buf_size )) return 0;
487 py_str = PyString_FromStringAndSize(self->write_buf,
488 self->buf_size);
489 if (!py_str)
490 return -1;
492 else {
493 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
494 if (write_other(self, NULL, 0) < 0)
495 return -1;
498 if (n > WRITE_BUF_SIZE) {
499 if (!( py_str =
500 PyString_FromStringAndSize(s, n)))
501 return -1;
503 else {
504 memcpy(self->write_buf + self->buf_size, s, n);
505 self->buf_size += n;
506 return n;
510 if (self->write) {
511 /* object with write method */
512 ARG_TUP(self, py_str);
513 if (self->arg) {
514 junk = PyObject_Call(self->write, self->arg, NULL);
515 FREE_ARG_TUP(self);
517 if (junk) Py_DECREF(junk);
518 else return -1;
520 else
521 PDATA_PUSH(self->file, py_str, -1);
523 self->buf_size = 0;
524 return n;
528 static Py_ssize_t
529 read_file(Unpicklerobject *self, char **s, Py_ssize_t n)
531 size_t nbytesread;
533 if (self->buf_size == 0) {
534 int size;
536 size = ((n < 32) ? 32 : n);
537 if (!( self->buf = (char *)malloc(size))) {
538 PyErr_NoMemory();
539 return -1;
542 self->buf_size = size;
544 else if (n > self->buf_size) {
545 char *newbuf = (char *)realloc(self->buf, n);
546 if (!newbuf) {
547 PyErr_NoMemory();
548 return -1;
550 self->buf = newbuf;
551 self->buf_size = n;
554 PyFile_IncUseCount((PyFileObject *)self->file);
555 Py_BEGIN_ALLOW_THREADS
556 nbytesread = fread(self->buf, sizeof(char), n, self->fp);
557 Py_END_ALLOW_THREADS
558 PyFile_DecUseCount((PyFileObject *)self->file);
559 if (nbytesread != (size_t)n) {
560 if (feof(self->fp)) {
561 PyErr_SetNone(PyExc_EOFError);
562 return -1;
565 PyErr_SetFromErrno(PyExc_IOError);
566 return -1;
569 *s = self->buf;
571 return n;
575 static Py_ssize_t
576 readline_file(Unpicklerobject *self, char **s)
578 int i;
580 if (self->buf_size == 0) {
581 if (!( self->buf = (char *)malloc(40))) {
582 PyErr_NoMemory();
583 return -1;
585 self->buf_size = 40;
588 i = 0;
589 while (1) {
590 int bigger;
591 char *newbuf;
592 for (; i < (self->buf_size - 1); i++) {
593 if (feof(self->fp) ||
594 (self->buf[i] = getc(self->fp)) == '\n') {
595 self->buf[i + 1] = '\0';
596 *s = self->buf;
597 return i + 1;
600 bigger = self->buf_size << 1;
601 if (bigger <= 0) { /* overflow */
602 PyErr_NoMemory();
603 return -1;
605 newbuf = (char *)realloc(self->buf, bigger);
606 if (!newbuf) {
607 PyErr_NoMemory();
608 return -1;
610 self->buf = newbuf;
611 self->buf_size = bigger;
616 static Py_ssize_t
617 read_cStringIO(Unpicklerobject *self, char **s, Py_ssize_t n)
619 char *ptr;
621 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
622 PyErr_SetNone(PyExc_EOFError);
623 return -1;
626 *s = ptr;
628 return n;
632 static Py_ssize_t
633 readline_cStringIO(Unpicklerobject *self, char **s)
635 Py_ssize_t n;
636 char *ptr;
638 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
639 return -1;
642 *s = ptr;
644 return n;
648 static Py_ssize_t
649 read_other(Unpicklerobject *self, char **s, Py_ssize_t n)
651 PyObject *bytes, *str=0;
653 if (!( bytes = PyInt_FromSsize_t(n))) return -1;
655 ARG_TUP(self, bytes);
656 if (self->arg) {
657 str = PyObject_Call(self->read, self->arg, NULL);
658 FREE_ARG_TUP(self);
660 if (! str) return -1;
662 Py_XDECREF(self->last_string);
663 self->last_string = str;
665 if (! (*s = PyString_AsString(str))) return -1;
667 if (PyString_GET_SIZE(str) != n) {
668 PyErr_SetNone(PyExc_EOFError);
669 return -1;
672 return n;
676 static Py_ssize_t
677 readline_other(Unpicklerobject *self, char **s)
679 PyObject *str;
680 Py_ssize_t str_size;
682 if (!( str = PyObject_CallObject(self->readline, empty_tuple))) {
683 return -1;
686 if ((str_size = PyString_Size(str)) < 0)
687 return -1;
689 Py_XDECREF(self->last_string);
690 self->last_string = str;
692 if (! (*s = PyString_AsString(str)))
693 return -1;
695 return str_size;
698 /* Copy the first n bytes from s into newly malloc'ed memory, plus a
699 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
700 * The caller is responsible for free()'ing the return value.
702 static char *
703 pystrndup(const char *s, int n)
705 char *r = (char *)malloc(n+1);
706 if (r == NULL)
707 return (char*)PyErr_NoMemory();
708 memcpy(r, s, n);
709 r[n] = 0;
710 return r;
714 static int
715 get(Picklerobject *self, PyObject *id)
717 PyObject *value, *mv;
718 long c_value;
719 char s[30];
720 size_t len;
722 if (!( mv = PyDict_GetItem(self->memo, id))) {
723 PyErr_SetObject(PyExc_KeyError, id);
724 return -1;
727 if (!( value = PyTuple_GetItem(mv, 0)))
728 return -1;
730 if (!( PyInt_Check(value))) {
731 PyErr_SetString(PicklingError, "no int where int expected in memo");
732 return -1;
734 c_value = PyInt_AS_LONG((PyIntObject*)value);
736 if (!self->bin) {
737 s[0] = GET;
738 PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value);
739 len = strlen(s);
741 else if (Pdata_Check(self->file)) {
742 if (write_other(self, NULL, 0) < 0) return -1;
743 PDATA_APPEND(self->file, mv, -1);
744 return 0;
746 else {
747 if (c_value < 256) {
748 s[0] = BINGET;
749 s[1] = (int)(c_value & 0xff);
750 len = 2;
752 else {
753 s[0] = LONG_BINGET;
754 s[1] = (int)(c_value & 0xff);
755 s[2] = (int)((c_value >> 8) & 0xff);
756 s[3] = (int)((c_value >> 16) & 0xff);
757 s[4] = (int)((c_value >> 24) & 0xff);
758 len = 5;
762 if (self->write_func(self, s, len) < 0)
763 return -1;
765 return 0;
769 static int
770 put(Picklerobject *self, PyObject *ob)
772 if (Py_REFCNT(ob) < 2 || self->fast)
773 return 0;
775 return put2(self, ob);
779 static int
780 put2(Picklerobject *self, PyObject *ob)
782 char c_str[30];
783 int p;
784 size_t len;
785 int res = -1;
786 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
788 if (self->fast)
789 return 0;
791 if ((p = PyDict_Size(self->memo)) < 0)
792 goto finally;
794 /* Make sure memo keys are positive! */
795 /* XXX Why?
796 * XXX And does "positive" really mean non-negative?
797 * XXX pickle.py starts with PUT index 0, not 1. This makes for
798 * XXX gratuitous differences between the pickling modules.
800 p++;
802 if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
803 goto finally;
805 if (!( memo_len = PyInt_FromLong(p)))
806 goto finally;
808 if (!( t = PyTuple_New(2)))
809 goto finally;
811 PyTuple_SET_ITEM(t, 0, memo_len);
812 Py_INCREF(memo_len);
813 PyTuple_SET_ITEM(t, 1, ob);
814 Py_INCREF(ob);
816 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
817 goto finally;
819 if (!self->bin) {
820 c_str[0] = PUT;
821 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p);
822 len = strlen(c_str);
824 else if (Pdata_Check(self->file)) {
825 if (write_other(self, NULL, 0) < 0) return -1;
826 PDATA_APPEND(self->file, memo_len, -1);
827 res=0; /* Job well done ;) */
828 goto finally;
830 else {
831 if (p >= 256) {
832 c_str[0] = LONG_BINPUT;
833 c_str[1] = (int)(p & 0xff);
834 c_str[2] = (int)((p >> 8) & 0xff);
835 c_str[3] = (int)((p >> 16) & 0xff);
836 c_str[4] = (int)((p >> 24) & 0xff);
837 len = 5;
839 else {
840 c_str[0] = BINPUT;
841 c_str[1] = p;
842 len = 2;
846 if (self->write_func(self, c_str, len) < 0)
847 goto finally;
849 res = 0;
851 finally:
852 Py_XDECREF(py_ob_id);
853 Py_XDECREF(memo_len);
854 Py_XDECREF(t);
856 return res;
859 static PyObject *
860 whichmodule(PyObject *global, PyObject *global_name)
862 Py_ssize_t i, j;
863 PyObject *module = 0, *modules_dict = 0,
864 *global_name_attr = 0, *name = 0;
866 module = PyObject_GetAttrString(global, "__module__");
867 if (module)
868 return module;
869 if (PyErr_ExceptionMatches(PyExc_AttributeError))
870 PyErr_Clear();
871 else
872 return NULL;
874 if (!( modules_dict = PySys_GetObject("modules")))
875 return NULL;
877 i = 0;
878 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
880 if (PyObject_Compare(name, __main___str)==0) continue;
882 global_name_attr = PyObject_GetAttr(module, global_name);
883 if (!global_name_attr) {
884 if (PyErr_ExceptionMatches(PyExc_AttributeError))
885 PyErr_Clear();
886 else
887 return NULL;
888 continue;
891 if (global_name_attr != global) {
892 Py_DECREF(global_name_attr);
893 continue;
896 Py_DECREF(global_name_attr);
898 break;
901 /* The following implements the rule in pickle.py added in 1.5
902 that used __main__ if no module is found. I don't actually
903 like this rule. jlf
905 if (!j) {
906 j=1;
907 name=__main___str;
910 Py_INCREF(name);
911 return name;
915 static int
916 fast_save_enter(Picklerobject *self, PyObject *obj)
918 /* if fast_container < 0, we're doing an error exit. */
919 if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
920 PyObject *key = NULL;
921 if (self->fast_memo == NULL) {
922 self->fast_memo = PyDict_New();
923 if (self->fast_memo == NULL) {
924 self->fast_container = -1;
925 return 0;
928 key = PyLong_FromVoidPtr(obj);
929 if (key == NULL)
930 return 0;
931 if (PyDict_GetItem(self->fast_memo, key)) {
932 Py_DECREF(key);
933 PyErr_Format(PyExc_ValueError,
934 "fast mode: can't pickle cyclic objects "
935 "including object type %s at %p",
936 Py_TYPE(obj)->tp_name, obj);
937 self->fast_container = -1;
938 return 0;
940 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
941 Py_DECREF(key);
942 self->fast_container = -1;
943 return 0;
945 Py_DECREF(key);
947 return 1;
951 fast_save_leave(Picklerobject *self, PyObject *obj)
953 if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
954 PyObject *key = PyLong_FromVoidPtr(obj);
955 if (key == NULL)
956 return 0;
957 if (PyDict_DelItem(self->fast_memo, key) < 0) {
958 Py_DECREF(key);
959 return 0;
961 Py_DECREF(key);
963 return 1;
966 static int
967 save_none(Picklerobject *self, PyObject *args)
969 static char none = NONE;
970 if (self->write_func(self, &none, 1) < 0)
971 return -1;
973 return 0;
976 static int
977 save_bool(Picklerobject *self, PyObject *args)
979 static const char *buf[2] = {FALSE, TRUE};
980 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
981 long l = PyInt_AS_LONG((PyIntObject *)args);
983 if (self->proto >= 2) {
984 char opcode = l ? NEWTRUE : NEWFALSE;
985 if (self->write_func(self, &opcode, 1) < 0)
986 return -1;
988 else if (self->write_func(self, buf[l], len[l]) < 0)
989 return -1;
990 return 0;
993 static int
994 save_int(Picklerobject *self, PyObject *args)
996 char c_str[32];
997 long l = PyInt_AS_LONG((PyIntObject *)args);
998 int len = 0;
1000 if (!self->bin
1001 #if SIZEOF_LONG > 4
1002 || l > 0x7fffffffL
1003 || l < -0x80000000L
1004 #endif
1006 /* Text-mode pickle, or long too big to fit in the 4-byte
1007 * signed BININT format: store as a string.
1009 c_str[0] = INT;
1010 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
1011 if (self->write_func(self, c_str, strlen(c_str)) < 0)
1012 return -1;
1014 else {
1015 /* Binary pickle and l fits in a signed 4-byte int. */
1016 c_str[1] = (int)( l & 0xff);
1017 c_str[2] = (int)((l >> 8) & 0xff);
1018 c_str[3] = (int)((l >> 16) & 0xff);
1019 c_str[4] = (int)((l >> 24) & 0xff);
1021 if ((c_str[4] == 0) && (c_str[3] == 0)) {
1022 if (c_str[2] == 0) {
1023 c_str[0] = BININT1;
1024 len = 2;
1026 else {
1027 c_str[0] = BININT2;
1028 len = 3;
1031 else {
1032 c_str[0] = BININT;
1033 len = 5;
1036 if (self->write_func(self, c_str, len) < 0)
1037 return -1;
1040 return 0;
1044 static int
1045 save_long(Picklerobject *self, PyObject *args)
1047 Py_ssize_t size;
1048 int res = -1;
1049 PyObject *repr = NULL;
1051 static char l = LONG;
1053 if (self->proto >= 2) {
1054 /* Linear-time pickling. */
1055 size_t nbits;
1056 size_t nbytes;
1057 unsigned char *pdata;
1058 char c_str[5];
1059 int i;
1060 int sign = _PyLong_Sign(args);
1062 if (sign == 0) {
1063 /* It's 0 -- an empty bytestring. */
1064 c_str[0] = LONG1;
1065 c_str[1] = 0;
1066 i = self->write_func(self, c_str, 2);
1067 if (i < 0) goto finally;
1068 res = 0;
1069 goto finally;
1071 nbits = _PyLong_NumBits(args);
1072 if (nbits == (size_t)-1 && PyErr_Occurred())
1073 goto finally;
1074 /* How many bytes do we need? There are nbits >> 3 full
1075 * bytes of data, and nbits & 7 leftover bits. If there
1076 * are any leftover bits, then we clearly need another
1077 * byte. Wnat's not so obvious is that we *probably*
1078 * need another byte even if there aren't any leftovers:
1079 * the most-significant bit of the most-significant byte
1080 * acts like a sign bit, and it's usually got a sense
1081 * opposite of the one we need. The exception is longs
1082 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1083 * its own 256's-complement, so has the right sign bit
1084 * even without the extra byte. That's a pain to check
1085 * for in advance, though, so we always grab an extra
1086 * byte at the start, and cut it back later if possible.
1088 nbytes = (nbits >> 3) + 1;
1089 if (nbytes > INT_MAX) {
1090 PyErr_SetString(PyExc_OverflowError, "long too large "
1091 "to pickle");
1092 goto finally;
1094 repr = PyString_FromStringAndSize(NULL, (int)nbytes);
1095 if (repr == NULL) goto finally;
1096 pdata = (unsigned char *)PyString_AS_STRING(repr);
1097 i = _PyLong_AsByteArray((PyLongObject *)args,
1098 pdata, nbytes,
1099 1 /* little endian */, 1 /* signed */);
1100 if (i < 0) goto finally;
1101 /* If the long is negative, this may be a byte more than
1102 * needed. This is so iff the MSB is all redundant sign
1103 * bits.
1105 if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
1106 (pdata[nbytes - 2] & 0x80) != 0)
1107 --nbytes;
1109 if (nbytes < 256) {
1110 c_str[0] = LONG1;
1111 c_str[1] = (char)nbytes;
1112 size = 2;
1114 else {
1115 c_str[0] = LONG4;
1116 size = (int)nbytes;
1117 for (i = 1; i < 5; i++) {
1118 c_str[i] = (char)(size & 0xff);
1119 size >>= 8;
1121 size = 5;
1123 i = self->write_func(self, c_str, size);
1124 if (i < 0) goto finally;
1125 i = self->write_func(self, (char *)pdata, (int)nbytes);
1126 if (i < 0) goto finally;
1127 res = 0;
1128 goto finally;
1131 /* proto < 2: write the repr and newline. This is quadratic-time
1132 * (in the number of digits), in both directions.
1134 if (!( repr = PyObject_Repr(args)))
1135 goto finally;
1137 if ((size = PyString_Size(repr)) < 0)
1138 goto finally;
1140 if (self->write_func(self, &l, 1) < 0)
1141 goto finally;
1143 if (self->write_func(self,
1144 PyString_AS_STRING((PyStringObject *)repr),
1145 size) < 0)
1146 goto finally;
1148 if (self->write_func(self, "\n", 1) < 0)
1149 goto finally;
1151 res = 0;
1153 finally:
1154 Py_XDECREF(repr);
1155 return res;
1159 static int
1160 save_float(Picklerobject *self, PyObject *args)
1162 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
1164 if (self->bin) {
1165 char str[9];
1166 str[0] = BINFLOAT;
1167 if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0)
1168 return -1;
1169 if (self->write_func(self, str, 9) < 0)
1170 return -1;
1172 else {
1173 char c_str[250];
1174 c_str[0] = FLOAT;
1175 _PyOS_double_to_string(c_str + 1, sizeof(c_str) - 2, x, 'g',
1176 17, 0, NULL);
1177 /* Extend the formatted string with a newline character */
1178 strcat(c_str, "\n");
1180 if (self->write_func(self, c_str, strlen(c_str)) < 0)
1181 return -1;
1184 return 0;
1188 static int
1189 save_string(Picklerobject *self, PyObject *args, int doput)
1191 int size, len;
1192 PyObject *repr=0;
1194 if ((size = PyString_Size(args)) < 0)
1195 return -1;
1197 if (!self->bin) {
1198 char *repr_str;
1200 static char string = STRING;
1202 if (!( repr = PyObject_Repr(args)))
1203 return -1;
1205 if ((len = PyString_Size(repr)) < 0)
1206 goto err;
1207 repr_str = PyString_AS_STRING((PyStringObject *)repr);
1209 if (self->write_func(self, &string, 1) < 0)
1210 goto err;
1212 if (self->write_func(self, repr_str, len) < 0)
1213 goto err;
1215 if (self->write_func(self, "\n", 1) < 0)
1216 goto err;
1218 Py_XDECREF(repr);
1220 else {
1221 int i;
1222 char c_str[5];
1224 if ((size = PyString_Size(args)) < 0)
1225 return -1;
1227 if (size < 256) {
1228 c_str[0] = SHORT_BINSTRING;
1229 c_str[1] = size;
1230 len = 2;
1232 else if (size <= INT_MAX) {
1233 c_str[0] = BINSTRING;
1234 for (i = 1; i < 5; i++)
1235 c_str[i] = (int)(size >> ((i - 1) * 8));
1236 len = 5;
1238 else
1239 return -1; /* string too large */
1241 if (self->write_func(self, c_str, len) < 0)
1242 return -1;
1244 if (size > 128 && Pdata_Check(self->file)) {
1245 if (write_other(self, NULL, 0) < 0) return -1;
1246 PDATA_APPEND(self->file, args, -1);
1248 else {
1249 if (self->write_func(self,
1250 PyString_AS_STRING(
1251 (PyStringObject *)args),
1252 size) < 0)
1253 return -1;
1257 if (doput)
1258 if (put(self, args) < 0)
1259 return -1;
1261 return 0;
1263 err:
1264 Py_XDECREF(repr);
1265 return -1;
1269 #ifdef Py_USING_UNICODE
1270 /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1271 backslash and newline characters to \uXXXX escapes. */
1272 static PyObject *
1273 modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size)
1275 PyObject *repr;
1276 char *p;
1277 char *q;
1279 static const char *hexdigit = "0123456789abcdef";
1280 #ifdef Py_UNICODE_WIDE
1281 const Py_ssize_t expandsize = 10;
1282 #else
1283 const Py_ssize_t expandsize = 6;
1284 #endif
1286 if (size > PY_SSIZE_T_MAX / expandsize)
1287 return PyErr_NoMemory();
1289 repr = PyString_FromStringAndSize(NULL, expandsize * size);
1290 if (repr == NULL)
1291 return NULL;
1292 if (size == 0)
1293 return repr;
1295 p = q = PyString_AS_STRING(repr);
1296 while (size-- > 0) {
1297 Py_UNICODE ch = *s++;
1298 #ifdef Py_UNICODE_WIDE
1299 /* Map 32-bit characters to '\Uxxxxxxxx' */
1300 if (ch >= 0x10000) {
1301 *p++ = '\\';
1302 *p++ = 'U';
1303 *p++ = hexdigit[(ch >> 28) & 0xf];
1304 *p++ = hexdigit[(ch >> 24) & 0xf];
1305 *p++ = hexdigit[(ch >> 20) & 0xf];
1306 *p++ = hexdigit[(ch >> 16) & 0xf];
1307 *p++ = hexdigit[(ch >> 12) & 0xf];
1308 *p++ = hexdigit[(ch >> 8) & 0xf];
1309 *p++ = hexdigit[(ch >> 4) & 0xf];
1310 *p++ = hexdigit[ch & 15];
1312 else
1313 #else
1314 /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */
1315 if (ch >= 0xD800 && ch < 0xDC00) {
1316 Py_UNICODE ch2;
1317 Py_UCS4 ucs;
1319 ch2 = *s++;
1320 size--;
1321 if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) {
1322 ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000;
1323 *p++ = '\\';
1324 *p++ = 'U';
1325 *p++ = hexdigit[(ucs >> 28) & 0xf];
1326 *p++ = hexdigit[(ucs >> 24) & 0xf];
1327 *p++ = hexdigit[(ucs >> 20) & 0xf];
1328 *p++ = hexdigit[(ucs >> 16) & 0xf];
1329 *p++ = hexdigit[(ucs >> 12) & 0xf];
1330 *p++ = hexdigit[(ucs >> 8) & 0xf];
1331 *p++ = hexdigit[(ucs >> 4) & 0xf];
1332 *p++ = hexdigit[ucs & 0xf];
1333 continue;
1335 /* Fall through: isolated surrogates are copied as-is */
1336 s--;
1337 size++;
1339 #endif
1340 /* Map 16-bit characters to '\uxxxx' */
1341 if (ch >= 256 || ch == '\\' || ch == '\n') {
1342 *p++ = '\\';
1343 *p++ = 'u';
1344 *p++ = hexdigit[(ch >> 12) & 0xf];
1345 *p++ = hexdigit[(ch >> 8) & 0xf];
1346 *p++ = hexdigit[(ch >> 4) & 0xf];
1347 *p++ = hexdigit[ch & 15];
1349 /* Copy everything else as-is */
1350 else
1351 *p++ = (char) ch;
1353 *p = '\0';
1354 _PyString_Resize(&repr, p - q);
1355 return repr;
1358 static int
1359 save_unicode(Picklerobject *self, PyObject *args, int doput)
1361 Py_ssize_t size, len;
1362 PyObject *repr=0;
1364 if (!PyUnicode_Check(args))
1365 return -1;
1367 if (!self->bin) {
1368 char *repr_str;
1369 static char string = UNICODE;
1371 repr = modified_EncodeRawUnicodeEscape(
1372 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
1373 if (!repr)
1374 return -1;
1376 if ((len = PyString_Size(repr)) < 0)
1377 goto err;
1378 repr_str = PyString_AS_STRING((PyStringObject *)repr);
1380 if (self->write_func(self, &string, 1) < 0)
1381 goto err;
1383 if (self->write_func(self, repr_str, len) < 0)
1384 goto err;
1386 if (self->write_func(self, "\n", 1) < 0)
1387 goto err;
1389 Py_XDECREF(repr);
1391 else {
1392 int i;
1393 char c_str[5];
1395 if (!( repr = PyUnicode_AsUTF8String(args)))
1396 return -1;
1398 if ((size = PyString_Size(repr)) < 0)
1399 goto err;
1400 if (size > INT_MAX)
1401 return -1; /* string too large */
1403 c_str[0] = BINUNICODE;
1404 for (i = 1; i < 5; i++)
1405 c_str[i] = (int)(size >> ((i - 1) * 8));
1406 len = 5;
1408 if (self->write_func(self, c_str, len) < 0)
1409 goto err;
1411 if (size > 128 && Pdata_Check(self->file)) {
1412 if (write_other(self, NULL, 0) < 0)
1413 goto err;
1414 PDATA_APPEND(self->file, repr, -1);
1416 else {
1417 if (self->write_func(self, PyString_AS_STRING(repr),
1418 size) < 0)
1419 goto err;
1422 Py_DECREF(repr);
1425 if (doput)
1426 if (put(self, args) < 0)
1427 return -1;
1429 return 0;
1431 err:
1432 Py_XDECREF(repr);
1433 return -1;
1435 #endif
1437 /* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1438 static int
1439 store_tuple_elements(Picklerobject *self, PyObject *t, int len)
1441 int i;
1442 int res = -1; /* guilty until proved innocent */
1444 assert(PyTuple_Size(t) == len);
1446 for (i = 0; i < len; i++) {
1447 PyObject *element = PyTuple_GET_ITEM(t, i);
1449 if (element == NULL)
1450 goto finally;
1451 if (save(self, element, 0) < 0)
1452 goto finally;
1454 res = 0;
1456 finally:
1457 return res;
1460 /* Tuples are ubiquitous in the pickle protocols, so many techniques are
1461 * used across protocols to minimize the space needed to pickle them.
1462 * Tuples are also the only builtin immutable type that can be recursive
1463 * (a tuple can be reached from itself), and that requires some subtle
1464 * magic so that it works in all cases. IOW, this is a long routine.
1466 static int
1467 save_tuple(Picklerobject *self, PyObject *args)
1469 PyObject *py_tuple_id = NULL;
1470 int len, i;
1471 int res = -1;
1473 static char tuple = TUPLE;
1474 static char pop = POP;
1475 static char pop_mark = POP_MARK;
1476 static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
1478 if ((len = PyTuple_Size(args)) < 0)
1479 goto finally;
1481 if (len == 0) {
1482 char c_str[2];
1484 if (self->proto) {
1485 c_str[0] = EMPTY_TUPLE;
1486 len = 1;
1488 else {
1489 c_str[0] = MARK;
1490 c_str[1] = TUPLE;
1491 len = 2;
1493 if (self->write_func(self, c_str, len) >= 0)
1494 res = 0;
1495 /* Don't memoize an empty tuple. */
1496 goto finally;
1499 /* A non-empty tuple. */
1501 /* id(tuple) isn't in the memo now. If it shows up there after
1502 * saving the tuple elements, the tuple must be recursive, in
1503 * which case we'll pop everything we put on the stack, and fetch
1504 * its value from the memo.
1506 py_tuple_id = PyLong_FromVoidPtr(args);
1507 if (py_tuple_id == NULL)
1508 goto finally;
1510 if (len <= 3 && self->proto >= 2) {
1511 /* Use TUPLE{1,2,3} opcodes. */
1512 if (store_tuple_elements(self, args, len) < 0)
1513 goto finally;
1514 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1515 /* pop the len elements */
1516 for (i = 0; i < len; ++i)
1517 if (self->write_func(self, &pop, 1) < 0)
1518 goto finally;
1519 /* fetch from memo */
1520 if (get(self, py_tuple_id) < 0)
1521 goto finally;
1522 res = 0;
1523 goto finally;
1525 /* Not recursive. */
1526 if (self->write_func(self, len2opcode + len, 1) < 0)
1527 goto finally;
1528 goto memoize;
1531 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1532 * Generate MARK elt1 elt2 ... TUPLE
1534 if (self->write_func(self, &MARKv, 1) < 0)
1535 goto finally;
1537 if (store_tuple_elements(self, args, len) < 0)
1538 goto finally;
1540 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1541 /* pop the stack stuff we pushed */
1542 if (self->bin) {
1543 if (self->write_func(self, &pop_mark, 1) < 0)
1544 goto finally;
1546 else {
1547 /* Note that we pop one more than len, to remove
1548 * the MARK too.
1550 for (i = 0; i <= len; i++)
1551 if (self->write_func(self, &pop, 1) < 0)
1552 goto finally;
1554 /* fetch from memo */
1555 if (get(self, py_tuple_id) >= 0)
1556 res = 0;
1557 goto finally;
1560 /* Not recursive. */
1561 if (self->write_func(self, &tuple, 1) < 0)
1562 goto finally;
1564 memoize:
1565 if (put(self, args) >= 0)
1566 res = 0;
1568 finally:
1569 Py_XDECREF(py_tuple_id);
1570 return res;
1573 /* iter is an iterator giving items, and we batch up chunks of
1574 * MARK item item ... item APPENDS
1575 * opcode sequences. Calling code should have arranged to first create an
1576 * empty list, or list-like object, for the APPENDS to operate on.
1577 * Returns 0 on success, <0 on error.
1579 static int
1580 batch_list(Picklerobject *self, PyObject *iter)
1582 PyObject *obj = NULL;
1583 PyObject *firstitem = NULL;
1584 int i, n;
1586 static char append = APPEND;
1587 static char appends = APPENDS;
1589 assert(iter != NULL);
1591 if (self->proto == 0) {
1592 /* APPENDS isn't available; do one at a time. */
1593 for (;;) {
1594 obj = PyIter_Next(iter);
1595 if (obj == NULL) {
1596 if (PyErr_Occurred())
1597 return -1;
1598 break;
1600 i = save(self, obj, 0);
1601 Py_DECREF(obj);
1602 if (i < 0)
1603 return -1;
1604 if (self->write_func(self, &append, 1) < 0)
1605 return -1;
1607 return 0;
1610 /* proto > 0: write in batches of BATCHSIZE. */
1611 do {
1612 /* Get first item */
1613 firstitem = PyIter_Next(iter);
1614 if (firstitem == NULL) {
1615 if (PyErr_Occurred())
1616 goto BatchFailed;
1618 /* nothing more to add */
1619 break;
1622 /* Try to get a second item */
1623 obj = PyIter_Next(iter);
1624 if (obj == NULL) {
1625 if (PyErr_Occurred())
1626 goto BatchFailed;
1628 /* Only one item to write */
1629 if (save(self, firstitem, 0) < 0)
1630 goto BatchFailed;
1631 if (self->write_func(self, &append, 1) < 0)
1632 goto BatchFailed;
1633 Py_CLEAR(firstitem);
1634 break;
1637 /* More than one item to write */
1639 /* Pump out MARK, items, APPENDS. */
1640 if (self->write_func(self, &MARKv, 1) < 0)
1641 goto BatchFailed;
1643 if (save(self, firstitem, 0) < 0)
1644 goto BatchFailed;
1645 Py_CLEAR(firstitem);
1646 n = 1;
1648 /* Fetch and save up to BATCHSIZE items */
1649 while (obj) {
1650 if (save(self, obj, 0) < 0)
1651 goto BatchFailed;
1652 Py_CLEAR(obj);
1653 n += 1;
1655 if (n == BATCHSIZE)
1656 break;
1658 obj = PyIter_Next(iter);
1659 if (obj == NULL) {
1660 if (PyErr_Occurred())
1661 goto BatchFailed;
1662 break;
1666 if (self->write_func(self, &appends, 1) < 0)
1667 goto BatchFailed;
1669 } while (n == BATCHSIZE);
1670 return 0;
1672 BatchFailed:
1673 Py_XDECREF(firstitem);
1674 Py_XDECREF(obj);
1675 return -1;
1678 static int
1679 save_list(Picklerobject *self, PyObject *args)
1681 int res = -1;
1682 char s[3];
1683 int len;
1684 PyObject *iter;
1686 if (self->fast && !fast_save_enter(self, args))
1687 goto finally;
1689 /* Create an empty list. */
1690 if (self->bin) {
1691 s[0] = EMPTY_LIST;
1692 len = 1;
1694 else {
1695 s[0] = MARK;
1696 s[1] = LIST;
1697 len = 2;
1700 if (self->write_func(self, s, len) < 0)
1701 goto finally;
1703 /* Get list length, and bow out early if empty. */
1704 if ((len = PyList_Size(args)) < 0)
1705 goto finally;
1707 /* Memoize. */
1708 if (len == 0) {
1709 if (put(self, args) >= 0)
1710 res = 0;
1711 goto finally;
1713 if (put2(self, args) < 0)
1714 goto finally;
1716 /* Materialize the list elements. */
1717 iter = PyObject_GetIter(args);
1718 if (iter == NULL)
1719 goto finally;
1721 if (Py_EnterRecursiveCall(" while pickling an object") == 0)
1723 res = batch_list(self, iter);
1724 Py_LeaveRecursiveCall();
1726 Py_DECREF(iter);
1728 finally:
1729 if (self->fast && !fast_save_leave(self, args))
1730 res = -1;
1732 return res;
1736 /* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1737 * MARK key value ... key value SETITEMS
1738 * opcode sequences. Calling code should have arranged to first create an
1739 * empty dict, or dict-like object, for the SETITEMS to operate on.
1740 * Returns 0 on success, <0 on error.
1742 * This is very much like batch_list(). The difference between saving
1743 * elements directly, and picking apart two-tuples, is so long-winded at
1744 * the C level, though, that attempts to combine these routines were too
1745 * ugly to bear.
1747 static int
1748 batch_dict(Picklerobject *self, PyObject *iter)
1750 PyObject *p = NULL;
1751 PyObject *firstitem = NULL;
1752 int i, n;
1754 static char setitem = SETITEM;
1755 static char setitems = SETITEMS;
1757 assert(iter != NULL);
1759 if (self->proto == 0) {
1760 /* SETITEMS isn't available; do one at a time. */
1761 for (;;) {
1762 p = PyIter_Next(iter);
1763 if (p == NULL) {
1764 if (PyErr_Occurred())
1765 return -1;
1766 break;
1768 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1769 PyErr_SetString(PyExc_TypeError, "dict items "
1770 "iterator must return 2-tuples");
1771 return -1;
1773 i = save(self, PyTuple_GET_ITEM(p, 0), 0);
1774 if (i >= 0)
1775 i = save(self, PyTuple_GET_ITEM(p, 1), 0);
1776 Py_DECREF(p);
1777 if (i < 0)
1778 return -1;
1779 if (self->write_func(self, &setitem, 1) < 0)
1780 return -1;
1782 return 0;
1785 /* proto > 0: write in batches of BATCHSIZE. */
1786 do {
1787 /* Get first item */
1788 firstitem = PyIter_Next(iter);
1789 if (firstitem == NULL) {
1790 if (PyErr_Occurred())
1791 goto BatchFailed;
1793 /* nothing more to add */
1794 break;
1796 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
1797 PyErr_SetString(PyExc_TypeError, "dict items "
1798 "iterator must return 2-tuples");
1799 goto BatchFailed;
1802 /* Try to get a second item */
1803 p = PyIter_Next(iter);
1804 if (p == NULL) {
1805 if (PyErr_Occurred())
1806 goto BatchFailed;
1808 /* Only one item to write */
1809 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
1810 goto BatchFailed;
1811 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
1812 goto BatchFailed;
1813 if (self->write_func(self, &setitem, 1) < 0)
1814 goto BatchFailed;
1815 Py_CLEAR(firstitem);
1816 break;
1819 /* More than one item to write */
1821 /* Pump out MARK, items, SETITEMS. */
1822 if (self->write_func(self, &MARKv, 1) < 0)
1823 goto BatchFailed;
1825 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
1826 goto BatchFailed;
1827 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
1828 goto BatchFailed;
1829 Py_CLEAR(firstitem);
1830 n = 1;
1832 /* Fetch and save up to BATCHSIZE items */
1833 while (p) {
1834 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1835 PyErr_SetString(PyExc_TypeError, "dict items "
1836 "iterator must return 2-tuples");
1837 goto BatchFailed;
1839 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1840 goto BatchFailed;
1841 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1842 goto BatchFailed;
1843 Py_CLEAR(p);
1844 n += 1;
1846 if (n == BATCHSIZE)
1847 break;
1849 p = PyIter_Next(iter);
1850 if (p == NULL) {
1851 if (PyErr_Occurred())
1852 goto BatchFailed;
1853 break;
1857 if (self->write_func(self, &setitems, 1) < 0)
1858 goto BatchFailed;
1860 } while (n == BATCHSIZE);
1861 return 0;
1863 BatchFailed:
1864 Py_XDECREF(firstitem);
1865 Py_XDECREF(p);
1866 return -1;
1869 /* This is a variant of batch_dict() above that specializes for dicts, with no
1870 * support for dict subclasses. Like batch_dict(), we batch up chunks of
1871 * MARK key value ... key value SETITEMS
1872 * opcode sequences. Calling code should have arranged to first create an
1873 * empty dict, or dict-like object, for the SETITEMS to operate on.
1874 * Returns 0 on success, -1 on error.
1876 * Note that this currently doesn't work for protocol 0.
1878 static int
1879 batch_dict_exact(Picklerobject *self, PyObject *obj)
1881 PyObject *key = NULL, *value = NULL;
1882 int i;
1883 Py_ssize_t dict_size, ppos = 0;
1885 static char setitem = SETITEM;
1886 static char setitems = SETITEMS;
1888 assert(obj != NULL);
1889 assert(self->proto > 0);
1891 dict_size = PyDict_Size(obj);
1893 /* Special-case len(d) == 1 to save space. */
1894 if (dict_size == 1) {
1895 PyDict_Next(obj, &ppos, &key, &value);
1896 if (save(self, key, 0) < 0)
1897 return -1;
1898 if (save(self, value, 0) < 0)
1899 return -1;
1900 if (self->write_func(self, &setitem, 1) < 0)
1901 return -1;
1902 return 0;
1905 /* Write in batches of BATCHSIZE. */
1906 do {
1907 i = 0;
1908 if (self->write_func(self, &MARKv, 1) < 0)
1909 return -1;
1910 while (PyDict_Next(obj, &ppos, &key, &value)) {
1911 if (save(self, key, 0) < 0)
1912 return -1;
1913 if (save(self, value, 0) < 0)
1914 return -1;
1915 if (++i == BATCHSIZE)
1916 break;
1918 if (self->write_func(self, &setitems, 1) < 0)
1919 return -1;
1920 if (PyDict_Size(obj) != dict_size) {
1921 PyErr_Format(
1922 PyExc_RuntimeError,
1923 "dictionary changed size during iteration");
1924 return -1;
1927 } while (i == BATCHSIZE);
1928 return 0;
1931 static int
1932 save_dict(Picklerobject *self, PyObject *args)
1934 int res = -1;
1935 char s[3];
1936 int len;
1938 if (self->fast && !fast_save_enter(self, args))
1939 goto finally;
1941 /* Create an empty dict. */
1942 if (self->bin) {
1943 s[0] = EMPTY_DICT;
1944 len = 1;
1946 else {
1947 s[0] = MARK;
1948 s[1] = DICT;
1949 len = 2;
1952 if (self->write_func(self, s, len) < 0)
1953 goto finally;
1955 /* Get dict size, and bow out early if empty. */
1956 if ((len = PyDict_Size(args)) < 0)
1957 goto finally;
1959 if (len == 0) {
1960 if (put(self, args) >= 0)
1961 res = 0;
1962 goto finally;
1964 if (put2(self, args) < 0)
1965 goto finally;
1967 /* Materialize the dict items. */
1968 if (PyDict_CheckExact(args) && self->proto > 0) {
1969 /* We can take certain shortcuts if we know this is a dict and
1970 not a dict subclass. */
1971 if (Py_EnterRecursiveCall(" while pickling an object") == 0) {
1972 res = batch_dict_exact(self, args);
1973 Py_LeaveRecursiveCall();
1975 } else {
1976 PyObject *iter = PyObject_CallMethod(args, "iteritems", "()");
1977 if (iter == NULL)
1978 goto finally;
1979 if (Py_EnterRecursiveCall(" while pickling an object") == 0) {
1980 res = batch_dict(self, iter);
1981 Py_LeaveRecursiveCall();
1983 Py_DECREF(iter);
1986 finally:
1987 if (self->fast && !fast_save_leave(self, args))
1988 res = -1;
1990 return res;
1994 static int
1995 save_inst(Picklerobject *self, PyObject *args)
1997 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1998 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1999 char *module_str, *name_str;
2000 int module_size, name_size, res = -1;
2002 static char inst = INST, obj = OBJ, build = BUILD;
2004 if (self->fast && !fast_save_enter(self, args))
2005 goto finally;
2007 if (self->write_func(self, &MARKv, 1) < 0)
2008 goto finally;
2010 if (!( class = PyObject_GetAttr(args, __class___str)))
2011 goto finally;
2013 if (self->bin) {
2014 if (save(self, class, 0) < 0)
2015 goto finally;
2018 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
2019 PyObject *element = 0;
2020 int i, len;
2022 if (!( class_args =
2023 PyObject_Call(getinitargs_func, empty_tuple, NULL)))
2024 goto finally;
2026 if ((len = PyObject_Size(class_args)) < 0)
2027 goto finally;
2029 for (i = 0; i < len; i++) {
2030 if (!( element = PySequence_GetItem(class_args, i)))
2031 goto finally;
2033 if (save(self, element, 0) < 0) {
2034 Py_DECREF(element);
2035 goto finally;
2038 Py_DECREF(element);
2041 else {
2042 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2043 PyErr_Clear();
2044 else
2045 goto finally;
2048 if (!self->bin) {
2049 if (!( name = ((PyClassObject *)class)->cl_name )) {
2050 PyErr_SetString(PicklingError, "class has no name");
2051 goto finally;
2054 if (!( module = whichmodule(class, name)))
2055 goto finally;
2058 if ((module_size = PyString_Size(module)) < 0 ||
2059 (name_size = PyString_Size(name)) < 0)
2060 goto finally;
2062 module_str = PyString_AS_STRING((PyStringObject *)module);
2063 name_str = PyString_AS_STRING((PyStringObject *)name);
2065 if (self->write_func(self, &inst, 1) < 0)
2066 goto finally;
2068 if (self->write_func(self, module_str, module_size) < 0)
2069 goto finally;
2071 if (self->write_func(self, "\n", 1) < 0)
2072 goto finally;
2074 if (self->write_func(self, name_str, name_size) < 0)
2075 goto finally;
2077 if (self->write_func(self, "\n", 1) < 0)
2078 goto finally;
2080 else if (self->write_func(self, &obj, 1) < 0) {
2081 goto finally;
2084 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
2085 state = PyObject_Call(getstate_func, empty_tuple, NULL);
2086 if (!state)
2087 goto finally;
2089 else {
2090 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2091 PyErr_Clear();
2092 else
2093 goto finally;
2095 if (!( state = PyObject_GetAttr(args, __dict___str))) {
2096 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2097 PyErr_Clear();
2098 else
2099 goto finally;
2100 res = 0;
2101 goto finally;
2105 if (!PyDict_Check(state)) {
2106 if (put2(self, args) < 0)
2107 goto finally;
2109 else {
2110 if (put(self, args) < 0)
2111 goto finally;
2114 if (save(self, state, 0) < 0)
2115 goto finally;
2117 if (self->write_func(self, &build, 1) < 0)
2118 goto finally;
2120 res = 0;
2122 finally:
2123 if (self->fast && !fast_save_leave(self, args))
2124 res = -1;
2126 Py_XDECREF(module);
2127 Py_XDECREF(class);
2128 Py_XDECREF(state);
2129 Py_XDECREF(getinitargs_func);
2130 Py_XDECREF(getstate_func);
2131 Py_XDECREF(class_args);
2133 return res;
2137 static int
2138 save_global(Picklerobject *self, PyObject *args, PyObject *name)
2140 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
2141 char *name_str, *module_str;
2142 int module_size, name_size, res = -1;
2144 static char global = GLOBAL;
2146 if (name) {
2147 global_name = name;
2148 Py_INCREF(global_name);
2150 else {
2151 if (!( global_name = PyObject_GetAttr(args, __name___str)))
2152 goto finally;
2155 if (!( module = whichmodule(args, global_name)))
2156 goto finally;
2158 if ((module_size = PyString_Size(module)) < 0 ||
2159 (name_size = PyString_Size(global_name)) < 0)
2160 goto finally;
2162 module_str = PyString_AS_STRING((PyStringObject *)module);
2163 name_str = PyString_AS_STRING((PyStringObject *)global_name);
2165 /* XXX This can be doing a relative import. Clearly it shouldn't,
2166 but I don't know how to stop it. :-( */
2167 mod = PyImport_ImportModule(module_str);
2168 if (mod == NULL) {
2169 cPickle_ErrFormat(PicklingError,
2170 "Can't pickle %s: import of module %s "
2171 "failed",
2172 "OS", args, module);
2173 goto finally;
2175 klass = PyObject_GetAttrString(mod, name_str);
2176 if (klass == NULL) {
2177 cPickle_ErrFormat(PicklingError,
2178 "Can't pickle %s: attribute lookup %s.%s "
2179 "failed",
2180 "OSS", args, module, global_name);
2181 goto finally;
2183 if (klass != args) {
2184 Py_DECREF(klass);
2185 cPickle_ErrFormat(PicklingError,
2186 "Can't pickle %s: it's not the same object "
2187 "as %s.%s",
2188 "OSS", args, module, global_name);
2189 goto finally;
2191 Py_DECREF(klass);
2193 if (self->proto >= 2) {
2194 /* See whether this is in the extension registry, and if
2195 * so generate an EXT opcode.
2197 PyObject *py_code; /* extension code as Python object */
2198 long code; /* extension code as C value */
2199 char c_str[5];
2200 int n;
2202 PyTuple_SET_ITEM(two_tuple, 0, module);
2203 PyTuple_SET_ITEM(two_tuple, 1, global_name);
2204 py_code = PyDict_GetItem(extension_registry, two_tuple);
2205 if (py_code == NULL)
2206 goto gen_global; /* not registered */
2208 /* Verify py_code has the right type and value. */
2209 if (!PyInt_Check(py_code)) {
2210 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2211 "extension code %s isn't an integer",
2212 "OO", args, py_code);
2213 goto finally;
2215 code = PyInt_AS_LONG(py_code);
2216 if (code <= 0 || code > 0x7fffffffL) {
2217 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2218 "extension code %ld is out of range",
2219 "Ol", args, code);
2220 goto finally;
2223 /* Generate an EXT opcode. */
2224 if (code <= 0xff) {
2225 c_str[0] = EXT1;
2226 c_str[1] = (char)code;
2227 n = 2;
2229 else if (code <= 0xffff) {
2230 c_str[0] = EXT2;
2231 c_str[1] = (char)(code & 0xff);
2232 c_str[2] = (char)((code >> 8) & 0xff);
2233 n = 3;
2235 else {
2236 c_str[0] = EXT4;
2237 c_str[1] = (char)(code & 0xff);
2238 c_str[2] = (char)((code >> 8) & 0xff);
2239 c_str[3] = (char)((code >> 16) & 0xff);
2240 c_str[4] = (char)((code >> 24) & 0xff);
2241 n = 5;
2244 if (self->write_func(self, c_str, n) >= 0)
2245 res = 0;
2246 goto finally; /* and don't memoize */
2249 gen_global:
2250 if (self->write_func(self, &global, 1) < 0)
2251 goto finally;
2253 if (self->write_func(self, module_str, module_size) < 0)
2254 goto finally;
2256 if (self->write_func(self, "\n", 1) < 0)
2257 goto finally;
2259 if (self->write_func(self, name_str, name_size) < 0)
2260 goto finally;
2262 if (self->write_func(self, "\n", 1) < 0)
2263 goto finally;
2265 if (put(self, args) < 0)
2266 goto finally;
2268 res = 0;
2270 finally:
2271 Py_XDECREF(module);
2272 Py_XDECREF(global_name);
2273 Py_XDECREF(mod);
2275 return res;
2278 static int
2279 save_pers(Picklerobject *self, PyObject *args, PyObject *f)
2281 PyObject *pid = 0;
2282 int size, res = -1;
2284 static char persid = PERSID, binpersid = BINPERSID;
2286 Py_INCREF(args);
2287 ARG_TUP(self, args);
2288 if (self->arg) {
2289 pid = PyObject_Call(f, self->arg, NULL);
2290 FREE_ARG_TUP(self);
2292 if (! pid) return -1;
2294 if (pid != Py_None) {
2295 if (!self->bin) {
2296 if (!PyString_Check(pid)) {
2297 PyErr_SetString(PicklingError,
2298 "persistent id must be string");
2299 goto finally;
2302 if (self->write_func(self, &persid, 1) < 0)
2303 goto finally;
2305 if ((size = PyString_Size(pid)) < 0)
2306 goto finally;
2308 if (self->write_func(self,
2309 PyString_AS_STRING(
2310 (PyStringObject *)pid),
2311 size) < 0)
2312 goto finally;
2314 if (self->write_func(self, "\n", 1) < 0)
2315 goto finally;
2317 res = 1;
2318 goto finally;
2320 else if (save(self, pid, 1) >= 0) {
2321 if (self->write_func(self, &binpersid, 1) < 0)
2322 res = -1;
2323 else
2324 res = 1;
2327 goto finally;
2330 res = 0;
2332 finally:
2333 Py_XDECREF(pid);
2335 return res;
2338 /* We're saving ob, and args is the 2-thru-5 tuple returned by the
2339 * appropriate __reduce__ method for ob.
2341 static int
2342 save_reduce(Picklerobject *self, PyObject *args, PyObject *fn, PyObject *ob)
2344 PyObject *callable;
2345 PyObject *argtup;
2346 PyObject *state = NULL;
2347 PyObject *listitems = Py_None;
2348 PyObject *dictitems = Py_None;
2349 Py_ssize_t size;
2351 int use_newobj = self->proto >= 2;
2353 static char reduce = REDUCE;
2354 static char build = BUILD;
2355 static char newobj = NEWOBJ;
2357 size = PyTuple_Size(args);
2358 if (size < 2 || size > 5) {
2359 cPickle_ErrFormat(PicklingError, "tuple returned by "
2360 "%s must contain 2 through 5 elements",
2361 "O", fn);
2362 return -1;
2365 if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2366 &callable,
2367 &argtup,
2368 &state,
2369 &listitems,
2370 &dictitems))
2371 return -1;
2373 if (!PyTuple_Check(argtup)) {
2374 cPickle_ErrFormat(PicklingError, "Second element of "
2375 "tuple returned by %s must be a tuple",
2376 "O", fn);
2377 return -1;
2380 if (state == Py_None)
2381 state = NULL;
2383 if (listitems == Py_None)
2384 listitems = NULL;
2385 else if (!PyIter_Check(listitems)) {
2386 cPickle_ErrFormat(PicklingError, "Fourth element of "
2387 "tuple returned by %s must be an iterator, not %s",
2388 "Os", fn, Py_TYPE(listitems)->tp_name);
2389 return -1;
2392 if (dictitems == Py_None)
2393 dictitems = NULL;
2394 else if (!PyIter_Check(dictitems)) {
2395 cPickle_ErrFormat(PicklingError, "Fifth element of "
2396 "tuple returned by %s must be an iterator, not %s",
2397 "Os", fn, Py_TYPE(dictitems)->tp_name);
2398 return -1;
2401 /* Protocol 2 special case: if callable's name is __newobj__, use
2402 * NEWOBJ. This consumes a lot of code.
2404 if (use_newobj) {
2405 PyObject *temp = PyObject_GetAttr(callable, __name___str);
2407 if (temp == NULL) {
2408 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2409 PyErr_Clear();
2410 else
2411 return -1;
2412 use_newobj = 0;
2414 else {
2415 use_newobj = PyString_Check(temp) &&
2416 strcmp(PyString_AS_STRING(temp),
2417 "__newobj__") == 0;
2418 Py_DECREF(temp);
2421 if (use_newobj) {
2422 PyObject *cls;
2423 PyObject *newargtup;
2424 int n, i;
2426 /* Sanity checks. */
2427 n = PyTuple_Size(argtup);
2428 if (n < 1) {
2429 PyErr_SetString(PicklingError, "__newobj__ arglist "
2430 "is empty");
2431 return -1;
2434 cls = PyTuple_GET_ITEM(argtup, 0);
2435 if (! PyObject_HasAttrString(cls, "__new__")) {
2436 PyErr_SetString(PicklingError, "args[0] from "
2437 "__newobj__ args has no __new__");
2438 return -1;
2441 /* XXX How could ob be NULL? */
2442 if (ob != NULL) {
2443 PyObject *ob_dot_class;
2445 ob_dot_class = PyObject_GetAttr(ob, __class___str);
2446 if (ob_dot_class == NULL) {
2447 if (PyErr_ExceptionMatches(
2448 PyExc_AttributeError))
2449 PyErr_Clear();
2450 else
2451 return -1;
2453 i = ob_dot_class != cls; /* true iff a problem */
2454 Py_XDECREF(ob_dot_class);
2455 if (i) {
2456 PyErr_SetString(PicklingError, "args[0] from "
2457 "__newobj__ args has the wrong class");
2458 return -1;
2462 /* Save the class and its __new__ arguments. */
2463 if (save(self, cls, 0) < 0)
2464 return -1;
2466 newargtup = PyTuple_New(n-1); /* argtup[1:] */
2467 if (newargtup == NULL)
2468 return -1;
2469 for (i = 1; i < n; ++i) {
2470 PyObject *temp = PyTuple_GET_ITEM(argtup, i);
2471 Py_INCREF(temp);
2472 PyTuple_SET_ITEM(newargtup, i-1, temp);
2474 i = save(self, newargtup, 0);
2475 Py_DECREF(newargtup);
2476 if (i < 0)
2477 return -1;
2479 /* Add NEWOBJ opcode. */
2480 if (self->write_func(self, &newobj, 1) < 0)
2481 return -1;
2483 else {
2484 /* Not using NEWOBJ. */
2485 if (save(self, callable, 0) < 0 ||
2486 save(self, argtup, 0) < 0 ||
2487 self->write_func(self, &reduce, 1) < 0)
2488 return -1;
2491 /* Memoize. */
2492 /* XXX How can ob be NULL? */
2493 if (ob != NULL) {
2494 if (state && !PyDict_Check(state)) {
2495 if (put2(self, ob) < 0)
2496 return -1;
2498 else if (put(self, ob) < 0)
2499 return -1;
2503 if (listitems && batch_list(self, listitems) < 0)
2504 return -1;
2506 if (dictitems && batch_dict(self, dictitems) < 0)
2507 return -1;
2509 if (state) {
2510 if (save(self, state, 0) < 0 ||
2511 self->write_func(self, &build, 1) < 0)
2512 return -1;
2515 return 0;
2518 static int
2519 save(Picklerobject *self, PyObject *args, int pers_save)
2521 PyTypeObject *type;
2522 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
2523 int res = -1;
2524 int tmp;
2526 if (Py_EnterRecursiveCall(" while pickling an object"))
2527 return -1;
2529 if (!pers_save && self->pers_func) {
2530 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2531 res = tmp;
2532 goto finally;
2536 if (args == Py_None) {
2537 res = save_none(self, args);
2538 goto finally;
2541 type = Py_TYPE(args);
2543 switch (type->tp_name[0]) {
2544 case 'b':
2545 if (args == Py_False || args == Py_True) {
2546 res = save_bool(self, args);
2547 goto finally;
2549 break;
2550 case 'i':
2551 if (type == &PyInt_Type) {
2552 res = save_int(self, args);
2553 goto finally;
2555 break;
2557 case 'l':
2558 if (type == &PyLong_Type) {
2559 res = save_long(self, args);
2560 goto finally;
2562 break;
2564 case 'f':
2565 if (type == &PyFloat_Type) {
2566 res = save_float(self, args);
2567 goto finally;
2569 break;
2571 case 't':
2572 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2573 res = save_tuple(self, args);
2574 goto finally;
2576 break;
2578 case 's':
2579 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
2580 res = save_string(self, args, 0);
2581 goto finally;
2583 break;
2585 #ifdef Py_USING_UNICODE
2586 case 'u':
2587 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
2588 res = save_unicode(self, args, 0);
2589 goto finally;
2591 break;
2592 #endif
2595 if (Py_REFCNT(args) > 1) {
2596 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
2597 goto finally;
2599 if (PyDict_GetItem(self->memo, py_ob_id)) {
2600 if (get(self, py_ob_id) < 0)
2601 goto finally;
2603 res = 0;
2604 goto finally;
2608 switch (type->tp_name[0]) {
2609 case 's':
2610 if (type == &PyString_Type) {
2611 res = save_string(self, args, 1);
2612 goto finally;
2614 break;
2616 #ifdef Py_USING_UNICODE
2617 case 'u':
2618 if (type == &PyUnicode_Type) {
2619 res = save_unicode(self, args, 1);
2620 goto finally;
2622 break;
2623 #endif
2625 case 't':
2626 if (type == &PyTuple_Type) {
2627 res = save_tuple(self, args);
2628 goto finally;
2630 if (type == &PyType_Type) {
2631 res = save_global(self, args, NULL);
2632 goto finally;
2634 break;
2636 case 'l':
2637 if (type == &PyList_Type) {
2638 res = save_list(self, args);
2639 goto finally;
2641 break;
2643 case 'd':
2644 if (type == &PyDict_Type) {
2645 res = save_dict(self, args);
2646 goto finally;
2648 break;
2650 case 'i':
2651 if (type == &PyInstance_Type) {
2652 res = save_inst(self, args);
2653 goto finally;
2655 break;
2657 case 'c':
2658 if (type == &PyClass_Type) {
2659 res = save_global(self, args, NULL);
2660 goto finally;
2662 break;
2664 case 'f':
2665 if (type == &PyFunction_Type) {
2666 res = save_global(self, args, NULL);
2667 if (res && PyErr_ExceptionMatches(PickleError)) {
2668 /* fall back to reduce */
2669 PyErr_Clear();
2670 break;
2672 goto finally;
2674 break;
2676 case 'b':
2677 if (type == &PyCFunction_Type) {
2678 res = save_global(self, args, NULL);
2679 goto finally;
2683 if (!pers_save && self->inst_pers_func) {
2684 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2685 res = tmp;
2686 goto finally;
2690 if (PyType_IsSubtype(type, &PyType_Type)) {
2691 res = save_global(self, args, NULL);
2692 goto finally;
2695 /* Get a reduction callable, and call it. This may come from
2696 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
2697 * or the object's __reduce__ method.
2699 __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2700 if (__reduce__ != NULL) {
2701 Py_INCREF(__reduce__);
2702 Py_INCREF(args);
2703 ARG_TUP(self, args);
2704 if (self->arg) {
2705 t = PyObject_Call(__reduce__, self->arg, NULL);
2706 FREE_ARG_TUP(self);
2709 else {
2710 /* Check for a __reduce_ex__ method. */
2711 __reduce__ = PyObject_GetAttr(args, __reduce_ex___str);
2712 if (__reduce__ != NULL) {
2713 t = PyInt_FromLong(self->proto);
2714 if (t != NULL) {
2715 ARG_TUP(self, t);
2716 t = NULL;
2717 if (self->arg) {
2718 t = PyObject_Call(__reduce__,
2719 self->arg, NULL);
2720 FREE_ARG_TUP(self);
2724 else {
2725 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2726 PyErr_Clear();
2727 else
2728 goto finally;
2729 /* Check for a __reduce__ method. */
2730 __reduce__ = PyObject_GetAttr(args, __reduce___str);
2731 if (__reduce__ != NULL) {
2732 t = PyObject_Call(__reduce__,
2733 empty_tuple, NULL);
2735 else {
2736 PyErr_SetObject(UnpickleableError, args);
2737 goto finally;
2742 if (t == NULL)
2743 goto finally;
2745 if (PyString_Check(t)) {
2746 res = save_global(self, args, t);
2747 goto finally;
2750 if (!PyTuple_Check(t)) {
2751 cPickle_ErrFormat(PicklingError, "Value returned by "
2752 "%s must be string or tuple",
2753 "O", __reduce__);
2754 goto finally;
2757 res = save_reduce(self, t, __reduce__, args);
2759 finally:
2760 Py_LeaveRecursiveCall();
2761 Py_XDECREF(py_ob_id);
2762 Py_XDECREF(__reduce__);
2763 Py_XDECREF(t);
2765 return res;
2769 static int
2770 dump(Picklerobject *self, PyObject *args)
2772 static char stop = STOP;
2774 if (self->proto >= 2) {
2775 char bytes[2];
2777 bytes[0] = PROTO;
2778 assert(self->proto >= 0 && self->proto < 256);
2779 bytes[1] = (char)self->proto;
2780 if (self->write_func(self, bytes, 2) < 0)
2781 return -1;
2784 if (save(self, args, 0) < 0)
2785 return -1;
2787 if (self->write_func(self, &stop, 1) < 0)
2788 return -1;
2790 if (self->write_func(self, NULL, 0) < 0)
2791 return -1;
2793 return 0;
2796 static PyObject *
2797 Pickle_clear_memo(Picklerobject *self, PyObject *args)
2799 if (self->memo)
2800 PyDict_Clear(self->memo);
2801 Py_INCREF(Py_None);
2802 return Py_None;
2805 static PyObject *
2806 Pickle_getvalue(Picklerobject *self, PyObject *args)
2808 int l, i, rsize, ssize, clear=1, lm;
2809 long ik;
2810 PyObject *k, *r;
2811 char *s, *p, *have_get;
2812 Pdata *data;
2814 /* Can be called by Python code or C code */
2815 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
2816 return NULL;
2818 /* Check to make sure we are based on a list */
2819 if (! Pdata_Check(self->file)) {
2820 PyErr_SetString(PicklingError,
2821 "Attempt to getvalue() a non-list-based pickler");
2822 return NULL;
2825 /* flush write buffer */
2826 if (write_other(self, NULL, 0) < 0) return NULL;
2828 data=(Pdata*)self->file;
2829 l=data->length;
2831 /* set up an array to hold get/put status */
2832 lm = PyDict_Size(self->memo);
2833 if (lm < 0) return NULL;
2834 lm++;
2835 have_get = malloc(lm);
2836 if (have_get == NULL) return PyErr_NoMemory();
2837 memset(have_get, 0, lm);
2839 /* Scan for gets. */
2840 for (rsize = 0, i = l; --i >= 0; ) {
2841 k = data->data[i];
2843 if (PyString_Check(k))
2844 rsize += PyString_GET_SIZE(k);
2846 else if (PyInt_Check(k)) { /* put */
2847 ik = PyInt_AS_LONG((PyIntObject*)k);
2848 if (ik >= lm || ik == 0) {
2849 PyErr_SetString(PicklingError,
2850 "Invalid get data");
2851 goto err;
2853 if (have_get[ik]) /* with matching get */
2854 rsize += ik < 256 ? 2 : 5;
2857 else if (! (PyTuple_Check(k) &&
2858 PyTuple_GET_SIZE(k) == 2 &&
2859 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
2861 PyErr_SetString(PicklingError,
2862 "Unexpected data in internal list");
2863 goto err;
2866 else { /* put */
2867 ik = PyInt_AS_LONG((PyIntObject *)k);
2868 if (ik >= lm || ik == 0) {
2869 PyErr_SetString(PicklingError,
2870 "Invalid get data");
2871 return NULL;
2873 have_get[ik] = 1;
2874 rsize += ik < 256 ? 2 : 5;
2878 /* Now generate the result */
2879 r = PyString_FromStringAndSize(NULL, rsize);
2880 if (r == NULL) goto err;
2881 s = PyString_AS_STRING((PyStringObject *)r);
2883 for (i = 0; i < l; i++) {
2884 k = data->data[i];
2886 if (PyString_Check(k)) {
2887 ssize = PyString_GET_SIZE(k);
2888 if (ssize) {
2889 p=PyString_AS_STRING((PyStringObject *)k);
2890 while (--ssize >= 0)
2891 *s++ = *p++;
2895 else if (PyTuple_Check(k)) { /* get */
2896 ik = PyInt_AS_LONG((PyIntObject *)
2897 PyTuple_GET_ITEM(k, 0));
2898 if (ik < 256) {
2899 *s++ = BINGET;
2900 *s++ = (int)(ik & 0xff);
2902 else {
2903 *s++ = LONG_BINGET;
2904 *s++ = (int)(ik & 0xff);
2905 *s++ = (int)((ik >> 8) & 0xff);
2906 *s++ = (int)((ik >> 16) & 0xff);
2907 *s++ = (int)((ik >> 24) & 0xff);
2911 else { /* put */
2912 ik = PyInt_AS_LONG((PyIntObject*)k);
2914 if (have_get[ik]) { /* with matching get */
2915 if (ik < 256) {
2916 *s++ = BINPUT;
2917 *s++ = (int)(ik & 0xff);
2919 else {
2920 *s++ = LONG_BINPUT;
2921 *s++ = (int)(ik & 0xff);
2922 *s++ = (int)((ik >> 8) & 0xff);
2923 *s++ = (int)((ik >> 16) & 0xff);
2924 *s++ = (int)((ik >> 24) & 0xff);
2930 if (clear) {
2931 PyDict_Clear(self->memo);
2932 Pdata_clear(data, 0);
2935 free(have_get);
2936 return r;
2937 err:
2938 free(have_get);
2939 return NULL;
2942 static PyObject *
2943 Pickler_dump(Picklerobject *self, PyObject *args)
2945 PyObject *ob;
2946 int get=0;
2948 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
2949 return NULL;
2951 if (dump(self, ob) < 0)
2952 return NULL;
2954 if (get) return Pickle_getvalue(self, NULL);
2956 /* XXX Why does dump() return self? */
2957 Py_INCREF(self);
2958 return (PyObject*)self;
2962 static struct PyMethodDef Pickler_methods[] =
2964 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
2965 PyDoc_STR("dump(object) -- "
2966 "Write an object in pickle format to the object's pickle stream")},
2967 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
2968 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
2969 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
2970 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
2971 {NULL, NULL} /* sentinel */
2975 static Picklerobject *
2976 newPicklerobject(PyObject *file, int proto)
2978 Picklerobject *self;
2980 if (proto < 0)
2981 proto = HIGHEST_PROTOCOL;
2982 if (proto > HIGHEST_PROTOCOL) {
2983 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
2984 "the highest available protocol is %d",
2985 proto, HIGHEST_PROTOCOL);
2986 return NULL;
2989 self = PyObject_GC_New(Picklerobject, &Picklertype);
2990 if (self == NULL)
2991 return NULL;
2992 self->proto = proto;
2993 self->bin = proto > 0;
2994 self->fp = NULL;
2995 self->write = NULL;
2996 self->memo = NULL;
2997 self->arg = NULL;
2998 self->pers_func = NULL;
2999 self->inst_pers_func = NULL;
3000 self->write_buf = NULL;
3001 self->fast = 0;
3002 self->fast_container = 0;
3003 self->fast_memo = NULL;
3004 self->buf_size = 0;
3005 self->dispatch_table = NULL;
3007 self->file = NULL;
3008 if (file)
3009 Py_INCREF(file);
3010 else {
3011 file = Pdata_New();
3012 if (file == NULL)
3013 goto err;
3015 self->file = file;
3017 if (!( self->memo = PyDict_New()))
3018 goto err;
3020 if (PyFile_Check(file)) {
3021 self->fp = PyFile_AsFile(file);
3022 if (self->fp == NULL) {
3023 PyErr_SetString(PyExc_ValueError,
3024 "I/O operation on closed file");
3025 goto err;
3027 self->write_func = write_file;
3029 else if (PycStringIO_OutputCheck(file)) {
3030 self->write_func = write_cStringIO;
3032 else if (file == Py_None) {
3033 self->write_func = write_none;
3035 else {
3036 self->write_func = write_other;
3038 if (! Pdata_Check(file)) {
3039 self->write = PyObject_GetAttr(file, write_str);
3040 if (!self->write) {
3041 PyErr_Clear();
3042 PyErr_SetString(PyExc_TypeError,
3043 "argument must have 'write' "
3044 "attribute");
3045 goto err;
3049 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
3050 if (self->write_buf == NULL) {
3051 PyErr_NoMemory();
3052 goto err;
3056 if (PyEval_GetRestricted()) {
3057 /* Restricted execution, get private tables */
3058 PyObject *m = PyImport_Import(copyreg_str);
3060 if (m == NULL)
3061 goto err;
3062 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
3063 Py_DECREF(m);
3064 if (self->dispatch_table == NULL)
3065 goto err;
3067 else {
3068 self->dispatch_table = dispatch_table;
3069 Py_INCREF(dispatch_table);
3071 PyObject_GC_Track(self);
3073 return self;
3075 err:
3076 Py_DECREF(self);
3077 return NULL;
3081 static PyObject *
3082 get_Pickler(PyObject *self, PyObject *args, PyObject *kwds)
3084 static char *kwlist[] = {"file", "protocol", NULL};
3085 PyObject *file = NULL;
3086 int proto = 0;
3088 /* XXX
3089 * The documented signature is Pickler(file, protocol=0), but this
3090 * accepts Pickler() and Pickler(integer) too. The meaning then
3091 * is clear as mud, undocumented, and not supported by pickle.py.
3092 * I'm told Zope uses this, but I haven't traced into this code
3093 * far enough to figure out what it means.
3095 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
3096 PyErr_Clear();
3097 proto = 0;
3098 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler",
3099 kwlist, &file, &proto))
3100 return NULL;
3102 return (PyObject *)newPicklerobject(file, proto);
3106 static void
3107 Pickler_dealloc(Picklerobject *self)
3109 PyObject_GC_UnTrack(self);
3110 Py_XDECREF(self->write);
3111 Py_XDECREF(self->memo);
3112 Py_XDECREF(self->fast_memo);
3113 Py_XDECREF(self->arg);
3114 Py_XDECREF(self->file);
3115 Py_XDECREF(self->pers_func);
3116 Py_XDECREF(self->inst_pers_func);
3117 Py_XDECREF(self->dispatch_table);
3118 PyMem_Free(self->write_buf);
3119 Py_TYPE(self)->tp_free((PyObject *)self);
3122 static int
3123 Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)
3125 Py_VISIT(self->write);
3126 Py_VISIT(self->memo);
3127 Py_VISIT(self->fast_memo);
3128 Py_VISIT(self->arg);
3129 Py_VISIT(self->file);
3130 Py_VISIT(self->pers_func);
3131 Py_VISIT(self->inst_pers_func);
3132 Py_VISIT(self->dispatch_table);
3133 return 0;
3136 static int
3137 Pickler_clear(Picklerobject *self)
3139 Py_CLEAR(self->write);
3140 Py_CLEAR(self->memo);
3141 Py_CLEAR(self->fast_memo);
3142 Py_CLEAR(self->arg);
3143 Py_CLEAR(self->file);
3144 Py_CLEAR(self->pers_func);
3145 Py_CLEAR(self->inst_pers_func);
3146 Py_CLEAR(self->dispatch_table);
3147 return 0;
3150 static PyObject *
3151 Pickler_get_pers_func(Picklerobject *p)
3153 if (p->pers_func == NULL)
3154 PyErr_SetString(PyExc_AttributeError, "persistent_id");
3155 else
3156 Py_INCREF(p->pers_func);
3157 return p->pers_func;
3160 static int
3161 Pickler_set_pers_func(Picklerobject *p, PyObject *v)
3163 if (v == NULL) {
3164 PyErr_SetString(PyExc_TypeError,
3165 "attribute deletion is not supported");
3166 return -1;
3168 Py_XDECREF(p->pers_func);
3169 Py_INCREF(v);
3170 p->pers_func = v;
3171 return 0;
3174 static int
3175 Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
3177 if (v == NULL) {
3178 PyErr_SetString(PyExc_TypeError,
3179 "attribute deletion is not supported");
3180 return -1;
3182 Py_XDECREF(p->inst_pers_func);
3183 Py_INCREF(v);
3184 p->inst_pers_func = v;
3185 return 0;
3188 static PyObject *
3189 Pickler_get_memo(Picklerobject *p)
3191 if (p->memo == NULL)
3192 PyErr_SetString(PyExc_AttributeError, "memo");
3193 else
3194 Py_INCREF(p->memo);
3195 return p->memo;
3198 static int
3199 Pickler_set_memo(Picklerobject *p, PyObject *v)
3201 if (v == NULL) {
3202 PyErr_SetString(PyExc_TypeError,
3203 "attribute deletion is not supported");
3204 return -1;
3206 if (!PyDict_Check(v)) {
3207 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
3208 return -1;
3210 Py_XDECREF(p->memo);
3211 Py_INCREF(v);
3212 p->memo = v;
3213 return 0;
3216 static PyObject *
3217 Pickler_get_error(Picklerobject *p)
3219 /* why is this an attribute on the Pickler? */
3220 Py_INCREF(PicklingError);
3221 return PicklingError;
3224 static PyMemberDef Pickler_members[] = {
3225 {"binary", T_INT, offsetof(Picklerobject, bin)},
3226 {"fast", T_INT, offsetof(Picklerobject, fast)},
3227 {NULL}
3230 static PyGetSetDef Pickler_getsets[] = {
3231 {"persistent_id", (getter)Pickler_get_pers_func,
3232 (setter)Pickler_set_pers_func},
3233 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
3234 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
3235 {"PicklingError", (getter)Pickler_get_error, NULL},
3236 {NULL}
3239 PyDoc_STRVAR(Picklertype__doc__,
3240 "Objects that know how to pickle objects\n");
3242 static PyTypeObject Picklertype = {
3243 PyVarObject_HEAD_INIT(NULL, 0)
3244 "cPickle.Pickler", /*tp_name*/
3245 sizeof(Picklerobject), /*tp_basicsize*/
3247 (destructor)Pickler_dealloc, /* tp_dealloc */
3248 0, /* tp_print */
3249 0, /* tp_getattr */
3250 0, /* tp_setattr */
3251 0, /* tp_compare */
3252 0, /* tp_repr */
3253 0, /* tp_as_number */
3254 0, /* tp_as_sequence */
3255 0, /* tp_as_mapping */
3256 0, /* tp_hash */
3257 0, /* tp_call */
3258 0, /* tp_str */
3259 PyObject_GenericGetAttr, /* tp_getattro */
3260 PyObject_GenericSetAttr, /* tp_setattro */
3261 0, /* tp_as_buffer */
3262 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
3263 Picklertype__doc__, /* tp_doc */
3264 (traverseproc)Pickler_traverse, /* tp_traverse */
3265 (inquiry)Pickler_clear, /* tp_clear */
3266 0, /* tp_richcompare */
3267 0, /* tp_weaklistoffset */
3268 0, /* tp_iter */
3269 0, /* tp_iternext */
3270 Pickler_methods, /* tp_methods */
3271 Pickler_members, /* tp_members */
3272 Pickler_getsets, /* tp_getset */
3275 static PyObject *
3276 find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
3278 PyObject *global = 0, *module;
3280 if (fc) {
3281 if (fc==Py_None) {
3282 PyErr_SetString(UnpicklingError, "Global and instance "
3283 "pickles are not supported.");
3284 return NULL;
3286 return PyObject_CallFunctionObjArgs(fc, py_module_name,
3287 py_global_name, NULL);
3290 module = PySys_GetObject("modules");
3291 if (module == NULL)
3292 return NULL;
3294 module = PyDict_GetItem(module, py_module_name);
3295 if (module == NULL) {
3296 module = PyImport_Import(py_module_name);
3297 if (!module)
3298 return NULL;
3299 global = PyObject_GetAttr(module, py_global_name);
3300 Py_DECREF(module);
3302 else
3303 global = PyObject_GetAttr(module, py_global_name);
3304 return global;
3307 static int
3308 marker(Unpicklerobject *self)
3310 if (self->num_marks < 1) {
3311 PyErr_SetString(UnpicklingError, "could not find MARK");
3312 return -1;
3315 return self->marks[--self->num_marks];
3319 static int
3320 load_none(Unpicklerobject *self)
3322 PDATA_APPEND(self->stack, Py_None, -1);
3323 return 0;
3326 static int
3327 bad_readline(void)
3329 PyErr_SetString(UnpicklingError, "pickle data was truncated");
3330 return -1;
3333 static int
3334 load_int(Unpicklerobject *self)
3336 PyObject *py_int = 0;
3337 char *endptr, *s;
3338 int len, res = -1;
3339 long l;
3341 if ((len = self->readline_func(self, &s)) < 0) return -1;
3342 if (len < 2) return bad_readline();
3343 if (!( s=pystrndup(s,len))) return -1;
3345 errno = 0;
3346 l = strtol(s, &endptr, 0);
3348 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
3349 /* Hm, maybe we've got something long. Let's try reading
3350 it as a Python long object. */
3351 errno = 0;
3352 py_int = PyLong_FromString(s, NULL, 0);
3353 if (py_int == NULL) {
3354 PyErr_SetString(PyExc_ValueError,
3355 "could not convert string to int");
3356 goto finally;
3359 else {
3360 if (len == 3 && (l == 0 || l == 1)) {
3361 if (!( py_int = PyBool_FromLong(l))) goto finally;
3363 else {
3364 if (!( py_int = PyInt_FromLong(l))) goto finally;
3368 free(s);
3369 PDATA_PUSH(self->stack, py_int, -1);
3370 return 0;
3372 finally:
3373 free(s);
3375 return res;
3378 static int
3379 load_bool(Unpicklerobject *self, PyObject *boolean)
3381 assert(boolean == Py_True || boolean == Py_False);
3382 PDATA_APPEND(self->stack, boolean, -1);
3383 return 0;
3386 /* s contains x bytes of a little-endian integer. Return its value as a
3387 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3388 * int, but when x is 4 it's a signed one. This is an historical source
3389 * of x-platform bugs.
3391 static long
3392 calc_binint(char *s, int x)
3394 unsigned char c;
3395 int i;
3396 long l;
3398 for (i = 0, l = 0L; i < x; i++) {
3399 c = (unsigned char)s[i];
3400 l |= (long)c << (i * 8);
3402 #if SIZEOF_LONG > 4
3403 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3404 * is signed, so on a box with longs bigger than 4 bytes we need
3405 * to extend a BININT's sign bit to the full width.
3407 if (x == 4 && l & (1L << 31))
3408 l |= (~0L) << 32;
3409 #endif
3410 return l;
3414 static int
3415 load_binintx(Unpicklerobject *self, char *s, int x)
3417 PyObject *py_int = 0;
3418 long l;
3420 l = calc_binint(s, x);
3422 if (!( py_int = PyInt_FromLong(l)))
3423 return -1;
3425 PDATA_PUSH(self->stack, py_int, -1);
3426 return 0;
3430 static int
3431 load_binint(Unpicklerobject *self)
3433 char *s;
3435 if (self->read_func(self, &s, 4) < 0)
3436 return -1;
3438 return load_binintx(self, s, 4);
3442 static int
3443 load_binint1(Unpicklerobject *self)
3445 char *s;
3447 if (self->read_func(self, &s, 1) < 0)
3448 return -1;
3450 return load_binintx(self, s, 1);
3454 static int
3455 load_binint2(Unpicklerobject *self)
3457 char *s;
3459 if (self->read_func(self, &s, 2) < 0)
3460 return -1;
3462 return load_binintx(self, s, 2);
3465 static int
3466 load_long(Unpicklerobject *self)
3468 PyObject *l = 0;
3469 char *end, *s;
3470 int len, res = -1;
3472 if ((len = self->readline_func(self, &s)) < 0) return -1;
3473 if (len < 2) return bad_readline();
3474 if (!( s=pystrndup(s,len))) return -1;
3476 if (!( l = PyLong_FromString(s, &end, 0)))
3477 goto finally;
3479 free(s);
3480 PDATA_PUSH(self->stack, l, -1);
3481 return 0;
3483 finally:
3484 free(s);
3486 return res;
3489 /* 'size' bytes contain the # of bytes of little-endian 256's-complement
3490 * data following.
3492 static int
3493 load_counted_long(Unpicklerobject *self, int size)
3495 Py_ssize_t i;
3496 char *nbytes;
3497 unsigned char *pdata;
3498 PyObject *along;
3500 assert(size == 1 || size == 4);
3501 i = self->read_func(self, &nbytes, size);
3502 if (i < 0) return -1;
3504 size = calc_binint(nbytes, size);
3505 if (size < 0) {
3506 /* Corrupt or hostile pickle -- we never write one like
3507 * this.
3509 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
3510 "byte count");
3511 return -1;
3514 if (size == 0)
3515 along = PyLong_FromLong(0L);
3516 else {
3517 /* Read the raw little-endian bytes & convert. */
3518 i = self->read_func(self, (char **)&pdata, size);
3519 if (i < 0) return -1;
3520 along = _PyLong_FromByteArray(pdata, (size_t)size,
3521 1 /* little endian */, 1 /* signed */);
3523 if (along == NULL)
3524 return -1;
3525 PDATA_PUSH(self->stack, along, -1);
3526 return 0;
3529 static int
3530 load_float(Unpicklerobject *self)
3532 PyObject *py_float = 0;
3533 char *endptr, *s;
3534 int len, res = -1;
3535 double d;
3537 if ((len = self->readline_func(self, &s)) < 0) return -1;
3538 if (len < 2) return bad_readline();
3539 if (!( s=pystrndup(s,len))) return -1;
3541 errno = 0;
3542 d = PyOS_ascii_strtod(s, &endptr);
3544 if ((errno == ERANGE && !(fabs(d) <= 1.0)) ||
3545 (endptr[0] != '\n') || (endptr[1] != '\0')) {
3546 PyErr_SetString(PyExc_ValueError,
3547 "could not convert string to float");
3548 goto finally;
3551 if (!( py_float = PyFloat_FromDouble(d)))
3552 goto finally;
3554 free(s);
3555 PDATA_PUSH(self->stack, py_float, -1);
3556 return 0;
3558 finally:
3559 free(s);
3561 return res;
3564 static int
3565 load_binfloat(Unpicklerobject *self)
3567 PyObject *py_float;
3568 double x;
3569 char *p;
3571 if (self->read_func(self, &p, 8) < 0)
3572 return -1;
3574 x = _PyFloat_Unpack8((unsigned char *)p, 0);
3575 if (x == -1.0 && PyErr_Occurred())
3576 return -1;
3578 py_float = PyFloat_FromDouble(x);
3579 if (py_float == NULL)
3580 return -1;
3582 PDATA_PUSH(self->stack, py_float, -1);
3583 return 0;
3586 static int
3587 load_string(Unpicklerobject *self)
3589 PyObject *str = 0;
3590 int len, res = -1;
3591 char *s, *p;
3593 if ((len = self->readline_func(self, &s)) < 0) return -1;
3594 if (len < 2) return bad_readline();
3595 if (!( s=pystrndup(s,len))) return -1;
3598 /* Strip outermost quotes */
3599 while (s[len-1] <= ' ')
3600 len--;
3601 if(s[0]=='"' && s[len-1]=='"'){
3602 s[len-1] = '\0';
3603 p = s + 1 ;
3604 len -= 2;
3605 } else if(s[0]=='\'' && s[len-1]=='\''){
3606 s[len-1] = '\0';
3607 p = s + 1 ;
3608 len -= 2;
3609 } else
3610 goto insecure;
3611 /********************************************/
3613 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
3614 free(s);
3615 if (str) {
3616 PDATA_PUSH(self->stack, str, -1);
3617 res = 0;
3619 return res;
3621 insecure:
3622 free(s);
3623 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3624 return -1;
3628 static int
3629 load_binstring(Unpicklerobject *self)
3631 PyObject *py_string = 0;
3632 long l;
3633 char *s;
3635 if (self->read_func(self, &s, 4) < 0) return -1;
3637 l = calc_binint(s, 4);
3638 if (l < 0) {
3639 /* Corrupt or hostile pickle -- we never write one like
3640 * this.
3642 PyErr_SetString(UnpicklingError,
3643 "BINSTRING pickle has negative byte count");
3644 return -1;
3647 if (self->read_func(self, &s, l) < 0)
3648 return -1;
3650 if (!( py_string = PyString_FromStringAndSize(s, l)))
3651 return -1;
3653 PDATA_PUSH(self->stack, py_string, -1);
3654 return 0;
3658 static int
3659 load_short_binstring(Unpicklerobject *self)
3661 PyObject *py_string = 0;
3662 unsigned char l;
3663 char *s;
3665 if (self->read_func(self, &s, 1) < 0)
3666 return -1;
3668 l = (unsigned char)s[0];
3670 if (self->read_func(self, &s, l) < 0) return -1;
3672 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
3674 PDATA_PUSH(self->stack, py_string, -1);
3675 return 0;
3679 #ifdef Py_USING_UNICODE
3680 static int
3681 load_unicode(Unpicklerobject *self)
3683 PyObject *str = 0;
3684 int len, res = -1;
3685 char *s;
3687 if ((len = self->readline_func(self, &s)) < 0) return -1;
3688 if (len < 1) return bad_readline();
3690 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
3691 goto finally;
3693 PDATA_PUSH(self->stack, str, -1);
3694 return 0;
3696 finally:
3697 return res;
3699 #endif
3702 #ifdef Py_USING_UNICODE
3703 static int
3704 load_binunicode(Unpicklerobject *self)
3706 PyObject *unicode;
3707 long l;
3708 char *s;
3710 if (self->read_func(self, &s, 4) < 0) return -1;
3712 l = calc_binint(s, 4);
3713 if (l < 0) {
3714 /* Corrupt or hostile pickle -- we never write one like
3715 * this.
3717 PyErr_SetString(UnpicklingError,
3718 "BINUNICODE pickle has negative byte count");
3719 return -1;
3722 if (self->read_func(self, &s, l) < 0)
3723 return -1;
3725 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
3726 return -1;
3728 PDATA_PUSH(self->stack, unicode, -1);
3729 return 0;
3731 #endif
3734 static int
3735 load_tuple(Unpicklerobject *self)
3737 PyObject *tup;
3738 int i;
3740 if ((i = marker(self)) < 0) return -1;
3741 if (!( tup=Pdata_popTuple(self->stack, i))) return -1;
3742 PDATA_PUSH(self->stack, tup, -1);
3743 return 0;
3746 static int
3747 load_counted_tuple(Unpicklerobject *self, int len)
3749 PyObject *tup = PyTuple_New(len);
3751 if (tup == NULL)
3752 return -1;
3754 while (--len >= 0) {
3755 PyObject *element;
3757 PDATA_POP(self->stack, element);
3758 if (element == NULL)
3759 return -1;
3760 PyTuple_SET_ITEM(tup, len, element);
3762 PDATA_PUSH(self->stack, tup, -1);
3763 return 0;
3766 static int
3767 load_empty_list(Unpicklerobject *self)
3769 PyObject *list;
3771 if (!( list=PyList_New(0))) return -1;
3772 PDATA_PUSH(self->stack, list, -1);
3773 return 0;
3776 static int
3777 load_empty_dict(Unpicklerobject *self)
3779 PyObject *dict;
3781 if (!( dict=PyDict_New())) return -1;
3782 PDATA_PUSH(self->stack, dict, -1);
3783 return 0;
3787 static int
3788 load_list(Unpicklerobject *self)
3790 PyObject *list = 0;
3791 int i;
3793 if ((i = marker(self)) < 0) return -1;
3794 if (!( list=Pdata_popList(self->stack, i))) return -1;
3795 PDATA_PUSH(self->stack, list, -1);
3796 return 0;
3799 static int
3800 load_dict(Unpicklerobject *self)
3802 PyObject *dict, *key, *value;
3803 int i, j, k;
3805 if ((i = marker(self)) < 0) return -1;
3806 j=self->stack->length;
3808 if (!( dict = PyDict_New())) return -1;
3810 for (k = i+1; k < j; k += 2) {
3811 key =self->stack->data[k-1];
3812 value=self->stack->data[k ];
3813 if (PyDict_SetItem(dict, key, value) < 0) {
3814 Py_DECREF(dict);
3815 return -1;
3818 Pdata_clear(self->stack, i);
3819 PDATA_PUSH(self->stack, dict, -1);
3820 return 0;
3823 static PyObject *
3824 Instance_New(PyObject *cls, PyObject *args)
3826 PyObject *r = 0;
3828 if (PyClass_Check(cls)) {
3829 int l;
3831 if ((l=PyObject_Size(args)) < 0) goto err;
3832 if (!( l )) {
3833 PyObject *__getinitargs__;
3835 __getinitargs__ = PyObject_GetAttr(cls,
3836 __getinitargs___str);
3837 if (!__getinitargs__) {
3838 /* We have a class with no __getinitargs__,
3839 so bypass usual construction */
3840 PyObject *inst;
3842 PyErr_Clear();
3843 if (!( inst=PyInstance_NewRaw(cls, NULL)))
3844 goto err;
3845 return inst;
3847 Py_DECREF(__getinitargs__);
3850 if ((r=PyInstance_New(cls, args, NULL))) return r;
3851 else goto err;
3854 if ((r=PyObject_CallObject(cls, args))) return r;
3856 err:
3858 PyObject *tp, *v, *tb, *tmp_value;
3860 PyErr_Fetch(&tp, &v, &tb);
3861 tmp_value = v;
3862 /* NULL occurs when there was a KeyboardInterrupt */
3863 if (tmp_value == NULL)
3864 tmp_value = Py_None;
3865 if ((r = PyTuple_Pack(3, tmp_value, cls, args))) {
3866 Py_XDECREF(v);
3867 v=r;
3869 PyErr_Restore(tp,v,tb);
3871 return NULL;
3875 static int
3876 load_obj(Unpicklerobject *self)
3878 PyObject *class, *tup, *obj=0;
3879 int i;
3881 if ((i = marker(self)) < 0) return -1;
3882 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3883 PDATA_POP(self->stack, class);
3884 if (class) {
3885 obj = Instance_New(class, tup);
3886 Py_DECREF(class);
3888 Py_DECREF(tup);
3890 if (! obj) return -1;
3891 PDATA_PUSH(self->stack, obj, -1);
3892 return 0;
3896 static int
3897 load_inst(Unpicklerobject *self)
3899 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
3900 int i, len;
3901 char *s;
3903 if ((i = marker(self)) < 0) return -1;
3905 if ((len = self->readline_func(self, &s)) < 0) return -1;
3906 if (len < 2) return bad_readline();
3907 module_name = PyString_FromStringAndSize(s, len - 1);
3908 if (!module_name) return -1;
3910 if ((len = self->readline_func(self, &s)) >= 0) {
3911 if (len < 2) return bad_readline();
3912 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
3913 class = find_class(module_name, class_name,
3914 self->find_class);
3915 Py_DECREF(class_name);
3918 Py_DECREF(module_name);
3920 if (! class) return -1;
3922 if ((tup=Pdata_popTuple(self->stack, i))) {
3923 obj = Instance_New(class, tup);
3924 Py_DECREF(tup);
3926 Py_DECREF(class);
3928 if (! obj) return -1;
3930 PDATA_PUSH(self->stack, obj, -1);
3931 return 0;
3934 static int
3935 load_newobj(Unpicklerobject *self)
3937 PyObject *args = NULL;
3938 PyObject *clsraw = NULL;
3939 PyTypeObject *cls; /* clsraw cast to its true type */
3940 PyObject *obj;
3942 /* Stack is ... cls argtuple, and we want to call
3943 * cls.__new__(cls, *argtuple).
3945 PDATA_POP(self->stack, args);
3946 if (args == NULL) goto Fail;
3947 if (! PyTuple_Check(args)) {
3948 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
3949 "tuple.");
3950 goto Fail;
3953 PDATA_POP(self->stack, clsraw);
3954 cls = (PyTypeObject *)clsraw;
3955 if (cls == NULL) goto Fail;
3956 if (! PyType_Check(cls)) {
3957 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3958 "isn't a type object");
3959 goto Fail;
3961 if (cls->tp_new == NULL) {
3962 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3963 "has NULL tp_new");
3964 goto Fail;
3967 /* Call __new__. */
3968 obj = cls->tp_new(cls, args, NULL);
3969 if (obj == NULL) goto Fail;
3971 Py_DECREF(args);
3972 Py_DECREF(clsraw);
3973 PDATA_PUSH(self->stack, obj, -1);
3974 return 0;
3976 Fail:
3977 Py_XDECREF(args);
3978 Py_XDECREF(clsraw);
3979 return -1;
3982 static int
3983 load_global(Unpicklerobject *self)
3985 PyObject *class = 0, *module_name = 0, *class_name = 0;
3986 int len;
3987 char *s;
3989 if ((len = self->readline_func(self, &s)) < 0) return -1;
3990 if (len < 2) return bad_readline();
3991 module_name = PyString_FromStringAndSize(s, len - 1);
3992 if (!module_name) return -1;
3994 if ((len = self->readline_func(self, &s)) >= 0) {
3995 if (len < 2) {
3996 Py_DECREF(module_name);
3997 return bad_readline();
3999 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
4000 class = find_class(module_name, class_name,
4001 self->find_class);
4002 Py_DECREF(class_name);
4005 Py_DECREF(module_name);
4007 if (! class) return -1;
4008 PDATA_PUSH(self->stack, class, -1);
4009 return 0;
4013 static int
4014 load_persid(Unpicklerobject *self)
4016 PyObject *pid = 0;
4017 int len;
4018 char *s;
4020 if (self->pers_func) {
4021 if ((len = self->readline_func(self, &s)) < 0) return -1;
4022 if (len < 2) return bad_readline();
4024 pid = PyString_FromStringAndSize(s, len - 1);
4025 if (!pid) return -1;
4027 if (PyList_Check(self->pers_func)) {
4028 if (PyList_Append(self->pers_func, pid) < 0) {
4029 Py_DECREF(pid);
4030 return -1;
4033 else {
4034 ARG_TUP(self, pid);
4035 if (self->arg) {
4036 pid = PyObject_Call(self->pers_func, self->arg,
4037 NULL);
4038 FREE_ARG_TUP(self);
4042 if (! pid) return -1;
4044 PDATA_PUSH(self->stack, pid, -1);
4045 return 0;
4047 else {
4048 PyErr_SetString(UnpicklingError,
4049 "A load persistent id instruction was encountered,\n"
4050 "but no persistent_load function was specified.");
4051 return -1;
4055 static int
4056 load_binpersid(Unpicklerobject *self)
4058 PyObject *pid = 0;
4060 if (self->pers_func) {
4061 PDATA_POP(self->stack, pid);
4062 if (! pid) return -1;
4064 if (PyList_Check(self->pers_func)) {
4065 if (PyList_Append(self->pers_func, pid) < 0) {
4066 Py_DECREF(pid);
4067 return -1;
4070 else {
4071 ARG_TUP(self, pid);
4072 if (self->arg) {
4073 pid = PyObject_Call(self->pers_func, self->arg,
4074 NULL);
4075 FREE_ARG_TUP(self);
4077 if (! pid) return -1;
4080 PDATA_PUSH(self->stack, pid, -1);
4081 return 0;
4083 else {
4084 PyErr_SetString(UnpicklingError,
4085 "A load persistent id instruction was encountered,\n"
4086 "but no persistent_load function was specified.");
4087 return -1;
4092 static int
4093 load_pop(Unpicklerobject *self)
4095 int len = self->stack->length;
4097 /* Note that we split the (pickle.py) stack into two stacks,
4098 an object stack and a mark stack. We have to be clever and
4099 pop the right one. We do this by looking at the top of the
4100 mark stack first, and only signalling a stack underflow if
4101 the object stack is empty and the mark stack doesn't match
4102 our expectations.
4104 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
4105 self->num_marks--;
4106 } else if (len >= 0) {
4107 len--;
4108 Py_DECREF(self->stack->data[len]);
4109 self->stack->length = len;
4110 } else {
4111 return stackUnderflow();
4113 return 0;
4117 static int
4118 load_pop_mark(Unpicklerobject *self)
4120 int i;
4122 if ((i = marker(self)) < 0)
4123 return -1;
4125 Pdata_clear(self->stack, i);
4127 return 0;
4131 static int
4132 load_dup(Unpicklerobject *self)
4134 PyObject *last;
4135 int len;
4137 if ((len = self->stack->length) <= 0) return stackUnderflow();
4138 last=self->stack->data[len-1];
4139 Py_INCREF(last);
4140 PDATA_PUSH(self->stack, last, -1);
4141 return 0;
4145 static int
4146 load_get(Unpicklerobject *self)
4148 PyObject *py_str = 0, *value = 0;
4149 int len;
4150 char *s;
4151 int rc;
4153 if ((len = self->readline_func(self, &s)) < 0) return -1;
4154 if (len < 2) return bad_readline();
4156 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
4158 value = PyDict_GetItem(self->memo, py_str);
4159 if (! value) {
4160 PyErr_SetObject(BadPickleGet, py_str);
4161 rc = -1;
4163 else {
4164 PDATA_APPEND(self->stack, value, -1);
4165 rc = 0;
4168 Py_DECREF(py_str);
4169 return rc;
4173 static int
4174 load_binget(Unpicklerobject *self)
4176 PyObject *py_key = 0, *value = 0;
4177 unsigned char key;
4178 char *s;
4179 int rc;
4181 if (self->read_func(self, &s, 1) < 0) return -1;
4183 key = (unsigned char)s[0];
4184 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4186 value = PyDict_GetItem(self->memo, py_key);
4187 if (! value) {
4188 PyErr_SetObject(BadPickleGet, py_key);
4189 rc = -1;
4191 else {
4192 PDATA_APPEND(self->stack, value, -1);
4193 rc = 0;
4196 Py_DECREF(py_key);
4197 return rc;
4201 static int
4202 load_long_binget(Unpicklerobject *self)
4204 PyObject *py_key = 0, *value = 0;
4205 unsigned char c;
4206 char *s;
4207 long key;
4208 int rc;
4210 if (self->read_func(self, &s, 4) < 0) return -1;
4212 c = (unsigned char)s[0];
4213 key = (long)c;
4214 c = (unsigned char)s[1];
4215 key |= (long)c << 8;
4216 c = (unsigned char)s[2];
4217 key |= (long)c << 16;
4218 c = (unsigned char)s[3];
4219 key |= (long)c << 24;
4221 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4223 value = PyDict_GetItem(self->memo, py_key);
4224 if (! value) {
4225 PyErr_SetObject(BadPickleGet, py_key);
4226 rc = -1;
4228 else {
4229 PDATA_APPEND(self->stack, value, -1);
4230 rc = 0;
4233 Py_DECREF(py_key);
4234 return rc;
4237 /* Push an object from the extension registry (EXT[124]). nbytes is
4238 * the number of bytes following the opcode, holding the index (code) value.
4240 static int
4241 load_extension(Unpicklerobject *self, int nbytes)
4243 char *codebytes; /* the nbytes bytes after the opcode */
4244 long code; /* calc_binint returns long */
4245 PyObject *py_code; /* code as a Python int */
4246 PyObject *obj; /* the object to push */
4247 PyObject *pair; /* (module_name, class_name) */
4248 PyObject *module_name, *class_name;
4250 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4251 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4252 code = calc_binint(codebytes, nbytes);
4253 if (code <= 0) { /* note that 0 is forbidden */
4254 /* Corrupt or hostile pickle. */
4255 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
4256 return -1;
4259 /* Look for the code in the cache. */
4260 py_code = PyInt_FromLong(code);
4261 if (py_code == NULL) return -1;
4262 obj = PyDict_GetItem(extension_cache, py_code);
4263 if (obj != NULL) {
4264 /* Bingo. */
4265 Py_DECREF(py_code);
4266 PDATA_APPEND(self->stack, obj, -1);
4267 return 0;
4270 /* Look up the (module_name, class_name) pair. */
4271 pair = PyDict_GetItem(inverted_registry, py_code);
4272 if (pair == NULL) {
4273 Py_DECREF(py_code);
4274 PyErr_Format(PyExc_ValueError, "unregistered extension "
4275 "code %ld", code);
4276 return -1;
4278 /* Since the extension registry is manipulable via Python code,
4279 * confirm that pair is really a 2-tuple of strings.
4281 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
4282 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
4283 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
4284 Py_DECREF(py_code);
4285 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
4286 "isn't a 2-tuple of strings", code);
4287 return -1;
4289 /* Load the object. */
4290 obj = find_class(module_name, class_name, self->find_class);
4291 if (obj == NULL) {
4292 Py_DECREF(py_code);
4293 return -1;
4295 /* Cache code -> obj. */
4296 code = PyDict_SetItem(extension_cache, py_code, obj);
4297 Py_DECREF(py_code);
4298 if (code < 0) {
4299 Py_DECREF(obj);
4300 return -1;
4302 PDATA_PUSH(self->stack, obj, -1);
4303 return 0;
4306 static int
4307 load_put(Unpicklerobject *self)
4309 PyObject *py_str = 0, *value = 0;
4310 int len, l;
4311 char *s;
4313 if ((l = self->readline_func(self, &s)) < 0) return -1;
4314 if (l < 2) return bad_readline();
4315 if (!( len=self->stack->length )) return stackUnderflow();
4316 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
4317 value=self->stack->data[len-1];
4318 l=PyDict_SetItem(self->memo, py_str, value);
4319 Py_DECREF(py_str);
4320 return l;
4324 static int
4325 load_binput(Unpicklerobject *self)
4327 PyObject *py_key = 0, *value = 0;
4328 unsigned char key;
4329 char *s;
4330 int len;
4332 if (self->read_func(self, &s, 1) < 0) return -1;
4333 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
4335 key = (unsigned char)s[0];
4337 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4338 value=self->stack->data[len-1];
4339 len=PyDict_SetItem(self->memo, py_key, value);
4340 Py_DECREF(py_key);
4341 return len;
4345 static int
4346 load_long_binput(Unpicklerobject *self)
4348 PyObject *py_key = 0, *value = 0;
4349 long key;
4350 unsigned char c;
4351 char *s;
4352 int len;
4354 if (self->read_func(self, &s, 4) < 0) return -1;
4355 if (!( len=self->stack->length )) return stackUnderflow();
4357 c = (unsigned char)s[0];
4358 key = (long)c;
4359 c = (unsigned char)s[1];
4360 key |= (long)c << 8;
4361 c = (unsigned char)s[2];
4362 key |= (long)c << 16;
4363 c = (unsigned char)s[3];
4364 key |= (long)c << 24;
4366 if (!( py_key = PyInt_FromLong(key))) return -1;
4367 value=self->stack->data[len-1];
4368 len=PyDict_SetItem(self->memo, py_key, value);
4369 Py_DECREF(py_key);
4370 return len;
4374 static int
4375 do_append(Unpicklerobject *self, int x)
4377 PyObject *value = 0, *list = 0, *append_method = 0;
4378 int len, i;
4380 len=self->stack->length;
4381 if (!( len >= x && x > 0 )) return stackUnderflow();
4382 /* nothing to do */
4383 if (len==x) return 0;
4385 list=self->stack->data[x-1];
4387 if (PyList_Check(list)) {
4388 PyObject *slice;
4389 int list_len;
4391 slice=Pdata_popList(self->stack, x);
4392 if (! slice) return -1;
4393 list_len = PyList_GET_SIZE(list);
4394 i=PyList_SetSlice(list, list_len, list_len, slice);
4395 Py_DECREF(slice);
4396 return i;
4398 else {
4400 if (!( append_method = PyObject_GetAttr(list, append_str)))
4401 return -1;
4403 for (i = x; i < len; i++) {
4404 PyObject *junk;
4406 value=self->stack->data[i];
4407 junk=0;
4408 ARG_TUP(self, value);
4409 if (self->arg) {
4410 junk = PyObject_Call(append_method, self->arg,
4411 NULL);
4412 FREE_ARG_TUP(self);
4414 if (! junk) {
4415 Pdata_clear(self->stack, i+1);
4416 self->stack->length=x;
4417 Py_DECREF(append_method);
4418 return -1;
4420 Py_DECREF(junk);
4422 self->stack->length=x;
4423 Py_DECREF(append_method);
4426 return 0;
4430 static int
4431 load_append(Unpicklerobject *self)
4433 return do_append(self, self->stack->length - 1);
4437 static int
4438 load_appends(Unpicklerobject *self)
4440 return do_append(self, marker(self));
4444 static int
4445 do_setitems(Unpicklerobject *self, int x)
4447 PyObject *value = 0, *key = 0, *dict = 0;
4448 int len, i, r=0;
4450 if (!( (len=self->stack->length) >= x
4451 && x > 0 )) return stackUnderflow();
4453 dict=self->stack->data[x-1];
4455 for (i = x+1; i < len; i += 2) {
4456 key =self->stack->data[i-1];
4457 value=self->stack->data[i ];
4458 if (PyObject_SetItem(dict, key, value) < 0) {
4459 r=-1;
4460 break;
4464 Pdata_clear(self->stack, x);
4466 return r;
4470 static int
4471 load_setitem(Unpicklerobject *self)
4473 return do_setitems(self, self->stack->length - 2);
4476 static int
4477 load_setitems(Unpicklerobject *self)
4479 return do_setitems(self, marker(self));
4483 static int
4484 load_build(Unpicklerobject *self)
4486 PyObject *state, *inst, *slotstate;
4487 PyObject *__setstate__;
4488 PyObject *d_key, *d_value;
4489 Py_ssize_t i;
4490 int res = -1;
4492 /* Stack is ... instance, state. We want to leave instance at
4493 * the stack top, possibly mutated via instance.__setstate__(state).
4495 if (self->stack->length < 2)
4496 return stackUnderflow();
4497 PDATA_POP(self->stack, state);
4498 if (state == NULL)
4499 return -1;
4500 inst = self->stack->data[self->stack->length - 1];
4502 __setstate__ = PyObject_GetAttr(inst, __setstate___str);
4503 if (__setstate__ != NULL) {
4504 PyObject *junk = NULL;
4506 /* The explicit __setstate__ is responsible for everything. */
4507 ARG_TUP(self, state);
4508 if (self->arg) {
4509 junk = PyObject_Call(__setstate__, self->arg, NULL);
4510 FREE_ARG_TUP(self);
4512 Py_DECREF(__setstate__);
4513 if (junk == NULL)
4514 return -1;
4515 Py_DECREF(junk);
4516 return 0;
4518 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4519 return -1;
4520 PyErr_Clear();
4522 /* A default __setstate__. First see whether state embeds a
4523 * slot state dict too (a proto 2 addition).
4525 if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
4526 PyObject *temp = state;
4527 state = PyTuple_GET_ITEM(temp, 0);
4528 slotstate = PyTuple_GET_ITEM(temp, 1);
4529 Py_INCREF(state);
4530 Py_INCREF(slotstate);
4531 Py_DECREF(temp);
4533 else
4534 slotstate = NULL;
4536 /* Set inst.__dict__ from the state dict (if any). */
4537 if (state != Py_None) {
4538 PyObject *dict;
4539 if (! PyDict_Check(state)) {
4540 PyErr_SetString(UnpicklingError, "state is not a "
4541 "dictionary");
4542 goto finally;
4544 dict = PyObject_GetAttr(inst, __dict___str);
4545 if (dict == NULL)
4546 goto finally;
4548 i = 0;
4549 while (PyDict_Next(state, &i, &d_key, &d_value)) {
4550 /* normally the keys for instance attributes are
4551 interned. we should try to do that here. */
4552 Py_INCREF(d_key);
4553 if (PyString_CheckExact(d_key))
4554 PyString_InternInPlace(&d_key);
4555 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
4556 Py_DECREF(d_key);
4557 goto finally;
4559 Py_DECREF(d_key);
4561 Py_DECREF(dict);
4564 /* Also set instance attributes from the slotstate dict (if any). */
4565 if (slotstate != NULL) {
4566 if (! PyDict_Check(slotstate)) {
4567 PyErr_SetString(UnpicklingError, "slot state is not "
4568 "a dictionary");
4569 goto finally;
4571 i = 0;
4572 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
4573 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
4574 goto finally;
4577 res = 0;
4579 finally:
4580 Py_DECREF(state);
4581 Py_XDECREF(slotstate);
4582 return res;
4586 static int
4587 load_mark(Unpicklerobject *self)
4589 int s;
4591 /* Note that we split the (pickle.py) stack into two stacks, an
4592 object stack and a mark stack. Here we push a mark onto the
4593 mark stack.
4596 if ((self->num_marks + 1) >= self->marks_size) {
4597 int *marks;
4598 s=self->marks_size+20;
4599 if (s <= self->num_marks) s=self->num_marks + 1;
4600 if (self->marks == NULL)
4601 marks=(int *)malloc(s * sizeof(int));
4602 else
4603 marks=(int *)realloc(self->marks,
4604 s * sizeof(int));
4605 if (!marks) {
4606 PyErr_NoMemory();
4607 return -1;
4609 self->marks = marks;
4610 self->marks_size = s;
4613 self->marks[self->num_marks++] = self->stack->length;
4615 return 0;
4618 static int
4619 load_reduce(Unpicklerobject *self)
4621 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
4623 PDATA_POP(self->stack, arg_tup);
4624 if (! arg_tup) return -1;
4625 PDATA_POP(self->stack, callable);
4626 if (callable) {
4627 ob = Instance_New(callable, arg_tup);
4628 Py_DECREF(callable);
4630 Py_DECREF(arg_tup);
4632 if (! ob) return -1;
4634 PDATA_PUSH(self->stack, ob, -1);
4635 return 0;
4638 /* Just raises an error if we don't know the protocol specified. PROTO
4639 * is the first opcode for protocols >= 2.
4641 static int
4642 load_proto(Unpicklerobject *self)
4644 int i;
4645 char *protobyte;
4647 i = self->read_func(self, &protobyte, 1);
4648 if (i < 0)
4649 return -1;
4651 i = calc_binint(protobyte, 1);
4652 /* No point checking for < 0, since calc_binint returns an unsigned
4653 * int when chewing on 1 byte.
4655 assert(i >= 0);
4656 if (i <= HIGHEST_PROTOCOL)
4657 return 0;
4659 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4660 return -1;
4663 static PyObject *
4664 load(Unpicklerobject *self)
4666 PyObject *err = 0, *val = 0;
4667 char *s;
4669 self->num_marks = 0;
4670 if (self->stack->length) Pdata_clear(self->stack, 0);
4672 while (1) {
4673 if (self->read_func(self, &s, 1) < 0)
4674 break;
4676 switch (s[0]) {
4677 case NONE:
4678 if (load_none(self) < 0)
4679 break;
4680 continue;
4682 case BININT:
4683 if (load_binint(self) < 0)
4684 break;
4685 continue;
4687 case BININT1:
4688 if (load_binint1(self) < 0)
4689 break;
4690 continue;
4692 case BININT2:
4693 if (load_binint2(self) < 0)
4694 break;
4695 continue;
4697 case INT:
4698 if (load_int(self) < 0)
4699 break;
4700 continue;
4702 case LONG:
4703 if (load_long(self) < 0)
4704 break;
4705 continue;
4707 case LONG1:
4708 if (load_counted_long(self, 1) < 0)
4709 break;
4710 continue;
4712 case LONG4:
4713 if (load_counted_long(self, 4) < 0)
4714 break;
4715 continue;
4717 case FLOAT:
4718 if (load_float(self) < 0)
4719 break;
4720 continue;
4722 case BINFLOAT:
4723 if (load_binfloat(self) < 0)
4724 break;
4725 continue;
4727 case BINSTRING:
4728 if (load_binstring(self) < 0)
4729 break;
4730 continue;
4732 case SHORT_BINSTRING:
4733 if (load_short_binstring(self) < 0)
4734 break;
4735 continue;
4737 case STRING:
4738 if (load_string(self) < 0)
4739 break;
4740 continue;
4742 #ifdef Py_USING_UNICODE
4743 case UNICODE:
4744 if (load_unicode(self) < 0)
4745 break;
4746 continue;
4748 case BINUNICODE:
4749 if (load_binunicode(self) < 0)
4750 break;
4751 continue;
4752 #endif
4754 case EMPTY_TUPLE:
4755 if (load_counted_tuple(self, 0) < 0)
4756 break;
4757 continue;
4759 case TUPLE1:
4760 if (load_counted_tuple(self, 1) < 0)
4761 break;
4762 continue;
4764 case TUPLE2:
4765 if (load_counted_tuple(self, 2) < 0)
4766 break;
4767 continue;
4769 case TUPLE3:
4770 if (load_counted_tuple(self, 3) < 0)
4771 break;
4772 continue;
4774 case TUPLE:
4775 if (load_tuple(self) < 0)
4776 break;
4777 continue;
4779 case EMPTY_LIST:
4780 if (load_empty_list(self) < 0)
4781 break;
4782 continue;
4784 case LIST:
4785 if (load_list(self) < 0)
4786 break;
4787 continue;
4789 case EMPTY_DICT:
4790 if (load_empty_dict(self) < 0)
4791 break;
4792 continue;
4794 case DICT:
4795 if (load_dict(self) < 0)
4796 break;
4797 continue;
4799 case OBJ:
4800 if (load_obj(self) < 0)
4801 break;
4802 continue;
4804 case INST:
4805 if (load_inst(self) < 0)
4806 break;
4807 continue;
4809 case NEWOBJ:
4810 if (load_newobj(self) < 0)
4811 break;
4812 continue;
4814 case GLOBAL:
4815 if (load_global(self) < 0)
4816 break;
4817 continue;
4819 case APPEND:
4820 if (load_append(self) < 0)
4821 break;
4822 continue;
4824 case APPENDS:
4825 if (load_appends(self) < 0)
4826 break;
4827 continue;
4829 case BUILD:
4830 if (load_build(self) < 0)
4831 break;
4832 continue;
4834 case DUP:
4835 if (load_dup(self) < 0)
4836 break;
4837 continue;
4839 case BINGET:
4840 if (load_binget(self) < 0)
4841 break;
4842 continue;
4844 case LONG_BINGET:
4845 if (load_long_binget(self) < 0)
4846 break;
4847 continue;
4849 case GET:
4850 if (load_get(self) < 0)
4851 break;
4852 continue;
4854 case EXT1:
4855 if (load_extension(self, 1) < 0)
4856 break;
4857 continue;
4859 case EXT2:
4860 if (load_extension(self, 2) < 0)
4861 break;
4862 continue;
4864 case EXT4:
4865 if (load_extension(self, 4) < 0)
4866 break;
4867 continue;
4868 case MARK:
4869 if (load_mark(self) < 0)
4870 break;
4871 continue;
4873 case BINPUT:
4874 if (load_binput(self) < 0)
4875 break;
4876 continue;
4878 case LONG_BINPUT:
4879 if (load_long_binput(self) < 0)
4880 break;
4881 continue;
4883 case PUT:
4884 if (load_put(self) < 0)
4885 break;
4886 continue;
4888 case POP:
4889 if (load_pop(self) < 0)
4890 break;
4891 continue;
4893 case POP_MARK:
4894 if (load_pop_mark(self) < 0)
4895 break;
4896 continue;
4898 case SETITEM:
4899 if (load_setitem(self) < 0)
4900 break;
4901 continue;
4903 case SETITEMS:
4904 if (load_setitems(self) < 0)
4905 break;
4906 continue;
4908 case STOP:
4909 break;
4911 case PERSID:
4912 if (load_persid(self) < 0)
4913 break;
4914 continue;
4916 case BINPERSID:
4917 if (load_binpersid(self) < 0)
4918 break;
4919 continue;
4921 case REDUCE:
4922 if (load_reduce(self) < 0)
4923 break;
4924 continue;
4926 case PROTO:
4927 if (load_proto(self) < 0)
4928 break;
4929 continue;
4931 case NEWTRUE:
4932 if (load_bool(self, Py_True) < 0)
4933 break;
4934 continue;
4936 case NEWFALSE:
4937 if (load_bool(self, Py_False) < 0)
4938 break;
4939 continue;
4941 case '\0':
4942 /* end of file */
4943 PyErr_SetNone(PyExc_EOFError);
4944 break;
4946 default:
4947 cPickle_ErrFormat(UnpicklingError,
4948 "invalid load key, '%s'.",
4949 "c", s[0]);
4950 return NULL;
4953 break;
4956 if ((err = PyErr_Occurred())) {
4957 if (err == PyExc_EOFError) {
4958 PyErr_SetNone(PyExc_EOFError);
4960 return NULL;
4963 PDATA_POP(self->stack, val);
4964 return val;
4968 /* No-load functions to support noload, which is used to
4969 find persistent references. */
4971 static int
4972 noload_obj(Unpicklerobject *self)
4974 int i;
4976 if ((i = marker(self)) < 0) return -1;
4977 return Pdata_clear(self->stack, i+1);
4981 static int
4982 noload_inst(Unpicklerobject *self)
4984 int i;
4985 char *s;
4987 if ((i = marker(self)) < 0) return -1;
4988 Pdata_clear(self->stack, i);
4989 if (self->readline_func(self, &s) < 0) return -1;
4990 if (self->readline_func(self, &s) < 0) return -1;
4991 PDATA_APPEND(self->stack, Py_None, -1);
4992 return 0;
4995 static int
4996 noload_newobj(Unpicklerobject *self)
4998 PyObject *obj;
5000 PDATA_POP(self->stack, obj); /* pop argtuple */
5001 if (obj == NULL) return -1;
5002 Py_DECREF(obj);
5004 PDATA_POP(self->stack, obj); /* pop cls */
5005 if (obj == NULL) return -1;
5006 Py_DECREF(obj);
5008 PDATA_APPEND(self->stack, Py_None, -1);
5009 return 0;
5012 static int
5013 noload_global(Unpicklerobject *self)
5015 char *s;
5017 if (self->readline_func(self, &s) < 0) return -1;
5018 if (self->readline_func(self, &s) < 0) return -1;
5019 PDATA_APPEND(self->stack, Py_None,-1);
5020 return 0;
5023 static int
5024 noload_reduce(Unpicklerobject *self)
5027 if (self->stack->length < 2) return stackUnderflow();
5028 Pdata_clear(self->stack, self->stack->length-2);
5029 PDATA_APPEND(self->stack, Py_None,-1);
5030 return 0;
5033 static int
5034 noload_build(Unpicklerobject *self) {
5036 if (self->stack->length < 1) return stackUnderflow();
5037 Pdata_clear(self->stack, self->stack->length-1);
5038 return 0;
5041 static int
5042 noload_extension(Unpicklerobject *self, int nbytes)
5044 char *codebytes;
5046 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
5047 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
5048 PDATA_APPEND(self->stack, Py_None, -1);
5049 return 0;
5052 static int
5053 noload_append(Unpicklerobject *self)
5055 return Pdata_clear(self->stack, self->stack->length - 1);
5058 static int
5059 noload_appends(Unpicklerobject *self)
5061 int i;
5062 if ((i = marker(self)) < 0) return -1;
5063 return Pdata_clear(self->stack, i);
5066 static int
5067 noload_setitem(Unpicklerobject *self)
5069 return Pdata_clear(self->stack, self->stack->length - 2);
5072 static int
5073 noload_setitems(Unpicklerobject *self)
5075 int i;
5076 if ((i = marker(self)) < 0) return -1;
5077 return Pdata_clear(self->stack, i);
5080 static PyObject *
5081 noload(Unpicklerobject *self)
5083 PyObject *err = 0, *val = 0;
5084 char *s;
5086 self->num_marks = 0;
5087 Pdata_clear(self->stack, 0);
5089 while (1) {
5090 if (self->read_func(self, &s, 1) < 0)
5091 break;
5093 switch (s[0]) {
5094 case NONE:
5095 if (load_none(self) < 0)
5096 break;
5097 continue;
5099 case BININT:
5100 if (load_binint(self) < 0)
5101 break;
5102 continue;
5104 case BININT1:
5105 if (load_binint1(self) < 0)
5106 break;
5107 continue;
5109 case BININT2:
5110 if (load_binint2(self) < 0)
5111 break;
5112 continue;
5114 case INT:
5115 if (load_int(self) < 0)
5116 break;
5117 continue;
5119 case LONG:
5120 if (load_long(self) < 0)
5121 break;
5122 continue;
5124 case LONG1:
5125 if (load_counted_long(self, 1) < 0)
5126 break;
5127 continue;
5129 case LONG4:
5130 if (load_counted_long(self, 4) < 0)
5131 break;
5132 continue;
5134 case FLOAT:
5135 if (load_float(self) < 0)
5136 break;
5137 continue;
5139 case BINFLOAT:
5140 if (load_binfloat(self) < 0)
5141 break;
5142 continue;
5144 case BINSTRING:
5145 if (load_binstring(self) < 0)
5146 break;
5147 continue;
5149 case SHORT_BINSTRING:
5150 if (load_short_binstring(self) < 0)
5151 break;
5152 continue;
5154 case STRING:
5155 if (load_string(self) < 0)
5156 break;
5157 continue;
5159 #ifdef Py_USING_UNICODE
5160 case UNICODE:
5161 if (load_unicode(self) < 0)
5162 break;
5163 continue;
5165 case BINUNICODE:
5166 if (load_binunicode(self) < 0)
5167 break;
5168 continue;
5169 #endif
5171 case EMPTY_TUPLE:
5172 if (load_counted_tuple(self, 0) < 0)
5173 break;
5174 continue;
5176 case TUPLE1:
5177 if (load_counted_tuple(self, 1) < 0)
5178 break;
5179 continue;
5181 case TUPLE2:
5182 if (load_counted_tuple(self, 2) < 0)
5183 break;
5184 continue;
5186 case TUPLE3:
5187 if (load_counted_tuple(self, 3) < 0)
5188 break;
5189 continue;
5191 case TUPLE:
5192 if (load_tuple(self) < 0)
5193 break;
5194 continue;
5196 case EMPTY_LIST:
5197 if (load_empty_list(self) < 0)
5198 break;
5199 continue;
5201 case LIST:
5202 if (load_list(self) < 0)
5203 break;
5204 continue;
5206 case EMPTY_DICT:
5207 if (load_empty_dict(self) < 0)
5208 break;
5209 continue;
5211 case DICT:
5212 if (load_dict(self) < 0)
5213 break;
5214 continue;
5216 case OBJ:
5217 if (noload_obj(self) < 0)
5218 break;
5219 continue;
5221 case INST:
5222 if (noload_inst(self) < 0)
5223 break;
5224 continue;
5226 case NEWOBJ:
5227 if (noload_newobj(self) < 0)
5228 break;
5229 continue;
5231 case GLOBAL:
5232 if (noload_global(self) < 0)
5233 break;
5234 continue;
5236 case APPEND:
5237 if (noload_append(self) < 0)
5238 break;
5239 continue;
5241 case APPENDS:
5242 if (noload_appends(self) < 0)
5243 break;
5244 continue;
5246 case BUILD:
5247 if (noload_build(self) < 0)
5248 break;
5249 continue;
5251 case DUP:
5252 if (load_dup(self) < 0)
5253 break;
5254 continue;
5256 case BINGET:
5257 if (load_binget(self) < 0)
5258 break;
5259 continue;
5261 case LONG_BINGET:
5262 if (load_long_binget(self) < 0)
5263 break;
5264 continue;
5266 case GET:
5267 if (load_get(self) < 0)
5268 break;
5269 continue;
5271 case EXT1:
5272 if (noload_extension(self, 1) < 0)
5273 break;
5274 continue;
5276 case EXT2:
5277 if (noload_extension(self, 2) < 0)
5278 break;
5279 continue;
5281 case EXT4:
5282 if (noload_extension(self, 4) < 0)
5283 break;
5284 continue;
5286 case MARK:
5287 if (load_mark(self) < 0)
5288 break;
5289 continue;
5291 case BINPUT:
5292 if (load_binput(self) < 0)
5293 break;
5294 continue;
5296 case LONG_BINPUT:
5297 if (load_long_binput(self) < 0)
5298 break;
5299 continue;
5301 case PUT:
5302 if (load_put(self) < 0)
5303 break;
5304 continue;
5306 case POP:
5307 if (load_pop(self) < 0)
5308 break;
5309 continue;
5311 case POP_MARK:
5312 if (load_pop_mark(self) < 0)
5313 break;
5314 continue;
5316 case SETITEM:
5317 if (noload_setitem(self) < 0)
5318 break;
5319 continue;
5321 case SETITEMS:
5322 if (noload_setitems(self) < 0)
5323 break;
5324 continue;
5326 case STOP:
5327 break;
5329 case PERSID:
5330 if (load_persid(self) < 0)
5331 break;
5332 continue;
5334 case BINPERSID:
5335 if (load_binpersid(self) < 0)
5336 break;
5337 continue;
5339 case REDUCE:
5340 if (noload_reduce(self) < 0)
5341 break;
5342 continue;
5344 case PROTO:
5345 if (load_proto(self) < 0)
5346 break;
5347 continue;
5349 case NEWTRUE:
5350 if (load_bool(self, Py_True) < 0)
5351 break;
5352 continue;
5354 case NEWFALSE:
5355 if (load_bool(self, Py_False) < 0)
5356 break;
5357 continue;
5358 default:
5359 cPickle_ErrFormat(UnpicklingError,
5360 "invalid load key, '%s'.",
5361 "c", s[0]);
5362 return NULL;
5365 break;
5368 if ((err = PyErr_Occurred())) {
5369 if (err == PyExc_EOFError) {
5370 PyErr_SetNone(PyExc_EOFError);
5372 return NULL;
5375 PDATA_POP(self->stack, val);
5376 return val;
5380 static PyObject *
5381 Unpickler_load(Unpicklerobject *self, PyObject *unused)
5383 return load(self);
5386 static PyObject *
5387 Unpickler_noload(Unpicklerobject *self, PyObject *unused)
5389 return noload(self);
5393 static struct PyMethodDef Unpickler_methods[] = {
5394 {"load", (PyCFunction)Unpickler_load, METH_NOARGS,
5395 PyDoc_STR("load() -- Load a pickle")
5397 {"noload", (PyCFunction)Unpickler_noload, METH_NOARGS,
5398 PyDoc_STR(
5399 "noload() -- not load a pickle, but go through most of the motions\n"
5400 "\n"
5401 "This function can be used to read past a pickle without instantiating\n"
5402 "any objects or importing any modules. It can also be used to find all\n"
5403 "persistent references without instantiating any objects or importing\n"
5404 "any modules.\n")
5406 {NULL, NULL} /* sentinel */
5410 static Unpicklerobject *
5411 newUnpicklerobject(PyObject *f)
5413 Unpicklerobject *self;
5415 if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype)))
5416 return NULL;
5418 self->file = NULL;
5419 self->arg = NULL;
5420 self->stack = (Pdata*)Pdata_New();
5421 self->pers_func = NULL;
5422 self->last_string = NULL;
5423 self->marks = NULL;
5424 self->num_marks = 0;
5425 self->marks_size = 0;
5426 self->buf_size = 0;
5427 self->read = NULL;
5428 self->readline = NULL;
5429 self->find_class = NULL;
5431 if (!( self->memo = PyDict_New()))
5432 goto err;
5434 if (!self->stack)
5435 goto err;
5437 Py_INCREF(f);
5438 self->file = f;
5440 /* Set read, readline based on type of f */
5441 if (PyFile_Check(f)) {
5442 self->fp = PyFile_AsFile(f);
5443 if (self->fp == NULL) {
5444 PyErr_SetString(PyExc_ValueError,
5445 "I/O operation on closed file");
5446 goto err;
5448 self->read_func = read_file;
5449 self->readline_func = readline_file;
5451 else if (PycStringIO_InputCheck(f)) {
5452 self->fp = NULL;
5453 self->read_func = read_cStringIO;
5454 self->readline_func = readline_cStringIO;
5456 else {
5458 self->fp = NULL;
5459 self->read_func = read_other;
5460 self->readline_func = readline_other;
5462 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5463 (self->read = PyObject_GetAttr(f, read_str)))) {
5464 PyErr_Clear();
5465 PyErr_SetString( PyExc_TypeError,
5466 "argument must have 'read' and "
5467 "'readline' attributes" );
5468 goto err;
5471 PyObject_GC_Track(self);
5473 return self;
5475 err:
5476 Py_DECREF((PyObject *)self);
5477 return NULL;
5481 static PyObject *
5482 get_Unpickler(PyObject *self, PyObject *file)
5484 return (PyObject *)newUnpicklerobject(file);
5488 static void
5489 Unpickler_dealloc(Unpicklerobject *self)
5491 PyObject_GC_UnTrack((PyObject *)self);
5492 Py_XDECREF(self->readline);
5493 Py_XDECREF(self->read);
5494 Py_XDECREF(self->file);
5495 Py_XDECREF(self->memo);
5496 Py_XDECREF(self->stack);
5497 Py_XDECREF(self->pers_func);
5498 Py_XDECREF(self->arg);
5499 Py_XDECREF(self->last_string);
5500 Py_XDECREF(self->find_class);
5502 if (self->marks) {
5503 free(self->marks);
5506 if (self->buf_size) {
5507 free(self->buf);
5510 Py_TYPE(self)->tp_free((PyObject *)self);
5513 static int
5514 Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg)
5516 Py_VISIT(self->readline);
5517 Py_VISIT(self->read);
5518 Py_VISIT(self->file);
5519 Py_VISIT(self->memo);
5520 Py_VISIT(self->stack);
5521 Py_VISIT(self->pers_func);
5522 Py_VISIT(self->arg);
5523 Py_VISIT(self->last_string);
5524 Py_VISIT(self->find_class);
5525 return 0;
5528 static int
5529 Unpickler_clear(Unpicklerobject *self)
5531 Py_CLEAR(self->readline);
5532 Py_CLEAR(self->read);
5533 Py_CLEAR(self->file);
5534 Py_CLEAR(self->memo);
5535 Py_CLEAR(self->stack);
5536 Py_CLEAR(self->pers_func);
5537 Py_CLEAR(self->arg);
5538 Py_CLEAR(self->last_string);
5539 Py_CLEAR(self->find_class);
5540 return 0;
5543 static PyObject *
5544 Unpickler_getattr(Unpicklerobject *self, char *name)
5546 if (!strcmp(name, "persistent_load")) {
5547 if (!self->pers_func) {
5548 PyErr_SetString(PyExc_AttributeError, name);
5549 return NULL;
5552 Py_INCREF(self->pers_func);
5553 return self->pers_func;
5556 if (!strcmp(name, "find_global")) {
5557 if (!self->find_class) {
5558 PyErr_SetString(PyExc_AttributeError, name);
5559 return NULL;
5562 Py_INCREF(self->find_class);
5563 return self->find_class;
5566 if (!strcmp(name, "memo")) {
5567 if (!self->memo) {
5568 PyErr_SetString(PyExc_AttributeError, name);
5569 return NULL;
5572 Py_INCREF(self->memo);
5573 return self->memo;
5576 if (!strcmp(name, "UnpicklingError")) {
5577 Py_INCREF(UnpicklingError);
5578 return UnpicklingError;
5581 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
5585 static int
5586 Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
5589 if (!strcmp(name, "persistent_load")) {
5590 Py_XDECREF(self->pers_func);
5591 self->pers_func = value;
5592 Py_XINCREF(value);
5593 return 0;
5596 if (!strcmp(name, "find_global")) {
5597 Py_XDECREF(self->find_class);
5598 self->find_class = value;
5599 Py_XINCREF(value);
5600 return 0;
5603 if (! value) {
5604 PyErr_SetString(PyExc_TypeError,
5605 "attribute deletion is not supported");
5606 return -1;
5609 if (strcmp(name, "memo") == 0) {
5610 if (!PyDict_Check(value)) {
5611 PyErr_SetString(PyExc_TypeError,
5612 "memo must be a dictionary");
5613 return -1;
5615 Py_XDECREF(self->memo);
5616 self->memo = value;
5617 Py_INCREF(value);
5618 return 0;
5621 PyErr_SetString(PyExc_AttributeError, name);
5622 return -1;
5625 /* ---------------------------------------------------------------------------
5626 * Module-level functions.
5629 /* dump(obj, file, protocol=0). */
5630 static PyObject *
5631 cpm_dump(PyObject *self, PyObject *args, PyObject *kwds)
5633 static char *kwlist[] = {"obj", "file", "protocol", NULL};
5634 PyObject *ob, *file, *res = NULL;
5635 Picklerobject *pickler = 0;
5636 int proto = 0;
5638 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist,
5639 &ob, &file, &proto)))
5640 goto finally;
5642 if (!( pickler = newPicklerobject(file, proto)))
5643 goto finally;
5645 if (dump(pickler, ob) < 0)
5646 goto finally;
5648 Py_INCREF(Py_None);
5649 res = Py_None;
5651 finally:
5652 Py_XDECREF(pickler);
5654 return res;
5658 /* dumps(obj, protocol=0). */
5659 static PyObject *
5660 cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds)
5662 static char *kwlist[] = {"obj", "protocol", NULL};
5663 PyObject *ob, *file = 0, *res = NULL;
5664 Picklerobject *pickler = 0;
5665 int proto = 0;
5667 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist,
5668 &ob, &proto)))
5669 goto finally;
5671 if (!( file = PycStringIO->NewOutput(128)))
5672 goto finally;
5674 if (!( pickler = newPicklerobject(file, proto)))
5675 goto finally;
5677 if (dump(pickler, ob) < 0)
5678 goto finally;
5680 res = PycStringIO->cgetvalue(file);
5682 finally:
5683 Py_XDECREF(pickler);
5684 Py_XDECREF(file);
5686 return res;
5690 /* load(fileobj). */
5691 static PyObject *
5692 cpm_load(PyObject *self, PyObject *ob)
5694 Unpicklerobject *unpickler = 0;
5695 PyObject *res = NULL;
5697 if (!( unpickler = newUnpicklerobject(ob)))
5698 goto finally;
5700 res = load(unpickler);
5702 finally:
5703 Py_XDECREF(unpickler);
5705 return res;
5709 /* loads(string) */
5710 static PyObject *
5711 cpm_loads(PyObject *self, PyObject *args)
5713 PyObject *ob, *file = 0, *res = NULL;
5714 Unpicklerobject *unpickler = 0;
5716 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
5717 goto finally;
5719 if (!( file = PycStringIO->NewInput(ob)))
5720 goto finally;
5722 if (!( unpickler = newUnpicklerobject(file)))
5723 goto finally;
5725 res = load(unpickler);
5727 finally:
5728 Py_XDECREF(file);
5729 Py_XDECREF(unpickler);
5731 return res;
5735 PyDoc_STRVAR(Unpicklertype__doc__,
5736 "Objects that know how to unpickle");
5738 static PyTypeObject Unpicklertype = {
5739 PyVarObject_HEAD_INIT(NULL, 0)
5740 "cPickle.Unpickler", /*tp_name*/
5741 sizeof(Unpicklerobject), /*tp_basicsize*/
5743 (destructor)Unpickler_dealloc, /* tp_dealloc */
5744 0, /* tp_print */
5745 (getattrfunc)Unpickler_getattr, /* tp_getattr */
5746 (setattrfunc)Unpickler_setattr, /* tp_setattr */
5747 0, /* tp_compare */
5748 0, /* tp_repr */
5749 0, /* tp_as_number */
5750 0, /* tp_as_sequence */
5751 0, /* tp_as_mapping */
5752 0, /* tp_hash */
5753 0, /* tp_call */
5754 0, /* tp_str */
5755 0, /* tp_getattro */
5756 0, /* tp_setattro */
5757 0, /* tp_as_buffer */
5758 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
5759 Unpicklertype__doc__, /* tp_doc */
5760 (traverseproc)Unpickler_traverse, /* tp_traverse */
5761 (inquiry)Unpickler_clear, /* tp_clear */
5764 static struct PyMethodDef cPickle_methods[] = {
5765 {"dump", (PyCFunction)cpm_dump, METH_VARARGS | METH_KEYWORDS,
5766 PyDoc_STR("dump(obj, file, protocol=0) -- "
5767 "Write an object in pickle format to the given file.\n"
5768 "\n"
5769 "See the Pickler docstring for the meaning of optional argument proto.")
5772 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS | METH_KEYWORDS,
5773 PyDoc_STR("dumps(obj, protocol=0) -- "
5774 "Return a string containing an object in pickle format.\n"
5775 "\n"
5776 "See the Pickler docstring for the meaning of optional argument proto.")
5779 {"load", (PyCFunction)cpm_load, METH_O,
5780 PyDoc_STR("load(file) -- Load a pickle from the given file")},
5782 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
5783 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
5785 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS | METH_KEYWORDS,
5786 PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n"
5787 "\n"
5788 "This takes a file-like object for writing a pickle data stream.\n"
5789 "The optional proto argument tells the pickler to use the given\n"
5790 "protocol; supported protocols are 0, 1, 2. The default\n"
5791 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5792 "only protocol that can be written to a file opened in text\n"
5793 "mode and read back successfully. When using a protocol higher\n"
5794 "than 0, make sure the file is opened in binary mode, both when\n"
5795 "pickling and unpickling.)\n"
5796 "\n"
5797 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5798 "more efficient than protocol 1.\n"
5799 "\n"
5800 "Specifying a negative protocol version selects the highest\n"
5801 "protocol version supported. The higher the protocol used, the\n"
5802 "more recent the version of Python needed to read the pickle\n"
5803 "produced.\n"
5804 "\n"
5805 "The file parameter must have a write() method that accepts a single\n"
5806 "string argument. It can thus be an open file object, a StringIO\n"
5807 "object, or any other custom object that meets this interface.\n")
5810 {"Unpickler", (PyCFunction)get_Unpickler, METH_O,
5811 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5813 { NULL, NULL }
5816 static int
5817 init_stuff(PyObject *module_dict)
5819 PyObject *copyreg, *t, *r;
5821 #define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
5823 if (PyType_Ready(&Unpicklertype) < 0)
5824 return -1;
5825 if (PyType_Ready(&Picklertype) < 0)
5826 return -1;
5828 INIT_STR(__class__);
5829 INIT_STR(__getinitargs__);
5830 INIT_STR(__dict__);
5831 INIT_STR(__getstate__);
5832 INIT_STR(__setstate__);
5833 INIT_STR(__name__);
5834 INIT_STR(__main__);
5835 INIT_STR(__reduce__);
5836 INIT_STR(__reduce_ex__);
5837 INIT_STR(write);
5838 INIT_STR(append);
5839 INIT_STR(read);
5840 INIT_STR(readline);
5841 INIT_STR(copyreg);
5842 INIT_STR(dispatch_table);
5844 if (!( copyreg = PyImport_ImportModule("copy_reg")))
5845 return -1;
5847 /* This is special because we want to use a different
5848 one in restricted mode. */
5849 dispatch_table = PyObject_GetAttr(copyreg, dispatch_table_str);
5850 if (!dispatch_table) return -1;
5852 extension_registry = PyObject_GetAttrString(copyreg,
5853 "_extension_registry");
5854 if (!extension_registry) return -1;
5856 inverted_registry = PyObject_GetAttrString(copyreg,
5857 "_inverted_registry");
5858 if (!inverted_registry) return -1;
5860 extension_cache = PyObject_GetAttrString(copyreg,
5861 "_extension_cache");
5862 if (!extension_cache) return -1;
5864 Py_DECREF(copyreg);
5866 if (!(empty_tuple = PyTuple_New(0)))
5867 return -1;
5869 two_tuple = PyTuple_New(2);
5870 if (two_tuple == NULL)
5871 return -1;
5872 /* We use this temp container with no regard to refcounts, or to
5873 * keeping containees alive. Exempt from GC, because we don't
5874 * want anything looking at two_tuple() by magic.
5876 PyObject_GC_UnTrack(two_tuple);
5878 /* Ugh */
5879 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5880 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5881 return -1;
5883 if (!( t=PyDict_New())) return -1;
5884 if (!( r=PyRun_String(
5885 "def __str__(self):\n"
5886 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5887 Py_file_input,
5888 module_dict, t) )) return -1;
5889 Py_DECREF(r);
5891 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
5892 if (!PickleError)
5893 return -1;
5895 Py_DECREF(t);
5897 PicklingError = PyErr_NewException("cPickle.PicklingError",
5898 PickleError, NULL);
5899 if (!PicklingError)
5900 return -1;
5902 if (!( t=PyDict_New())) return -1;
5903 if (!( r=PyRun_String(
5904 "def __str__(self):\n"
5905 " a=self.args\n"
5906 " a=a and type(a[0]) or '(what)'\n"
5907 " return 'Cannot pickle %s objects' % a\n"
5908 , Py_file_input,
5909 module_dict, t) )) return -1;
5910 Py_DECREF(r);
5912 if (!( UnpickleableError = PyErr_NewException(
5913 "cPickle.UnpickleableError", PicklingError, t)))
5914 return -1;
5916 Py_DECREF(t);
5918 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
5919 PickleError, NULL)))
5920 return -1;
5922 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5923 UnpicklingError, NULL)))
5924 return -1;
5926 if (PyDict_SetItemString(module_dict, "PickleError",
5927 PickleError) < 0)
5928 return -1;
5930 if (PyDict_SetItemString(module_dict, "PicklingError",
5931 PicklingError) < 0)
5932 return -1;
5934 if (PyDict_SetItemString(module_dict, "UnpicklingError",
5935 UnpicklingError) < 0)
5936 return -1;
5938 if (PyDict_SetItemString(module_dict, "UnpickleableError",
5939 UnpickleableError) < 0)
5940 return -1;
5942 if (PyDict_SetItemString(module_dict, "BadPickleGet",
5943 BadPickleGet) < 0)
5944 return -1;
5946 PycString_IMPORT;
5948 return 0;
5951 #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5952 #define PyMODINIT_FUNC void
5953 #endif
5954 PyMODINIT_FUNC
5955 initcPickle(void)
5957 PyObject *m, *d, *di, *v, *k;
5958 Py_ssize_t i;
5959 char *rev = "1.71"; /* XXX when does this change? */
5960 PyObject *format_version;
5961 PyObject *compatible_formats;
5963 /* XXX: Should mention that the pickle module will include the C
5964 XXX: optimized implementation automatically. */
5965 if (PyErr_WarnPy3k("the cPickle module has been removed in "
5966 "Python 3.0", 2) < 0)
5967 return;
5969 Py_TYPE(&Picklertype) = &PyType_Type;
5970 Py_TYPE(&Unpicklertype) = &PyType_Type;
5971 Py_TYPE(&PdataType) = &PyType_Type;
5973 /* Initialize some pieces. We need to do this before module creation,
5974 * so we're forced to use a temporary dictionary. :(
5976 di = PyDict_New();
5977 if (!di) return;
5978 if (init_stuff(di) < 0) return;
5980 /* Create the module and add the functions */
5981 m = Py_InitModule4("cPickle", cPickle_methods,
5982 cPickle_module_documentation,
5983 (PyObject*)NULL,PYTHON_API_VERSION);
5984 if (m == NULL)
5985 return;
5987 /* Add some symbolic constants to the module */
5988 d = PyModule_GetDict(m);
5989 v = PyString_FromString(rev);
5990 PyDict_SetItemString(d, "__version__", v);
5991 Py_XDECREF(v);
5993 /* Copy data from di. Waaa. */
5994 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
5995 if (PyObject_SetItem(d, k, v) < 0) {
5996 Py_DECREF(di);
5997 return;
6000 Py_DECREF(di);
6002 i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
6003 if (i < 0)
6004 return;
6006 /* These are purely informational; no code uses them. */
6007 /* File format version we write. */
6008 format_version = PyString_FromString("2.0");
6009 /* Format versions we can read. */
6010 compatible_formats = Py_BuildValue("[sssss]",
6011 "1.0", /* Original protocol 0 */
6012 "1.1", /* Protocol 0 + INST */
6013 "1.2", /* Original protocol 1 */
6014 "1.3", /* Protocol 1 + BINFLOAT */
6015 "2.0"); /* Original protocol 2 */
6016 PyDict_SetItemString(d, "format_version", format_version);
6017 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
6018 Py_XDECREF(format_version);
6019 Py_XDECREF(compatible_formats);