blob: 6244f8b33355274cf344562b5c85344f2ff29458 [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 }
685 else {
686 PyErr_Clear();
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000687 }
688
Guido van Rossum60456fd1997-04-09 17:36:32 +0000689 UNLESS(modules_dict = PySys_GetObject("modules"))
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000690 return NULL;
691
Guido van Rossum60456fd1997-04-09 17:36:32 +0000692 i = 0;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000693 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
694
695 if(PyObject_Compare(name, __main___str)==0) continue;
696
Guido van Rossum60456fd1997-04-09 17:36:32 +0000697 UNLESS(global_name_attr = PyObject_GetAttr(module, global_name)) {
698 PyErr_Clear();
699 continue;
700 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000701
Guido van Rossum60456fd1997-04-09 17:36:32 +0000702 if (global_name_attr != global) {
703 Py_DECREF(global_name_attr);
704 continue;
705 }
706
707 Py_DECREF(global_name_attr);
708
709 break;
710 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000711
712 /* The following implements the rule in pickle.py added in 1.5
713 that used __main__ if no module is found. I don't actually
714 like this rule. jlf
715 */
716 if(!j) {
717 j=1;
718 name=__main___str;
719 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000720
Guido van Rossum142eeb81997-08-13 03:14:41 +0000721 /*
Guido van Rossum60456fd1997-04-09 17:36:32 +0000722 if (!j) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +0000723 cPickle_ErrFormat(PicklingError, "Could not find module for %s.",
Guido van Rossum60456fd1997-04-09 17:36:32 +0000724 "O", global_name);
725 return NULL;
726 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000727 */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000728
729 PyDict_SetItem(class_map, global, name);
730
731 Py_INCREF(name);
732 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000733}
734
735
Guido van Rossum60456fd1997-04-09 17:36:32 +0000736static int
737save_none(Picklerobject *self, PyObject *args) {
738 static char none = NONE;
739 if ((*self->write_func)(self, &none, 1) < 0)
740 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000741
Guido van Rossum60456fd1997-04-09 17:36:32 +0000742 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000743}
744
745
Guido van Rossum60456fd1997-04-09 17:36:32 +0000746static int
747save_int(Picklerobject *self, PyObject *args) {
748 char c_str[32];
749 long l = PyInt_AS_LONG((PyIntObject *)args);
750 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000751
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000752 if (!self->bin
753#if SIZEOF_LONG > 4
754 || (l >> 32)
755#endif
756 ) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000757 /* Save extra-long ints in non-binary mode, so that
758 we can use python long parsing code to restore,
759 if necessary. */
760 c_str[0] = INT;
761 sprintf(c_str + 1, "%ld\n", l);
762 if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
763 return -1;
764 }
765 else {
766 c_str[1] = (int)( l & 0xff);
767 c_str[2] = (int)((l >> 8) & 0xff);
768 c_str[3] = (int)((l >> 16) & 0xff);
769 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000770
Guido van Rossum60456fd1997-04-09 17:36:32 +0000771 if ((c_str[4] == 0) && (c_str[3] == 0)) {
772 if (c_str[2] == 0) {
773 c_str[0] = BININT1;
774 len = 2;
775 }
776 else {
777 c_str[0] = BININT2;
778 len = 3;
779 }
780 }
781 else {
782 c_str[0] = BININT;
783 len = 5;
784 }
785
786 if ((*self->write_func)(self, c_str, len) < 0)
787 return -1;
788 }
789
790 return 0;
791}
792
793
794static int
795save_long(Picklerobject *self, PyObject *args) {
796 int size, res = -1;
797 PyObject *repr = 0;
798
799 static char l = LONG;
800
801 UNLESS(repr = PyObject_Repr(args))
802 goto finally;
803
804 if ((size = PyString_Size(repr)) < 0)
805 goto finally;
806
807 if ((*self->write_func)(self, &l, 1) < 0)
808 goto finally;
809
810 if ((*self->write_func)(self,
811 PyString_AS_STRING((PyStringObject *)repr), size) < 0)
812 goto finally;
813
814 if ((*self->write_func)(self, "\n", 1) < 0)
815 goto finally;
816
817 res = 0;
818
819finally:
820 Py_XDECREF(repr);
821
822 return res;
823}
824
825
826static int
827save_float(Picklerobject *self, PyObject *args) {
828 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
829
830#ifdef FORMAT_1_3
831 if (self->bin) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000832 int s, e;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000833 double f;
834 long fhi, flo;
835 char str[9], *p = str;
836
837 *p = BINFLOAT;
838 p++;
839
840 if (x < 0) {
841 s = 1;
842 x = -x;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000843 }
844 else
Guido van Rossum60456fd1997-04-09 17:36:32 +0000845 s = 0;
846
847 f = frexp(x, &e);
848
849 /* Normalize f to be in the range [1.0, 2.0) */
850 if (0.5 <= f && f < 1.0) {
851 f *= 2.0;
852 e--;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000853 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000854 else if (f == 0.0) {
855 e = 0;
856 }
857 else {
858 PyErr_SetString(PyExc_SystemError,
859 "frexp() result out of range");
860 return -1;
861 }
862
863 if (e >= 1024) {
864 /* XXX 1024 itself is reserved for Inf/NaN */
865 PyErr_SetString(PyExc_OverflowError,
866 "float too large to pack with d format");
867 return -1;
868 }
869 else if (e < -1022) {
870 /* Gradual underflow */
871 f = ldexp(f, 1022 + e);
872 e = 0;
873 }
874 else {
875 e += 1023;
876 f -= 1.0; /* Get rid of leading 1 */
877 }
878
879 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
880 f *= 268435456.0; /* 2**28 */
881 fhi = (long) floor(f); /* Truncate */
882 f -= (double)fhi;
883 f *= 16777216.0; /* 2**24 */
884 flo = (long) floor(f + 0.5); /* Round */
885
886 /* First byte */
887 *p = (s<<7) | (e>>4);
888 p++;
889
890 /* Second byte */
891 *p = ((e&0xF)<<4) | (fhi>>24);
892 p++;
893
894 /* Third byte */
895 *p = (fhi>>16) & 0xFF;
896 p++;
897
898 /* Fourth byte */
899 *p = (fhi>>8) & 0xFF;
900 p++;
901
902 /* Fifth byte */
903 *p = fhi & 0xFF;
904 p++;
905
906 /* Sixth byte */
907 *p = (flo>>16) & 0xFF;
908 p++;
909
910 /* Seventh byte */
911 *p = (flo>>8) & 0xFF;
912 p++;
913
914 /* Eighth byte */
915 *p = flo & 0xFF;
916
917 if ((*self->write_func)(self, str, 9) < 0)
918 return -1;
919 }
920 else
921#endif
922 {
923 char c_str[250];
924 c_str[0] = FLOAT;
925 sprintf(c_str + 1, "%f\n", x);
926
927 if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
928 return -1;
929 }
930
931 return 0;
932}
933
934
935static int
Guido van Rossum142eeb81997-08-13 03:14:41 +0000936save_string(Picklerobject *self, PyObject *args, int doput) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000937 int size, len;
938
939 size = PyString_Size(args);
940
941 if (!self->bin) {
942 PyObject *repr;
943 char *repr_str;
944
945 static char string = STRING;
946
947 UNLESS(repr = PyObject_Repr(args))
948 return -1;
949
950 repr_str = PyString_AS_STRING((PyStringObject *)repr);
951 len = PyString_Size(repr);
952
953 if ((*self->write_func)(self, &string, 1) < 0)
954 return -1;
955
956 if ((*self->write_func)(self, repr_str, len) < 0)
957 return -1;
958
959 if ((*self->write_func)(self, "\n", 1) < 0)
960 return -1;
961
962 Py_XDECREF(repr);
963 }
964 else {
965 int i;
966 char c_str[5];
967
968 size = PyString_Size(args);
969
970 if (size < 256) {
971 c_str[0] = SHORT_BINSTRING;
972 c_str[1] = size;
973 len = 2;
974 }
975 else {
976 c_str[0] = BINSTRING;
977 for (i = 1; i < 5; i++)
978 c_str[i] = (int)(size >> ((i - 1) * 8));
979 len = 5;
980 }
981
982 if ((*self->write_func)(self, c_str, len) < 0)
983 return -1;
984
985 if ((*self->write_func)(self,
986 PyString_AS_STRING((PyStringObject *)args), size) < 0)
987 return -1;
988 }
989
Guido van Rossum142eeb81997-08-13 03:14:41 +0000990 if (doput)
991 if (put(self, args) < 0)
992 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000993
994 return 0;
995}
996
997
998static int
999save_tuple(Picklerobject *self, PyObject *args) {
1000 PyObject *element = 0, *py_tuple_id = 0;
1001 int len, i, has_key, res = -1;
1002
1003 static char tuple = TUPLE;
1004
1005 if ((*self->write_func)(self, &MARKv, 1) < 0)
1006 goto finally;
1007
1008 if ((len = PyTuple_Size(args)) < 0)
1009 goto finally;
1010
1011 for (i = 0; i < len; i++) {
1012 UNLESS(element = PyTuple_GET_ITEM((PyTupleObject *)args, i))
1013 goto finally;
1014
1015 if (save(self, element, 0) < 0)
1016 goto finally;
1017 }
1018
1019 UNLESS(py_tuple_id = PyInt_FromLong((long)args))
1020 goto finally;
1021
1022 if (len) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001023 if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_tuple_id)) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001024 goto finally;
1025
1026 if (has_key) {
1027 if (self->bin) {
1028 static char pop_mark = POP_MARK;
1029
1030 if ((*self->write_func)(self, &pop_mark, 1) < 0)
1031 goto finally;
1032 }
1033 else {
1034 static char pop = POP;
1035
1036 for (i = 0; i <= len; i++) {
1037 if ((*self->write_func)(self, &pop, 1) < 0)
1038 goto finally;
1039 }
1040 }
1041
1042 if (get(self, py_tuple_id) < 0)
1043 goto finally;
1044
1045 res = 0;
1046 goto finally;
1047 }
1048 }
1049
1050 if ((*self->write_func)(self, &tuple, 1) < 0) {
1051 goto finally;
1052 }
1053
1054 if (put(self, args) < 0)
1055 goto finally;
1056
1057 res = 0;
1058
1059finally:
1060 Py_XDECREF(py_tuple_id);
1061
1062 return res;
1063}
1064
1065static int
1066save_empty_tuple(Picklerobject *self, PyObject *args) {
1067 static char tuple = EMPTY_TUPLE;
1068
1069 return (*self->write_func)(self, &tuple, 1);
1070}
1071
1072
1073static int
1074save_list(Picklerobject *self, PyObject *args) {
1075 PyObject *element = 0;
1076 int s_len, len, i, using_appends, res = -1;
1077 char s[3];
1078
1079 static char append = APPEND, appends = APPENDS;
1080
1081 if(self->bin) {
1082 s[0] = EMPTY_LIST;
1083 s_len = 1;
1084 }
1085 else {
1086 s[0] = MARK;
1087 s[1] = LIST;
1088 s_len = 2;
1089 }
1090
1091 if ((len = PyList_Size(args)) < 0)
1092 goto finally;
1093
1094 if ((*self->write_func)(self, s, s_len) < 0)
1095 goto finally;
1096
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001097 if (len == 0) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001098 if (put(self, args) < 0)
1099 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001100 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001101 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001102 if (put2(self, args) < 0)
1103 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001104 }
1105
Guido van Rossum142eeb81997-08-13 03:14:41 +00001106 if ((using_appends = (self->bin && (len > 1))))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001107 if ((*self->write_func)(self, &MARKv, 1) < 0)
1108 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001109
Guido van Rossum60456fd1997-04-09 17:36:32 +00001110 for (i = 0; i < len; i++) {
1111 UNLESS(element = PyList_GET_ITEM((PyListObject *)args, i))
1112 goto finally;
1113
1114 if (save(self, element, 0) < 0)
1115 goto finally;
1116
1117 if (!using_appends) {
1118 if ((*self->write_func)(self, &append, 1) < 0)
1119 goto finally;
1120 }
1121 }
1122
1123 if (using_appends) {
1124 if ((*self->write_func)(self, &appends, 1) < 0)
1125 goto finally;
1126 }
1127
1128 res = 0;
1129
1130finally:
1131
1132 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001133}
1134
1135
Guido van Rossum60456fd1997-04-09 17:36:32 +00001136static int
1137save_dict(Picklerobject *self, PyObject *args) {
1138 PyObject *key = 0, *value = 0;
1139 int i, len, res = -1, using_setitems;
1140 char s[3];
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001141
Guido van Rossum60456fd1997-04-09 17:36:32 +00001142 static char setitem = SETITEM, setitems = SETITEMS;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001143
Guido van Rossum60456fd1997-04-09 17:36:32 +00001144 if (self->bin) {
1145 s[0] = EMPTY_DICT;
1146 len = 1;
1147 }
1148 else {
1149 s[0] = MARK;
1150 s[1] = DICT;
1151 len = 2;
1152 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001153
Guido van Rossum60456fd1997-04-09 17:36:32 +00001154 if ((*self->write_func)(self, s, len) < 0)
1155 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001156
Guido van Rossum60456fd1997-04-09 17:36:32 +00001157 if ((len = PyDict_Size(args)) < 0)
1158 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001159
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001160 if (len == 0) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001161 if (put(self, args) < 0)
1162 goto finally;
1163 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001164 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001165 if (put2(self, args) < 0)
1166 goto finally;
1167 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001168
Guido van Rossum142eeb81997-08-13 03:14:41 +00001169 if ((using_setitems = (self->bin && (PyDict_Size(args) > 1))))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001170 if ((*self->write_func)(self, &MARKv, 1) < 0)
1171 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001172
Guido van Rossum60456fd1997-04-09 17:36:32 +00001173 i = 0;
1174 while (PyDict_Next(args, &i, &key, &value)) {
1175 if (save(self, key, 0) < 0)
1176 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001177
Guido van Rossum60456fd1997-04-09 17:36:32 +00001178 if (save(self, value, 0) < 0)
1179 goto finally;
1180
1181 if (!using_setitems) {
1182 if ((*self->write_func)(self, &setitem, 1) < 0)
1183 goto finally;
1184 }
1185 }
1186
1187 if (using_setitems) {
1188 if ((*self->write_func)(self, &setitems, 1) < 0)
1189 goto finally;
1190 }
1191
1192 res = 0;
1193
1194finally:
1195
1196 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001197}
1198
1199
Guido van Rossum60456fd1997-04-09 17:36:32 +00001200static int
1201save_inst(Picklerobject *self, PyObject *args) {
1202 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1203 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1204 char *module_str, *name_str;
1205 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001206
Guido van Rossum60456fd1997-04-09 17:36:32 +00001207 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001208
Guido van Rossum60456fd1997-04-09 17:36:32 +00001209 if ((*self->write_func)(self, &MARKv, 1) < 0)
1210 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001211
Guido van Rossum60456fd1997-04-09 17:36:32 +00001212 UNLESS(class = PyObject_GetAttr(args, __class___str))
1213 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001214
Guido van Rossum60456fd1997-04-09 17:36:32 +00001215 if (self->bin) {
1216 if (save(self, class, 0) < 0)
1217 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001218 }
1219
Guido van Rossum142eeb81997-08-13 03:14:41 +00001220 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001221 PyObject *element = 0;
1222 int i, len;
1223
1224 UNLESS(class_args =
1225 PyObject_CallObject(getinitargs_func, empty_tuple))
1226 goto finally;
1227
1228 if ((len = PyObject_Length(class_args)) < 0)
1229 goto finally;
1230
1231 for (i = 0; i < len; i++) {
1232 UNLESS(element = PySequence_GetItem(class_args, i))
1233 goto finally;
1234
1235 if (save(self, element, 0) < 0) {
1236 Py_DECREF(element);
1237 goto finally;
1238 }
1239
1240 Py_DECREF(element);
1241 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001242 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001243 else {
1244 PyErr_Clear();
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001245 }
1246
Guido van Rossum60456fd1997-04-09 17:36:32 +00001247 if (!self->bin) {
1248 UNLESS(name = ((PyClassObject *)class)->cl_name) {
1249 PyErr_SetString(PicklingError, "class has no name");
1250 goto finally;
1251 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001252
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001253 UNLESS(module = whichmodule(self->class_map, class, name))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001254 goto finally;
1255
1256 module_str = PyString_AS_STRING((PyStringObject *)module);
1257 module_size = PyString_Size(module);
1258 name_str = PyString_AS_STRING((PyStringObject *)name);
1259 name_size = PyString_Size(name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001260
Guido van Rossum60456fd1997-04-09 17:36:32 +00001261 if ((*self->write_func)(self, &inst, 1) < 0)
1262 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001263
Guido van Rossum60456fd1997-04-09 17:36:32 +00001264 if ((*self->write_func)(self, module_str, module_size) < 0)
1265 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001266
Guido van Rossum60456fd1997-04-09 17:36:32 +00001267 if ((*self->write_func)(self, "\n", 1) < 0)
1268 goto finally;
1269
1270 if ((*self->write_func)(self, name_str, name_size) < 0)
1271 goto finally;
1272
1273 if ((*self->write_func)(self, "\n", 1) < 0)
1274 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001275 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001276 else if ((*self->write_func)(self, &obj, 1) < 0) {
1277 goto finally;
1278 }
1279
Guido van Rossum142eeb81997-08-13 03:14:41 +00001280 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001281 UNLESS(state = PyObject_CallObject(getstate_func, empty_tuple))
1282 goto finally;
1283 }
1284 else {
1285 PyErr_Clear();
1286
1287 UNLESS(state = PyObject_GetAttr(args, __dict___str)) {
1288 PyErr_Clear();
1289 res = 0;
1290 goto finally;
1291 }
1292 }
1293
1294 if (!PyDict_Check(state)) {
1295 if (put2(self, args) < 0)
1296 goto finally;
1297 }
1298 else {
1299 if (put(self, args) < 0)
1300 goto finally;
1301 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001302
Guido van Rossum60456fd1997-04-09 17:36:32 +00001303 if (save(self, state, 0) < 0)
1304 goto finally;
1305
1306 if ((*self->write_func)(self, &build, 1) < 0)
1307 goto finally;
1308
1309 res = 0;
1310
1311finally:
1312 Py_XDECREF(module);
1313 Py_XDECREF(class);
1314 Py_XDECREF(state);
1315 Py_XDECREF(getinitargs_func);
1316 Py_XDECREF(getstate_func);
1317 Py_XDECREF(class_args);
1318
1319 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001320}
1321
1322
Guido van Rossum60456fd1997-04-09 17:36:32 +00001323static int
1324save_global(Picklerobject *self, PyObject *args, PyObject *name) {
1325 PyObject *global_name = 0, *module = 0;
1326 char *name_str, *module_str;
1327 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001328
Guido van Rossum60456fd1997-04-09 17:36:32 +00001329 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001330
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001331 if (name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001332 global_name = name;
1333 Py_INCREF(global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001334 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001335 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001336 UNLESS(global_name = PyObject_GetAttr(args, __name___str))
1337 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001338 }
1339
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001340 UNLESS(module = whichmodule(self->class_map, args, global_name))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001341 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001342
1343 module_str = PyString_AS_STRING((PyStringObject *)module);
1344 module_size = PyString_Size(module);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001345 name_str = PyString_AS_STRING((PyStringObject *)global_name);
1346 name_size = PyString_Size(global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001347
Guido van Rossum60456fd1997-04-09 17:36:32 +00001348 if ((*self->write_func)(self, &global, 1) < 0)
1349 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001350
Guido van Rossum60456fd1997-04-09 17:36:32 +00001351 if ((*self->write_func)(self, module_str, module_size) < 0)
1352 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001353
Guido van Rossum60456fd1997-04-09 17:36:32 +00001354 if ((*self->write_func)(self, "\n", 1) < 0)
1355 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001356
Guido van Rossum60456fd1997-04-09 17:36:32 +00001357 if ((*self->write_func)(self, name_str, name_size) < 0)
1358 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001359
Guido van Rossum60456fd1997-04-09 17:36:32 +00001360 if ((*self->write_func)(self, "\n", 1) < 0)
1361 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001362
Guido van Rossum60456fd1997-04-09 17:36:32 +00001363 if (put(self, args) < 0)
1364 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001365
Guido van Rossum60456fd1997-04-09 17:36:32 +00001366 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001367
Guido van Rossum60456fd1997-04-09 17:36:32 +00001368finally:
1369 Py_XDECREF(module);
1370 Py_XDECREF(global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001371
Guido van Rossum60456fd1997-04-09 17:36:32 +00001372 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001373}
1374
Guido van Rossum60456fd1997-04-09 17:36:32 +00001375static int
1376save_pers(Picklerobject *self, PyObject *args, PyObject *f) {
1377 PyObject *pid = 0;
1378 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001379
Guido van Rossum60456fd1997-04-09 17:36:32 +00001380 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001381
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001382 UNLESS(self->arg)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001383 UNLESS(self->arg = PyTuple_New(1))
1384 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001385
Guido van Rossum60456fd1997-04-09 17:36:32 +00001386 Py_INCREF(args);
1387 if (PyTuple_SetItem(self->arg, 0, args) < 0)
1388 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001389
Guido van Rossum60456fd1997-04-09 17:36:32 +00001390 UNLESS(pid = PyObject_CallObject(f, self->arg))
1391 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001392
Guido van Rossum60456fd1997-04-09 17:36:32 +00001393 if (pid != Py_None) {
1394 if (!self->bin) {
1395 if (!PyString_Check(pid)) {
1396 PyErr_SetString(PicklingError,
1397 "persistent id must be string");
1398 goto finally;
1399 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001400
Guido van Rossum60456fd1997-04-09 17:36:32 +00001401 if ((*self->write_func)(self, &persid, 1) < 0)
1402 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001403
Guido van Rossum60456fd1997-04-09 17:36:32 +00001404 if ((size = PyString_Size(pid)) < 0)
1405 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001406
Guido van Rossum60456fd1997-04-09 17:36:32 +00001407 if ((*self->write_func)(self,
1408 PyString_AS_STRING((PyStringObject *)pid), size) < 0)
1409 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001410
Guido van Rossum60456fd1997-04-09 17:36:32 +00001411 if ((*self->write_func)(self, "\n", 1) < 0)
1412 goto finally;
1413
1414 res = 1;
1415 goto finally;
1416 }
1417 else if (save(self, pid, 1) >= 0) {
1418 if ((*self->write_func)(self, &binpersid, 1) < 0)
1419 res = -1;
1420 else
1421 res = 1;
1422 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001423
Guido van Rossum60456fd1997-04-09 17:36:32 +00001424 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001425 }
1426
Guido van Rossum60456fd1997-04-09 17:36:32 +00001427 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001428
Guido van Rossum60456fd1997-04-09 17:36:32 +00001429finally:
1430 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001431
Guido van Rossum60456fd1997-04-09 17:36:32 +00001432 return res;
1433}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001434
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001435
Guido van Rossum60456fd1997-04-09 17:36:32 +00001436static int
1437save_reduce(Picklerobject *self, PyObject *callable,
1438 PyObject *tup, PyObject *state, PyObject *ob) {
1439 static char reduce = REDUCE, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001440
Guido van Rossum60456fd1997-04-09 17:36:32 +00001441 if (save(self, callable, 0) < 0)
1442 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001443
Guido van Rossum60456fd1997-04-09 17:36:32 +00001444 if (save(self, tup, 0) < 0)
1445 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001446
Guido van Rossum60456fd1997-04-09 17:36:32 +00001447 if ((*self->write_func)(self, &reduce, 1) < 0)
1448 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001449
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001450 if (ob != NULL) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001451 if (state && !PyDict_Check(state)) {
1452 if (put2(self, ob) < 0)
1453 return -1;
1454 }
1455 else {
1456 if (put(self, ob) < 0)
1457 return -1;
1458 }
1459 }
1460
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001461 if (state) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001462 if (save(self, state, 0) < 0)
1463 return -1;
1464
1465 if ((*self->write_func)(self, &build, 1) < 0)
1466 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001467 }
1468
Guido van Rossum60456fd1997-04-09 17:36:32 +00001469 return 0;
1470}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001471
Guido van Rossum60456fd1997-04-09 17:36:32 +00001472static int
1473save(Picklerobject *self, PyObject *args, int pers_save) {
1474 PyTypeObject *type;
1475 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0, *arg_tup = 0,
Guido van Rossum142eeb81997-08-13 03:14:41 +00001476 *callable = 0, *state = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001477 int res = -1, tmp, size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001478
Guido van Rossum60456fd1997-04-09 17:36:32 +00001479 if (!pers_save && self->pers_func) {
1480 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
1481 res = tmp;
1482 goto finally;
1483 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001484 }
1485
Guido van Rossum60456fd1997-04-09 17:36:32 +00001486 if (args == Py_None) {
1487 res = save_none(self, args);
1488 goto finally;
1489 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001490
Guido van Rossum60456fd1997-04-09 17:36:32 +00001491 type = args->ob_type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001492
Guido van Rossum60456fd1997-04-09 17:36:32 +00001493 switch (type->tp_name[0]) {
1494 case 'i':
1495 if (type == &PyInt_Type) {
1496 res = save_int(self, args);
1497 goto finally;
1498 }
1499 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001500
Guido van Rossum60456fd1997-04-09 17:36:32 +00001501 case 'l':
1502 if (type == &PyLong_Type) {
1503 res = save_long(self, args);
1504 goto finally;
1505 }
1506 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001507
Guido van Rossum60456fd1997-04-09 17:36:32 +00001508 case 'f':
1509 if (type == &PyFloat_Type) {
1510 res = save_float(self, args);
1511 goto finally;
1512 }
1513 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001514
Guido van Rossum60456fd1997-04-09 17:36:32 +00001515 case 't':
1516 if (type == &PyTuple_Type && PyTuple_Size(args)==0) {
1517 if(self->bin) res = save_empty_tuple(self, args);
1518 else res = save_tuple(self, args);
1519 goto finally;
1520 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001521
Guido van Rossum60456fd1997-04-09 17:36:32 +00001522 case 's':
1523 if ((type == &PyString_Type) && (PyString_Size(args) < 2)) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001524 res = save_string(self, args, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001525 goto finally;
1526 }
1527 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001528
Guido van Rossum60456fd1997-04-09 17:36:32 +00001529 if (args->ob_refcnt > 1) {
1530 long ob_id;
1531 int has_key;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001532
Guido van Rossum60456fd1997-04-09 17:36:32 +00001533 ob_id = (long)args;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001534
Guido van Rossum60456fd1997-04-09 17:36:32 +00001535 UNLESS(py_ob_id = PyInt_FromLong(ob_id))
1536 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001537
Guido van Rossum60456fd1997-04-09 17:36:32 +00001538 if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_ob_id)) < 0)
1539 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001540
Guido van Rossum60456fd1997-04-09 17:36:32 +00001541 if (has_key) {
1542 if (get(self, py_ob_id) < 0)
1543 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001544
Guido van Rossum60456fd1997-04-09 17:36:32 +00001545 res = 0;
1546 goto finally;
1547 }
1548 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001549
Guido van Rossum60456fd1997-04-09 17:36:32 +00001550 switch (type->tp_name[0]) {
1551 case 's':
1552 if (type == &PyString_Type) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001553 res = save_string(self, args, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001554 goto finally;
1555 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001556
Guido van Rossum60456fd1997-04-09 17:36:32 +00001557 case 't':
1558 if (type == &PyTuple_Type) {
1559 res = save_tuple(self, args);
1560 goto finally;
1561 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001562
Guido van Rossum60456fd1997-04-09 17:36:32 +00001563 case 'l':
1564 if (type == &PyList_Type) {
1565 res = save_list(self, args);
1566 goto finally;
1567 }
1568
1569 case 'd':
1570 if (type == &PyDict_Type) {
1571 res = save_dict(self, args);
1572 goto finally;
1573 }
1574
1575 case 'i':
1576 if (type == &PyInstance_Type) {
1577 res = save_inst(self, args);
1578 goto finally;
1579 }
1580
1581 case 'c':
1582 if (type == &PyClass_Type) {
1583 res = save_global(self, args, NULL);
1584 goto finally;
1585 }
1586
1587 case 'f':
1588 if (type == &PyFunction_Type) {
1589 res = save_global(self, args, NULL);
1590 goto finally;
1591 }
1592
1593 case 'b':
1594 if (type == &PyCFunction_Type) {
1595 res = save_global(self, args, NULL);
1596 goto finally;
1597 }
1598 }
1599
1600 if (!pers_save && self->inst_pers_func) {
1601 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
1602 res = tmp;
1603 goto finally;
1604 }
1605 }
1606
Guido van Rossum142eeb81997-08-13 03:14:41 +00001607 if ((__reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001608 Py_INCREF(__reduce__);
1609
1610 UNLESS(self->arg)
1611 UNLESS(self->arg = PyTuple_New(1))
1612 goto finally;
1613
1614 Py_INCREF(args);
1615 if (PyTuple_SetItem(self->arg, 0, args) < 0)
1616 goto finally;
1617
1618 UNLESS(t = PyObject_CallObject(__reduce__, self->arg))
1619 goto finally;
1620 }
1621 else {
1622 PyErr_Clear();
1623
Guido van Rossum142eeb81997-08-13 03:14:41 +00001624 if ((__reduce__ = PyObject_GetAttr(args, __reduce___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001625 UNLESS(t = PyObject_CallObject(__reduce__, empty_tuple))
1626 goto finally;
1627 }
1628 else {
1629 PyErr_Clear();
1630 }
1631 }
1632
1633 if (t) {
1634 if (PyString_Check(t)) {
1635 res = save_global(self, args, t);
1636 goto finally;
1637 }
1638
1639 if (!PyTuple_Check(t)) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001640 cPickle_ErrFormat(PicklingError, "Value returned by %s must "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001641 "be a tuple", "O", __reduce__);
1642 goto finally;
1643 }
1644
1645 size = PyTuple_Size(t);
1646
1647 if ((size != 3) && (size != 2)) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001648 cPickle_ErrFormat(PicklingError, "tuple returned by %s must "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001649 "contain only two or three elements", "O", __reduce__);
1650 goto finally;
1651 }
1652
1653 callable = PyTuple_GET_ITEM(t, 0);
Guido van Rossum142eeb81997-08-13 03:14:41 +00001654
Guido van Rossum60456fd1997-04-09 17:36:32 +00001655 arg_tup = PyTuple_GET_ITEM(t, 1);
1656
1657 if (size > 2) {
1658 state = PyTuple_GET_ITEM(t, 2);
1659 }
1660
Guido van Rossum9716aaa1997-12-08 15:15:16 +00001661 UNLESS(PyTuple_Check(arg_tup) || arg_tup==Py_None) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001662 cPickle_ErrFormat(PicklingError, "Second element of tuple "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001663 "returned by %s must be a tuple", "O", __reduce__);
1664 goto finally;
1665 }
1666
1667 res = save_reduce(self, callable, arg_tup, state, args);
1668 goto finally;
1669 }
1670
1671 /*
1672 if (PyObject_HasAttrString(args, "__class__")) {
1673 res = save_inst(self, args);
1674 goto finally;
1675 }
1676 */
1677
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001678 cPickle_ErrFormat(PicklingError, "Cannot pickle %s objects.",
Guido van Rossum60456fd1997-04-09 17:36:32 +00001679 "O", (PyObject *)type);
1680
1681finally:
1682 Py_XDECREF(py_ob_id);
1683 Py_XDECREF(__reduce__);
1684 Py_XDECREF(t);
1685
1686 return res;
1687}
1688
1689
1690static int
1691dump(Picklerobject *self, PyObject *args) {
1692 static char stop = STOP;
1693
1694 if (save(self, args, 0) < 0)
1695 return -1;
1696
1697 if ((*self->write_func)(self, &stop, 1) < 0)
1698 return -1;
1699
1700 if ((*self->write_func)(self, NULL, 0) < 0)
1701 return -1;
1702
1703 return 0;
1704}
1705
1706static PyObject *
1707Pickler_dump(Picklerobject *self, PyObject *args) {
1708 PyObject *ob;
1709
1710 UNLESS(PyArg_ParseTuple(args, "O", &ob))
1711 return NULL;
1712
1713 if (dump(self, ob) < 0)
1714 return NULL;
1715
1716 Py_INCREF(Py_None);
1717 return Py_None;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001718}
1719
1720
1721static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00001722dump_special(Picklerobject *self, PyObject *args) {
1723 static char stop = STOP;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001724
Guido van Rossum60456fd1997-04-09 17:36:32 +00001725 PyObject *callable, *arg_tup, *state = NULL;
1726
1727 UNLESS(PyArg_ParseTuple(args, "OO|O", &callable, &arg_tup, &state))
1728 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001729
Guido van Rossum60456fd1997-04-09 17:36:32 +00001730 UNLESS(PyTuple_Check(arg_tup)) {
1731 PyErr_SetString(PicklingError, "Second arg to dump_special must "
1732 "be tuple");
1733 return NULL;
1734 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001735
Guido van Rossum60456fd1997-04-09 17:36:32 +00001736 if (save_reduce(self, callable, arg_tup, state, NULL) < 0)
1737 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001738
Guido van Rossum60456fd1997-04-09 17:36:32 +00001739 if ((*self->write_func)(self, &stop, 1) < 0)
1740 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001741
Guido van Rossum60456fd1997-04-09 17:36:32 +00001742 if ((*self->write_func)(self, NULL, 0) < 0)
1743 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001744
Guido van Rossum60456fd1997-04-09 17:36:32 +00001745 Py_INCREF(Py_None);
1746 return Py_None;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001747}
1748
Guido van Rossum142eeb81997-08-13 03:14:41 +00001749static PyObject *
1750Pickle_clear_memo(Picklerobject *self, PyObject *args) {
1751 if(self->memo) PyDict_Clear(self->memo);
1752 Py_INCREF(Py_None);
1753 return Py_None;
1754}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001755
1756static struct PyMethodDef Pickler_methods[] = {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001757 {"dump", (PyCFunction)Pickler_dump, 1,
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001758 "dump(object) --"
1759 "Write an object in pickle format to the object's pickle stream\n"
1760 },
Guido van Rossum142eeb81997-08-13 03:14:41 +00001761 {"dump_special", (PyCFunction)dump_special, 1,
1762 ""},
1763 {"clear_memo", (PyCFunction)Pickle_clear_memo, 1,
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001764 "clear_memo() -- Clear the picklers memo"},
Guido van Rossum60456fd1997-04-09 17:36:32 +00001765 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001766};
1767
1768
1769static Picklerobject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00001770newPicklerobject(PyObject *file, int bin) {
1771 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001772
Guido van Rossum60456fd1997-04-09 17:36:32 +00001773 UNLESS(self = PyObject_NEW(Picklerobject, &Picklertype))
1774 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001775
1776 self->fp = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001777 self->write = NULL;
1778 self->memo = NULL;
1779 self->arg = NULL;
1780 self->pers_func = NULL;
1781 self->inst_pers_func = NULL;
1782 self->write_buf = NULL;
1783 self->bin = bin;
1784 self->buf_size = 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001785 self->class_map = NULL;
1786 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001787
Guido van Rossum60456fd1997-04-09 17:36:32 +00001788 Py_INCREF(file);
1789 self->file = file;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001790
Guido van Rossum60456fd1997-04-09 17:36:32 +00001791 UNLESS(self->memo = PyDict_New()) {
1792 Py_XDECREF((PyObject *)self);
1793 return NULL;
1794 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001795
Guido van Rossum60456fd1997-04-09 17:36:32 +00001796 if (PyFile_Check(file)) {
1797 self->fp = PyFile_AsFile(file);
1798 self->write_func = write_file;
1799 }
1800 else if (PycStringIO_OutputCheck(file)) {
1801 self->write_func = write_cStringIO;
1802 }
Guido van Rossum142eeb81997-08-13 03:14:41 +00001803 else if (file == Py_None) {
1804 self->write_func = write_none;
1805 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001806 else {
1807 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001808
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001809 UNLESS(self->write = PyObject_GetAttr(file, write_str)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001810 PyErr_Clear();
1811 PyErr_SetString(PyExc_TypeError, "argument must have 'write' "
1812 "attribute");
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001813 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001814 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001815
Guido van Rossum60456fd1997-04-09 17:36:32 +00001816 UNLESS(self->write_buf =
1817 (char *)malloc(WRITE_BUF_SIZE * sizeof(char))) {
1818 PyErr_NoMemory();
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001819 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001820 }
1821 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001822
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001823 if(PyEval_GetRestricted()) {
1824 /* Restricted execution, get private tables */
1825 PyObject *m;
1826
1827 UNLESS(self->class_map=PyDict_New()) goto err;
1828 UNLESS(m=PyImport_Import(copy_reg_str)) goto err;
1829 self->dispatch_table=PyObject_GetAttr(m, dispatch_table_str);
1830 Py_DECREF(m);
1831 UNLESS(self->dispatch_table) goto err;
1832 }
1833 else {
1834 self->class_map=class_map;
1835 Py_INCREF(class_map);
1836 self->dispatch_table=dispatch_table;
1837 Py_INCREF(dispatch_table);
1838 }
1839
Guido van Rossum60456fd1997-04-09 17:36:32 +00001840 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001841
1842err:
1843 Py_DECREF((PyObject *)self);
1844 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001845}
1846
1847
1848static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00001849get_Pickler(PyObject *self, PyObject *args) {
1850 PyObject *file;
1851 int bin = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001852
Guido van Rossum60456fd1997-04-09 17:36:32 +00001853 UNLESS(PyArg_ParseTuple(args, "O|i", &file, &bin)) return NULL;
1854 return (PyObject *)newPicklerobject(file, bin);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001855}
1856
1857
1858static void
Guido van Rossum60456fd1997-04-09 17:36:32 +00001859Pickler_dealloc(Picklerobject *self) {
1860 Py_XDECREF(self->write);
1861 Py_XDECREF(self->memo);
1862 Py_XDECREF(self->arg);
1863 Py_XDECREF(self->file);
1864 Py_XDECREF(self->pers_func);
1865 Py_XDECREF(self->inst_pers_func);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001866 Py_XDECREF(self->class_map);
1867 Py_XDECREF(self->dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001868
1869 if (self->write_buf) {
1870 free(self->write_buf);
1871 }
1872
1873 PyMem_DEL(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001874}
1875
1876
1877static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00001878Pickler_getattr(Picklerobject *self, char *name) {
1879 if (strcmp(name, "persistent_id") == 0) {
1880 if (!self->pers_func) {
1881 PyErr_SetString(PyExc_AttributeError, name);
1882 return NULL;
1883 }
1884
1885 Py_INCREF(self->pers_func);
1886 return self->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001887 }
1888
Guido van Rossum60456fd1997-04-09 17:36:32 +00001889 if (strcmp(name, "memo") == 0) {
1890 if (!self->memo) {
1891 PyErr_SetString(PyExc_AttributeError, name);
1892 return NULL;
1893 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001894
Guido van Rossum60456fd1997-04-09 17:36:32 +00001895 Py_INCREF(self->memo);
1896 return self->memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001897 }
1898
Guido van Rossum60456fd1997-04-09 17:36:32 +00001899 if (strcmp(name, "PicklingError") == 0) {
1900 Py_INCREF(PicklingError);
1901 return PicklingError;
1902 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001903
Guido van Rossum60456fd1997-04-09 17:36:32 +00001904 return Py_FindMethod(Pickler_methods, (PyObject *)self, name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001905}
1906
1907
1908int
Guido van Rossum60456fd1997-04-09 17:36:32 +00001909Pickler_setattr(Picklerobject *self, char *name, PyObject *value) {
1910 if (strcmp(name, "persistent_id") == 0) {
1911 Py_XDECREF(self->pers_func);
1912 self->pers_func = value;
1913 Py_INCREF(value);
1914 return 0;
1915 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001916
Guido van Rossum60456fd1997-04-09 17:36:32 +00001917 if (strcmp(name, "inst_persistent_id") == 0) {
1918 Py_XDECREF(self->inst_pers_func);
1919 self->inst_pers_func = value;
1920 Py_INCREF(value);
1921 return 0;
1922 }
1923
1924 PyErr_SetString(PyExc_AttributeError, name);
1925 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001926}
1927
1928
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001929static char Picklertype__doc__[] =
1930"Objects that know how to pickle objects\n"
1931;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001932
Guido van Rossum9716aaa1997-12-08 15:15:16 +00001933static PyTypeObject Picklertype = {
1934 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001935 0, /*ob_size*/
1936 "Pickler", /*tp_name*/
1937 sizeof(Picklerobject), /*tp_basicsize*/
1938 0, /*tp_itemsize*/
1939 /* methods */
1940 (destructor)Pickler_dealloc, /*tp_dealloc*/
1941 (printfunc)0, /*tp_print*/
1942 (getattrfunc)Pickler_getattr, /*tp_getattr*/
1943 (setattrfunc)Pickler_setattr, /*tp_setattr*/
1944 (cmpfunc)0, /*tp_compare*/
1945 (reprfunc)0, /*tp_repr*/
1946 0, /*tp_as_number*/
1947 0, /*tp_as_sequence*/
1948 0, /*tp_as_mapping*/
1949 (hashfunc)0, /*tp_hash*/
1950 (ternaryfunc)0, /*tp_call*/
1951 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001952
Guido van Rossum60456fd1997-04-09 17:36:32 +00001953 /* Space for future expansion */
1954 0L,0L,0L,0L,
1955 Picklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00001956};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001957
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001958static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001959find_class(PyObject *class_map,
1960 PyObject *py_module_name, PyObject *py_global_name) {
1961 PyObject *global = 0, *t = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001962
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001963 UNLESS(t = PyTuple_New(2)) return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001964
Guido van Rossum60456fd1997-04-09 17:36:32 +00001965 PyTuple_SET_ITEM((PyTupleObject *)t, 0, py_module_name);
1966 Py_INCREF(py_module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001967 PyTuple_SET_ITEM((PyTupleObject *)t, 1, py_global_name);
1968 Py_INCREF(py_global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001969
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001970 global=PyDict_GetItem(class_map, t);
1971
1972 if (global) {
1973 Py_DECREF(t);
1974 Py_INCREF(global);
1975 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001976 }
1977
Guido van Rossum60456fd1997-04-09 17:36:32 +00001978 PyErr_Clear();
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001979
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001980 UNLESS(module=PyImport_Import(py_module_name)) return NULL;
1981 global=PyObject_GetAttr(module, py_global_name);
1982 Py_DECREF(module);
1983 UNLESS(global) return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001984
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001985 if (PyDict_SetItem(class_map, t, global) < 0) global=NULL;
1986 Py_DECREF(t);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001987
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001988 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001989}
1990
1991
1992static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00001993marker(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001994 if (self->num_marks < 1) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001995 PyErr_SetString(UnpicklingError, "could not find MARK");
1996 return -1;
1997 }
1998
1999 return self->marks[--self->num_marks];
2000}
2001
2002
2003static int
2004load_none(Unpicklerobject *self) {
2005 if (PyList_Append(self->stack, Py_None) < 0)
2006 return -1;
2007
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002008 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002009}
2010
2011
2012static int
2013load_int(Unpicklerobject *self) {
2014 PyObject *py_int = 0;
2015 char *endptr, *s;
2016 int len, res = -1;
2017 long l;
2018
2019 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossum725d9411997-08-20 23:38:57 +00002020 UNLESS(s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002021
2022 errno = 0;
2023 l = strtol(s, &endptr, 0);
2024
2025 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
2026 /* Hm, maybe we've got something long. Let's try reading
2027 it as a Python long object. */
2028 errno=0;
2029 UNLESS(py_int=PyLong_FromString(s,&endptr,0)) goto finally;
2030
2031 if ((*endptr != '\n') || (endptr[1] != '\0')) {
2032 PyErr_SetString(PyExc_ValueError,
2033 "could not convert string to int");
2034 goto finally;
2035 }
2036 }
2037 else {
2038 UNLESS(py_int = PyInt_FromLong(l)) goto finally;
2039 }
2040
2041 if (PyList_Append(self->stack, py_int) < 0) goto finally;
2042
2043 res = 0;
2044
2045finally:
2046 free(s);
2047 Py_XDECREF(py_int);
2048
2049 return res;
2050}
2051
2052
2053static long
2054calc_binint(char *s, int x) {
2055 unsigned char c;
2056 int i;
2057 long l;
2058
2059 for (i = 0, l = 0L; i < x; i++) {
2060 c = (unsigned char)s[i];
2061 l |= (long)c << (i * 8);
2062 }
2063
2064 return l;
2065}
2066
2067
2068static int
2069load_binintx(Unpicklerobject *self, char *s, int x) {
2070 PyObject *py_int = 0;
2071 long l;
2072
2073 l = calc_binint(s, x);
2074
2075 UNLESS(py_int = PyInt_FromLong(l))
2076 return -1;
2077
2078 if (PyList_Append(self->stack, py_int) < 0) {
2079 Py_DECREF(py_int);
2080 return -1;
2081 }
2082
2083 Py_DECREF(py_int);
2084
2085 return 0;
2086}
2087
2088
2089static int
2090load_binint(Unpicklerobject *self) {
2091 char *s;
2092
2093 if ((*self->read_func)(self, &s, 4) < 0)
2094 return -1;
2095
2096 return load_binintx(self, s, 4);
2097}
2098
2099
2100static int
2101load_binint1(Unpicklerobject *self) {
2102 char *s;
2103
2104 if ((*self->read_func)(self, &s, 1) < 0)
2105 return -1;
2106
2107 return load_binintx(self, s, 1);
2108}
2109
2110
2111static int
2112load_binint2(Unpicklerobject *self) {
2113 char *s;
2114
2115 if ((*self->read_func)(self, &s, 2) < 0)
2116 return -1;
2117
2118 return load_binintx(self, s, 2);
2119}
2120
2121static int
2122load_long(Unpicklerobject *self) {
2123 PyObject *l = 0;
2124 char *end, *s;
2125 int len, res = -1;
2126
Guido van Rossum60456fd1997-04-09 17:36:32 +00002127 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossum725d9411997-08-20 23:38:57 +00002128 UNLESS(s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002129
2130 UNLESS(l = PyLong_FromString(s, &end, 0))
2131 goto finally;
2132
2133 if (PyList_Append(self->stack, l) < 0)
2134 goto finally;
2135
2136 res = 0;
2137
2138finally:
2139 free(s);
2140 Py_XDECREF(l);
2141
2142 return res;
2143}
2144
2145
2146static int
2147load_float(Unpicklerobject *self) {
2148 PyObject *py_float = 0;
2149 char *endptr, *s;
2150 int len, res = -1;
2151 double d;
2152
2153 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossum725d9411997-08-20 23:38:57 +00002154 UNLESS(s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002155
2156 errno = 0;
2157 d = strtod(s, &endptr);
2158
2159 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
2160 PyErr_SetString(PyExc_ValueError,
2161 "could not convert string to float");
2162 goto finally;
2163 }
2164
2165 UNLESS(py_float = PyFloat_FromDouble(d))
2166 goto finally;
2167
2168 if (PyList_Append(self->stack, py_float) < 0)
2169 goto finally;
2170
2171 res = 0;
2172
2173finally:
2174 free(s);
2175 Py_XDECREF(py_float);
2176
2177 return res;
2178}
2179
Guido van Rossum60456fd1997-04-09 17:36:32 +00002180static int
2181load_binfloat(Unpicklerobject *self) {
2182 PyObject *py_float = 0;
2183 int s, e, res = -1;
2184 long fhi, flo;
2185 double x;
2186 char *p;
2187
2188 if ((*self->read_func)(self, &p, 8) < 0)
2189 return -1;
2190
2191 /* First byte */
2192 s = (*p>>7) & 1;
2193 e = (*p & 0x7F) << 4;
2194 p++;
2195
2196 /* Second byte */
2197 e |= (*p>>4) & 0xF;
2198 fhi = (*p & 0xF) << 24;
2199 p++;
2200
2201 /* Third byte */
2202 fhi |= (*p & 0xFF) << 16;
2203 p++;
2204
2205 /* Fourth byte */
2206 fhi |= (*p & 0xFF) << 8;
2207 p++;
2208
2209 /* Fifth byte */
2210 fhi |= *p & 0xFF;
2211 p++;
2212
2213 /* Sixth byte */
2214 flo = (*p & 0xFF) << 16;
2215 p++;
2216
2217 /* Seventh byte */
2218 flo |= (*p & 0xFF) << 8;
2219 p++;
2220
2221 /* Eighth byte */
2222 flo |= *p & 0xFF;
2223
2224 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
2225 x /= 268435456.0; /* 2**28 */
2226
2227 /* XXX This sadly ignores Inf/NaN */
2228 if (e == 0)
2229 e = -1022;
2230 else {
2231 x += 1.0;
2232 e -= 1023;
2233 }
2234 x = ldexp(x, e);
2235
2236 if (s)
2237 x = -x;
2238
2239 UNLESS(py_float = PyFloat_FromDouble(x))
2240 goto finally;
2241
2242 if (PyList_Append(self->stack, py_float) < 0)
2243 goto finally;
2244
2245 res = 0;
2246
2247finally:
2248 Py_XDECREF(py_float);
2249
2250 return res;
2251}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002252
2253static int
2254load_string(Unpicklerobject *self) {
2255 PyObject *str = 0;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002256 int len, res = -1, nslash;
2257 char *s, q, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002258
2259 static PyObject *eval_dict = 0;
2260
2261 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossum725d9411997-08-20 23:38:57 +00002262 UNLESS(s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002263
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002264 /* Check for unquoted quotes (evil strings) */
2265 q=*s;
2266 if(q != '"' && q != '\'') goto insecure;
2267 for(p=s+1, nslash=0; *p; p++)
2268 {
2269 if(*p==q && nslash%2==0) break;
2270 if(*p=='\\') nslash++;
2271 else nslash=0;
2272 }
2273 if(*p==q)
2274 {
2275 for(p++; *p; p++) if(*p > ' ') goto insecure;
2276 }
2277 else goto insecure;
2278 /********************************************/
2279
Guido van Rossum60456fd1997-04-09 17:36:32 +00002280 UNLESS(eval_dict)
2281 UNLESS(eval_dict = Py_BuildValue("{s{}}", "__builtins__"))
2282 goto finally;
2283
Guido van Rossumb05a5c71997-05-07 17:46:13 +00002284 UNLESS(str = PyRun_String(s, Py_eval_input, eval_dict, eval_dict))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002285 goto finally;
2286
2287 if (PyList_Append(self->stack, str) < 0)
2288 goto finally;
2289
2290 res = 0;
2291
2292finally:
2293 free(s);
2294 Py_XDECREF(str);
2295
2296 return res;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002297
2298insecure:
2299 free(s);
2300 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
2301 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002302}
2303
2304
2305static int
2306load_binstring(Unpicklerobject *self) {
2307 PyObject *py_string = 0;
2308 long l;
2309 int res = -1;
2310 char *s;
2311
2312 if ((*self->read_func)(self, &s, 4) < 0)
2313 goto finally;
2314
2315 l = calc_binint(s, 4);
2316
2317 if ((*self->read_func)(self, &s, l) < 0)
2318 goto finally;
2319
2320 UNLESS(py_string = PyString_FromStringAndSize(s, l))
2321 goto finally;
2322
2323 if (PyList_Append(self->stack, py_string) < 0)
2324 goto finally;
2325
2326 res = 0;
2327
2328finally:
2329 Py_XDECREF(py_string);
2330
2331 return res;
2332}
2333
2334
2335static int
2336load_short_binstring(Unpicklerobject *self) {
2337 PyObject *py_string = 0;
2338 unsigned char l;
2339 int res = -1;
2340 char *s;
2341
2342 if ((*self->read_func)(self, &s, 1) < 0)
2343 return -1;
2344
2345 l = (unsigned char)s[0];
2346
2347 if ((*self->read_func)(self, &s, l) < 0)
2348 goto finally;
2349
2350 UNLESS(py_string = PyString_FromStringAndSize(s, l))
2351 goto finally;
2352
2353 if (PyList_Append(self->stack, py_string) < 0)
2354 goto finally;
2355
2356 res = 0;
2357
2358finally:
2359 Py_XDECREF(py_string);
2360
2361 return res;
2362}
2363
2364
2365static int
2366load_tuple(Unpicklerobject *self) {
2367 PyObject *tup = 0, *slice = 0, *list = 0;
2368 int i, j, res = -1;
2369
2370 if ((i = marker(self)) < 0)
2371 goto finally;
2372
2373 if ((j = PyList_Size(self->stack)) < 0)
2374 goto finally;
2375
2376 UNLESS(slice = PyList_GetSlice(self->stack, i, j))
2377 goto finally;
2378
2379 UNLESS(tup = PySequence_Tuple(slice))
2380 goto finally;
2381
2382 UNLESS(list = PyList_New(1))
2383 goto finally;
2384
2385 Py_INCREF(tup);
2386 if (PyList_SetItem(list, 0, tup) < 0)
2387 goto finally;
2388
2389 if (PyList_SetSlice(self->stack, i, j, list) < 0)
2390 goto finally;
2391
2392 res = 0;
2393
2394finally:
2395 Py_XDECREF(tup);
2396 Py_XDECREF(list);
2397 Py_XDECREF(slice);
2398
2399 return res;
2400}
2401
2402static int
2403load_empty_tuple(Unpicklerobject *self) {
2404 PyObject *tup = 0;
2405 int res;
2406
2407 UNLESS(tup=PyTuple_New(0)) return -1;
2408 res=PyList_Append(self->stack, tup);
2409 Py_DECREF(tup);
2410 return res;
2411}
2412
2413static int
2414load_empty_list(Unpicklerobject *self) {
2415 PyObject *list = 0;
2416 int res;
2417
2418 UNLESS(list=PyList_New(0)) return -1;
2419 res=PyList_Append(self->stack, list);
2420 Py_DECREF(list);
2421 return res;
2422}
2423
2424static int
2425load_empty_dict(Unpicklerobject *self) {
2426 PyObject *dict = 0;
2427 int res;
2428
2429 UNLESS(dict=PyDict_New()) return -1;
2430 res=PyList_Append(self->stack, dict);
2431 Py_DECREF(dict);
2432 return res;
2433}
2434
2435
2436static int
2437load_list(Unpicklerobject *self) {
2438 PyObject *list = 0, *slice = 0;
2439 int i, j, l, res = -1;
2440
2441 if ((i = marker(self)) < 0)
2442 goto finally;
2443
2444 if ((j = PyList_Size(self->stack)) < 0)
2445 goto finally;
2446
2447 UNLESS(slice = PyList_GetSlice(self->stack, i, j))
2448 goto finally;
2449
2450 if((l=PyList_Size(slice)) < 0)
2451 goto finally;
2452
2453 if(l) {
2454 UNLESS(list = PyList_New(1))
2455 goto finally;
2456
2457 Py_INCREF(slice);
2458 if (PyList_SetItem(list, 0, slice) < 0)
2459 goto finally;
2460
2461 if (PyList_SetSlice(self->stack, i, j, list) < 0)
2462 goto finally;
2463 } else {
2464 if(PyList_Append(self->stack,slice) < 0)
2465 goto finally;
2466 }
2467
2468 res = 0;
2469
2470finally:
2471 Py_XDECREF(list);
2472 Py_XDECREF(slice);
2473
2474 return res;
2475}
2476
2477static int
2478load_dict(Unpicklerobject *self) {
2479 PyObject *list = 0, *dict = 0, *key = 0, *value = 0;
2480 int i, j, k, res = -1;
2481
2482 if ((i = marker(self)) < 0)
2483 goto finally;
2484
2485 if ((j = PyList_Size(self->stack)) < 0)
2486 goto finally;
2487
2488 UNLESS(dict = PyDict_New())
2489 goto finally;
2490
2491 for (k = i; k < j; k += 2) {
2492 UNLESS(key = PyList_GET_ITEM((PyListObject *)self->stack, k))
2493 goto finally;
2494
2495 UNLESS(value = PyList_GET_ITEM((PyListObject *)self->stack, k + 1))
2496 goto finally;
2497
2498 if (PyDict_SetItem(dict, key, value) < 0)
2499 goto finally;
2500 }
2501
2502 if(j) {
2503
2504 UNLESS(list = PyList_New(1))
2505 goto finally;
2506
2507 Py_INCREF(dict);
2508 if (PyList_SetItem(list, 0, dict) < 0)
2509 goto finally;
2510
2511 if (PyList_SetSlice(self->stack, i, j, list) < 0)
2512 goto finally;
2513 }
2514 else
2515 if(PyList_Append(self->stack, dict) < 0)
2516 goto finally;
2517
2518 res = 0;
2519
2520finally:
2521 Py_XDECREF(dict);
2522 Py_XDECREF(list);
2523
2524 return res;
2525}
2526
2527static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002528Instance_New(PyObject *cls, PyObject *args) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002529 int has_key;
2530 PyObject *safe=0, *r=0;
2531
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002532 if (PyClass_Check(cls)) {
2533 int l;
2534
2535 if((l=PyObject_Length(args)) < 0) goto err;
2536 UNLESS(l) {
2537 PyObject *__getinitargs__;
2538
2539 UNLESS(__getinitargs__=PyObject_GetAttr(cls, __getinitargs___str)) {
2540 /* We have a class with no __getinitargs__, so bypass usual
2541 construction */
2542 PyInstanceObject *inst;
2543
2544 PyErr_Clear();
2545 UNLESS(inst=PyObject_NEW(PyInstanceObject, &PyInstance_Type))
2546 goto err;
2547 inst->in_class=(PyClassObject*)cls;
2548 Py_INCREF(cls);
2549 UNLESS(inst->in_dict=PyDict_New()) {
2550 Py_DECREF(inst);
2551 goto err;
2552 }
2553
2554 return (PyObject *)inst;
2555 }
2556 Py_DECREF(__getinitargs__);
2557 }
2558
2559 if((r=PyInstance_New(cls, args, NULL))) return r;
2560 else goto err;
2561 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002562
2563
2564 if ((has_key = cPickle_PyMapping_HasKey(safe_constructors, cls)) < 0)
2565 goto err;
2566
2567 if (!has_key)
2568 if(!(safe = PyObject_GetAttr(cls, __safe_for_unpickling___str)) ||
2569 !PyObject_IsTrue(safe)) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00002570 cPickle_ErrFormat(UnpicklingError, "%s is not safe for unpickling", "O", cls);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002571 Py_XDECREF(safe);
2572 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002573 }
2574
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002575 if(args==Py_None)
2576 {
2577 /* Special case, call cls.__basicnew__() */
2578 PyObject *basicnew;
2579
2580 UNLESS(basicnew=PyObject_GetAttr(cls, __basicnew___str)) return NULL;
2581 r=PyObject_CallObject(basicnew, NULL);
2582 Py_DECREF(basicnew);
2583 if(r) return r;
2584 }
2585
Guido van Rossum142eeb81997-08-13 03:14:41 +00002586 if((r=PyObject_CallObject(cls, args))) return r;
2587
Guido van Rossum60456fd1997-04-09 17:36:32 +00002588err:
2589 {
2590 PyObject *tp, *v, *tb;
2591
2592 PyErr_Fetch(&tp, &v, &tb);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002593 if((r=Py_BuildValue("OOO",v,cls,args))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002594 Py_XDECREF(v);
2595 v=r;
2596 }
2597 PyErr_Restore(tp,v,tb);
2598 }
2599 return NULL;
2600}
2601
2602
2603static int
2604load_obj(Unpicklerobject *self) {
2605 PyObject *class = 0, *slice = 0, *tup = 0, *obj = 0;
2606 int i, len, res = -1;
2607
2608 if ((i = marker(self)) < 0)
2609 goto finally;
2610
Guido van Rossum60456fd1997-04-09 17:36:32 +00002611 if ((len = PyList_Size(self->stack)) < 0)
2612 goto finally;
2613
2614 UNLESS(slice = PyList_GetSlice(self->stack, i + 1, len))
2615 goto finally;
2616
2617 UNLESS(tup = PySequence_Tuple(slice))
2618 goto finally;
2619
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002620 class = PyList_GET_ITEM((PyListObject *)self->stack, i);
2621 Py_INCREF(class);
2622
Guido van Rossum60456fd1997-04-09 17:36:32 +00002623 UNLESS(obj = Instance_New(class, tup))
2624 goto finally;
2625
2626 if (DEL_LIST_SLICE(self->stack, i, len) < 0)
2627 goto finally;
2628
2629 if (PyList_Append(self->stack, obj) < 0)
2630 goto finally;
2631
2632 res = 0;
2633
2634finally:
2635
2636 Py_XDECREF(class);
2637 Py_XDECREF(slice);
2638 Py_XDECREF(tup);
2639 Py_XDECREF(obj);
2640
2641 return res;
2642}
2643
2644
2645static int
2646load_inst(Unpicklerobject *self) {
2647 PyObject *arg_tup = 0, *arg_slice = 0, *class = 0, *obj = 0,
2648 *module_name = 0, *class_name = 0;
2649 int i, j, len, res = -1;
2650 char *s;
2651
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002652 if ((i = marker(self)) < 0) goto finally;
2653
2654 if ((j = PyList_Size(self->stack)) < 0) goto finally;
2655
2656 UNLESS(arg_slice = PyList_GetSlice(self->stack, i, j)) goto finally;
2657
2658 UNLESS(arg_tup = PySequence_Tuple(arg_slice)) goto finally;
2659
2660 if (DEL_LIST_SLICE(self->stack, i, j) < 0) goto finally;
2661
2662 if ((len = (*self->readline_func)(self, &s)) < 0) goto finally;
2663
2664 UNLESS(module_name = PyString_FromStringAndSize(s, len - 1)) goto finally;
2665
2666 if ((len = (*self->readline_func)(self, &s)) < 0) goto finally;
2667
2668 UNLESS(class_name = PyString_FromStringAndSize(s, len - 1)) goto finally;
2669
2670 UNLESS(class = find_class(self->class_map, module_name, class_name))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002671 goto finally;
2672
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002673 UNLESS(obj = Instance_New(class, arg_tup)) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002674
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002675 if (PyList_Append(self->stack, obj) < 0) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002676
2677 res = 0;
2678
2679finally:
2680 Py_XDECREF(class);
2681 Py_XDECREF(arg_slice);
2682 Py_XDECREF(arg_tup);
2683 Py_XDECREF(obj);
2684 Py_XDECREF(module_name);
2685 Py_XDECREF(class_name);
2686
2687 return res;
2688}
2689
2690
2691static int
2692load_global(Unpicklerobject *self) {
2693 PyObject *class = 0, *module_name = 0, *class_name = 0;
2694 int res = -1, len;
2695 char *s;
2696
2697 if ((len = (*self->readline_func)(self, &s)) < 0)
2698 goto finally;
2699
2700 UNLESS(module_name = PyString_FromStringAndSize(s, len - 1))
2701 goto finally;
2702
2703 if ((len = (*self->readline_func)(self, &s)) < 0)
2704 goto finally;
2705
2706 UNLESS(class_name = PyString_FromStringAndSize(s, len - 1))
2707 goto finally;
2708
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002709 UNLESS(class = find_class(self->class_map, module_name, class_name))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002710 goto finally;
2711
2712 if (PyList_Append(self->stack, class) < 0)
2713 goto finally;
2714
2715 res = 0;
2716
2717finally:
2718 Py_XDECREF(class);
2719 Py_XDECREF(module_name);
2720 Py_XDECREF(class_name);
2721
2722 return res;
2723}
2724
2725
2726static int
2727load_persid(Unpicklerobject *self) {
2728 PyObject *pid = 0, *pers_load_val = 0;
2729 int len, res = -1;
2730 char *s;
2731
2732 if (self->pers_func) {
2733 if ((len = (*self->readline_func)(self, &s)) < 0)
2734 goto finally;
2735
2736 UNLESS(pid = PyString_FromStringAndSize(s, len - 1))
2737 goto finally;
2738
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002739 if(PyList_Check(self->pers_func)) {
2740 if(PyList_Append(self->pers_func, pid) < 0) goto finally;
2741 pers_load_val=pid;
2742 Py_INCREF(pid);
2743 }
2744 else {
2745 UNLESS(self->arg)
2746 UNLESS(self->arg = PyTuple_New(1))
2747 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002748
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002749 Py_INCREF(pid);
2750 if (PyTuple_SetItem(self->arg, 0, pid) < 0)
2751 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002752
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002753 UNLESS(pers_load_val =
2754 PyObject_CallObject(self->pers_func, self->arg))
2755 goto finally;
2756 }
2757 if (PyList_Append(self->stack, pers_load_val) < 0)
2758 goto finally;
2759 }
2760 else {
2761 PyErr_SetString(UnpicklingError,
2762 "A load persistent id instruction was encountered,\n"
2763 "but no persistent_load function was specified.");
2764 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002765 }
2766
2767 res = 0;
2768
2769finally:
2770 Py_XDECREF(pid);
2771 Py_XDECREF(pers_load_val);
2772
2773 return res;
2774}
2775
2776
2777static int
2778load_binpersid(Unpicklerobject *self) {
2779 PyObject *pid = 0, *pers_load_val = 0;
2780 int len, res = -1;
2781
2782 if (self->pers_func) {
2783 if ((len = PyList_Size(self->stack)) < 0)
2784 goto finally;
2785
2786 pid = PyList_GET_ITEM((PyListObject *)self->stack, len - 1);
2787 Py_INCREF(pid);
2788
2789 if (DEL_LIST_SLICE(self->stack, len - 1, len) < 0)
2790 goto finally;
2791
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002792 if(PyList_Check(self->pers_func)) {
2793 if(PyList_Append(self->pers_func, pid) < 0) goto finally;
2794 pers_load_val=pid;
2795 Py_INCREF(pid);
2796 }
2797 else {
2798 UNLESS(self->arg)
Guido van Rossum60456fd1997-04-09 17:36:32 +00002799 UNLESS(self->arg = PyTuple_New(1))
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002800 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002801
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002802 Py_INCREF(pid);
2803 if (PyTuple_SetItem(self->arg, 0, pid) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00002804 goto finally;
2805
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002806 UNLESS(pers_load_val =
2807 PyObject_CallObject(self->pers_func, self->arg))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002808 goto finally;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002809 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002810 if (PyList_Append(self->stack, pers_load_val) < 0)
2811 goto finally;
2812 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002813 else {
2814 PyErr_SetString(UnpicklingError,
2815 "A load persistent id instruction was encountered,\n"
2816 "but no persistent_load function was specified.");
2817 goto finally;
2818 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002819
2820 res = 0;
2821
2822finally:
2823 Py_XDECREF(pid);
2824 Py_XDECREF(pers_load_val);
2825
2826 return res;
2827}
2828
2829
2830static int
2831load_pop(Unpicklerobject *self) {
2832 int len;
2833
2834 if ((len = PyList_Size(self->stack)) < 0)
2835 return -1;
2836
2837 if ((self->num_marks > 0) &&
2838 (self->marks[self->num_marks - 1] == len))
2839 self->num_marks--;
2840 else if (DEL_LIST_SLICE(self->stack, len - 1, len) < 0)
2841 return -1;
2842
2843 return 0;
2844}
2845
2846
2847static int
2848load_pop_mark(Unpicklerobject *self) {
2849 int i, len;
2850
2851 if ((i = marker(self)) < 0)
2852 return -1;
2853
2854 if ((len = PyList_Size(self->stack)) < 0)
2855 return -1;
2856
2857 if (DEL_LIST_SLICE(self->stack, i, len) < 0)
2858 return -1;
2859
2860 return 0;
2861}
2862
2863
2864static int
2865load_dup(Unpicklerobject *self) {
2866 PyObject *last;
2867 int len;
2868
2869 if ((len = PyList_Size(self->stack)) < 0)
2870 return -1;
2871
2872 UNLESS(last = PyList_GetItem(self->stack, len - 1))
2873 return -1;
2874
2875 if (PyList_Append(self->stack, last) < 0)
2876 return -1;
2877
2878 return 0;
2879}
2880
2881
2882static int
2883load_get(Unpicklerobject *self) {
2884 PyObject *py_str = 0, *value = 0;
2885 int len, res = -1;
2886 char *s;
2887
2888 if ((len = (*self->readline_func)(self, &s)) < 0)
2889 goto finally;
2890
2891 UNLESS(py_str = PyString_FromStringAndSize(s, len - 1))
2892 goto finally;
2893
2894 UNLESS(value = PyDict_GetItem(self->memo, py_str))
2895 goto finally;
2896
2897 if (PyList_Append(self->stack, value) < 0)
2898 goto finally;
2899
2900 res = 0;
2901
2902finally:
2903 Py_XDECREF(py_str);
2904
2905 return res;
2906}
2907
2908
2909static int
2910load_binget(Unpicklerobject *self) {
2911 PyObject *py_key = 0, *value = 0;
2912 unsigned char key;
2913 int res = -1;
2914 char *s;
2915
2916 if ((*self->read_func)(self, &s, 1) < 0)
2917 goto finally;
2918
2919 key = (unsigned char)s[0];
2920
2921 UNLESS(py_key = PyInt_FromLong((long)key))
2922 goto finally;
2923
2924 UNLESS(value = PyDict_GetItem(self->memo, py_key))
2925 goto finally;
2926
2927 if (PyList_Append(self->stack, value) < 0)
2928 goto finally;
2929
2930 res = 0;
2931
2932finally:
2933 Py_XDECREF(py_key);
2934
2935 return res;
2936}
2937
2938
2939static int
2940load_long_binget(Unpicklerobject *self) {
2941 PyObject *py_key = 0, *value = 0;
2942 unsigned char c, *s;
2943 long key;
2944 int res = -1;
2945
2946 if ((*self->read_func)(self, &s, 4) < 0)
2947 goto finally;
2948
2949 c = (unsigned char)s[0];
2950 key = (long)c;
2951 c = (unsigned char)s[1];
2952 key |= (long)c << 8;
2953 c = (unsigned char)s[2];
2954 key |= (long)c << 16;
2955 c = (unsigned char)s[3];
2956 key |= (long)c << 24;
2957
2958 UNLESS(py_key = PyInt_FromLong(key))
2959 goto finally;
2960
2961 UNLESS(value = PyDict_GetItem(self->memo, py_key))
2962 goto finally;
2963
2964 if (PyList_Append(self->stack, value) < 0)
2965 goto finally;
2966
2967 res = 0;
2968
2969finally:
2970 Py_XDECREF(py_key);
2971
2972 return res;
2973}
2974
2975
2976static int
2977load_put(Unpicklerobject *self) {
2978 PyObject *py_str = 0, *value = 0;
2979 int len, res = -1;
2980 char *s;
2981
2982 if ((len = (*self->readline_func)(self, &s)) < 0)
2983 goto finally;
2984
2985 UNLESS(py_str = PyString_FromStringAndSize(s, len - 1))
2986 goto finally;
2987
2988 if ((len = PyList_Size(self->stack)) < 0)
2989 goto finally;
2990
2991 UNLESS(value = PyList_GetItem(self->stack, len - 1))
2992 goto finally;
2993
2994 if (PyDict_SetItem(self->memo, py_str, value) < 0)
2995 goto finally;
2996
2997 res = 0;
2998
2999finally:
3000 Py_XDECREF(py_str);
3001
3002 return res;
3003}
3004
3005
3006static int
3007load_binput(Unpicklerobject *self) {
3008 PyObject *py_key = 0, *value = 0;
3009 unsigned char key, *s;
3010 int len, res = -1;
3011
3012 if ((*self->read_func)(self, &s, 1) < 0)
3013 goto finally;
3014
3015 key = (unsigned char)s[0];
3016
3017 UNLESS(py_key = PyInt_FromLong((long)key))
3018 goto finally;
3019
3020 if ((len = PyList_Size(self->stack)) < 0)
3021 goto finally;
3022
3023 UNLESS(value = PyList_GetItem(self->stack, len - 1))
3024 goto finally;
3025
3026 if (PyDict_SetItem(self->memo, py_key, value) < 0)
3027 goto finally;
3028
3029 res = 0;
3030
3031finally:
3032 Py_XDECREF(py_key);
3033
3034 return res;
3035}
3036
3037
3038static int
3039load_long_binput(Unpicklerobject *self) {
3040 PyObject *py_key = 0, *value = 0;
3041 long key;
3042 unsigned char c, *s;
3043 int len, res = -1;
3044
3045 if ((*self->read_func)(self, &s, 4) < 0)
3046 goto finally;
3047
3048 c = (unsigned char)s[0];
3049 key = (long)c;
3050 c = (unsigned char)s[1];
3051 key |= (long)c << 8;
3052 c = (unsigned char)s[2];
3053 key |= (long)c << 16;
3054 c = (unsigned char)s[3];
3055 key |= (long)c << 24;
3056
3057 UNLESS(py_key = PyInt_FromLong(key))
3058 goto finally;
3059
3060 if ((len = PyList_Size(self->stack)) < 0)
3061 goto finally;
3062
3063 UNLESS(value = PyList_GetItem(self->stack, len - 1))
3064 goto finally;
3065
3066 if (PyDict_SetItem(self->memo, py_key, value) < 0)
3067 goto finally;
3068
3069 res = 0;
3070
3071finally:
3072 Py_XDECREF(py_key);
3073
3074 return res;
3075}
3076
3077
3078static int
3079do_append(Unpicklerobject *self, int x) {
3080 PyObject *value = 0, *list = 0, *append_method = 0;
3081 int len, i;
3082
3083 if ((len = PyList_Size(self->stack)) < 0)
3084 return -1;
3085
3086 UNLESS(list = PyList_GetItem(self->stack, x - 1))
3087 goto err;
3088
3089 if (PyList_Check(list)) {
3090 PyObject *slice = 0;
3091 int list_len;
3092
3093 UNLESS(slice = PyList_GetSlice(self->stack, x, len))
3094 return -1;
3095
3096 list_len = PyList_Size(list);
3097 if (PyList_SetSlice(list, list_len, list_len, slice) < 0) {
3098 Py_DECREF(slice);
3099 return -1;
3100 }
3101
3102 Py_DECREF(slice);
3103 }
3104 else {
3105
3106 UNLESS(append_method = PyObject_GetAttr(list, append_str))
3107 return -1;
3108
3109 for (i = x; i < len; i++) {
3110 PyObject *junk;
3111
3112 UNLESS(value = PyList_GetItem(self->stack, i))
3113 return -1;
3114
3115 UNLESS(self->arg)
3116 UNLESS(self->arg = PyTuple_New(1))
3117 goto err;
3118
3119 Py_INCREF(value);
3120 if (PyTuple_SetItem(self->arg, 0, value) < 0)
3121 goto err;
3122
3123 UNLESS(junk = PyObject_CallObject(append_method, self->arg))
3124 goto err;
3125 Py_DECREF(junk);
3126 }
3127 }
3128
3129 if (DEL_LIST_SLICE(self->stack, x, len) < 0)
3130 goto err;
3131
3132 Py_XDECREF(append_method);
3133
3134 return 0;
3135
3136err:
3137 Py_XDECREF(append_method);
3138
3139 return -1;
3140}
3141
3142
3143static int
3144load_append(Unpicklerobject *self) {
3145 return do_append(self, PyList_Size(self->stack) - 1);
3146}
3147
3148
3149static int
3150load_appends(Unpicklerobject *self) {
3151 return do_append(self, marker(self));
3152}
3153
3154
3155static int
3156do_setitems(Unpicklerobject *self, int x) {
3157 PyObject *value = 0, *key = 0, *dict = 0;
3158 int len, i, res = -1;
3159
3160 if ((len = PyList_Size(self->stack)) < 0)
3161 goto finally;
3162
3163 UNLESS(dict = PyList_GetItem(self->stack, x - 1))
3164 goto finally;
3165
3166 for (i = x; i < len; i += 2) {
3167 UNLESS(key = PyList_GetItem(self->stack, i))
3168 goto finally;
3169
3170 UNLESS(value = PyList_GetItem(self->stack, i + 1))
3171 goto finally;
3172
3173 if (PyObject_SetItem(dict, key, value) < 0)
3174 goto finally;
3175 }
3176
3177 if (DEL_LIST_SLICE(self->stack, x, len) < 0)
3178 goto finally;
3179
3180 res = 0;
3181
3182finally:
3183
3184 return res;
3185}
3186
3187
3188static int
3189load_setitem(Unpicklerobject *self) {
3190 return do_setitems(self, PyList_Size(self->stack) - 2);
3191}
3192
3193
3194static int
3195load_setitems(Unpicklerobject *self) {
3196 return do_setitems(self, marker(self));
3197}
3198
3199
3200static int
3201load_build(Unpicklerobject *self) {
3202 PyObject *value = 0, *inst = 0, *instdict = 0, *d_key = 0, *d_value = 0,
3203 *junk = 0, *__setstate__ = 0;
3204 int len, i, res = -1;
3205
3206 if ((len = PyList_Size(self->stack)) < 0)
3207 goto finally;
3208
3209 UNLESS(value = PyList_GetItem(self->stack, len - 1))
3210 goto finally;
3211 Py_INCREF(value);
3212
3213 if (DEL_LIST_SLICE(self->stack, len - 1, len) < 0)
3214 goto finally;
3215
3216 UNLESS(inst = PyList_GetItem(self->stack, len - 2))
3217 goto finally;
3218
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003219 UNLESS(__setstate__ = PyObject_GetAttr(inst, __setstate___str)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003220 PyErr_Clear();
3221
3222 UNLESS(instdict = PyObject_GetAttr(inst, __dict___str))
3223 goto finally;
3224
3225 i = 0;
3226 while (PyDict_Next(value, &i, &d_key, &d_value)) {
3227 if (PyObject_SetItem(instdict, d_key, d_value) < 0)
3228 goto finally;
3229 }
3230 }
3231 else {
3232 UNLESS(self->arg)
3233 UNLESS(self->arg = PyTuple_New(1))
3234 goto finally;
3235
3236 Py_INCREF(value);
3237 if (PyTuple_SetItem(self->arg, 0, value) < 0)
3238 goto finally;
3239
3240 UNLESS(junk = PyObject_CallObject(__setstate__, self->arg))
3241 goto finally;
3242 Py_DECREF(junk);
3243 }
3244
3245 res = 0;
3246
3247finally:
3248 Py_XDECREF(value);
3249 Py_XDECREF(instdict);
3250 Py_XDECREF(__setstate__);
3251
3252 return res;
3253}
3254
3255
3256static int
3257load_mark(Unpicklerobject *self) {
3258 int len;
3259
3260 if ((len = PyList_Size(self->stack)) < 0)
3261 return -1;
3262
3263 if (!self->marks_size) {
3264 self->marks_size = 20;
3265 UNLESS(self->marks = (int *)malloc(self->marks_size * sizeof(int))) {
3266 PyErr_NoMemory();
3267 return -1;
3268 }
3269 }
3270 else if ((self->num_marks + 1) >= self->marks_size) {
3271 UNLESS(self->marks = (int *)realloc(self->marks,
3272 (self->marks_size + 20) * sizeof(int))) {
3273 PyErr_NoMemory();
3274 return -1;
3275 }
3276
3277 self->marks_size += 20;
3278 }
3279
3280 self->marks[self->num_marks++] = len;
3281
3282 return 0;
3283}
3284
3285static int
3286load_reduce(Unpicklerobject *self) {
3287 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
3288 int len, res = -1;
3289
3290 if ((len = PyList_Size(self->stack)) < 0)
3291 goto finally;
3292
3293 UNLESS(arg_tup = PyList_GetItem(self->stack, len - 1))
3294 goto finally;
3295
3296 UNLESS(callable = PyList_GetItem(self->stack, len - 2))
3297 goto finally;
3298
3299 UNLESS(ob = Instance_New(callable, arg_tup))
3300 goto finally;
3301
3302 if (PyList_Append(self->stack, ob) < 0)
3303 goto finally;
3304
3305 if (DEL_LIST_SLICE(self->stack, len - 2, len) < 0)
3306 goto finally;
3307
3308 res = 0;
3309
3310finally:
3311 Py_XDECREF(ob);
3312
3313 return res;
3314}
3315
3316static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003317load(Unpicklerobject *self) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00003318 PyObject *stack = 0, *err = 0, *val = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003319 int len;
3320 char *s;
3321
3322 UNLESS(stack = PyList_New(0))
3323 goto err;
3324
3325 self->stack = stack;
3326 self->num_marks = 0;
3327
3328 while (1) {
3329 if ((*self->read_func)(self, &s, 1) < 0)
3330 break;
3331
3332 switch (s[0]) {
3333 case NONE:
3334 if (load_none(self) < 0)
3335 break;
3336 continue;
3337
3338 case BININT:
3339 if (load_binint(self) < 0)
3340 break;
3341 continue;
3342
3343 case BININT1:
3344 if (load_binint1(self) < 0)
3345 break;
3346 continue;
3347
3348 case BININT2:
3349 if (load_binint2(self) < 0)
3350 break;
3351 continue;
3352
3353 case INT:
3354 if (load_int(self) < 0)
3355 break;
3356 continue;
3357
3358 case LONG:
3359 if (load_long(self) < 0)
3360 break;
3361 continue;
3362
3363 case FLOAT:
3364 if (load_float(self) < 0)
3365 break;
3366 continue;
3367
3368#ifdef FORMAT_1_3
3369 case BINFLOAT:
3370 if (load_binfloat(self) < 0)
3371 break;
3372 continue;
3373#endif
3374
3375 case BINSTRING:
3376 if (load_binstring(self) < 0)
3377 break;
3378 continue;
3379
3380 case SHORT_BINSTRING:
3381 if (load_short_binstring(self) < 0)
3382 break;
3383 continue;
3384
3385 case STRING:
3386 if (load_string(self) < 0)
3387 break;
3388 continue;
3389
3390 case EMPTY_TUPLE:
3391 if (load_empty_tuple(self) < 0)
3392 break;
3393 continue;
3394
3395 case TUPLE:
3396 if (load_tuple(self) < 0)
3397 break;
3398 continue;
3399
3400 case EMPTY_LIST:
3401 if (load_empty_list(self) < 0)
3402 break;
3403 continue;
3404
3405 case LIST:
3406 if (load_list(self) < 0)
3407 break;
3408 continue;
3409
3410 case EMPTY_DICT:
3411 if (load_empty_dict(self) < 0)
3412 break;
3413 continue;
3414
3415 case DICT:
3416 if (load_dict(self) < 0)
3417 break;
3418 continue;
3419
3420 case OBJ:
3421 if (load_obj(self) < 0)
3422 break;
3423 continue;
3424
3425 case INST:
3426 if (load_inst(self) < 0)
3427 break;
3428 continue;
3429
3430 case GLOBAL:
3431 if (load_global(self) < 0)
3432 break;
3433 continue;
3434
3435 case APPEND:
3436 if (load_append(self) < 0)
3437 break;
3438 continue;
3439
3440 case APPENDS:
3441 if (load_appends(self) < 0)
3442 break;
3443 continue;
3444
3445 case BUILD:
3446 if (load_build(self) < 0)
3447 break;
3448 continue;
3449
3450 case DUP:
3451 if (load_dup(self) < 0)
3452 break;
3453 continue;
3454
3455 case BINGET:
3456 if (load_binget(self) < 0)
3457 break;
3458 continue;
3459
3460 case LONG_BINGET:
3461 if (load_long_binget(self) < 0)
3462 break;
3463 continue;
3464
3465 case GET:
3466 if (load_get(self) < 0)
3467 break;
3468 continue;
3469
3470 case MARK:
3471 if (load_mark(self) < 0)
3472 break;
3473 continue;
3474
3475 case BINPUT:
3476 if (load_binput(self) < 0)
3477 break;
3478 continue;
3479
3480 case LONG_BINPUT:
3481 if (load_long_binput(self) < 0)
3482 break;
3483 continue;
3484
3485 case PUT:
3486 if (load_put(self) < 0)
3487 break;
3488 continue;
3489
3490 case POP:
3491 if (load_pop(self) < 0)
3492 break;
3493 continue;
3494
3495 case POP_MARK:
3496 if (load_pop_mark(self) < 0)
3497 break;
3498 continue;
3499
3500 case SETITEM:
3501 if (load_setitem(self) < 0)
3502 break;
3503 continue;
3504
3505 case SETITEMS:
3506 if (load_setitems(self) < 0)
3507 break;
3508 continue;
3509
3510 case STOP:
3511 break;
3512
3513 case PERSID:
3514 if (load_persid(self) < 0)
3515 break;
3516 continue;
3517
3518 case BINPERSID:
3519 if (load_binpersid(self) < 0)
3520 break;
3521 continue;
3522
3523 case REDUCE:
3524 if (load_reduce(self) < 0)
3525 break;
3526 continue;
3527
3528 default:
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00003529 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossum60456fd1997-04-09 17:36:32 +00003530 "c", s[0]);
3531 goto err;
3532 }
3533
3534 break;
3535 }
3536
3537 if ((err = PyErr_Occurred()) == PyExc_EOFError) {
3538 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
3834 if ((err = PyErr_Occurred()) == PyExc_EOFError) {
3835 PyErr_SetNone(PyExc_EOFError);
3836 goto err;
3837 }
3838
3839 if (err) goto err;
3840
3841 if ((len = PyList_Size(stack)) < 0) goto err;
3842
3843 UNLESS(val = PyList_GetItem(stack, len - 1)) goto err;
3844 Py_INCREF(val);
3845
3846 Py_DECREF(stack);
3847
3848 self->stack=NULL;
3849 return val;
3850
3851err:
3852 self->stack=NULL;
3853 Py_XDECREF(stack);
3854
3855 return NULL;
3856}
3857
3858
Guido van Rossum60456fd1997-04-09 17:36:32 +00003859static PyObject *
3860Unpickler_load(Unpicklerobject *self, PyObject *args) {
3861 UNLESS(PyArg_ParseTuple(args, ""))
3862 return NULL;
3863
3864 return load(self);
3865}
3866
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003867static PyObject *
3868Unpickler_noload(Unpicklerobject *self, PyObject *args) {
3869 UNLESS(PyArg_ParseTuple(args, ""))
3870 return NULL;
3871
3872 return noload(self);
3873}
3874
Guido van Rossum60456fd1997-04-09 17:36:32 +00003875
3876static struct PyMethodDef Unpickler_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003877 {"load", (PyCFunction)Unpickler_load, 1,
3878 "load() -- Load a pickle"
3879 },
3880 {"noload", (PyCFunction)Unpickler_noload, 1,
3881 "noload() -- not load a pickle, but go through most of the motions\n"
3882 "\n"
3883 "This function can be used to read past a pickle without instantiating\n"
3884 "any objects or importing any modules. It can also be used to find all\n"
3885 "persistent references without instantiating any objects or importing\n"
3886 "any modules.\n"
3887 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00003888 {NULL, NULL} /* sentinel */
3889};
3890
3891
3892static Unpicklerobject *
3893newUnpicklerobject(PyObject *f) {
3894 Unpicklerobject *self;
3895
3896 UNLESS(self = PyObject_NEW(Unpicklerobject, &Unpicklertype))
3897 return NULL;
3898
3899 self->file = NULL;
3900 self->arg = NULL;
3901 self->stack = NULL;
3902 self->pers_func = NULL;
3903 self->last_string = NULL;
3904 self->marks = NULL;
3905 self->num_marks = 0;
3906 self->marks_size = 0;
3907 self->buf_size = 0;
3908 self->read = NULL;
3909 self->readline = NULL;
3910
3911 UNLESS(self->memo = PyDict_New()) {
3912 Py_XDECREF((PyObject *)self);
3913 return NULL;
3914 }
3915
3916 Py_INCREF(f);
3917 self->file = f;
3918
3919 /* Set read, readline based on type of f */
3920 if (PyFile_Check(f)) {
3921 self->fp = PyFile_AsFile(f);
3922 self->read_func = read_file;
3923 self->readline_func = readline_file;
3924 }
3925 else if (PycStringIO_InputCheck(f)) {
3926 self->fp = NULL;
3927 self->read_func = read_cStringIO;
3928 self->readline_func = readline_cStringIO;
3929 }
3930 else {
3931
3932 self->fp = NULL;
3933 self->read_func = read_other;
3934 self->readline_func = readline_other;
3935
3936 UNLESS((self->readline = PyObject_GetAttr(f, readline_str)) &&
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003937 (self->read = PyObject_GetAttr(f, read_str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003938 PyErr_Clear();
3939 PyErr_SetString( PyExc_TypeError, "argument must have 'read' and "
3940 "'readline' attributes" );
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003941 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003942 }
3943 }
3944
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003945 if(PyEval_GetRestricted()) {
3946 /* Restricted execution, get private tables */
3947 PyObject *m;
3948
3949 UNLESS(self->class_map=PyDict_New()) goto err;
3950 UNLESS(m=PyImport_Import(copy_reg_str)) goto err;
3951 self->safe_constructors=PyObject_GetAttr(m, safe_constructors_str);
3952 Py_DECREF(m);
3953 UNLESS(self->safe_constructors) goto err;
3954 }
3955 else {
3956 self->class_map=class_map;
3957 Py_INCREF(class_map);
3958 self->safe_constructors=safe_constructors;
3959 Py_INCREF(safe_constructors);
3960 }
3961
Guido van Rossum60456fd1997-04-09 17:36:32 +00003962 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003963
3964err:
3965 Py_DECREF((PyObject *)self);
3966 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003967}
3968
3969
3970static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00003971get_Unpickler(PyObject *self, PyObject *args) {
3972 PyObject *file;
3973
3974 UNLESS(PyArg_ParseTuple(args, "O", &file))
3975 return NULL;
3976 return (PyObject *)newUnpicklerobject(file);
3977}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003978
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003979
Guido van Rossum60456fd1997-04-09 17:36:32 +00003980static void
3981Unpickler_dealloc(Unpicklerobject *self) {
3982 Py_XDECREF(self->readline);
3983 Py_XDECREF(self->read);
3984 Py_XDECREF(self->file);
3985 Py_XDECREF(self->memo);
3986 Py_XDECREF(self->stack);
3987 Py_XDECREF(self->pers_func);
3988 Py_XDECREF(self->arg);
3989 Py_XDECREF(self->last_string);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003990 Py_XDECREF(self->class_map);
3991 Py_XDECREF(self->safe_constructors);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003992
Guido van Rossum60456fd1997-04-09 17:36:32 +00003993 if (self->marks) {
3994 free(self->marks);
3995 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003996
Guido van Rossum60456fd1997-04-09 17:36:32 +00003997 if (self->buf_size) {
3998 free(self->buf);
3999 }
4000
4001 PyMem_DEL(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004002}
4003
4004
4005static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004006Unpickler_getattr(Unpicklerobject *self, char *name) {
4007 if (!strcmp(name, "persistent_load")) {
4008 if (!self->pers_func) {
4009 PyErr_SetString(PyExc_AttributeError, name);
4010 return NULL;
4011 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004012
Guido van Rossum60456fd1997-04-09 17:36:32 +00004013 Py_INCREF(self->pers_func);
4014 return self->pers_func;
4015 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004016
Guido van Rossum60456fd1997-04-09 17:36:32 +00004017 if (!strcmp(name, "memo")) {
4018 if (!self->memo) {
4019 PyErr_SetString(PyExc_AttributeError, name);
4020 return NULL;
4021 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004022
Guido van Rossum60456fd1997-04-09 17:36:32 +00004023 Py_INCREF(self->memo);
4024 return self->memo;
4025 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004026
Guido van Rossum60456fd1997-04-09 17:36:32 +00004027 if (!strcmp(name, "stack")) {
4028 if (!self->stack) {
4029 PyErr_SetString(PyExc_AttributeError, name);
4030 return NULL;
4031 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004032
Guido van Rossum60456fd1997-04-09 17:36:32 +00004033 Py_INCREF(self->stack);
4034 return self->stack;
4035 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004036
Guido van Rossum60456fd1997-04-09 17:36:32 +00004037 if (!strcmp(name, "UnpicklingError")) {
4038 Py_INCREF(UnpicklingError);
4039 return UnpicklingError;
4040 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004041
Guido van Rossum60456fd1997-04-09 17:36:32 +00004042 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
4043}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004044
Guido van Rossum60456fd1997-04-09 17:36:32 +00004045
4046static int
4047Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) {
4048 if (!strcmp(name, "persistent_load")) {
4049 Py_XDECREF(self->pers_func);
4050 self->pers_func = value;
4051 Py_INCREF(value);
4052 return 0;
4053 }
4054
4055 PyErr_SetString(PyExc_AttributeError, name);
4056 return -1;
4057}
4058
4059
4060static PyObject *
4061cpm_dump(PyObject *self, PyObject *args) {
4062 PyObject *ob, *file, *res = NULL;
4063 Picklerobject *pickler = 0;
4064 int bin = 0;
4065
4066 UNLESS(PyArg_ParseTuple(args, "OO|i", &ob, &file, &bin))
4067 goto finally;
4068
4069 UNLESS(pickler = newPicklerobject(file, bin))
4070 goto finally;
4071
4072 if (dump(pickler, ob) < 0)
4073 goto finally;
4074
4075 Py_INCREF(Py_None);
4076 res = Py_None;
4077
4078finally:
4079 Py_XDECREF(pickler);
4080
4081 return res;
4082}
4083
4084
4085static PyObject *
4086cpm_dumps(PyObject *self, PyObject *args) {
4087 PyObject *ob, *file = 0, *res = NULL;
4088 Picklerobject *pickler = 0;
4089 int bin = 0;
4090
4091 UNLESS(PyArg_ParseTuple(args, "O|i", &ob, &bin))
4092 goto finally;
4093
4094 UNLESS(file = PycStringIO->NewOutput(128))
4095 goto finally;
4096
4097 UNLESS(pickler = newPicklerobject(file, bin))
4098 goto finally;
4099
4100 if (dump(pickler, ob) < 0)
4101 goto finally;
4102
4103 res = PycStringIO->cgetvalue(file);
4104
4105finally:
4106 Py_XDECREF(pickler);
4107 Py_XDECREF(file);
4108
4109 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004110}
4111
4112
4113static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004114cpm_load(PyObject *self, PyObject *args) {
4115 Unpicklerobject *unpickler = 0;
4116 PyObject *ob, *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004117
Guido van Rossum60456fd1997-04-09 17:36:32 +00004118 UNLESS(PyArg_ParseTuple(args, "O", &ob))
4119 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004120
Guido van Rossum60456fd1997-04-09 17:36:32 +00004121 UNLESS(unpickler = newUnpicklerobject(ob))
4122 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004123
Guido van Rossum60456fd1997-04-09 17:36:32 +00004124 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004125
Guido van Rossum60456fd1997-04-09 17:36:32 +00004126finally:
4127 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004128
Guido van Rossum60456fd1997-04-09 17:36:32 +00004129 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004130}
4131
4132
4133static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004134cpm_loads(PyObject *self, PyObject *args) {
4135 PyObject *ob, *file = 0, *res = NULL;
4136 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004137
Guido van Rossum60456fd1997-04-09 17:36:32 +00004138 UNLESS(PyArg_ParseTuple(args, "O", &ob))
4139 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004140
Guido van Rossum60456fd1997-04-09 17:36:32 +00004141 UNLESS(file = PycStringIO->NewInput(ob))
4142 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004143
Guido van Rossum60456fd1997-04-09 17:36:32 +00004144 UNLESS(unpickler = newUnpicklerobject(file))
4145 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004146
Guido van Rossum60456fd1997-04-09 17:36:32 +00004147 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004148
Guido van Rossum60456fd1997-04-09 17:36:32 +00004149finally:
4150 Py_XDECREF(file);
4151 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004152
Guido van Rossum60456fd1997-04-09 17:36:32 +00004153 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004154}
4155
4156
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004157static char Unpicklertype__doc__[] =
4158"Objects that know how to unpickle";
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004159
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004160static PyTypeObject Unpicklertype = {
4161 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004162 0, /*ob_size*/
4163 "Unpickler", /*tp_name*/
4164 sizeof(Unpicklerobject), /*tp_basicsize*/
4165 0, /*tp_itemsize*/
4166 /* methods */
4167 (destructor)Unpickler_dealloc, /*tp_dealloc*/
4168 (printfunc)0, /*tp_print*/
4169 (getattrfunc)Unpickler_getattr, /*tp_getattr*/
4170 (setattrfunc)Unpickler_setattr, /*tp_setattr*/
4171 (cmpfunc)0, /*tp_compare*/
4172 (reprfunc)0, /*tp_repr*/
4173 0, /*tp_as_number*/
4174 0, /*tp_as_sequence*/
4175 0, /*tp_as_mapping*/
4176 (hashfunc)0, /*tp_hash*/
4177 (ternaryfunc)0, /*tp_call*/
4178 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004179
Guido van Rossum60456fd1997-04-09 17:36:32 +00004180 /* Space for future expansion */
4181 0L,0L,0L,0L,
4182 Unpicklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004183};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004184
Guido van Rossum60456fd1997-04-09 17:36:32 +00004185static struct PyMethodDef cPickle_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004186 {"dump", (PyCFunction)cpm_dump, 1,
4187 "dump(object, file, [binary]) --"
4188 "Write an object in pickle format to the given file\n"
4189 "\n"
4190 "If the optional argument, binary, is provided and is true, then the\n"
4191 "pickle will be written in binary format, which is more space and\n"
4192 "computationally efficient. \n"
4193 },
4194 {"dumps", (PyCFunction)cpm_dumps, 1,
4195 "dumps(object, [binary]) --"
4196 "Return a string containing an object in pickle format\n"
4197 "\n"
4198 "If the optional argument, binary, is provided and is true, then the\n"
4199 "pickle will be written in binary format, which is more space and\n"
4200 "computationally efficient. \n"
4201 },
4202 {"load", (PyCFunction)cpm_load, 1,
4203 "load(file) -- Load a pickle from the given file"},
4204 {"loads", (PyCFunction)cpm_loads, 1,
4205 "loads(string) -- Load a pickle from the given string"},
4206 {"Pickler", (PyCFunction)get_Pickler, 1,
4207 "Pickler(file, [binary]) -- Create a pickler\n"
4208 "\n"
4209 "If the optional argument, binary, is provided and is true, then\n"
4210 "pickles will be written in binary format, which is more space and\n"
4211 "computationally efficient. \n"
4212 },
4213 {"Unpickler", (PyCFunction)get_Unpickler, 1,
4214 "Unpickler(file) -- Create an unpickler"},
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004215 { NULL, NULL }
4216};
4217
4218
Guido van Rossum60456fd1997-04-09 17:36:32 +00004219#define CHECK_FOR_ERRORS(MESS) \
4220if(PyErr_Occurred()) { \
4221 PyObject *__sys_exc_type, *__sys_exc_value, *__sys_exc_traceback; \
4222 PyErr_Fetch( &__sys_exc_type, &__sys_exc_value, &__sys_exc_traceback); \
4223 fprintf(stderr, # MESS ":\n\t"); \
4224 PyObject_Print(__sys_exc_type, stderr,0); \
4225 fprintf(stderr,", "); \
4226 PyObject_Print(__sys_exc_value, stderr,0); \
4227 fprintf(stderr,"\n"); \
4228 fflush(stderr); \
4229 Py_FatalError(# MESS); \
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004230}
4231
4232
Guido van Rossum60456fd1997-04-09 17:36:32 +00004233static int
4234init_stuff(PyObject *module, PyObject *module_dict) {
4235 PyObject *string, *copy_reg;
4236
4237#define INIT_STR(S) UNLESS(S ## _str=PyString_FromString(#S)) return -1;
4238
4239 INIT_STR(__class__);
4240 INIT_STR(__getinitargs__);
4241 INIT_STR(__dict__);
4242 INIT_STR(__getstate__);
4243 INIT_STR(__setstate__);
4244 INIT_STR(__name__);
Guido van Rossum142eeb81997-08-13 03:14:41 +00004245 INIT_STR(__main__);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004246 INIT_STR(__reduce__);
4247 INIT_STR(write);
4248 INIT_STR(__safe_for_unpickling__);
4249 INIT_STR(append);
4250 INIT_STR(read);
4251 INIT_STR(readline);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004252 INIT_STR(copy_reg);
4253 INIT_STR(dispatch_table);
4254 INIT_STR(safe_constructors);
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004255 INIT_STR(__basicnew__);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004256
4257 UNLESS(copy_reg = PyImport_ImportModule("copy_reg"))
4258 return -1;
4259
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004260 /* These next few are special because we want to use different
4261 ones in restricted mode. */
4262
4263 UNLESS(dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004264 return -1;
4265
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004266 UNLESS(safe_constructors = PyObject_GetAttr(copy_reg,
4267 safe_constructors_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004268 return -1;
4269
4270 Py_DECREF(copy_reg);
4271
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004272 UNLESS(class_map = PyDict_New()) return -1;
4273
4274 /* Down to here ********************************** */
4275
Guido van Rossum60456fd1997-04-09 17:36:32 +00004276 UNLESS(string = PyImport_ImportModule("string"))
4277 return -1;
4278
4279 UNLESS(atol_func = PyObject_GetAttrString(string, "atol"))
4280 return -1;
4281
4282 Py_DECREF(string);
4283
4284 UNLESS(empty_tuple = PyTuple_New(0))
4285 return -1;
4286
Guido van Rossum60456fd1997-04-09 17:36:32 +00004287 UNLESS(PicklingError = PyString_FromString("cPickle.PicklingError"))
4288 return -1;
4289
4290 if (PyDict_SetItemString(module_dict, "PicklingError",
4291 PicklingError) < 0)
4292 return -1;
4293
4294 UNLESS(UnpicklingError = PyString_FromString("cPickle.UnpicklingError"))
4295 return -1;
4296
4297 if (PyDict_SetItemString(module_dict, "UnpicklingError",
4298 UnpicklingError) < 0)
4299 return -1;
4300
4301 PycString_IMPORT;
4302
4303 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004304}
4305
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004306void
Guido van Rossum60456fd1997-04-09 17:36:32 +00004307initcPickle() {
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004308 PyObject *m, *d, *v;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004309 char *rev="1.48";
Guido van Rossum60456fd1997-04-09 17:36:32 +00004310 PyObject *format_version;
4311 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004312
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004313 Picklertype.ob_type = &PyType_Type;
4314 Unpicklertype.ob_type = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004315
Guido van Rossum60456fd1997-04-09 17:36:32 +00004316 /* Create the module and add the functions */
4317 m = Py_InitModule4("cPickle", cPickle_methods,
4318 cPickle_module_documentation,
4319 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004320
Guido van Rossum60456fd1997-04-09 17:36:32 +00004321 /* Add some symbolic constants to the module */
4322 d = PyModule_GetDict(m);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004323 PyDict_SetItemString(d,"__version__", v = PyString_FromString(rev));
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004324 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00004325
Guido van Rossum60456fd1997-04-09 17:36:32 +00004326#ifdef FORMAT_1_3
4327 format_version = PyString_FromString("1.3");
4328 compatible_formats = Py_BuildValue("[sss]", "1.0", "1.1", "1.2");
4329#else
4330 format_version = PyString_FromString("1.2");
4331 compatible_formats = Py_BuildValue("[ss]", "1.0", "1.1");
4332#endif
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004333
Guido van Rossum60456fd1997-04-09 17:36:32 +00004334 PyDict_SetItemString(d, "format_version", format_version);
4335 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004336 Py_XDECREF(format_version);
4337 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004338
Guido van Rossum60456fd1997-04-09 17:36:32 +00004339 init_stuff(m, d);
4340 CHECK_FOR_ERRORS("can't initialize module cPickle");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004341}