blob: 6c70ad9cb49d1e7bb5a43de04389fcb07f74d609 [file] [log] [blame]
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001/*
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002 cPickle.c,v 1.48 1997/12/07 14:37:39 jim Exp
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003
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[] =
Guido van Rossum142eeb81997-08-13 03:14:41 +000056"C implementation and optimization of the Python pickle module\n"
57"\n"
Guido van Rossum9716aaa1997-12-08 15:15:16 +000058"cPickle.c,v 1.48 1997/12/07 14:37:39 jim Exp\n"
Guido van Rossum2f4caa41997-01-06 22:59:08 +000059;
60
61#include "Python.h"
62#include "cStringIO.h"
Guido van Rossum60456fd1997-04-09 17:36:32 +000063#include "mymath.h"
Guido van Rossum2f4caa41997-01-06 22:59:08 +000064
Guido van Rossum142eeb81997-08-13 03:14:41 +000065#ifndef Py_eval_input
66#include <graminit.h>
67#define Py_eval_input eval_input
Guido van Rossumc6ef2041997-08-21 02:30:45 +000068#endif /* Py_eval_input */
Guido van Rossum142eeb81997-08-13 03:14:41 +000069
Guido van Rossum2f4caa41997-01-06 22:59:08 +000070#include <errno.h>
71
Guido van Rossum2f4caa41997-01-06 22:59:08 +000072#define UNLESS(E) if (!(E))
Guido van Rossum2f4caa41997-01-06 22:59:08 +000073
Guido van Rossum60456fd1997-04-09 17:36:32 +000074#define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL))
Guido van Rossum2f4caa41997-01-06 22:59:08 +000075
Guido van Rossum60456fd1997-04-09 17:36:32 +000076#define WRITE_BUF_SIZE 256
77
78
79#define MARK '('
80#define STOP '.'
81#define POP '0'
82#define POP_MARK '1'
83#define DUP '2'
84#define FLOAT 'F'
Guido van Rossum60456fd1997-04-09 17:36:32 +000085#define BINFLOAT 'G'
Guido van Rossum60456fd1997-04-09 17:36:32 +000086#define INT 'I'
87#define BININT 'J'
88#define BININT1 'K'
89#define LONG 'L'
90#define BININT2 'M'
91#define NONE 'N'
92#define PERSID 'P'
93#define BINPERSID 'Q'
94#define REDUCE 'R'
95#define STRING 'S'
96#define BINSTRING 'T'
Guido van Rossum2f4caa41997-01-06 22:59:08 +000097#define SHORT_BINSTRING 'U'
Guido van Rossum60456fd1997-04-09 17:36:32 +000098#define APPEND 'a'
99#define BUILD 'b'
100#define GLOBAL 'c'
101#define DICT 'd'
102#define EMPTY_DICT '}'
103#define APPENDS 'e'
104#define GET 'g'
105#define BINGET 'h'
106#define INST 'i'
107#define LONG_BINGET 'j'
108#define LIST 'l'
109#define EMPTY_LIST ']'
110#define OBJ 'o'
111#define PUT 'p'
112#define BINPUT 'q'
113#define LONG_BINPUT 'r'
114#define SETITEM 's'
115#define TUPLE 't'
116#define EMPTY_TUPLE ')'
117#define SETITEMS 'u'
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000118
Guido van Rossum60456fd1997-04-09 17:36:32 +0000119static char MARKv = MARK;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000120
121/* atol function from string module */
122static PyObject *atol_func;
123
124static PyObject *PicklingError;
125static PyObject *UnpicklingError;
126
Guido van Rossum60456fd1997-04-09 17:36:32 +0000127static PyObject *dispatch_table;
128static PyObject *safe_constructors;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000129static PyObject *class_map;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000130static PyObject *empty_tuple;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000131
Guido van Rossum60456fd1997-04-09 17:36:32 +0000132static PyObject *__class___str, *__getinitargs___str, *__dict___str,
133 *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
134 *write_str, *__safe_for_unpickling___str, *append_str,
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000135 *read_str, *readline_str, *__main___str, *__basicnew___str,
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000136 *copy_reg_str, *dispatch_table_str, *safe_constructors_str;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000137
Guido van Rossum60456fd1997-04-09 17:36:32 +0000138static int save();
139static int put2();
140
141typedef struct {
142 PyObject_HEAD
143 FILE *fp;
144 PyObject *write;
145 PyObject *file;
146 PyObject *memo;
147 PyObject *arg;
148 PyObject *pers_func;
149 PyObject *inst_pers_func;
150 int bin;
151 int (*write_func)();
152 char *write_buf;
153 int buf_size;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000154 PyObject *dispatch_table;
155 PyObject *class_map;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000156} Picklerobject;
157
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000158staticforward PyTypeObject Picklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000159
Guido van Rossum60456fd1997-04-09 17:36:32 +0000160typedef struct {
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000161 PyObject_HEAD
162 FILE *fp;
163 PyObject *file;
164 PyObject *readline;
165 PyObject *read;
166 PyObject *memo;
167 PyObject *arg;
168 PyObject *stack;
169 PyObject *mark;
170 PyObject *pers_func;
171 PyObject *last_string;
172 int *marks;
173 int num_marks;
174 int marks_size;
175 int (*read_func)();
176 int (*readline_func)();
177 int buf_size;
178 char *buf;
179 PyObject *safe_constructors;
180 PyObject *class_map;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000181} Unpicklerobject;
182
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000183staticforward PyTypeObject Unpicklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000184
Guido van Rossum60456fd1997-04-09 17:36:32 +0000185int
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000186cPickle_PyMapping_HasKey(PyObject *o, PyObject *key) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000187 PyObject *v;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000188
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000189 if((v = PyObject_GetItem(o,key))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000190 Py_DECREF(v);
191 return 1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000192 }
193
Guido van Rossum60456fd1997-04-09 17:36:32 +0000194 PyErr_Clear();
195 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000196}
197
Guido van Rossumd385d591997-04-09 17:47:47 +0000198static
Guido van Rossum60456fd1997-04-09 17:36:32 +0000199PyObject *
200#ifdef HAVE_STDARG_PROTOTYPES
201/* VARARGS 2 */
Guido van Rossum57d9f2e1998-01-19 23:18:18 +0000202cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000203#else
204/* VARARGS */
Guido van Rossum57d9f2e1998-01-19 23:18:18 +0000205cPickle_ErrFormat(va_alist) va_dcl {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000206#endif
Guido van Rossum60456fd1997-04-09 17:36:32 +0000207 va_list va;
208 PyObject *args=0, *retval=0;
209#ifdef HAVE_STDARG_PROTOTYPES
210 va_start(va, format);
211#else
212 PyObject *ErrType;
213 char *stringformat, *format;
214 va_start(va);
215 ErrType = va_arg(va, PyObject *);
216 stringformat = va_arg(va, char *);
217 format = va_arg(va, char *);
218#endif
219
220 if(format) args = Py_VaBuildValue(format, va);
221 va_end(va);
222 if(format && ! args) return NULL;
223 if(stringformat && !(retval=PyString_FromString(stringformat))) return NULL;
224
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000225 if(retval) {
226 if(args) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000227 PyObject *v;
228 v=PyString_Format(retval, args);
229 Py_DECREF(retval);
230 Py_DECREF(args);
231 if(! v) return NULL;
232 retval=v;
233 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000234 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000235 else
236 if(args) retval=args;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000237 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000238 PyErr_SetObject(ErrType,Py_None);
239 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000240 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000241 PyErr_SetObject(ErrType,retval);
242 Py_DECREF(retval);
243 return NULL;
244}
245
246static int
247write_file(Picklerobject *self, char *s, int n) {
248 if (s == NULL) {
249 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000250 }
251
Guido van Rossum60456fd1997-04-09 17:36:32 +0000252 if ((int)fwrite(s, sizeof(char), n, self->fp) != n) {
253 PyErr_SetFromErrno(PyExc_IOError);
254 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000255 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000256
257 return n;
258}
259
260
261static int
262write_cStringIO(Picklerobject *self, char *s, int n) {
263 if (s == NULL) {
264 return 0;
265 }
266
267 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
268 return -1;
269 }
270
271 return n;
272}
273
274
275static int
Guido van Rossum142eeb81997-08-13 03:14:41 +0000276write_none(Picklerobject *self, char *s, int n) {
277 if (s == NULL) return 0;
278 return n;
279}
280
281
282static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000283write_other(Picklerobject *self, char *s, int n) {
284 PyObject *py_str = 0, *junk = 0;
285 int res = -1;
286
287 if (s == NULL) {
288 UNLESS(self->buf_size) return 0;
289 UNLESS(py_str =
290 PyString_FromStringAndSize(self->write_buf, self->buf_size))
291 goto finally;
292 }
293 else {
294 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
295 if (write_other(self, NULL, 0) < 0)
296 goto finally;
297 }
298
299 if (n > WRITE_BUF_SIZE) {
300 UNLESS(py_str =
301 PyString_FromStringAndSize(s, n))
302 goto finally;
303 }
304 else {
305 memcpy(self->write_buf + self->buf_size, s, n);
306 self->buf_size += n;
307 res = n;
308 goto finally;
309 }
310 }
311
312 UNLESS(self->arg)
313 UNLESS(self->arg = PyTuple_New(1))
314 goto finally;
315
316 Py_INCREF(py_str);
317 if (PyTuple_SetItem(self->arg, 0, py_str) < 0)
318 goto finally;
319
320 UNLESS(junk = PyObject_CallObject(self->write, self->arg))
321 goto finally;
322 Py_DECREF(junk);
323
324 self->buf_size = 0;
325
326 res = n;
327
328finally:
329 Py_XDECREF(py_str);
330
331 return res;
332}
333
334
335static int
336read_file(Unpicklerobject *self, char **s, int n) {
337
338 if (self->buf_size == 0) {
339 int size;
340
341 size = ((n < 32) ? 32 : n);
342 UNLESS(self->buf = (char *)malloc(size * sizeof(char))) {
343 PyErr_NoMemory();
344 return -1;
345 }
346
347 self->buf_size = size;
348 }
349 else if (n > self->buf_size) {
350 UNLESS(self->buf = (char *)realloc(self->buf, n * sizeof(char))) {
351 PyErr_NoMemory();
352 return -1;
353 }
354
355 self->buf_size = n;
356 }
357
358 if ((int)fread(self->buf, sizeof(char), n, self->fp) != n) {
359 if (feof(self->fp)) {
360 PyErr_SetNone(PyExc_EOFError);
361 return -1;
362 }
363
364 PyErr_SetFromErrno(PyExc_IOError);
365 return -1;
366 }
367
368 *s = self->buf;
369
370 return n;
371}
372
373
374static int
375readline_file(Unpicklerobject *self, char **s) {
376 int i;
377
378 if (self->buf_size == 0) {
379 UNLESS(self->buf = (char *)malloc(40 * sizeof(char))) {
380 PyErr_NoMemory();
381 return -1;
382 }
383
384 self->buf_size = 40;
385 }
386
387 i = 0;
388 while (1) {
389 for (; i < (self->buf_size - 1); i++) {
390 if (feof(self->fp) || (self->buf[i] = getc(self->fp)) == '\n') {
391 self->buf[i + 1] = '\0';
392 *s = self->buf;
393 return i + 1;
394 }
395 }
396
397 UNLESS(self->buf = (char *)realloc(self->buf,
398 (self->buf_size * 2) * sizeof(char))) {
399 PyErr_NoMemory();
400 return -1;
401 }
402
403 self->buf_size *= 2;
404 }
405
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000406}
407
408
409static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000410read_cStringIO(Unpicklerobject *self, char **s, int n) {
411 char *ptr;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000412
Guido van Rossum60456fd1997-04-09 17:36:32 +0000413 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
414 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000415 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000416 }
417
Guido van Rossum60456fd1997-04-09 17:36:32 +0000418 *s = ptr;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000419
Guido van Rossum60456fd1997-04-09 17:36:32 +0000420 return n;
421}
422
423
424static int
425readline_cStringIO(Unpicklerobject *self, char **s) {
426 int n;
427 char *ptr;
428
429 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
430 return -1;
431 }
432
433 *s = ptr;
434
435 return n;
436}
437
438
439static int
440read_other(Unpicklerobject *self, char **s, int n) {
441 PyObject *bytes, *str;
442 int res = -1;
443
444 UNLESS(bytes = PyInt_FromLong(n)) {
445 if (!PyErr_Occurred())
446 PyErr_SetNone(PyExc_EOFError);
447
448 goto finally;
449 }
450
451 UNLESS(self->arg)
452 UNLESS(self->arg = PyTuple_New(1))
453 goto finally;
454
455 Py_INCREF(bytes);
456 if (PyTuple_SetItem(self->arg, 0, bytes) < 0)
457 goto finally;
458
459 UNLESS(str = PyObject_CallObject(self->read, self->arg))
460 goto finally;
461
462 Py_XDECREF(self->last_string);
463 self->last_string = str;
464
465 *s = PyString_AsString(str);
466
467 res = n;
468
469finally:
470 Py_XDECREF(bytes);
471
472 return res;
473}
474
475
476static int
477readline_other(Unpicklerobject *self, char **s) {
478 PyObject *str;
479 int str_size;
480
481 UNLESS(str = PyObject_CallObject(self->readline, empty_tuple)) {
482 return -1;
483 }
484
485 str_size = PyString_Size(str);
486
487 Py_XDECREF(self->last_string);
488 self->last_string = str;
489
490 *s = PyString_AsString(str);
491
492 return str_size;
493}
494
495
496static char *
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000497pystrndup(char *s, int l) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000498 char *r;
499 UNLESS(r=malloc((l+1)*sizeof(char))) return (char*)PyErr_NoMemory();
500 memcpy(r,s,l);
501 r[l]=0;
502 return r;
503}
504
505
506static int
507get(Picklerobject *self, PyObject *id) {
508 PyObject *value = 0;
509 long c_value;
510 char s[30];
511 int len;
512
513 UNLESS(value = PyDict_GetItem(self->memo, id))
514 return -1;
515
516 UNLESS(value = PyTuple_GetItem(value, 0))
517 return -1;
518
519 c_value = PyInt_AsLong(value);
520
521 if (!self->bin) {
522 s[0] = GET;
523 sprintf(s + 1, "%ld\n", c_value);
524 len = strlen(s);
525 }
526 else {
527 if (c_value < 256) {
528 s[0] = BINGET;
529 s[1] = (int)(c_value & 0xff);
530 len = 2;
531 }
532 else {
533 s[0] = LONG_BINGET;
534 s[1] = (int)(c_value & 0xff);
535 s[2] = (int)((c_value >> 8) & 0xff);
536 s[3] = (int)((c_value >> 16) & 0xff);
537 s[4] = (int)((c_value >> 24) & 0xff);
538 len = 5;
539 }
540 }
541
542 if ((*self->write_func)(self, s, len) < 0)
543 return -1;
544
545 return 0;
546}
547
548
549static int
550put(Picklerobject *self, PyObject *ob) {
551 if (ob->ob_refcnt < 2)
552 return 0;
553
554 return put2(self, ob);
555}
556
557
558static int
559put2(Picklerobject *self, PyObject *ob) {
560 char c_str[30];
561 int p, len, res = -1;
562 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
563 if ((p = PyDict_Size(self->memo)) < 0)
564 goto finally;
565
566 if (!self->bin) {
567 c_str[0] = PUT;
568 sprintf(c_str + 1, "%d\n", p);
569 len = strlen(c_str);
570 }
571 else {
572 if (p >= 256) {
573 c_str[0] = LONG_BINPUT;
574 c_str[1] = (int)(p & 0xff);
575 c_str[2] = (int)((p >> 8) & 0xff);
576 c_str[3] = (int)((p >> 16) & 0xff);
577 c_str[4] = (int)((p >> 24) & 0xff);
578 len = 5;
579 }
580 else {
581 c_str[0] = BINPUT;
582 c_str[1] = p;
583 len = 2;
584 }
585 }
586
587 if ((*self->write_func)(self, c_str, len) < 0)
588 goto finally;
589
590 UNLESS(py_ob_id = PyInt_FromLong((long)ob))
591 goto finally;
592
593 UNLESS(memo_len = PyInt_FromLong(p))
594 goto finally;
595
596 UNLESS(t = PyTuple_New(2))
597 goto finally;
598
599 PyTuple_SET_ITEM(t, 0, memo_len);
600 Py_INCREF(memo_len);
601 PyTuple_SET_ITEM(t, 1, ob);
602 Py_INCREF(ob);
603
604 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
605 goto finally;
606
607 res = 0;
608
609finally:
610 Py_XDECREF(py_ob_id);
611 Py_XDECREF(memo_len);
612 Py_XDECREF(t);
613
614 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000615}
616
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000617#define PyImport_Import cPickle_Import
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000618
619static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000620PyImport_Import(PyObject *module_name) {
621 static PyObject *silly_list=0, *__builtins___str=0, *__import___str;
622 static PyObject *standard_builtins=0;
623 PyObject *globals=0, *__import__=0, *__builtins__=0, *r=0;
624
625 UNLESS(silly_list) {
626 UNLESS(__import___str=PyString_FromString("__import__")) return NULL;
627 UNLESS(__builtins___str=PyString_FromString("__builtins__")) return NULL;
628 UNLESS(silly_list=Py_BuildValue("[s]","__doc__")) return NULL;
629 }
630
631 if((globals=PyEval_GetGlobals())) {
632 Py_INCREF(globals);
633 UNLESS(__builtins__=PyObject_GetItem(globals,__builtins___str)) goto err;
634 }
635 else {
636 PyErr_Clear();
637
638 UNLESS(standard_builtins ||
639 (standard_builtins=PyImport_ImportModule("__builtin__")))
640 return NULL;
641
642 __builtins__=standard_builtins;
643 Py_INCREF(__builtins__);
644 UNLESS(globals = Py_BuildValue("{sO}", "__builtins__", __builtins__))
645 goto err;
646 }
647
648 if(PyDict_Check(__builtins__)) {
649 UNLESS(__import__=PyObject_GetItem(__builtins__,__import___str)) goto err;
650 }
651 else {
652 UNLESS(__import__=PyObject_GetAttr(__builtins__,__import___str)) goto err;
653 }
654
655 UNLESS(r=PyObject_CallFunction(__import__,"OOOO",
656 module_name, globals, globals, silly_list))
657 goto err;
658
659 Py_DECREF(globals);
660 Py_DECREF(__builtins__);
661 Py_DECREF(__import__);
662
663 return r;
664err:
665 Py_XDECREF(globals);
666 Py_XDECREF(__builtins__);
667 Py_XDECREF(__import__);
668 return NULL;
669}
670
671static PyObject *
672whichmodule(PyObject *class_map, PyObject *global, PyObject *global_name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000673 int i, j;
674 PyObject *module = 0, *modules_dict = 0,
675 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000676
Guido van Rossum45188231997-09-28 05:38:51 +0000677 module = PyObject_GetAttrString(global, "__module__");
678 if (module) return module;
679 PyErr_Clear();
680
Guido van Rossum142eeb81997-08-13 03:14:41 +0000681 if ((module = PyDict_GetItem(class_map, global))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000682 Py_INCREF(module);
683 return module;
684 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000685
Guido van Rossumed33a3f1998-05-14 02:34:46 +0000686 UNLESS(modules_dict = PySys_GetObject("modules")) {
687 PyErr_SetString(PyExc_SystemError, "lost sys.modules");
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000688 return NULL;
Guido van Rossumed33a3f1998-05-14 02:34:46 +0000689 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000690
Guido van Rossum60456fd1997-04-09 17:36:32 +0000691 i = 0;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000692 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
693
694 if(PyObject_Compare(name, __main___str)==0) continue;
695
Guido van Rossum60456fd1997-04-09 17:36:32 +0000696 UNLESS(global_name_attr = PyObject_GetAttr(module, global_name)) {
697 PyErr_Clear();
698 continue;
699 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000700
Guido van Rossum60456fd1997-04-09 17:36:32 +0000701 if (global_name_attr != global) {
702 Py_DECREF(global_name_attr);
703 continue;
704 }
705
706 Py_DECREF(global_name_attr);
707
708 break;
709 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000710
711 /* The following implements the rule in pickle.py added in 1.5
712 that used __main__ if no module is found. I don't actually
713 like this rule. jlf
714 */
715 if(!j) {
716 j=1;
717 name=__main___str;
718 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000719
Guido van Rossum142eeb81997-08-13 03:14:41 +0000720 /*
Guido van Rossum60456fd1997-04-09 17:36:32 +0000721 if (!j) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +0000722 cPickle_ErrFormat(PicklingError, "Could not find module for %s.",
Guido van Rossum60456fd1997-04-09 17:36:32 +0000723 "O", global_name);
724 return NULL;
725 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000726 */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000727
728 PyDict_SetItem(class_map, global, name);
729
730 Py_INCREF(name);
731 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000732}
733
734
Guido van Rossum60456fd1997-04-09 17:36:32 +0000735static int
736save_none(Picklerobject *self, PyObject *args) {
737 static char none = NONE;
738 if ((*self->write_func)(self, &none, 1) < 0)
739 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000740
Guido van Rossum60456fd1997-04-09 17:36:32 +0000741 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000742}
743
744
Guido van Rossum60456fd1997-04-09 17:36:32 +0000745static int
746save_int(Picklerobject *self, PyObject *args) {
747 char c_str[32];
748 long l = PyInt_AS_LONG((PyIntObject *)args);
749 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000750
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000751 if (!self->bin
752#if SIZEOF_LONG > 4
753 || (l >> 32)
754#endif
755 ) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000756 /* Save extra-long ints in non-binary mode, so that
757 we can use python long parsing code to restore,
758 if necessary. */
759 c_str[0] = INT;
760 sprintf(c_str + 1, "%ld\n", l);
761 if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
762 return -1;
763 }
764 else {
765 c_str[1] = (int)( l & 0xff);
766 c_str[2] = (int)((l >> 8) & 0xff);
767 c_str[3] = (int)((l >> 16) & 0xff);
768 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000769
Guido van Rossum60456fd1997-04-09 17:36:32 +0000770 if ((c_str[4] == 0) && (c_str[3] == 0)) {
771 if (c_str[2] == 0) {
772 c_str[0] = BININT1;
773 len = 2;
774 }
775 else {
776 c_str[0] = BININT2;
777 len = 3;
778 }
779 }
780 else {
781 c_str[0] = BININT;
782 len = 5;
783 }
784
785 if ((*self->write_func)(self, c_str, len) < 0)
786 return -1;
787 }
788
789 return 0;
790}
791
792
793static int
794save_long(Picklerobject *self, PyObject *args) {
795 int size, res = -1;
796 PyObject *repr = 0;
797
798 static char l = LONG;
799
800 UNLESS(repr = PyObject_Repr(args))
801 goto finally;
802
803 if ((size = PyString_Size(repr)) < 0)
804 goto finally;
805
806 if ((*self->write_func)(self, &l, 1) < 0)
807 goto finally;
808
809 if ((*self->write_func)(self,
810 PyString_AS_STRING((PyStringObject *)repr), size) < 0)
811 goto finally;
812
813 if ((*self->write_func)(self, "\n", 1) < 0)
814 goto finally;
815
816 res = 0;
817
818finally:
819 Py_XDECREF(repr);
820
821 return res;
822}
823
824
825static int
826save_float(Picklerobject *self, PyObject *args) {
827 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
828
829#ifdef FORMAT_1_3
830 if (self->bin) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000831 int s, e;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000832 double f;
833 long fhi, flo;
834 char str[9], *p = str;
835
836 *p = BINFLOAT;
837 p++;
838
839 if (x < 0) {
840 s = 1;
841 x = -x;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000842 }
843 else
Guido van Rossum60456fd1997-04-09 17:36:32 +0000844 s = 0;
845
846 f = frexp(x, &e);
847
848 /* Normalize f to be in the range [1.0, 2.0) */
849 if (0.5 <= f && f < 1.0) {
850 f *= 2.0;
851 e--;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000852 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000853 else if (f == 0.0) {
854 e = 0;
855 }
856 else {
857 PyErr_SetString(PyExc_SystemError,
858 "frexp() result out of range");
859 return -1;
860 }
861
862 if (e >= 1024) {
863 /* XXX 1024 itself is reserved for Inf/NaN */
864 PyErr_SetString(PyExc_OverflowError,
865 "float too large to pack with d format");
866 return -1;
867 }
868 else if (e < -1022) {
869 /* Gradual underflow */
870 f = ldexp(f, 1022 + e);
871 e = 0;
872 }
873 else {
874 e += 1023;
875 f -= 1.0; /* Get rid of leading 1 */
876 }
877
878 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
879 f *= 268435456.0; /* 2**28 */
880 fhi = (long) floor(f); /* Truncate */
881 f -= (double)fhi;
882 f *= 16777216.0; /* 2**24 */
883 flo = (long) floor(f + 0.5); /* Round */
884
885 /* First byte */
886 *p = (s<<7) | (e>>4);
887 p++;
888
889 /* Second byte */
890 *p = ((e&0xF)<<4) | (fhi>>24);
891 p++;
892
893 /* Third byte */
894 *p = (fhi>>16) & 0xFF;
895 p++;
896
897 /* Fourth byte */
898 *p = (fhi>>8) & 0xFF;
899 p++;
900
901 /* Fifth byte */
902 *p = fhi & 0xFF;
903 p++;
904
905 /* Sixth byte */
906 *p = (flo>>16) & 0xFF;
907 p++;
908
909 /* Seventh byte */
910 *p = (flo>>8) & 0xFF;
911 p++;
912
913 /* Eighth byte */
914 *p = flo & 0xFF;
915
916 if ((*self->write_func)(self, str, 9) < 0)
917 return -1;
918 }
919 else
920#endif
921 {
922 char c_str[250];
923 c_str[0] = FLOAT;
Guido van Rossum104be4a1998-04-03 21:13:02 +0000924 sprintf(c_str + 1, "%.17g\n", x);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000925
926 if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
927 return -1;
928 }
929
930 return 0;
931}
932
933
934static int
Guido van Rossum142eeb81997-08-13 03:14:41 +0000935save_string(Picklerobject *self, PyObject *args, int doput) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000936 int size, len;
937
938 size = PyString_Size(args);
939
940 if (!self->bin) {
941 PyObject *repr;
942 char *repr_str;
943
944 static char string = STRING;
945
946 UNLESS(repr = PyObject_Repr(args))
947 return -1;
948
949 repr_str = PyString_AS_STRING((PyStringObject *)repr);
950 len = PyString_Size(repr);
951
952 if ((*self->write_func)(self, &string, 1) < 0)
953 return -1;
954
955 if ((*self->write_func)(self, repr_str, len) < 0)
956 return -1;
957
958 if ((*self->write_func)(self, "\n", 1) < 0)
959 return -1;
960
961 Py_XDECREF(repr);
962 }
963 else {
964 int i;
965 char c_str[5];
966
967 size = PyString_Size(args);
968
969 if (size < 256) {
970 c_str[0] = SHORT_BINSTRING;
971 c_str[1] = size;
972 len = 2;
973 }
974 else {
975 c_str[0] = BINSTRING;
976 for (i = 1; i < 5; i++)
977 c_str[i] = (int)(size >> ((i - 1) * 8));
978 len = 5;
979 }
980
981 if ((*self->write_func)(self, c_str, len) < 0)
982 return -1;
983
984 if ((*self->write_func)(self,
985 PyString_AS_STRING((PyStringObject *)args), size) < 0)
986 return -1;
987 }
988
Guido van Rossum142eeb81997-08-13 03:14:41 +0000989 if (doput)
990 if (put(self, args) < 0)
991 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000992
993 return 0;
994}
995
996
997static int
998save_tuple(Picklerobject *self, PyObject *args) {
999 PyObject *element = 0, *py_tuple_id = 0;
1000 int len, i, has_key, res = -1;
1001
1002 static char tuple = TUPLE;
1003
1004 if ((*self->write_func)(self, &MARKv, 1) < 0)
1005 goto finally;
1006
1007 if ((len = PyTuple_Size(args)) < 0)
1008 goto finally;
1009
1010 for (i = 0; i < len; i++) {
1011 UNLESS(element = PyTuple_GET_ITEM((PyTupleObject *)args, i))
1012 goto finally;
1013
1014 if (save(self, element, 0) < 0)
1015 goto finally;
1016 }
1017
1018 UNLESS(py_tuple_id = PyInt_FromLong((long)args))
1019 goto finally;
1020
1021 if (len) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001022 if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_tuple_id)) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001023 goto finally;
1024
1025 if (has_key) {
1026 if (self->bin) {
1027 static char pop_mark = POP_MARK;
1028
1029 if ((*self->write_func)(self, &pop_mark, 1) < 0)
1030 goto finally;
1031 }
1032 else {
1033 static char pop = POP;
1034
1035 for (i = 0; i <= len; i++) {
1036 if ((*self->write_func)(self, &pop, 1) < 0)
1037 goto finally;
1038 }
1039 }
1040
1041 if (get(self, py_tuple_id) < 0)
1042 goto finally;
1043
1044 res = 0;
1045 goto finally;
1046 }
1047 }
1048
1049 if ((*self->write_func)(self, &tuple, 1) < 0) {
1050 goto finally;
1051 }
1052
1053 if (put(self, args) < 0)
1054 goto finally;
1055
1056 res = 0;
1057
1058finally:
1059 Py_XDECREF(py_tuple_id);
1060
1061 return res;
1062}
1063
1064static int
1065save_empty_tuple(Picklerobject *self, PyObject *args) {
1066 static char tuple = EMPTY_TUPLE;
1067
1068 return (*self->write_func)(self, &tuple, 1);
1069}
1070
1071
1072static int
1073save_list(Picklerobject *self, PyObject *args) {
1074 PyObject *element = 0;
1075 int s_len, len, i, using_appends, res = -1;
1076 char s[3];
1077
1078 static char append = APPEND, appends = APPENDS;
1079
1080 if(self->bin) {
1081 s[0] = EMPTY_LIST;
1082 s_len = 1;
1083 }
1084 else {
1085 s[0] = MARK;
1086 s[1] = LIST;
1087 s_len = 2;
1088 }
1089
1090 if ((len = PyList_Size(args)) < 0)
1091 goto finally;
1092
1093 if ((*self->write_func)(self, s, s_len) < 0)
1094 goto finally;
1095
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001096 if (len == 0) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001097 if (put(self, args) < 0)
1098 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001099 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001100 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001101 if (put2(self, args) < 0)
1102 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001103 }
1104
Guido van Rossum142eeb81997-08-13 03:14:41 +00001105 if ((using_appends = (self->bin && (len > 1))))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001106 if ((*self->write_func)(self, &MARKv, 1) < 0)
1107 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001108
Guido van Rossum60456fd1997-04-09 17:36:32 +00001109 for (i = 0; i < len; i++) {
1110 UNLESS(element = PyList_GET_ITEM((PyListObject *)args, i))
1111 goto finally;
1112
1113 if (save(self, element, 0) < 0)
1114 goto finally;
1115
1116 if (!using_appends) {
1117 if ((*self->write_func)(self, &append, 1) < 0)
1118 goto finally;
1119 }
1120 }
1121
1122 if (using_appends) {
1123 if ((*self->write_func)(self, &appends, 1) < 0)
1124 goto finally;
1125 }
1126
1127 res = 0;
1128
1129finally:
1130
1131 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001132}
1133
1134
Guido van Rossum60456fd1997-04-09 17:36:32 +00001135static int
1136save_dict(Picklerobject *self, PyObject *args) {
1137 PyObject *key = 0, *value = 0;
1138 int i, len, res = -1, using_setitems;
1139 char s[3];
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001140
Guido van Rossum60456fd1997-04-09 17:36:32 +00001141 static char setitem = SETITEM, setitems = SETITEMS;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001142
Guido van Rossum60456fd1997-04-09 17:36:32 +00001143 if (self->bin) {
1144 s[0] = EMPTY_DICT;
1145 len = 1;
1146 }
1147 else {
1148 s[0] = MARK;
1149 s[1] = DICT;
1150 len = 2;
1151 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001152
Guido van Rossum60456fd1997-04-09 17:36:32 +00001153 if ((*self->write_func)(self, s, len) < 0)
1154 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001155
Guido van Rossum60456fd1997-04-09 17:36:32 +00001156 if ((len = PyDict_Size(args)) < 0)
1157 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001158
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001159 if (len == 0) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001160 if (put(self, args) < 0)
1161 goto finally;
1162 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001163 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001164 if (put2(self, args) < 0)
1165 goto finally;
1166 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001167
Guido van Rossum142eeb81997-08-13 03:14:41 +00001168 if ((using_setitems = (self->bin && (PyDict_Size(args) > 1))))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001169 if ((*self->write_func)(self, &MARKv, 1) < 0)
1170 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001171
Guido van Rossum60456fd1997-04-09 17:36:32 +00001172 i = 0;
1173 while (PyDict_Next(args, &i, &key, &value)) {
1174 if (save(self, key, 0) < 0)
1175 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001176
Guido van Rossum60456fd1997-04-09 17:36:32 +00001177 if (save(self, value, 0) < 0)
1178 goto finally;
1179
1180 if (!using_setitems) {
1181 if ((*self->write_func)(self, &setitem, 1) < 0)
1182 goto finally;
1183 }
1184 }
1185
1186 if (using_setitems) {
1187 if ((*self->write_func)(self, &setitems, 1) < 0)
1188 goto finally;
1189 }
1190
1191 res = 0;
1192
1193finally:
1194
1195 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001196}
1197
1198
Guido van Rossum60456fd1997-04-09 17:36:32 +00001199static int
1200save_inst(Picklerobject *self, PyObject *args) {
1201 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1202 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1203 char *module_str, *name_str;
1204 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001205
Guido van Rossum60456fd1997-04-09 17:36:32 +00001206 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001207
Guido van Rossum60456fd1997-04-09 17:36:32 +00001208 if ((*self->write_func)(self, &MARKv, 1) < 0)
1209 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001210
Guido van Rossum60456fd1997-04-09 17:36:32 +00001211 UNLESS(class = PyObject_GetAttr(args, __class___str))
1212 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001213
Guido van Rossum60456fd1997-04-09 17:36:32 +00001214 if (self->bin) {
1215 if (save(self, class, 0) < 0)
1216 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001217 }
1218
Guido van Rossum142eeb81997-08-13 03:14:41 +00001219 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001220 PyObject *element = 0;
1221 int i, len;
1222
1223 UNLESS(class_args =
1224 PyObject_CallObject(getinitargs_func, empty_tuple))
1225 goto finally;
1226
1227 if ((len = PyObject_Length(class_args)) < 0)
1228 goto finally;
1229
1230 for (i = 0; i < len; i++) {
1231 UNLESS(element = PySequence_GetItem(class_args, i))
1232 goto finally;
1233
1234 if (save(self, element, 0) < 0) {
1235 Py_DECREF(element);
1236 goto finally;
1237 }
1238
1239 Py_DECREF(element);
1240 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001241 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001242 else {
1243 PyErr_Clear();
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001244 }
1245
Guido van Rossum60456fd1997-04-09 17:36:32 +00001246 if (!self->bin) {
1247 UNLESS(name = ((PyClassObject *)class)->cl_name) {
1248 PyErr_SetString(PicklingError, "class has no name");
1249 goto finally;
1250 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001251
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001252 UNLESS(module = whichmodule(self->class_map, class, name))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001253 goto finally;
1254
1255 module_str = PyString_AS_STRING((PyStringObject *)module);
1256 module_size = PyString_Size(module);
1257 name_str = PyString_AS_STRING((PyStringObject *)name);
1258 name_size = PyString_Size(name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001259
Guido van Rossum60456fd1997-04-09 17:36:32 +00001260 if ((*self->write_func)(self, &inst, 1) < 0)
1261 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001262
Guido van Rossum60456fd1997-04-09 17:36:32 +00001263 if ((*self->write_func)(self, module_str, module_size) < 0)
1264 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001265
Guido van Rossum60456fd1997-04-09 17:36:32 +00001266 if ((*self->write_func)(self, "\n", 1) < 0)
1267 goto finally;
1268
1269 if ((*self->write_func)(self, name_str, name_size) < 0)
1270 goto finally;
1271
1272 if ((*self->write_func)(self, "\n", 1) < 0)
1273 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001274 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001275 else if ((*self->write_func)(self, &obj, 1) < 0) {
1276 goto finally;
1277 }
1278
Guido van Rossum142eeb81997-08-13 03:14:41 +00001279 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001280 UNLESS(state = PyObject_CallObject(getstate_func, empty_tuple))
1281 goto finally;
1282 }
1283 else {
1284 PyErr_Clear();
1285
1286 UNLESS(state = PyObject_GetAttr(args, __dict___str)) {
1287 PyErr_Clear();
1288 res = 0;
1289 goto finally;
1290 }
1291 }
1292
1293 if (!PyDict_Check(state)) {
1294 if (put2(self, args) < 0)
1295 goto finally;
1296 }
1297 else {
1298 if (put(self, args) < 0)
1299 goto finally;
1300 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001301
Guido van Rossum60456fd1997-04-09 17:36:32 +00001302 if (save(self, state, 0) < 0)
1303 goto finally;
1304
1305 if ((*self->write_func)(self, &build, 1) < 0)
1306 goto finally;
1307
1308 res = 0;
1309
1310finally:
1311 Py_XDECREF(module);
1312 Py_XDECREF(class);
1313 Py_XDECREF(state);
1314 Py_XDECREF(getinitargs_func);
1315 Py_XDECREF(getstate_func);
1316 Py_XDECREF(class_args);
1317
1318 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001319}
1320
1321
Guido van Rossum60456fd1997-04-09 17:36:32 +00001322static int
1323save_global(Picklerobject *self, PyObject *args, PyObject *name) {
1324 PyObject *global_name = 0, *module = 0;
1325 char *name_str, *module_str;
1326 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001327
Guido van Rossum60456fd1997-04-09 17:36:32 +00001328 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001329
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001330 if (name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001331 global_name = name;
1332 Py_INCREF(global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001333 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001334 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001335 UNLESS(global_name = PyObject_GetAttr(args, __name___str))
1336 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001337 }
1338
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001339 UNLESS(module = whichmodule(self->class_map, args, global_name))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001340 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001341
1342 module_str = PyString_AS_STRING((PyStringObject *)module);
1343 module_size = PyString_Size(module);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001344 name_str = PyString_AS_STRING((PyStringObject *)global_name);
1345 name_size = PyString_Size(global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001346
Guido van Rossum60456fd1997-04-09 17:36:32 +00001347 if ((*self->write_func)(self, &global, 1) < 0)
1348 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001349
Guido van Rossum60456fd1997-04-09 17:36:32 +00001350 if ((*self->write_func)(self, module_str, module_size) < 0)
1351 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001352
Guido van Rossum60456fd1997-04-09 17:36:32 +00001353 if ((*self->write_func)(self, "\n", 1) < 0)
1354 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001355
Guido van Rossum60456fd1997-04-09 17:36:32 +00001356 if ((*self->write_func)(self, name_str, name_size) < 0)
1357 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001358
Guido van Rossum60456fd1997-04-09 17:36:32 +00001359 if ((*self->write_func)(self, "\n", 1) < 0)
1360 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001361
Guido van Rossum60456fd1997-04-09 17:36:32 +00001362 if (put(self, args) < 0)
1363 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001364
Guido van Rossum60456fd1997-04-09 17:36:32 +00001365 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001366
Guido van Rossum60456fd1997-04-09 17:36:32 +00001367finally:
1368 Py_XDECREF(module);
1369 Py_XDECREF(global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001370
Guido van Rossum60456fd1997-04-09 17:36:32 +00001371 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001372}
1373
Guido van Rossum60456fd1997-04-09 17:36:32 +00001374static int
1375save_pers(Picklerobject *self, PyObject *args, PyObject *f) {
1376 PyObject *pid = 0;
1377 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001378
Guido van Rossum60456fd1997-04-09 17:36:32 +00001379 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001380
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001381 UNLESS(self->arg)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001382 UNLESS(self->arg = PyTuple_New(1))
1383 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001384
Guido van Rossum60456fd1997-04-09 17:36:32 +00001385 Py_INCREF(args);
1386 if (PyTuple_SetItem(self->arg, 0, args) < 0)
1387 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001388
Guido van Rossum60456fd1997-04-09 17:36:32 +00001389 UNLESS(pid = PyObject_CallObject(f, self->arg))
1390 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001391
Guido van Rossum60456fd1997-04-09 17:36:32 +00001392 if (pid != Py_None) {
1393 if (!self->bin) {
1394 if (!PyString_Check(pid)) {
1395 PyErr_SetString(PicklingError,
1396 "persistent id must be string");
1397 goto finally;
1398 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001399
Guido van Rossum60456fd1997-04-09 17:36:32 +00001400 if ((*self->write_func)(self, &persid, 1) < 0)
1401 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001402
Guido van Rossum60456fd1997-04-09 17:36:32 +00001403 if ((size = PyString_Size(pid)) < 0)
1404 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001405
Guido van Rossum60456fd1997-04-09 17:36:32 +00001406 if ((*self->write_func)(self,
1407 PyString_AS_STRING((PyStringObject *)pid), size) < 0)
1408 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001409
Guido van Rossum60456fd1997-04-09 17:36:32 +00001410 if ((*self->write_func)(self, "\n", 1) < 0)
1411 goto finally;
1412
1413 res = 1;
1414 goto finally;
1415 }
1416 else if (save(self, pid, 1) >= 0) {
1417 if ((*self->write_func)(self, &binpersid, 1) < 0)
1418 res = -1;
1419 else
1420 res = 1;
1421 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001422
Guido van Rossum60456fd1997-04-09 17:36:32 +00001423 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001424 }
1425
Guido van Rossum60456fd1997-04-09 17:36:32 +00001426 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001427
Guido van Rossum60456fd1997-04-09 17:36:32 +00001428finally:
1429 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001430
Guido van Rossum60456fd1997-04-09 17:36:32 +00001431 return res;
1432}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001433
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001434
Guido van Rossum60456fd1997-04-09 17:36:32 +00001435static int
1436save_reduce(Picklerobject *self, PyObject *callable,
1437 PyObject *tup, PyObject *state, PyObject *ob) {
1438 static char reduce = REDUCE, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001439
Guido van Rossum60456fd1997-04-09 17:36:32 +00001440 if (save(self, callable, 0) < 0)
1441 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001442
Guido van Rossum60456fd1997-04-09 17:36:32 +00001443 if (save(self, tup, 0) < 0)
1444 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001445
Guido van Rossum60456fd1997-04-09 17:36:32 +00001446 if ((*self->write_func)(self, &reduce, 1) < 0)
1447 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001448
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001449 if (ob != NULL) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001450 if (state && !PyDict_Check(state)) {
1451 if (put2(self, ob) < 0)
1452 return -1;
1453 }
1454 else {
1455 if (put(self, ob) < 0)
1456 return -1;
1457 }
1458 }
1459
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001460 if (state) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001461 if (save(self, state, 0) < 0)
1462 return -1;
1463
1464 if ((*self->write_func)(self, &build, 1) < 0)
1465 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001466 }
1467
Guido van Rossum60456fd1997-04-09 17:36:32 +00001468 return 0;
1469}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001470
Guido van Rossum60456fd1997-04-09 17:36:32 +00001471static int
1472save(Picklerobject *self, PyObject *args, int pers_save) {
1473 PyTypeObject *type;
1474 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0, *arg_tup = 0,
Guido van Rossum142eeb81997-08-13 03:14:41 +00001475 *callable = 0, *state = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001476 int res = -1, tmp, size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001477
Guido van Rossum60456fd1997-04-09 17:36:32 +00001478 if (!pers_save && self->pers_func) {
1479 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
1480 res = tmp;
1481 goto finally;
1482 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001483 }
1484
Guido van Rossum60456fd1997-04-09 17:36:32 +00001485 if (args == Py_None) {
1486 res = save_none(self, args);
1487 goto finally;
1488 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001489
Guido van Rossum60456fd1997-04-09 17:36:32 +00001490 type = args->ob_type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001491
Guido van Rossum60456fd1997-04-09 17:36:32 +00001492 switch (type->tp_name[0]) {
1493 case 'i':
1494 if (type == &PyInt_Type) {
1495 res = save_int(self, args);
1496 goto finally;
1497 }
1498 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001499
Guido van Rossum60456fd1997-04-09 17:36:32 +00001500 case 'l':
1501 if (type == &PyLong_Type) {
1502 res = save_long(self, args);
1503 goto finally;
1504 }
1505 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001506
Guido van Rossum60456fd1997-04-09 17:36:32 +00001507 case 'f':
1508 if (type == &PyFloat_Type) {
1509 res = save_float(self, args);
1510 goto finally;
1511 }
1512 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001513
Guido van Rossum60456fd1997-04-09 17:36:32 +00001514 case 't':
1515 if (type == &PyTuple_Type && PyTuple_Size(args)==0) {
1516 if(self->bin) res = save_empty_tuple(self, args);
1517 else res = save_tuple(self, args);
1518 goto finally;
1519 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001520
Guido van Rossum60456fd1997-04-09 17:36:32 +00001521 case 's':
1522 if ((type == &PyString_Type) && (PyString_Size(args) < 2)) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001523 res = save_string(self, args, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001524 goto finally;
1525 }
1526 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001527
Guido van Rossum60456fd1997-04-09 17:36:32 +00001528 if (args->ob_refcnt > 1) {
1529 long ob_id;
1530 int has_key;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001531
Guido van Rossum60456fd1997-04-09 17:36:32 +00001532 ob_id = (long)args;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001533
Guido van Rossum60456fd1997-04-09 17:36:32 +00001534 UNLESS(py_ob_id = PyInt_FromLong(ob_id))
1535 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001536
Guido van Rossum60456fd1997-04-09 17:36:32 +00001537 if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_ob_id)) < 0)
1538 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001539
Guido van Rossum60456fd1997-04-09 17:36:32 +00001540 if (has_key) {
1541 if (get(self, py_ob_id) < 0)
1542 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001543
Guido van Rossum60456fd1997-04-09 17:36:32 +00001544 res = 0;
1545 goto finally;
1546 }
1547 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001548
Guido van Rossum60456fd1997-04-09 17:36:32 +00001549 switch (type->tp_name[0]) {
1550 case 's':
1551 if (type == &PyString_Type) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001552 res = save_string(self, args, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001553 goto finally;
1554 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001555
Guido van Rossum60456fd1997-04-09 17:36:32 +00001556 case 't':
1557 if (type == &PyTuple_Type) {
1558 res = save_tuple(self, args);
1559 goto finally;
1560 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001561
Guido van Rossum60456fd1997-04-09 17:36:32 +00001562 case 'l':
1563 if (type == &PyList_Type) {
1564 res = save_list(self, args);
1565 goto finally;
1566 }
1567
1568 case 'd':
1569 if (type == &PyDict_Type) {
1570 res = save_dict(self, args);
1571 goto finally;
1572 }
1573
1574 case 'i':
1575 if (type == &PyInstance_Type) {
1576 res = save_inst(self, args);
1577 goto finally;
1578 }
1579
1580 case 'c':
1581 if (type == &PyClass_Type) {
1582 res = save_global(self, args, NULL);
1583 goto finally;
1584 }
1585
1586 case 'f':
1587 if (type == &PyFunction_Type) {
1588 res = save_global(self, args, NULL);
1589 goto finally;
1590 }
1591
1592 case 'b':
1593 if (type == &PyCFunction_Type) {
1594 res = save_global(self, args, NULL);
1595 goto finally;
1596 }
1597 }
1598
1599 if (!pers_save && self->inst_pers_func) {
1600 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
1601 res = tmp;
1602 goto finally;
1603 }
1604 }
1605
Guido van Rossum142eeb81997-08-13 03:14:41 +00001606 if ((__reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001607 Py_INCREF(__reduce__);
1608
1609 UNLESS(self->arg)
1610 UNLESS(self->arg = PyTuple_New(1))
1611 goto finally;
1612
1613 Py_INCREF(args);
1614 if (PyTuple_SetItem(self->arg, 0, args) < 0)
1615 goto finally;
1616
1617 UNLESS(t = PyObject_CallObject(__reduce__, self->arg))
1618 goto finally;
1619 }
1620 else {
1621 PyErr_Clear();
1622
Guido van Rossum142eeb81997-08-13 03:14:41 +00001623 if ((__reduce__ = PyObject_GetAttr(args, __reduce___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001624 UNLESS(t = PyObject_CallObject(__reduce__, empty_tuple))
1625 goto finally;
1626 }
1627 else {
1628 PyErr_Clear();
1629 }
1630 }
1631
1632 if (t) {
1633 if (PyString_Check(t)) {
1634 res = save_global(self, args, t);
1635 goto finally;
1636 }
1637
1638 if (!PyTuple_Check(t)) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001639 cPickle_ErrFormat(PicklingError, "Value returned by %s must "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001640 "be a tuple", "O", __reduce__);
1641 goto finally;
1642 }
1643
1644 size = PyTuple_Size(t);
1645
1646 if ((size != 3) && (size != 2)) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001647 cPickle_ErrFormat(PicklingError, "tuple returned by %s must "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001648 "contain only two or three elements", "O", __reduce__);
1649 goto finally;
1650 }
1651
1652 callable = PyTuple_GET_ITEM(t, 0);
Guido van Rossum142eeb81997-08-13 03:14:41 +00001653
Guido van Rossum60456fd1997-04-09 17:36:32 +00001654 arg_tup = PyTuple_GET_ITEM(t, 1);
1655
1656 if (size > 2) {
1657 state = PyTuple_GET_ITEM(t, 2);
1658 }
1659
Guido van Rossum9716aaa1997-12-08 15:15:16 +00001660 UNLESS(PyTuple_Check(arg_tup) || arg_tup==Py_None) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001661 cPickle_ErrFormat(PicklingError, "Second element of tuple "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001662 "returned by %s must be a tuple", "O", __reduce__);
1663 goto finally;
1664 }
1665
1666 res = save_reduce(self, callable, arg_tup, state, args);
1667 goto finally;
1668 }
1669
1670 /*
1671 if (PyObject_HasAttrString(args, "__class__")) {
1672 res = save_inst(self, args);
1673 goto finally;
1674 }
1675 */
1676
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001677 cPickle_ErrFormat(PicklingError, "Cannot pickle %s objects.",
Guido van Rossum60456fd1997-04-09 17:36:32 +00001678 "O", (PyObject *)type);
1679
1680finally:
1681 Py_XDECREF(py_ob_id);
1682 Py_XDECREF(__reduce__);
1683 Py_XDECREF(t);
1684
1685 return res;
1686}
1687
1688
1689static int
1690dump(Picklerobject *self, PyObject *args) {
1691 static char stop = STOP;
1692
1693 if (save(self, args, 0) < 0)
1694 return -1;
1695
1696 if ((*self->write_func)(self, &stop, 1) < 0)
1697 return -1;
1698
1699 if ((*self->write_func)(self, NULL, 0) < 0)
1700 return -1;
1701
1702 return 0;
1703}
1704
1705static PyObject *
1706Pickler_dump(Picklerobject *self, PyObject *args) {
1707 PyObject *ob;
1708
1709 UNLESS(PyArg_ParseTuple(args, "O", &ob))
1710 return NULL;
1711
1712 if (dump(self, ob) < 0)
1713 return NULL;
1714
1715 Py_INCREF(Py_None);
1716 return Py_None;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001717}
1718
1719
1720static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00001721dump_special(Picklerobject *self, PyObject *args) {
1722 static char stop = STOP;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001723
Guido van Rossum60456fd1997-04-09 17:36:32 +00001724 PyObject *callable, *arg_tup, *state = NULL;
1725
1726 UNLESS(PyArg_ParseTuple(args, "OO|O", &callable, &arg_tup, &state))
1727 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001728
Guido van Rossum60456fd1997-04-09 17:36:32 +00001729 UNLESS(PyTuple_Check(arg_tup)) {
1730 PyErr_SetString(PicklingError, "Second arg to dump_special must "
1731 "be tuple");
1732 return NULL;
1733 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001734
Guido van Rossum60456fd1997-04-09 17:36:32 +00001735 if (save_reduce(self, callable, arg_tup, state, NULL) < 0)
1736 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001737
Guido van Rossum60456fd1997-04-09 17:36:32 +00001738 if ((*self->write_func)(self, &stop, 1) < 0)
1739 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001740
Guido van Rossum60456fd1997-04-09 17:36:32 +00001741 if ((*self->write_func)(self, NULL, 0) < 0)
1742 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001743
Guido van Rossum60456fd1997-04-09 17:36:32 +00001744 Py_INCREF(Py_None);
1745 return Py_None;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001746}
1747
Guido van Rossum142eeb81997-08-13 03:14:41 +00001748static PyObject *
1749Pickle_clear_memo(Picklerobject *self, PyObject *args) {
1750 if(self->memo) PyDict_Clear(self->memo);
1751 Py_INCREF(Py_None);
1752 return Py_None;
1753}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001754
1755static struct PyMethodDef Pickler_methods[] = {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001756 {"dump", (PyCFunction)Pickler_dump, 1,
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001757 "dump(object) --"
1758 "Write an object in pickle format to the object's pickle stream\n"
1759 },
Guido van Rossum142eeb81997-08-13 03:14:41 +00001760 {"dump_special", (PyCFunction)dump_special, 1,
1761 ""},
1762 {"clear_memo", (PyCFunction)Pickle_clear_memo, 1,
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001763 "clear_memo() -- Clear the picklers memo"},
Guido van Rossum60456fd1997-04-09 17:36:32 +00001764 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001765};
1766
1767
1768static Picklerobject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00001769newPicklerobject(PyObject *file, int bin) {
1770 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001771
Guido van Rossum60456fd1997-04-09 17:36:32 +00001772 UNLESS(self = PyObject_NEW(Picklerobject, &Picklertype))
1773 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001774
1775 self->fp = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001776 self->write = NULL;
1777 self->memo = NULL;
1778 self->arg = NULL;
1779 self->pers_func = NULL;
1780 self->inst_pers_func = NULL;
1781 self->write_buf = NULL;
1782 self->bin = bin;
1783 self->buf_size = 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001784 self->class_map = NULL;
1785 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001786
Guido van Rossum60456fd1997-04-09 17:36:32 +00001787 Py_INCREF(file);
1788 self->file = file;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001789
Guido van Rossum60456fd1997-04-09 17:36:32 +00001790 UNLESS(self->memo = PyDict_New()) {
1791 Py_XDECREF((PyObject *)self);
1792 return NULL;
1793 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001794
Guido van Rossum60456fd1997-04-09 17:36:32 +00001795 if (PyFile_Check(file)) {
1796 self->fp = PyFile_AsFile(file);
1797 self->write_func = write_file;
1798 }
1799 else if (PycStringIO_OutputCheck(file)) {
1800 self->write_func = write_cStringIO;
1801 }
Guido van Rossum142eeb81997-08-13 03:14:41 +00001802 else if (file == Py_None) {
1803 self->write_func = write_none;
1804 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001805 else {
1806 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001807
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001808 UNLESS(self->write = PyObject_GetAttr(file, write_str)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001809 PyErr_Clear();
1810 PyErr_SetString(PyExc_TypeError, "argument must have 'write' "
1811 "attribute");
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001812 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001813 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001814
Guido van Rossum60456fd1997-04-09 17:36:32 +00001815 UNLESS(self->write_buf =
1816 (char *)malloc(WRITE_BUF_SIZE * sizeof(char))) {
1817 PyErr_NoMemory();
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001818 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001819 }
1820 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001821
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001822 if(PyEval_GetRestricted()) {
1823 /* Restricted execution, get private tables */
1824 PyObject *m;
1825
1826 UNLESS(self->class_map=PyDict_New()) goto err;
1827 UNLESS(m=PyImport_Import(copy_reg_str)) goto err;
1828 self->dispatch_table=PyObject_GetAttr(m, dispatch_table_str);
1829 Py_DECREF(m);
1830 UNLESS(self->dispatch_table) goto err;
1831 }
1832 else {
1833 self->class_map=class_map;
1834 Py_INCREF(class_map);
1835 self->dispatch_table=dispatch_table;
1836 Py_INCREF(dispatch_table);
1837 }
1838
Guido van Rossum60456fd1997-04-09 17:36:32 +00001839 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001840
1841err:
1842 Py_DECREF((PyObject *)self);
1843 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001844}
1845
1846
1847static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00001848get_Pickler(PyObject *self, PyObject *args) {
1849 PyObject *file;
1850 int bin = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001851
Guido van Rossum60456fd1997-04-09 17:36:32 +00001852 UNLESS(PyArg_ParseTuple(args, "O|i", &file, &bin)) return NULL;
1853 return (PyObject *)newPicklerobject(file, bin);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001854}
1855
1856
1857static void
Guido van Rossum60456fd1997-04-09 17:36:32 +00001858Pickler_dealloc(Picklerobject *self) {
1859 Py_XDECREF(self->write);
1860 Py_XDECREF(self->memo);
1861 Py_XDECREF(self->arg);
1862 Py_XDECREF(self->file);
1863 Py_XDECREF(self->pers_func);
1864 Py_XDECREF(self->inst_pers_func);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001865 Py_XDECREF(self->class_map);
1866 Py_XDECREF(self->dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001867
1868 if (self->write_buf) {
1869 free(self->write_buf);
1870 }
1871
1872 PyMem_DEL(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001873}
1874
1875
1876static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00001877Pickler_getattr(Picklerobject *self, char *name) {
1878 if (strcmp(name, "persistent_id") == 0) {
1879 if (!self->pers_func) {
1880 PyErr_SetString(PyExc_AttributeError, name);
1881 return NULL;
1882 }
1883
1884 Py_INCREF(self->pers_func);
1885 return self->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001886 }
1887
Guido van Rossum60456fd1997-04-09 17:36:32 +00001888 if (strcmp(name, "memo") == 0) {
1889 if (!self->memo) {
1890 PyErr_SetString(PyExc_AttributeError, name);
1891 return NULL;
1892 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001893
Guido van Rossum60456fd1997-04-09 17:36:32 +00001894 Py_INCREF(self->memo);
1895 return self->memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001896 }
1897
Guido van Rossum60456fd1997-04-09 17:36:32 +00001898 if (strcmp(name, "PicklingError") == 0) {
1899 Py_INCREF(PicklingError);
1900 return PicklingError;
1901 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001902
Guido van Rossum60456fd1997-04-09 17:36:32 +00001903 return Py_FindMethod(Pickler_methods, (PyObject *)self, name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001904}
1905
1906
1907int
Guido van Rossum60456fd1997-04-09 17:36:32 +00001908Pickler_setattr(Picklerobject *self, char *name, PyObject *value) {
1909 if (strcmp(name, "persistent_id") == 0) {
1910 Py_XDECREF(self->pers_func);
1911 self->pers_func = value;
1912 Py_INCREF(value);
1913 return 0;
1914 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001915
Guido van Rossum60456fd1997-04-09 17:36:32 +00001916 if (strcmp(name, "inst_persistent_id") == 0) {
1917 Py_XDECREF(self->inst_pers_func);
1918 self->inst_pers_func = value;
1919 Py_INCREF(value);
1920 return 0;
1921 }
1922
1923 PyErr_SetString(PyExc_AttributeError, name);
1924 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001925}
1926
1927
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001928static char Picklertype__doc__[] =
1929"Objects that know how to pickle objects\n"
1930;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001931
Guido van Rossum9716aaa1997-12-08 15:15:16 +00001932static PyTypeObject Picklertype = {
1933 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001934 0, /*ob_size*/
1935 "Pickler", /*tp_name*/
1936 sizeof(Picklerobject), /*tp_basicsize*/
1937 0, /*tp_itemsize*/
1938 /* methods */
1939 (destructor)Pickler_dealloc, /*tp_dealloc*/
1940 (printfunc)0, /*tp_print*/
1941 (getattrfunc)Pickler_getattr, /*tp_getattr*/
1942 (setattrfunc)Pickler_setattr, /*tp_setattr*/
1943 (cmpfunc)0, /*tp_compare*/
1944 (reprfunc)0, /*tp_repr*/
1945 0, /*tp_as_number*/
1946 0, /*tp_as_sequence*/
1947 0, /*tp_as_mapping*/
1948 (hashfunc)0, /*tp_hash*/
1949 (ternaryfunc)0, /*tp_call*/
1950 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001951
Guido van Rossum60456fd1997-04-09 17:36:32 +00001952 /* Space for future expansion */
1953 0L,0L,0L,0L,
1954 Picklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00001955};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001956
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001957static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001958find_class(PyObject *class_map,
1959 PyObject *py_module_name, PyObject *py_global_name) {
1960 PyObject *global = 0, *t = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001961
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001962 UNLESS(t = PyTuple_New(2)) return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001963
Guido van Rossum60456fd1997-04-09 17:36:32 +00001964 PyTuple_SET_ITEM((PyTupleObject *)t, 0, py_module_name);
1965 Py_INCREF(py_module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001966 PyTuple_SET_ITEM((PyTupleObject *)t, 1, py_global_name);
1967 Py_INCREF(py_global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001968
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001969 global=PyDict_GetItem(class_map, t);
1970
1971 if (global) {
1972 Py_DECREF(t);
1973 Py_INCREF(global);
1974 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001975 }
1976
Guido van Rossum60456fd1997-04-09 17:36:32 +00001977 PyErr_Clear();
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001978
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001979 UNLESS(module=PyImport_Import(py_module_name)) return NULL;
1980 global=PyObject_GetAttr(module, py_global_name);
1981 Py_DECREF(module);
1982 UNLESS(global) return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001983
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001984 if (PyDict_SetItem(class_map, t, global) < 0) global=NULL;
1985 Py_DECREF(t);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001986
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001987 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001988}
1989
1990
1991static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00001992marker(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001993 if (self->num_marks < 1) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001994 PyErr_SetString(UnpicklingError, "could not find MARK");
1995 return -1;
1996 }
1997
1998 return self->marks[--self->num_marks];
1999}
2000
2001
2002static int
2003load_none(Unpicklerobject *self) {
2004 if (PyList_Append(self->stack, Py_None) < 0)
2005 return -1;
2006
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002007 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002008}
2009
2010
2011static int
2012load_int(Unpicklerobject *self) {
2013 PyObject *py_int = 0;
2014 char *endptr, *s;
2015 int len, res = -1;
2016 long l;
2017
2018 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossum725d9411997-08-20 23:38:57 +00002019 UNLESS(s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002020
2021 errno = 0;
2022 l = strtol(s, &endptr, 0);
2023
2024 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
2025 /* Hm, maybe we've got something long. Let's try reading
2026 it as a Python long object. */
2027 errno=0;
2028 UNLESS(py_int=PyLong_FromString(s,&endptr,0)) goto finally;
2029
2030 if ((*endptr != '\n') || (endptr[1] != '\0')) {
2031 PyErr_SetString(PyExc_ValueError,
2032 "could not convert string to int");
2033 goto finally;
2034 }
2035 }
2036 else {
2037 UNLESS(py_int = PyInt_FromLong(l)) goto finally;
2038 }
2039
2040 if (PyList_Append(self->stack, py_int) < 0) goto finally;
2041
2042 res = 0;
2043
2044finally:
2045 free(s);
2046 Py_XDECREF(py_int);
2047
2048 return res;
2049}
2050
2051
2052static long
2053calc_binint(char *s, int x) {
2054 unsigned char c;
2055 int i;
2056 long l;
2057
2058 for (i = 0, l = 0L; i < x; i++) {
2059 c = (unsigned char)s[i];
2060 l |= (long)c << (i * 8);
2061 }
2062
2063 return l;
2064}
2065
2066
2067static int
2068load_binintx(Unpicklerobject *self, char *s, int x) {
2069 PyObject *py_int = 0;
2070 long l;
2071
2072 l = calc_binint(s, x);
2073
2074 UNLESS(py_int = PyInt_FromLong(l))
2075 return -1;
2076
2077 if (PyList_Append(self->stack, py_int) < 0) {
2078 Py_DECREF(py_int);
2079 return -1;
2080 }
2081
2082 Py_DECREF(py_int);
2083
2084 return 0;
2085}
2086
2087
2088static int
2089load_binint(Unpicklerobject *self) {
2090 char *s;
2091
2092 if ((*self->read_func)(self, &s, 4) < 0)
2093 return -1;
2094
2095 return load_binintx(self, s, 4);
2096}
2097
2098
2099static int
2100load_binint1(Unpicklerobject *self) {
2101 char *s;
2102
2103 if ((*self->read_func)(self, &s, 1) < 0)
2104 return -1;
2105
2106 return load_binintx(self, s, 1);
2107}
2108
2109
2110static int
2111load_binint2(Unpicklerobject *self) {
2112 char *s;
2113
2114 if ((*self->read_func)(self, &s, 2) < 0)
2115 return -1;
2116
2117 return load_binintx(self, s, 2);
2118}
2119
2120static int
2121load_long(Unpicklerobject *self) {
2122 PyObject *l = 0;
2123 char *end, *s;
2124 int len, res = -1;
2125
Guido van Rossum60456fd1997-04-09 17:36:32 +00002126 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossum725d9411997-08-20 23:38:57 +00002127 UNLESS(s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002128
2129 UNLESS(l = PyLong_FromString(s, &end, 0))
2130 goto finally;
2131
2132 if (PyList_Append(self->stack, l) < 0)
2133 goto finally;
2134
2135 res = 0;
2136
2137finally:
2138 free(s);
2139 Py_XDECREF(l);
2140
2141 return res;
2142}
2143
2144
2145static int
2146load_float(Unpicklerobject *self) {
2147 PyObject *py_float = 0;
2148 char *endptr, *s;
2149 int len, res = -1;
2150 double d;
2151
2152 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossum725d9411997-08-20 23:38:57 +00002153 UNLESS(s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002154
2155 errno = 0;
2156 d = strtod(s, &endptr);
2157
2158 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
2159 PyErr_SetString(PyExc_ValueError,
2160 "could not convert string to float");
2161 goto finally;
2162 }
2163
2164 UNLESS(py_float = PyFloat_FromDouble(d))
2165 goto finally;
2166
2167 if (PyList_Append(self->stack, py_float) < 0)
2168 goto finally;
2169
2170 res = 0;
2171
2172finally:
2173 free(s);
2174 Py_XDECREF(py_float);
2175
2176 return res;
2177}
2178
Guido van Rossum60456fd1997-04-09 17:36:32 +00002179static int
2180load_binfloat(Unpicklerobject *self) {
2181 PyObject *py_float = 0;
2182 int s, e, res = -1;
2183 long fhi, flo;
2184 double x;
2185 char *p;
2186
2187 if ((*self->read_func)(self, &p, 8) < 0)
2188 return -1;
2189
2190 /* First byte */
2191 s = (*p>>7) & 1;
2192 e = (*p & 0x7F) << 4;
2193 p++;
2194
2195 /* Second byte */
2196 e |= (*p>>4) & 0xF;
2197 fhi = (*p & 0xF) << 24;
2198 p++;
2199
2200 /* Third byte */
2201 fhi |= (*p & 0xFF) << 16;
2202 p++;
2203
2204 /* Fourth byte */
2205 fhi |= (*p & 0xFF) << 8;
2206 p++;
2207
2208 /* Fifth byte */
2209 fhi |= *p & 0xFF;
2210 p++;
2211
2212 /* Sixth byte */
2213 flo = (*p & 0xFF) << 16;
2214 p++;
2215
2216 /* Seventh byte */
2217 flo |= (*p & 0xFF) << 8;
2218 p++;
2219
2220 /* Eighth byte */
2221 flo |= *p & 0xFF;
2222
2223 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
2224 x /= 268435456.0; /* 2**28 */
2225
2226 /* XXX This sadly ignores Inf/NaN */
2227 if (e == 0)
2228 e = -1022;
2229 else {
2230 x += 1.0;
2231 e -= 1023;
2232 }
2233 x = ldexp(x, e);
2234
2235 if (s)
2236 x = -x;
2237
2238 UNLESS(py_float = PyFloat_FromDouble(x))
2239 goto finally;
2240
2241 if (PyList_Append(self->stack, py_float) < 0)
2242 goto finally;
2243
2244 res = 0;
2245
2246finally:
2247 Py_XDECREF(py_float);
2248
2249 return res;
2250}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002251
2252static int
2253load_string(Unpicklerobject *self) {
2254 PyObject *str = 0;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002255 int len, res = -1, nslash;
2256 char *s, q, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002257
2258 static PyObject *eval_dict = 0;
2259
2260 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossum725d9411997-08-20 23:38:57 +00002261 UNLESS(s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002262
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002263 /* Check for unquoted quotes (evil strings) */
2264 q=*s;
2265 if(q != '"' && q != '\'') goto insecure;
2266 for(p=s+1, nslash=0; *p; p++)
2267 {
2268 if(*p==q && nslash%2==0) break;
2269 if(*p=='\\') nslash++;
2270 else nslash=0;
2271 }
2272 if(*p==q)
2273 {
2274 for(p++; *p; p++) if(*p > ' ') goto insecure;
2275 }
2276 else goto insecure;
2277 /********************************************/
2278
Guido van Rossum60456fd1997-04-09 17:36:32 +00002279 UNLESS(eval_dict)
2280 UNLESS(eval_dict = Py_BuildValue("{s{}}", "__builtins__"))
2281 goto finally;
2282
Guido van Rossumb05a5c71997-05-07 17:46:13 +00002283 UNLESS(str = PyRun_String(s, Py_eval_input, eval_dict, eval_dict))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002284 goto finally;
2285
2286 if (PyList_Append(self->stack, str) < 0)
2287 goto finally;
2288
2289 res = 0;
2290
2291finally:
2292 free(s);
2293 Py_XDECREF(str);
2294
2295 return res;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002296
2297insecure:
2298 free(s);
2299 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
2300 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002301}
2302
2303
2304static int
2305load_binstring(Unpicklerobject *self) {
2306 PyObject *py_string = 0;
2307 long l;
2308 int res = -1;
2309 char *s;
2310
2311 if ((*self->read_func)(self, &s, 4) < 0)
2312 goto finally;
2313
2314 l = calc_binint(s, 4);
2315
2316 if ((*self->read_func)(self, &s, l) < 0)
2317 goto finally;
2318
2319 UNLESS(py_string = PyString_FromStringAndSize(s, l))
2320 goto finally;
2321
2322 if (PyList_Append(self->stack, py_string) < 0)
2323 goto finally;
2324
2325 res = 0;
2326
2327finally:
2328 Py_XDECREF(py_string);
2329
2330 return res;
2331}
2332
2333
2334static int
2335load_short_binstring(Unpicklerobject *self) {
2336 PyObject *py_string = 0;
2337 unsigned char l;
2338 int res = -1;
2339 char *s;
2340
2341 if ((*self->read_func)(self, &s, 1) < 0)
2342 return -1;
2343
2344 l = (unsigned char)s[0];
2345
2346 if ((*self->read_func)(self, &s, l) < 0)
2347 goto finally;
2348
2349 UNLESS(py_string = PyString_FromStringAndSize(s, l))
2350 goto finally;
2351
2352 if (PyList_Append(self->stack, py_string) < 0)
2353 goto finally;
2354
2355 res = 0;
2356
2357finally:
2358 Py_XDECREF(py_string);
2359
2360 return res;
2361}
2362
2363
2364static int
2365load_tuple(Unpicklerobject *self) {
2366 PyObject *tup = 0, *slice = 0, *list = 0;
2367 int i, j, res = -1;
2368
2369 if ((i = marker(self)) < 0)
2370 goto finally;
2371
2372 if ((j = PyList_Size(self->stack)) < 0)
2373 goto finally;
2374
2375 UNLESS(slice = PyList_GetSlice(self->stack, i, j))
2376 goto finally;
2377
2378 UNLESS(tup = PySequence_Tuple(slice))
2379 goto finally;
2380
2381 UNLESS(list = PyList_New(1))
2382 goto finally;
2383
2384 Py_INCREF(tup);
2385 if (PyList_SetItem(list, 0, tup) < 0)
2386 goto finally;
2387
2388 if (PyList_SetSlice(self->stack, i, j, list) < 0)
2389 goto finally;
2390
2391 res = 0;
2392
2393finally:
2394 Py_XDECREF(tup);
2395 Py_XDECREF(list);
2396 Py_XDECREF(slice);
2397
2398 return res;
2399}
2400
2401static int
2402load_empty_tuple(Unpicklerobject *self) {
2403 PyObject *tup = 0;
2404 int res;
2405
2406 UNLESS(tup=PyTuple_New(0)) return -1;
2407 res=PyList_Append(self->stack, tup);
2408 Py_DECREF(tup);
2409 return res;
2410}
2411
2412static int
2413load_empty_list(Unpicklerobject *self) {
2414 PyObject *list = 0;
2415 int res;
2416
2417 UNLESS(list=PyList_New(0)) return -1;
2418 res=PyList_Append(self->stack, list);
2419 Py_DECREF(list);
2420 return res;
2421}
2422
2423static int
2424load_empty_dict(Unpicklerobject *self) {
2425 PyObject *dict = 0;
2426 int res;
2427
2428 UNLESS(dict=PyDict_New()) return -1;
2429 res=PyList_Append(self->stack, dict);
2430 Py_DECREF(dict);
2431 return res;
2432}
2433
2434
2435static int
2436load_list(Unpicklerobject *self) {
2437 PyObject *list = 0, *slice = 0;
2438 int i, j, l, res = -1;
2439
2440 if ((i = marker(self)) < 0)
2441 goto finally;
2442
2443 if ((j = PyList_Size(self->stack)) < 0)
2444 goto finally;
2445
2446 UNLESS(slice = PyList_GetSlice(self->stack, i, j))
2447 goto finally;
2448
2449 if((l=PyList_Size(slice)) < 0)
2450 goto finally;
2451
2452 if(l) {
2453 UNLESS(list = PyList_New(1))
2454 goto finally;
2455
2456 Py_INCREF(slice);
2457 if (PyList_SetItem(list, 0, slice) < 0)
2458 goto finally;
2459
2460 if (PyList_SetSlice(self->stack, i, j, list) < 0)
2461 goto finally;
2462 } else {
2463 if(PyList_Append(self->stack,slice) < 0)
2464 goto finally;
2465 }
2466
2467 res = 0;
2468
2469finally:
2470 Py_XDECREF(list);
2471 Py_XDECREF(slice);
2472
2473 return res;
2474}
2475
2476static int
2477load_dict(Unpicklerobject *self) {
2478 PyObject *list = 0, *dict = 0, *key = 0, *value = 0;
2479 int i, j, k, res = -1;
2480
2481 if ((i = marker(self)) < 0)
2482 goto finally;
2483
2484 if ((j = PyList_Size(self->stack)) < 0)
2485 goto finally;
2486
2487 UNLESS(dict = PyDict_New())
2488 goto finally;
2489
2490 for (k = i; k < j; k += 2) {
2491 UNLESS(key = PyList_GET_ITEM((PyListObject *)self->stack, k))
2492 goto finally;
2493
2494 UNLESS(value = PyList_GET_ITEM((PyListObject *)self->stack, k + 1))
2495 goto finally;
2496
2497 if (PyDict_SetItem(dict, key, value) < 0)
2498 goto finally;
2499 }
2500
2501 if(j) {
2502
2503 UNLESS(list = PyList_New(1))
2504 goto finally;
2505
2506 Py_INCREF(dict);
2507 if (PyList_SetItem(list, 0, dict) < 0)
2508 goto finally;
2509
2510 if (PyList_SetSlice(self->stack, i, j, list) < 0)
2511 goto finally;
2512 }
2513 else
2514 if(PyList_Append(self->stack, dict) < 0)
2515 goto finally;
2516
2517 res = 0;
2518
2519finally:
2520 Py_XDECREF(dict);
2521 Py_XDECREF(list);
2522
2523 return res;
2524}
2525
2526static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002527Instance_New(PyObject *cls, PyObject *args) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002528 int has_key;
2529 PyObject *safe=0, *r=0;
2530
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002531 if (PyClass_Check(cls)) {
2532 int l;
2533
2534 if((l=PyObject_Length(args)) < 0) goto err;
2535 UNLESS(l) {
2536 PyObject *__getinitargs__;
2537
2538 UNLESS(__getinitargs__=PyObject_GetAttr(cls, __getinitargs___str)) {
2539 /* We have a class with no __getinitargs__, so bypass usual
2540 construction */
2541 PyInstanceObject *inst;
2542
2543 PyErr_Clear();
2544 UNLESS(inst=PyObject_NEW(PyInstanceObject, &PyInstance_Type))
2545 goto err;
2546 inst->in_class=(PyClassObject*)cls;
2547 Py_INCREF(cls);
2548 UNLESS(inst->in_dict=PyDict_New()) {
2549 Py_DECREF(inst);
2550 goto err;
2551 }
2552
2553 return (PyObject *)inst;
2554 }
2555 Py_DECREF(__getinitargs__);
2556 }
2557
2558 if((r=PyInstance_New(cls, args, NULL))) return r;
2559 else goto err;
2560 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002561
2562
2563 if ((has_key = cPickle_PyMapping_HasKey(safe_constructors, cls)) < 0)
2564 goto err;
2565
2566 if (!has_key)
2567 if(!(safe = PyObject_GetAttr(cls, __safe_for_unpickling___str)) ||
2568 !PyObject_IsTrue(safe)) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00002569 cPickle_ErrFormat(UnpicklingError, "%s is not safe for unpickling", "O", cls);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002570 Py_XDECREF(safe);
2571 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002572 }
2573
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002574 if(args==Py_None)
2575 {
2576 /* Special case, call cls.__basicnew__() */
2577 PyObject *basicnew;
2578
2579 UNLESS(basicnew=PyObject_GetAttr(cls, __basicnew___str)) return NULL;
2580 r=PyObject_CallObject(basicnew, NULL);
2581 Py_DECREF(basicnew);
2582 if(r) return r;
2583 }
2584
Guido van Rossum142eeb81997-08-13 03:14:41 +00002585 if((r=PyObject_CallObject(cls, args))) return r;
2586
Guido van Rossum60456fd1997-04-09 17:36:32 +00002587err:
2588 {
2589 PyObject *tp, *v, *tb;
2590
2591 PyErr_Fetch(&tp, &v, &tb);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002592 if((r=Py_BuildValue("OOO",v,cls,args))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002593 Py_XDECREF(v);
2594 v=r;
2595 }
2596 PyErr_Restore(tp,v,tb);
2597 }
2598 return NULL;
2599}
2600
2601
2602static int
2603load_obj(Unpicklerobject *self) {
2604 PyObject *class = 0, *slice = 0, *tup = 0, *obj = 0;
2605 int i, len, res = -1;
2606
2607 if ((i = marker(self)) < 0)
2608 goto finally;
2609
Guido van Rossum60456fd1997-04-09 17:36:32 +00002610 if ((len = PyList_Size(self->stack)) < 0)
2611 goto finally;
2612
2613 UNLESS(slice = PyList_GetSlice(self->stack, i + 1, len))
2614 goto finally;
2615
2616 UNLESS(tup = PySequence_Tuple(slice))
2617 goto finally;
2618
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002619 class = PyList_GET_ITEM((PyListObject *)self->stack, i);
2620 Py_INCREF(class);
2621
Guido van Rossum60456fd1997-04-09 17:36:32 +00002622 UNLESS(obj = Instance_New(class, tup))
2623 goto finally;
2624
2625 if (DEL_LIST_SLICE(self->stack, i, len) < 0)
2626 goto finally;
2627
2628 if (PyList_Append(self->stack, obj) < 0)
2629 goto finally;
2630
2631 res = 0;
2632
2633finally:
2634
2635 Py_XDECREF(class);
2636 Py_XDECREF(slice);
2637 Py_XDECREF(tup);
2638 Py_XDECREF(obj);
2639
2640 return res;
2641}
2642
2643
2644static int
2645load_inst(Unpicklerobject *self) {
2646 PyObject *arg_tup = 0, *arg_slice = 0, *class = 0, *obj = 0,
2647 *module_name = 0, *class_name = 0;
2648 int i, j, len, res = -1;
2649 char *s;
2650
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002651 if ((i = marker(self)) < 0) goto finally;
2652
2653 if ((j = PyList_Size(self->stack)) < 0) goto finally;
2654
2655 UNLESS(arg_slice = PyList_GetSlice(self->stack, i, j)) goto finally;
2656
2657 UNLESS(arg_tup = PySequence_Tuple(arg_slice)) goto finally;
2658
2659 if (DEL_LIST_SLICE(self->stack, i, j) < 0) goto finally;
2660
2661 if ((len = (*self->readline_func)(self, &s)) < 0) goto finally;
2662
2663 UNLESS(module_name = PyString_FromStringAndSize(s, len - 1)) goto finally;
2664
2665 if ((len = (*self->readline_func)(self, &s)) < 0) goto finally;
2666
2667 UNLESS(class_name = PyString_FromStringAndSize(s, len - 1)) goto finally;
2668
2669 UNLESS(class = find_class(self->class_map, module_name, class_name))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002670 goto finally;
2671
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002672 UNLESS(obj = Instance_New(class, arg_tup)) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002673
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002674 if (PyList_Append(self->stack, obj) < 0) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002675
2676 res = 0;
2677
2678finally:
2679 Py_XDECREF(class);
2680 Py_XDECREF(arg_slice);
2681 Py_XDECREF(arg_tup);
2682 Py_XDECREF(obj);
2683 Py_XDECREF(module_name);
2684 Py_XDECREF(class_name);
2685
2686 return res;
2687}
2688
2689
2690static int
2691load_global(Unpicklerobject *self) {
2692 PyObject *class = 0, *module_name = 0, *class_name = 0;
2693 int res = -1, len;
2694 char *s;
2695
2696 if ((len = (*self->readline_func)(self, &s)) < 0)
2697 goto finally;
2698
2699 UNLESS(module_name = PyString_FromStringAndSize(s, len - 1))
2700 goto finally;
2701
2702 if ((len = (*self->readline_func)(self, &s)) < 0)
2703 goto finally;
2704
2705 UNLESS(class_name = PyString_FromStringAndSize(s, len - 1))
2706 goto finally;
2707
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002708 UNLESS(class = find_class(self->class_map, module_name, class_name))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002709 goto finally;
2710
2711 if (PyList_Append(self->stack, class) < 0)
2712 goto finally;
2713
2714 res = 0;
2715
2716finally:
2717 Py_XDECREF(class);
2718 Py_XDECREF(module_name);
2719 Py_XDECREF(class_name);
2720
2721 return res;
2722}
2723
2724
2725static int
2726load_persid(Unpicklerobject *self) {
2727 PyObject *pid = 0, *pers_load_val = 0;
2728 int len, res = -1;
2729 char *s;
2730
2731 if (self->pers_func) {
2732 if ((len = (*self->readline_func)(self, &s)) < 0)
2733 goto finally;
2734
2735 UNLESS(pid = PyString_FromStringAndSize(s, len - 1))
2736 goto finally;
2737
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002738 if(PyList_Check(self->pers_func)) {
2739 if(PyList_Append(self->pers_func, pid) < 0) goto finally;
2740 pers_load_val=pid;
2741 Py_INCREF(pid);
2742 }
2743 else {
2744 UNLESS(self->arg)
2745 UNLESS(self->arg = PyTuple_New(1))
2746 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002747
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002748 Py_INCREF(pid);
2749 if (PyTuple_SetItem(self->arg, 0, pid) < 0)
2750 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002751
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002752 UNLESS(pers_load_val =
2753 PyObject_CallObject(self->pers_func, self->arg))
2754 goto finally;
2755 }
2756 if (PyList_Append(self->stack, pers_load_val) < 0)
2757 goto finally;
2758 }
2759 else {
2760 PyErr_SetString(UnpicklingError,
2761 "A load persistent id instruction was encountered,\n"
2762 "but no persistent_load function was specified.");
2763 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002764 }
2765
2766 res = 0;
2767
2768finally:
2769 Py_XDECREF(pid);
2770 Py_XDECREF(pers_load_val);
2771
2772 return res;
2773}
2774
2775
2776static int
2777load_binpersid(Unpicklerobject *self) {
2778 PyObject *pid = 0, *pers_load_val = 0;
2779 int len, res = -1;
2780
2781 if (self->pers_func) {
2782 if ((len = PyList_Size(self->stack)) < 0)
2783 goto finally;
2784
2785 pid = PyList_GET_ITEM((PyListObject *)self->stack, len - 1);
2786 Py_INCREF(pid);
2787
2788 if (DEL_LIST_SLICE(self->stack, len - 1, len) < 0)
2789 goto finally;
2790
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002791 if(PyList_Check(self->pers_func)) {
2792 if(PyList_Append(self->pers_func, pid) < 0) goto finally;
2793 pers_load_val=pid;
2794 Py_INCREF(pid);
2795 }
2796 else {
2797 UNLESS(self->arg)
Guido van Rossum60456fd1997-04-09 17:36:32 +00002798 UNLESS(self->arg = PyTuple_New(1))
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002799 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002800
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002801 Py_INCREF(pid);
2802 if (PyTuple_SetItem(self->arg, 0, pid) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00002803 goto finally;
2804
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002805 UNLESS(pers_load_val =
2806 PyObject_CallObject(self->pers_func, self->arg))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002807 goto finally;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002808 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002809 if (PyList_Append(self->stack, pers_load_val) < 0)
2810 goto finally;
2811 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002812 else {
2813 PyErr_SetString(UnpicklingError,
2814 "A load persistent id instruction was encountered,\n"
2815 "but no persistent_load function was specified.");
2816 goto finally;
2817 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002818
2819 res = 0;
2820
2821finally:
2822 Py_XDECREF(pid);
2823 Py_XDECREF(pers_load_val);
2824
2825 return res;
2826}
2827
2828
2829static int
2830load_pop(Unpicklerobject *self) {
2831 int len;
2832
2833 if ((len = PyList_Size(self->stack)) < 0)
2834 return -1;
2835
2836 if ((self->num_marks > 0) &&
2837 (self->marks[self->num_marks - 1] == len))
2838 self->num_marks--;
2839 else if (DEL_LIST_SLICE(self->stack, len - 1, len) < 0)
2840 return -1;
2841
2842 return 0;
2843}
2844
2845
2846static int
2847load_pop_mark(Unpicklerobject *self) {
2848 int i, len;
2849
2850 if ((i = marker(self)) < 0)
2851 return -1;
2852
2853 if ((len = PyList_Size(self->stack)) < 0)
2854 return -1;
2855
2856 if (DEL_LIST_SLICE(self->stack, i, len) < 0)
2857 return -1;
2858
2859 return 0;
2860}
2861
2862
2863static int
2864load_dup(Unpicklerobject *self) {
2865 PyObject *last;
2866 int len;
2867
2868 if ((len = PyList_Size(self->stack)) < 0)
2869 return -1;
2870
2871 UNLESS(last = PyList_GetItem(self->stack, len - 1))
2872 return -1;
2873
2874 if (PyList_Append(self->stack, last) < 0)
2875 return -1;
2876
2877 return 0;
2878}
2879
2880
2881static int
2882load_get(Unpicklerobject *self) {
2883 PyObject *py_str = 0, *value = 0;
2884 int len, res = -1;
2885 char *s;
2886
2887 if ((len = (*self->readline_func)(self, &s)) < 0)
2888 goto finally;
2889
2890 UNLESS(py_str = PyString_FromStringAndSize(s, len - 1))
2891 goto finally;
2892
2893 UNLESS(value = PyDict_GetItem(self->memo, py_str))
2894 goto finally;
2895
2896 if (PyList_Append(self->stack, value) < 0)
2897 goto finally;
2898
2899 res = 0;
2900
2901finally:
2902 Py_XDECREF(py_str);
2903
2904 return res;
2905}
2906
2907
2908static int
2909load_binget(Unpicklerobject *self) {
2910 PyObject *py_key = 0, *value = 0;
2911 unsigned char key;
2912 int res = -1;
2913 char *s;
2914
2915 if ((*self->read_func)(self, &s, 1) < 0)
2916 goto finally;
2917
2918 key = (unsigned char)s[0];
2919
2920 UNLESS(py_key = PyInt_FromLong((long)key))
2921 goto finally;
2922
2923 UNLESS(value = PyDict_GetItem(self->memo, py_key))
2924 goto finally;
2925
2926 if (PyList_Append(self->stack, value) < 0)
2927 goto finally;
2928
2929 res = 0;
2930
2931finally:
2932 Py_XDECREF(py_key);
2933
2934 return res;
2935}
2936
2937
2938static int
2939load_long_binget(Unpicklerobject *self) {
2940 PyObject *py_key = 0, *value = 0;
2941 unsigned char c, *s;
2942 long key;
2943 int res = -1;
2944
2945 if ((*self->read_func)(self, &s, 4) < 0)
2946 goto finally;
2947
2948 c = (unsigned char)s[0];
2949 key = (long)c;
2950 c = (unsigned char)s[1];
2951 key |= (long)c << 8;
2952 c = (unsigned char)s[2];
2953 key |= (long)c << 16;
2954 c = (unsigned char)s[3];
2955 key |= (long)c << 24;
2956
2957 UNLESS(py_key = PyInt_FromLong(key))
2958 goto finally;
2959
2960 UNLESS(value = PyDict_GetItem(self->memo, py_key))
2961 goto finally;
2962
2963 if (PyList_Append(self->stack, value) < 0)
2964 goto finally;
2965
2966 res = 0;
2967
2968finally:
2969 Py_XDECREF(py_key);
2970
2971 return res;
2972}
2973
2974
2975static int
2976load_put(Unpicklerobject *self) {
2977 PyObject *py_str = 0, *value = 0;
2978 int len, res = -1;
2979 char *s;
2980
2981 if ((len = (*self->readline_func)(self, &s)) < 0)
2982 goto finally;
2983
2984 UNLESS(py_str = PyString_FromStringAndSize(s, len - 1))
2985 goto finally;
2986
2987 if ((len = PyList_Size(self->stack)) < 0)
2988 goto finally;
2989
2990 UNLESS(value = PyList_GetItem(self->stack, len - 1))
2991 goto finally;
2992
2993 if (PyDict_SetItem(self->memo, py_str, value) < 0)
2994 goto finally;
2995
2996 res = 0;
2997
2998finally:
2999 Py_XDECREF(py_str);
3000
3001 return res;
3002}
3003
3004
3005static int
3006load_binput(Unpicklerobject *self) {
3007 PyObject *py_key = 0, *value = 0;
3008 unsigned char key, *s;
3009 int len, res = -1;
3010
3011 if ((*self->read_func)(self, &s, 1) < 0)
3012 goto finally;
3013
3014 key = (unsigned char)s[0];
3015
3016 UNLESS(py_key = PyInt_FromLong((long)key))
3017 goto finally;
3018
3019 if ((len = PyList_Size(self->stack)) < 0)
3020 goto finally;
3021
3022 UNLESS(value = PyList_GetItem(self->stack, len - 1))
3023 goto finally;
3024
3025 if (PyDict_SetItem(self->memo, py_key, value) < 0)
3026 goto finally;
3027
3028 res = 0;
3029
3030finally:
3031 Py_XDECREF(py_key);
3032
3033 return res;
3034}
3035
3036
3037static int
3038load_long_binput(Unpicklerobject *self) {
3039 PyObject *py_key = 0, *value = 0;
3040 long key;
3041 unsigned char c, *s;
3042 int len, res = -1;
3043
3044 if ((*self->read_func)(self, &s, 4) < 0)
3045 goto finally;
3046
3047 c = (unsigned char)s[0];
3048 key = (long)c;
3049 c = (unsigned char)s[1];
3050 key |= (long)c << 8;
3051 c = (unsigned char)s[2];
3052 key |= (long)c << 16;
3053 c = (unsigned char)s[3];
3054 key |= (long)c << 24;
3055
3056 UNLESS(py_key = PyInt_FromLong(key))
3057 goto finally;
3058
3059 if ((len = PyList_Size(self->stack)) < 0)
3060 goto finally;
3061
3062 UNLESS(value = PyList_GetItem(self->stack, len - 1))
3063 goto finally;
3064
3065 if (PyDict_SetItem(self->memo, py_key, value) < 0)
3066 goto finally;
3067
3068 res = 0;
3069
3070finally:
3071 Py_XDECREF(py_key);
3072
3073 return res;
3074}
3075
3076
3077static int
3078do_append(Unpicklerobject *self, int x) {
3079 PyObject *value = 0, *list = 0, *append_method = 0;
3080 int len, i;
3081
3082 if ((len = PyList_Size(self->stack)) < 0)
3083 return -1;
3084
3085 UNLESS(list = PyList_GetItem(self->stack, x - 1))
3086 goto err;
3087
3088 if (PyList_Check(list)) {
3089 PyObject *slice = 0;
3090 int list_len;
3091
3092 UNLESS(slice = PyList_GetSlice(self->stack, x, len))
3093 return -1;
3094
3095 list_len = PyList_Size(list);
3096 if (PyList_SetSlice(list, list_len, list_len, slice) < 0) {
3097 Py_DECREF(slice);
3098 return -1;
3099 }
3100
3101 Py_DECREF(slice);
3102 }
3103 else {
3104
3105 UNLESS(append_method = PyObject_GetAttr(list, append_str))
3106 return -1;
3107
3108 for (i = x; i < len; i++) {
3109 PyObject *junk;
3110
3111 UNLESS(value = PyList_GetItem(self->stack, i))
3112 return -1;
3113
3114 UNLESS(self->arg)
3115 UNLESS(self->arg = PyTuple_New(1))
3116 goto err;
3117
3118 Py_INCREF(value);
3119 if (PyTuple_SetItem(self->arg, 0, value) < 0)
3120 goto err;
3121
3122 UNLESS(junk = PyObject_CallObject(append_method, self->arg))
3123 goto err;
3124 Py_DECREF(junk);
3125 }
3126 }
3127
3128 if (DEL_LIST_SLICE(self->stack, x, len) < 0)
3129 goto err;
3130
3131 Py_XDECREF(append_method);
3132
3133 return 0;
3134
3135err:
3136 Py_XDECREF(append_method);
3137
3138 return -1;
3139}
3140
3141
3142static int
3143load_append(Unpicklerobject *self) {
3144 return do_append(self, PyList_Size(self->stack) - 1);
3145}
3146
3147
3148static int
3149load_appends(Unpicklerobject *self) {
3150 return do_append(self, marker(self));
3151}
3152
3153
3154static int
3155do_setitems(Unpicklerobject *self, int x) {
3156 PyObject *value = 0, *key = 0, *dict = 0;
3157 int len, i, res = -1;
3158
3159 if ((len = PyList_Size(self->stack)) < 0)
3160 goto finally;
3161
3162 UNLESS(dict = PyList_GetItem(self->stack, x - 1))
3163 goto finally;
3164
3165 for (i = x; i < len; i += 2) {
3166 UNLESS(key = PyList_GetItem(self->stack, i))
3167 goto finally;
3168
3169 UNLESS(value = PyList_GetItem(self->stack, i + 1))
3170 goto finally;
3171
3172 if (PyObject_SetItem(dict, key, value) < 0)
3173 goto finally;
3174 }
3175
3176 if (DEL_LIST_SLICE(self->stack, x, len) < 0)
3177 goto finally;
3178
3179 res = 0;
3180
3181finally:
3182
3183 return res;
3184}
3185
3186
3187static int
3188load_setitem(Unpicklerobject *self) {
3189 return do_setitems(self, PyList_Size(self->stack) - 2);
3190}
3191
3192
3193static int
3194load_setitems(Unpicklerobject *self) {
3195 return do_setitems(self, marker(self));
3196}
3197
3198
3199static int
3200load_build(Unpicklerobject *self) {
3201 PyObject *value = 0, *inst = 0, *instdict = 0, *d_key = 0, *d_value = 0,
3202 *junk = 0, *__setstate__ = 0;
3203 int len, i, res = -1;
3204
3205 if ((len = PyList_Size(self->stack)) < 0)
3206 goto finally;
3207
3208 UNLESS(value = PyList_GetItem(self->stack, len - 1))
3209 goto finally;
3210 Py_INCREF(value);
3211
3212 if (DEL_LIST_SLICE(self->stack, len - 1, len) < 0)
3213 goto finally;
3214
3215 UNLESS(inst = PyList_GetItem(self->stack, len - 2))
3216 goto finally;
3217
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003218 UNLESS(__setstate__ = PyObject_GetAttr(inst, __setstate___str)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003219 PyErr_Clear();
3220
3221 UNLESS(instdict = PyObject_GetAttr(inst, __dict___str))
3222 goto finally;
3223
3224 i = 0;
3225 while (PyDict_Next(value, &i, &d_key, &d_value)) {
3226 if (PyObject_SetItem(instdict, d_key, d_value) < 0)
3227 goto finally;
3228 }
3229 }
3230 else {
3231 UNLESS(self->arg)
3232 UNLESS(self->arg = PyTuple_New(1))
3233 goto finally;
3234
3235 Py_INCREF(value);
3236 if (PyTuple_SetItem(self->arg, 0, value) < 0)
3237 goto finally;
3238
3239 UNLESS(junk = PyObject_CallObject(__setstate__, self->arg))
3240 goto finally;
3241 Py_DECREF(junk);
3242 }
3243
3244 res = 0;
3245
3246finally:
3247 Py_XDECREF(value);
3248 Py_XDECREF(instdict);
3249 Py_XDECREF(__setstate__);
3250
3251 return res;
3252}
3253
3254
3255static int
3256load_mark(Unpicklerobject *self) {
3257 int len;
3258
3259 if ((len = PyList_Size(self->stack)) < 0)
3260 return -1;
3261
3262 if (!self->marks_size) {
3263 self->marks_size = 20;
3264 UNLESS(self->marks = (int *)malloc(self->marks_size * sizeof(int))) {
3265 PyErr_NoMemory();
3266 return -1;
3267 }
3268 }
3269 else if ((self->num_marks + 1) >= self->marks_size) {
3270 UNLESS(self->marks = (int *)realloc(self->marks,
3271 (self->marks_size + 20) * sizeof(int))) {
3272 PyErr_NoMemory();
3273 return -1;
3274 }
3275
3276 self->marks_size += 20;
3277 }
3278
3279 self->marks[self->num_marks++] = len;
3280
3281 return 0;
3282}
3283
3284static int
3285load_reduce(Unpicklerobject *self) {
3286 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
3287 int len, res = -1;
3288
3289 if ((len = PyList_Size(self->stack)) < 0)
3290 goto finally;
3291
3292 UNLESS(arg_tup = PyList_GetItem(self->stack, len - 1))
3293 goto finally;
3294
3295 UNLESS(callable = PyList_GetItem(self->stack, len - 2))
3296 goto finally;
3297
3298 UNLESS(ob = Instance_New(callable, arg_tup))
3299 goto finally;
3300
3301 if (PyList_Append(self->stack, ob) < 0)
3302 goto finally;
3303
3304 if (DEL_LIST_SLICE(self->stack, len - 2, len) < 0)
3305 goto finally;
3306
3307 res = 0;
3308
3309finally:
3310 Py_XDECREF(ob);
3311
3312 return res;
3313}
3314
3315static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003316load(Unpicklerobject *self) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00003317 PyObject *stack = 0, *err = 0, *val = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003318 int len;
3319 char *s;
3320
3321 UNLESS(stack = PyList_New(0))
3322 goto err;
3323
3324 self->stack = stack;
3325 self->num_marks = 0;
3326
3327 while (1) {
3328 if ((*self->read_func)(self, &s, 1) < 0)
3329 break;
3330
3331 switch (s[0]) {
3332 case NONE:
3333 if (load_none(self) < 0)
3334 break;
3335 continue;
3336
3337 case BININT:
3338 if (load_binint(self) < 0)
3339 break;
3340 continue;
3341
3342 case BININT1:
3343 if (load_binint1(self) < 0)
3344 break;
3345 continue;
3346
3347 case BININT2:
3348 if (load_binint2(self) < 0)
3349 break;
3350 continue;
3351
3352 case INT:
3353 if (load_int(self) < 0)
3354 break;
3355 continue;
3356
3357 case LONG:
3358 if (load_long(self) < 0)
3359 break;
3360 continue;
3361
3362 case FLOAT:
3363 if (load_float(self) < 0)
3364 break;
3365 continue;
3366
3367#ifdef FORMAT_1_3
3368 case BINFLOAT:
3369 if (load_binfloat(self) < 0)
3370 break;
3371 continue;
3372#endif
3373
3374 case BINSTRING:
3375 if (load_binstring(self) < 0)
3376 break;
3377 continue;
3378
3379 case SHORT_BINSTRING:
3380 if (load_short_binstring(self) < 0)
3381 break;
3382 continue;
3383
3384 case STRING:
3385 if (load_string(self) < 0)
3386 break;
3387 continue;
3388
3389 case EMPTY_TUPLE:
3390 if (load_empty_tuple(self) < 0)
3391 break;
3392 continue;
3393
3394 case TUPLE:
3395 if (load_tuple(self) < 0)
3396 break;
3397 continue;
3398
3399 case EMPTY_LIST:
3400 if (load_empty_list(self) < 0)
3401 break;
3402 continue;
3403
3404 case LIST:
3405 if (load_list(self) < 0)
3406 break;
3407 continue;
3408
3409 case EMPTY_DICT:
3410 if (load_empty_dict(self) < 0)
3411 break;
3412 continue;
3413
3414 case DICT:
3415 if (load_dict(self) < 0)
3416 break;
3417 continue;
3418
3419 case OBJ:
3420 if (load_obj(self) < 0)
3421 break;
3422 continue;
3423
3424 case INST:
3425 if (load_inst(self) < 0)
3426 break;
3427 continue;
3428
3429 case GLOBAL:
3430 if (load_global(self) < 0)
3431 break;
3432 continue;
3433
3434 case APPEND:
3435 if (load_append(self) < 0)
3436 break;
3437 continue;
3438
3439 case APPENDS:
3440 if (load_appends(self) < 0)
3441 break;
3442 continue;
3443
3444 case BUILD:
3445 if (load_build(self) < 0)
3446 break;
3447 continue;
3448
3449 case DUP:
3450 if (load_dup(self) < 0)
3451 break;
3452 continue;
3453
3454 case BINGET:
3455 if (load_binget(self) < 0)
3456 break;
3457 continue;
3458
3459 case LONG_BINGET:
3460 if (load_long_binget(self) < 0)
3461 break;
3462 continue;
3463
3464 case GET:
3465 if (load_get(self) < 0)
3466 break;
3467 continue;
3468
3469 case MARK:
3470 if (load_mark(self) < 0)
3471 break;
3472 continue;
3473
3474 case BINPUT:
3475 if (load_binput(self) < 0)
3476 break;
3477 continue;
3478
3479 case LONG_BINPUT:
3480 if (load_long_binput(self) < 0)
3481 break;
3482 continue;
3483
3484 case PUT:
3485 if (load_put(self) < 0)
3486 break;
3487 continue;
3488
3489 case POP:
3490 if (load_pop(self) < 0)
3491 break;
3492 continue;
3493
3494 case POP_MARK:
3495 if (load_pop_mark(self) < 0)
3496 break;
3497 continue;
3498
3499 case SETITEM:
3500 if (load_setitem(self) < 0)
3501 break;
3502 continue;
3503
3504 case SETITEMS:
3505 if (load_setitems(self) < 0)
3506 break;
3507 continue;
3508
3509 case STOP:
3510 break;
3511
3512 case PERSID:
3513 if (load_persid(self) < 0)
3514 break;
3515 continue;
3516
3517 case BINPERSID:
3518 if (load_binpersid(self) < 0)
3519 break;
3520 continue;
3521
3522 case REDUCE:
3523 if (load_reduce(self) < 0)
3524 break;
3525 continue;
3526
3527 default:
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00003528 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossum60456fd1997-04-09 17:36:32 +00003529 "c", s[0]);
3530 goto err;
3531 }
3532
3533 break;
3534 }
3535
Fred Drake764b9841998-05-28 04:33:37 +00003536 err = PyErr_Occurred();
3537 if (err && PyErr_ExceptionMatches(PyExc_EOFError)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003538 PyErr_SetNone(PyExc_EOFError);
3539 goto err;
3540 }
3541
3542 if (err) goto err;
3543
3544 if ((len = PyList_Size(stack)) < 0) goto err;
3545
3546 UNLESS(val = PyList_GetItem(stack, len - 1)) goto err;
3547 Py_INCREF(val);
3548
3549 Py_DECREF(stack);
3550
3551 self->stack=NULL;
3552 return val;
3553
3554err:
3555 self->stack=NULL;
3556 Py_XDECREF(stack);
3557
3558 return NULL;
3559}
3560
3561
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003562/* No-load functions to support noload, which is used to
3563 find persistent references. */
3564
3565static int
3566noload_obj(Unpicklerobject *self) {
3567 int i, len;
3568
3569 if ((i = marker(self)) < 0) return -1;
3570 if ((len = PyList_Size(self->stack)) < 0) return -1;
3571 return DEL_LIST_SLICE(self->stack, i+1, len);
3572}
3573
3574
3575static int
3576noload_inst(Unpicklerobject *self) {
3577 int i, j;
3578 char *s;
3579
3580 if ((i = marker(self)) < 0) return -1;
3581 if ((j = PyList_Size(self->stack)) < 0) return -1;
3582 if (DEL_LIST_SLICE(self->stack, i, j) < 0) return -1;
3583 if ((*self->readline_func)(self, &s) < 0) return -1;
3584 if ((*self->readline_func)(self, &s) < 0) return -1;
3585 return PyList_Append(self->stack, Py_None);
3586}
3587
3588static int
3589noload_global(Unpicklerobject *self) {
3590 char *s;
3591
3592 if ((*self->readline_func)(self, &s) < 0) return -1;
3593 if ((*self->readline_func)(self, &s) < 0) return -1;
3594 return PyList_Append(self->stack, Py_None);
3595}
3596
3597static int
3598noload_reduce(Unpicklerobject *self) {
3599 int len;
3600
3601 if ((len = PyList_Size(self->stack)) < 0) return -1;
3602 if (DEL_LIST_SLICE(self->stack, len - 2, len) < 0) return -1;
3603 return PyList_Append(self->stack, Py_None);
3604}
3605
3606static int
3607noload_build(Unpicklerobject *self) {
3608 int len;
3609
3610 if ((len = PyList_Size(self->stack)) < 0) return -1;
3611 return DEL_LIST_SLICE(self->stack, len - 1, len);
3612}
3613
3614
3615static PyObject *
3616noload(Unpicklerobject *self) {
3617 PyObject *stack = 0, *err = 0, *val = 0;
3618 int len;
3619 char *s;
3620
3621 UNLESS(stack = PyList_New(0))
3622 goto err;
3623
3624 self->stack = stack;
3625 self->num_marks = 0;
3626
3627 while (1) {
3628 if ((*self->read_func)(self, &s, 1) < 0)
3629 break;
3630
3631 switch (s[0]) {
3632 case NONE:
3633 if (load_none(self) < 0)
3634 break;
3635 continue;
3636
3637 case BININT:
3638 if (load_binint(self) < 0)
3639 break;
3640 continue;
3641
3642 case BININT1:
3643 if (load_binint1(self) < 0)
3644 break;
3645 continue;
3646
3647 case BININT2:
3648 if (load_binint2(self) < 0)
3649 break;
3650 continue;
3651
3652 case INT:
3653 if (load_int(self) < 0)
3654 break;
3655 continue;
3656
3657 case LONG:
3658 if (load_long(self) < 0)
3659 break;
3660 continue;
3661
3662 case FLOAT:
3663 if (load_float(self) < 0)
3664 break;
3665 continue;
3666
3667 case BINFLOAT:
3668 if (load_binfloat(self) < 0)
3669 break;
3670 continue;
3671
3672 case BINSTRING:
3673 if (load_binstring(self) < 0)
3674 break;
3675 continue;
3676
3677 case SHORT_BINSTRING:
3678 if (load_short_binstring(self) < 0)
3679 break;
3680 continue;
3681
3682 case STRING:
3683 if (load_string(self) < 0)
3684 break;
3685 continue;
3686
3687 case EMPTY_TUPLE:
3688 if (load_empty_tuple(self) < 0)
3689 break;
3690 continue;
3691
3692 case TUPLE:
3693 if (load_tuple(self) < 0)
3694 break;
3695 continue;
3696
3697 case EMPTY_LIST:
3698 if (load_empty_list(self) < 0)
3699 break;
3700 continue;
3701
3702 case LIST:
3703 if (load_list(self) < 0)
3704 break;
3705 continue;
3706
3707 case EMPTY_DICT:
3708 if (load_empty_dict(self) < 0)
3709 break;
3710 continue;
3711
3712 case DICT:
3713 if (load_dict(self) < 0)
3714 break;
3715 continue;
3716
3717 case OBJ:
3718 if (noload_obj(self) < 0)
3719 break;
3720 continue;
3721
3722 case INST:
3723 if (noload_inst(self) < 0)
3724 break;
3725 continue;
3726
3727 case GLOBAL:
3728 if (noload_global(self) < 0)
3729 break;
3730 continue;
3731
3732 case APPEND:
3733 if (load_append(self) < 0)
3734 break;
3735 continue;
3736
3737 case APPENDS:
3738 if (load_appends(self) < 0)
3739 break;
3740 continue;
3741
3742 case BUILD:
3743 if (noload_build(self) < 0)
3744 break;
3745 continue;
3746
3747 case DUP:
3748 if (load_dup(self) < 0)
3749 break;
3750 continue;
3751
3752 case BINGET:
3753 if (load_binget(self) < 0)
3754 break;
3755 continue;
3756
3757 case LONG_BINGET:
3758 if (load_long_binget(self) < 0)
3759 break;
3760 continue;
3761
3762 case GET:
3763 if (load_get(self) < 0)
3764 break;
3765 continue;
3766
3767 case MARK:
3768 if (load_mark(self) < 0)
3769 break;
3770 continue;
3771
3772 case BINPUT:
3773 if (load_binput(self) < 0)
3774 break;
3775 continue;
3776
3777 case LONG_BINPUT:
3778 if (load_long_binput(self) < 0)
3779 break;
3780 continue;
3781
3782 case PUT:
3783 if (load_put(self) < 0)
3784 break;
3785 continue;
3786
3787 case POP:
3788 if (load_pop(self) < 0)
3789 break;
3790 continue;
3791
3792 case POP_MARK:
3793 if (load_pop_mark(self) < 0)
3794 break;
3795 continue;
3796
3797 case SETITEM:
3798 if (load_setitem(self) < 0)
3799 break;
3800 continue;
3801
3802 case SETITEMS:
3803 if (load_setitems(self) < 0)
3804 break;
3805 continue;
3806
3807 case STOP:
3808 break;
3809
3810 case PERSID:
3811 if (load_persid(self) < 0)
3812 break;
3813 continue;
3814
3815 case BINPERSID:
3816 if (load_binpersid(self) < 0)
3817 break;
3818 continue;
3819
3820 case REDUCE:
3821 if (noload_reduce(self) < 0)
3822 break;
3823 continue;
3824
3825 default:
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00003826 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003827 "c", s[0]);
3828 goto err;
3829 }
3830
3831 break;
3832 }
3833
Fred Drake764b9841998-05-28 04:33:37 +00003834 err = PyErr_Occurred();
3835 if (err && PyErr_ExceptionMatches(PyExc_EOFError)) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003836 PyErr_SetNone(PyExc_EOFError);
3837 goto err;
3838 }
3839
3840 if (err) goto err;
3841
3842 if ((len = PyList_Size(stack)) < 0) goto err;
3843
3844 UNLESS(val = PyList_GetItem(stack, len - 1)) goto err;
3845 Py_INCREF(val);
3846
3847 Py_DECREF(stack);
3848
3849 self->stack=NULL;
3850 return val;
3851
3852err:
3853 self->stack=NULL;
3854 Py_XDECREF(stack);
3855
3856 return NULL;
3857}
3858
3859
Guido van Rossum60456fd1997-04-09 17:36:32 +00003860static PyObject *
3861Unpickler_load(Unpicklerobject *self, PyObject *args) {
3862 UNLESS(PyArg_ParseTuple(args, ""))
3863 return NULL;
3864
3865 return load(self);
3866}
3867
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003868static PyObject *
3869Unpickler_noload(Unpicklerobject *self, PyObject *args) {
3870 UNLESS(PyArg_ParseTuple(args, ""))
3871 return NULL;
3872
3873 return noload(self);
3874}
3875
Guido van Rossum60456fd1997-04-09 17:36:32 +00003876
3877static struct PyMethodDef Unpickler_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003878 {"load", (PyCFunction)Unpickler_load, 1,
3879 "load() -- Load a pickle"
3880 },
3881 {"noload", (PyCFunction)Unpickler_noload, 1,
3882 "noload() -- not load a pickle, but go through most of the motions\n"
3883 "\n"
3884 "This function can be used to read past a pickle without instantiating\n"
3885 "any objects or importing any modules. It can also be used to find all\n"
3886 "persistent references without instantiating any objects or importing\n"
3887 "any modules.\n"
3888 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00003889 {NULL, NULL} /* sentinel */
3890};
3891
3892
3893static Unpicklerobject *
3894newUnpicklerobject(PyObject *f) {
3895 Unpicklerobject *self;
3896
3897 UNLESS(self = PyObject_NEW(Unpicklerobject, &Unpicklertype))
3898 return NULL;
3899
3900 self->file = NULL;
3901 self->arg = NULL;
3902 self->stack = NULL;
3903 self->pers_func = NULL;
3904 self->last_string = NULL;
3905 self->marks = NULL;
3906 self->num_marks = 0;
3907 self->marks_size = 0;
3908 self->buf_size = 0;
3909 self->read = NULL;
Guido van Rossum8a6dba31998-03-06 01:39:39 +00003910 self->readline = NULL;
3911 self->class_map = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003912
3913 UNLESS(self->memo = PyDict_New()) {
3914 Py_XDECREF((PyObject *)self);
3915 return NULL;
3916 }
3917
3918 Py_INCREF(f);
3919 self->file = f;
3920
3921 /* Set read, readline based on type of f */
3922 if (PyFile_Check(f)) {
3923 self->fp = PyFile_AsFile(f);
3924 self->read_func = read_file;
3925 self->readline_func = readline_file;
3926 }
3927 else if (PycStringIO_InputCheck(f)) {
3928 self->fp = NULL;
3929 self->read_func = read_cStringIO;
3930 self->readline_func = readline_cStringIO;
3931 }
3932 else {
3933
3934 self->fp = NULL;
3935 self->read_func = read_other;
3936 self->readline_func = readline_other;
3937
3938 UNLESS((self->readline = PyObject_GetAttr(f, readline_str)) &&
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003939 (self->read = PyObject_GetAttr(f, read_str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003940 PyErr_Clear();
3941 PyErr_SetString( PyExc_TypeError, "argument must have 'read' and "
3942 "'readline' attributes" );
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003943 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003944 }
3945 }
3946
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003947 if(PyEval_GetRestricted()) {
3948 /* Restricted execution, get private tables */
3949 PyObject *m;
3950
3951 UNLESS(self->class_map=PyDict_New()) goto err;
3952 UNLESS(m=PyImport_Import(copy_reg_str)) goto err;
3953 self->safe_constructors=PyObject_GetAttr(m, safe_constructors_str);
3954 Py_DECREF(m);
3955 UNLESS(self->safe_constructors) goto err;
3956 }
3957 else {
3958 self->class_map=class_map;
3959 Py_INCREF(class_map);
3960 self->safe_constructors=safe_constructors;
3961 Py_INCREF(safe_constructors);
3962 }
3963
Guido van Rossum60456fd1997-04-09 17:36:32 +00003964 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003965
3966err:
3967 Py_DECREF((PyObject *)self);
3968 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003969}
3970
3971
3972static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00003973get_Unpickler(PyObject *self, PyObject *args) {
3974 PyObject *file;
3975
3976 UNLESS(PyArg_ParseTuple(args, "O", &file))
3977 return NULL;
3978 return (PyObject *)newUnpicklerobject(file);
3979}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003980
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003981
Guido van Rossum60456fd1997-04-09 17:36:32 +00003982static void
3983Unpickler_dealloc(Unpicklerobject *self) {
3984 Py_XDECREF(self->readline);
3985 Py_XDECREF(self->read);
3986 Py_XDECREF(self->file);
3987 Py_XDECREF(self->memo);
3988 Py_XDECREF(self->stack);
3989 Py_XDECREF(self->pers_func);
3990 Py_XDECREF(self->arg);
3991 Py_XDECREF(self->last_string);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003992 Py_XDECREF(self->class_map);
3993 Py_XDECREF(self->safe_constructors);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003994
Guido van Rossum60456fd1997-04-09 17:36:32 +00003995 if (self->marks) {
3996 free(self->marks);
3997 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003998
Guido van Rossum60456fd1997-04-09 17:36:32 +00003999 if (self->buf_size) {
4000 free(self->buf);
4001 }
4002
4003 PyMem_DEL(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004004}
4005
4006
4007static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004008Unpickler_getattr(Unpicklerobject *self, char *name) {
4009 if (!strcmp(name, "persistent_load")) {
4010 if (!self->pers_func) {
4011 PyErr_SetString(PyExc_AttributeError, name);
4012 return NULL;
4013 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004014
Guido van Rossum60456fd1997-04-09 17:36:32 +00004015 Py_INCREF(self->pers_func);
4016 return self->pers_func;
4017 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004018
Guido van Rossum60456fd1997-04-09 17:36:32 +00004019 if (!strcmp(name, "memo")) {
4020 if (!self->memo) {
4021 PyErr_SetString(PyExc_AttributeError, name);
4022 return NULL;
4023 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004024
Guido van Rossum60456fd1997-04-09 17:36:32 +00004025 Py_INCREF(self->memo);
4026 return self->memo;
4027 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004028
Guido van Rossum60456fd1997-04-09 17:36:32 +00004029 if (!strcmp(name, "stack")) {
4030 if (!self->stack) {
4031 PyErr_SetString(PyExc_AttributeError, name);
4032 return NULL;
4033 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004034
Guido van Rossum60456fd1997-04-09 17:36:32 +00004035 Py_INCREF(self->stack);
4036 return self->stack;
4037 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004038
Guido van Rossum60456fd1997-04-09 17:36:32 +00004039 if (!strcmp(name, "UnpicklingError")) {
4040 Py_INCREF(UnpicklingError);
4041 return UnpicklingError;
4042 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004043
Guido van Rossum60456fd1997-04-09 17:36:32 +00004044 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
4045}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004046
Guido van Rossum60456fd1997-04-09 17:36:32 +00004047
4048static int
4049Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) {
4050 if (!strcmp(name, "persistent_load")) {
4051 Py_XDECREF(self->pers_func);
4052 self->pers_func = value;
4053 Py_INCREF(value);
4054 return 0;
4055 }
4056
4057 PyErr_SetString(PyExc_AttributeError, name);
4058 return -1;
4059}
4060
4061
4062static PyObject *
4063cpm_dump(PyObject *self, PyObject *args) {
4064 PyObject *ob, *file, *res = NULL;
4065 Picklerobject *pickler = 0;
4066 int bin = 0;
4067
4068 UNLESS(PyArg_ParseTuple(args, "OO|i", &ob, &file, &bin))
4069 goto finally;
4070
4071 UNLESS(pickler = newPicklerobject(file, bin))
4072 goto finally;
4073
4074 if (dump(pickler, ob) < 0)
4075 goto finally;
4076
4077 Py_INCREF(Py_None);
4078 res = Py_None;
4079
4080finally:
4081 Py_XDECREF(pickler);
4082
4083 return res;
4084}
4085
4086
4087static PyObject *
4088cpm_dumps(PyObject *self, PyObject *args) {
4089 PyObject *ob, *file = 0, *res = NULL;
4090 Picklerobject *pickler = 0;
4091 int bin = 0;
4092
4093 UNLESS(PyArg_ParseTuple(args, "O|i", &ob, &bin))
4094 goto finally;
4095
4096 UNLESS(file = PycStringIO->NewOutput(128))
4097 goto finally;
4098
4099 UNLESS(pickler = newPicklerobject(file, bin))
4100 goto finally;
4101
4102 if (dump(pickler, ob) < 0)
4103 goto finally;
4104
4105 res = PycStringIO->cgetvalue(file);
4106
4107finally:
4108 Py_XDECREF(pickler);
4109 Py_XDECREF(file);
4110
4111 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004112}
4113
4114
4115static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004116cpm_load(PyObject *self, PyObject *args) {
4117 Unpicklerobject *unpickler = 0;
4118 PyObject *ob, *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004119
Guido van Rossum60456fd1997-04-09 17:36:32 +00004120 UNLESS(PyArg_ParseTuple(args, "O", &ob))
4121 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004122
Guido van Rossum60456fd1997-04-09 17:36:32 +00004123 UNLESS(unpickler = newUnpicklerobject(ob))
4124 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004125
Guido van Rossum60456fd1997-04-09 17:36:32 +00004126 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004127
Guido van Rossum60456fd1997-04-09 17:36:32 +00004128finally:
4129 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004130
Guido van Rossum60456fd1997-04-09 17:36:32 +00004131 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004132}
4133
4134
4135static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004136cpm_loads(PyObject *self, PyObject *args) {
4137 PyObject *ob, *file = 0, *res = NULL;
4138 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004139
Guido van Rossum60456fd1997-04-09 17:36:32 +00004140 UNLESS(PyArg_ParseTuple(args, "O", &ob))
4141 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004142
Guido van Rossum60456fd1997-04-09 17:36:32 +00004143 UNLESS(file = PycStringIO->NewInput(ob))
4144 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004145
Guido van Rossum60456fd1997-04-09 17:36:32 +00004146 UNLESS(unpickler = newUnpicklerobject(file))
4147 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004148
Guido van Rossum60456fd1997-04-09 17:36:32 +00004149 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004150
Guido van Rossum60456fd1997-04-09 17:36:32 +00004151finally:
4152 Py_XDECREF(file);
4153 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004154
Guido van Rossum60456fd1997-04-09 17:36:32 +00004155 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004156}
4157
4158
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004159static char Unpicklertype__doc__[] =
4160"Objects that know how to unpickle";
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004161
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004162static PyTypeObject Unpicklertype = {
4163 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004164 0, /*ob_size*/
4165 "Unpickler", /*tp_name*/
4166 sizeof(Unpicklerobject), /*tp_basicsize*/
4167 0, /*tp_itemsize*/
4168 /* methods */
4169 (destructor)Unpickler_dealloc, /*tp_dealloc*/
4170 (printfunc)0, /*tp_print*/
4171 (getattrfunc)Unpickler_getattr, /*tp_getattr*/
4172 (setattrfunc)Unpickler_setattr, /*tp_setattr*/
4173 (cmpfunc)0, /*tp_compare*/
4174 (reprfunc)0, /*tp_repr*/
4175 0, /*tp_as_number*/
4176 0, /*tp_as_sequence*/
4177 0, /*tp_as_mapping*/
4178 (hashfunc)0, /*tp_hash*/
4179 (ternaryfunc)0, /*tp_call*/
4180 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004181
Guido van Rossum60456fd1997-04-09 17:36:32 +00004182 /* Space for future expansion */
4183 0L,0L,0L,0L,
4184 Unpicklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004185};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004186
Guido van Rossum60456fd1997-04-09 17:36:32 +00004187static struct PyMethodDef cPickle_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004188 {"dump", (PyCFunction)cpm_dump, 1,
4189 "dump(object, file, [binary]) --"
4190 "Write an object in pickle format to the given file\n"
4191 "\n"
4192 "If the optional argument, binary, is provided and is true, then the\n"
4193 "pickle will be written in binary format, which is more space and\n"
4194 "computationally efficient. \n"
4195 },
4196 {"dumps", (PyCFunction)cpm_dumps, 1,
4197 "dumps(object, [binary]) --"
4198 "Return a string containing an object in pickle format\n"
4199 "\n"
4200 "If the optional argument, binary, is provided and is true, then the\n"
4201 "pickle will be written in binary format, which is more space and\n"
4202 "computationally efficient. \n"
4203 },
4204 {"load", (PyCFunction)cpm_load, 1,
4205 "load(file) -- Load a pickle from the given file"},
4206 {"loads", (PyCFunction)cpm_loads, 1,
4207 "loads(string) -- Load a pickle from the given string"},
4208 {"Pickler", (PyCFunction)get_Pickler, 1,
4209 "Pickler(file, [binary]) -- Create a pickler\n"
4210 "\n"
4211 "If the optional argument, binary, is provided and is true, then\n"
4212 "pickles will be written in binary format, which is more space and\n"
4213 "computationally efficient. \n"
4214 },
4215 {"Unpickler", (PyCFunction)get_Unpickler, 1,
4216 "Unpickler(file) -- Create an unpickler"},
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004217 { NULL, NULL }
4218};
4219
4220
Guido van Rossum60456fd1997-04-09 17:36:32 +00004221#define CHECK_FOR_ERRORS(MESS) \
4222if(PyErr_Occurred()) { \
4223 PyObject *__sys_exc_type, *__sys_exc_value, *__sys_exc_traceback; \
4224 PyErr_Fetch( &__sys_exc_type, &__sys_exc_value, &__sys_exc_traceback); \
4225 fprintf(stderr, # MESS ":\n\t"); \
4226 PyObject_Print(__sys_exc_type, stderr,0); \
4227 fprintf(stderr,", "); \
4228 PyObject_Print(__sys_exc_value, stderr,0); \
4229 fprintf(stderr,"\n"); \
4230 fflush(stderr); \
4231 Py_FatalError(# MESS); \
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004232}
4233
4234
Guido van Rossum60456fd1997-04-09 17:36:32 +00004235static int
4236init_stuff(PyObject *module, PyObject *module_dict) {
4237 PyObject *string, *copy_reg;
4238
4239#define INIT_STR(S) UNLESS(S ## _str=PyString_FromString(#S)) return -1;
4240
4241 INIT_STR(__class__);
4242 INIT_STR(__getinitargs__);
4243 INIT_STR(__dict__);
4244 INIT_STR(__getstate__);
4245 INIT_STR(__setstate__);
4246 INIT_STR(__name__);
Guido van Rossum142eeb81997-08-13 03:14:41 +00004247 INIT_STR(__main__);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004248 INIT_STR(__reduce__);
4249 INIT_STR(write);
4250 INIT_STR(__safe_for_unpickling__);
4251 INIT_STR(append);
4252 INIT_STR(read);
4253 INIT_STR(readline);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004254 INIT_STR(copy_reg);
4255 INIT_STR(dispatch_table);
4256 INIT_STR(safe_constructors);
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004257 INIT_STR(__basicnew__);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004258
4259 UNLESS(copy_reg = PyImport_ImportModule("copy_reg"))
4260 return -1;
4261
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004262 /* These next few are special because we want to use different
4263 ones in restricted mode. */
4264
4265 UNLESS(dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004266 return -1;
4267
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004268 UNLESS(safe_constructors = PyObject_GetAttr(copy_reg,
4269 safe_constructors_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004270 return -1;
4271
4272 Py_DECREF(copy_reg);
4273
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004274 UNLESS(class_map = PyDict_New()) return -1;
4275
4276 /* Down to here ********************************** */
4277
Guido van Rossum60456fd1997-04-09 17:36:32 +00004278 UNLESS(string = PyImport_ImportModule("string"))
4279 return -1;
4280
4281 UNLESS(atol_func = PyObject_GetAttrString(string, "atol"))
4282 return -1;
4283
4284 Py_DECREF(string);
4285
4286 UNLESS(empty_tuple = PyTuple_New(0))
4287 return -1;
4288
Guido van Rossum60456fd1997-04-09 17:36:32 +00004289 UNLESS(PicklingError = PyString_FromString("cPickle.PicklingError"))
4290 return -1;
4291
4292 if (PyDict_SetItemString(module_dict, "PicklingError",
4293 PicklingError) < 0)
4294 return -1;
4295
4296 UNLESS(UnpicklingError = PyString_FromString("cPickle.UnpicklingError"))
4297 return -1;
4298
4299 if (PyDict_SetItemString(module_dict, "UnpicklingError",
4300 UnpicklingError) < 0)
4301 return -1;
4302
4303 PycString_IMPORT;
4304
4305 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004306}
4307
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004308void
Guido van Rossum60456fd1997-04-09 17:36:32 +00004309initcPickle() {
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004310 PyObject *m, *d, *v;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004311 char *rev="1.48";
Guido van Rossum60456fd1997-04-09 17:36:32 +00004312 PyObject *format_version;
4313 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004314
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004315 Picklertype.ob_type = &PyType_Type;
4316 Unpicklertype.ob_type = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004317
Guido van Rossum60456fd1997-04-09 17:36:32 +00004318 /* Create the module and add the functions */
4319 m = Py_InitModule4("cPickle", cPickle_methods,
4320 cPickle_module_documentation,
4321 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004322
Guido van Rossum60456fd1997-04-09 17:36:32 +00004323 /* Add some symbolic constants to the module */
4324 d = PyModule_GetDict(m);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004325 PyDict_SetItemString(d,"__version__", v = PyString_FromString(rev));
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004326 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00004327
Guido van Rossum60456fd1997-04-09 17:36:32 +00004328#ifdef FORMAT_1_3
4329 format_version = PyString_FromString("1.3");
4330 compatible_formats = Py_BuildValue("[sss]", "1.0", "1.1", "1.2");
4331#else
4332 format_version = PyString_FromString("1.2");
4333 compatible_formats = Py_BuildValue("[ss]", "1.0", "1.1");
4334#endif
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004335
Guido van Rossum60456fd1997-04-09 17:36:32 +00004336 PyDict_SetItemString(d, "format_version", format_version);
4337 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004338 Py_XDECREF(format_version);
4339 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004340
Guido van Rossum60456fd1997-04-09 17:36:32 +00004341 init_stuff(m, d);
4342 CHECK_FOR_ERRORS("can't initialize module cPickle");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004343}