blob: c592f3bce53b96798095c3251f0d0421e9055c1f [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"
Guido van Rossum60456fd1997-04-09 17:36:32 +000062#include "mymath.h"
Guido van Rossum2f4caa41997-01-06 22:59:08 +000063
64#include <errno.h>
65
Guido van Rossum2f4caa41997-01-06 22:59:08 +000066#define UNLESS(E) if (!(E))
Guido van Rossum2f4caa41997-01-06 22:59:08 +000067
Guido van Rossum60456fd1997-04-09 17:36:32 +000068#define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL))
Guido van Rossum2f4caa41997-01-06 22:59:08 +000069
Guido van Rossum60456fd1997-04-09 17:36:32 +000070#define WRITE_BUF_SIZE 256
71
72
73#define MARK '('
74#define STOP '.'
75#define POP '0'
76#define POP_MARK '1'
77#define DUP '2'
78#define FLOAT 'F'
79#ifdef FORMAT_1_3
80#define BINFLOAT 'G'
81#endif
82#define INT 'I'
83#define BININT 'J'
84#define BININT1 'K'
85#define LONG 'L'
86#define BININT2 'M'
87#define NONE 'N'
88#define PERSID 'P'
89#define BINPERSID 'Q'
90#define REDUCE 'R'
91#define STRING 'S'
92#define BINSTRING 'T'
Guido van Rossum2f4caa41997-01-06 22:59:08 +000093#define SHORT_BINSTRING 'U'
Guido van Rossum60456fd1997-04-09 17:36:32 +000094#define APPEND 'a'
95#define BUILD 'b'
96#define GLOBAL 'c'
97#define DICT 'd'
98#define EMPTY_DICT '}'
99#define APPENDS 'e'
100#define GET 'g'
101#define BINGET 'h'
102#define INST 'i'
103#define LONG_BINGET 'j'
104#define LIST 'l'
105#define EMPTY_LIST ']'
106#define OBJ 'o'
107#define PUT 'p'
108#define BINPUT 'q'
109#define LONG_BINPUT 'r'
110#define SETITEM 's'
111#define TUPLE 't'
112#define EMPTY_TUPLE ')'
113#define SETITEMS 'u'
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000114
Guido van Rossum60456fd1997-04-09 17:36:32 +0000115static char MARKv = MARK;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000116
117/* atol function from string module */
118static PyObject *atol_func;
119
120static PyObject *PicklingError;
121static PyObject *UnpicklingError;
122
Guido van Rossum60456fd1997-04-09 17:36:32 +0000123static PyObject *dispatch_table;
124static PyObject *safe_constructors;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000125static PyObject *class_map;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000126static PyObject *empty_tuple;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000127
Guido van Rossum60456fd1997-04-09 17:36:32 +0000128static PyObject *__class___str, *__getinitargs___str, *__dict___str,
129 *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
130 *write_str, *__safe_for_unpickling___str, *append_str,
131 *read_str, *readline_str;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000132
Guido van Rossum60456fd1997-04-09 17:36:32 +0000133/* __builtins__ module */
134static PyObject *builtins;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000135
Guido van Rossum60456fd1997-04-09 17:36:32 +0000136static int save();
137static int put2();
138
139typedef struct {
140 PyObject_HEAD
141 FILE *fp;
142 PyObject *write;
143 PyObject *file;
144 PyObject *memo;
145 PyObject *arg;
146 PyObject *pers_func;
147 PyObject *inst_pers_func;
148 int bin;
149 int (*write_func)();
150 char *write_buf;
151 int buf_size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000152} Picklerobject;
153
Guido van Rossum60456fd1997-04-09 17:36:32 +0000154static PyTypeObject Picklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000155
156
Guido van Rossum60456fd1997-04-09 17:36:32 +0000157typedef struct {
158 PyObject_HEAD
159 FILE *fp;
160 PyObject *file;
161 PyObject *readline;
162 PyObject *read;
163 PyObject *memo;
164 PyObject *arg;
165 PyObject *stack;
166 PyObject *mark;
167 PyObject *pers_func;
168 PyObject *last_string;
169 int *marks;
170 int num_marks;
171 int marks_size;
172 int (*read_func)();
173 int (*readline_func)();
174 int buf_size;
175 char *buf;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000176} Unpicklerobject;
177
Guido van Rossum60456fd1997-04-09 17:36:32 +0000178static PyTypeObject Unpicklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000179
Guido van Rossum60456fd1997-04-09 17:36:32 +0000180int
181cPickle_PyMapping_HasKey(o, key)
182 PyObject *o;
183 PyObject *key;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000184{
Guido van Rossum60456fd1997-04-09 17:36:32 +0000185 PyObject *v;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000186
Guido van Rossum60456fd1997-04-09 17:36:32 +0000187 if (v = PyObject_GetItem(o,key))
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000188 {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000189 Py_DECREF(v);
190 return 1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000191 }
192
Guido van Rossum60456fd1997-04-09 17:36:32 +0000193 PyErr_Clear();
194 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000195}
196
Guido van Rossumd385d591997-04-09 17:47:47 +0000197#define PyErr_Format PyErr_JFFormat
198static
Guido van Rossum60456fd1997-04-09 17:36:32 +0000199PyObject *
200#ifdef HAVE_STDARG_PROTOTYPES
201/* VARARGS 2 */
202PyErr_Format(PyObject *ErrType, char *stringformat, char *format, ...)
203#else
204/* VARARGS */
205PyErr_Format(va_alist) va_dcl
206#endif
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000207{
Guido van Rossum60456fd1997-04-09 17:36:32 +0000208 va_list va;
209 PyObject *args=0, *retval=0;
210#ifdef HAVE_STDARG_PROTOTYPES
211 va_start(va, format);
212#else
213 PyObject *ErrType;
214 char *stringformat, *format;
215 va_start(va);
216 ErrType = va_arg(va, PyObject *);
217 stringformat = va_arg(va, char *);
218 format = va_arg(va, char *);
219#endif
220
221 if(format) args = Py_VaBuildValue(format, va);
222 va_end(va);
223 if(format && ! args) return NULL;
224 if(stringformat && !(retval=PyString_FromString(stringformat))) return NULL;
225
226 if(retval)
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000227 {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000228 if(args)
229 {
230 PyObject *v;
231 v=PyString_Format(retval, args);
232 Py_DECREF(retval);
233 Py_DECREF(args);
234 if(! v) return NULL;
235 retval=v;
236 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000237 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000238 else
239 if(args) retval=args;
240 else
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000241 {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000242 PyErr_SetObject(ErrType,Py_None);
243 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000244 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000245 PyErr_SetObject(ErrType,retval);
246 Py_DECREF(retval);
247 return NULL;
248}
249
250static int
251write_file(Picklerobject *self, char *s, int n) {
252 if (s == NULL) {
253 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000254 }
255
Guido van Rossum60456fd1997-04-09 17:36:32 +0000256 if ((int)fwrite(s, sizeof(char), n, self->fp) != n) {
257 PyErr_SetFromErrno(PyExc_IOError);
258 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000259 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000260
261 return n;
262}
263
264
265static int
266write_cStringIO(Picklerobject *self, char *s, int n) {
267 if (s == NULL) {
268 return 0;
269 }
270
271 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
272 return -1;
273 }
274
275 return n;
276}
277
278
279static int
280write_other(Picklerobject *self, char *s, int n) {
281 PyObject *py_str = 0, *junk = 0;
282 int res = -1;
283
284 if (s == NULL) {
285 UNLESS(self->buf_size) return 0;
286 UNLESS(py_str =
287 PyString_FromStringAndSize(self->write_buf, self->buf_size))
288 goto finally;
289 }
290 else {
291 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
292 if (write_other(self, NULL, 0) < 0)
293 goto finally;
294 }
295
296 if (n > WRITE_BUF_SIZE) {
297 UNLESS(py_str =
298 PyString_FromStringAndSize(s, n))
299 goto finally;
300 }
301 else {
302 memcpy(self->write_buf + self->buf_size, s, n);
303 self->buf_size += n;
304 res = n;
305 goto finally;
306 }
307 }
308
309 UNLESS(self->arg)
310 UNLESS(self->arg = PyTuple_New(1))
311 goto finally;
312
313 Py_INCREF(py_str);
314 if (PyTuple_SetItem(self->arg, 0, py_str) < 0)
315 goto finally;
316
317 UNLESS(junk = PyObject_CallObject(self->write, self->arg))
318 goto finally;
319 Py_DECREF(junk);
320
321 self->buf_size = 0;
322
323 res = n;
324
325finally:
326 Py_XDECREF(py_str);
327
328 return res;
329}
330
331
332static int
333read_file(Unpicklerobject *self, char **s, int n) {
334
335 if (self->buf_size == 0) {
336 int size;
337
338 size = ((n < 32) ? 32 : n);
339 UNLESS(self->buf = (char *)malloc(size * sizeof(char))) {
340 PyErr_NoMemory();
341 return -1;
342 }
343
344 self->buf_size = size;
345 }
346 else if (n > self->buf_size) {
347 UNLESS(self->buf = (char *)realloc(self->buf, n * sizeof(char))) {
348 PyErr_NoMemory();
349 return -1;
350 }
351
352 self->buf_size = n;
353 }
354
355 if ((int)fread(self->buf, sizeof(char), n, self->fp) != n) {
356 if (feof(self->fp)) {
357 PyErr_SetNone(PyExc_EOFError);
358 return -1;
359 }
360
361 PyErr_SetFromErrno(PyExc_IOError);
362 return -1;
363 }
364
365 *s = self->buf;
366
367 return n;
368}
369
370
371static int
372readline_file(Unpicklerobject *self, char **s) {
373 int i;
374
375 if (self->buf_size == 0) {
376 UNLESS(self->buf = (char *)malloc(40 * sizeof(char))) {
377 PyErr_NoMemory();
378 return -1;
379 }
380
381 self->buf_size = 40;
382 }
383
384 i = 0;
385 while (1) {
386 for (; i < (self->buf_size - 1); i++) {
387 if (feof(self->fp) || (self->buf[i] = getc(self->fp)) == '\n') {
388 self->buf[i + 1] = '\0';
389 *s = self->buf;
390 return i + 1;
391 }
392 }
393
394 UNLESS(self->buf = (char *)realloc(self->buf,
395 (self->buf_size * 2) * sizeof(char))) {
396 PyErr_NoMemory();
397 return -1;
398 }
399
400 self->buf_size *= 2;
401 }
402
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000403}
404
405
406static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000407read_cStringIO(Unpicklerobject *self, char **s, int n) {
408 char *ptr;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000409
Guido van Rossum60456fd1997-04-09 17:36:32 +0000410 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
411 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000412 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000413 }
414
Guido van Rossum60456fd1997-04-09 17:36:32 +0000415 *s = ptr;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000416
Guido van Rossum60456fd1997-04-09 17:36:32 +0000417 return n;
418}
419
420
421static int
422readline_cStringIO(Unpicklerobject *self, char **s) {
423 int n;
424 char *ptr;
425
426 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
427 return -1;
428 }
429
430 *s = ptr;
431
432 return n;
433}
434
435
436static int
437read_other(Unpicklerobject *self, char **s, int n) {
438 PyObject *bytes, *str;
439 int res = -1;
440
441 UNLESS(bytes = PyInt_FromLong(n)) {
442 if (!PyErr_Occurred())
443 PyErr_SetNone(PyExc_EOFError);
444
445 goto finally;
446 }
447
448 UNLESS(self->arg)
449 UNLESS(self->arg = PyTuple_New(1))
450 goto finally;
451
452 Py_INCREF(bytes);
453 if (PyTuple_SetItem(self->arg, 0, bytes) < 0)
454 goto finally;
455
456 UNLESS(str = PyObject_CallObject(self->read, self->arg))
457 goto finally;
458
459 Py_XDECREF(self->last_string);
460 self->last_string = str;
461
462 *s = PyString_AsString(str);
463
464 res = n;
465
466finally:
467 Py_XDECREF(bytes);
468
469 return res;
470}
471
472
473static int
474readline_other(Unpicklerobject *self, char **s) {
475 PyObject *str;
476 int str_size;
477
478 UNLESS(str = PyObject_CallObject(self->readline, empty_tuple)) {
479 return -1;
480 }
481
482 str_size = PyString_Size(str);
483
484 Py_XDECREF(self->last_string);
485 self->last_string = str;
486
487 *s = PyString_AsString(str);
488
489 return str_size;
490}
491
492
493static char *
494strndup(char *s, int l)
495{
496 char *r;
497 UNLESS(r=malloc((l+1)*sizeof(char))) return (char*)PyErr_NoMemory();
498 memcpy(r,s,l);
499 r[l]=0;
500 return r;
501}
502
503
504static int
505get(Picklerobject *self, PyObject *id) {
506 PyObject *value = 0;
507 long c_value;
508 char s[30];
509 int len;
510
511 UNLESS(value = PyDict_GetItem(self->memo, id))
512 return -1;
513
514 UNLESS(value = PyTuple_GetItem(value, 0))
515 return -1;
516
517 c_value = PyInt_AsLong(value);
518
519 if (!self->bin) {
520 s[0] = GET;
521 sprintf(s + 1, "%ld\n", c_value);
522 len = strlen(s);
523 }
524 else {
525 if (c_value < 256) {
526 s[0] = BINGET;
527 s[1] = (int)(c_value & 0xff);
528 len = 2;
529 }
530 else {
531 s[0] = LONG_BINGET;
532 s[1] = (int)(c_value & 0xff);
533 s[2] = (int)((c_value >> 8) & 0xff);
534 s[3] = (int)((c_value >> 16) & 0xff);
535 s[4] = (int)((c_value >> 24) & 0xff);
536 len = 5;
537 }
538 }
539
540 if ((*self->write_func)(self, s, len) < 0)
541 return -1;
542
543 return 0;
544}
545
546
547static int
548put(Picklerobject *self, PyObject *ob) {
549 if (ob->ob_refcnt < 2)
550 return 0;
551
552 return put2(self, ob);
553}
554
555
556static int
557put2(Picklerobject *self, PyObject *ob) {
558 char c_str[30];
559 int p, len, res = -1;
560 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
561 if ((p = PyDict_Size(self->memo)) < 0)
562 goto finally;
563
564 if (!self->bin) {
565 c_str[0] = PUT;
566 sprintf(c_str + 1, "%d\n", p);
567 len = strlen(c_str);
568 }
569 else {
570 if (p >= 256) {
571 c_str[0] = LONG_BINPUT;
572 c_str[1] = (int)(p & 0xff);
573 c_str[2] = (int)((p >> 8) & 0xff);
574 c_str[3] = (int)((p >> 16) & 0xff);
575 c_str[4] = (int)((p >> 24) & 0xff);
576 len = 5;
577 }
578 else {
579 c_str[0] = BINPUT;
580 c_str[1] = p;
581 len = 2;
582 }
583 }
584
585 if ((*self->write_func)(self, c_str, len) < 0)
586 goto finally;
587
588 UNLESS(py_ob_id = PyInt_FromLong((long)ob))
589 goto finally;
590
591 UNLESS(memo_len = PyInt_FromLong(p))
592 goto finally;
593
594 UNLESS(t = PyTuple_New(2))
595 goto finally;
596
597 PyTuple_SET_ITEM(t, 0, memo_len);
598 Py_INCREF(memo_len);
599 PyTuple_SET_ITEM(t, 1, ob);
600 Py_INCREF(ob);
601
602 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
603 goto finally;
604
605 res = 0;
606
607finally:
608 Py_XDECREF(py_ob_id);
609 Py_XDECREF(memo_len);
610 Py_XDECREF(t);
611
612 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000613}
614
615
616static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +0000617whichmodule(PyObject *global, PyObject *global_name) {
618 int i, j;
619 PyObject *module = 0, *modules_dict = 0,
620 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000621
Guido van Rossum60456fd1997-04-09 17:36:32 +0000622 if (module = PyDict_GetItem(class_map, global)) {
623 Py_INCREF(module);
624 return module;
625 }
626 else {
627 PyErr_Clear();
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000628 }
629
Guido van Rossum60456fd1997-04-09 17:36:32 +0000630 UNLESS(modules_dict = PySys_GetObject("modules"))
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000631 return NULL;
632
Guido van Rossum60456fd1997-04-09 17:36:32 +0000633 i = 0;
634 while (j = PyDict_Next(modules_dict, &i, &name, &module)) {
635 UNLESS(global_name_attr = PyObject_GetAttr(module, global_name)) {
636 PyErr_Clear();
637 continue;
638 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000639
Guido van Rossum60456fd1997-04-09 17:36:32 +0000640 if (global_name_attr != global) {
641 Py_DECREF(global_name_attr);
642 continue;
643 }
644
645 Py_DECREF(global_name_attr);
646
647 break;
648 }
649
650 if (!j) {
651 PyErr_Format(PicklingError, "Could not find module for %s.",
652 "O", global_name);
653 return NULL;
654 }
655
656 PyDict_SetItem(class_map, global, name);
657
658 Py_INCREF(name);
659 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000660}
661
662
Guido van Rossum60456fd1997-04-09 17:36:32 +0000663static int
664save_none(Picklerobject *self, PyObject *args) {
665 static char none = NONE;
666 if ((*self->write_func)(self, &none, 1) < 0)
667 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000668
Guido van Rossum60456fd1997-04-09 17:36:32 +0000669 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000670}
671
672
Guido van Rossum60456fd1997-04-09 17:36:32 +0000673static int
674save_int(Picklerobject *self, PyObject *args) {
675 char c_str[32];
676 long l = PyInt_AS_LONG((PyIntObject *)args);
677 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000678
Guido van Rossum60456fd1997-04-09 17:36:32 +0000679 if (!self->bin || sizeof(long) == 8 && (l >> 32)) {
680 /* Save extra-long ints in non-binary mode, so that
681 we can use python long parsing code to restore,
682 if necessary. */
683 c_str[0] = INT;
684 sprintf(c_str + 1, "%ld\n", l);
685 if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
686 return -1;
687 }
688 else {
689 c_str[1] = (int)( l & 0xff);
690 c_str[2] = (int)((l >> 8) & 0xff);
691 c_str[3] = (int)((l >> 16) & 0xff);
692 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000693
Guido van Rossum60456fd1997-04-09 17:36:32 +0000694 if ((c_str[4] == 0) && (c_str[3] == 0)) {
695 if (c_str[2] == 0) {
696 c_str[0] = BININT1;
697 len = 2;
698 }
699 else {
700 c_str[0] = BININT2;
701 len = 3;
702 }
703 }
704 else {
705 c_str[0] = BININT;
706 len = 5;
707 }
708
709 if ((*self->write_func)(self, c_str, len) < 0)
710 return -1;
711 }
712
713 return 0;
714}
715
716
717static int
718save_long(Picklerobject *self, PyObject *args) {
719 int size, res = -1;
720 PyObject *repr = 0;
721
722 static char l = LONG;
723
724 UNLESS(repr = PyObject_Repr(args))
725 goto finally;
726
727 if ((size = PyString_Size(repr)) < 0)
728 goto finally;
729
730 if ((*self->write_func)(self, &l, 1) < 0)
731 goto finally;
732
733 if ((*self->write_func)(self,
734 PyString_AS_STRING((PyStringObject *)repr), size) < 0)
735 goto finally;
736
737 if ((*self->write_func)(self, "\n", 1) < 0)
738 goto finally;
739
740 res = 0;
741
742finally:
743 Py_XDECREF(repr);
744
745 return res;
746}
747
748
749static int
750save_float(Picklerobject *self, PyObject *args) {
751 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
752
753#ifdef FORMAT_1_3
754 if (self->bin) {
755 int s, e, i = -1;
756 double f;
757 long fhi, flo;
758 char str[9], *p = str;
759
760 *p = BINFLOAT;
761 p++;
762
763 if (x < 0) {
764 s = 1;
765 x = -x;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000766 }
767 else
Guido van Rossum60456fd1997-04-09 17:36:32 +0000768 s = 0;
769
770 f = frexp(x, &e);
771
772 /* Normalize f to be in the range [1.0, 2.0) */
773 if (0.5 <= f && f < 1.0) {
774 f *= 2.0;
775 e--;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000776 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000777 else if (f == 0.0) {
778 e = 0;
779 }
780 else {
781 PyErr_SetString(PyExc_SystemError,
782 "frexp() result out of range");
783 return -1;
784 }
785
786 if (e >= 1024) {
787 /* XXX 1024 itself is reserved for Inf/NaN */
788 PyErr_SetString(PyExc_OverflowError,
789 "float too large to pack with d format");
790 return -1;
791 }
792 else if (e < -1022) {
793 /* Gradual underflow */
794 f = ldexp(f, 1022 + e);
795 e = 0;
796 }
797 else {
798 e += 1023;
799 f -= 1.0; /* Get rid of leading 1 */
800 }
801
802 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
803 f *= 268435456.0; /* 2**28 */
804 fhi = (long) floor(f); /* Truncate */
805 f -= (double)fhi;
806 f *= 16777216.0; /* 2**24 */
807 flo = (long) floor(f + 0.5); /* Round */
808
809 /* First byte */
810 *p = (s<<7) | (e>>4);
811 p++;
812
813 /* Second byte */
814 *p = ((e&0xF)<<4) | (fhi>>24);
815 p++;
816
817 /* Third byte */
818 *p = (fhi>>16) & 0xFF;
819 p++;
820
821 /* Fourth byte */
822 *p = (fhi>>8) & 0xFF;
823 p++;
824
825 /* Fifth byte */
826 *p = fhi & 0xFF;
827 p++;
828
829 /* Sixth byte */
830 *p = (flo>>16) & 0xFF;
831 p++;
832
833 /* Seventh byte */
834 *p = (flo>>8) & 0xFF;
835 p++;
836
837 /* Eighth byte */
838 *p = flo & 0xFF;
839
840 if ((*self->write_func)(self, str, 9) < 0)
841 return -1;
842 }
843 else
844#endif
845 {
846 char c_str[250];
847 c_str[0] = FLOAT;
848 sprintf(c_str + 1, "%f\n", x);
849
850 if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
851 return -1;
852 }
853
854 return 0;
855}
856
857
858static int
859save_string(Picklerobject *self, PyObject *args) {
860 int size, len;
861
862 size = PyString_Size(args);
863
864 if (!self->bin) {
865 PyObject *repr;
866 char *repr_str;
867
868 static char string = STRING;
869
870 UNLESS(repr = PyObject_Repr(args))
871 return -1;
872
873 repr_str = PyString_AS_STRING((PyStringObject *)repr);
874 len = PyString_Size(repr);
875
876 if ((*self->write_func)(self, &string, 1) < 0)
877 return -1;
878
879 if ((*self->write_func)(self, repr_str, len) < 0)
880 return -1;
881
882 if ((*self->write_func)(self, "\n", 1) < 0)
883 return -1;
884
885 Py_XDECREF(repr);
886 }
887 else {
888 int i;
889 char c_str[5];
890
891 size = PyString_Size(args);
892
893 if (size < 256) {
894 c_str[0] = SHORT_BINSTRING;
895 c_str[1] = size;
896 len = 2;
897 }
898 else {
899 c_str[0] = BINSTRING;
900 for (i = 1; i < 5; i++)
901 c_str[i] = (int)(size >> ((i - 1) * 8));
902 len = 5;
903 }
904
905 if ((*self->write_func)(self, c_str, len) < 0)
906 return -1;
907
908 if ((*self->write_func)(self,
909 PyString_AS_STRING((PyStringObject *)args), size) < 0)
910 return -1;
911 }
912
913 if (size > 1)
914 if (put(self, args) < 0)
915 return -1;
916
917 return 0;
918}
919
920
921static int
922save_tuple(Picklerobject *self, PyObject *args) {
923 PyObject *element = 0, *py_tuple_id = 0;
924 int len, i, has_key, res = -1;
925
926 static char tuple = TUPLE;
927
928 if ((*self->write_func)(self, &MARKv, 1) < 0)
929 goto finally;
930
931 if ((len = PyTuple_Size(args)) < 0)
932 goto finally;
933
934 for (i = 0; i < len; i++) {
935 UNLESS(element = PyTuple_GET_ITEM((PyTupleObject *)args, i))
936 goto finally;
937
938 if (save(self, element, 0) < 0)
939 goto finally;
940 }
941
942 UNLESS(py_tuple_id = PyInt_FromLong((long)args))
943 goto finally;
944
945 if (len) {
946 if (has_key = cPickle_PyMapping_HasKey(self->memo, py_tuple_id) < 0)
947 goto finally;
948
949 if (has_key) {
950 if (self->bin) {
951 static char pop_mark = POP_MARK;
952
953 if ((*self->write_func)(self, &pop_mark, 1) < 0)
954 goto finally;
955 }
956 else {
957 static char pop = POP;
958
959 for (i = 0; i <= len; i++) {
960 if ((*self->write_func)(self, &pop, 1) < 0)
961 goto finally;
962 }
963 }
964
965 if (get(self, py_tuple_id) < 0)
966 goto finally;
967
968 res = 0;
969 goto finally;
970 }
971 }
972
973 if ((*self->write_func)(self, &tuple, 1) < 0) {
974 goto finally;
975 }
976
977 if (put(self, args) < 0)
978 goto finally;
979
980 res = 0;
981
982finally:
983 Py_XDECREF(py_tuple_id);
984
985 return res;
986}
987
988static int
989save_empty_tuple(Picklerobject *self, PyObject *args) {
990 static char tuple = EMPTY_TUPLE;
991
992 return (*self->write_func)(self, &tuple, 1);
993}
994
995
996static int
997save_list(Picklerobject *self, PyObject *args) {
998 PyObject *element = 0;
999 int s_len, len, i, using_appends, res = -1;
1000 char s[3];
1001
1002 static char append = APPEND, appends = APPENDS;
1003
1004 if(self->bin) {
1005 s[0] = EMPTY_LIST;
1006 s_len = 1;
1007 }
1008 else {
1009 s[0] = MARK;
1010 s[1] = LIST;
1011 s_len = 2;
1012 }
1013
1014 if ((len = PyList_Size(args)) < 0)
1015 goto finally;
1016
1017 if ((*self->write_func)(self, s, s_len) < 0)
1018 goto finally;
1019
1020 if (len == 0)
1021 {
1022 if (put(self, args) < 0)
1023 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001024 }
1025 else
1026 {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001027 if (put2(self, args) < 0)
1028 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001029 }
1030
Guido van Rossum60456fd1997-04-09 17:36:32 +00001031 if (using_appends = (self->bin && (len > 1)))
1032 if ((*self->write_func)(self, &MARKv, 1) < 0)
1033 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001034
Guido van Rossum60456fd1997-04-09 17:36:32 +00001035 for (i = 0; i < len; i++) {
1036 UNLESS(element = PyList_GET_ITEM((PyListObject *)args, i))
1037 goto finally;
1038
1039 if (save(self, element, 0) < 0)
1040 goto finally;
1041
1042 if (!using_appends) {
1043 if ((*self->write_func)(self, &append, 1) < 0)
1044 goto finally;
1045 }
1046 }
1047
1048 if (using_appends) {
1049 if ((*self->write_func)(self, &appends, 1) < 0)
1050 goto finally;
1051 }
1052
1053 res = 0;
1054
1055finally:
1056
1057 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001058}
1059
1060
Guido van Rossum60456fd1997-04-09 17:36:32 +00001061static int
1062save_dict(Picklerobject *self, PyObject *args) {
1063 PyObject *key = 0, *value = 0;
1064 int i, len, res = -1, using_setitems;
1065 char s[3];
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001066
Guido van Rossum60456fd1997-04-09 17:36:32 +00001067 static char setitem = SETITEM, setitems = SETITEMS;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001068
Guido van Rossum60456fd1997-04-09 17:36:32 +00001069 if (self->bin) {
1070 s[0] = EMPTY_DICT;
1071 len = 1;
1072 }
1073 else {
1074 s[0] = MARK;
1075 s[1] = DICT;
1076 len = 2;
1077 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001078
Guido van Rossum60456fd1997-04-09 17:36:32 +00001079 if ((*self->write_func)(self, s, len) < 0)
1080 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001081
Guido van Rossum60456fd1997-04-09 17:36:32 +00001082 if ((len = PyDict_Size(args)) < 0)
1083 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001084
Guido van Rossum60456fd1997-04-09 17:36:32 +00001085 if (len == 0)
1086 {
1087 if (put(self, args) < 0)
1088 goto finally;
1089 }
1090 else
1091 {
1092 if (put2(self, args) < 0)
1093 goto finally;
1094 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001095
Guido van Rossum60456fd1997-04-09 17:36:32 +00001096 if (using_setitems = (self->bin && (PyDict_Size(args) > 1)))
1097 if ((*self->write_func)(self, &MARKv, 1) < 0)
1098 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001099
Guido van Rossum60456fd1997-04-09 17:36:32 +00001100 i = 0;
1101 while (PyDict_Next(args, &i, &key, &value)) {
1102 if (save(self, key, 0) < 0)
1103 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001104
Guido van Rossum60456fd1997-04-09 17:36:32 +00001105 if (save(self, value, 0) < 0)
1106 goto finally;
1107
1108 if (!using_setitems) {
1109 if ((*self->write_func)(self, &setitem, 1) < 0)
1110 goto finally;
1111 }
1112 }
1113
1114 if (using_setitems) {
1115 if ((*self->write_func)(self, &setitems, 1) < 0)
1116 goto finally;
1117 }
1118
1119 res = 0;
1120
1121finally:
1122
1123 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001124}
1125
1126
Guido van Rossum60456fd1997-04-09 17:36:32 +00001127static int
1128save_inst(Picklerobject *self, PyObject *args) {
1129 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1130 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1131 char *module_str, *name_str;
1132 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001133
Guido van Rossum60456fd1997-04-09 17:36:32 +00001134 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001135
Guido van Rossum60456fd1997-04-09 17:36:32 +00001136 if ((*self->write_func)(self, &MARKv, 1) < 0)
1137 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001138
Guido van Rossum60456fd1997-04-09 17:36:32 +00001139 UNLESS(class = PyObject_GetAttr(args, __class___str))
1140 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001141
Guido van Rossum60456fd1997-04-09 17:36:32 +00001142 if (self->bin) {
1143 if (save(self, class, 0) < 0)
1144 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001145 }
1146
Guido van Rossum60456fd1997-04-09 17:36:32 +00001147 if (getinitargs_func = PyObject_GetAttr(args, __getinitargs___str)) {
1148 PyObject *element = 0;
1149 int i, len;
1150
1151 UNLESS(class_args =
1152 PyObject_CallObject(getinitargs_func, empty_tuple))
1153 goto finally;
1154
1155 if ((len = PyObject_Length(class_args)) < 0)
1156 goto finally;
1157
1158 for (i = 0; i < len; i++) {
1159 UNLESS(element = PySequence_GetItem(class_args, i))
1160 goto finally;
1161
1162 if (save(self, element, 0) < 0) {
1163 Py_DECREF(element);
1164 goto finally;
1165 }
1166
1167 Py_DECREF(element);
1168 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001169 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001170 else {
1171 PyErr_Clear();
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001172 }
1173
Guido van Rossum60456fd1997-04-09 17:36:32 +00001174 if (!self->bin) {
1175 UNLESS(name = ((PyClassObject *)class)->cl_name) {
1176 PyErr_SetString(PicklingError, "class has no name");
1177 goto finally;
1178 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001179
Guido van Rossum60456fd1997-04-09 17:36:32 +00001180 UNLESS(module = whichmodule(class, name))
1181 goto finally;
1182
1183 module_str = PyString_AS_STRING((PyStringObject *)module);
1184 module_size = PyString_Size(module);
1185 name_str = PyString_AS_STRING((PyStringObject *)name);
1186 name_size = PyString_Size(name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001187
Guido van Rossum60456fd1997-04-09 17:36:32 +00001188 if ((*self->write_func)(self, &inst, 1) < 0)
1189 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001190
Guido van Rossum60456fd1997-04-09 17:36:32 +00001191 if ((*self->write_func)(self, module_str, module_size) < 0)
1192 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001193
Guido van Rossum60456fd1997-04-09 17:36:32 +00001194 if ((*self->write_func)(self, "\n", 1) < 0)
1195 goto finally;
1196
1197 if ((*self->write_func)(self, name_str, name_size) < 0)
1198 goto finally;
1199
1200 if ((*self->write_func)(self, "\n", 1) < 0)
1201 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001202 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001203 else if ((*self->write_func)(self, &obj, 1) < 0) {
1204 goto finally;
1205 }
1206
1207 if (getstate_func = PyObject_GetAttr(args, __getstate___str)) {
1208 UNLESS(state = PyObject_CallObject(getstate_func, empty_tuple))
1209 goto finally;
1210 }
1211 else {
1212 PyErr_Clear();
1213
1214 UNLESS(state = PyObject_GetAttr(args, __dict___str)) {
1215 PyErr_Clear();
1216 res = 0;
1217 goto finally;
1218 }
1219 }
1220
1221 if (!PyDict_Check(state)) {
1222 if (put2(self, args) < 0)
1223 goto finally;
1224 }
1225 else {
1226 if (put(self, args) < 0)
1227 goto finally;
1228 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001229
Guido van Rossum60456fd1997-04-09 17:36:32 +00001230 if (save(self, state, 0) < 0)
1231 goto finally;
1232
1233 if ((*self->write_func)(self, &build, 1) < 0)
1234 goto finally;
1235
1236 res = 0;
1237
1238finally:
1239 Py_XDECREF(module);
1240 Py_XDECREF(class);
1241 Py_XDECREF(state);
1242 Py_XDECREF(getinitargs_func);
1243 Py_XDECREF(getstate_func);
1244 Py_XDECREF(class_args);
1245
1246 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001247}
1248
1249
Guido van Rossum60456fd1997-04-09 17:36:32 +00001250static int
1251save_global(Picklerobject *self, PyObject *args, PyObject *name) {
1252 PyObject *global_name = 0, *module = 0;
1253 char *name_str, *module_str;
1254 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001255
Guido van Rossum60456fd1997-04-09 17:36:32 +00001256 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001257
Guido van Rossum60456fd1997-04-09 17:36:32 +00001258 if (name)
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001259 {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001260 global_name = name;
1261 Py_INCREF(global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001262 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001263 else
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001264 {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001265 UNLESS(global_name = PyObject_GetAttr(args, __name___str))
1266 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001267 }
1268
Guido van Rossum60456fd1997-04-09 17:36:32 +00001269 UNLESS(module = whichmodule(args, global_name))
1270 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001271
1272 module_str = PyString_AS_STRING((PyStringObject *)module);
1273 module_size = PyString_Size(module);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001274 name_str = PyString_AS_STRING((PyStringObject *)global_name);
1275 name_size = PyString_Size(global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001276
Guido van Rossum60456fd1997-04-09 17:36:32 +00001277 if ((*self->write_func)(self, &global, 1) < 0)
1278 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001279
Guido van Rossum60456fd1997-04-09 17:36:32 +00001280 if ((*self->write_func)(self, module_str, module_size) < 0)
1281 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001282
Guido van Rossum60456fd1997-04-09 17:36:32 +00001283 if ((*self->write_func)(self, "\n", 1) < 0)
1284 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001285
Guido van Rossum60456fd1997-04-09 17:36:32 +00001286 if ((*self->write_func)(self, name_str, name_size) < 0)
1287 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001288
Guido van Rossum60456fd1997-04-09 17:36:32 +00001289 if ((*self->write_func)(self, "\n", 1) < 0)
1290 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001291
Guido van Rossum60456fd1997-04-09 17:36:32 +00001292 if (put(self, args) < 0)
1293 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001294
Guido van Rossum60456fd1997-04-09 17:36:32 +00001295 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001296
Guido van Rossum60456fd1997-04-09 17:36:32 +00001297finally:
1298 Py_XDECREF(module);
1299 Py_XDECREF(global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001300
Guido van Rossum60456fd1997-04-09 17:36:32 +00001301 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001302}
1303
Guido van Rossum60456fd1997-04-09 17:36:32 +00001304static int
1305save_pers(Picklerobject *self, PyObject *args, PyObject *f) {
1306 PyObject *pid = 0;
1307 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001308
Guido van Rossum60456fd1997-04-09 17:36:32 +00001309 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001310
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001311 UNLESS(self->arg)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001312 UNLESS(self->arg = PyTuple_New(1))
1313 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001314
Guido van Rossum60456fd1997-04-09 17:36:32 +00001315 Py_INCREF(args);
1316 if (PyTuple_SetItem(self->arg, 0, args) < 0)
1317 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001318
Guido van Rossum60456fd1997-04-09 17:36:32 +00001319 UNLESS(pid = PyObject_CallObject(f, self->arg))
1320 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001321
Guido van Rossum60456fd1997-04-09 17:36:32 +00001322 if (pid != Py_None) {
1323 if (!self->bin) {
1324 if (!PyString_Check(pid)) {
1325 PyErr_SetString(PicklingError,
1326 "persistent id must be string");
1327 goto finally;
1328 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001329
Guido van Rossum60456fd1997-04-09 17:36:32 +00001330 if ((*self->write_func)(self, &persid, 1) < 0)
1331 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001332
Guido van Rossum60456fd1997-04-09 17:36:32 +00001333 if ((size = PyString_Size(pid)) < 0)
1334 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001335
Guido van Rossum60456fd1997-04-09 17:36:32 +00001336 if ((*self->write_func)(self,
1337 PyString_AS_STRING((PyStringObject *)pid), size) < 0)
1338 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001339
Guido van Rossum60456fd1997-04-09 17:36:32 +00001340 if ((*self->write_func)(self, "\n", 1) < 0)
1341 goto finally;
1342
1343 res = 1;
1344 goto finally;
1345 }
1346 else if (save(self, pid, 1) >= 0) {
1347 if ((*self->write_func)(self, &binpersid, 1) < 0)
1348 res = -1;
1349 else
1350 res = 1;
1351 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001352
Guido van Rossum60456fd1997-04-09 17:36:32 +00001353 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001354 }
1355
Guido van Rossum60456fd1997-04-09 17:36:32 +00001356 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001357
Guido van Rossum60456fd1997-04-09 17:36:32 +00001358finally:
1359 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001360
Guido van Rossum60456fd1997-04-09 17:36:32 +00001361 return res;
1362}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001363
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001364
Guido van Rossum60456fd1997-04-09 17:36:32 +00001365static int
1366save_reduce(Picklerobject *self, PyObject *callable,
1367 PyObject *tup, PyObject *state, PyObject *ob) {
1368 static char reduce = REDUCE, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001369
Guido van Rossum60456fd1997-04-09 17:36:32 +00001370 if (save(self, callable, 0) < 0)
1371 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001372
Guido van Rossum60456fd1997-04-09 17:36:32 +00001373 if (save(self, tup, 0) < 0)
1374 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001375
Guido van Rossum60456fd1997-04-09 17:36:32 +00001376 if ((*self->write_func)(self, &reduce, 1) < 0)
1377 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001378
Guido van Rossum60456fd1997-04-09 17:36:32 +00001379 if (ob != NULL)
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001380 {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001381 if (state && !PyDict_Check(state)) {
1382 if (put2(self, ob) < 0)
1383 return -1;
1384 }
1385 else {
1386 if (put(self, ob) < 0)
1387 return -1;
1388 }
1389 }
1390
1391 if (state)
1392 {
1393 if (save(self, state, 0) < 0)
1394 return -1;
1395
1396 if ((*self->write_func)(self, &build, 1) < 0)
1397 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001398 }
1399
Guido van Rossum60456fd1997-04-09 17:36:32 +00001400 return 0;
1401}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001402
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001403
Guido van Rossum60456fd1997-04-09 17:36:32 +00001404static int
1405save(Picklerobject *self, PyObject *args, int pers_save) {
1406 PyTypeObject *type;
1407 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0, *arg_tup = 0,
1408 *callable = 0, *state = 0, *junk = 0;
1409 int res = -1, tmp, size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001410
Guido van Rossum60456fd1997-04-09 17:36:32 +00001411 if (!pers_save && self->pers_func) {
1412 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
1413 res = tmp;
1414 goto finally;
1415 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001416 }
1417
Guido van Rossum60456fd1997-04-09 17:36:32 +00001418 if (args == Py_None) {
1419 res = save_none(self, args);
1420 goto finally;
1421 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001422
Guido van Rossum60456fd1997-04-09 17:36:32 +00001423 type = args->ob_type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001424
Guido van Rossum60456fd1997-04-09 17:36:32 +00001425 switch (type->tp_name[0]) {
1426 case 'i':
1427 if (type == &PyInt_Type) {
1428 res = save_int(self, args);
1429 goto finally;
1430 }
1431 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001432
Guido van Rossum60456fd1997-04-09 17:36:32 +00001433 case 'l':
1434 if (type == &PyLong_Type) {
1435 res = save_long(self, args);
1436 goto finally;
1437 }
1438 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001439
Guido van Rossum60456fd1997-04-09 17:36:32 +00001440 case 'f':
1441 if (type == &PyFloat_Type) {
1442 res = save_float(self, args);
1443 goto finally;
1444 }
1445 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001446
Guido van Rossum60456fd1997-04-09 17:36:32 +00001447 case 't':
1448 if (type == &PyTuple_Type && PyTuple_Size(args)==0) {
1449 if(self->bin) res = save_empty_tuple(self, args);
1450 else res = save_tuple(self, args);
1451 goto finally;
1452 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001453
Guido van Rossum60456fd1997-04-09 17:36:32 +00001454 case 's':
1455 if ((type == &PyString_Type) && (PyString_Size(args) < 2)) {
1456 res = save_string(self, args);
1457 goto finally;
1458 }
1459 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001460
Guido van Rossum60456fd1997-04-09 17:36:32 +00001461 if (args->ob_refcnt > 1) {
1462 long ob_id;
1463 int has_key;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001464
Guido van Rossum60456fd1997-04-09 17:36:32 +00001465 ob_id = (long)args;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001466
Guido van Rossum60456fd1997-04-09 17:36:32 +00001467 UNLESS(py_ob_id = PyInt_FromLong(ob_id))
1468 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001469
Guido van Rossum60456fd1997-04-09 17:36:32 +00001470 if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_ob_id)) < 0)
1471 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001472
Guido van Rossum60456fd1997-04-09 17:36:32 +00001473 if (has_key) {
1474 if (get(self, py_ob_id) < 0)
1475 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001476
Guido van Rossum60456fd1997-04-09 17:36:32 +00001477 res = 0;
1478 goto finally;
1479 }
1480 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001481
Guido van Rossum60456fd1997-04-09 17:36:32 +00001482 switch (type->tp_name[0]) {
1483 case 's':
1484 if (type == &PyString_Type) {
1485 res = save_string(self, args);
1486 goto finally;
1487 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001488
Guido van Rossum60456fd1997-04-09 17:36:32 +00001489 case 't':
1490 if (type == &PyTuple_Type) {
1491 res = save_tuple(self, args);
1492 goto finally;
1493 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001494
Guido van Rossum60456fd1997-04-09 17:36:32 +00001495 case 'l':
1496 if (type == &PyList_Type) {
1497 res = save_list(self, args);
1498 goto finally;
1499 }
1500
1501 case 'd':
1502 if (type == &PyDict_Type) {
1503 res = save_dict(self, args);
1504 goto finally;
1505 }
1506
1507 case 'i':
1508 if (type == &PyInstance_Type) {
1509 res = save_inst(self, args);
1510 goto finally;
1511 }
1512
1513 case 'c':
1514 if (type == &PyClass_Type) {
1515 res = save_global(self, args, NULL);
1516 goto finally;
1517 }
1518
1519 case 'f':
1520 if (type == &PyFunction_Type) {
1521 res = save_global(self, args, NULL);
1522 goto finally;
1523 }
1524
1525 case 'b':
1526 if (type == &PyCFunction_Type) {
1527 res = save_global(self, args, NULL);
1528 goto finally;
1529 }
1530 }
1531
1532 if (!pers_save && self->inst_pers_func) {
1533 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
1534 res = tmp;
1535 goto finally;
1536 }
1537 }
1538
1539 if (__reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type)) {
1540 Py_INCREF(__reduce__);
1541
1542 UNLESS(self->arg)
1543 UNLESS(self->arg = PyTuple_New(1))
1544 goto finally;
1545
1546 Py_INCREF(args);
1547 if (PyTuple_SetItem(self->arg, 0, args) < 0)
1548 goto finally;
1549
1550 UNLESS(t = PyObject_CallObject(__reduce__, self->arg))
1551 goto finally;
1552 }
1553 else {
1554 PyErr_Clear();
1555
1556 if (__reduce__ = PyObject_GetAttr(args, __reduce___str)) {
1557 UNLESS(t = PyObject_CallObject(__reduce__, empty_tuple))
1558 goto finally;
1559 }
1560 else {
1561 PyErr_Clear();
1562 }
1563 }
1564
1565 if (t) {
1566 if (PyString_Check(t)) {
1567 res = save_global(self, args, t);
1568 goto finally;
1569 }
1570
1571 if (!PyTuple_Check(t)) {
1572 PyErr_Format(PicklingError, "Value returned by %s must "
1573 "be a tuple", "O", __reduce__);
1574 goto finally;
1575 }
1576
1577 size = PyTuple_Size(t);
1578
1579 if ((size != 3) && (size != 2)) {
1580 PyErr_Format(PicklingError, "tuple returned by %s must "
1581 "contain only two or three elements", "O", __reduce__);
1582 goto finally;
1583 }
1584
1585 callable = PyTuple_GET_ITEM(t, 0);
1586 arg_tup = PyTuple_GET_ITEM(t, 1);
1587
1588 if (size > 2) {
1589 state = PyTuple_GET_ITEM(t, 2);
1590 }
1591
1592 UNLESS(PyTuple_Check(arg_tup)) {
1593 PyErr_Format(PicklingError, "Second element of tuple "
1594 "returned by %s must be a tuple", "O", __reduce__);
1595 goto finally;
1596 }
1597
1598 res = save_reduce(self, callable, arg_tup, state, args);
1599 goto finally;
1600 }
1601
1602 /*
1603 if (PyObject_HasAttrString(args, "__class__")) {
1604 res = save_inst(self, args);
1605 goto finally;
1606 }
1607 */
1608
1609 PyErr_Format(PicklingError, "Cannot pickle %s objects.",
1610 "O", (PyObject *)type);
1611
1612finally:
1613 Py_XDECREF(py_ob_id);
1614 Py_XDECREF(__reduce__);
1615 Py_XDECREF(t);
1616
1617 return res;
1618}
1619
1620
1621static int
1622dump(Picklerobject *self, PyObject *args) {
1623 static char stop = STOP;
1624
1625 if (save(self, args, 0) < 0)
1626 return -1;
1627
1628 if ((*self->write_func)(self, &stop, 1) < 0)
1629 return -1;
1630
1631 if ((*self->write_func)(self, NULL, 0) < 0)
1632 return -1;
1633
1634 return 0;
1635}
1636
1637static PyObject *
1638Pickler_dump(Picklerobject *self, PyObject *args) {
1639 PyObject *ob;
1640
1641 UNLESS(PyArg_ParseTuple(args, "O", &ob))
1642 return NULL;
1643
1644 if (dump(self, ob) < 0)
1645 return NULL;
1646
1647 Py_INCREF(Py_None);
1648 return Py_None;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001649}
1650
1651
1652static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00001653dump_special(Picklerobject *self, PyObject *args) {
1654 static char stop = STOP;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001655
Guido van Rossum60456fd1997-04-09 17:36:32 +00001656 PyObject *callable, *arg_tup, *state = NULL;
1657
1658 UNLESS(PyArg_ParseTuple(args, "OO|O", &callable, &arg_tup, &state))
1659 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001660
Guido van Rossum60456fd1997-04-09 17:36:32 +00001661 UNLESS(PyTuple_Check(arg_tup)) {
1662 PyErr_SetString(PicklingError, "Second arg to dump_special must "
1663 "be tuple");
1664 return NULL;
1665 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001666
Guido van Rossum60456fd1997-04-09 17:36:32 +00001667 if (save_reduce(self, callable, arg_tup, state, NULL) < 0)
1668 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001669
Guido van Rossum60456fd1997-04-09 17:36:32 +00001670 if ((*self->write_func)(self, &stop, 1) < 0)
1671 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001672
Guido van Rossum60456fd1997-04-09 17:36:32 +00001673 if ((*self->write_func)(self, NULL, 0) < 0)
1674 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001675
Guido van Rossum60456fd1997-04-09 17:36:32 +00001676 Py_INCREF(Py_None);
1677 return Py_None;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001678}
1679
1680
1681static struct PyMethodDef Pickler_methods[] = {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001682 {"dump", (PyCFunction)Pickler_dump, 1, ""},
1683 {"dump_special", (PyCFunction)dump_special, 1, ""},
1684 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001685};
1686
1687
1688static Picklerobject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00001689newPicklerobject(PyObject *file, int bin) {
1690 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001691
Guido van Rossum60456fd1997-04-09 17:36:32 +00001692 UNLESS(self = PyObject_NEW(Picklerobject, &Picklertype))
1693 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001694
1695 self->fp = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001696 self->write = NULL;
1697 self->memo = NULL;
1698 self->arg = NULL;
1699 self->pers_func = NULL;
1700 self->inst_pers_func = NULL;
1701 self->write_buf = NULL;
1702 self->bin = bin;
1703 self->buf_size = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001704
Guido van Rossum60456fd1997-04-09 17:36:32 +00001705 Py_INCREF(file);
1706 self->file = file;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001707
Guido van Rossum60456fd1997-04-09 17:36:32 +00001708 UNLESS(self->memo = PyDict_New()) {
1709 Py_XDECREF((PyObject *)self);
1710 return NULL;
1711 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001712
Guido van Rossum60456fd1997-04-09 17:36:32 +00001713 if (PyFile_Check(file)) {
1714 self->fp = PyFile_AsFile(file);
1715 self->write_func = write_file;
1716 }
1717 else if (PycStringIO_OutputCheck(file)) {
1718 self->write_func = write_cStringIO;
1719 }
1720 else {
1721 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001722
Guido van Rossum60456fd1997-04-09 17:36:32 +00001723 UNLESS(self->write = PyObject_GetAttr(file, write_str))
1724 {
1725 PyErr_Clear();
1726 PyErr_SetString(PyExc_TypeError, "argument must have 'write' "
1727 "attribute");
1728 Py_XDECREF((PyObject *)self);
1729 return NULL;
1730 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001731
Guido van Rossum60456fd1997-04-09 17:36:32 +00001732 UNLESS(self->write_buf =
1733 (char *)malloc(WRITE_BUF_SIZE * sizeof(char))) {
1734 PyErr_NoMemory();
1735 Py_XDECREF((PyObject *)self);
1736 return NULL;
1737 }
1738 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001739
Guido van Rossum60456fd1997-04-09 17:36:32 +00001740 return self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001741}
1742
1743
1744static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00001745get_Pickler(PyObject *self, PyObject *args) {
1746 PyObject *file;
1747 int bin = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001748
Guido van Rossum60456fd1997-04-09 17:36:32 +00001749 UNLESS(PyArg_ParseTuple(args, "O|i", &file, &bin)) return NULL;
1750 return (PyObject *)newPicklerobject(file, bin);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001751}
1752
1753
1754static void
Guido van Rossum60456fd1997-04-09 17:36:32 +00001755Pickler_dealloc(Picklerobject *self) {
1756 Py_XDECREF(self->write);
1757 Py_XDECREF(self->memo);
1758 Py_XDECREF(self->arg);
1759 Py_XDECREF(self->file);
1760 Py_XDECREF(self->pers_func);
1761 Py_XDECREF(self->inst_pers_func);
1762
1763 if (self->write_buf) {
1764 free(self->write_buf);
1765 }
1766
1767 PyMem_DEL(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001768}
1769
1770
1771static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00001772Pickler_getattr(Picklerobject *self, char *name) {
1773 if (strcmp(name, "persistent_id") == 0) {
1774 if (!self->pers_func) {
1775 PyErr_SetString(PyExc_AttributeError, name);
1776 return NULL;
1777 }
1778
1779 Py_INCREF(self->pers_func);
1780 return self->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001781 }
1782
Guido van Rossum60456fd1997-04-09 17:36:32 +00001783 if (strcmp(name, "memo") == 0) {
1784 if (!self->memo) {
1785 PyErr_SetString(PyExc_AttributeError, name);
1786 return NULL;
1787 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001788
Guido van Rossum60456fd1997-04-09 17:36:32 +00001789 Py_INCREF(self->memo);
1790 return self->memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001791 }
1792
Guido van Rossum60456fd1997-04-09 17:36:32 +00001793 if (strcmp(name, "PicklingError") == 0) {
1794 Py_INCREF(PicklingError);
1795 return PicklingError;
1796 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001797
Guido van Rossum60456fd1997-04-09 17:36:32 +00001798 return Py_FindMethod(Pickler_methods, (PyObject *)self, name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001799}
1800
1801
1802int
Guido van Rossum60456fd1997-04-09 17:36:32 +00001803Pickler_setattr(Picklerobject *self, char *name, PyObject *value) {
1804 if (strcmp(name, "persistent_id") == 0) {
1805 Py_XDECREF(self->pers_func);
1806 self->pers_func = value;
1807 Py_INCREF(value);
1808 return 0;
1809 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001810
Guido van Rossum60456fd1997-04-09 17:36:32 +00001811 if (strcmp(name, "inst_persistent_id") == 0) {
1812 Py_XDECREF(self->inst_pers_func);
1813 self->inst_pers_func = value;
1814 Py_INCREF(value);
1815 return 0;
1816 }
1817
1818 PyErr_SetString(PyExc_AttributeError, name);
1819 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001820}
1821
1822
1823static char Picklertype__doc__[] = "";
1824
Guido van Rossum60456fd1997-04-09 17:36:32 +00001825static PyTypeObject Picklertype_value() {
1826 PyTypeObject Picklertype = {
1827 PyObject_HEAD_INIT(&PyType_Type)
1828 0, /*ob_size*/
1829 "Pickler", /*tp_name*/
1830 sizeof(Picklerobject), /*tp_basicsize*/
1831 0, /*tp_itemsize*/
1832 /* methods */
1833 (destructor)Pickler_dealloc, /*tp_dealloc*/
1834 (printfunc)0, /*tp_print*/
1835 (getattrfunc)Pickler_getattr, /*tp_getattr*/
1836 (setattrfunc)Pickler_setattr, /*tp_setattr*/
1837 (cmpfunc)0, /*tp_compare*/
1838 (reprfunc)0, /*tp_repr*/
1839 0, /*tp_as_number*/
1840 0, /*tp_as_sequence*/
1841 0, /*tp_as_mapping*/
1842 (hashfunc)0, /*tp_hash*/
1843 (ternaryfunc)0, /*tp_call*/
1844 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001845
Guido van Rossum60456fd1997-04-09 17:36:32 +00001846 /* Space for future expansion */
1847 0L,0L,0L,0L,
1848 Picklertype__doc__ /* Documentation string */
1849 };
1850 return Picklertype;
1851}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001852
1853
Guido van Rossum60456fd1997-04-09 17:36:32 +00001854PyObject *
1855PyImport_ImportModuleNi(char *module_name)
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001856{
Guido van Rossum60456fd1997-04-09 17:36:32 +00001857 char *import_str;
1858 int size, i;
1859 PyObject *import;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001860
Guido van Rossum60456fd1997-04-09 17:36:32 +00001861 static PyObject *eval_dict = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001862
Guido van Rossum60456fd1997-04-09 17:36:32 +00001863 size = strlen(module_name);
1864 for (i = 0; i < size; i++) {
1865 if (((module_name[i] < 'A') || (module_name[i] > 'z')) &&
1866 (module_name[i] != '_')) {
1867 PyErr_SetString(PyExc_ImportError, "module name contains "
1868 "invalid characters.");
1869 return NULL;
1870 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001871 }
1872
Guido van Rossum60456fd1997-04-09 17:36:32 +00001873 UNLESS(import_str =
1874 (char *)malloc((strlen(module_name) + 15) * sizeof(char))) {
1875 PyErr_NoMemory();
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001876 return NULL;
1877 }
1878
Guido van Rossum60456fd1997-04-09 17:36:32 +00001879 sprintf(import_str, "__import__('%s')", module_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001880
Guido van Rossum60456fd1997-04-09 17:36:32 +00001881 UNLESS(eval_dict)
1882 UNLESS(eval_dict = Py_BuildValue("{sO}", "__builtins__", builtins))
1883 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001884
Guido van Rossum60456fd1997-04-09 17:36:32 +00001885 if (!(import =
1886 PyRun_String(import_str, eval_input, eval_dict, eval_dict))) {
1887 free(import_str);
1888 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001889 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001890
Guido van Rossum60456fd1997-04-09 17:36:32 +00001891 free(import_str);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001892
Guido van Rossum60456fd1997-04-09 17:36:32 +00001893 return import;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001894}
1895
1896
1897static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00001898find_class(PyObject *py_module_name, PyObject *py_class_name) {
1899 PyObject *import = 0, *class = 0, *t = 0;
1900 char *module_name, *class_name;
1901 PyObject *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001902
Guido van Rossum60456fd1997-04-09 17:36:32 +00001903 static PyObject *eval_dict = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001904
Guido van Rossum60456fd1997-04-09 17:36:32 +00001905 module_name = PyString_AS_STRING((PyStringObject *)py_module_name);
1906 class_name = PyString_AS_STRING((PyStringObject *)py_class_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001907
Guido van Rossum60456fd1997-04-09 17:36:32 +00001908 UNLESS(t = PyTuple_New(2))
1909 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001910
Guido van Rossum60456fd1997-04-09 17:36:32 +00001911 PyTuple_SET_ITEM((PyTupleObject *)t, 0, py_module_name);
1912 Py_INCREF(py_module_name);
1913
1914 PyTuple_SET_ITEM((PyTupleObject *)t, 1, py_class_name);
1915 Py_INCREF(py_class_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001916
Guido van Rossum60456fd1997-04-09 17:36:32 +00001917 if (class = PyDict_GetItem(class_map, t)) {
1918 res = class;
1919 Py_INCREF(class);
1920 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001921 }
1922
Guido van Rossum60456fd1997-04-09 17:36:32 +00001923 PyErr_Clear();
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001924
Guido van Rossum60456fd1997-04-09 17:36:32 +00001925 if (!(import = PyImport_ImportModuleNi(module_name)) ||
1926 !(class = PyObject_GetAttr(import, py_class_name))) {
1927 PyErr_Format(PyExc_SystemError, "Failed to import global %s "
1928 "from module %s", "ss", class_name, module_name);
1929 goto finally;
1930 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001931
Guido van Rossum60456fd1997-04-09 17:36:32 +00001932 if (PyDict_SetItem(class_map, t, class) < 0)
1933 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001934
Guido van Rossum60456fd1997-04-09 17:36:32 +00001935 res = class;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001936
Guido van Rossum60456fd1997-04-09 17:36:32 +00001937finally:
1938 Py_XDECREF(import);
1939 Py_XDECREF(t);
1940
1941 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001942}
1943
1944
1945static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00001946marker(Unpicklerobject *self) {
1947 if (self->num_marks < 1)
1948 {
1949 PyErr_SetString(UnpicklingError, "could not find MARK");
1950 return -1;
1951 }
1952
1953 return self->marks[--self->num_marks];
1954}
1955
1956
1957static int
1958load_none(Unpicklerobject *self) {
1959 if (PyList_Append(self->stack, Py_None) < 0)
1960 return -1;
1961
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001962 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001963}
1964
1965
1966static int
1967load_int(Unpicklerobject *self) {
1968 PyObject *py_int = 0;
1969 char *endptr, *s;
1970 int len, res = -1;
1971 long l;
1972
1973 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
1974 UNLESS(s=strndup(s,len)) return -1;
1975
1976 errno = 0;
1977 l = strtol(s, &endptr, 0);
1978
1979 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
1980 /* Hm, maybe we've got something long. Let's try reading
1981 it as a Python long object. */
1982 errno=0;
1983 UNLESS(py_int=PyLong_FromString(s,&endptr,0)) goto finally;
1984
1985 if ((*endptr != '\n') || (endptr[1] != '\0')) {
1986 PyErr_SetString(PyExc_ValueError,
1987 "could not convert string to int");
1988 goto finally;
1989 }
1990 }
1991 else {
1992 UNLESS(py_int = PyInt_FromLong(l)) goto finally;
1993 }
1994
1995 if (PyList_Append(self->stack, py_int) < 0) goto finally;
1996
1997 res = 0;
1998
1999finally:
2000 free(s);
2001 Py_XDECREF(py_int);
2002
2003 return res;
2004}
2005
2006
2007static long
2008calc_binint(char *s, int x) {
2009 unsigned char c;
2010 int i;
2011 long l;
2012
2013 for (i = 0, l = 0L; i < x; i++) {
2014 c = (unsigned char)s[i];
2015 l |= (long)c << (i * 8);
2016 }
2017
2018 return l;
2019}
2020
2021
2022static int
2023load_binintx(Unpicklerobject *self, char *s, int x) {
2024 PyObject *py_int = 0;
2025 long l;
2026
2027 l = calc_binint(s, x);
2028
2029 UNLESS(py_int = PyInt_FromLong(l))
2030 return -1;
2031
2032 if (PyList_Append(self->stack, py_int) < 0) {
2033 Py_DECREF(py_int);
2034 return -1;
2035 }
2036
2037 Py_DECREF(py_int);
2038
2039 return 0;
2040}
2041
2042
2043static int
2044load_binint(Unpicklerobject *self) {
2045 char *s;
2046
2047 if ((*self->read_func)(self, &s, 4) < 0)
2048 return -1;
2049
2050 return load_binintx(self, s, 4);
2051}
2052
2053
2054static int
2055load_binint1(Unpicklerobject *self) {
2056 char *s;
2057
2058 if ((*self->read_func)(self, &s, 1) < 0)
2059 return -1;
2060
2061 return load_binintx(self, s, 1);
2062}
2063
2064
2065static int
2066load_binint2(Unpicklerobject *self) {
2067 char *s;
2068
2069 if ((*self->read_func)(self, &s, 2) < 0)
2070 return -1;
2071
2072 return load_binintx(self, s, 2);
2073}
2074
2075static int
2076load_long(Unpicklerobject *self) {
2077 PyObject *l = 0;
2078 char *end, *s;
2079 int len, res = -1;
2080
2081 static PyObject *arg = 0;
2082
2083 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
2084 UNLESS(s=strndup(s,len)) return -1;
2085
2086 UNLESS(l = PyLong_FromString(s, &end, 0))
2087 goto finally;
2088
2089 if (PyList_Append(self->stack, l) < 0)
2090 goto finally;
2091
2092 res = 0;
2093
2094finally:
2095 free(s);
2096 Py_XDECREF(l);
2097
2098 return res;
2099}
2100
2101
2102static int
2103load_float(Unpicklerobject *self) {
2104 PyObject *py_float = 0;
2105 char *endptr, *s;
2106 int len, res = -1;
2107 double d;
2108
2109 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
2110 UNLESS(s=strndup(s,len)) return -1;
2111
2112 errno = 0;
2113 d = strtod(s, &endptr);
2114
2115 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
2116 PyErr_SetString(PyExc_ValueError,
2117 "could not convert string to float");
2118 goto finally;
2119 }
2120
2121 UNLESS(py_float = PyFloat_FromDouble(d))
2122 goto finally;
2123
2124 if (PyList_Append(self->stack, py_float) < 0)
2125 goto finally;
2126
2127 res = 0;
2128
2129finally:
2130 free(s);
2131 Py_XDECREF(py_float);
2132
2133 return res;
2134}
2135
2136#ifdef FORMAT_1_3
2137static int
2138load_binfloat(Unpicklerobject *self) {
2139 PyObject *py_float = 0;
2140 int s, e, res = -1;
2141 long fhi, flo;
2142 double x;
2143 char *p;
2144
2145 if ((*self->read_func)(self, &p, 8) < 0)
2146 return -1;
2147
2148 /* First byte */
2149 s = (*p>>7) & 1;
2150 e = (*p & 0x7F) << 4;
2151 p++;
2152
2153 /* Second byte */
2154 e |= (*p>>4) & 0xF;
2155 fhi = (*p & 0xF) << 24;
2156 p++;
2157
2158 /* Third byte */
2159 fhi |= (*p & 0xFF) << 16;
2160 p++;
2161
2162 /* Fourth byte */
2163 fhi |= (*p & 0xFF) << 8;
2164 p++;
2165
2166 /* Fifth byte */
2167 fhi |= *p & 0xFF;
2168 p++;
2169
2170 /* Sixth byte */
2171 flo = (*p & 0xFF) << 16;
2172 p++;
2173
2174 /* Seventh byte */
2175 flo |= (*p & 0xFF) << 8;
2176 p++;
2177
2178 /* Eighth byte */
2179 flo |= *p & 0xFF;
2180
2181 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
2182 x /= 268435456.0; /* 2**28 */
2183
2184 /* XXX This sadly ignores Inf/NaN */
2185 if (e == 0)
2186 e = -1022;
2187 else {
2188 x += 1.0;
2189 e -= 1023;
2190 }
2191 x = ldexp(x, e);
2192
2193 if (s)
2194 x = -x;
2195
2196 UNLESS(py_float = PyFloat_FromDouble(x))
2197 goto finally;
2198
2199 if (PyList_Append(self->stack, py_float) < 0)
2200 goto finally;
2201
2202 res = 0;
2203
2204finally:
2205 Py_XDECREF(py_float);
2206
2207 return res;
2208}
2209#endif
2210
2211static int
2212load_string(Unpicklerobject *self) {
2213 PyObject *str = 0;
2214 int len, res = -1;
2215 char *s;
2216
2217 static PyObject *eval_dict = 0;
2218
2219 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
2220 UNLESS(s=strndup(s,len)) return -1;
2221
2222 UNLESS(eval_dict)
2223 UNLESS(eval_dict = Py_BuildValue("{s{}}", "__builtins__"))
2224 goto finally;
2225
2226 UNLESS(str = PyRun_String(s, eval_input, eval_dict, eval_dict))
2227 goto finally;
2228
2229 if (PyList_Append(self->stack, str) < 0)
2230 goto finally;
2231
2232 res = 0;
2233
2234finally:
2235 free(s);
2236 Py_XDECREF(str);
2237
2238 return res;
2239}
2240
2241
2242static int
2243load_binstring(Unpicklerobject *self) {
2244 PyObject *py_string = 0;
2245 long l;
2246 int res = -1;
2247 char *s;
2248
2249 if ((*self->read_func)(self, &s, 4) < 0)
2250 goto finally;
2251
2252 l = calc_binint(s, 4);
2253
2254 if ((*self->read_func)(self, &s, l) < 0)
2255 goto finally;
2256
2257 UNLESS(py_string = PyString_FromStringAndSize(s, l))
2258 goto finally;
2259
2260 if (PyList_Append(self->stack, py_string) < 0)
2261 goto finally;
2262
2263 res = 0;
2264
2265finally:
2266 Py_XDECREF(py_string);
2267
2268 return res;
2269}
2270
2271
2272static int
2273load_short_binstring(Unpicklerobject *self) {
2274 PyObject *py_string = 0;
2275 unsigned char l;
2276 int res = -1;
2277 char *s;
2278
2279 if ((*self->read_func)(self, &s, 1) < 0)
2280 return -1;
2281
2282 l = (unsigned char)s[0];
2283
2284 if ((*self->read_func)(self, &s, l) < 0)
2285 goto finally;
2286
2287 UNLESS(py_string = PyString_FromStringAndSize(s, l))
2288 goto finally;
2289
2290 if (PyList_Append(self->stack, py_string) < 0)
2291 goto finally;
2292
2293 res = 0;
2294
2295finally:
2296 Py_XDECREF(py_string);
2297
2298 return res;
2299}
2300
2301
2302static int
2303load_tuple(Unpicklerobject *self) {
2304 PyObject *tup = 0, *slice = 0, *list = 0;
2305 int i, j, res = -1;
2306
2307 if ((i = marker(self)) < 0)
2308 goto finally;
2309
2310 if ((j = PyList_Size(self->stack)) < 0)
2311 goto finally;
2312
2313 UNLESS(slice = PyList_GetSlice(self->stack, i, j))
2314 goto finally;
2315
2316 UNLESS(tup = PySequence_Tuple(slice))
2317 goto finally;
2318
2319 UNLESS(list = PyList_New(1))
2320 goto finally;
2321
2322 Py_INCREF(tup);
2323 if (PyList_SetItem(list, 0, tup) < 0)
2324 goto finally;
2325
2326 if (PyList_SetSlice(self->stack, i, j, list) < 0)
2327 goto finally;
2328
2329 res = 0;
2330
2331finally:
2332 Py_XDECREF(tup);
2333 Py_XDECREF(list);
2334 Py_XDECREF(slice);
2335
2336 return res;
2337}
2338
2339static int
2340load_empty_tuple(Unpicklerobject *self) {
2341 PyObject *tup = 0;
2342 int res;
2343
2344 UNLESS(tup=PyTuple_New(0)) return -1;
2345 res=PyList_Append(self->stack, tup);
2346 Py_DECREF(tup);
2347 return res;
2348}
2349
2350static int
2351load_empty_list(Unpicklerobject *self) {
2352 PyObject *list = 0;
2353 int res;
2354
2355 UNLESS(list=PyList_New(0)) return -1;
2356 res=PyList_Append(self->stack, list);
2357 Py_DECREF(list);
2358 return res;
2359}
2360
2361static int
2362load_empty_dict(Unpicklerobject *self) {
2363 PyObject *dict = 0;
2364 int res;
2365
2366 UNLESS(dict=PyDict_New()) return -1;
2367 res=PyList_Append(self->stack, dict);
2368 Py_DECREF(dict);
2369 return res;
2370}
2371
2372
2373static int
2374load_list(Unpicklerobject *self) {
2375 PyObject *list = 0, *slice = 0;
2376 int i, j, l, res = -1;
2377
2378 if ((i = marker(self)) < 0)
2379 goto finally;
2380
2381 if ((j = PyList_Size(self->stack)) < 0)
2382 goto finally;
2383
2384 UNLESS(slice = PyList_GetSlice(self->stack, i, j))
2385 goto finally;
2386
2387 if((l=PyList_Size(slice)) < 0)
2388 goto finally;
2389
2390 if(l) {
2391 UNLESS(list = PyList_New(1))
2392 goto finally;
2393
2394 Py_INCREF(slice);
2395 if (PyList_SetItem(list, 0, slice) < 0)
2396 goto finally;
2397
2398 if (PyList_SetSlice(self->stack, i, j, list) < 0)
2399 goto finally;
2400 } else {
2401 if(PyList_Append(self->stack,slice) < 0)
2402 goto finally;
2403 }
2404
2405 res = 0;
2406
2407finally:
2408 Py_XDECREF(list);
2409 Py_XDECREF(slice);
2410
2411 return res;
2412}
2413
2414static int
2415load_dict(Unpicklerobject *self) {
2416 PyObject *list = 0, *dict = 0, *key = 0, *value = 0;
2417 int i, j, k, res = -1;
2418
2419 if ((i = marker(self)) < 0)
2420 goto finally;
2421
2422 if ((j = PyList_Size(self->stack)) < 0)
2423 goto finally;
2424
2425 UNLESS(dict = PyDict_New())
2426 goto finally;
2427
2428 for (k = i; k < j; k += 2) {
2429 UNLESS(key = PyList_GET_ITEM((PyListObject *)self->stack, k))
2430 goto finally;
2431
2432 UNLESS(value = PyList_GET_ITEM((PyListObject *)self->stack, k + 1))
2433 goto finally;
2434
2435 if (PyDict_SetItem(dict, key, value) < 0)
2436 goto finally;
2437 }
2438
2439 if(j) {
2440
2441 UNLESS(list = PyList_New(1))
2442 goto finally;
2443
2444 Py_INCREF(dict);
2445 if (PyList_SetItem(list, 0, dict) < 0)
2446 goto finally;
2447
2448 if (PyList_SetSlice(self->stack, i, j, list) < 0)
2449 goto finally;
2450 }
2451 else
2452 if(PyList_Append(self->stack, dict) < 0)
2453 goto finally;
2454
2455 res = 0;
2456
2457finally:
2458 Py_XDECREF(dict);
2459 Py_XDECREF(list);
2460
2461 return res;
2462}
2463
2464static PyObject *
2465Instance_New(PyObject *cls, PyObject *args)
2466{
2467 int has_key;
2468 PyObject *safe=0, *r=0;
2469
2470 if (PyClass_Check(cls))
2471 if(r=PyInstance_New(cls, args, NULL)) return r;
2472 else goto err;
2473
2474
2475 if ((has_key = cPickle_PyMapping_HasKey(safe_constructors, cls)) < 0)
2476 goto err;
2477
2478 if (!has_key)
2479 if(!(safe = PyObject_GetAttr(cls, __safe_for_unpickling___str)) ||
2480 !PyObject_IsTrue(safe)) {
2481 PyErr_Format(UnpicklingError, "%s is not safe for unpickling", "O", cls);
2482 Py_XDECREF(safe);
2483 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002484 }
2485
Guido van Rossum60456fd1997-04-09 17:36:32 +00002486 if(r=PyObject_CallObject(cls, args)) return r;
2487err:
2488 {
2489 PyObject *tp, *v, *tb;
2490
2491 PyErr_Fetch(&tp, &v, &tb);
2492 if(r=Py_BuildValue("OOO",v,cls,args))
2493 {
2494 Py_XDECREF(v);
2495 v=r;
2496 }
2497 PyErr_Restore(tp,v,tb);
2498 }
2499 return NULL;
2500}
2501
2502
2503static int
2504load_obj(Unpicklerobject *self) {
2505 PyObject *class = 0, *slice = 0, *tup = 0, *obj = 0;
2506 int i, len, res = -1;
2507
2508 if ((i = marker(self)) < 0)
2509 goto finally;
2510
2511 class = PyList_GET_ITEM((PyListObject *)self->stack, i);
2512 Py_INCREF(class);
2513
2514 if ((len = PyList_Size(self->stack)) < 0)
2515 goto finally;
2516
2517 UNLESS(slice = PyList_GetSlice(self->stack, i + 1, len))
2518 goto finally;
2519
2520 UNLESS(tup = PySequence_Tuple(slice))
2521 goto finally;
2522
2523 UNLESS(obj = Instance_New(class, tup))
2524 goto finally;
2525
2526 if (DEL_LIST_SLICE(self->stack, i, len) < 0)
2527 goto finally;
2528
2529 if (PyList_Append(self->stack, obj) < 0)
2530 goto finally;
2531
2532 res = 0;
2533
2534finally:
2535
2536 Py_XDECREF(class);
2537 Py_XDECREF(slice);
2538 Py_XDECREF(tup);
2539 Py_XDECREF(obj);
2540
2541 return res;
2542}
2543
2544
2545static int
2546load_inst(Unpicklerobject *self) {
2547 PyObject *arg_tup = 0, *arg_slice = 0, *class = 0, *obj = 0,
2548 *module_name = 0, *class_name = 0;
2549 int i, j, len, res = -1;
2550 char *s;
2551
2552 if ((i = marker(self)) < 0)
2553 goto finally;
2554
2555 if ((j = PyList_Size(self->stack)) < 0)
2556 goto finally;
2557
2558 UNLESS(arg_slice = PyList_GetSlice(self->stack, i, j))
2559 goto finally;
2560
2561 UNLESS(arg_tup = PySequence_Tuple(arg_slice))
2562 goto finally;
2563
2564 if (DEL_LIST_SLICE(self->stack, i, j) < 0)
2565 goto finally;
2566
2567 if ((len = (*self->readline_func)(self, &s)) < 0)
2568 goto finally;
2569
2570 UNLESS(module_name = PyString_FromStringAndSize(s, len - 1))
2571 goto finally;
2572
2573 if ((len = (*self->readline_func)(self, &s)) < 0)
2574 goto finally;
2575
2576 UNLESS(class_name = PyString_FromStringAndSize(s, len - 1))
2577 goto finally;
2578
2579 UNLESS(class = find_class(module_name, class_name))
2580 goto finally;
2581
2582 UNLESS(obj = Instance_New(class, arg_tup))
2583 goto finally;
2584
2585 if (PyList_Append(self->stack, obj) < 0)
2586 goto finally;
2587
2588 res = 0;
2589
2590finally:
2591 Py_XDECREF(class);
2592 Py_XDECREF(arg_slice);
2593 Py_XDECREF(arg_tup);
2594 Py_XDECREF(obj);
2595 Py_XDECREF(module_name);
2596 Py_XDECREF(class_name);
2597
2598 return res;
2599}
2600
2601
2602static int
2603load_global(Unpicklerobject *self) {
2604 PyObject *class = 0, *module_name = 0, *class_name = 0;
2605 int res = -1, len;
2606 char *s;
2607
2608 if ((len = (*self->readline_func)(self, &s)) < 0)
2609 goto finally;
2610
2611 UNLESS(module_name = PyString_FromStringAndSize(s, len - 1))
2612 goto finally;
2613
2614 if ((len = (*self->readline_func)(self, &s)) < 0)
2615 goto finally;
2616
2617 UNLESS(class_name = PyString_FromStringAndSize(s, len - 1))
2618 goto finally;
2619
2620 UNLESS(class = find_class(module_name, class_name))
2621 goto finally;
2622
2623 if (PyList_Append(self->stack, class) < 0)
2624 goto finally;
2625
2626 res = 0;
2627
2628finally:
2629 Py_XDECREF(class);
2630 Py_XDECREF(module_name);
2631 Py_XDECREF(class_name);
2632
2633 return res;
2634}
2635
2636
2637static int
2638load_persid(Unpicklerobject *self) {
2639 PyObject *pid = 0, *pers_load_val = 0;
2640 int len, res = -1;
2641 char *s;
2642
2643 if (self->pers_func) {
2644 if ((len = (*self->readline_func)(self, &s)) < 0)
2645 goto finally;
2646
2647 UNLESS(pid = PyString_FromStringAndSize(s, len - 1))
2648 goto finally;
2649
2650 UNLESS(self->arg)
2651 UNLESS(self->arg = PyTuple_New(1))
2652 goto finally;
2653
2654 Py_INCREF(pid);
2655 if (PyTuple_SetItem(self->arg, 0, pid) < 0)
2656 goto finally;
2657
2658 UNLESS(pers_load_val =
2659 PyObject_CallObject(self->pers_func, self->arg))
2660 goto finally;
2661
2662 if (PyList_Append(self->stack, pers_load_val) < 0)
2663 goto finally;
2664 }
2665
2666 res = 0;
2667
2668finally:
2669 Py_XDECREF(pid);
2670 Py_XDECREF(pers_load_val);
2671
2672 return res;
2673}
2674
2675
2676static int
2677load_binpersid(Unpicklerobject *self) {
2678 PyObject *pid = 0, *pers_load_val = 0;
2679 int len, res = -1;
2680
2681 if (self->pers_func) {
2682 if ((len = PyList_Size(self->stack)) < 0)
2683 goto finally;
2684
2685 pid = PyList_GET_ITEM((PyListObject *)self->stack, len - 1);
2686 Py_INCREF(pid);
2687
2688 if (DEL_LIST_SLICE(self->stack, len - 1, len) < 0)
2689 goto finally;
2690
2691 UNLESS(self->arg)
2692 UNLESS(self->arg = PyTuple_New(1))
2693 goto finally;
2694
2695 Py_INCREF(pid);
2696 if (PyTuple_SetItem(self->arg, 0, pid) < 0)
2697 goto finally;
2698
2699 UNLESS(pers_load_val =
2700 PyObject_CallObject(self->pers_func, self->arg))
2701 goto finally;
2702
2703 if (PyList_Append(self->stack, pers_load_val) < 0)
2704 goto finally;
2705 }
2706
2707 res = 0;
2708
2709finally:
2710 Py_XDECREF(pid);
2711 Py_XDECREF(pers_load_val);
2712
2713 return res;
2714}
2715
2716
2717static int
2718load_pop(Unpicklerobject *self) {
2719 int len;
2720
2721 if ((len = PyList_Size(self->stack)) < 0)
2722 return -1;
2723
2724 if ((self->num_marks > 0) &&
2725 (self->marks[self->num_marks - 1] == len))
2726 self->num_marks--;
2727 else if (DEL_LIST_SLICE(self->stack, len - 1, len) < 0)
2728 return -1;
2729
2730 return 0;
2731}
2732
2733
2734static int
2735load_pop_mark(Unpicklerobject *self) {
2736 int i, len;
2737
2738 if ((i = marker(self)) < 0)
2739 return -1;
2740
2741 if ((len = PyList_Size(self->stack)) < 0)
2742 return -1;
2743
2744 if (DEL_LIST_SLICE(self->stack, i, len) < 0)
2745 return -1;
2746
2747 return 0;
2748}
2749
2750
2751static int
2752load_dup(Unpicklerobject *self) {
2753 PyObject *last;
2754 int len;
2755
2756 if ((len = PyList_Size(self->stack)) < 0)
2757 return -1;
2758
2759 UNLESS(last = PyList_GetItem(self->stack, len - 1))
2760 return -1;
2761
2762 if (PyList_Append(self->stack, last) < 0)
2763 return -1;
2764
2765 return 0;
2766}
2767
2768
2769static int
2770load_get(Unpicklerobject *self) {
2771 PyObject *py_str = 0, *value = 0;
2772 int len, res = -1;
2773 char *s;
2774
2775 if ((len = (*self->readline_func)(self, &s)) < 0)
2776 goto finally;
2777
2778 UNLESS(py_str = PyString_FromStringAndSize(s, len - 1))
2779 goto finally;
2780
2781 UNLESS(value = PyDict_GetItem(self->memo, py_str))
2782 goto finally;
2783
2784 if (PyList_Append(self->stack, value) < 0)
2785 goto finally;
2786
2787 res = 0;
2788
2789finally:
2790 Py_XDECREF(py_str);
2791
2792 return res;
2793}
2794
2795
2796static int
2797load_binget(Unpicklerobject *self) {
2798 PyObject *py_key = 0, *value = 0;
2799 unsigned char key;
2800 int res = -1;
2801 char *s;
2802
2803 if ((*self->read_func)(self, &s, 1) < 0)
2804 goto finally;
2805
2806 key = (unsigned char)s[0];
2807
2808 UNLESS(py_key = PyInt_FromLong((long)key))
2809 goto finally;
2810
2811 UNLESS(value = PyDict_GetItem(self->memo, py_key))
2812 goto finally;
2813
2814 if (PyList_Append(self->stack, value) < 0)
2815 goto finally;
2816
2817 res = 0;
2818
2819finally:
2820 Py_XDECREF(py_key);
2821
2822 return res;
2823}
2824
2825
2826static int
2827load_long_binget(Unpicklerobject *self) {
2828 PyObject *py_key = 0, *value = 0;
2829 unsigned char c, *s;
2830 long key;
2831 int res = -1;
2832
2833 if ((*self->read_func)(self, &s, 4) < 0)
2834 goto finally;
2835
2836 c = (unsigned char)s[0];
2837 key = (long)c;
2838 c = (unsigned char)s[1];
2839 key |= (long)c << 8;
2840 c = (unsigned char)s[2];
2841 key |= (long)c << 16;
2842 c = (unsigned char)s[3];
2843 key |= (long)c << 24;
2844
2845 UNLESS(py_key = PyInt_FromLong(key))
2846 goto finally;
2847
2848 UNLESS(value = PyDict_GetItem(self->memo, py_key))
2849 goto finally;
2850
2851 if (PyList_Append(self->stack, value) < 0)
2852 goto finally;
2853
2854 res = 0;
2855
2856finally:
2857 Py_XDECREF(py_key);
2858
2859 return res;
2860}
2861
2862
2863static int
2864load_put(Unpicklerobject *self) {
2865 PyObject *py_str = 0, *value = 0;
2866 int len, res = -1;
2867 char *s;
2868
2869 if ((len = (*self->readline_func)(self, &s)) < 0)
2870 goto finally;
2871
2872 UNLESS(py_str = PyString_FromStringAndSize(s, len - 1))
2873 goto finally;
2874
2875 if ((len = PyList_Size(self->stack)) < 0)
2876 goto finally;
2877
2878 UNLESS(value = PyList_GetItem(self->stack, len - 1))
2879 goto finally;
2880
2881 if (PyDict_SetItem(self->memo, py_str, value) < 0)
2882 goto finally;
2883
2884 res = 0;
2885
2886finally:
2887 Py_XDECREF(py_str);
2888
2889 return res;
2890}
2891
2892
2893static int
2894load_binput(Unpicklerobject *self) {
2895 PyObject *py_key = 0, *value = 0;
2896 unsigned char key, *s;
2897 int len, res = -1;
2898
2899 if ((*self->read_func)(self, &s, 1) < 0)
2900 goto finally;
2901
2902 key = (unsigned char)s[0];
2903
2904 UNLESS(py_key = PyInt_FromLong((long)key))
2905 goto finally;
2906
2907 if ((len = PyList_Size(self->stack)) < 0)
2908 goto finally;
2909
2910 UNLESS(value = PyList_GetItem(self->stack, len - 1))
2911 goto finally;
2912
2913 if (PyDict_SetItem(self->memo, py_key, value) < 0)
2914 goto finally;
2915
2916 res = 0;
2917
2918finally:
2919 Py_XDECREF(py_key);
2920
2921 return res;
2922}
2923
2924
2925static int
2926load_long_binput(Unpicklerobject *self) {
2927 PyObject *py_key = 0, *value = 0;
2928 long key;
2929 unsigned char c, *s;
2930 int len, res = -1;
2931
2932 if ((*self->read_func)(self, &s, 4) < 0)
2933 goto finally;
2934
2935 c = (unsigned char)s[0];
2936 key = (long)c;
2937 c = (unsigned char)s[1];
2938 key |= (long)c << 8;
2939 c = (unsigned char)s[2];
2940 key |= (long)c << 16;
2941 c = (unsigned char)s[3];
2942 key |= (long)c << 24;
2943
2944 UNLESS(py_key = PyInt_FromLong(key))
2945 goto finally;
2946
2947 if ((len = PyList_Size(self->stack)) < 0)
2948 goto finally;
2949
2950 UNLESS(value = PyList_GetItem(self->stack, len - 1))
2951 goto finally;
2952
2953 if (PyDict_SetItem(self->memo, py_key, value) < 0)
2954 goto finally;
2955
2956 res = 0;
2957
2958finally:
2959 Py_XDECREF(py_key);
2960
2961 return res;
2962}
2963
2964
2965static int
2966do_append(Unpicklerobject *self, int x) {
2967 PyObject *value = 0, *list = 0, *append_method = 0;
2968 int len, i;
2969
2970 if ((len = PyList_Size(self->stack)) < 0)
2971 return -1;
2972
2973 UNLESS(list = PyList_GetItem(self->stack, x - 1))
2974 goto err;
2975
2976 if (PyList_Check(list)) {
2977 PyObject *slice = 0;
2978 int list_len;
2979
2980 UNLESS(slice = PyList_GetSlice(self->stack, x, len))
2981 return -1;
2982
2983 list_len = PyList_Size(list);
2984 if (PyList_SetSlice(list, list_len, list_len, slice) < 0) {
2985 Py_DECREF(slice);
2986 return -1;
2987 }
2988
2989 Py_DECREF(slice);
2990 }
2991 else {
2992
2993 UNLESS(append_method = PyObject_GetAttr(list, append_str))
2994 return -1;
2995
2996 for (i = x; i < len; i++) {
2997 PyObject *junk;
2998
2999 UNLESS(value = PyList_GetItem(self->stack, i))
3000 return -1;
3001
3002 UNLESS(self->arg)
3003 UNLESS(self->arg = PyTuple_New(1))
3004 goto err;
3005
3006 Py_INCREF(value);
3007 if (PyTuple_SetItem(self->arg, 0, value) < 0)
3008 goto err;
3009
3010 UNLESS(junk = PyObject_CallObject(append_method, self->arg))
3011 goto err;
3012 Py_DECREF(junk);
3013 }
3014 }
3015
3016 if (DEL_LIST_SLICE(self->stack, x, len) < 0)
3017 goto err;
3018
3019 Py_XDECREF(append_method);
3020
3021 return 0;
3022
3023err:
3024 Py_XDECREF(append_method);
3025
3026 return -1;
3027}
3028
3029
3030static int
3031load_append(Unpicklerobject *self) {
3032 return do_append(self, PyList_Size(self->stack) - 1);
3033}
3034
3035
3036static int
3037load_appends(Unpicklerobject *self) {
3038 return do_append(self, marker(self));
3039}
3040
3041
3042static int
3043do_setitems(Unpicklerobject *self, int x) {
3044 PyObject *value = 0, *key = 0, *dict = 0;
3045 int len, i, res = -1;
3046
3047 if ((len = PyList_Size(self->stack)) < 0)
3048 goto finally;
3049
3050 UNLESS(dict = PyList_GetItem(self->stack, x - 1))
3051 goto finally;
3052
3053 for (i = x; i < len; i += 2) {
3054 UNLESS(key = PyList_GetItem(self->stack, i))
3055 goto finally;
3056
3057 UNLESS(value = PyList_GetItem(self->stack, i + 1))
3058 goto finally;
3059
3060 if (PyObject_SetItem(dict, key, value) < 0)
3061 goto finally;
3062 }
3063
3064 if (DEL_LIST_SLICE(self->stack, x, len) < 0)
3065 goto finally;
3066
3067 res = 0;
3068
3069finally:
3070
3071 return res;
3072}
3073
3074
3075static int
3076load_setitem(Unpicklerobject *self) {
3077 return do_setitems(self, PyList_Size(self->stack) - 2);
3078}
3079
3080
3081static int
3082load_setitems(Unpicklerobject *self) {
3083 return do_setitems(self, marker(self));
3084}
3085
3086
3087static int
3088load_build(Unpicklerobject *self) {
3089 PyObject *value = 0, *inst = 0, *instdict = 0, *d_key = 0, *d_value = 0,
3090 *junk = 0, *__setstate__ = 0;
3091 int len, i, res = -1;
3092
3093 if ((len = PyList_Size(self->stack)) < 0)
3094 goto finally;
3095
3096 UNLESS(value = PyList_GetItem(self->stack, len - 1))
3097 goto finally;
3098 Py_INCREF(value);
3099
3100 if (DEL_LIST_SLICE(self->stack, len - 1, len) < 0)
3101 goto finally;
3102
3103 UNLESS(inst = PyList_GetItem(self->stack, len - 2))
3104 goto finally;
3105
3106 UNLESS(__setstate__ = PyObject_GetAttr(inst, __setstate___str))
3107 {
3108 PyErr_Clear();
3109
3110 UNLESS(instdict = PyObject_GetAttr(inst, __dict___str))
3111 goto finally;
3112
3113 i = 0;
3114 while (PyDict_Next(value, &i, &d_key, &d_value)) {
3115 if (PyObject_SetItem(instdict, d_key, d_value) < 0)
3116 goto finally;
3117 }
3118 }
3119 else {
3120 UNLESS(self->arg)
3121 UNLESS(self->arg = PyTuple_New(1))
3122 goto finally;
3123
3124 Py_INCREF(value);
3125 if (PyTuple_SetItem(self->arg, 0, value) < 0)
3126 goto finally;
3127
3128 UNLESS(junk = PyObject_CallObject(__setstate__, self->arg))
3129 goto finally;
3130 Py_DECREF(junk);
3131 }
3132
3133 res = 0;
3134
3135finally:
3136 Py_XDECREF(value);
3137 Py_XDECREF(instdict);
3138 Py_XDECREF(__setstate__);
3139
3140 return res;
3141}
3142
3143
3144static int
3145load_mark(Unpicklerobject *self) {
3146 int len;
3147
3148 if ((len = PyList_Size(self->stack)) < 0)
3149 return -1;
3150
3151 if (!self->marks_size) {
3152 self->marks_size = 20;
3153 UNLESS(self->marks = (int *)malloc(self->marks_size * sizeof(int))) {
3154 PyErr_NoMemory();
3155 return -1;
3156 }
3157 }
3158 else if ((self->num_marks + 1) >= self->marks_size) {
3159 UNLESS(self->marks = (int *)realloc(self->marks,
3160 (self->marks_size + 20) * sizeof(int))) {
3161 PyErr_NoMemory();
3162 return -1;
3163 }
3164
3165 self->marks_size += 20;
3166 }
3167
3168 self->marks[self->num_marks++] = len;
3169
3170 return 0;
3171}
3172
3173static int
3174load_reduce(Unpicklerobject *self) {
3175 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
3176 int len, res = -1;
3177
3178 if ((len = PyList_Size(self->stack)) < 0)
3179 goto finally;
3180
3181 UNLESS(arg_tup = PyList_GetItem(self->stack, len - 1))
3182 goto finally;
3183
3184 UNLESS(callable = PyList_GetItem(self->stack, len - 2))
3185 goto finally;
3186
3187 UNLESS(ob = Instance_New(callable, arg_tup))
3188 goto finally;
3189
3190 if (PyList_Append(self->stack, ob) < 0)
3191 goto finally;
3192
3193 if (DEL_LIST_SLICE(self->stack, len - 2, len) < 0)
3194 goto finally;
3195
3196 res = 0;
3197
3198finally:
3199 Py_XDECREF(ob);
3200
3201 return res;
3202}
3203
3204static PyObject *
3205load(Unpicklerobject *self)
3206{
3207 PyObject *stack = 0, *err = 0, *exc = 0, *val = 0, *tb = 0;
3208 int len;
3209 char *s;
3210
3211 UNLESS(stack = PyList_New(0))
3212 goto err;
3213
3214 self->stack = stack;
3215 self->num_marks = 0;
3216
3217 while (1) {
3218 if ((*self->read_func)(self, &s, 1) < 0)
3219 break;
3220
3221 switch (s[0]) {
3222 case NONE:
3223 if (load_none(self) < 0)
3224 break;
3225 continue;
3226
3227 case BININT:
3228 if (load_binint(self) < 0)
3229 break;
3230 continue;
3231
3232 case BININT1:
3233 if (load_binint1(self) < 0)
3234 break;
3235 continue;
3236
3237 case BININT2:
3238 if (load_binint2(self) < 0)
3239 break;
3240 continue;
3241
3242 case INT:
3243 if (load_int(self) < 0)
3244 break;
3245 continue;
3246
3247 case LONG:
3248 if (load_long(self) < 0)
3249 break;
3250 continue;
3251
3252 case FLOAT:
3253 if (load_float(self) < 0)
3254 break;
3255 continue;
3256
3257#ifdef FORMAT_1_3
3258 case BINFLOAT:
3259 if (load_binfloat(self) < 0)
3260 break;
3261 continue;
3262#endif
3263
3264 case BINSTRING:
3265 if (load_binstring(self) < 0)
3266 break;
3267 continue;
3268
3269 case SHORT_BINSTRING:
3270 if (load_short_binstring(self) < 0)
3271 break;
3272 continue;
3273
3274 case STRING:
3275 if (load_string(self) < 0)
3276 break;
3277 continue;
3278
3279 case EMPTY_TUPLE:
3280 if (load_empty_tuple(self) < 0)
3281 break;
3282 continue;
3283
3284 case TUPLE:
3285 if (load_tuple(self) < 0)
3286 break;
3287 continue;
3288
3289 case EMPTY_LIST:
3290 if (load_empty_list(self) < 0)
3291 break;
3292 continue;
3293
3294 case LIST:
3295 if (load_list(self) < 0)
3296 break;
3297 continue;
3298
3299 case EMPTY_DICT:
3300 if (load_empty_dict(self) < 0)
3301 break;
3302 continue;
3303
3304 case DICT:
3305 if (load_dict(self) < 0)
3306 break;
3307 continue;
3308
3309 case OBJ:
3310 if (load_obj(self) < 0)
3311 break;
3312 continue;
3313
3314 case INST:
3315 if (load_inst(self) < 0)
3316 break;
3317 continue;
3318
3319 case GLOBAL:
3320 if (load_global(self) < 0)
3321 break;
3322 continue;
3323
3324 case APPEND:
3325 if (load_append(self) < 0)
3326 break;
3327 continue;
3328
3329 case APPENDS:
3330 if (load_appends(self) < 0)
3331 break;
3332 continue;
3333
3334 case BUILD:
3335 if (load_build(self) < 0)
3336 break;
3337 continue;
3338
3339 case DUP:
3340 if (load_dup(self) < 0)
3341 break;
3342 continue;
3343
3344 case BINGET:
3345 if (load_binget(self) < 0)
3346 break;
3347 continue;
3348
3349 case LONG_BINGET:
3350 if (load_long_binget(self) < 0)
3351 break;
3352 continue;
3353
3354 case GET:
3355 if (load_get(self) < 0)
3356 break;
3357 continue;
3358
3359 case MARK:
3360 if (load_mark(self) < 0)
3361 break;
3362 continue;
3363
3364 case BINPUT:
3365 if (load_binput(self) < 0)
3366 break;
3367 continue;
3368
3369 case LONG_BINPUT:
3370 if (load_long_binput(self) < 0)
3371 break;
3372 continue;
3373
3374 case PUT:
3375 if (load_put(self) < 0)
3376 break;
3377 continue;
3378
3379 case POP:
3380 if (load_pop(self) < 0)
3381 break;
3382 continue;
3383
3384 case POP_MARK:
3385 if (load_pop_mark(self) < 0)
3386 break;
3387 continue;
3388
3389 case SETITEM:
3390 if (load_setitem(self) < 0)
3391 break;
3392 continue;
3393
3394 case SETITEMS:
3395 if (load_setitems(self) < 0)
3396 break;
3397 continue;
3398
3399 case STOP:
3400 break;
3401
3402 case PERSID:
3403 if (load_persid(self) < 0)
3404 break;
3405 continue;
3406
3407 case BINPERSID:
3408 if (load_binpersid(self) < 0)
3409 break;
3410 continue;
3411
3412 case REDUCE:
3413 if (load_reduce(self) < 0)
3414 break;
3415 continue;
3416
3417 default:
3418 PyErr_Format(UnpicklingError, "invalid load key, '%s'.",
3419 "c", s[0]);
3420 goto err;
3421 }
3422
3423 break;
3424 }
3425
3426 if ((err = PyErr_Occurred()) == PyExc_EOFError) {
3427 PyErr_SetNone(PyExc_EOFError);
3428 goto err;
3429 }
3430
3431 if (err) goto err;
3432
3433 if ((len = PyList_Size(stack)) < 0) goto err;
3434
3435 UNLESS(val = PyList_GetItem(stack, len - 1)) goto err;
3436 Py_INCREF(val);
3437
3438 Py_DECREF(stack);
3439
3440 self->stack=NULL;
3441 return val;
3442
3443err:
3444 self->stack=NULL;
3445 Py_XDECREF(stack);
3446
3447 return NULL;
3448}
3449
3450
3451static PyObject *
3452Unpickler_load(Unpicklerobject *self, PyObject *args) {
3453 UNLESS(PyArg_ParseTuple(args, ""))
3454 return NULL;
3455
3456 return load(self);
3457}
3458
3459
3460static struct PyMethodDef Unpickler_methods[] = {
3461 {"load", (PyCFunction)Unpickler_load, 1, ""},
3462 {NULL, NULL} /* sentinel */
3463};
3464
3465
3466static Unpicklerobject *
3467newUnpicklerobject(PyObject *f) {
3468 Unpicklerobject *self;
3469
3470 UNLESS(self = PyObject_NEW(Unpicklerobject, &Unpicklertype))
3471 return NULL;
3472
3473 self->file = NULL;
3474 self->arg = NULL;
3475 self->stack = NULL;
3476 self->pers_func = NULL;
3477 self->last_string = NULL;
3478 self->marks = NULL;
3479 self->num_marks = 0;
3480 self->marks_size = 0;
3481 self->buf_size = 0;
3482 self->read = NULL;
3483 self->readline = NULL;
3484
3485 UNLESS(self->memo = PyDict_New()) {
3486 Py_XDECREF((PyObject *)self);
3487 return NULL;
3488 }
3489
3490 Py_INCREF(f);
3491 self->file = f;
3492
3493 /* Set read, readline based on type of f */
3494 if (PyFile_Check(f)) {
3495 self->fp = PyFile_AsFile(f);
3496 self->read_func = read_file;
3497 self->readline_func = readline_file;
3498 }
3499 else if (PycStringIO_InputCheck(f)) {
3500 self->fp = NULL;
3501 self->read_func = read_cStringIO;
3502 self->readline_func = readline_cStringIO;
3503 }
3504 else {
3505
3506 self->fp = NULL;
3507 self->read_func = read_other;
3508 self->readline_func = readline_other;
3509
3510 UNLESS((self->readline = PyObject_GetAttr(f, readline_str)) &&
3511 (self->read = PyObject_GetAttr(f, read_str)))
3512 {
3513 PyErr_Clear();
3514 PyErr_SetString( PyExc_TypeError, "argument must have 'read' and "
3515 "'readline' attributes" );
3516 Py_XDECREF((PyObject *)self);
3517 return NULL;
3518 }
3519 }
3520
3521 return self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003522}
3523
3524
3525static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00003526get_Unpickler(PyObject *self, PyObject *args) {
3527 PyObject *file;
3528
3529 UNLESS(PyArg_ParseTuple(args, "O", &file))
3530 return NULL;
3531 return (PyObject *)newUnpicklerobject(file);
3532}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003533
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003534
Guido van Rossum60456fd1997-04-09 17:36:32 +00003535static void
3536Unpickler_dealloc(Unpicklerobject *self) {
3537 Py_XDECREF(self->readline);
3538 Py_XDECREF(self->read);
3539 Py_XDECREF(self->file);
3540 Py_XDECREF(self->memo);
3541 Py_XDECREF(self->stack);
3542 Py_XDECREF(self->pers_func);
3543 Py_XDECREF(self->arg);
3544 Py_XDECREF(self->last_string);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003545
Guido van Rossum60456fd1997-04-09 17:36:32 +00003546 if (self->marks) {
3547 free(self->marks);
3548 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003549
Guido van Rossum60456fd1997-04-09 17:36:32 +00003550 if (self->buf_size) {
3551 free(self->buf);
3552 }
3553
3554 PyMem_DEL(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003555}
3556
3557
3558static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00003559Unpickler_getattr(Unpicklerobject *self, char *name) {
3560 if (!strcmp(name, "persistent_load")) {
3561 if (!self->pers_func) {
3562 PyErr_SetString(PyExc_AttributeError, name);
3563 return NULL;
3564 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003565
Guido van Rossum60456fd1997-04-09 17:36:32 +00003566 Py_INCREF(self->pers_func);
3567 return self->pers_func;
3568 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003569
Guido van Rossum60456fd1997-04-09 17:36:32 +00003570 if (!strcmp(name, "memo")) {
3571 if (!self->memo) {
3572 PyErr_SetString(PyExc_AttributeError, name);
3573 return NULL;
3574 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003575
Guido van Rossum60456fd1997-04-09 17:36:32 +00003576 Py_INCREF(self->memo);
3577 return self->memo;
3578 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003579
Guido van Rossum60456fd1997-04-09 17:36:32 +00003580 if (!strcmp(name, "stack")) {
3581 if (!self->stack) {
3582 PyErr_SetString(PyExc_AttributeError, name);
3583 return NULL;
3584 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003585
Guido van Rossum60456fd1997-04-09 17:36:32 +00003586 Py_INCREF(self->stack);
3587 return self->stack;
3588 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003589
Guido van Rossum60456fd1997-04-09 17:36:32 +00003590 if (!strcmp(name, "UnpicklingError")) {
3591 Py_INCREF(UnpicklingError);
3592 return UnpicklingError;
3593 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003594
Guido van Rossum60456fd1997-04-09 17:36:32 +00003595 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
3596}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003597
Guido van Rossum60456fd1997-04-09 17:36:32 +00003598
3599static int
3600Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) {
3601 if (!strcmp(name, "persistent_load")) {
3602 Py_XDECREF(self->pers_func);
3603 self->pers_func = value;
3604 Py_INCREF(value);
3605 return 0;
3606 }
3607
3608 PyErr_SetString(PyExc_AttributeError, name);
3609 return -1;
3610}
3611
3612
3613static PyObject *
3614cpm_dump(PyObject *self, PyObject *args) {
3615 PyObject *ob, *file, *res = NULL;
3616 Picklerobject *pickler = 0;
3617 int bin = 0;
3618
3619 UNLESS(PyArg_ParseTuple(args, "OO|i", &ob, &file, &bin))
3620 goto finally;
3621
3622 UNLESS(pickler = newPicklerobject(file, bin))
3623 goto finally;
3624
3625 if (dump(pickler, ob) < 0)
3626 goto finally;
3627
3628 Py_INCREF(Py_None);
3629 res = Py_None;
3630
3631finally:
3632 Py_XDECREF(pickler);
3633
3634 return res;
3635}
3636
3637
3638static PyObject *
3639cpm_dumps(PyObject *self, PyObject *args) {
3640 PyObject *ob, *file = 0, *res = NULL;
3641 Picklerobject *pickler = 0;
3642 int bin = 0;
3643
3644 UNLESS(PyArg_ParseTuple(args, "O|i", &ob, &bin))
3645 goto finally;
3646
3647 UNLESS(file = PycStringIO->NewOutput(128))
3648 goto finally;
3649
3650 UNLESS(pickler = newPicklerobject(file, bin))
3651 goto finally;
3652
3653 if (dump(pickler, ob) < 0)
3654 goto finally;
3655
3656 res = PycStringIO->cgetvalue(file);
3657
3658finally:
3659 Py_XDECREF(pickler);
3660 Py_XDECREF(file);
3661
3662 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003663}
3664
3665
3666static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00003667cpm_load(PyObject *self, PyObject *args) {
3668 Unpicklerobject *unpickler = 0;
3669 PyObject *ob, *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003670
Guido van Rossum60456fd1997-04-09 17:36:32 +00003671 UNLESS(PyArg_ParseTuple(args, "O", &ob))
3672 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003673
Guido van Rossum60456fd1997-04-09 17:36:32 +00003674 UNLESS(unpickler = newUnpicklerobject(ob))
3675 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003676
Guido van Rossum60456fd1997-04-09 17:36:32 +00003677 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003678
Guido van Rossum60456fd1997-04-09 17:36:32 +00003679finally:
3680 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003681
Guido van Rossum60456fd1997-04-09 17:36:32 +00003682 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003683}
3684
3685
3686static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00003687cpm_loads(PyObject *self, PyObject *args) {
3688 PyObject *ob, *file = 0, *res = NULL;
3689 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003690
Guido van Rossum60456fd1997-04-09 17:36:32 +00003691 UNLESS(PyArg_ParseTuple(args, "O", &ob))
3692 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003693
Guido van Rossum60456fd1997-04-09 17:36:32 +00003694 UNLESS(file = PycStringIO->NewInput(ob))
3695 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003696
Guido van Rossum60456fd1997-04-09 17:36:32 +00003697 UNLESS(unpickler = newUnpicklerobject(file))
3698 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003699
Guido van Rossum60456fd1997-04-09 17:36:32 +00003700 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003701
Guido van Rossum60456fd1997-04-09 17:36:32 +00003702finally:
3703 Py_XDECREF(file);
3704 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003705
Guido van Rossum60456fd1997-04-09 17:36:32 +00003706 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003707}
3708
3709
3710static char Unpicklertype__doc__[] = "";
3711
Guido van Rossum60456fd1997-04-09 17:36:32 +00003712static PyTypeObject Unpicklertype_value() {
3713 PyTypeObject Unpicklertype = {
3714 PyObject_HEAD_INIT(&PyType_Type)
3715 0, /*ob_size*/
3716 "Unpickler", /*tp_name*/
3717 sizeof(Unpicklerobject), /*tp_basicsize*/
3718 0, /*tp_itemsize*/
3719 /* methods */
3720 (destructor)Unpickler_dealloc, /*tp_dealloc*/
3721 (printfunc)0, /*tp_print*/
3722 (getattrfunc)Unpickler_getattr, /*tp_getattr*/
3723 (setattrfunc)Unpickler_setattr, /*tp_setattr*/
3724 (cmpfunc)0, /*tp_compare*/
3725 (reprfunc)0, /*tp_repr*/
3726 0, /*tp_as_number*/
3727 0, /*tp_as_sequence*/
3728 0, /*tp_as_mapping*/
3729 (hashfunc)0, /*tp_hash*/
3730 (ternaryfunc)0, /*tp_call*/
3731 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003732
Guido van Rossum60456fd1997-04-09 17:36:32 +00003733 /* Space for future expansion */
3734 0L,0L,0L,0L,
3735 Unpicklertype__doc__ /* Documentation string */
3736 };
3737 return Unpicklertype;
3738}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003739
Guido van Rossum60456fd1997-04-09 17:36:32 +00003740static struct PyMethodDef cPickle_methods[] = {
3741 {"dump", (PyCFunction)cpm_dump, 1, ""},
3742 {"dumps", (PyCFunction)cpm_dumps, 1, ""},
3743 {"load", (PyCFunction)cpm_load, 1, ""},
3744 {"loads", (PyCFunction)cpm_loads, 1, ""},
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003745 {"Pickler", (PyCFunction)get_Pickler, 1, ""},
Guido van Rossum60456fd1997-04-09 17:36:32 +00003746 {"Unpickler", (PyCFunction)get_Unpickler, 1, ""},
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003747 { NULL, NULL }
3748};
3749
3750
Guido van Rossum60456fd1997-04-09 17:36:32 +00003751#define CHECK_FOR_ERRORS(MESS) \
3752if(PyErr_Occurred()) { \
3753 PyObject *__sys_exc_type, *__sys_exc_value, *__sys_exc_traceback; \
3754 PyErr_Fetch( &__sys_exc_type, &__sys_exc_value, &__sys_exc_traceback); \
3755 fprintf(stderr, # MESS ":\n\t"); \
3756 PyObject_Print(__sys_exc_type, stderr,0); \
3757 fprintf(stderr,", "); \
3758 PyObject_Print(__sys_exc_value, stderr,0); \
3759 fprintf(stderr,"\n"); \
3760 fflush(stderr); \
3761 Py_FatalError(# MESS); \
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003762}
3763
3764
Guido van Rossum60456fd1997-04-09 17:36:32 +00003765static int
3766init_stuff(PyObject *module, PyObject *module_dict) {
3767 PyObject *string, *copy_reg;
3768
3769#define INIT_STR(S) UNLESS(S ## _str=PyString_FromString(#S)) return -1;
3770
3771 INIT_STR(__class__);
3772 INIT_STR(__getinitargs__);
3773 INIT_STR(__dict__);
3774 INIT_STR(__getstate__);
3775 INIT_STR(__setstate__);
3776 INIT_STR(__name__);
3777 INIT_STR(__reduce__);
3778 INIT_STR(write);
3779 INIT_STR(__safe_for_unpickling__);
3780 INIT_STR(append);
3781 INIT_STR(read);
3782 INIT_STR(readline);
3783
3784 UNLESS(builtins = PyImport_ImportModule("__builtin__"))
3785 return -1;
3786
3787 UNLESS(copy_reg = PyImport_ImportModule("copy_reg"))
3788 return -1;
3789
3790 UNLESS(dispatch_table = PyObject_GetAttrString(copy_reg,
3791 "dispatch_table"))
3792 return -1;
3793
3794 UNLESS(safe_constructors = PyObject_GetAttrString(copy_reg,
3795 "safe_constructors"))
3796 return -1;
3797
3798 Py_DECREF(copy_reg);
3799
3800 UNLESS(string = PyImport_ImportModule("string"))
3801 return -1;
3802
3803 UNLESS(atol_func = PyObject_GetAttrString(string, "atol"))
3804 return -1;
3805
3806 Py_DECREF(string);
3807
3808 UNLESS(empty_tuple = PyTuple_New(0))
3809 return -1;
3810
3811 UNLESS(class_map = PyDict_New())
3812 return -1;
3813
3814 UNLESS(PicklingError = PyString_FromString("cPickle.PicklingError"))
3815 return -1;
3816
3817 if (PyDict_SetItemString(module_dict, "PicklingError",
3818 PicklingError) < 0)
3819 return -1;
3820
3821 UNLESS(UnpicklingError = PyString_FromString("cPickle.UnpicklingError"))
3822 return -1;
3823
3824 if (PyDict_SetItemString(module_dict, "UnpicklingError",
3825 UnpicklingError) < 0)
3826 return -1;
3827
3828 PycString_IMPORT;
3829
3830 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003831}
3832
3833
3834/* Initialization function for the module (*must* be called initcPickle) */
3835void
Guido van Rossum60456fd1997-04-09 17:36:32 +00003836initcPickle() {
3837 PyObject *m, *d;
3838 char *rev="$Revision$";
3839 PyObject *format_version;
3840 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003841
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003842
Guido van Rossum60456fd1997-04-09 17:36:32 +00003843 /* Create the module and add the functions */
3844 m = Py_InitModule4("cPickle", cPickle_methods,
3845 cPickle_module_documentation,
3846 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003847
Guido van Rossum60456fd1997-04-09 17:36:32 +00003848 Picklertype=Picklertype_value();
3849 Unpicklertype=Unpicklertype_value();
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003850
Guido van Rossum60456fd1997-04-09 17:36:32 +00003851 /* Add some symbolic constants to the module */
3852 d = PyModule_GetDict(m);
3853 PyDict_SetItemString(d,"__version__",
3854 PyString_FromStringAndSize(rev+11,strlen(rev+11)-2));
Barry Warsaw93d29b61997-01-14 17:45:08 +00003855
Guido van Rossum60456fd1997-04-09 17:36:32 +00003856#ifdef FORMAT_1_3
3857 format_version = PyString_FromString("1.3");
3858 compatible_formats = Py_BuildValue("[sss]", "1.0", "1.1", "1.2");
3859#else
3860 format_version = PyString_FromString("1.2");
3861 compatible_formats = Py_BuildValue("[ss]", "1.0", "1.1");
3862#endif
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003863
Guido van Rossum60456fd1997-04-09 17:36:32 +00003864 PyDict_SetItemString(d, "format_version", format_version);
3865 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003866
Guido van Rossum60456fd1997-04-09 17:36:32 +00003867 init_stuff(m, d);
3868 CHECK_FOR_ERRORS("can't initialize module cPickle");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003869}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003870
3871/****************************************************************************
3872 $Log$
Guido van Rossumd385d591997-04-09 17:47:47 +00003873 Revision 2.4 1997/04/09 17:47:47 guido
3874 Give PyErr_Format a new name and make it static.
3875
Guido van Rossum60456fd1997-04-09 17:36:32 +00003876 Revision 2.3 1997/04/09 17:36:32 guido
3877 Jim Fulton's version 2.2.
3878
3879 Revision 1.36 1997/03/11 22:05:02 chris
3880 write POP rather than POPMARK in non-binary mode
3881 use put2() in save_reduce() and save_inst() only if state is not a dictionary
3882 removed put2() from save_tuple()
3883
3884 Revision 1.35 1997/03/11 20:03:30 jim
3885 Added log comment at bottom.
3886
3887
3888
3889 ****************************************************************************/