blob: f342051040433ba378439a2a48995e21749798f5 [file] [log] [blame]
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001/*
2 $Id$
3
4 Copyright
5
6 Copyright 1996 Digital Creations, L.C., 910 Princess Anne
7 Street, Suite 300, Fredericksburg, Virginia 22401 U.S.A. All
8 rights reserved. Copyright in this software is owned by DCLC,
9 unless otherwise indicated. Permission to use, copy and
10 distribute this software is hereby granted, provided that the
11 above copyright notice appear in all copies and that both that
12 copyright notice and this permission notice appear. Note that
13 any product, process or technology described in this software
14 may be the subject of other Intellectual Property rights
15 reserved by Digital Creations, L.C. and are not licensed
16 hereunder.
17
18 Trademarks
19
20 Digital Creations & DCLC, are trademarks of Digital Creations, L.C..
21 All other trademarks are owned by their respective companies.
22
23 No Warranty
24
25 The software is provided "as is" without warranty of any kind,
26 either express or implied, including, but not limited to, the
27 implied warranties of merchantability, fitness for a particular
28 purpose, or non-infringement. This software could include
29 technical inaccuracies or typographical errors. Changes are
30 periodically made to the software; these changes will be
31 incorporated in new editions of the software. DCLC may make
32 improvements and/or changes in this software at any time
33 without notice.
34
35 Limitation Of Liability
36
37 In no event will DCLC be liable for direct, indirect, special,
38 incidental, economic, cover, or consequential damages arising
39 out of the use of or inability to use this software even if
40 advised of the possibility of such damages. Some states do not
41 allow the exclusion or limitation of implied warranties or
42 limitation of liability for incidental or consequential
43 damages, so the above limitation or exclusion may not apply to
44 you.
45
46 If you have questions regarding this software,
47 contact:
48
49 Jim Fulton, jim@digicool.com
50 Digital Creations L.C.
51
52 (540) 371-6909
53*/
54
55static char cPickle_module_documentation[] =
56""
57;
58
59#include "Python.h"
60#include "cStringIO.h"
61#include "graminit.h"
62
63#include <errno.h>
64
65static PyObject *ErrorObject;
66
67#ifdef __cplusplus
68#define ARG(T, N) T N
69#define ARGDECL(T, N)
70#else
71#define ARG(T, N) N
72#define ARGDECL(T, N) T N;
73#endif
74
75#define UNLESS(E) if (!(E))
76#define ASSIGN(V,E) {PyObject *__e; __e=(E); Py_XDECREF(V); (V)=__e;}
77#define UNLESS_ASSIGN(V,E) ASSIGN(V,E) UNLESS(V)
78
79#define DEL_LIST_SLICE(list, from, to) \
80 (PyList_SetSlice(list, from, to, empty_list))
81
82#define APPEND 'a'
83#define BUILD 'b'
84#define DUP '2'
85#define GET 'g'
86#define BINGET 'h'
87#define INST 'i'
88#define OBJ 'o'
89#define MARK '('
90static char MARKv = MARK;
91#define PUT 'p'
92#define BINPUT 'q'
93#define POP '0'
94#define SETITEM 's'
95#define STOP '.'
96#define CLASS 'c'
97#define DICT 'd'
98#define LIST 'l'
99#define TUPLE 't'
100#define NONE 'N'
101#define INT 'I'
102#define BININT 'J'
103#define BININT1 'K'
104#define BININT2 'M'
105#define BININT3 'O'
106#define LONG 'L'
107#define FLOAT 'F'
108#define STRING 'S'
109#define BINSTRING 'T'
110#define SHORT_BINSTRING 'U'
111#define PERSID 'P'
112
113
114PyTypeObject *BuiltinFunctionType;
115
116/* atol function from string module */
117static PyObject *atol_func;
118
119static PyObject *PicklingError;
120static PyObject *UnpicklingError;
121
122static PyObject *class_map;
123static PyObject *empty_list, *empty_tuple;
124
125static PyObject *save();
126
127
128typedef struct
129{
130 PyObject_HEAD
131 FILE *fp;
132 PyObject *write;
133 PyObject *file;
134 PyObject *memo;
135 PyObject *arg;
136 PyObject *pers_func;
137 char *mark;
138 int bin;
139 int (*write_func)();
140} Picklerobject;
141
142staticforward PyTypeObject Picklertype;
143
144
145typedef struct
146{
147 PyObject_HEAD
148 FILE *fp;
149 PyObject *file;
150 PyObject *readline;
151 PyObject *read;
152 PyObject *memo;
153 PyObject *arg;
154 PyObject *stack;
155 PyObject *mark;
156 PyObject *pers_func;
157 int *marks;
158 int num_marks;
159 int marks_size;
160 int (*read_func)();
161 int (*readline_func)();
162} Unpicklerobject;
163
164staticforward PyTypeObject Unpicklertype;
165
166
167static int
168write_file(ARG(Picklerobject *, self), ARG(char *, s), ARG(int, n))
169 ARGDECL(Picklerobject *, self)
170 ARGDECL(char *, s)
171 ARGDECL(int, n)
172{
173 if (fwrite(s, sizeof(char), n, self->fp) != n)
174 {
175 PyErr_SetFromErrno(PyExc_IOError);
176 return -1;
177 }
178
179 return n;
180}
181
182
183static int
184write_cStringIO(ARG(Picklerobject *, self), ARG(char *, s), ARG(int, n))
185 ARGDECL(Picklerobject *, self)
186 ARGDECL(char *, s)
187 ARGDECL(int, n)
188{
189 if ((*PycStringIO_cwrite)((PyObject *)self->file, s, n) != n)
190 {
191 return -1;
192 }
193
194 return n;
195}
196
197
198static int
199write_other(ARG(Picklerobject *, self), ARG(char *, s), ARG(int, n))
200 ARGDECL(Picklerobject *, self)
201 ARGDECL(char *, s)
202 ARGDECL(int, n)
203{
204 PyObject *py_str, *junk;
205
206 UNLESS(py_str = PyString_FromStringAndSize(s, n))
207 return -1;
208
209 UNLESS(self->arg)
210 UNLESS(self->arg = PyTuple_New(1))
211 {
212 Py_DECREF(py_str);
213 return -1;
214 }
215
216 if (PyTuple_SetItem(self->arg, 0, py_str) == -1)
217 {
218 Py_DECREF(py_str);
219 return -1;
220 }
221
222 Py_INCREF(py_str);
223 UNLESS(junk = PyObject_CallObject(self->write, self->arg))
224 {
225 Py_DECREF(py_str);
226 return -1;
227 }
228
229 Py_DECREF(junk);
230
231 return n;
232}
233
234
235static int
236read_file(ARG(Unpicklerobject *, self), ARG(char **, s), ARG(int, n))
237 ARGDECL(Unpicklerobject *, self)
238 ARGDECL(char **, s)
239 ARGDECL(int, n)
240{
241 if (fread(*s, sizeof(char), n, self->fp) != n)
242 {
243 if (feof(self->fp))
244 {
245 PyErr_SetNone(PyExc_EOFError);
246 return -1;
247 }
248
249 PyErr_SetFromErrno(PyExc_IOError);
250 return -1;
251 }
252
253 return n;
254}
255
256
257static int
258readline_file(ARG(Unpicklerobject *, self), ARG(char **, s))
259 ARGDECL(Unpicklerobject *, self)
260 ARGDECL(char **, s)
261{
262 int size, i;
263 char *str;
264
265 UNLESS(str = (char *)malloc(100))
266 {
267 PyErr_SetString(PyExc_MemoryError, "out of memory");
268 return -1;
269 }
270
271 size = 100;
272 i = 0;
273
274 while (1)
275 {
276 for (; i < size; i++)
277 {
278 if (feof(self->fp) || (str[i] = getc(self->fp)) == '\n')
279 {
280 str[i] = 0;
281 *s = str;
282 return i;
283 }
284 }
285
286 UNLESS(str = realloc(str, size += 100))
287 {
288 PyErr_SetString(PyExc_MemoryError, "out of memory");
289 return -1;
290 }
291 }
292}
293
294
295static int
296read_cStringIO(ARG(Unpicklerobject *, self), ARG(char **, s), ARG(int, n))
297 ARGDECL(Unpicklerobject *, self)
298 ARGDECL(char **, s)
299 ARGDECL(int, n)
300{
301 char *ptr;
302
303 if ((*PycStringIO_cread)((PyObject *)self->file, &ptr, n) != n)
304 {
305 PyErr_SetNone(PyExc_EOFError);
306 return -1;
307 }
308
309 memcpy(*s, ptr, n);
310 return n;
311}
312
313
314static int
315readline_cStringIO(ARG(Unpicklerobject *, self), ARG(char **, s))
316 ARGDECL(Unpicklerobject *, self)
317 ARGDECL(char **, s)
318{
319 int n;
320 char *ptr, *str;
321
322 if ((n = (*PycStringIO_creadline)((PyObject *)self->file, &ptr)) == -1)
323 {
324 return -1;
325 }
326
327 UNLESS(str = (char *)malloc(n * sizeof(char) + 1))
328 {
329 PyErr_SetString(PyExc_MemoryError, "out of memory");
330 return -1;
331 }
332
333 memcpy(str, ptr, n);
334
335 str[((str[n - 1] == '\n') ? --n : n)] = 0;
336
337 *s = str;
338 return n;
339}
340
341
342static int
343read_other(ARG(Unpicklerobject *, self), ARG(char **, s), ARG(int, n))
344 ARGDECL(Unpicklerobject *, self)
345 ARGDECL(char **, s)
346 ARGDECL(int, n)
347{
348 PyObject *bytes, *str;
349 char *ret_str;
350
351 UNLESS(bytes = PyInt_FromLong(n))
352 {
353 if (!PyErr_Occurred())
354 PyErr_SetNone(PyExc_EOFError);
355
356 return -1;
357 }
358
359 UNLESS(self->arg)
360 UNLESS(self->arg = PyTuple_New(1))
361 {
362 Py_DECREF(bytes);
363 return -1;
364 }
365
366 if (PyTuple_SetItem(self->arg, 0, bytes) == -1)
367 {
368 Py_DECREF(bytes);
369 return -1;
370 }
371 Py_INCREF(bytes);
372
373 UNLESS(str = PyObject_CallObject(self->read, self->arg))
374 {
375 Py_DECREF(bytes);
376 return -1;
377 }
378
379 memcpy(*s, PyString_AsString(str), n);
380
381 Py_DECREF(bytes);
382 Py_DECREF(str);
383
384 return n;
385}
386
387
388static int
389readline_other(ARG(Unpicklerobject *, self), ARG(char **, s))
390 ARGDECL(Unpicklerobject *, self)
391 ARGDECL(char **, s)
392{
393 PyObject *str;
394 char *c_str;
395 int size;
396
397 UNLESS(str = PyObject_CallObject(self->readline, empty_tuple))
398 {
399 return -1;
400 }
401
402 size = PyString_Size(str);
403
404 UNLESS(c_str = (char *)malloc((size + 1) * sizeof(char)))
405 {
406 PyErr_SetString(PyExc_MemoryError, "out of memory");
407 return -1;
408 }
409
410 memcpy(c_str, PyString_AsString(str), size);
411
412 if (size > 0)
413 {
414 c_str[((c_str[size - 1] == '\n') ? --size : size)] = 0;
415 }
416
417 *s = c_str;
418
419 Py_DECREF(str);
420
421 return size;
422}
423
424
425static int
426put(ARG(Picklerobject *, self), ARG(PyObject *, ob))
427 ARGDECL(Picklerobject *, self)
428 ARGDECL(PyObject *, ob)
429{
430 char c_str[30];
431 int p, len;
432 PyObject *py_ob_id = 0, *memo_len = 0;
433
434 if ((p = PyDict_Size(self->memo)) == -1)
435 return -1;
436
437 if (!self->bin || (p >= 256))
438 {
439 c_str[0] = PUT;
440 sprintf(c_str + 1, "%d\n", p);
441 len = strlen(c_str);
442 }
443 else
444 {
445 c_str[0] = BINPUT;
446 c_str[1] = p;
447 len = 2;
448 }
449
450 if ((*self->write_func)(self, c_str, len) == -1)
451 return -1;
452
453 UNLESS(py_ob_id = PyInt_FromLong((long)ob))
454 return -1;
455
456 UNLESS(memo_len = PyInt_FromLong(p))
457 goto err;
458
459 if (PyDict_SetItem(self->memo, py_ob_id, memo_len) == -1)
460 goto err;
461
462 Py_DECREF(py_ob_id);
463 Py_DECREF(memo_len);
464
465 return 1;
466
467err:
468 Py_XDECREF(py_ob_id);
469 Py_XDECREF(memo_len);
470
471 return -1;
472}
473
474
475static int
476safe(ARG(PyObject *, ob))
477 ARGDECL(PyObject *, ob)
478{
479 PyTypeObject *type;
480 PyObject *this_item;
481 int len, res, i;
482
483 type = ob->ob_type;
484
485 if (type == &PyInt_Type ||
486 type == &PyFloat_Type ||
487 type == &PyString_Type ||
488 ob == Py_None)
489 {
490 return 1;
491 }
492
493 if (type == &PyTuple_Type)
494 {
495 len = PyTuple_Size(ob);
496 for (i = 0; i < len; i++)
497 {
498 UNLESS(this_item = PyTuple_GET_ITEM((PyTupleObject *)ob, i))
499 return -1;
500
501 if ((res = safe(this_item)) == 1)
502 continue;
503
504 return res;
505 }
506
507 return 1;
508 }
509
510 return 0;
511}
512
513
514static PyObject *
515whichmodule(ARG(PyObject *, class))
516 ARGDECL(PyObject *, class)
517{
518 int has_key, i, j;
519 PyObject *module = 0, *modules_dict = 0, *class_name = 0, *class_name_attr = 0,
520 *name = 0;
521 char *name_str, *class_name_str;
522
523 static PyObject *__main__str;
524
525 if ((has_key = PyMapping_HasKey(class_map, class)) == -1)
526 return NULL;
527
528 if (has_key)
529 {
530 return ((module = PyDict_GetItem(class_map, class)) ? module : NULL);
531 }
532
533 UNLESS(modules_dict = PySys_GetObject("modules"))
534 return NULL;
535
536 UNLESS(class_name = ((PyClassObject *)class)->cl_name)
537 {
538 PyErr_SetString(PicklingError, "class has no name");
539 return NULL;
540 }
541
542 UNLESS(class_name_str = PyString_AsString(class_name))
543 return NULL;
544
545 i = 0;
546 while (j = PyDict_Next(modules_dict, &i, &name, &module))
547 {
548 UNLESS(name_str = PyString_AsString(name))
549 return NULL;
550
551 if (!strcmp(name_str, "__main__"))
552 continue;
553
554 UNLESS(class_name_attr = PyObject_GetAttr(module, class_name))
555 {
556 PyErr_Clear();
557 continue;
558 }
559
560 if (class_name_attr != class)
561 {
562 Py_DECREF(class_name_attr);
563 continue;
564 }
565
566 Py_DECREF(class_name_attr);
567
568 break;
569 }
570
571 if (!j) /* previous while exited normally */
572 {
573 UNLESS(__main__str)
574 UNLESS(__main__str = PyString_FromString("__main__"))
575 return NULL;
576
577 name = __main__str;
578 }
579 else /* previous while exited via break */
580 {
581 Py_INCREF(name);
582 }
583
584 PyDict_SetItem(class_map, class, name);
585
586 return name;
587}
588
589
590static PyObject *
591save_none(ARG(Picklerobject *, self), ARG(PyObject *, args))
592 ARGDECL(Picklerobject *, self)
593 ARGDECL(PyObject *, args)
594{
595 static char none[] = { NONE };
596
597 if ((*self->write_func)(self, none, 1) == -1)
598 return NULL;
599
600 Py_INCREF(Py_None);
601 return Py_None;
602}
603
604
605static PyObject *
606save_int(ARG(Picklerobject *, self), ARG(PyObject *, args))
607 ARGDECL(Picklerobject *, self)
608 ARGDECL(PyObject *, args)
609{
610 char c_str[25];
611 long l = PyInt_AS_LONG((PyIntObject *)args);
612 int len;
613
614 if (!self->bin)
615 {
616 c_str[0] = INT;
617 sprintf(c_str + 1, "%ld\n", l);
618 if ((*self->write_func)(self, c_str, strlen(c_str)) == -1)
619 return NULL;
620 }
621 else
622 {
623 c_str[1] = (int)(l & 0xff);
624 c_str[2] = (int)((l >> 8) & 0xff);
625 c_str[3] = (int)((l >> 16) & 0xff);
626 c_str[4] = (int)((l >> 24) & 0xff);
627
628 if (!c_str[4])
629 {
630 if (!c_str[3])
631 {
632 if (!c_str[2])
633 {
634 c_str[0] = BININT3;
635 len = 2;
636 }
637 else
638 {
639 c_str[0] = BININT2;
640 len = 3;
641 }
642 }
643 else
644 {
645 c_str[0] = BININT1;
646 len = 4;
647 }
648 }
649 else
650 {
651 c_str[0] = BININT;
652 len = 5;
653 }
654
655 if ((*self->write_func)(self, c_str, len) == -1)
656 return NULL;
657 }
658
659 Py_INCREF(Py_None);
660 return Py_None;
661}
662
663
664static PyObject *
665save_long(ARG(Picklerobject *, self), ARG(PyObject *, args))
666 ARGDECL(Picklerobject *, self)
667 ARGDECL(PyObject *, args)
668{
669 char *c_str;
670 int size;
671 PyObject *repr = 0;
672
673 UNLESS(repr = PyObject_Repr(args))
674 return NULL;
675
676 if ((size = PyString_Size(repr)) == -1)
677 {
678 Py_DECREF(repr);
679 return NULL;
680 }
681
682 UNLESS(c_str = (char *)malloc((size + 2) * sizeof(char)))
683 {
684 Py_DECREF(repr);
685 PyErr_SetString(PyExc_MemoryError, "out of memory");
686 return NULL;
687 }
688
689 c_str[0] = LONG;
690 memcpy(c_str + 1, PyString_AS_STRING((PyStringObject *)repr), size);
691 c_str[size + 1] = '\n';
692
693 Py_DECREF(repr);
694
695 if ((*self->write_func)(self, c_str, size + 2) == -1)
696 {
697 free(c_str);
698 return NULL;
699 }
700
701 free(c_str);
702
703 Py_INCREF(Py_None);
704 return Py_None;
705}
706
707
708static PyObject *
709save_float(ARG(Picklerobject *, self), ARG(PyObject *, args))
710 ARGDECL(Picklerobject *, self)
711 ARGDECL(PyObject *, args)
712{
713 char c_str[250];
714
715 c_str[0] = FLOAT;
716 sprintf(c_str + 1, "%f\n", PyFloat_AS_DOUBLE((PyFloatObject *)args));
717
718 if ((*self->write_func)(self, c_str, strlen(c_str)) == -1)
719 return NULL;
720
721 Py_INCREF(Py_None);
722 return Py_None;
723}
724
725
726static PyObject *
727save_string(ARG(Picklerobject *, self), ARG(PyObject *, args))
728 ARGDECL(Picklerobject *, self)
729 ARGDECL(PyObject *, args)
730{
731 char *c_str;
732 int size, len;
733
734 if (!self->bin)
735 {
736 PyObject *repr;
737 char *repr_str;
738
739 UNLESS(repr = PyObject_Repr(args))
740 return NULL;
741
742 repr_str = PyString_AS_STRING((PyStringObject *)repr);
743 size = PyString_Size(repr);
744
745 UNLESS(c_str = (char *)malloc((size + 2) * sizeof(char)))
746 {
747 PyErr_SetString(PyExc_MemoryError, "out of memory");
748 return NULL;
749 }
750
751 c_str[0] = STRING;
752 memcpy(c_str + 1, repr_str, size);
753 c_str[size + 1] = '\n';
754
755 len = size + 2;
756
757 Py_XDECREF(repr);
758 }
759 else
760 {
761 size = PyString_Size(args);
762
763 UNLESS(c_str = (char *)malloc((size + 30) * sizeof(char)))
764 {
765 PyErr_SetString(PyExc_MemoryError, "out of memory");
766 return NULL;
767 }
768
769 if (size < 256)
770 {
771 c_str[0] = SHORT_BINSTRING;
772 c_str[1] = size;
773 len = 2;
774 }
775 else
776 {
777 c_str[0] = BINSTRING;
778 sprintf(c_str + 1, "%d\n", size);
779 len = strlen(c_str);
780 }
781
782 memcpy(c_str + len, PyString_AS_STRING((PyStringObject *)args), size);
783
784 len += size;
785 }
786
787 if ((*self->write_func)(self, c_str, len) == -1)
788 {
789 free(c_str);
790 return NULL;
791 }
792
793 free(c_str);
794
795 if (args->ob_refcnt > 1)
796 {
797 if (put(self, args) == -1)
798 {
799 return NULL;
800 }
801 }
802
803 Py_INCREF(Py_None);
804 return Py_None;
805}
806
807
808static PyObject *
809save_tuple(ARG(Picklerobject *, self), ARG(PyObject *, args))
810 ARGDECL(Picklerobject *, self)
811 ARGDECL(PyObject *, args)
812{
813 PyObject *element = 0, *junk = 0, *py_tuple_id = 0;
814 int len, i, dict_size;
815
816 static char tuple = TUPLE;
817
818 if ((*self->write_func)(self, &MARKv, 1) == -1)
819 return NULL;
820
821 UNLESS(py_tuple_id = PyInt_FromLong((long)args)) return NULL;
822
823 if ((len = PyTuple_Size(args)) == -1)
824 goto err;
825
826 for (i = 0; i < len; i++)
827 {
828 UNLESS(element = PyTuple_GET_ITEM((PyTupleObject *)args, i))
829 goto err;
830
831 dict_size = PyDict_Size(self->memo);
832
833 UNLESS(junk = save(self, element))
834 goto err;
835 Py_DECREF(junk);
836
837 if (((PyDict_Size(self->memo) - dict_size) > 1) &&
838 PyMapping_HasKey(self->memo, py_tuple_id))
839 {
840 char c_str[30];
841 long c_value;
842 int c_str_len;
843 PyObject *value;
844 static char pop = POP;
845
846 while (i-- >= 0)
847 {
848 if ((*self->write_func)(self, &pop, 1) == -1)
849 goto err;
850 }
851
852 UNLESS(value = PyDict_GetItem(self->memo, py_tuple_id))
853 goto err;
854
855 c_value = PyInt_AsLong(value);
856
857 if (self->bin && (c_value < 256))
858 {
859 c_str[0] = BINGET;
860 c_str[1] = c_value;
861 c_str_len = 2;
862 }
863 else
864 {
865 c_str[0] = GET;
866 sprintf(c_str + 1, "%ld\n", c_value);
867 c_str_len = strlen(c_str);
868 }
869
870 if ((*self->write_func)(self, c_str, c_str_len) == -1)
871 goto err;
872
873 break;
874 }
875 }
876
877 if (i >= len)
878 {
879 if ((*self->write_func)(self, &tuple, 1) == -1)
880 goto err;
881
882 if (args->ob_refcnt > 1)
883 {
884 if (put(self, args) == -1)
885 {
886 goto err;
887 }
888 }
889 }
890
891 Py_DECREF(py_tuple_id);
892
893 Py_INCREF(Py_None);
894 return Py_None;
895
896err:
897 Py_XDECREF(py_tuple_id);
898
899 return NULL;
900}
901
902
903static PyObject *
904save_list(ARG(Picklerobject *, self), ARG(PyObject *, args))
905 ARGDECL(Picklerobject *, self)
906 ARGDECL(PyObject *, args)
907{
908 PyObject *element = 0, *junk = 0;
909 int len, i, safe_val;
910 static char append = APPEND, list = LIST;
911
912 if ((*self->write_func)(self, &MARKv, 1) == -1)
913 return NULL;
914
915 if ((len = PyList_Size(args)) == -1)
916 return NULL;
917
918 for (i = 0; i < len; i++)
919 {
920 UNLESS(element = PyList_GET_ITEM((PyListObject *)args, i))
921 return NULL;
922
923 if ((safe_val = safe(element)) == -1)
924 return NULL;
925 UNLESS(safe_val)
926 break;
927
928 UNLESS(junk = save(self, element))
929 return NULL;
930 Py_DECREF(junk);
931 }
932
933 if (args->ob_refcnt > 1)
934 {
935 if (put(self, args) == -1)
936 {
937 return NULL;
938 }
939 }
940
941 if ((*self->write_func)(self, &list, 1) == -1)
942 return NULL;
943
944 for (; i < len; i++)
945 {
946 UNLESS(element = PyList_GET_ITEM((PyListObject *)args, i))
947 return NULL;
948
949 UNLESS(junk = save(self, element))
950 return NULL;
951 Py_DECREF(junk);
952
953 if ((*self->write_func)(self, &append, 1) == -1)
954 return NULL;
955 }
956
957 Py_INCREF(Py_None);
958 return Py_None;
959}
960
961
962static PyObject *
963save_dict(ARG(Picklerobject *, self), ARG(PyObject *, args))
964 ARGDECL(Picklerobject *, self)
965 ARGDECL(PyObject *, args)
966{
967 PyObject *key = 0, *value = 0, *junk = 0;
968 int i, safe_key, safe_value;
969
970 static char setitem = SETITEM, dict = DICT;
971
972 if ((*self->write_func)(self, &MARKv, 1) == -1)
973 return NULL;
974
975 i = 0;
976
977 while (PyDict_Next(args, &i, &key, &value))
978 {
979 if ((safe_key = safe(key)) == -1)
980 return NULL;
981
982 UNLESS(safe_key)
983 break;
984
985 if ((safe_value = safe(value)) == -1)
986 return NULL;
987
988 UNLESS(safe_value)
989 break;
990
991 UNLESS(junk = save(self, key))
992 return NULL;
993 Py_DECREF(junk);
994
995 UNLESS(junk = save(self, value))
996 return NULL;
997 Py_DECREF(junk);
998 }
999
1000 if ((*self->write_func)(self, &dict, 1) == -1)
1001 return NULL;
1002
1003 if (args->ob_refcnt > 1)
1004 {
1005 if (put(self, args) == -1)
1006 {
1007 return NULL;
1008 }
1009 }
1010
1011 while (PyDict_Next(args, &i, &key, &value))
1012 {
1013 UNLESS(junk = save(self, key))
1014 return NULL;
1015 Py_DECREF(junk);
1016
1017 UNLESS(junk = save(self, value))
1018 return NULL;
1019 Py_DECREF(junk);
1020
1021 if ((*self->write_func)(self, &setitem, 1) == -1)
1022 return NULL;
1023 }
1024
1025 Py_INCREF(Py_None);
1026 return Py_None;
1027}
1028
1029
1030static PyObject *
1031save_inst(ARG(Picklerobject *, self), ARG(PyObject *, args))
1032 ARGDECL(Picklerobject *, self)
1033 ARGDECL(PyObject *, args)
1034{
1035 PyObject *class = 0, *module = 0, *name = 0, *py_inst_id = 0, *init_args = 0,
1036 *junk = 0, *state = 0, *getinitargs_func = 0, *getstate_func = 0;
1037 char *module_str, *name_str, *c_str;
1038 int len, p, module_size, name_size, size;
1039 static char build = BUILD;
1040
1041 if ((*self->write_func)(self, &MARKv, 1) == -1)
1042 return NULL;
1043
1044 UNLESS(class = PyObject_GetAttrString(args, "__class__"))
1045 return NULL;
1046
1047 if (self->bin)
1048 {
1049 UNLESS(junk = save(self, class))
1050 goto err;
1051 Py_DECREF(junk);
1052 }
1053
1054 if (getinitargs_func = PyObject_GetAttrString(args, "__getinitargs__"))
1055 {
1056 PyObject *class_args = 0, *element = 0;
1057 int i, len;
1058
1059 UNLESS(class_args = PyObject_CallObject(getinitargs_func, empty_tuple))
1060 {
1061 Py_DECREF(getinitargs_func);
1062 goto err;
1063 }
1064
1065 if ((len = PyObject_Length(class_args)) == -1)
1066 {
1067 Py_DECREF(class_args);
1068 goto err;
1069 }
1070
1071 for (i = 0; i < len; i++)
1072 {
1073 UNLESS(element = PySequence_GetItem(class_args, i))
1074 {
1075 Py_DECREF(class_args);
1076 goto err;
1077 }
1078
1079 UNLESS(junk = save(self, element))
1080 {
1081 Py_DECREF(element); Py_DECREF(class_args);
1082 goto err;
1083 }
1084
1085 Py_DECREF(junk);
1086 Py_DECREF(element);
1087 }
1088
1089 Py_DECREF(class_args);
1090 }
1091 else
1092 {
1093 PyErr_Clear();
1094 }
1095
1096 if (!self->bin)
1097 {
1098 UNLESS(module = whichmodule(class))
1099 goto err;
1100
1101 UNLESS(name = ((PyClassObject *)class)->cl_name)
1102 {
1103 PyErr_SetString(PicklingError, "class has no name");
1104 goto err;
1105 }
1106
1107 module_str = PyString_AS_STRING((PyStringObject *)module);
1108 module_size = PyString_Size(module);
1109 name_str = PyString_AS_STRING((PyStringObject *)name);
1110 name_size = PyString_Size(name);
1111
1112 size = name_size + module_size + 3;
1113
1114 UNLESS(c_str = (char *)malloc(size * sizeof(char)))
1115 {
1116 PyErr_SetString(PyExc_MemoryError, "out of memory");
1117 return NULL;
1118 }
1119
1120 c_str[0] = INST;
1121 memcpy(c_str + 1, module_str, module_size);
1122 c_str[module_size + 1] = '\n';
1123 memcpy(c_str + module_size + 2, name_str, name_size);
1124 c_str[module_size + name_size + 2] = '\n';
1125
1126 if ((*self->write_func)(self, c_str, size) == -1)
1127 {
1128 free(c_str);
1129 goto err;
1130 }
1131
1132 free(c_str);
1133 }
1134
1135 if (args->ob_refcnt > 1)
1136 {
1137 if (put(self, args) == -1)
1138 {
1139 goto err;
1140 }
1141 }
1142
1143 if (getstate_func = PyObject_GetAttrString(args, "__getstate__"))
1144 {
1145 UNLESS(state = PyObject_CallObject(getstate_func, empty_tuple))
1146 {
1147 Py_DECREF(getstate_func);
1148 goto err;
1149 }
1150 }
1151 else
1152 {
1153 PyErr_Clear();
1154
1155 UNLESS(state = PyObject_GetAttrString(args, "__dict__"))
1156 goto err;
1157 }
1158
1159 UNLESS(junk = save(self, state))
1160 goto err;
1161 Py_DECREF(junk);
1162
1163 Py_XDECREF(getinitargs_func);
1164 Py_XDECREF(getstate_func);
1165 Py_XDECREF(module);
1166 Py_XDECREF(state);
1167
1168 if ((*self->write_func)(self, &build, 1) == -1)
1169 return NULL;
1170
1171 Py_INCREF(Py_None);
1172 return Py_None;
1173
1174err:
1175 Py_XDECREF(getinitargs_func);
1176 Py_XDECREF(getstate_func);
1177 Py_XDECREF(module);
1178 Py_XDECREF(state);
1179
1180 return NULL;
1181}
1182
1183
1184static PyObject *
1185save_class(ARG(Picklerobject *, self), ARG(PyObject *, args))
1186 ARGDECL(Picklerobject *, self)
1187 ARGDECL(PyObject *, args)
1188{
1189 PyObject *module = 0, *name = 0;
1190 char *name_str, *module_str, *c_str;
1191 int module_size, name_size, size;
1192
1193 UNLESS(module = whichmodule(args))
1194 return NULL;
1195
1196 UNLESS(name = ((PyClassObject *)args)->cl_name)
1197 {
1198 PyErr_SetString(PicklingError, "class has no name");
1199 goto err;
1200 }
1201
1202 module_str = PyString_AS_STRING((PyStringObject *)module);
1203 module_size = PyString_Size(module);
1204 name_str = PyString_AS_STRING((PyStringObject *)name);
1205 name_size = PyString_Size(name);
1206
1207 size = name_size + module_size + 3;
1208
1209 UNLESS(c_str = (char *)malloc(size * sizeof(char)))
1210 {
1211 PyErr_SetString(PyExc_MemoryError, "out of memory");
1212 return NULL;
1213 }
1214
1215 c_str[0] = CLASS;
1216 memcpy(c_str + 1, module_str, module_size);
1217 c_str[module_size + 1] = '\n';
1218 memcpy(c_str + module_size + 2, name_str, name_size);
1219 c_str[module_size + name_size + 2] = '\n';
1220
1221 if ((*self->write_func)(self, c_str, size) == -1)
1222 {
1223 free(c_str);
1224 goto err;
1225 }
1226
1227 free(c_str);
1228
1229 if (args->ob_refcnt > 1)
1230 {
1231 if (put(self, args) == -1)
1232 {
1233 goto err;
1234 }
1235 }
1236
1237 Py_DECREF(module);
1238
1239 Py_INCREF(Py_None);
1240 return Py_None;
1241
1242err:
1243 Py_XDECREF(module);
1244
1245 return NULL;
1246}
1247
1248
1249static PyObject *
1250save(ARG(Picklerobject *, self), ARG(PyObject *, args))
1251 ARGDECL(Picklerobject *, self)
1252 ARGDECL(PyObject *, args)
1253{
1254 PyTypeObject *type;
1255 char *error_str, *name_c;
1256 PyObject *name, *name_repr;
1257
1258 if (self->pers_func)
1259 {
1260 PyObject *pid;
1261 int size;
1262
1263 UNLESS(self->arg)
1264 UNLESS(self->arg = PyTuple_New(1))
1265 return NULL;
1266
1267 if (PyTuple_SetItem(self->arg, 0, args) == -1)
1268 return NULL;
1269 Py_INCREF(args);
1270
1271 UNLESS(pid = PyObject_CallObject(self->pers_func, self->arg))
1272 return NULL;
1273
1274 if (pid != Py_None)
1275 {
1276 char *pid_str;
1277
1278 if ((size = PyString_Size(pid)) == -1)
1279 {
1280 Py_DECREF(pid);
1281 return NULL;
1282 }
1283
1284 UNLESS(pid_str = (char *)malloc((2 + size) * sizeof(char)))
1285 {
1286 PyErr_SetString(PyExc_MemoryError, "out of memory");
1287 Py_DECREF(pid);
1288 return NULL;
1289 }
1290
1291 pid_str[0] = PERSID;
1292 memcpy(pid_str + 1, PyString_AS_STRING((PyStringObject *)pid), size);
1293 pid_str[size + 1] = '\n';
1294
1295 Py_DECREF(pid);
1296
1297 if ((*self->write_func)(self, pid_str, size + 2) == -1)
1298 {
1299 free(pid_str);
1300 return NULL;
1301 }
1302
1303 free(pid_str);
1304
1305 Py_INCREF(Py_None);
1306 return Py_None;
1307 }
1308
1309 Py_DECREF(pid);
1310 }
1311
1312 if (args == Py_None)
1313 {
1314 return save_none(self, args);
1315 }
1316
1317 type = args->ob_type;
1318
1319 if (type == &PyInt_Type)
1320 {
1321 return save_int(self, args);
1322 }
1323
1324 if (type == &PyLong_Type)
1325 {
1326 return save_long(self, args);
1327 }
1328
1329 if (type == &PyFloat_Type)
1330 {
1331 return save_float(self, args);
1332 }
1333
1334 if (args->ob_refcnt > 1)
1335 {
1336 long ob_id;
1337 int has_key;
1338 PyObject *py_ob_id;
1339
1340 ob_id = (long)args;
1341
1342 UNLESS(py_ob_id = PyInt_FromLong(ob_id))
1343 return NULL;
1344
1345 if ((has_key = PyMapping_HasKey(self->memo, py_ob_id)) == -1)
1346 {
1347 Py_DECREF(py_ob_id);
1348 return NULL;
1349 }
1350
1351 if (has_key)
1352 {
1353 PyObject *value;
1354 long c_value;
1355 char get_str[30];
1356 int len;
1357
1358 UNLESS(value = PyDict_GetItem(self->memo, py_ob_id))
1359 {
1360 Py_DECREF(py_ob_id);
1361 return NULL;
1362 }
1363
1364 Py_DECREF(py_ob_id);
1365
1366 c_value = PyInt_AsLong(value);
1367
1368 if (self->bin && (c_value < 256))
1369 {
1370 get_str[0] = BINGET;
1371 get_str[1] = c_value;
1372 len = 2;
1373 }
1374 else
1375 {
1376 get_str[0] = GET;
1377 sprintf(get_str + 1, "%ld\n", c_value);
1378 len = strlen(get_str);
1379 }
1380
1381 if ((*self->write_func)(self, get_str, len) == -1)
1382 return NULL;
1383
1384 Py_INCREF(Py_None);
1385 return Py_None;
1386 }
1387
1388 Py_DECREF(py_ob_id);
1389 }
1390
1391 if (type == &PyString_Type)
1392 {
1393 return save_string(self, args);
1394 }
1395
1396 if (type == &PyTuple_Type)
1397 {
1398 return save_tuple(self, args);
1399 }
1400
1401 if (type == &PyList_Type)
1402 {
1403 return save_list(self, args);
1404 }
1405
1406 if (type == &PyDict_Type)
1407 {
1408 return save_dict(self, args);
1409 }
1410
1411 if (type == &PyInstance_Type)
1412 {
1413 return save_inst(self, args);
1414 }
1415
1416 if (type == &PyClass_Type)
1417 {
1418 return save_class(self, args);
1419 }
1420
1421 if (PyObject_HasAttrString(args, "__class__"))
1422 {
1423 return save_inst(self, args);
1424 }
1425
1426 UNLESS(name = PyObject_GetAttrString((PyObject *)type, "__name__"))
1427 return NULL;
1428
1429 UNLESS(name_repr = PyObject_Repr(name))
1430 {
1431 Py_DECREF(name);
1432 }
1433
1434 name_c = PyString_AsString(name_repr);
1435
1436 UNLESS(error_str = (char *)malloc((strlen(name_c) + 25) * sizeof(char)))
1437 {
1438 PyErr_SetString(PyExc_MemoryError, "out of memory");
1439 Py_DECREF(name);
1440 Py_DECREF(name_repr);
1441 return NULL;
1442 }
1443
1444 sprintf(error_str, "Cannot pickle %s objects.", name_c);
1445
1446 Py_DECREF(name);
1447 Py_DECREF(name_repr);
1448
1449 PyErr_SetString(PicklingError, error_str);
1450
1451 free(error_str);
1452
1453 return NULL;
1454}
1455
1456
1457static PyObject *
1458Pickler_dump(ARG(Picklerobject *, self), ARG(PyObject *, args))
1459 ARGDECL(Picklerobject *, self)
1460 ARGDECL(PyObject *, args)
1461{
1462 PyObject *junk;
1463 static char stop = STOP;
1464
1465 UNLESS(PyArg_Parse(args, "O", &args)) return NULL;
1466
1467 UNLESS(junk = save(self, args)) return NULL;
1468 Py_DECREF(junk);
1469
1470 if ((*self->write_func)(self, &stop, 1) == -1)
1471 return NULL;
1472
1473 Py_INCREF(Py_None);
1474 return Py_None;
1475}
1476
1477
1478static PyObject *
1479write(ARG(Picklerobject *, self), ARG(PyObject *, args))
1480 ARGDECL(Picklerobject *, self)
1481 ARGDECL(PyObject *, args)
1482{
1483 char *ptr;
1484 int size;
1485
1486 UNLESS(ptr = PyString_AsString(args))
1487 return NULL;
1488
1489 if ((size = PyString_Size(args)) == -1)
1490 return NULL;
1491
1492 if ((*self->write_func)(self, ptr, size) == -1)
1493 return NULL;
1494
1495 Py_INCREF(Py_None);
1496 return Py_None;
1497}
1498
1499
1500static struct PyMethodDef Pickler_methods[] = {
1501 {"save", (PyCFunction)save, 0, ""},
1502 {"dump", (PyCFunction)Pickler_dump, 0, ""},
1503 {"save_none", (PyCFunction)save_none, 0, ""},
1504 {"save_int", (PyCFunction)save_int, 0, ""},
1505 {"save_long", (PyCFunction)save_long, 0, ""},
1506 {"save_float", (PyCFunction)save_float, 0, ""},
1507 {"save_string", (PyCFunction)save_string, 0, ""},
1508 {"save_tuple", (PyCFunction)save_tuple, 0, ""},
1509 {"save_list", (PyCFunction)save_list, 0, ""},
1510 {"save_dict", (PyCFunction)save_dict, 0, ""},
1511 {"save_inst", (PyCFunction)save_inst, 0, ""},
1512 {"save_class", (PyCFunction)save_class, 0, ""},
1513 {"write", (PyCFunction)write, 0, ""},
1514 {NULL, NULL} /* sentinel */
1515};
1516
1517
1518static Picklerobject *
1519newPicklerobject(ARG(PyObject *, file), ARG(int, bin))
1520 ARGDECL(PyObject *, file)
1521 ARGDECL(int, bin)
1522{
1523 Picklerobject *self;
1524 PyObject *memo = 0;
1525
1526 UNLESS(memo = PyDict_New()) goto err;
1527
1528 UNLESS(self = PyObject_NEW(Picklerobject, &Picklertype))
1529 goto err;
1530
1531 if (PyFile_Check(file))
1532 {
1533 self->fp = PyFile_AsFile(file);
1534 self->write_func = write_file;
1535 self->write = NULL;
1536 }
1537 else if (PycStringIO_OutputCheck(file))
1538 {
1539 self->fp = NULL;
1540 self->write_func = write_cStringIO;
1541 self->write = NULL;
1542 }
1543 else
1544 {
1545 PyObject *write;
1546
1547 self->fp = NULL;
1548 self->write_func = write_other;
1549
1550 UNLESS(write = PyObject_GetAttrString(file, "write"))
1551 goto err;
1552
1553 self->write = write;
1554 }
1555
1556 Py_INCREF(file);
1557
1558 self->file = file;
1559 self->bin = bin;
1560 self->memo = memo;
1561 self->arg = NULL;
1562 self->pers_func = NULL;
1563
1564 return self;
1565
1566err:
1567 Py_XDECREF((PyObject *)self);
1568 Py_XDECREF(memo);
1569 return NULL;
1570}
1571
1572
1573static PyObject *
1574get_Pickler(ARG(PyObject *, self), ARG(PyObject *, args))
1575 ARGDECL(PyObject *, self)
1576 ARGDECL(PyObject *, args)
1577{
1578 PyObject *file;
1579 int bin = 0;
1580
1581 UNLESS(PyArg_ParseTuple(args, "O|i", &file, &bin)) return NULL;
1582 return (PyObject *)newPicklerobject(file, bin);
1583}
1584
1585
1586static void
1587Pickler_dealloc(ARG(Picklerobject *, self))
1588 ARGDECL(Picklerobject *, self)
1589{
1590 Py_XDECREF(self->write);
1591 Py_XDECREF(self->memo);
1592 Py_XDECREF(self->arg);
1593 Py_XDECREF(self->file);
1594 Py_XDECREF(self->pers_func);
1595 PyMem_DEL(self);
1596}
1597
1598
1599static PyObject *
1600Pickler_getattr(ARG(Picklerobject *, self), ARG(char *, name))
1601 ARGDECL(Picklerobject *, self)
1602 ARGDECL(char *, name)
1603{
1604 if (!strcmp(name, "persistent_id"))
1605 {
1606 if (!self->pers_func)
1607 {
1608 PyErr_SetString(PyExc_NameError, name);
1609 return NULL;
1610 }
1611
1612 Py_INCREF(self->pers_func);
1613 return self->pers_func;
1614 }
1615
1616 if (!strcmp(name, "memo"))
1617 {
1618 if (!self->memo)
1619 {
1620 PyErr_SetString(PyExc_NameError, name);
1621 return NULL;
1622 }
1623
1624 Py_INCREF(self->memo);
1625 return self->memo;
1626 }
1627
1628 if (!strcmp(name, "PicklingError"))
1629 {
1630 Py_INCREF(PicklingError);
1631 return PicklingError;
1632 }
1633
1634 return Py_FindMethod(Pickler_methods, (PyObject *)self, name);
1635}
1636
1637
1638int
1639Pickler_setattr(ARG(Picklerobject *, self), ARG(char *, name), ARG(PyObject *, value))
1640 ARGDECL(Picklerobject *, self)
1641 ARGDECL(char *, name)
1642 ARGDECL(PyObject *, value)
1643{
1644 if (!strcmp(name, "persistent_id"))
1645 {
1646 Py_XDECREF(self->pers_func);
1647 self->pers_func = value;
1648 Py_INCREF(value);
1649 return 0;
1650 }
1651
1652 return -1;
1653}
1654
1655
1656static char Picklertype__doc__[] = "";
1657
1658static PyTypeObject Picklertype = {
1659 PyObject_HEAD_INIT(&PyType_Type)
1660 0, /*ob_size*/
1661 "Pickler", /*tp_name*/
1662 sizeof(Picklerobject), /*tp_basicsize*/
1663 0, /*tp_itemsize*/
1664 /* methods */
1665 (destructor)Pickler_dealloc, /*tp_dealloc*/
1666 (printfunc)0, /*tp_print*/
1667 (getattrfunc)Pickler_getattr, /*tp_getattr*/
1668 (setattrfunc)Pickler_setattr, /*tp_setattr*/
1669 (cmpfunc)0, /*tp_compare*/
1670 (reprfunc)0, /*tp_repr*/
1671 0, /*tp_as_number*/
1672 0, /*tp_as_sequence*/
1673 0, /*tp_as_mapping*/
1674 (hashfunc)0, /*tp_hash*/
1675 (ternaryfunc)0, /*tp_call*/
1676 (reprfunc)0, /*tp_str*/
1677
1678 /* Space for future expansion */
1679 0L,0L,0L,0L,
1680 Picklertype__doc__ /* Documentation string */
1681};
1682
1683
1684static PyObject *
1685find_class(ARG(char *, module_name), ARG(char *, class_name))
1686 ARGDECL(char *, module_name)
1687 ARGDECL(char *, class_name)
1688{
1689 PyObject *import = 0, *class = 0, *py_module_name = 0, *py_class_name = 0,
1690 *t = 0;
1691 char *error_str;
1692 int has_key;
1693
1694 UNLESS(py_module_name = PyString_FromString(module_name))
1695 return NULL;
1696
1697 UNLESS(py_class_name = PyString_FromString(class_name))
1698 goto err;
1699
1700 UNLESS(t = PyTuple_New(2))
1701 goto err;
1702
1703 UNLESS(PyTuple_SET_ITEM((PyTupleObject *)t, 0, py_module_name))
1704 goto err;
1705 Py_INCREF(py_module_name);
1706
1707 UNLESS(PyTuple_SET_ITEM((PyTupleObject *)t, 1, py_class_name))
1708 goto err;
1709 Py_INCREF(py_module_name);
1710
1711 if ((has_key = PyMapping_HasKey(class_map, t)) == -1)
1712 goto err;
1713
1714 if (has_key)
1715 {
1716 UNLESS(class = PyDict_GetItem(class_map, t))
1717 goto err;
1718
1719 Py_INCREF(class);
1720
1721 Py_DECREF(py_module_name);
1722 Py_DECREF(py_class_name);
1723 Py_DECREF(t);
1724
1725 return class;
1726 }
1727
1728 if (!(import = PyImport_ImportModule(module_name)) ||
1729 !(class = PyObject_GetAttrString(import, class_name)))
1730 {
1731 UNLESS(error_str = (char *)malloc((strlen(module_name) +
1732 strlen(class_name) + 40) * sizeof(char)))
1733 {
1734 PyErr_SetString(PyExc_MemoryError, "out of memory");
1735 goto err;
1736 }
1737
1738 sprintf(error_str, "Failed to import class %s from module %s",
1739 class_name, module_name);
1740
1741 PyErr_SetString(PyExc_SystemError, error_str);
1742
1743 free(error_str);
1744 goto err;
1745 }
1746
1747 if (class->ob_type == BuiltinFunctionType)
1748 {
1749 UNLESS(error_str = (char *)malloc((strlen(module_name) +
1750 strlen(class_name) + 45) * sizeof(char)))
1751 {
1752 PyErr_SetString(PyExc_MemoryError, "out of memory");
1753 goto err;
1754 }
1755
1756 sprintf(error_str, "Imported object %s from module %s is not a class",
1757 class_name, module_name);
1758
1759 PyErr_SetString(PyExc_SystemError, error_str);
1760
1761 free(error_str);
1762 goto err;
1763 }
1764
1765 if (PyDict_SetItem(class_map, t, class) == -1)
1766 goto err;
1767
1768 Py_DECREF(t);
1769 Py_DECREF(py_module_name);
1770 Py_DECREF(py_class_name);
1771 Py_DECREF(import);
1772
1773 return class;
1774
1775err:
1776 Py_XDECREF(import);
1777 Py_XDECREF(class);
1778 Py_XDECREF(t);
1779 Py_XDECREF(py_module_name);
1780 Py_XDECREF(py_class_name);
1781
1782 return NULL;
1783}
1784
1785
1786int
1787marker(ARG(Unpicklerobject *, self))
1788 ARGDECL(Unpicklerobject *, self)
1789{
1790 if (!self->num_marks)
1791 return -1;
1792
1793 return self->marks[--self->num_marks];
1794}
1795
1796
1797static PyObject *
1798load_none(ARG(Unpicklerobject *, self), ARG(PyObject *, args))
1799 ARGDECL(Unpicklerobject *, self)
1800 ARGDECL(PyObject *, args)
1801{
1802 if (PyList_Append(self->stack, Py_None) == -1)
1803 return NULL;
1804
1805 Py_INCREF(Py_None);
1806 return Py_None;
1807}
1808
1809
1810static PyObject *
1811load_int(ARG(Unpicklerobject *, self), ARG(PyObject *, args))
1812 ARGDECL(Unpicklerobject *, self)
1813 ARGDECL(PyObject *, args)
1814{
1815 PyObject *py_int = 0;
1816 char *s, *endptr;
1817 int len;
1818 long l;
1819
1820 if ((len = (*self->readline_func)(self, &s)) == -1)
1821 return NULL;
1822
1823 errno = 0;
1824 l = strtol(s, &endptr, 0);
1825
1826 free(s);
1827
1828 if (errno || strlen(endptr))
1829 {
1830 PyErr_SetString(PyExc_ValueError, "could not convert string to int");
1831 goto err;
1832 }
1833
1834 UNLESS(py_int = PyInt_FromLong(l))
1835 goto err;
1836
1837 if (PyList_Append(self->stack, py_int) == -1)
1838 goto err;
1839
1840 Py_DECREF(py_int);
1841
1842 Py_INCREF(Py_None);
1843 return Py_None;
1844
1845err:
1846 Py_XDECREF(py_int);
1847
1848 return NULL;
1849}
1850
1851
1852static PyObject *
1853load_binint(ARG(Unpicklerobject *, self), ARG(PyObject *, args))
1854 ARGDECL(Unpicklerobject *, self)
1855 ARGDECL(PyObject *, args)
1856{
1857 PyObject *py_int = 0;
1858 char *s;
1859 unsigned char c;
1860 long l;
1861
1862 UNLESS(s = (char *)malloc(4 * sizeof(char)))
1863 {
1864 PyErr_SetString(PyExc_MemoryError, "out of memory");
1865 return NULL;
1866 }
1867
1868 if ((*self->read_func)(self, &s, 4) == -1)
1869 {
1870 free(s);
1871 return NULL;
1872 }
1873
1874 c = (unsigned char)s[0];
1875 l = (long)c;
1876 c = (unsigned char)s[1];
1877 l |= (long)c << 8;
1878 c = (unsigned char)s[2];
1879 l |= (long)c << 16;
1880 c = (unsigned char)s[3];
1881 l |= (long)c << 24;
1882
1883 free(s);
1884
1885 UNLESS(py_int = PyInt_FromLong(l))
1886 return NULL;
1887
1888 if (PyList_Append(self->stack, py_int) == -1)
1889 {
1890 Py_DECREF(py_int);
1891 return NULL;
1892 }
1893
1894 Py_DECREF(py_int);
1895
1896 Py_INCREF(Py_None);
1897 return Py_None;
1898}
1899
1900
1901static PyObject *
1902load_binint1(ARG(Unpicklerobject *, self), ARG(PyObject *, args))
1903 ARGDECL(Unpicklerobject *, self)
1904 ARGDECL(PyObject *, args)
1905{
1906 PyObject *py_int = 0;
1907 char *s;
1908 unsigned char c;
1909 long l;
1910
1911 UNLESS(s = (char *)malloc(3 * sizeof(char)))
1912 {
1913 PyErr_SetString(PyExc_MemoryError, "out of memory");
1914 return NULL;
1915 }
1916
1917 if ((*self->read_func)(self, &s, 3) == -1)
1918 {
1919 free(s);
1920 return NULL;
1921 }
1922
1923 c = (unsigned char)s[0];
1924 l = (long)c;
1925 c = (unsigned char)s[1];
1926 l |= (long)c << 8;
1927 c = (unsigned char)s[2];
1928 l |= (long)c << 16;
1929
1930 free(s);
1931
1932 UNLESS(py_int = PyInt_FromLong(l))
1933 return NULL;
1934
1935 if (PyList_Append(self->stack, py_int) == -1)
1936 {
1937 Py_DECREF(py_int);
1938 return NULL;
1939 }
1940
1941 Py_DECREF(py_int);
1942
1943 Py_INCREF(Py_None);
1944 return Py_None;
1945}
1946
1947
1948static PyObject *
1949load_binint2(ARG(Unpicklerobject *, self), ARG(PyObject *, args))
1950 ARGDECL(Unpicklerobject *, self)
1951 ARGDECL(PyObject *, args)
1952{
1953 PyObject *py_int = 0;
1954 char *s;
1955 unsigned char c;
1956 long l;
1957
1958 UNLESS(s = (char *)malloc(2 * sizeof(char)))
1959 {
1960 PyErr_SetString(PyExc_MemoryError, "out of memory");
1961 return NULL;
1962 }
1963
1964 if ((*self->read_func)(self, &s, 2) == -1)
1965 return NULL;
1966
1967 c = (unsigned char)s[0];
1968 l = (long)c;
1969 c = (unsigned char)s[1];
1970 l |= (long)c << 8;
1971
1972 free(s);
1973
1974 UNLESS(py_int = PyInt_FromLong(l))
1975 return NULL;
1976
1977 if (PyList_Append(self->stack, py_int) == -1)
1978 {
1979 Py_DECREF(py_int);
1980 return NULL;
1981 }
1982
1983 Py_DECREF(py_int);
1984
1985 Py_INCREF(Py_None);
1986 return Py_None;
1987}
1988
1989
1990static PyObject *
1991load_binint3(ARG(Unpicklerobject *, self), ARG(PyObject *, args))
1992 ARGDECL(Unpicklerobject *, self)
1993 ARGDECL(PyObject *, args)
1994{
1995 PyObject *py_int = 0;
1996 char *s;
1997 unsigned char c;
1998 long l;
1999
2000 UNLESS(s = (char *)malloc(sizeof(char)))
2001 {
2002 PyErr_SetString(PyExc_MemoryError, "out of memory");
2003 return NULL;
2004 }
2005
2006 if ((*self->read_func)(self, &s, 1) == -1)
2007 {
2008 free(s);
2009 return NULL;
2010 }
2011
2012 c = (unsigned char)s[0];
2013 l = (long)c;
2014
2015 free(s);
2016
2017 UNLESS(py_int = PyInt_FromLong(l))
2018 return NULL;
2019
2020 if (PyList_Append(self->stack, py_int) == -1)
2021 {
2022 Py_DECREF(py_int);
2023 return NULL;
2024 }
2025
2026 Py_DECREF(py_int);
2027
2028 Py_INCREF(Py_None);
2029 return Py_None;
2030}
2031
2032
2033static PyObject *
2034load_long(ARG(Unpicklerobject *, self), ARG(PyObject *, args))
2035 ARGDECL(Unpicklerobject *, self)
2036 ARGDECL(PyObject *, args)
2037{
2038 PyObject *py_str = 0, *l = 0;
2039 char *s;
2040 int len;
2041
2042 static PyObject *arg;
2043
2044 if ((len = (*self->readline_func)(self, &s)) == -1)
2045 return NULL;
2046
2047 UNLESS(py_str = PyString_FromStringAndSize(s, len))
2048 {
2049 free(s);
2050 return NULL;
2051 }
2052
2053 free(s);
2054
2055 UNLESS(arg)
2056 {
2057 UNLESS(arg = Py_BuildValue("(Oi)", py_str, 0))
2058 return NULL;
2059 }
2060 else
2061 {
2062 if (PyTuple_SetItem(arg, 0, py_str) == -1)
2063 goto err;
2064 Py_INCREF(py_str);
2065 }
2066
2067 UNLESS(l = PyObject_CallObject(atol_func, arg))
2068 goto err;
2069
2070 if (PyList_Append(self->stack, l) == -1)
2071 goto err;
2072
2073 Py_DECREF(py_str);
2074 Py_DECREF(l);
2075
2076 Py_INCREF(Py_None);
2077 return Py_None;
2078
2079err:
2080 Py_XDECREF(l);
2081 Py_XDECREF(py_str);
2082
2083 return NULL;
2084}
2085
2086
2087static PyObject *
2088load_float(ARG(Unpicklerobject *, self), ARG(PyObject *, args))
2089 ARGDECL(Unpicklerobject *, self)
2090 ARGDECL(PyObject *, args)
2091{
2092 PyObject *py_float = 0;
2093 char *s, *endptr;
2094 int len;
2095 double d;
2096
2097 if ((len = (*self->readline_func)(self, &s)) == -1)
2098 return NULL;
2099
2100 errno = 0;
2101 d = strtod(s, &endptr);
2102
2103 free(s);
2104
2105 if (errno || strlen(endptr))
2106 {
2107 PyErr_SetString(PyExc_ValueError, "could not convert string to long");
2108 goto err;
2109 }
2110
2111 UNLESS(py_float = PyFloat_FromDouble(d))
2112 goto err;
2113
2114 if (PyList_Append(self->stack, py_float) == -1)
2115 goto err;
2116
2117 Py_DECREF(py_float);
2118
2119 Py_INCREF(Py_None);
2120 return Py_None;
2121
2122err:
2123 Py_XDECREF(py_float);
2124
2125 return NULL;
2126}
2127
2128
2129static PyObject *
2130load_string(ARG(Unpicklerobject *, self), ARG(PyObject *, args))
2131 ARGDECL(Unpicklerobject *, self)
2132 ARGDECL(PyObject *, args)
2133{
2134 PyObject *str = 0;
2135 char *s;
2136 int len;
2137 static PyObject *eval_dict = 0;
2138
2139 UNLESS(eval_dict)
2140 UNLESS(eval_dict = Py_BuildValue("{s{}}", "__builtins__"))
2141 return NULL;
2142
2143 if ((len = (*self->readline_func)(self, &s)) == -1)
2144 return NULL;
2145
2146 UNLESS(str = PyRun_String(s, eval_input, eval_dict, eval_dict))
2147 {
2148 free(s);
2149 goto err;
2150 }
2151
2152 free(s);
2153
2154 if (PyList_Append(self->stack, str) == -1)
2155 goto err;
2156
2157 Py_DECREF(str);
2158
2159 Py_INCREF(Py_None);
2160 return Py_None;
2161
2162err:
2163 Py_XDECREF(str);
2164
2165 return NULL;
2166}
2167
2168
2169static PyObject *
2170load_binstring(ARG(Unpicklerobject *, self), ARG(PyObject *, args))
2171 ARGDECL(Unpicklerobject *, self)
2172 ARGDECL(PyObject *, args)
2173{
2174 PyObject *py_string = 0;
2175 char *s, *endptr;
2176 int len, i;
2177
2178 if ((len = (*self->readline_func)(self, &s)) == -1)
2179 return NULL;
2180
2181 i = atoi(s);
2182
2183 if (i > len)
2184 {
2185 free(s);
2186
2187 UNLESS(s = (char *)malloc(i * sizeof(char)))
2188 {
2189 PyErr_SetString(PyExc_MemoryError, "out of memory");
2190 return NULL;
2191 }
2192 }
2193
2194 if ((*self->read_func)(self, &s, i) == -1)
2195 {
2196 free(s);
2197 return NULL;
2198 }
2199
2200 UNLESS(py_string = PyString_FromStringAndSize(s, i))
2201 {
2202 free(s);
2203 return NULL;
2204 }
2205
2206 free(s);
2207
2208 if (PyList_Append(self->stack, py_string) == -1)
2209 goto err;
2210
2211 Py_DECREF(py_string);
2212
2213 Py_INCREF(Py_None);
2214 return Py_None;
2215
2216err:
2217 Py_XDECREF(py_string);
2218
2219 return NULL;
2220}
2221
2222
2223static PyObject *
2224load_short_binstring(ARG(Unpicklerobject *, self), ARG(PyObject *, args))
2225 ARGDECL(Unpicklerobject *, self)
2226 ARGDECL(PyObject *, args)
2227{
2228 PyObject *py_string = 0;
2229 char *s;
2230 unsigned char l;
2231
2232
2233 UNLESS(s = (char *)malloc(sizeof(char)))
2234 {
2235 PyErr_SetString(PyExc_MemoryError, "out of memory");
2236 return NULL;
2237 }
2238
2239 if ((*self->read_func)(self, &s, 1) == -1)
2240 {
2241 free(s);
2242 return NULL;
2243 }
2244
2245 l = (unsigned char)s[0];
2246
2247 if (l > 1)
2248 {
2249 free(s);
2250
2251 UNLESS(s = (char *)malloc(l * sizeof(char)))
2252 {
2253 PyErr_SetString(PyExc_MemoryError, "out of memory");
2254 return NULL;
2255 }
2256 }
2257
2258 if ((*self->read_func)(self, &s, l) == -1)
2259 {
2260 free(s);
2261 return NULL;
2262 }
2263
2264 UNLESS(py_string = PyString_FromStringAndSize(s, l))
2265 {
2266 free(s);
2267 return NULL;
2268 }
2269
2270 free(s);
2271
2272 if (PyList_Append(self->stack, py_string) == -1)
2273 {
2274 Py_DECREF(py_string);
2275 return NULL;
2276 }
2277
2278 Py_DECREF(py_string);
2279
2280 Py_INCREF(Py_None);
2281 return Py_None;
2282
2283err:
2284 Py_XDECREF(py_string);
2285
2286 return NULL;
2287}
2288
2289
2290static PyObject *
2291load_tuple(ARG(Unpicklerobject *, self), ARG(PyObject *, args))
2292 ARGDECL(Unpicklerobject *, self)
2293 ARGDECL(PyObject *, args)
2294{
2295 PyObject *tup = 0, *slice = 0, *list = 0;
2296 int i, j;
2297
2298 if ((i = marker(self)) == -1)
2299 return NULL;
2300
2301 if ((j = PyList_Size(self->stack)) == -1)
2302 goto err;
2303
2304 UNLESS(slice = PyList_GetSlice(self->stack, i, j))
2305 goto err;
2306
2307 UNLESS(tup = PySequence_Tuple(slice))
2308 goto err;
2309
2310 UNLESS(list = PyList_New(1))
2311 goto err;
2312
2313 if (PyList_SetItem(list, 0, tup) == -1)
2314 goto err;
2315
2316 Py_INCREF(tup);
2317
2318 if (PyList_SetSlice(self->stack, i, j, list) == -1)
2319 goto err;
2320
2321 Py_DECREF(tup);
2322 Py_DECREF(list);
2323 Py_DECREF(slice);
2324
2325 Py_INCREF(Py_None);
2326 return Py_None;
2327
2328err:
2329 Py_XDECREF(tup);
2330 Py_XDECREF(list);
2331 Py_XDECREF(slice);
2332
2333 return NULL;
2334}
2335
2336
2337static PyObject *
2338load_list(ARG(Unpicklerobject *, self), ARG(PyObject *, args))
2339 ARGDECL(Unpicklerobject *, self)
2340 ARGDECL(PyObject *, args)
2341{
2342 PyObject *list = 0, *slice = 0;
2343 int i, j;
2344
2345 if ((i = marker(self)) == -1)
2346 return NULL;
2347
2348 if ((j = PyList_Size(self->stack)) == -1)
2349 goto err;
2350
2351 UNLESS(slice = PyList_GetSlice(self->stack, i, j))
2352 goto err;
2353
2354 UNLESS(list = PyList_New(1))
2355 goto err;
2356
2357 if (PyList_SetItem(list, 0, slice) == -1)
2358 goto err;
2359 Py_INCREF(slice);
2360
2361 if (PyList_SetSlice(self->stack, i, j, list) == -1)
2362 goto err;
2363
2364 Py_DECREF(list);
2365 Py_DECREF(slice);
2366
2367 Py_INCREF(Py_None);
2368 return Py_None;
2369
2370err:
2371 Py_XDECREF(list);
2372 Py_XDECREF(slice);
2373
2374 return NULL;
2375}
2376
2377
2378static PyObject *
2379load_dict(ARG(Unpicklerobject *, self), ARG(PyObject *, args))
2380 ARGDECL(Unpicklerobject *, self)
2381 ARGDECL(PyObject *, args)
2382{
2383 PyObject *list = 0, *dict = 0, *key = 0, *value = 0;
2384 int i, j, k;
2385
2386 if ((i = marker(self)) == -1)
2387 return NULL;
2388
2389 if ((j = PyList_Size(self->stack)) == -1)
2390 goto err;
2391
2392 UNLESS(dict = PyDict_New())
2393 goto err;
2394
2395 for (k = i; k < j; k += 2)
2396 {
2397 UNLESS(key = PyList_GET_ITEM((PyListObject *)self->stack, k))
2398 goto err;
2399
2400 UNLESS(value = PyList_GET_ITEM((PyListObject *)self->stack, k + 1))
2401 goto err;
2402
2403 if (PyDict_SetItem(dict, key, value) == -1)
2404 goto err;
2405 }
2406
2407 UNLESS(list = PyList_New(1))
2408 goto err;
2409
2410 if (PyList_SetItem(list, 0, dict) == -1)
2411 goto err;
2412 Py_INCREF(dict);
2413
2414 if (PyList_SetSlice(self->stack, i, j, list) == -1)
2415 goto err;
2416
2417 Py_DECREF(list);
2418 Py_DECREF(dict);
2419
2420 Py_INCREF(Py_None);
2421 return Py_None;
2422
2423err:
2424 Py_XDECREF(dict);
2425 Py_XDECREF(list);
2426
2427 return NULL;
2428}
2429
2430
2431static PyObject *
2432load_obj(ARG(Unpicklerobject *, self), ARG(PyObject *, args))
2433 ARGDECL(Unpicklerobject *, self)
2434 ARGDECL(PyObject *, args)
2435{
2436 PyObject *class = 0, *slice = 0, *tup = 0, *obj = 0;
2437 long i;
2438 int len;
2439
2440 if ((i = marker(self)) == -1)
2441 return NULL;
2442
2443 class = PyList_GET_ITEM((PyListObject *)self->stack, i);
2444 Py_INCREF(class);
2445
2446 if ((len = PyList_Size(self->stack)) == -1)
2447 goto err;
2448
2449 UNLESS(slice = PyList_GetSlice(self->stack, i + 1, len))
2450 goto err;
2451
2452 UNLESS(tup = PySequence_Tuple(slice))
2453 goto err;
2454
2455 if (DEL_LIST_SLICE(self->stack, i, len) == -1)
2456 goto err;
2457
2458 UNLESS(obj = PyInstance_New(class, tup, NULL))
2459 goto err;
2460
2461 if (PyList_Append(self->stack, obj) == -1)
2462 goto err;
2463
2464 Py_DECREF(class);
2465 Py_DECREF(slice);
2466 Py_DECREF(tup);
2467 Py_DECREF(obj);
2468
2469 Py_INCREF(Py_None);
2470 return Py_None;
2471
2472err:
2473 Py_XDECREF(class);
2474 Py_XDECREF(slice);
2475 Py_XDECREF(tup);
2476 Py_XDECREF(obj);
2477
2478 return NULL;
2479}
2480
2481
2482static PyObject *
2483load_inst(ARG(Unpicklerobject *, self), ARG(PyObject *, args))
2484 ARGDECL(Unpicklerobject *, self)
2485 ARGDECL(PyObject *, args)
2486{
2487 PyObject *arg_tup = 0, *arg_slice = 0, *class = 0, *obj = 0;
2488 int i, j, len;
2489 char *s, *module_name, *class_name;
2490
2491 if ((i = marker(self)) == -1)
2492 return NULL;
2493
2494 if ((j = PyList_Size(self->stack)) == -1)
2495 goto err;
2496
2497 UNLESS(arg_slice = PyList_GetSlice(self->stack, i, j))
2498 goto err;
2499
2500 UNLESS(arg_tup = PySequence_Tuple(arg_slice))
2501 goto err;
2502
2503 if (DEL_LIST_SLICE(self->stack, i, j) == -1)
2504 goto err;
2505
2506 if ((*self->readline_func)(self, &s) == -1)
2507 goto err;
2508
2509 module_name = s;
2510
2511 if ((*self->readline_func)(self, &s) == -1)
2512 {
2513 free(module_name);
2514 goto err;
2515 }
2516
2517 class_name = s;
2518
2519 UNLESS(class = find_class(module_name, class_name))
2520 {
2521 free(module_name);
2522 free(class_name);
2523 goto err;
2524 }
2525
2526 free(module_name);
2527 free(class_name);
2528
2529 UNLESS(obj = PyInstance_New(class, arg_tup, NULL))
2530 goto err;
2531
2532 if (PyList_Append(self->stack, obj) == -1)
2533 goto err;
2534
2535 Py_DECREF(arg_slice);
2536 Py_DECREF(arg_tup);
2537 Py_DECREF(class);
2538 Py_DECREF(obj);
2539
2540 Py_INCREF(Py_None);
2541 return Py_None;
2542
2543err:
2544 Py_XDECREF(arg_slice);
2545 Py_XDECREF(arg_tup);
2546 Py_XDECREF(class);
2547 Py_XDECREF(obj);
2548
2549 return NULL;
2550}
2551
2552
2553static PyObject *
2554load_class(ARG(Unpicklerobject *, self), ARG(PyObject *, args))
2555 ARGDECL(Unpicklerobject *, self)
2556 ARGDECL(PyObject *, args)
2557{
2558 PyObject *class = 0;
2559 char *s, *module_name, *class_name;
2560
2561 if ((*self->readline_func)(self, &s) == -1)
2562 return NULL;
2563
2564 module_name = s;
2565
2566 if ((*self->readline_func)(self, &s) == -1)
2567 {
2568 free(module_name);
2569 return NULL;
2570 }
2571
2572 class_name = s;
2573
2574 UNLESS(class = find_class(module_name, class_name))
2575 {
2576 free(module_name);
2577 free(class_name);
2578 return NULL;
2579 }
2580
2581 free(module_name);
2582 free(class_name);
2583
2584 if (PyList_Append(self->stack, class) == -1)
2585 goto err;
2586
2587 Py_DECREF(class);
2588
2589 Py_INCREF(Py_None);
2590 return Py_None;
2591
2592err:
2593 Py_XDECREF(class);
2594
2595 return NULL;
2596}
2597
2598
2599static PyObject *
2600load_persid(ARG(Unpicklerobject *, self), ARG(PyObject *, args))
2601 ARGDECL(Unpicklerobject *, self)
2602 ARGDECL(PyObject *, args)
2603{
2604 PyObject *pid = 0, *pers_load_val = 0;
2605 char *s;
2606 int len;
2607
2608 if (self->pers_func)
2609 {
2610 if ((len = (*self->readline_func)(self, &s)) == -1)
2611 return NULL;
2612
2613 UNLESS(pid = PyString_FromStringAndSize(s, len))
2614 {
2615 free(s);
2616 return NULL;
2617 }
2618
2619 free(s);
2620
2621 UNLESS(self->arg)
2622 UNLESS(self->arg = PyTuple_New(1))
2623 goto err;
2624
2625 if (PyTuple_SetItem(self->arg, 0, pid) == -1)
2626 goto err;
2627 Py_INCREF(pid);
2628
2629 UNLESS(pers_load_val = PyObject_CallObject(self->pers_func, self->arg))
2630 goto err;
2631
2632 if (PyList_Append(self->stack, pers_load_val) == -1)
2633 goto err;
2634
2635 Py_DECREF(pid);
2636 Py_DECREF(pers_load_val);
2637
2638 Py_INCREF(Py_None);
2639 return Py_None;
2640 }
2641
2642 Py_INCREF(Py_None);
2643 return Py_None;
2644
2645err:
2646 Py_XDECREF(pid);
2647 Py_XDECREF(pers_load_val);
2648
2649 return NULL;
2650}
2651
2652
2653static PyObject *
2654load_pop(ARG(Unpicklerobject *, self), ARG(PyObject *, args))
2655 ARGDECL(Unpicklerobject *, self)
2656 ARGDECL(PyObject *, args)
2657{
2658 int len;
2659
2660 if ((len = PyList_Size(self->stack)) == -1)
2661 return NULL;
2662
2663 if (DEL_LIST_SLICE(self->stack, len - 1, len) == -1)
2664 return NULL;
2665
2666 Py_INCREF(Py_None);
2667 return Py_None;
2668}
2669
2670
2671static PyObject *
2672load_dup(ARG(Unpicklerobject *, self), ARG(PyObject *, args))
2673 ARGDECL(Unpicklerobject *, self)
2674 ARGDECL(PyObject *, args)
2675{
2676 PyObject *last;
2677 int len;
2678
2679 if ((len = PyList_Size(self->stack)) == -1)
2680 return NULL;
2681
2682 UNLESS(last = PyList_GetItem(self->stack, len - 1))
2683 return NULL;
2684
2685 if (PyList_Append(self->stack, last) == -1)
2686 return NULL;
2687
2688 Py_INCREF(Py_None);
2689 return Py_None;
2690}
2691
2692
2693static PyObject *
2694load_get(ARG(Unpicklerobject *, self), ARG(PyObject *, args))
2695 ARGDECL(Unpicklerobject *, self)
2696 ARGDECL(PyObject *, args)
2697{
2698 PyObject *py_str = 0, *value = 0;
2699 char *s;
2700 int len;
2701
2702 if ((len = (*self->readline_func)(self, &s)) == -1)
2703 return NULL;
2704
2705 UNLESS(py_str = PyString_FromStringAndSize(s, len))
2706 {
2707 free(s);
2708 return NULL;
2709 }
2710
2711 free(s);
2712
2713 UNLESS(value = PyDict_GetItem(self->memo, py_str))
2714 goto err;
2715
2716 if (PyList_Append(self->stack, value) == -1)
2717 goto err;
2718
2719 Py_DECREF(py_str);
2720
2721 Py_INCREF(Py_None);
2722 return Py_None;
2723
2724err:
2725 Py_XDECREF(py_str);
2726
2727 return NULL;
2728}
2729
2730
2731static PyObject *
2732load_binget(ARG(Unpicklerobject *, self), ARG(PyObject *, args))
2733 ARGDECL(Unpicklerobject *, self)
2734 ARGDECL(PyObject *, args)
2735{
2736 PyObject *py_key = 0, *value = 0;
2737 char *s;
2738 unsigned char key;
2739
2740 UNLESS(s = (char *)malloc(sizeof(char)))
2741 {
2742 PyErr_SetString(PyExc_MemoryError, "out of memory");
2743 return NULL;
2744 }
2745
2746 if ((*self->read_func)(self, &s, 1) == -1)
2747 {
2748 free(s);
2749 return NULL;
2750 }
2751
2752 key = (unsigned char)s[0];
2753 free(s);
2754
2755 UNLESS(py_key = PyInt_FromLong((long)key))
2756 return NULL;
2757
2758 UNLESS(value = PyDict_GetItem(self->memo, py_key))
2759 goto err;
2760
2761 if (PyList_Append(self->stack, value) == -1)
2762 return NULL;
2763
2764 Py_DECREF(py_key);
2765
2766 Py_INCREF(Py_None);
2767 return Py_None;
2768
2769err:
2770 Py_XDECREF(py_key);
2771
2772 return NULL;
2773}
2774
2775
2776static PyObject *
2777load_put(ARG(Unpicklerobject *, self), ARG(PyObject *, args))
2778 ARGDECL(Unpicklerobject *, self)
2779 ARGDECL(PyObject *, args)
2780{
2781 PyObject *py_str = 0, *value = 0;
2782 int len;
2783 char *s;
2784
2785 if ((len = (*self->readline_func)(self, &s)) == -1)
2786 return NULL;
2787
2788 UNLESS(py_str = PyString_FromStringAndSize(s, len))
2789 {
2790 free(s);
2791 return NULL;
2792 }
2793
2794 free(s);
2795
2796 if ((len = PyList_Size(self->stack)) == -1)
2797 goto err;
2798
2799 UNLESS(value = PyList_GetItem(self->stack, len - 1))
2800 goto err;
2801
2802 if (PyDict_SetItem(self->memo, py_str, value) == -1)
2803 goto err;
2804
2805 Py_DECREF(py_str);
2806
2807 Py_INCREF(Py_None);
2808 return Py_None;
2809
2810err:
2811 Py_XDECREF(py_str);
2812
2813 return NULL;
2814}
2815
2816
2817static PyObject *
2818load_binput(ARG(Unpicklerobject *, self), ARG(PyObject *, args))
2819 ARGDECL(Unpicklerobject *, self)
2820 ARGDECL(PyObject *, args)
2821{
2822 PyObject *py_key = 0, *value = 0;
2823 char *s;
2824 unsigned char key;
2825 int len;
2826
2827 UNLESS(s = (char *)malloc(sizeof(char)))
2828 {
2829 PyErr_SetString(PyExc_MemoryError, "out of memory");
2830 return NULL;
2831 }
2832
2833 if ((*self->read_func)(self, &s, 1) == -1)
2834 return NULL;
2835
2836 key = (unsigned char)s[0];
2837
2838 free(s);
2839
2840 UNLESS(py_key = PyInt_FromLong((long)key))
2841 return NULL;
2842
2843 if ((len = PyList_Size(self->stack)) == -1)
2844 goto err;
2845
2846 UNLESS(value = PyList_GetItem(self->stack, len - 1))
2847 goto err;
2848
2849 if (PyDict_SetItem(self->memo, py_key, value) == -1)
2850 goto err;
2851
2852 Py_DECREF(py_key);
2853
2854 Py_INCREF(Py_None);
2855 return Py_None;
2856
2857err:
2858 Py_XDECREF(py_key);
2859
2860 return NULL;
2861}
2862
2863
2864static PyObject *
2865load_append(ARG(Unpicklerobject *, self), ARG(PyObject *, args))
2866 ARGDECL(Unpicklerobject *, self)
2867 ARGDECL(PyObject *, args)
2868{
2869 PyObject *value = 0, *list = 0;
2870 int len;
2871 static PyObject *append_str = 0;
2872
2873 UNLESS(append_str)
2874 UNLESS(append_str = PyString_FromString("append"))
2875 return NULL;
2876
2877 if ((len = PyList_Size(self->stack)) == -1)
2878 return NULL;
2879
2880 UNLESS(value = PyList_GetItem(self->stack, len - 1))
2881 return NULL;
2882 Py_INCREF(value);
2883
2884 if (DEL_LIST_SLICE(self->stack, len - 1, len) == -1)
2885 goto err;
2886
2887 UNLESS(list = PyList_GetItem(self->stack, len - 2))
2888 goto err;
2889
2890 if (PyList_Check(list))
2891 {
2892 if (PyList_Append(list, value) == -1)
2893 goto err;
2894 }
2895 else
2896 {
2897 PyObject *append_method, *junk;
2898
2899 UNLESS(append_method = PyObject_GetAttr(list, append_str))
2900 return NULL;
2901
2902 UNLESS(self->arg)
2903 UNLESS(self->arg = PyTuple_New(1))
2904 {
2905 Py_DECREF(append_method);
2906 goto err;
2907 }
2908
2909 if (PyTuple_SetItem(self->arg, 0, value) == -1)
2910 {
2911 Py_DECREF(append_method);
2912 goto err;
2913 }
2914 Py_INCREF(value);
2915
2916 UNLESS(junk = PyObject_CallObject(append_method, self->arg))
2917 {
2918 Py_DECREF(append_method);
2919 goto err;
2920 }
2921 Py_DECREF(junk);
2922
2923 Py_DECREF(append_method);
2924 }
2925
2926 Py_DECREF(value);
2927
2928 Py_INCREF(Py_None);
2929 return Py_None;
2930
2931err:
2932 Py_XDECREF(value);
2933
2934 return NULL;
2935}
2936
2937
2938static PyObject *
2939load_setitem(ARG(Unpicklerobject *, self), ARG(PyObject *, args))
2940 ARGDECL(Unpicklerobject *, self)
2941 ARGDECL(PyObject *, args)
2942{
2943 PyObject *value = 0, *key = 0, *dict = 0;
2944 int len;
2945
2946 if ((len = PyList_Size(self->stack)) == -1)
2947 return NULL;
2948
2949 UNLESS(value = PyList_GetItem(self->stack, len - 1))
2950 return NULL;
2951 Py_INCREF(value);
2952
2953 UNLESS(key = PyList_GetItem(self->stack, len - 2))
2954 goto err;
2955 Py_INCREF(key);
2956
2957 if (DEL_LIST_SLICE(self->stack, len - 2, len) == -1)
2958 goto err;
2959
2960 UNLESS(dict = PyList_GetItem(self->stack, len - 3))
2961 goto err;
2962
2963 if (PyObject_SetItem(dict, key, value) == -1)
2964 goto err;
2965
2966 Py_DECREF(value);
2967 Py_DECREF(key);
2968
2969 Py_INCREF(Py_None);
2970 return Py_None;
2971
2972err:
2973 Py_XDECREF(value);
2974 Py_XDECREF(key);
2975
2976 return NULL;
2977}
2978
2979
2980static PyObject *
2981load_build(ARG(Unpicklerobject *, self), ARG(PyObject *, args))
2982 ARGDECL(Unpicklerobject *, self)
2983 ARGDECL(PyObject *, args)
2984{
2985 PyObject *value = 0, *inst = 0, *instdict = 0, *d_key = 0, *d_value = 0,
2986 *junk = 0;
2987 static PyObject *py_string__dict__;
2988 int len, i;
2989
2990 UNLESS(py_string__dict__)
2991 UNLESS(py_string__dict__ = PyString_FromString("__dict__"))
2992 return NULL;
2993
2994 if ((len = PyList_Size(self->stack)) == -1)
2995 goto err;
2996
2997 UNLESS(value = PyList_GetItem(self->stack, len - 1))
2998 goto err;
2999 Py_INCREF(value);
3000
3001 if (DEL_LIST_SLICE(self->stack, len - 1, len) == -1)
3002 goto err;
3003
3004 UNLESS(inst = PyList_GetItem(self->stack, len - 2))
3005 goto err;
3006
3007 UNLESS(PyObject_HasAttrString(inst, "__setstate__"))
3008 {
3009 UNLESS(instdict = PyObject_GetAttr(inst, py_string__dict__))
3010 goto err;
3011
3012 i = 0;
3013 while (PyDict_Next(value, &i, &d_key, &d_value))
3014 {
3015 if (PyObject_SetItem(instdict, d_key, d_value) == -1)
3016 goto err;
3017 }
3018 }
3019 else
3020 {
3021 UNLESS(junk = PyObject_CallMethod(inst, "__setstate__", "O", value))
3022 goto err;
3023 Py_DECREF(junk);
3024 }
3025
3026 Py_DECREF(value);
3027 Py_XDECREF(instdict);
3028
3029 Py_INCREF(Py_None);
3030 return Py_None;
3031
3032err:
3033 Py_XDECREF(value);
3034 Py_XDECREF(instdict);
3035
3036 return NULL;
3037}
3038
3039
3040static PyObject *
3041load_mark(ARG(Unpicklerobject *, self), ARG(PyObject *, args))
3042 ARGDECL(Unpicklerobject *, self)
3043 ARGDECL(PyObject *, args)
3044{
3045 int len;
3046
3047 if ((len = PyList_Size(self->stack)) == -1)
3048 return NULL;
3049
3050 if (!self->num_marks)
3051 {
3052 UNLESS(self->marks = (int *)malloc(20 * sizeof(int)))
3053 {
3054 PyErr_SetString(PyExc_MemoryError, "out of memory");
3055 return NULL;
3056 }
3057
3058 self->marks_size = 20;
3059 }
3060 else if ((self->num_marks + 1) > self->marks_size)
3061 {
3062 UNLESS(self->marks = (int *)realloc(self->marks, (self->marks_size + 20) * sizeof(int)))
3063 {
3064 PyErr_SetString(PyExc_MemoryError, "out of memory");
3065 return NULL;
3066 }
3067
3068 self->marks_size += 20;
3069 }
3070
3071 self->marks[self->num_marks++] = len;
3072
3073 Py_INCREF(Py_None);
3074 return Py_None;
3075}
3076
3077
3078static PyObject *
3079load_eof(ARG(Unpicklerobject *, self), ARG(PyObject *, args))
3080 ARGDECL(Unpicklerobject *, self)
3081 ARGDECL(PyObject *, args)
3082{
3083 PyErr_SetNone(PyExc_EOFError);
3084 return NULL;
3085}
3086
3087
3088static PyObject *
3089Unpickler_load(ARG(Unpicklerobject *, self), ARG(PyObject *, args))
3090 ARGDECL(Unpicklerobject *, self)
3091 ARGDECL(PyObject *, args)
3092{
3093 PyObject *stack = 0, *key = 0, *junk = 0, *err = 0,
3094 *exc = 0, *val = 0, *tb = 0, *str = 0,
3095 *key_repr = 0;
3096 char c;
3097 char *c_str;
3098 int len;
3099
3100 c_str=&c;
3101
3102 UNLESS(stack = PyList_New(0))
3103 goto err;
3104
3105 self->stack = stack;
3106 self->num_marks = 0;
3107
3108 while (1)
3109 {
3110 if ((*self->read_func)(self, &c_str, 1) == -1)
3111 break;
3112
3113 switch (c_str[0])
3114 {
3115 case NONE:
3116 UNLESS(junk = load_none(self, NULL))
3117 break;
3118 continue;
3119
3120 case BININT:
3121 UNLESS(junk = load_binint(self, NULL))
3122 break;
3123 continue;
3124
3125 case BININT1:
3126 UNLESS(junk = load_binint1(self, NULL))
3127 break;
3128 continue;
3129
3130 case BININT2:
3131 UNLESS(junk = load_binint2(self, NULL))
3132 break;
3133 continue;
3134
3135 case BININT3:
3136 UNLESS(junk = load_binint3(self, NULL))
3137 break;
3138 continue;
3139
3140 case INT:
3141 UNLESS(junk = load_int(self, NULL))
3142 break;
3143 continue;
3144
3145 case LONG:
3146 UNLESS(junk = load_long(self, NULL))
3147 break;
3148 continue;
3149
3150 case FLOAT:
3151 UNLESS(junk = load_float(self, NULL))
3152 break;
3153 continue;
3154
3155 case BINSTRING:
3156 UNLESS(junk = load_binstring(self, NULL))
3157 break;
3158 continue;
3159
3160 case SHORT_BINSTRING:
3161 UNLESS(junk = load_short_binstring(self, NULL))
3162 break;
3163 continue;
3164
3165 case STRING:
3166 UNLESS(junk = load_string(self, NULL))
3167 break;
3168 Py_DECREF(junk);
3169 continue;
3170
3171 case TUPLE:
3172 UNLESS(junk = load_tuple(self, NULL))
3173 break;
3174 Py_DECREF(junk);
3175 continue;
3176
3177 case LIST:
3178 UNLESS(junk = load_list(self, NULL))
3179 break;
3180 Py_DECREF(junk);
3181 continue;
3182
3183 case DICT:
3184 UNLESS(junk = load_dict(self, NULL))
3185 break;
3186 Py_DECREF(junk);
3187 continue;
3188
3189 case OBJ:
3190 UNLESS(junk = load_obj(self, NULL))
3191 break;
3192 Py_DECREF(junk);
3193 continue;
3194
3195 case INST:
3196 UNLESS(junk = load_inst(self, NULL))
3197 break;
3198 Py_DECREF(junk);
3199 continue;
3200
3201 case CLASS:
3202 UNLESS(junk = load_class(self, NULL))
3203 break;
3204 Py_DECREF(junk);
3205 continue;
3206
3207 case APPEND:
3208 UNLESS(junk = load_append(self, NULL))
3209 break;
3210 Py_DECREF(junk);
3211 continue;
3212
3213 case BUILD:
3214 UNLESS(junk = load_build(self, NULL))
3215 break;
3216 Py_DECREF(junk);
3217 continue;
3218
3219 case DUP:
3220 UNLESS(junk = load_dup(self, NULL))
3221 break;
3222 Py_DECREF(junk);
3223 continue;
3224
3225 case BINGET:
3226 UNLESS(junk = load_binget(self, NULL))
3227 break;
3228 Py_DECREF(junk);
3229 continue;
3230
3231 case GET:
3232 UNLESS(junk = load_get(self, NULL))
3233 break;
3234 Py_DECREF(junk);
3235 continue;
3236
3237 case MARK:
3238 UNLESS(junk = load_mark(self, NULL))
3239 break;
3240 Py_DECREF(junk);
3241 continue;
3242
3243 case BINPUT:
3244 UNLESS(junk = load_binput(self, NULL))
3245 break;
3246 Py_DECREF(junk);
3247 continue;
3248
3249 case PUT:
3250 UNLESS(junk = load_put(self, NULL))
3251 break;
3252 Py_DECREF(junk);
3253 continue;
3254
3255 case POP:
3256 UNLESS(junk = load_pop(self, NULL))
3257 break;
3258 Py_DECREF(junk);
3259 continue;
3260
3261 case SETITEM:
3262 UNLESS(junk = load_setitem(self, NULL))
3263 break;
3264 Py_DECREF(junk);
3265 continue;
3266
3267 case STOP:
3268 break;
3269
3270 case PERSID:
3271 UNLESS(junk = load_persid(self, NULL))
3272 break;
3273 Py_DECREF(junk);
3274 continue;
3275
3276 default:
3277 UNLESS(key = PyString_FromStringAndSize(c_str, 1))
3278 return NULL;
3279
3280 UNLESS(key_repr = PyObject_Repr(key))
3281 return NULL;
3282
3283 UNLESS(str = PyString_FromStringAndSize(NULL, 19 + PyString_Size(key_repr)))
3284 return NULL;
3285
3286 sprintf(PyString_AS_STRING((PyStringObject *)str),
3287 "invalid load key, \%s\.", PyString_AS_STRING((PyStringObject *)key_repr));
3288
3289 PyErr_SetObject(UnpicklingError, str);
3290
3291 Py_DECREF(str);
3292 Py_DECREF(key);
3293 Py_DECREF(key_repr);
3294
3295 return NULL;
3296 }
3297
3298 break;
3299 }
3300
3301 if ((err = PyErr_Occurred()) == PyExc_EOFError)
3302 {
3303 return load_eof(self, NULL);
3304 }
3305
3306 if (err)
3307 return NULL;
3308
3309 if ((len = PyList_Size(self->stack)) == -1)
3310 return NULL;
3311
3312 UNLESS(val = PyList_GetItem(self->stack, len - 1))
3313 return NULL;
3314 Py_INCREF(val);
3315
3316 if (DEL_LIST_SLICE(self->stack, len - 1, len) == -1)
3317 {
3318 Py_DECREF(val);
3319 return NULL;
3320 }
3321
3322 return val;
3323
3324err:
3325 Py_XDECREF(stack);
3326
3327 return NULL;
3328}
3329
3330
3331static struct PyMethodDef Unpickler_methods[] =
3332{
3333 {"load", (PyCFunction)Unpickler_load, 0, ""},
3334 {"load_none", (PyCFunction)load_none, 0, ""},
3335 {"load_int", (PyCFunction)load_int, 0, ""},
3336 {"load_long", (PyCFunction)load_long, 0, ""},
3337 {"load_float", (PyCFunction)load_float, 0, ""},
3338 {"load_string", (PyCFunction)load_string, 0, ""},
3339 {"load_tuple", (PyCFunction)load_tuple, 0, ""},
3340 {"load_list", (PyCFunction)load_list, 0, ""},
3341 {"load_dict", (PyCFunction)load_dict, 0, ""},
3342 {"load_inst", (PyCFunction)load_inst, 0, ""},
3343 {"load_class", (PyCFunction)load_class, 0, ""},
3344 {"load_persid", (PyCFunction)load_persid, 0, ""},
3345 {"load_pop", (PyCFunction)load_pop, 0, ""},
3346 {"load_dup", (PyCFunction)load_dup, 0, ""},
3347 {"load_get", (PyCFunction)load_get, 0, ""},
3348 {"load_put", (PyCFunction)load_put, 0, ""},
3349 {"load_append", (PyCFunction)load_append, 0, ""},
3350 {"load_setitem", (PyCFunction)load_setitem, 0, ""},
3351 {"load_build", (PyCFunction)load_build, 0, ""},
3352 {"load_mark", (PyCFunction)load_mark, 0, ""},
3353 {"load_eof", (PyCFunction)load_eof, 0, ""},
3354 {NULL, NULL} /* sentinel */
3355};
3356
3357
3358static Unpicklerobject *
3359newUnpicklerobject(ARG(PyObject *, f))
3360 ARGDECL(PyObject *, f)
3361{
3362 Unpicklerobject *self;
3363 PyObject *memo = 0;
3364
3365 UNLESS(memo = PyDict_New())
3366 goto err;
3367
3368 UNLESS(self = PyObject_NEW(Unpicklerobject, &Unpicklertype)) goto err;
3369
3370 if (PyFile_Check(f))
3371 {
3372 self->fp = PyFile_AsFile(f);
3373 self->read_func = read_file;
3374 self->readline_func = readline_file;
3375 self->read = NULL;
3376 self->readline = NULL;
3377 }
3378 else if (PycStringIO_InputCheck(f))
3379 {
3380 self->fp = NULL;
3381 self->read_func = read_cStringIO;
3382 self->readline_func = readline_cStringIO;
3383 self->read = NULL;
3384 self->readline = NULL;
3385 }
3386 else
3387 {
3388 PyObject *readline, *read;
3389
3390 self->fp = NULL;
3391 self->read_func = read_other;
3392 self->readline_func = readline_other;
3393
3394 UNLESS(readline = PyObject_GetAttrString(f, "readline"))
3395 goto err;
3396
3397 UNLESS(read = PyObject_GetAttrString(f, "read"))
3398 {
3399 Py_DECREF(readline);
3400 goto err;
3401 }
3402
3403 self->read = read;
3404 self->readline = readline;
3405 }
3406
3407 Py_INCREF(f);
3408
3409 self->file = f;
3410 self->memo = memo;
3411 self->arg = NULL;
3412 self->stack = NULL;
3413 self->pers_func = NULL;
3414 self->marks = NULL;
3415 self->num_marks = 0;
3416 self->marks_size = 0;
3417
3418 return self;
3419
3420err:
3421 Py_XDECREF(memo);
3422 Py_XDECREF((PyObject *)self);
3423
3424 return NULL;
3425}
3426
3427
3428static PyObject *
3429get_Unpickler(ARG(PyObject *, self), ARG(PyObject *, args))
3430 ARGDECL(PyObject *, self)
3431 ARGDECL(PyObject *, args)
3432{
3433 PyObject *file;
3434
3435 UNLESS(PyArg_Parse(args, "O", &file)) return NULL;
3436 return (PyObject *)newUnpicklerobject(file);
3437}
3438
3439
3440static void
3441Unpickler_dealloc(ARG(Unpicklerobject *, self))
3442 ARGDECL(Unpicklerobject *, self)
3443{
3444 Py_XDECREF(self->readline);
3445 Py_XDECREF(self->read);
3446 Py_XDECREF(self->file);
3447 Py_XDECREF(self->memo);
3448 Py_XDECREF(self->stack);
3449 Py_XDECREF(self->pers_func);
3450 Py_XDECREF(self->arg);
3451 free(self->marks);
3452 PyMem_DEL(self);
3453}
3454
3455
3456static PyObject *
3457Unpickler_getattr(ARG(Unpicklerobject *, self), ARG(char *, name))
3458 ARGDECL(Unpicklerobject *, self)
3459 ARGDECL(char *, name)
3460{
3461 if (!strcmp(name, "persistent_load"))
3462 {
3463 if (!self->pers_func)
3464 {
3465 PyErr_SetString(PyExc_NameError, name);
3466 return NULL;
3467 }
3468
3469 Py_INCREF(self->pers_func);
3470 return self->pers_func;
3471 }
3472
3473 if (!strcmp(name, "memo"))
3474 {
3475 if (!self->memo)
3476 {
3477 PyErr_SetString(PyExc_NameError, name);
3478 return NULL;
3479 }
3480
3481 Py_INCREF(self->memo);
3482 return self->memo;
3483 }
3484
3485 if (!strcmp(name, "UnpicklingError"))
3486 {
3487 Py_INCREF(UnpicklingError);
3488 return UnpicklingError;
3489 }
3490
3491 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
3492}
3493
3494
3495static int
3496Unpickler_setattr(ARG(Unpicklerobject *, self), ARG(char *, name), ARG(PyObject *, value))
3497 ARGDECL(Unpicklerobject *, self)
3498 ARGDECL(char *, name)
3499 ARGDECL(PyObject *, value)
3500{
3501 if (!strcmp(name, "persistent_load"))
3502 {
3503 Py_XDECREF(self->pers_func);
3504 self->pers_func = value;
3505 Py_INCREF(value);
3506 return 0;
3507 }
3508
3509 return -1;
3510}
3511
3512
3513static PyObject *
3514dump(ARG(PyObject *, self), ARG(PyObject *, args))
3515 ARGDECL(PyObject *, self)
3516 ARGDECL(PyObject *, args)
3517{
3518 PyObject *ob, *file, *ret_val;
3519 Picklerobject *pickler;
3520 int bin = 0;
3521
3522 UNLESS(PyArg_ParseTuple(args, "OO|i", &ob, &file, &bin))
3523 return NULL;
3524
3525 UNLESS(pickler = newPicklerobject(file, bin))
3526 return NULL;
3527
3528 UNLESS(ret_val = Pickler_dump(pickler, ob))
3529 {
3530 Pickler_dealloc(pickler);
3531 return NULL;
3532 }
3533
3534 Pickler_dealloc(pickler);
3535
3536 return ret_val;
3537}
3538
3539
3540static PyObject *
3541dumps(ARG(PyObject *, self), ARG(PyObject *, args))
3542 ARGDECL(PyObject *, self)
3543 ARGDECL(PyObject *, args)
3544{
3545 PyObject *ob, *file, *pickle_str;
3546 Picklerobject *pickler;
3547 int bin = 0;
3548
3549 UNLESS(PyArg_ParseTuple(args, "O|i", &ob, &bin))
3550 return NULL;
3551
3552 UNLESS(file = (*PycStringIO_NewOutput)(128))
3553 return NULL;
3554
3555 UNLESS(pickler = newPicklerobject(file, bin))
3556 {
3557 Py_DECREF(file);
3558 return NULL;
3559 }
3560
3561 UNLESS(Pickler_dump(pickler, ob))
3562 {
3563 Pickler_dealloc(pickler);
3564 Py_DECREF(file);
3565 return NULL;
3566 }
3567
3568 Pickler_dealloc(pickler);
3569
3570 UNLESS(pickle_str = (*PycStringIO_cgetvalue)((PyObject *)file))
3571 {
3572 Py_DECREF(file);
3573 return NULL;
3574 }
3575
3576 Py_DECREF(file);
3577
3578 return pickle_str;
3579}
3580
3581
3582static PyObject *
3583cpm_load(ARG(PyObject *, self), ARG(PyObject *, args))
3584 ARGDECL(PyObject *, self)
3585 ARGDECL(PyObject *, args)
3586{
3587 Unpicklerobject *unpickler;
3588 PyObject *load_result;
3589
3590 UNLESS(PyArg_Parse(args, "O", &args))
3591 return NULL;
3592
3593 UNLESS(unpickler = newUnpicklerobject(args))
3594 return NULL;
3595
3596 UNLESS(load_result = Unpickler_load(unpickler, NULL))
3597 {
3598 Unpickler_dealloc(unpickler);
3599 return NULL;
3600 }
3601
3602 Py_DECREF(unpickler);
3603
3604 return load_result;
3605}
3606
3607
3608static PyObject *
3609loads(ARG(PyObject *, self), ARG(PyObject *, args))
3610 ARGDECL(PyObject *, self)
3611 ARGDECL(PyObject *, args)
3612{
3613 PyObject *file, *load_result;
3614 Unpicklerobject *unpickler;
3615
3616 UNLESS(PyArg_Parse(args, "O", &args))
3617 return NULL;
3618
3619 UNLESS(file = (*PycStringIO_NewInput)(args))
3620 return NULL;
3621
3622 UNLESS(unpickler = newUnpicklerobject(file))
3623 {
3624 Py_DECREF(file);
3625 return NULL;
3626 }
3627
3628 UNLESS(load_result = Unpickler_load(unpickler, NULL))
3629 {
3630 Unpickler_dealloc(unpickler);
3631 Py_DECREF(file);
3632 return NULL;
3633 }
3634
3635 Py_DECREF(file);
3636 Unpickler_dealloc(unpickler);
3637
3638 return load_result;
3639}
3640
3641
3642static char Unpicklertype__doc__[] = "";
3643
3644static PyTypeObject Unpicklertype =
3645{
3646 PyObject_HEAD_INIT(&PyType_Type)
3647 0, /*ob_size*/
3648 "Unpickler", /*tp_name*/
3649 sizeof(Unpicklerobject), /*tp_basicsize*/
3650 0, /*tp_itemsize*/
3651 /* methods */
3652 (destructor)Unpickler_dealloc, /*tp_dealloc*/
3653 (printfunc)0, /*tp_print*/
3654 (getattrfunc)Unpickler_getattr, /*tp_getattr*/
3655 (setattrfunc)Unpickler_setattr, /*tp_setattr*/
3656 (cmpfunc)0, /*tp_compare*/
3657 (reprfunc)0, /*tp_repr*/
3658 0, /*tp_as_number*/
3659 0, /*tp_as_sequence*/
3660 0, /*tp_as_mapping*/
3661 (hashfunc)0, /*tp_hash*/
3662 (ternaryfunc)0, /*tp_call*/
3663 (reprfunc)0, /*tp_str*/
3664
3665 /* Space for future expansion */
3666 0L,0L,0L,0L,
3667 Unpicklertype__doc__ /* Documentation string */
3668};
3669
3670
3671static struct PyMethodDef cPickle_methods[] =
3672{
3673 {"dump", (PyCFunction)dump, 1, ""},
3674 {"dumps", (PyCFunction)dumps, 1, ""},
3675 {"load", (PyCFunction)cpm_load, 0, ""},
3676 {"loads", (PyCFunction)loads, 0, ""},
3677 {"Pickler", (PyCFunction)get_Pickler, 1, ""},
3678 {"Unpickler", (PyCFunction)get_Unpickler, 0, ""},
3679 { NULL, NULL }
3680};
3681
3682
3683static int
3684init_stuff()
3685{
3686 PyObject *builtins, *apply_func, *string;
3687
3688 UNLESS(builtins = PyImport_ImportModule("__builtin__"))
3689 return NULL;
3690
3691 UNLESS(apply_func = PyObject_GetAttrString(builtins, "apply"))
3692 return NULL;
3693
3694 BuiltinFunctionType = apply_func->ob_type;
3695
3696 Py_DECREF(apply_func);
3697
3698 Py_DECREF(builtins);
3699
3700 UNLESS(string = PyImport_ImportModule("string"))
3701 return NULL;
3702
3703 UNLESS(atol_func = PyObject_GetAttrString(string, "atol"))
3704 return NULL;
3705
3706 Py_DECREF(string);
3707
3708 UNLESS(empty_list = PyList_New(0))
3709 return NULL;
3710
3711 UNLESS(empty_tuple = PyTuple_New(0))
3712 return NULL;
3713
3714 UNLESS(class_map = PyDict_New())
3715 return NULL;
3716
3717 UNLESS(PicklingError = PyString_FromString("cPickle.PicklingError"))
3718 return NULL;
3719
3720 UNLESS(UnpicklingError = PyString_FromString("cPickle.UnpicklingError"))
3721 return NULL;
3722
3723 PycString_IMPORT;
3724}
3725
3726
3727#define CHECK_FOR_ERRORS(MESS) \
3728if(PyErr_Occurred()) { \
3729 PyObject *__sys_exc_type, *__sys_exc_value, *__sys_exc_traceback; \
3730 PyErr_Fetch( &__sys_exc_type, &__sys_exc_value, &__sys_exc_traceback); \
3731 fprintf(stderr, # MESS ":\n\t"); \
3732 PyObject_Print(__sys_exc_type, stderr,0); \
3733 fprintf(stderr,", "); \
3734 PyObject_Print(__sys_exc_value, stderr,0); \
3735 fprintf(stderr,"\n"); \
3736 fflush(stderr); \
3737 Py_FatalError(# MESS); \
3738}
3739
3740
3741/* Initialization function for the module (*must* be called initcPickle) */
3742void
3743initcPickle()
3744{
3745 PyObject *m, *d;
3746
3747 /* Create the module and add the functions */
3748 m = Py_InitModule4("cPickle", cPickle_methods,
3749 cPickle_module_documentation,
3750 (PyObject*)NULL,PYTHON_API_VERSION);
3751
3752 /* Add some symbolic constants to the module */
3753 d = PyModule_GetDict(m);
3754 ErrorObject = PyString_FromString("cPickle.error");
3755 PyDict_SetItemString(d, "error", ErrorObject);
3756
3757 /* XXXX Add constants here */
3758
3759 init_stuff();
3760 CHECK_FOR_ERRORS("can't initialize module cPickle");
3761
3762 PyDict_SetItemString(d, "PicklingError", PicklingError);
3763 CHECK_FOR_ERRORS("can't initialize module cPickle: error creating PicklingError");
3764
3765 PyDict_SetItemString(d, "UnpicklingError", UnpicklingError);
3766 CHECK_FOR_ERRORS("can't initialize module cPickle: error creating UnpicklingError");
3767}