blob: f6e43eefbe587533a9f1c8b526dab64db32aa6a3 [file] [log] [blame]
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001/*
Guido van Rossume2d81cd1998-08-08 19:40:10 +00002 cPickle.c,v 1.53 1998/05/05 15:41:31 jim Exp
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003
4 Copyright
5
Guido van Rossume2d81cd1998-08-08 19:40:10 +00006 Copyright 1996, 1997, 1998 Digital Creations, Inc., 910
7 Princess Anne Street, Suite 300, Fredericksburg, Virginia 22401
8 U.S.A. All rights reserved. Copyright in this software is
9 owned by DCLC, unless otherwise indicated. Permission to use,
10 copy and distribute this software is hereby granted, provided
11 that the above copyright notice appear in all copies and that
12 both that copyright notice and this permission notice
13 appear. Note that any product, process or technology described
14 in this software may be the subject of other Intellectual
15 Property rights reserved by Digital Creations, L.C. and are not
16 licensed hereunder.
Guido van Rossum2f4caa41997-01-06 22:59:08 +000017
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 Rossume2d81cd1998-08-08 19:40:10 +000058"cPickle.c,v 1.53 1998/05/05 15:41:31 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 Rossum60456fd1997-04-09 17:36:32 +0000129static PyObject *empty_tuple;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000130
Guido van Rossum60456fd1997-04-09 17:36:32 +0000131static PyObject *__class___str, *__getinitargs___str, *__dict___str,
132 *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
133 *write_str, *__safe_for_unpickling___str, *append_str,
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000134 *read_str, *readline_str, *__main___str, *__basicnew___str,
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000135 *copy_reg_str, *dispatch_table_str, *safe_constructors_str;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000136
Guido van Rossum60456fd1997-04-09 17:36:32 +0000137static int save();
138static int put2();
139
140typedef struct {
141 PyObject_HEAD
142 FILE *fp;
143 PyObject *write;
144 PyObject *file;
145 PyObject *memo;
146 PyObject *arg;
147 PyObject *pers_func;
148 PyObject *inst_pers_func;
149 int bin;
150 int (*write_func)();
151 char *write_buf;
152 int buf_size;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000153 PyObject *dispatch_table;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000154} Picklerobject;
155
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000156staticforward PyTypeObject Picklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000157
Guido van Rossum60456fd1997-04-09 17:36:32 +0000158typedef struct {
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000159 PyObject_HEAD
160 FILE *fp;
161 PyObject *file;
162 PyObject *readline;
163 PyObject *read;
164 PyObject *memo;
165 PyObject *arg;
166 PyObject *stack;
167 PyObject *mark;
168 PyObject *pers_func;
169 PyObject *last_string;
170 int *marks;
171 int num_marks;
172 int marks_size;
173 int (*read_func)();
174 int (*readline_func)();
175 int buf_size;
176 char *buf;
177 PyObject *safe_constructors;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000178} Unpicklerobject;
179
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000180staticforward PyTypeObject Unpicklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000181
Guido van Rossum60456fd1997-04-09 17:36:32 +0000182int
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000183cPickle_PyMapping_HasKey(PyObject *o, PyObject *key) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000184 PyObject *v;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000185
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000186 if((v = PyObject_GetItem(o,key))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000187 Py_DECREF(v);
188 return 1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000189 }
190
Guido van Rossum60456fd1997-04-09 17:36:32 +0000191 PyErr_Clear();
192 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000193}
194
Guido van Rossumd385d591997-04-09 17:47:47 +0000195static
Guido van Rossum60456fd1997-04-09 17:36:32 +0000196PyObject *
197#ifdef HAVE_STDARG_PROTOTYPES
198/* VARARGS 2 */
Guido van Rossum57d9f2e1998-01-19 23:18:18 +0000199cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000200#else
201/* VARARGS */
Guido van Rossum57d9f2e1998-01-19 23:18:18 +0000202cPickle_ErrFormat(va_alist) va_dcl {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000203#endif
Guido van Rossum60456fd1997-04-09 17:36:32 +0000204 va_list va;
205 PyObject *args=0, *retval=0;
206#ifdef HAVE_STDARG_PROTOTYPES
207 va_start(va, format);
208#else
209 PyObject *ErrType;
210 char *stringformat, *format;
211 va_start(va);
212 ErrType = va_arg(va, PyObject *);
213 stringformat = va_arg(va, char *);
214 format = va_arg(va, char *);
215#endif
216
217 if(format) args = Py_VaBuildValue(format, va);
218 va_end(va);
219 if(format && ! args) return NULL;
220 if(stringformat && !(retval=PyString_FromString(stringformat))) return NULL;
221
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000222 if(retval) {
223 if(args) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000224 PyObject *v;
225 v=PyString_Format(retval, args);
226 Py_DECREF(retval);
227 Py_DECREF(args);
228 if(! v) return NULL;
229 retval=v;
230 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000231 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000232 else
233 if(args) retval=args;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000234 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000235 PyErr_SetObject(ErrType,Py_None);
236 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000237 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000238 PyErr_SetObject(ErrType,retval);
239 Py_DECREF(retval);
240 return NULL;
241}
242
243static int
244write_file(Picklerobject *self, char *s, int n) {
245 if (s == NULL) {
246 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000247 }
248
Guido van Rossum60456fd1997-04-09 17:36:32 +0000249 if ((int)fwrite(s, sizeof(char), n, self->fp) != n) {
250 PyErr_SetFromErrno(PyExc_IOError);
251 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000252 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000253
254 return n;
255}
256
257
258static int
259write_cStringIO(Picklerobject *self, char *s, int n) {
260 if (s == NULL) {
261 return 0;
262 }
263
264 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
265 return -1;
266 }
267
268 return n;
269}
270
271
272static int
Guido van Rossum142eeb81997-08-13 03:14:41 +0000273write_none(Picklerobject *self, char *s, int n) {
274 if (s == NULL) return 0;
275 return n;
276}
277
278
279static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000280write_other(Picklerobject *self, char *s, int n) {
281 PyObject *py_str = 0, *junk = 0;
282 int res = -1;
283
284 if (s == NULL) {
285 UNLESS(self->buf_size) return 0;
286 UNLESS(py_str =
287 PyString_FromStringAndSize(self->write_buf, self->buf_size))
288 goto finally;
289 }
290 else {
291 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
292 if (write_other(self, NULL, 0) < 0)
293 goto finally;
294 }
295
296 if (n > WRITE_BUF_SIZE) {
297 UNLESS(py_str =
298 PyString_FromStringAndSize(s, n))
299 goto finally;
300 }
301 else {
302 memcpy(self->write_buf + self->buf_size, s, n);
303 self->buf_size += n;
304 res = n;
305 goto finally;
306 }
307 }
308
309 UNLESS(self->arg)
310 UNLESS(self->arg = PyTuple_New(1))
311 goto finally;
312
313 Py_INCREF(py_str);
314 if (PyTuple_SetItem(self->arg, 0, py_str) < 0)
315 goto finally;
316
317 UNLESS(junk = PyObject_CallObject(self->write, self->arg))
318 goto finally;
319 Py_DECREF(junk);
320
321 self->buf_size = 0;
322
323 res = n;
324
325finally:
326 Py_XDECREF(py_str);
327
328 return res;
329}
330
331
332static int
333read_file(Unpicklerobject *self, char **s, int n) {
334
335 if (self->buf_size == 0) {
336 int size;
337
338 size = ((n < 32) ? 32 : n);
339 UNLESS(self->buf = (char *)malloc(size * sizeof(char))) {
340 PyErr_NoMemory();
341 return -1;
342 }
343
344 self->buf_size = size;
345 }
346 else if (n > self->buf_size) {
347 UNLESS(self->buf = (char *)realloc(self->buf, n * sizeof(char))) {
348 PyErr_NoMemory();
349 return -1;
350 }
351
352 self->buf_size = n;
353 }
354
355 if ((int)fread(self->buf, sizeof(char), n, self->fp) != n) {
356 if (feof(self->fp)) {
357 PyErr_SetNone(PyExc_EOFError);
358 return -1;
359 }
360
361 PyErr_SetFromErrno(PyExc_IOError);
362 return -1;
363 }
364
365 *s = self->buf;
366
367 return n;
368}
369
370
371static int
372readline_file(Unpicklerobject *self, char **s) {
373 int i;
374
375 if (self->buf_size == 0) {
376 UNLESS(self->buf = (char *)malloc(40 * sizeof(char))) {
377 PyErr_NoMemory();
378 return -1;
379 }
380
381 self->buf_size = 40;
382 }
383
384 i = 0;
385 while (1) {
386 for (; i < (self->buf_size - 1); i++) {
387 if (feof(self->fp) || (self->buf[i] = getc(self->fp)) == '\n') {
388 self->buf[i + 1] = '\0';
389 *s = self->buf;
390 return i + 1;
391 }
392 }
393
394 UNLESS(self->buf = (char *)realloc(self->buf,
395 (self->buf_size * 2) * sizeof(char))) {
396 PyErr_NoMemory();
397 return -1;
398 }
399
400 self->buf_size *= 2;
401 }
402
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000403}
404
405
406static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000407read_cStringIO(Unpicklerobject *self, char **s, int n) {
408 char *ptr;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000409
Guido van Rossum60456fd1997-04-09 17:36:32 +0000410 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
411 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000412 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000413 }
414
Guido van Rossum60456fd1997-04-09 17:36:32 +0000415 *s = ptr;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000416
Guido van Rossum60456fd1997-04-09 17:36:32 +0000417 return n;
418}
419
420
421static int
422readline_cStringIO(Unpicklerobject *self, char **s) {
423 int n;
424 char *ptr;
425
426 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
427 return -1;
428 }
429
430 *s = ptr;
431
432 return n;
433}
434
435
436static int
437read_other(Unpicklerobject *self, char **s, int n) {
438 PyObject *bytes, *str;
439 int res = -1;
440
441 UNLESS(bytes = PyInt_FromLong(n)) {
442 if (!PyErr_Occurred())
443 PyErr_SetNone(PyExc_EOFError);
444
445 goto finally;
446 }
447
448 UNLESS(self->arg)
449 UNLESS(self->arg = PyTuple_New(1))
450 goto finally;
451
452 Py_INCREF(bytes);
453 if (PyTuple_SetItem(self->arg, 0, bytes) < 0)
454 goto finally;
455
456 UNLESS(str = PyObject_CallObject(self->read, self->arg))
457 goto finally;
458
459 Py_XDECREF(self->last_string);
460 self->last_string = str;
461
462 *s = PyString_AsString(str);
463
464 res = n;
465
466finally:
467 Py_XDECREF(bytes);
468
469 return res;
470}
471
472
473static int
474readline_other(Unpicklerobject *self, char **s) {
475 PyObject *str;
476 int str_size;
477
478 UNLESS(str = PyObject_CallObject(self->readline, empty_tuple)) {
479 return -1;
480 }
481
482 str_size = PyString_Size(str);
483
484 Py_XDECREF(self->last_string);
485 self->last_string = str;
486
487 *s = PyString_AsString(str);
488
489 return str_size;
490}
491
492
493static char *
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000494pystrndup(char *s, int l) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000495 char *r;
496 UNLESS(r=malloc((l+1)*sizeof(char))) return (char*)PyErr_NoMemory();
497 memcpy(r,s,l);
498 r[l]=0;
499 return r;
500}
501
502
503static int
504get(Picklerobject *self, PyObject *id) {
505 PyObject *value = 0;
506 long c_value;
507 char s[30];
508 int len;
509
510 UNLESS(value = PyDict_GetItem(self->memo, id))
511 return -1;
512
513 UNLESS(value = PyTuple_GetItem(value, 0))
514 return -1;
515
516 c_value = PyInt_AsLong(value);
517
518 if (!self->bin) {
519 s[0] = GET;
520 sprintf(s + 1, "%ld\n", c_value);
521 len = strlen(s);
522 }
523 else {
524 if (c_value < 256) {
525 s[0] = BINGET;
526 s[1] = (int)(c_value & 0xff);
527 len = 2;
528 }
529 else {
530 s[0] = LONG_BINGET;
531 s[1] = (int)(c_value & 0xff);
532 s[2] = (int)((c_value >> 8) & 0xff);
533 s[3] = (int)((c_value >> 16) & 0xff);
534 s[4] = (int)((c_value >> 24) & 0xff);
535 len = 5;
536 }
537 }
538
539 if ((*self->write_func)(self, s, len) < 0)
540 return -1;
541
542 return 0;
543}
544
545
546static int
547put(Picklerobject *self, PyObject *ob) {
548 if (ob->ob_refcnt < 2)
549 return 0;
550
551 return put2(self, ob);
552}
553
554
555static int
556put2(Picklerobject *self, PyObject *ob) {
557 char c_str[30];
558 int p, len, res = -1;
559 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
560 if ((p = PyDict_Size(self->memo)) < 0)
561 goto finally;
562
563 if (!self->bin) {
564 c_str[0] = PUT;
565 sprintf(c_str + 1, "%d\n", p);
566 len = strlen(c_str);
567 }
568 else {
569 if (p >= 256) {
570 c_str[0] = LONG_BINPUT;
571 c_str[1] = (int)(p & 0xff);
572 c_str[2] = (int)((p >> 8) & 0xff);
573 c_str[3] = (int)((p >> 16) & 0xff);
574 c_str[4] = (int)((p >> 24) & 0xff);
575 len = 5;
576 }
577 else {
578 c_str[0] = BINPUT;
579 c_str[1] = p;
580 len = 2;
581 }
582 }
583
584 if ((*self->write_func)(self, c_str, len) < 0)
585 goto finally;
586
587 UNLESS(py_ob_id = PyInt_FromLong((long)ob))
588 goto finally;
589
590 UNLESS(memo_len = PyInt_FromLong(p))
591 goto finally;
592
593 UNLESS(t = PyTuple_New(2))
594 goto finally;
595
596 PyTuple_SET_ITEM(t, 0, memo_len);
597 Py_INCREF(memo_len);
598 PyTuple_SET_ITEM(t, 1, ob);
599 Py_INCREF(ob);
600
601 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
602 goto finally;
603
604 res = 0;
605
606finally:
607 Py_XDECREF(py_ob_id);
608 Py_XDECREF(memo_len);
609 Py_XDECREF(t);
610
611 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000612}
613
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000614#define PyImport_Import cPickle_Import
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000615
616static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000617PyImport_Import(PyObject *module_name) {
618 static PyObject *silly_list=0, *__builtins___str=0, *__import___str;
619 static PyObject *standard_builtins=0;
620 PyObject *globals=0, *__import__=0, *__builtins__=0, *r=0;
621
622 UNLESS(silly_list) {
623 UNLESS(__import___str=PyString_FromString("__import__")) return NULL;
624 UNLESS(__builtins___str=PyString_FromString("__builtins__")) return NULL;
625 UNLESS(silly_list=Py_BuildValue("[s]","__doc__")) return NULL;
626 }
627
628 if((globals=PyEval_GetGlobals())) {
629 Py_INCREF(globals);
630 UNLESS(__builtins__=PyObject_GetItem(globals,__builtins___str)) goto err;
631 }
632 else {
633 PyErr_Clear();
634
635 UNLESS(standard_builtins ||
636 (standard_builtins=PyImport_ImportModule("__builtin__")))
637 return NULL;
638
639 __builtins__=standard_builtins;
640 Py_INCREF(__builtins__);
641 UNLESS(globals = Py_BuildValue("{sO}", "__builtins__", __builtins__))
642 goto err;
643 }
644
645 if(PyDict_Check(__builtins__)) {
646 UNLESS(__import__=PyObject_GetItem(__builtins__,__import___str)) goto err;
647 }
648 else {
649 UNLESS(__import__=PyObject_GetAttr(__builtins__,__import___str)) goto err;
650 }
651
652 UNLESS(r=PyObject_CallFunction(__import__,"OOOO",
653 module_name, globals, globals, silly_list))
654 goto err;
655
656 Py_DECREF(globals);
657 Py_DECREF(__builtins__);
658 Py_DECREF(__import__);
659
660 return r;
661err:
662 Py_XDECREF(globals);
663 Py_XDECREF(__builtins__);
664 Py_XDECREF(__import__);
665 return NULL;
666}
667
668static PyObject *
Guido van Rossume2d81cd1998-08-08 19:40:10 +0000669whichmodule(PyObject *global, PyObject *global_name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000670 int i, j;
671 PyObject *module = 0, *modules_dict = 0,
672 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000673
Guido van Rossum45188231997-09-28 05:38:51 +0000674 module = PyObject_GetAttrString(global, "__module__");
675 if (module) return module;
676 PyErr_Clear();
677
Guido van Rossumed33a3f1998-05-14 02:34:46 +0000678 UNLESS(modules_dict = PySys_GetObject("modules")) {
679 PyErr_SetString(PyExc_SystemError, "lost sys.modules");
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000680 return NULL;
Guido van Rossumed33a3f1998-05-14 02:34:46 +0000681 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000682
Guido van Rossum60456fd1997-04-09 17:36:32 +0000683 i = 0;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000684 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
685
686 if(PyObject_Compare(name, __main___str)==0) continue;
687
Guido van Rossum60456fd1997-04-09 17:36:32 +0000688 UNLESS(global_name_attr = PyObject_GetAttr(module, global_name)) {
689 PyErr_Clear();
690 continue;
691 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000692
Guido van Rossum60456fd1997-04-09 17:36:32 +0000693 if (global_name_attr != global) {
694 Py_DECREF(global_name_attr);
695 continue;
696 }
697
698 Py_DECREF(global_name_attr);
699
700 break;
701 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000702
703 /* The following implements the rule in pickle.py added in 1.5
704 that used __main__ if no module is found. I don't actually
705 like this rule. jlf
706 */
707 if(!j) {
708 j=1;
709 name=__main___str;
710 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000711
712 Py_INCREF(name);
713 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000714}
715
716
Guido van Rossum60456fd1997-04-09 17:36:32 +0000717static int
718save_none(Picklerobject *self, PyObject *args) {
719 static char none = NONE;
720 if ((*self->write_func)(self, &none, 1) < 0)
721 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000722
Guido van Rossum60456fd1997-04-09 17:36:32 +0000723 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000724}
725
726
Guido van Rossum60456fd1997-04-09 17:36:32 +0000727static int
728save_int(Picklerobject *self, PyObject *args) {
729 char c_str[32];
730 long l = PyInt_AS_LONG((PyIntObject *)args);
731 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000732
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000733 if (!self->bin
734#if SIZEOF_LONG > 4
735 || (l >> 32)
736#endif
737 ) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000738 /* Save extra-long ints in non-binary mode, so that
739 we can use python long parsing code to restore,
740 if necessary. */
741 c_str[0] = INT;
742 sprintf(c_str + 1, "%ld\n", l);
743 if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
744 return -1;
745 }
746 else {
747 c_str[1] = (int)( l & 0xff);
748 c_str[2] = (int)((l >> 8) & 0xff);
749 c_str[3] = (int)((l >> 16) & 0xff);
750 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000751
Guido van Rossum60456fd1997-04-09 17:36:32 +0000752 if ((c_str[4] == 0) && (c_str[3] == 0)) {
753 if (c_str[2] == 0) {
754 c_str[0] = BININT1;
755 len = 2;
756 }
757 else {
758 c_str[0] = BININT2;
759 len = 3;
760 }
761 }
762 else {
763 c_str[0] = BININT;
764 len = 5;
765 }
766
767 if ((*self->write_func)(self, c_str, len) < 0)
768 return -1;
769 }
770
771 return 0;
772}
773
774
775static int
776save_long(Picklerobject *self, PyObject *args) {
777 int size, res = -1;
778 PyObject *repr = 0;
779
780 static char l = LONG;
781
782 UNLESS(repr = PyObject_Repr(args))
783 goto finally;
784
785 if ((size = PyString_Size(repr)) < 0)
786 goto finally;
787
788 if ((*self->write_func)(self, &l, 1) < 0)
789 goto finally;
790
791 if ((*self->write_func)(self,
792 PyString_AS_STRING((PyStringObject *)repr), size) < 0)
793 goto finally;
794
795 if ((*self->write_func)(self, "\n", 1) < 0)
796 goto finally;
797
798 res = 0;
799
800finally:
801 Py_XDECREF(repr);
802
803 return res;
804}
805
806
807static int
808save_float(Picklerobject *self, PyObject *args) {
809 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
810
811#ifdef FORMAT_1_3
812 if (self->bin) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000813 int s, e;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000814 double f;
815 long fhi, flo;
816 char str[9], *p = str;
817
818 *p = BINFLOAT;
819 p++;
820
821 if (x < 0) {
822 s = 1;
823 x = -x;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000824 }
825 else
Guido van Rossum60456fd1997-04-09 17:36:32 +0000826 s = 0;
827
828 f = frexp(x, &e);
829
830 /* Normalize f to be in the range [1.0, 2.0) */
831 if (0.5 <= f && f < 1.0) {
832 f *= 2.0;
833 e--;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000834 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000835 else if (f == 0.0) {
836 e = 0;
837 }
838 else {
839 PyErr_SetString(PyExc_SystemError,
840 "frexp() result out of range");
841 return -1;
842 }
843
844 if (e >= 1024) {
845 /* XXX 1024 itself is reserved for Inf/NaN */
846 PyErr_SetString(PyExc_OverflowError,
847 "float too large to pack with d format");
848 return -1;
849 }
850 else if (e < -1022) {
851 /* Gradual underflow */
852 f = ldexp(f, 1022 + e);
853 e = 0;
854 }
855 else {
856 e += 1023;
857 f -= 1.0; /* Get rid of leading 1 */
858 }
859
860 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
861 f *= 268435456.0; /* 2**28 */
862 fhi = (long) floor(f); /* Truncate */
863 f -= (double)fhi;
864 f *= 16777216.0; /* 2**24 */
865 flo = (long) floor(f + 0.5); /* Round */
866
867 /* First byte */
868 *p = (s<<7) | (e>>4);
869 p++;
870
871 /* Second byte */
872 *p = ((e&0xF)<<4) | (fhi>>24);
873 p++;
874
875 /* Third byte */
876 *p = (fhi>>16) & 0xFF;
877 p++;
878
879 /* Fourth byte */
880 *p = (fhi>>8) & 0xFF;
881 p++;
882
883 /* Fifth byte */
884 *p = fhi & 0xFF;
885 p++;
886
887 /* Sixth byte */
888 *p = (flo>>16) & 0xFF;
889 p++;
890
891 /* Seventh byte */
892 *p = (flo>>8) & 0xFF;
893 p++;
894
895 /* Eighth byte */
896 *p = flo & 0xFF;
897
898 if ((*self->write_func)(self, str, 9) < 0)
899 return -1;
900 }
901 else
902#endif
903 {
904 char c_str[250];
905 c_str[0] = FLOAT;
Guido van Rossum104be4a1998-04-03 21:13:02 +0000906 sprintf(c_str + 1, "%.17g\n", x);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000907
908 if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
909 return -1;
910 }
911
912 return 0;
913}
914
915
916static int
Guido van Rossum142eeb81997-08-13 03:14:41 +0000917save_string(Picklerobject *self, PyObject *args, int doput) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000918 int size, len;
919
920 size = PyString_Size(args);
921
922 if (!self->bin) {
923 PyObject *repr;
924 char *repr_str;
925
926 static char string = STRING;
927
928 UNLESS(repr = PyObject_Repr(args))
929 return -1;
930
931 repr_str = PyString_AS_STRING((PyStringObject *)repr);
932 len = PyString_Size(repr);
933
934 if ((*self->write_func)(self, &string, 1) < 0)
935 return -1;
936
937 if ((*self->write_func)(self, repr_str, len) < 0)
938 return -1;
939
940 if ((*self->write_func)(self, "\n", 1) < 0)
941 return -1;
942
943 Py_XDECREF(repr);
944 }
945 else {
946 int i;
947 char c_str[5];
948
949 size = PyString_Size(args);
950
951 if (size < 256) {
952 c_str[0] = SHORT_BINSTRING;
953 c_str[1] = size;
954 len = 2;
955 }
956 else {
957 c_str[0] = BINSTRING;
958 for (i = 1; i < 5; i++)
959 c_str[i] = (int)(size >> ((i - 1) * 8));
960 len = 5;
961 }
962
963 if ((*self->write_func)(self, c_str, len) < 0)
964 return -1;
965
966 if ((*self->write_func)(self,
967 PyString_AS_STRING((PyStringObject *)args), size) < 0)
968 return -1;
969 }
970
Guido van Rossum142eeb81997-08-13 03:14:41 +0000971 if (doput)
972 if (put(self, args) < 0)
973 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000974
975 return 0;
976}
977
978
979static int
980save_tuple(Picklerobject *self, PyObject *args) {
981 PyObject *element = 0, *py_tuple_id = 0;
982 int len, i, has_key, res = -1;
983
984 static char tuple = TUPLE;
985
986 if ((*self->write_func)(self, &MARKv, 1) < 0)
987 goto finally;
988
989 if ((len = PyTuple_Size(args)) < 0)
990 goto finally;
991
992 for (i = 0; i < len; i++) {
993 UNLESS(element = PyTuple_GET_ITEM((PyTupleObject *)args, i))
994 goto finally;
995
996 if (save(self, element, 0) < 0)
997 goto finally;
998 }
999
1000 UNLESS(py_tuple_id = PyInt_FromLong((long)args))
1001 goto finally;
1002
1003 if (len) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001004 if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_tuple_id)) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001005 goto finally;
1006
1007 if (has_key) {
1008 if (self->bin) {
1009 static char pop_mark = POP_MARK;
1010
1011 if ((*self->write_func)(self, &pop_mark, 1) < 0)
1012 goto finally;
1013 }
1014 else {
1015 static char pop = POP;
1016
1017 for (i = 0; i <= len; i++) {
1018 if ((*self->write_func)(self, &pop, 1) < 0)
1019 goto finally;
1020 }
1021 }
1022
1023 if (get(self, py_tuple_id) < 0)
1024 goto finally;
1025
1026 res = 0;
1027 goto finally;
1028 }
1029 }
1030
1031 if ((*self->write_func)(self, &tuple, 1) < 0) {
1032 goto finally;
1033 }
1034
1035 if (put(self, args) < 0)
1036 goto finally;
1037
1038 res = 0;
1039
1040finally:
1041 Py_XDECREF(py_tuple_id);
1042
1043 return res;
1044}
1045
1046static int
1047save_empty_tuple(Picklerobject *self, PyObject *args) {
1048 static char tuple = EMPTY_TUPLE;
1049
1050 return (*self->write_func)(self, &tuple, 1);
1051}
1052
1053
1054static int
1055save_list(Picklerobject *self, PyObject *args) {
1056 PyObject *element = 0;
1057 int s_len, len, i, using_appends, res = -1;
1058 char s[3];
1059
1060 static char append = APPEND, appends = APPENDS;
1061
1062 if(self->bin) {
1063 s[0] = EMPTY_LIST;
1064 s_len = 1;
1065 }
1066 else {
1067 s[0] = MARK;
1068 s[1] = LIST;
1069 s_len = 2;
1070 }
1071
1072 if ((len = PyList_Size(args)) < 0)
1073 goto finally;
1074
1075 if ((*self->write_func)(self, s, s_len) < 0)
1076 goto finally;
1077
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001078 if (len == 0) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001079 if (put(self, args) < 0)
1080 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001081 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001082 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001083 if (put2(self, args) < 0)
1084 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001085 }
1086
Guido van Rossum142eeb81997-08-13 03:14:41 +00001087 if ((using_appends = (self->bin && (len > 1))))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001088 if ((*self->write_func)(self, &MARKv, 1) < 0)
1089 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001090
Guido van Rossum60456fd1997-04-09 17:36:32 +00001091 for (i = 0; i < len; i++) {
1092 UNLESS(element = PyList_GET_ITEM((PyListObject *)args, i))
1093 goto finally;
1094
1095 if (save(self, element, 0) < 0)
1096 goto finally;
1097
1098 if (!using_appends) {
1099 if ((*self->write_func)(self, &append, 1) < 0)
1100 goto finally;
1101 }
1102 }
1103
1104 if (using_appends) {
1105 if ((*self->write_func)(self, &appends, 1) < 0)
1106 goto finally;
1107 }
1108
1109 res = 0;
1110
1111finally:
1112
1113 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001114}
1115
1116
Guido van Rossum60456fd1997-04-09 17:36:32 +00001117static int
1118save_dict(Picklerobject *self, PyObject *args) {
1119 PyObject *key = 0, *value = 0;
1120 int i, len, res = -1, using_setitems;
1121 char s[3];
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001122
Guido van Rossum60456fd1997-04-09 17:36:32 +00001123 static char setitem = SETITEM, setitems = SETITEMS;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001124
Guido van Rossum60456fd1997-04-09 17:36:32 +00001125 if (self->bin) {
1126 s[0] = EMPTY_DICT;
1127 len = 1;
1128 }
1129 else {
1130 s[0] = MARK;
1131 s[1] = DICT;
1132 len = 2;
1133 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001134
Guido van Rossum60456fd1997-04-09 17:36:32 +00001135 if ((*self->write_func)(self, s, len) < 0)
1136 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001137
Guido van Rossum60456fd1997-04-09 17:36:32 +00001138 if ((len = PyDict_Size(args)) < 0)
1139 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001140
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001141 if (len == 0) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001142 if (put(self, args) < 0)
1143 goto finally;
1144 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001145 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001146 if (put2(self, args) < 0)
1147 goto finally;
1148 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001149
Guido van Rossum142eeb81997-08-13 03:14:41 +00001150 if ((using_setitems = (self->bin && (PyDict_Size(args) > 1))))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001151 if ((*self->write_func)(self, &MARKv, 1) < 0)
1152 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001153
Guido van Rossum60456fd1997-04-09 17:36:32 +00001154 i = 0;
1155 while (PyDict_Next(args, &i, &key, &value)) {
1156 if (save(self, key, 0) < 0)
1157 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001158
Guido van Rossum60456fd1997-04-09 17:36:32 +00001159 if (save(self, value, 0) < 0)
1160 goto finally;
1161
1162 if (!using_setitems) {
1163 if ((*self->write_func)(self, &setitem, 1) < 0)
1164 goto finally;
1165 }
1166 }
1167
1168 if (using_setitems) {
1169 if ((*self->write_func)(self, &setitems, 1) < 0)
1170 goto finally;
1171 }
1172
1173 res = 0;
1174
1175finally:
1176
1177 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001178}
1179
1180
Guido van Rossum60456fd1997-04-09 17:36:32 +00001181static int
1182save_inst(Picklerobject *self, PyObject *args) {
1183 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1184 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1185 char *module_str, *name_str;
1186 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001187
Guido van Rossum60456fd1997-04-09 17:36:32 +00001188 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001189
Guido van Rossum60456fd1997-04-09 17:36:32 +00001190 if ((*self->write_func)(self, &MARKv, 1) < 0)
1191 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001192
Guido van Rossum60456fd1997-04-09 17:36:32 +00001193 UNLESS(class = PyObject_GetAttr(args, __class___str))
1194 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001195
Guido van Rossum60456fd1997-04-09 17:36:32 +00001196 if (self->bin) {
1197 if (save(self, class, 0) < 0)
1198 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001199 }
1200
Guido van Rossum142eeb81997-08-13 03:14:41 +00001201 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001202 PyObject *element = 0;
1203 int i, len;
1204
1205 UNLESS(class_args =
1206 PyObject_CallObject(getinitargs_func, empty_tuple))
1207 goto finally;
1208
1209 if ((len = PyObject_Length(class_args)) < 0)
1210 goto finally;
1211
1212 for (i = 0; i < len; i++) {
1213 UNLESS(element = PySequence_GetItem(class_args, i))
1214 goto finally;
1215
1216 if (save(self, element, 0) < 0) {
1217 Py_DECREF(element);
1218 goto finally;
1219 }
1220
1221 Py_DECREF(element);
1222 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001223 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001224 else {
1225 PyErr_Clear();
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001226 }
1227
Guido van Rossum60456fd1997-04-09 17:36:32 +00001228 if (!self->bin) {
1229 UNLESS(name = ((PyClassObject *)class)->cl_name) {
1230 PyErr_SetString(PicklingError, "class has no name");
1231 goto finally;
1232 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001233
Guido van Rossume2d81cd1998-08-08 19:40:10 +00001234 UNLESS(module = whichmodule(class, name))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001235 goto finally;
1236
1237 module_str = PyString_AS_STRING((PyStringObject *)module);
1238 module_size = PyString_Size(module);
1239 name_str = PyString_AS_STRING((PyStringObject *)name);
1240 name_size = PyString_Size(name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001241
Guido van Rossum60456fd1997-04-09 17:36:32 +00001242 if ((*self->write_func)(self, &inst, 1) < 0)
1243 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001244
Guido van Rossum60456fd1997-04-09 17:36:32 +00001245 if ((*self->write_func)(self, module_str, module_size) < 0)
1246 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001247
Guido van Rossum60456fd1997-04-09 17:36:32 +00001248 if ((*self->write_func)(self, "\n", 1) < 0)
1249 goto finally;
1250
1251 if ((*self->write_func)(self, name_str, name_size) < 0)
1252 goto finally;
1253
1254 if ((*self->write_func)(self, "\n", 1) < 0)
1255 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001256 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001257 else if ((*self->write_func)(self, &obj, 1) < 0) {
1258 goto finally;
1259 }
1260
Guido van Rossum142eeb81997-08-13 03:14:41 +00001261 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001262 UNLESS(state = PyObject_CallObject(getstate_func, empty_tuple))
1263 goto finally;
1264 }
1265 else {
1266 PyErr_Clear();
1267
1268 UNLESS(state = PyObject_GetAttr(args, __dict___str)) {
1269 PyErr_Clear();
1270 res = 0;
1271 goto finally;
1272 }
1273 }
1274
1275 if (!PyDict_Check(state)) {
1276 if (put2(self, args) < 0)
1277 goto finally;
1278 }
1279 else {
1280 if (put(self, args) < 0)
1281 goto finally;
1282 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001283
Guido van Rossum60456fd1997-04-09 17:36:32 +00001284 if (save(self, state, 0) < 0)
1285 goto finally;
1286
1287 if ((*self->write_func)(self, &build, 1) < 0)
1288 goto finally;
1289
1290 res = 0;
1291
1292finally:
1293 Py_XDECREF(module);
1294 Py_XDECREF(class);
1295 Py_XDECREF(state);
1296 Py_XDECREF(getinitargs_func);
1297 Py_XDECREF(getstate_func);
1298 Py_XDECREF(class_args);
1299
1300 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001301}
1302
1303
Guido van Rossum60456fd1997-04-09 17:36:32 +00001304static int
1305save_global(Picklerobject *self, PyObject *args, PyObject *name) {
1306 PyObject *global_name = 0, *module = 0;
1307 char *name_str, *module_str;
1308 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001309
Guido van Rossum60456fd1997-04-09 17:36:32 +00001310 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001311
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001312 if (name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001313 global_name = name;
1314 Py_INCREF(global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001315 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001316 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001317 UNLESS(global_name = PyObject_GetAttr(args, __name___str))
1318 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001319 }
1320
Guido van Rossume2d81cd1998-08-08 19:40:10 +00001321 UNLESS(module = whichmodule(args, global_name))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001322 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001323
1324 module_str = PyString_AS_STRING((PyStringObject *)module);
1325 module_size = PyString_Size(module);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001326 name_str = PyString_AS_STRING((PyStringObject *)global_name);
1327 name_size = PyString_Size(global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001328
Guido van Rossum60456fd1997-04-09 17:36:32 +00001329 if ((*self->write_func)(self, &global, 1) < 0)
1330 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001331
Guido van Rossum60456fd1997-04-09 17:36:32 +00001332 if ((*self->write_func)(self, module_str, module_size) < 0)
1333 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001334
Guido van Rossum60456fd1997-04-09 17:36:32 +00001335 if ((*self->write_func)(self, "\n", 1) < 0)
1336 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001337
Guido van Rossum60456fd1997-04-09 17:36:32 +00001338 if ((*self->write_func)(self, name_str, name_size) < 0)
1339 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001340
Guido van Rossum60456fd1997-04-09 17:36:32 +00001341 if ((*self->write_func)(self, "\n", 1) < 0)
1342 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001343
Guido van Rossum60456fd1997-04-09 17:36:32 +00001344 if (put(self, args) < 0)
1345 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001346
Guido van Rossum60456fd1997-04-09 17:36:32 +00001347 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001348
Guido van Rossum60456fd1997-04-09 17:36:32 +00001349finally:
1350 Py_XDECREF(module);
1351 Py_XDECREF(global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001352
Guido van Rossum60456fd1997-04-09 17:36:32 +00001353 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001354}
1355
Guido van Rossum60456fd1997-04-09 17:36:32 +00001356static int
1357save_pers(Picklerobject *self, PyObject *args, PyObject *f) {
1358 PyObject *pid = 0;
1359 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001360
Guido van Rossum60456fd1997-04-09 17:36:32 +00001361 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001362
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001363 UNLESS(self->arg)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001364 UNLESS(self->arg = PyTuple_New(1))
1365 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001366
Guido van Rossum60456fd1997-04-09 17:36:32 +00001367 Py_INCREF(args);
1368 if (PyTuple_SetItem(self->arg, 0, args) < 0)
1369 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001370
Guido van Rossum60456fd1997-04-09 17:36:32 +00001371 UNLESS(pid = PyObject_CallObject(f, self->arg))
1372 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001373
Guido van Rossum60456fd1997-04-09 17:36:32 +00001374 if (pid != Py_None) {
1375 if (!self->bin) {
1376 if (!PyString_Check(pid)) {
1377 PyErr_SetString(PicklingError,
1378 "persistent id must be string");
1379 goto finally;
1380 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001381
Guido van Rossum60456fd1997-04-09 17:36:32 +00001382 if ((*self->write_func)(self, &persid, 1) < 0)
1383 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001384
Guido van Rossum60456fd1997-04-09 17:36:32 +00001385 if ((size = PyString_Size(pid)) < 0)
1386 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001387
Guido van Rossum60456fd1997-04-09 17:36:32 +00001388 if ((*self->write_func)(self,
1389 PyString_AS_STRING((PyStringObject *)pid), size) < 0)
1390 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001391
Guido van Rossum60456fd1997-04-09 17:36:32 +00001392 if ((*self->write_func)(self, "\n", 1) < 0)
1393 goto finally;
1394
1395 res = 1;
1396 goto finally;
1397 }
1398 else if (save(self, pid, 1) >= 0) {
1399 if ((*self->write_func)(self, &binpersid, 1) < 0)
1400 res = -1;
1401 else
1402 res = 1;
1403 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001404
Guido van Rossum60456fd1997-04-09 17:36:32 +00001405 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001406 }
1407
Guido van Rossum60456fd1997-04-09 17:36:32 +00001408 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001409
Guido van Rossum60456fd1997-04-09 17:36:32 +00001410finally:
1411 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001412
Guido van Rossum60456fd1997-04-09 17:36:32 +00001413 return res;
1414}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001415
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001416
Guido van Rossum60456fd1997-04-09 17:36:32 +00001417static int
1418save_reduce(Picklerobject *self, PyObject *callable,
1419 PyObject *tup, PyObject *state, PyObject *ob) {
1420 static char reduce = REDUCE, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001421
Guido van Rossum60456fd1997-04-09 17:36:32 +00001422 if (save(self, callable, 0) < 0)
1423 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001424
Guido van Rossum60456fd1997-04-09 17:36:32 +00001425 if (save(self, tup, 0) < 0)
1426 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001427
Guido van Rossum60456fd1997-04-09 17:36:32 +00001428 if ((*self->write_func)(self, &reduce, 1) < 0)
1429 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001430
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001431 if (ob != NULL) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001432 if (state && !PyDict_Check(state)) {
1433 if (put2(self, ob) < 0)
1434 return -1;
1435 }
1436 else {
1437 if (put(self, ob) < 0)
1438 return -1;
1439 }
1440 }
1441
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001442 if (state) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001443 if (save(self, state, 0) < 0)
1444 return -1;
1445
1446 if ((*self->write_func)(self, &build, 1) < 0)
1447 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001448 }
1449
Guido van Rossum60456fd1997-04-09 17:36:32 +00001450 return 0;
1451}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001452
Guido van Rossum60456fd1997-04-09 17:36:32 +00001453static int
1454save(Picklerobject *self, PyObject *args, int pers_save) {
1455 PyTypeObject *type;
1456 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0, *arg_tup = 0,
Guido van Rossum142eeb81997-08-13 03:14:41 +00001457 *callable = 0, *state = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001458 int res = -1, tmp, size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001459
Guido van Rossum60456fd1997-04-09 17:36:32 +00001460 if (!pers_save && self->pers_func) {
1461 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
1462 res = tmp;
1463 goto finally;
1464 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001465 }
1466
Guido van Rossum60456fd1997-04-09 17:36:32 +00001467 if (args == Py_None) {
1468 res = save_none(self, args);
1469 goto finally;
1470 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001471
Guido van Rossum60456fd1997-04-09 17:36:32 +00001472 type = args->ob_type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001473
Guido van Rossum60456fd1997-04-09 17:36:32 +00001474 switch (type->tp_name[0]) {
1475 case 'i':
1476 if (type == &PyInt_Type) {
1477 res = save_int(self, args);
1478 goto finally;
1479 }
1480 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001481
Guido van Rossum60456fd1997-04-09 17:36:32 +00001482 case 'l':
1483 if (type == &PyLong_Type) {
1484 res = save_long(self, args);
1485 goto finally;
1486 }
1487 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001488
Guido van Rossum60456fd1997-04-09 17:36:32 +00001489 case 'f':
1490 if (type == &PyFloat_Type) {
1491 res = save_float(self, args);
1492 goto finally;
1493 }
1494 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001495
Guido van Rossum60456fd1997-04-09 17:36:32 +00001496 case 't':
1497 if (type == &PyTuple_Type && PyTuple_Size(args)==0) {
1498 if(self->bin) res = save_empty_tuple(self, args);
1499 else res = save_tuple(self, args);
1500 goto finally;
1501 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001502
Guido van Rossum60456fd1997-04-09 17:36:32 +00001503 case 's':
1504 if ((type == &PyString_Type) && (PyString_Size(args) < 2)) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001505 res = save_string(self, args, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001506 goto finally;
1507 }
1508 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001509
Guido van Rossum60456fd1997-04-09 17:36:32 +00001510 if (args->ob_refcnt > 1) {
1511 long ob_id;
1512 int has_key;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001513
Guido van Rossum60456fd1997-04-09 17:36:32 +00001514 ob_id = (long)args;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001515
Guido van Rossum60456fd1997-04-09 17:36:32 +00001516 UNLESS(py_ob_id = PyInt_FromLong(ob_id))
1517 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001518
Guido van Rossum60456fd1997-04-09 17:36:32 +00001519 if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_ob_id)) < 0)
1520 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001521
Guido van Rossum60456fd1997-04-09 17:36:32 +00001522 if (has_key) {
1523 if (get(self, py_ob_id) < 0)
1524 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001525
Guido van Rossum60456fd1997-04-09 17:36:32 +00001526 res = 0;
1527 goto finally;
1528 }
1529 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001530
Guido van Rossum60456fd1997-04-09 17:36:32 +00001531 switch (type->tp_name[0]) {
1532 case 's':
1533 if (type == &PyString_Type) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001534 res = save_string(self, args, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001535 goto finally;
1536 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001537
Guido van Rossum60456fd1997-04-09 17:36:32 +00001538 case 't':
1539 if (type == &PyTuple_Type) {
1540 res = save_tuple(self, args);
1541 goto finally;
1542 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001543
Guido van Rossum60456fd1997-04-09 17:36:32 +00001544 case 'l':
1545 if (type == &PyList_Type) {
1546 res = save_list(self, args);
1547 goto finally;
1548 }
1549
1550 case 'd':
1551 if (type == &PyDict_Type) {
1552 res = save_dict(self, args);
1553 goto finally;
1554 }
1555
1556 case 'i':
1557 if (type == &PyInstance_Type) {
1558 res = save_inst(self, args);
1559 goto finally;
1560 }
1561
1562 case 'c':
1563 if (type == &PyClass_Type) {
1564 res = save_global(self, args, NULL);
1565 goto finally;
1566 }
1567
1568 case 'f':
1569 if (type == &PyFunction_Type) {
1570 res = save_global(self, args, NULL);
1571 goto finally;
1572 }
1573
1574 case 'b':
1575 if (type == &PyCFunction_Type) {
1576 res = save_global(self, args, NULL);
1577 goto finally;
1578 }
1579 }
1580
1581 if (!pers_save && self->inst_pers_func) {
1582 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
1583 res = tmp;
1584 goto finally;
1585 }
1586 }
1587
Guido van Rossum142eeb81997-08-13 03:14:41 +00001588 if ((__reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001589 Py_INCREF(__reduce__);
1590
1591 UNLESS(self->arg)
1592 UNLESS(self->arg = PyTuple_New(1))
1593 goto finally;
1594
1595 Py_INCREF(args);
1596 if (PyTuple_SetItem(self->arg, 0, args) < 0)
1597 goto finally;
1598
1599 UNLESS(t = PyObject_CallObject(__reduce__, self->arg))
1600 goto finally;
1601 }
1602 else {
1603 PyErr_Clear();
1604
Guido van Rossum142eeb81997-08-13 03:14:41 +00001605 if ((__reduce__ = PyObject_GetAttr(args, __reduce___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001606 UNLESS(t = PyObject_CallObject(__reduce__, empty_tuple))
1607 goto finally;
1608 }
1609 else {
1610 PyErr_Clear();
1611 }
1612 }
1613
1614 if (t) {
1615 if (PyString_Check(t)) {
1616 res = save_global(self, args, t);
1617 goto finally;
1618 }
1619
1620 if (!PyTuple_Check(t)) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001621 cPickle_ErrFormat(PicklingError, "Value returned by %s must "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001622 "be a tuple", "O", __reduce__);
1623 goto finally;
1624 }
1625
1626 size = PyTuple_Size(t);
1627
1628 if ((size != 3) && (size != 2)) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001629 cPickle_ErrFormat(PicklingError, "tuple returned by %s must "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001630 "contain only two or three elements", "O", __reduce__);
1631 goto finally;
1632 }
1633
1634 callable = PyTuple_GET_ITEM(t, 0);
Guido van Rossum142eeb81997-08-13 03:14:41 +00001635
Guido van Rossum60456fd1997-04-09 17:36:32 +00001636 arg_tup = PyTuple_GET_ITEM(t, 1);
1637
1638 if (size > 2) {
1639 state = PyTuple_GET_ITEM(t, 2);
1640 }
1641
Guido van Rossum9716aaa1997-12-08 15:15:16 +00001642 UNLESS(PyTuple_Check(arg_tup) || arg_tup==Py_None) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001643 cPickle_ErrFormat(PicklingError, "Second element of tuple "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001644 "returned by %s must be a tuple", "O", __reduce__);
1645 goto finally;
1646 }
1647
1648 res = save_reduce(self, callable, arg_tup, state, args);
1649 goto finally;
1650 }
1651
1652 /*
1653 if (PyObject_HasAttrString(args, "__class__")) {
1654 res = save_inst(self, args);
1655 goto finally;
1656 }
1657 */
1658
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001659 cPickle_ErrFormat(PicklingError, "Cannot pickle %s objects.",
Guido van Rossum60456fd1997-04-09 17:36:32 +00001660 "O", (PyObject *)type);
1661
1662finally:
1663 Py_XDECREF(py_ob_id);
1664 Py_XDECREF(__reduce__);
1665 Py_XDECREF(t);
1666
1667 return res;
1668}
1669
1670
1671static int
1672dump(Picklerobject *self, PyObject *args) {
1673 static char stop = STOP;
1674
1675 if (save(self, args, 0) < 0)
1676 return -1;
1677
1678 if ((*self->write_func)(self, &stop, 1) < 0)
1679 return -1;
1680
1681 if ((*self->write_func)(self, NULL, 0) < 0)
1682 return -1;
1683
1684 return 0;
1685}
1686
1687static PyObject *
1688Pickler_dump(Picklerobject *self, PyObject *args) {
1689 PyObject *ob;
1690
1691 UNLESS(PyArg_ParseTuple(args, "O", &ob))
1692 return NULL;
1693
1694 if (dump(self, ob) < 0)
1695 return NULL;
1696
1697 Py_INCREF(Py_None);
1698 return Py_None;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001699}
1700
1701
1702static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00001703dump_special(Picklerobject *self, PyObject *args) {
1704 static char stop = STOP;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001705
Guido van Rossum60456fd1997-04-09 17:36:32 +00001706 PyObject *callable, *arg_tup, *state = NULL;
1707
1708 UNLESS(PyArg_ParseTuple(args, "OO|O", &callable, &arg_tup, &state))
1709 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001710
Guido van Rossum60456fd1997-04-09 17:36:32 +00001711 UNLESS(PyTuple_Check(arg_tup)) {
1712 PyErr_SetString(PicklingError, "Second arg to dump_special must "
1713 "be tuple");
1714 return NULL;
1715 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001716
Guido van Rossum60456fd1997-04-09 17:36:32 +00001717 if (save_reduce(self, callable, arg_tup, state, NULL) < 0)
1718 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001719
Guido van Rossum60456fd1997-04-09 17:36:32 +00001720 if ((*self->write_func)(self, &stop, 1) < 0)
1721 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001722
Guido van Rossum60456fd1997-04-09 17:36:32 +00001723 if ((*self->write_func)(self, NULL, 0) < 0)
1724 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001725
Guido van Rossum60456fd1997-04-09 17:36:32 +00001726 Py_INCREF(Py_None);
1727 return Py_None;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001728}
1729
Guido van Rossum142eeb81997-08-13 03:14:41 +00001730static PyObject *
1731Pickle_clear_memo(Picklerobject *self, PyObject *args) {
1732 if(self->memo) PyDict_Clear(self->memo);
1733 Py_INCREF(Py_None);
1734 return Py_None;
1735}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001736
1737static struct PyMethodDef Pickler_methods[] = {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001738 {"dump", (PyCFunction)Pickler_dump, 1,
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001739 "dump(object) --"
1740 "Write an object in pickle format to the object's pickle stream\n"
1741 },
Guido van Rossum142eeb81997-08-13 03:14:41 +00001742 {"dump_special", (PyCFunction)dump_special, 1,
1743 ""},
1744 {"clear_memo", (PyCFunction)Pickle_clear_memo, 1,
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001745 "clear_memo() -- Clear the picklers memo"},
Guido van Rossum60456fd1997-04-09 17:36:32 +00001746 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001747};
1748
1749
1750static Picklerobject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00001751newPicklerobject(PyObject *file, int bin) {
1752 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001753
Guido van Rossum60456fd1997-04-09 17:36:32 +00001754 UNLESS(self = PyObject_NEW(Picklerobject, &Picklertype))
1755 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001756
1757 self->fp = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001758 self->write = NULL;
1759 self->memo = NULL;
1760 self->arg = NULL;
1761 self->pers_func = NULL;
1762 self->inst_pers_func = NULL;
1763 self->write_buf = NULL;
1764 self->bin = bin;
1765 self->buf_size = 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001766 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001767
Guido van Rossum60456fd1997-04-09 17:36:32 +00001768 Py_INCREF(file);
1769 self->file = file;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001770
Guido van Rossum60456fd1997-04-09 17:36:32 +00001771 UNLESS(self->memo = PyDict_New()) {
1772 Py_XDECREF((PyObject *)self);
1773 return NULL;
1774 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001775
Guido van Rossum60456fd1997-04-09 17:36:32 +00001776 if (PyFile_Check(file)) {
1777 self->fp = PyFile_AsFile(file);
1778 self->write_func = write_file;
1779 }
1780 else if (PycStringIO_OutputCheck(file)) {
1781 self->write_func = write_cStringIO;
1782 }
Guido van Rossum142eeb81997-08-13 03:14:41 +00001783 else if (file == Py_None) {
1784 self->write_func = write_none;
1785 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001786 else {
1787 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001788
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001789 UNLESS(self->write = PyObject_GetAttr(file, write_str)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001790 PyErr_Clear();
1791 PyErr_SetString(PyExc_TypeError, "argument must have 'write' "
1792 "attribute");
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001793 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001794 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001795
Guido van Rossum60456fd1997-04-09 17:36:32 +00001796 UNLESS(self->write_buf =
1797 (char *)malloc(WRITE_BUF_SIZE * sizeof(char))) {
1798 PyErr_NoMemory();
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001799 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001800 }
1801 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001802
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001803 if(PyEval_GetRestricted()) {
1804 /* Restricted execution, get private tables */
1805 PyObject *m;
1806
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001807 UNLESS(m=PyImport_Import(copy_reg_str)) goto err;
1808 self->dispatch_table=PyObject_GetAttr(m, dispatch_table_str);
1809 Py_DECREF(m);
1810 UNLESS(self->dispatch_table) goto err;
1811 }
1812 else {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001813 self->dispatch_table=dispatch_table;
1814 Py_INCREF(dispatch_table);
1815 }
1816
Guido van Rossum60456fd1997-04-09 17:36:32 +00001817 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001818
1819err:
1820 Py_DECREF((PyObject *)self);
1821 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001822}
1823
1824
1825static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00001826get_Pickler(PyObject *self, PyObject *args) {
1827 PyObject *file;
1828 int bin = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001829
Guido van Rossum60456fd1997-04-09 17:36:32 +00001830 UNLESS(PyArg_ParseTuple(args, "O|i", &file, &bin)) return NULL;
1831 return (PyObject *)newPicklerobject(file, bin);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001832}
1833
1834
1835static void
Guido van Rossum60456fd1997-04-09 17:36:32 +00001836Pickler_dealloc(Picklerobject *self) {
1837 Py_XDECREF(self->write);
1838 Py_XDECREF(self->memo);
1839 Py_XDECREF(self->arg);
1840 Py_XDECREF(self->file);
1841 Py_XDECREF(self->pers_func);
1842 Py_XDECREF(self->inst_pers_func);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001843 Py_XDECREF(self->dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001844
1845 if (self->write_buf) {
1846 free(self->write_buf);
1847 }
1848
1849 PyMem_DEL(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001850}
1851
1852
1853static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00001854Pickler_getattr(Picklerobject *self, char *name) {
1855 if (strcmp(name, "persistent_id") == 0) {
1856 if (!self->pers_func) {
1857 PyErr_SetString(PyExc_AttributeError, name);
1858 return NULL;
1859 }
1860
1861 Py_INCREF(self->pers_func);
1862 return self->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001863 }
1864
Guido van Rossum60456fd1997-04-09 17:36:32 +00001865 if (strcmp(name, "memo") == 0) {
1866 if (!self->memo) {
1867 PyErr_SetString(PyExc_AttributeError, name);
1868 return NULL;
1869 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001870
Guido van Rossum60456fd1997-04-09 17:36:32 +00001871 Py_INCREF(self->memo);
1872 return self->memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001873 }
1874
Guido van Rossum60456fd1997-04-09 17:36:32 +00001875 if (strcmp(name, "PicklingError") == 0) {
1876 Py_INCREF(PicklingError);
1877 return PicklingError;
1878 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001879
Guido van Rossum60456fd1997-04-09 17:36:32 +00001880 return Py_FindMethod(Pickler_methods, (PyObject *)self, name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001881}
1882
1883
1884int
Guido van Rossum60456fd1997-04-09 17:36:32 +00001885Pickler_setattr(Picklerobject *self, char *name, PyObject *value) {
1886 if (strcmp(name, "persistent_id") == 0) {
1887 Py_XDECREF(self->pers_func);
1888 self->pers_func = value;
1889 Py_INCREF(value);
1890 return 0;
1891 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001892
Guido van Rossum60456fd1997-04-09 17:36:32 +00001893 if (strcmp(name, "inst_persistent_id") == 0) {
1894 Py_XDECREF(self->inst_pers_func);
1895 self->inst_pers_func = value;
1896 Py_INCREF(value);
1897 return 0;
1898 }
1899
1900 PyErr_SetString(PyExc_AttributeError, name);
1901 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001902}
1903
1904
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001905static char Picklertype__doc__[] =
1906"Objects that know how to pickle objects\n"
1907;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001908
Guido van Rossum9716aaa1997-12-08 15:15:16 +00001909static PyTypeObject Picklertype = {
1910 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001911 0, /*ob_size*/
1912 "Pickler", /*tp_name*/
1913 sizeof(Picklerobject), /*tp_basicsize*/
1914 0, /*tp_itemsize*/
1915 /* methods */
1916 (destructor)Pickler_dealloc, /*tp_dealloc*/
1917 (printfunc)0, /*tp_print*/
1918 (getattrfunc)Pickler_getattr, /*tp_getattr*/
1919 (setattrfunc)Pickler_setattr, /*tp_setattr*/
1920 (cmpfunc)0, /*tp_compare*/
1921 (reprfunc)0, /*tp_repr*/
1922 0, /*tp_as_number*/
1923 0, /*tp_as_sequence*/
1924 0, /*tp_as_mapping*/
1925 (hashfunc)0, /*tp_hash*/
1926 (ternaryfunc)0, /*tp_call*/
1927 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001928
Guido van Rossum60456fd1997-04-09 17:36:32 +00001929 /* Space for future expansion */
1930 0L,0L,0L,0L,
1931 Picklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00001932};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001933
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001934static PyObject *
Guido van Rossume2d81cd1998-08-08 19:40:10 +00001935find_class(PyObject *py_module_name, PyObject *py_global_name) {
1936 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001937
Guido van Rossume2d81cd1998-08-08 19:40:10 +00001938 UNLESS(module=PySys_GetObject("modules")) return NULL;
1939 UNLESS(module=PyDict_GetItem(module, py_module_name)) {
1940 PyErr_Clear();
1941 UNLESS(module=PyImport_Import(py_module_name)) return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001942 }
1943
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001944 global=PyObject_GetAttr(module, py_global_name);
1945 Py_DECREF(module);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001946
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001947 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001948}
1949
1950
1951static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00001952marker(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001953 if (self->num_marks < 1) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001954 PyErr_SetString(UnpicklingError, "could not find MARK");
1955 return -1;
1956 }
1957
1958 return self->marks[--self->num_marks];
1959}
1960
1961
1962static int
1963load_none(Unpicklerobject *self) {
1964 if (PyList_Append(self->stack, Py_None) < 0)
1965 return -1;
1966
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001967 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001968}
1969
1970
1971static int
1972load_int(Unpicklerobject *self) {
1973 PyObject *py_int = 0;
1974 char *endptr, *s;
1975 int len, res = -1;
1976 long l;
1977
1978 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossum725d9411997-08-20 23:38:57 +00001979 UNLESS(s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001980
1981 errno = 0;
1982 l = strtol(s, &endptr, 0);
1983
1984 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
1985 /* Hm, maybe we've got something long. Let's try reading
1986 it as a Python long object. */
1987 errno=0;
1988 UNLESS(py_int=PyLong_FromString(s,&endptr,0)) goto finally;
1989
1990 if ((*endptr != '\n') || (endptr[1] != '\0')) {
1991 PyErr_SetString(PyExc_ValueError,
1992 "could not convert string to int");
1993 goto finally;
1994 }
1995 }
1996 else {
1997 UNLESS(py_int = PyInt_FromLong(l)) goto finally;
1998 }
1999
2000 if (PyList_Append(self->stack, py_int) < 0) goto finally;
2001
2002 res = 0;
2003
2004finally:
2005 free(s);
2006 Py_XDECREF(py_int);
2007
2008 return res;
2009}
2010
2011
2012static long
2013calc_binint(char *s, int x) {
2014 unsigned char c;
2015 int i;
2016 long l;
2017
2018 for (i = 0, l = 0L; i < x; i++) {
2019 c = (unsigned char)s[i];
2020 l |= (long)c << (i * 8);
2021 }
2022
2023 return l;
2024}
2025
2026
2027static int
2028load_binintx(Unpicklerobject *self, char *s, int x) {
2029 PyObject *py_int = 0;
2030 long l;
2031
2032 l = calc_binint(s, x);
2033
2034 UNLESS(py_int = PyInt_FromLong(l))
2035 return -1;
2036
2037 if (PyList_Append(self->stack, py_int) < 0) {
2038 Py_DECREF(py_int);
2039 return -1;
2040 }
2041
2042 Py_DECREF(py_int);
2043
2044 return 0;
2045}
2046
2047
2048static int
2049load_binint(Unpicklerobject *self) {
2050 char *s;
2051
2052 if ((*self->read_func)(self, &s, 4) < 0)
2053 return -1;
2054
2055 return load_binintx(self, s, 4);
2056}
2057
2058
2059static int
2060load_binint1(Unpicklerobject *self) {
2061 char *s;
2062
2063 if ((*self->read_func)(self, &s, 1) < 0)
2064 return -1;
2065
2066 return load_binintx(self, s, 1);
2067}
2068
2069
2070static int
2071load_binint2(Unpicklerobject *self) {
2072 char *s;
2073
2074 if ((*self->read_func)(self, &s, 2) < 0)
2075 return -1;
2076
2077 return load_binintx(self, s, 2);
2078}
2079
2080static int
2081load_long(Unpicklerobject *self) {
2082 PyObject *l = 0;
2083 char *end, *s;
2084 int len, res = -1;
2085
Guido van Rossum60456fd1997-04-09 17:36:32 +00002086 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossum725d9411997-08-20 23:38:57 +00002087 UNLESS(s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002088
2089 UNLESS(l = PyLong_FromString(s, &end, 0))
2090 goto finally;
2091
2092 if (PyList_Append(self->stack, l) < 0)
2093 goto finally;
2094
2095 res = 0;
2096
2097finally:
2098 free(s);
2099 Py_XDECREF(l);
2100
2101 return res;
2102}
2103
2104
2105static int
2106load_float(Unpicklerobject *self) {
2107 PyObject *py_float = 0;
2108 char *endptr, *s;
2109 int len, res = -1;
2110 double d;
2111
2112 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossum725d9411997-08-20 23:38:57 +00002113 UNLESS(s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002114
2115 errno = 0;
2116 d = strtod(s, &endptr);
2117
2118 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
2119 PyErr_SetString(PyExc_ValueError,
2120 "could not convert string to float");
2121 goto finally;
2122 }
2123
2124 UNLESS(py_float = PyFloat_FromDouble(d))
2125 goto finally;
2126
2127 if (PyList_Append(self->stack, py_float) < 0)
2128 goto finally;
2129
2130 res = 0;
2131
2132finally:
2133 free(s);
2134 Py_XDECREF(py_float);
2135
2136 return res;
2137}
2138
Guido van Rossum60456fd1997-04-09 17:36:32 +00002139static int
2140load_binfloat(Unpicklerobject *self) {
2141 PyObject *py_float = 0;
2142 int s, e, res = -1;
2143 long fhi, flo;
2144 double x;
2145 char *p;
2146
2147 if ((*self->read_func)(self, &p, 8) < 0)
2148 return -1;
2149
2150 /* First byte */
2151 s = (*p>>7) & 1;
2152 e = (*p & 0x7F) << 4;
2153 p++;
2154
2155 /* Second byte */
2156 e |= (*p>>4) & 0xF;
2157 fhi = (*p & 0xF) << 24;
2158 p++;
2159
2160 /* Third byte */
2161 fhi |= (*p & 0xFF) << 16;
2162 p++;
2163
2164 /* Fourth byte */
2165 fhi |= (*p & 0xFF) << 8;
2166 p++;
2167
2168 /* Fifth byte */
2169 fhi |= *p & 0xFF;
2170 p++;
2171
2172 /* Sixth byte */
2173 flo = (*p & 0xFF) << 16;
2174 p++;
2175
2176 /* Seventh byte */
2177 flo |= (*p & 0xFF) << 8;
2178 p++;
2179
2180 /* Eighth byte */
2181 flo |= *p & 0xFF;
2182
2183 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
2184 x /= 268435456.0; /* 2**28 */
2185
2186 /* XXX This sadly ignores Inf/NaN */
2187 if (e == 0)
2188 e = -1022;
2189 else {
2190 x += 1.0;
2191 e -= 1023;
2192 }
2193 x = ldexp(x, e);
2194
2195 if (s)
2196 x = -x;
2197
2198 UNLESS(py_float = PyFloat_FromDouble(x))
2199 goto finally;
2200
2201 if (PyList_Append(self->stack, py_float) < 0)
2202 goto finally;
2203
2204 res = 0;
2205
2206finally:
2207 Py_XDECREF(py_float);
2208
2209 return res;
2210}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002211
2212static int
2213load_string(Unpicklerobject *self) {
2214 PyObject *str = 0;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002215 int len, res = -1, nslash;
2216 char *s, q, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002217
2218 static PyObject *eval_dict = 0;
2219
2220 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossum725d9411997-08-20 23:38:57 +00002221 UNLESS(s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002222
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002223 /* Check for unquoted quotes (evil strings) */
2224 q=*s;
2225 if(q != '"' && q != '\'') goto insecure;
2226 for(p=s+1, nslash=0; *p; p++)
2227 {
2228 if(*p==q && nslash%2==0) break;
2229 if(*p=='\\') nslash++;
2230 else nslash=0;
2231 }
2232 if(*p==q)
2233 {
2234 for(p++; *p; p++) if(*p > ' ') goto insecure;
2235 }
2236 else goto insecure;
2237 /********************************************/
2238
Guido van Rossum60456fd1997-04-09 17:36:32 +00002239 UNLESS(eval_dict)
2240 UNLESS(eval_dict = Py_BuildValue("{s{}}", "__builtins__"))
2241 goto finally;
2242
Guido van Rossumb05a5c71997-05-07 17:46:13 +00002243 UNLESS(str = PyRun_String(s, Py_eval_input, eval_dict, eval_dict))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002244 goto finally;
2245
2246 if (PyList_Append(self->stack, str) < 0)
2247 goto finally;
2248
2249 res = 0;
2250
2251finally:
2252 free(s);
2253 Py_XDECREF(str);
2254
2255 return res;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002256
2257insecure:
2258 free(s);
2259 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
2260 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002261}
2262
2263
2264static int
2265load_binstring(Unpicklerobject *self) {
2266 PyObject *py_string = 0;
2267 long l;
2268 int res = -1;
2269 char *s;
2270
2271 if ((*self->read_func)(self, &s, 4) < 0)
2272 goto finally;
2273
2274 l = calc_binint(s, 4);
2275
2276 if ((*self->read_func)(self, &s, l) < 0)
2277 goto finally;
2278
2279 UNLESS(py_string = PyString_FromStringAndSize(s, l))
2280 goto finally;
2281
2282 if (PyList_Append(self->stack, py_string) < 0)
2283 goto finally;
2284
2285 res = 0;
2286
2287finally:
2288 Py_XDECREF(py_string);
2289
2290 return res;
2291}
2292
2293
2294static int
2295load_short_binstring(Unpicklerobject *self) {
2296 PyObject *py_string = 0;
2297 unsigned char l;
2298 int res = -1;
2299 char *s;
2300
2301 if ((*self->read_func)(self, &s, 1) < 0)
2302 return -1;
2303
2304 l = (unsigned char)s[0];
2305
2306 if ((*self->read_func)(self, &s, l) < 0)
2307 goto finally;
2308
2309 UNLESS(py_string = PyString_FromStringAndSize(s, l))
2310 goto finally;
2311
2312 if (PyList_Append(self->stack, py_string) < 0)
2313 goto finally;
2314
2315 res = 0;
2316
2317finally:
2318 Py_XDECREF(py_string);
2319
2320 return res;
2321}
2322
2323
2324static int
2325load_tuple(Unpicklerobject *self) {
2326 PyObject *tup = 0, *slice = 0, *list = 0;
2327 int i, j, res = -1;
2328
2329 if ((i = marker(self)) < 0)
2330 goto finally;
2331
2332 if ((j = PyList_Size(self->stack)) < 0)
2333 goto finally;
2334
2335 UNLESS(slice = PyList_GetSlice(self->stack, i, j))
2336 goto finally;
2337
2338 UNLESS(tup = PySequence_Tuple(slice))
2339 goto finally;
2340
2341 UNLESS(list = PyList_New(1))
2342 goto finally;
2343
2344 Py_INCREF(tup);
2345 if (PyList_SetItem(list, 0, tup) < 0)
2346 goto finally;
2347
2348 if (PyList_SetSlice(self->stack, i, j, list) < 0)
2349 goto finally;
2350
2351 res = 0;
2352
2353finally:
2354 Py_XDECREF(tup);
2355 Py_XDECREF(list);
2356 Py_XDECREF(slice);
2357
2358 return res;
2359}
2360
2361static int
2362load_empty_tuple(Unpicklerobject *self) {
2363 PyObject *tup = 0;
2364 int res;
2365
2366 UNLESS(tup=PyTuple_New(0)) return -1;
2367 res=PyList_Append(self->stack, tup);
2368 Py_DECREF(tup);
2369 return res;
2370}
2371
2372static int
2373load_empty_list(Unpicklerobject *self) {
2374 PyObject *list = 0;
2375 int res;
2376
2377 UNLESS(list=PyList_New(0)) return -1;
2378 res=PyList_Append(self->stack, list);
2379 Py_DECREF(list);
2380 return res;
2381}
2382
2383static int
2384load_empty_dict(Unpicklerobject *self) {
2385 PyObject *dict = 0;
2386 int res;
2387
2388 UNLESS(dict=PyDict_New()) return -1;
2389 res=PyList_Append(self->stack, dict);
2390 Py_DECREF(dict);
2391 return res;
2392}
2393
2394
2395static int
2396load_list(Unpicklerobject *self) {
2397 PyObject *list = 0, *slice = 0;
2398 int i, j, l, res = -1;
2399
2400 if ((i = marker(self)) < 0)
2401 goto finally;
2402
2403 if ((j = PyList_Size(self->stack)) < 0)
2404 goto finally;
2405
2406 UNLESS(slice = PyList_GetSlice(self->stack, i, j))
2407 goto finally;
2408
2409 if((l=PyList_Size(slice)) < 0)
2410 goto finally;
2411
2412 if(l) {
2413 UNLESS(list = PyList_New(1))
2414 goto finally;
2415
2416 Py_INCREF(slice);
2417 if (PyList_SetItem(list, 0, slice) < 0)
2418 goto finally;
2419
2420 if (PyList_SetSlice(self->stack, i, j, list) < 0)
2421 goto finally;
2422 } else {
2423 if(PyList_Append(self->stack,slice) < 0)
2424 goto finally;
2425 }
2426
2427 res = 0;
2428
2429finally:
2430 Py_XDECREF(list);
2431 Py_XDECREF(slice);
2432
2433 return res;
2434}
2435
2436static int
2437load_dict(Unpicklerobject *self) {
2438 PyObject *list = 0, *dict = 0, *key = 0, *value = 0;
2439 int i, j, k, 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(dict = PyDict_New())
2448 goto finally;
2449
2450 for (k = i; k < j; k += 2) {
2451 UNLESS(key = PyList_GET_ITEM((PyListObject *)self->stack, k))
2452 goto finally;
2453
2454 UNLESS(value = PyList_GET_ITEM((PyListObject *)self->stack, k + 1))
2455 goto finally;
2456
2457 if (PyDict_SetItem(dict, key, value) < 0)
2458 goto finally;
2459 }
2460
2461 if(j) {
2462
2463 UNLESS(list = PyList_New(1))
2464 goto finally;
2465
2466 Py_INCREF(dict);
2467 if (PyList_SetItem(list, 0, dict) < 0)
2468 goto finally;
2469
2470 if (PyList_SetSlice(self->stack, i, j, list) < 0)
2471 goto finally;
2472 }
2473 else
2474 if(PyList_Append(self->stack, dict) < 0)
2475 goto finally;
2476
2477 res = 0;
2478
2479finally:
2480 Py_XDECREF(dict);
2481 Py_XDECREF(list);
2482
2483 return res;
2484}
2485
2486static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002487Instance_New(PyObject *cls, PyObject *args) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002488 int has_key;
2489 PyObject *safe=0, *r=0;
2490
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002491 if (PyClass_Check(cls)) {
2492 int l;
2493
2494 if((l=PyObject_Length(args)) < 0) goto err;
2495 UNLESS(l) {
2496 PyObject *__getinitargs__;
2497
2498 UNLESS(__getinitargs__=PyObject_GetAttr(cls, __getinitargs___str)) {
2499 /* We have a class with no __getinitargs__, so bypass usual
2500 construction */
2501 PyInstanceObject *inst;
2502
2503 PyErr_Clear();
2504 UNLESS(inst=PyObject_NEW(PyInstanceObject, &PyInstance_Type))
2505 goto err;
2506 inst->in_class=(PyClassObject*)cls;
2507 Py_INCREF(cls);
2508 UNLESS(inst->in_dict=PyDict_New()) {
2509 Py_DECREF(inst);
2510 goto err;
2511 }
2512
2513 return (PyObject *)inst;
2514 }
2515 Py_DECREF(__getinitargs__);
2516 }
2517
2518 if((r=PyInstance_New(cls, args, NULL))) return r;
2519 else goto err;
2520 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002521
2522
2523 if ((has_key = cPickle_PyMapping_HasKey(safe_constructors, cls)) < 0)
2524 goto err;
2525
2526 if (!has_key)
2527 if(!(safe = PyObject_GetAttr(cls, __safe_for_unpickling___str)) ||
2528 !PyObject_IsTrue(safe)) {
Guido van Rossume2d81cd1998-08-08 19:40:10 +00002529 cPickle_ErrFormat(UnpicklingError,
2530 "%s is not safe for unpickling", "O", cls);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002531 Py_XDECREF(safe);
2532 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002533 }
2534
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002535 if(args==Py_None)
2536 {
2537 /* Special case, call cls.__basicnew__() */
2538 PyObject *basicnew;
2539
2540 UNLESS(basicnew=PyObject_GetAttr(cls, __basicnew___str)) return NULL;
2541 r=PyObject_CallObject(basicnew, NULL);
2542 Py_DECREF(basicnew);
2543 if(r) return r;
2544 }
2545
Guido van Rossum142eeb81997-08-13 03:14:41 +00002546 if((r=PyObject_CallObject(cls, args))) return r;
2547
Guido van Rossum60456fd1997-04-09 17:36:32 +00002548err:
2549 {
2550 PyObject *tp, *v, *tb;
2551
2552 PyErr_Fetch(&tp, &v, &tb);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002553 if((r=Py_BuildValue("OOO",v,cls,args))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002554 Py_XDECREF(v);
2555 v=r;
2556 }
2557 PyErr_Restore(tp,v,tb);
2558 }
2559 return NULL;
2560}
2561
2562
2563static int
2564load_obj(Unpicklerobject *self) {
2565 PyObject *class = 0, *slice = 0, *tup = 0, *obj = 0;
2566 int i, len, res = -1;
2567
2568 if ((i = marker(self)) < 0)
2569 goto finally;
2570
Guido van Rossum60456fd1997-04-09 17:36:32 +00002571 if ((len = PyList_Size(self->stack)) < 0)
2572 goto finally;
2573
2574 UNLESS(slice = PyList_GetSlice(self->stack, i + 1, len))
2575 goto finally;
2576
2577 UNLESS(tup = PySequence_Tuple(slice))
2578 goto finally;
2579
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002580 class = PyList_GET_ITEM((PyListObject *)self->stack, i);
2581 Py_INCREF(class);
2582
Guido van Rossum60456fd1997-04-09 17:36:32 +00002583 UNLESS(obj = Instance_New(class, tup))
2584 goto finally;
2585
2586 if (DEL_LIST_SLICE(self->stack, i, len) < 0)
2587 goto finally;
2588
2589 if (PyList_Append(self->stack, obj) < 0)
2590 goto finally;
2591
2592 res = 0;
2593
2594finally:
2595
2596 Py_XDECREF(class);
2597 Py_XDECREF(slice);
2598 Py_XDECREF(tup);
2599 Py_XDECREF(obj);
2600
2601 return res;
2602}
2603
2604
2605static int
2606load_inst(Unpicklerobject *self) {
2607 PyObject *arg_tup = 0, *arg_slice = 0, *class = 0, *obj = 0,
2608 *module_name = 0, *class_name = 0;
2609 int i, j, len, res = -1;
2610 char *s;
2611
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002612 if ((i = marker(self)) < 0) goto finally;
2613
2614 if ((j = PyList_Size(self->stack)) < 0) goto finally;
2615
2616 UNLESS(arg_slice = PyList_GetSlice(self->stack, i, j)) goto finally;
2617
2618 UNLESS(arg_tup = PySequence_Tuple(arg_slice)) goto finally;
2619
2620 if (DEL_LIST_SLICE(self->stack, i, j) < 0) goto finally;
2621
2622 if ((len = (*self->readline_func)(self, &s)) < 0) goto finally;
2623
2624 UNLESS(module_name = PyString_FromStringAndSize(s, len - 1)) goto finally;
2625
2626 if ((len = (*self->readline_func)(self, &s)) < 0) goto finally;
2627
2628 UNLESS(class_name = PyString_FromStringAndSize(s, len - 1)) goto finally;
2629
Guido van Rossume2d81cd1998-08-08 19:40:10 +00002630 UNLESS(class = find_class(module_name, class_name))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002631 goto finally;
2632
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002633 UNLESS(obj = Instance_New(class, arg_tup)) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002634
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002635 if (PyList_Append(self->stack, obj) < 0) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002636
2637 res = 0;
2638
2639finally:
2640 Py_XDECREF(class);
2641 Py_XDECREF(arg_slice);
2642 Py_XDECREF(arg_tup);
2643 Py_XDECREF(obj);
2644 Py_XDECREF(module_name);
2645 Py_XDECREF(class_name);
2646
2647 return res;
2648}
2649
2650
2651static int
2652load_global(Unpicklerobject *self) {
2653 PyObject *class = 0, *module_name = 0, *class_name = 0;
2654 int res = -1, len;
2655 char *s;
2656
2657 if ((len = (*self->readline_func)(self, &s)) < 0)
2658 goto finally;
2659
2660 UNLESS(module_name = PyString_FromStringAndSize(s, len - 1))
2661 goto finally;
2662
2663 if ((len = (*self->readline_func)(self, &s)) < 0)
2664 goto finally;
2665
2666 UNLESS(class_name = PyString_FromStringAndSize(s, len - 1))
2667 goto finally;
2668
Guido van Rossume2d81cd1998-08-08 19:40:10 +00002669 UNLESS(class = find_class(module_name, class_name))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002670 goto finally;
2671
2672 if (PyList_Append(self->stack, class) < 0)
2673 goto finally;
2674
2675 res = 0;
2676
2677finally:
2678 Py_XDECREF(class);
2679 Py_XDECREF(module_name);
2680 Py_XDECREF(class_name);
2681
2682 return res;
2683}
2684
2685
2686static int
2687load_persid(Unpicklerobject *self) {
2688 PyObject *pid = 0, *pers_load_val = 0;
2689 int len, res = -1;
2690 char *s;
2691
2692 if (self->pers_func) {
2693 if ((len = (*self->readline_func)(self, &s)) < 0)
2694 goto finally;
2695
2696 UNLESS(pid = PyString_FromStringAndSize(s, len - 1))
2697 goto finally;
2698
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002699 if(PyList_Check(self->pers_func)) {
2700 if(PyList_Append(self->pers_func, pid) < 0) goto finally;
2701 pers_load_val=pid;
2702 Py_INCREF(pid);
2703 }
2704 else {
2705 UNLESS(self->arg)
2706 UNLESS(self->arg = PyTuple_New(1))
2707 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002708
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002709 Py_INCREF(pid);
2710 if (PyTuple_SetItem(self->arg, 0, pid) < 0)
2711 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002712
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002713 UNLESS(pers_load_val =
2714 PyObject_CallObject(self->pers_func, self->arg))
2715 goto finally;
2716 }
2717 if (PyList_Append(self->stack, pers_load_val) < 0)
2718 goto finally;
2719 }
2720 else {
2721 PyErr_SetString(UnpicklingError,
2722 "A load persistent id instruction was encountered,\n"
2723 "but no persistent_load function was specified.");
2724 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002725 }
2726
2727 res = 0;
2728
2729finally:
2730 Py_XDECREF(pid);
2731 Py_XDECREF(pers_load_val);
2732
2733 return res;
2734}
2735
2736
2737static int
2738load_binpersid(Unpicklerobject *self) {
2739 PyObject *pid = 0, *pers_load_val = 0;
2740 int len, res = -1;
2741
2742 if (self->pers_func) {
2743 if ((len = PyList_Size(self->stack)) < 0)
2744 goto finally;
2745
2746 pid = PyList_GET_ITEM((PyListObject *)self->stack, len - 1);
2747 Py_INCREF(pid);
2748
2749 if (DEL_LIST_SLICE(self->stack, len - 1, len) < 0)
2750 goto finally;
2751
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002752 if(PyList_Check(self->pers_func)) {
2753 if(PyList_Append(self->pers_func, pid) < 0) goto finally;
2754 pers_load_val=pid;
2755 Py_INCREF(pid);
2756 }
2757 else {
2758 UNLESS(self->arg)
Guido van Rossum60456fd1997-04-09 17:36:32 +00002759 UNLESS(self->arg = PyTuple_New(1))
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002760 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002761
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002762 Py_INCREF(pid);
2763 if (PyTuple_SetItem(self->arg, 0, pid) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00002764 goto finally;
2765
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002766 UNLESS(pers_load_val =
2767 PyObject_CallObject(self->pers_func, self->arg))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002768 goto finally;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002769 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002770 if (PyList_Append(self->stack, pers_load_val) < 0)
2771 goto finally;
2772 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002773 else {
2774 PyErr_SetString(UnpicklingError,
2775 "A load persistent id instruction was encountered,\n"
2776 "but no persistent_load function was specified.");
2777 goto finally;
2778 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002779
2780 res = 0;
2781
2782finally:
2783 Py_XDECREF(pid);
2784 Py_XDECREF(pers_load_val);
2785
2786 return res;
2787}
2788
2789
2790static int
2791load_pop(Unpicklerobject *self) {
2792 int len;
2793
2794 if ((len = PyList_Size(self->stack)) < 0)
2795 return -1;
2796
2797 if ((self->num_marks > 0) &&
2798 (self->marks[self->num_marks - 1] == len))
2799 self->num_marks--;
2800 else if (DEL_LIST_SLICE(self->stack, len - 1, len) < 0)
2801 return -1;
2802
2803 return 0;
2804}
2805
2806
2807static int
2808load_pop_mark(Unpicklerobject *self) {
2809 int i, len;
2810
2811 if ((i = marker(self)) < 0)
2812 return -1;
2813
2814 if ((len = PyList_Size(self->stack)) < 0)
2815 return -1;
2816
2817 if (DEL_LIST_SLICE(self->stack, i, len) < 0)
2818 return -1;
2819
2820 return 0;
2821}
2822
2823
2824static int
2825load_dup(Unpicklerobject *self) {
2826 PyObject *last;
2827 int len;
2828
2829 if ((len = PyList_Size(self->stack)) < 0)
2830 return -1;
2831
2832 UNLESS(last = PyList_GetItem(self->stack, len - 1))
2833 return -1;
2834
2835 if (PyList_Append(self->stack, last) < 0)
2836 return -1;
2837
2838 return 0;
2839}
2840
2841
2842static int
2843load_get(Unpicklerobject *self) {
2844 PyObject *py_str = 0, *value = 0;
2845 int len, res = -1;
2846 char *s;
2847
2848 if ((len = (*self->readline_func)(self, &s)) < 0)
2849 goto finally;
2850
2851 UNLESS(py_str = PyString_FromStringAndSize(s, len - 1))
2852 goto finally;
2853
2854 UNLESS(value = PyDict_GetItem(self->memo, py_str))
2855 goto finally;
2856
2857 if (PyList_Append(self->stack, value) < 0)
2858 goto finally;
2859
2860 res = 0;
2861
2862finally:
2863 Py_XDECREF(py_str);
2864
2865 return res;
2866}
2867
2868
2869static int
2870load_binget(Unpicklerobject *self) {
2871 PyObject *py_key = 0, *value = 0;
2872 unsigned char key;
2873 int res = -1;
2874 char *s;
2875
2876 if ((*self->read_func)(self, &s, 1) < 0)
2877 goto finally;
2878
2879 key = (unsigned char)s[0];
2880
2881 UNLESS(py_key = PyInt_FromLong((long)key))
2882 goto finally;
2883
2884 UNLESS(value = PyDict_GetItem(self->memo, py_key))
2885 goto finally;
2886
2887 if (PyList_Append(self->stack, value) < 0)
2888 goto finally;
2889
2890 res = 0;
2891
2892finally:
2893 Py_XDECREF(py_key);
2894
2895 return res;
2896}
2897
2898
2899static int
2900load_long_binget(Unpicklerobject *self) {
2901 PyObject *py_key = 0, *value = 0;
2902 unsigned char c, *s;
2903 long key;
2904 int res = -1;
2905
2906 if ((*self->read_func)(self, &s, 4) < 0)
2907 goto finally;
2908
2909 c = (unsigned char)s[0];
2910 key = (long)c;
2911 c = (unsigned char)s[1];
2912 key |= (long)c << 8;
2913 c = (unsigned char)s[2];
2914 key |= (long)c << 16;
2915 c = (unsigned char)s[3];
2916 key |= (long)c << 24;
2917
2918 UNLESS(py_key = PyInt_FromLong(key))
2919 goto finally;
2920
2921 UNLESS(value = PyDict_GetItem(self->memo, py_key))
2922 goto finally;
2923
2924 if (PyList_Append(self->stack, value) < 0)
2925 goto finally;
2926
2927 res = 0;
2928
2929finally:
2930 Py_XDECREF(py_key);
2931
2932 return res;
2933}
2934
2935
2936static int
2937load_put(Unpicklerobject *self) {
2938 PyObject *py_str = 0, *value = 0;
2939 int len, res = -1;
2940 char *s;
2941
2942 if ((len = (*self->readline_func)(self, &s)) < 0)
2943 goto finally;
2944
2945 UNLESS(py_str = PyString_FromStringAndSize(s, len - 1))
2946 goto finally;
2947
2948 if ((len = PyList_Size(self->stack)) < 0)
2949 goto finally;
2950
2951 UNLESS(value = PyList_GetItem(self->stack, len - 1))
2952 goto finally;
2953
2954 if (PyDict_SetItem(self->memo, py_str, value) < 0)
2955 goto finally;
2956
2957 res = 0;
2958
2959finally:
2960 Py_XDECREF(py_str);
2961
2962 return res;
2963}
2964
2965
2966static int
2967load_binput(Unpicklerobject *self) {
2968 PyObject *py_key = 0, *value = 0;
2969 unsigned char key, *s;
2970 int len, res = -1;
2971
2972 if ((*self->read_func)(self, &s, 1) < 0)
2973 goto finally;
2974
2975 key = (unsigned char)s[0];
2976
2977 UNLESS(py_key = PyInt_FromLong((long)key))
2978 goto finally;
2979
2980 if ((len = PyList_Size(self->stack)) < 0)
2981 goto finally;
2982
2983 UNLESS(value = PyList_GetItem(self->stack, len - 1))
2984 goto finally;
2985
2986 if (PyDict_SetItem(self->memo, py_key, value) < 0)
2987 goto finally;
2988
2989 res = 0;
2990
2991finally:
2992 Py_XDECREF(py_key);
2993
2994 return res;
2995}
2996
2997
2998static int
2999load_long_binput(Unpicklerobject *self) {
3000 PyObject *py_key = 0, *value = 0;
3001 long key;
3002 unsigned char c, *s;
3003 int len, res = -1;
3004
3005 if ((*self->read_func)(self, &s, 4) < 0)
3006 goto finally;
3007
3008 c = (unsigned char)s[0];
3009 key = (long)c;
3010 c = (unsigned char)s[1];
3011 key |= (long)c << 8;
3012 c = (unsigned char)s[2];
3013 key |= (long)c << 16;
3014 c = (unsigned char)s[3];
3015 key |= (long)c << 24;
3016
3017 UNLESS(py_key = PyInt_FromLong(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
3039do_append(Unpicklerobject *self, int x) {
3040 PyObject *value = 0, *list = 0, *append_method = 0;
3041 int len, i;
3042
3043 if ((len = PyList_Size(self->stack)) < 0)
3044 return -1;
3045
3046 UNLESS(list = PyList_GetItem(self->stack, x - 1))
3047 goto err;
3048
3049 if (PyList_Check(list)) {
3050 PyObject *slice = 0;
3051 int list_len;
3052
3053 UNLESS(slice = PyList_GetSlice(self->stack, x, len))
3054 return -1;
3055
3056 list_len = PyList_Size(list);
3057 if (PyList_SetSlice(list, list_len, list_len, slice) < 0) {
3058 Py_DECREF(slice);
3059 return -1;
3060 }
3061
3062 Py_DECREF(slice);
3063 }
3064 else {
3065
3066 UNLESS(append_method = PyObject_GetAttr(list, append_str))
3067 return -1;
3068
3069 for (i = x; i < len; i++) {
3070 PyObject *junk;
3071
3072 UNLESS(value = PyList_GetItem(self->stack, i))
3073 return -1;
3074
3075 UNLESS(self->arg)
3076 UNLESS(self->arg = PyTuple_New(1))
3077 goto err;
3078
3079 Py_INCREF(value);
3080 if (PyTuple_SetItem(self->arg, 0, value) < 0)
3081 goto err;
3082
3083 UNLESS(junk = PyObject_CallObject(append_method, self->arg))
3084 goto err;
3085 Py_DECREF(junk);
3086 }
3087 }
3088
3089 if (DEL_LIST_SLICE(self->stack, x, len) < 0)
3090 goto err;
3091
3092 Py_XDECREF(append_method);
3093
3094 return 0;
3095
3096err:
3097 Py_XDECREF(append_method);
3098
3099 return -1;
3100}
3101
3102
3103static int
3104load_append(Unpicklerobject *self) {
3105 return do_append(self, PyList_Size(self->stack) - 1);
3106}
3107
3108
3109static int
3110load_appends(Unpicklerobject *self) {
3111 return do_append(self, marker(self));
3112}
3113
3114
3115static int
3116do_setitems(Unpicklerobject *self, int x) {
3117 PyObject *value = 0, *key = 0, *dict = 0;
3118 int len, i, res = -1;
3119
3120 if ((len = PyList_Size(self->stack)) < 0)
3121 goto finally;
3122
3123 UNLESS(dict = PyList_GetItem(self->stack, x - 1))
3124 goto finally;
3125
3126 for (i = x; i < len; i += 2) {
3127 UNLESS(key = PyList_GetItem(self->stack, i))
3128 goto finally;
3129
3130 UNLESS(value = PyList_GetItem(self->stack, i + 1))
3131 goto finally;
3132
3133 if (PyObject_SetItem(dict, key, value) < 0)
3134 goto finally;
3135 }
3136
3137 if (DEL_LIST_SLICE(self->stack, x, len) < 0)
3138 goto finally;
3139
3140 res = 0;
3141
3142finally:
3143
3144 return res;
3145}
3146
3147
3148static int
3149load_setitem(Unpicklerobject *self) {
3150 return do_setitems(self, PyList_Size(self->stack) - 2);
3151}
3152
3153
3154static int
3155load_setitems(Unpicklerobject *self) {
3156 return do_setitems(self, marker(self));
3157}
3158
3159
3160static int
3161load_build(Unpicklerobject *self) {
3162 PyObject *value = 0, *inst = 0, *instdict = 0, *d_key = 0, *d_value = 0,
3163 *junk = 0, *__setstate__ = 0;
3164 int len, i, res = -1;
3165
3166 if ((len = PyList_Size(self->stack)) < 0)
3167 goto finally;
3168
3169 UNLESS(value = PyList_GetItem(self->stack, len - 1))
3170 goto finally;
3171 Py_INCREF(value);
3172
3173 if (DEL_LIST_SLICE(self->stack, len - 1, len) < 0)
3174 goto finally;
3175
3176 UNLESS(inst = PyList_GetItem(self->stack, len - 2))
3177 goto finally;
3178
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003179 UNLESS(__setstate__ = PyObject_GetAttr(inst, __setstate___str)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003180 PyErr_Clear();
3181
3182 UNLESS(instdict = PyObject_GetAttr(inst, __dict___str))
3183 goto finally;
3184
3185 i = 0;
3186 while (PyDict_Next(value, &i, &d_key, &d_value)) {
3187 if (PyObject_SetItem(instdict, d_key, d_value) < 0)
3188 goto finally;
3189 }
3190 }
3191 else {
3192 UNLESS(self->arg)
3193 UNLESS(self->arg = PyTuple_New(1))
3194 goto finally;
3195
3196 Py_INCREF(value);
3197 if (PyTuple_SetItem(self->arg, 0, value) < 0)
3198 goto finally;
3199
3200 UNLESS(junk = PyObject_CallObject(__setstate__, self->arg))
3201 goto finally;
3202 Py_DECREF(junk);
3203 }
3204
3205 res = 0;
3206
3207finally:
3208 Py_XDECREF(value);
3209 Py_XDECREF(instdict);
3210 Py_XDECREF(__setstate__);
3211
3212 return res;
3213}
3214
3215
3216static int
3217load_mark(Unpicklerobject *self) {
3218 int len;
3219
3220 if ((len = PyList_Size(self->stack)) < 0)
3221 return -1;
3222
3223 if (!self->marks_size) {
3224 self->marks_size = 20;
3225 UNLESS(self->marks = (int *)malloc(self->marks_size * sizeof(int))) {
3226 PyErr_NoMemory();
3227 return -1;
3228 }
3229 }
3230 else if ((self->num_marks + 1) >= self->marks_size) {
3231 UNLESS(self->marks = (int *)realloc(self->marks,
3232 (self->marks_size + 20) * sizeof(int))) {
3233 PyErr_NoMemory();
3234 return -1;
3235 }
3236
3237 self->marks_size += 20;
3238 }
3239
3240 self->marks[self->num_marks++] = len;
3241
3242 return 0;
3243}
3244
3245static int
3246load_reduce(Unpicklerobject *self) {
3247 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
3248 int len, res = -1;
3249
3250 if ((len = PyList_Size(self->stack)) < 0)
3251 goto finally;
3252
3253 UNLESS(arg_tup = PyList_GetItem(self->stack, len - 1))
3254 goto finally;
3255
3256 UNLESS(callable = PyList_GetItem(self->stack, len - 2))
3257 goto finally;
3258
3259 UNLESS(ob = Instance_New(callable, arg_tup))
3260 goto finally;
3261
3262 if (PyList_Append(self->stack, ob) < 0)
3263 goto finally;
3264
3265 if (DEL_LIST_SLICE(self->stack, len - 2, len) < 0)
3266 goto finally;
3267
3268 res = 0;
3269
3270finally:
3271 Py_XDECREF(ob);
3272
3273 return res;
3274}
3275
3276static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003277load(Unpicklerobject *self) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00003278 PyObject *stack = 0, *err = 0, *val = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003279 int len;
3280 char *s;
3281
3282 UNLESS(stack = PyList_New(0))
3283 goto err;
3284
3285 self->stack = stack;
3286 self->num_marks = 0;
3287
3288 while (1) {
3289 if ((*self->read_func)(self, &s, 1) < 0)
3290 break;
3291
3292 switch (s[0]) {
3293 case NONE:
3294 if (load_none(self) < 0)
3295 break;
3296 continue;
3297
3298 case BININT:
3299 if (load_binint(self) < 0)
3300 break;
3301 continue;
3302
3303 case BININT1:
3304 if (load_binint1(self) < 0)
3305 break;
3306 continue;
3307
3308 case BININT2:
3309 if (load_binint2(self) < 0)
3310 break;
3311 continue;
3312
3313 case INT:
3314 if (load_int(self) < 0)
3315 break;
3316 continue;
3317
3318 case LONG:
3319 if (load_long(self) < 0)
3320 break;
3321 continue;
3322
3323 case FLOAT:
3324 if (load_float(self) < 0)
3325 break;
3326 continue;
3327
3328#ifdef FORMAT_1_3
3329 case BINFLOAT:
3330 if (load_binfloat(self) < 0)
3331 break;
3332 continue;
3333#endif
3334
3335 case BINSTRING:
3336 if (load_binstring(self) < 0)
3337 break;
3338 continue;
3339
3340 case SHORT_BINSTRING:
3341 if (load_short_binstring(self) < 0)
3342 break;
3343 continue;
3344
3345 case STRING:
3346 if (load_string(self) < 0)
3347 break;
3348 continue;
3349
3350 case EMPTY_TUPLE:
3351 if (load_empty_tuple(self) < 0)
3352 break;
3353 continue;
3354
3355 case TUPLE:
3356 if (load_tuple(self) < 0)
3357 break;
3358 continue;
3359
3360 case EMPTY_LIST:
3361 if (load_empty_list(self) < 0)
3362 break;
3363 continue;
3364
3365 case LIST:
3366 if (load_list(self) < 0)
3367 break;
3368 continue;
3369
3370 case EMPTY_DICT:
3371 if (load_empty_dict(self) < 0)
3372 break;
3373 continue;
3374
3375 case DICT:
3376 if (load_dict(self) < 0)
3377 break;
3378 continue;
3379
3380 case OBJ:
3381 if (load_obj(self) < 0)
3382 break;
3383 continue;
3384
3385 case INST:
3386 if (load_inst(self) < 0)
3387 break;
3388 continue;
3389
3390 case GLOBAL:
3391 if (load_global(self) < 0)
3392 break;
3393 continue;
3394
3395 case APPEND:
3396 if (load_append(self) < 0)
3397 break;
3398 continue;
3399
3400 case APPENDS:
3401 if (load_appends(self) < 0)
3402 break;
3403 continue;
3404
3405 case BUILD:
3406 if (load_build(self) < 0)
3407 break;
3408 continue;
3409
3410 case DUP:
3411 if (load_dup(self) < 0)
3412 break;
3413 continue;
3414
3415 case BINGET:
3416 if (load_binget(self) < 0)
3417 break;
3418 continue;
3419
3420 case LONG_BINGET:
3421 if (load_long_binget(self) < 0)
3422 break;
3423 continue;
3424
3425 case GET:
3426 if (load_get(self) < 0)
3427 break;
3428 continue;
3429
3430 case MARK:
3431 if (load_mark(self) < 0)
3432 break;
3433 continue;
3434
3435 case BINPUT:
3436 if (load_binput(self) < 0)
3437 break;
3438 continue;
3439
3440 case LONG_BINPUT:
3441 if (load_long_binput(self) < 0)
3442 break;
3443 continue;
3444
3445 case PUT:
3446 if (load_put(self) < 0)
3447 break;
3448 continue;
3449
3450 case POP:
3451 if (load_pop(self) < 0)
3452 break;
3453 continue;
3454
3455 case POP_MARK:
3456 if (load_pop_mark(self) < 0)
3457 break;
3458 continue;
3459
3460 case SETITEM:
3461 if (load_setitem(self) < 0)
3462 break;
3463 continue;
3464
3465 case SETITEMS:
3466 if (load_setitems(self) < 0)
3467 break;
3468 continue;
3469
3470 case STOP:
3471 break;
3472
3473 case PERSID:
3474 if (load_persid(self) < 0)
3475 break;
3476 continue;
3477
3478 case BINPERSID:
3479 if (load_binpersid(self) < 0)
3480 break;
3481 continue;
3482
3483 case REDUCE:
3484 if (load_reduce(self) < 0)
3485 break;
3486 continue;
3487
3488 default:
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00003489 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossum60456fd1997-04-09 17:36:32 +00003490 "c", s[0]);
3491 goto err;
3492 }
3493
3494 break;
3495 }
3496
Fred Drake764b9841998-05-28 04:33:37 +00003497 err = PyErr_Occurred();
3498 if (err && PyErr_ExceptionMatches(PyExc_EOFError)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003499 PyErr_SetNone(PyExc_EOFError);
3500 goto err;
3501 }
3502
3503 if (err) goto err;
3504
3505 if ((len = PyList_Size(stack)) < 0) goto err;
3506
3507 UNLESS(val = PyList_GetItem(stack, len - 1)) goto err;
3508 Py_INCREF(val);
3509
3510 Py_DECREF(stack);
3511
3512 self->stack=NULL;
3513 return val;
3514
3515err:
3516 self->stack=NULL;
3517 Py_XDECREF(stack);
3518
3519 return NULL;
3520}
3521
3522
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003523/* No-load functions to support noload, which is used to
3524 find persistent references. */
3525
3526static int
3527noload_obj(Unpicklerobject *self) {
3528 int i, len;
3529
3530 if ((i = marker(self)) < 0) return -1;
3531 if ((len = PyList_Size(self->stack)) < 0) return -1;
3532 return DEL_LIST_SLICE(self->stack, i+1, len);
3533}
3534
3535
3536static int
3537noload_inst(Unpicklerobject *self) {
3538 int i, j;
3539 char *s;
3540
3541 if ((i = marker(self)) < 0) return -1;
3542 if ((j = PyList_Size(self->stack)) < 0) return -1;
3543 if (DEL_LIST_SLICE(self->stack, i, j) < 0) return -1;
3544 if ((*self->readline_func)(self, &s) < 0) return -1;
3545 if ((*self->readline_func)(self, &s) < 0) return -1;
3546 return PyList_Append(self->stack, Py_None);
3547}
3548
3549static int
3550noload_global(Unpicklerobject *self) {
3551 char *s;
3552
3553 if ((*self->readline_func)(self, &s) < 0) return -1;
3554 if ((*self->readline_func)(self, &s) < 0) return -1;
3555 return PyList_Append(self->stack, Py_None);
3556}
3557
3558static int
3559noload_reduce(Unpicklerobject *self) {
3560 int len;
3561
3562 if ((len = PyList_Size(self->stack)) < 0) return -1;
3563 if (DEL_LIST_SLICE(self->stack, len - 2, len) < 0) return -1;
3564 return PyList_Append(self->stack, Py_None);
3565}
3566
3567static int
3568noload_build(Unpicklerobject *self) {
3569 int len;
3570
3571 if ((len = PyList_Size(self->stack)) < 0) return -1;
3572 return DEL_LIST_SLICE(self->stack, len - 1, len);
3573}
3574
3575
3576static PyObject *
3577noload(Unpicklerobject *self) {
3578 PyObject *stack = 0, *err = 0, *val = 0;
3579 int len;
3580 char *s;
3581
3582 UNLESS(stack = PyList_New(0))
3583 goto err;
3584
3585 self->stack = stack;
3586 self->num_marks = 0;
3587
3588 while (1) {
3589 if ((*self->read_func)(self, &s, 1) < 0)
3590 break;
3591
3592 switch (s[0]) {
3593 case NONE:
3594 if (load_none(self) < 0)
3595 break;
3596 continue;
3597
3598 case BININT:
3599 if (load_binint(self) < 0)
3600 break;
3601 continue;
3602
3603 case BININT1:
3604 if (load_binint1(self) < 0)
3605 break;
3606 continue;
3607
3608 case BININT2:
3609 if (load_binint2(self) < 0)
3610 break;
3611 continue;
3612
3613 case INT:
3614 if (load_int(self) < 0)
3615 break;
3616 continue;
3617
3618 case LONG:
3619 if (load_long(self) < 0)
3620 break;
3621 continue;
3622
3623 case FLOAT:
3624 if (load_float(self) < 0)
3625 break;
3626 continue;
3627
3628 case BINFLOAT:
3629 if (load_binfloat(self) < 0)
3630 break;
3631 continue;
3632
3633 case BINSTRING:
3634 if (load_binstring(self) < 0)
3635 break;
3636 continue;
3637
3638 case SHORT_BINSTRING:
3639 if (load_short_binstring(self) < 0)
3640 break;
3641 continue;
3642
3643 case STRING:
3644 if (load_string(self) < 0)
3645 break;
3646 continue;
3647
3648 case EMPTY_TUPLE:
3649 if (load_empty_tuple(self) < 0)
3650 break;
3651 continue;
3652
3653 case TUPLE:
3654 if (load_tuple(self) < 0)
3655 break;
3656 continue;
3657
3658 case EMPTY_LIST:
3659 if (load_empty_list(self) < 0)
3660 break;
3661 continue;
3662
3663 case LIST:
3664 if (load_list(self) < 0)
3665 break;
3666 continue;
3667
3668 case EMPTY_DICT:
3669 if (load_empty_dict(self) < 0)
3670 break;
3671 continue;
3672
3673 case DICT:
3674 if (load_dict(self) < 0)
3675 break;
3676 continue;
3677
3678 case OBJ:
3679 if (noload_obj(self) < 0)
3680 break;
3681 continue;
3682
3683 case INST:
3684 if (noload_inst(self) < 0)
3685 break;
3686 continue;
3687
3688 case GLOBAL:
3689 if (noload_global(self) < 0)
3690 break;
3691 continue;
3692
3693 case APPEND:
3694 if (load_append(self) < 0)
3695 break;
3696 continue;
3697
3698 case APPENDS:
3699 if (load_appends(self) < 0)
3700 break;
3701 continue;
3702
3703 case BUILD:
3704 if (noload_build(self) < 0)
3705 break;
3706 continue;
3707
3708 case DUP:
3709 if (load_dup(self) < 0)
3710 break;
3711 continue;
3712
3713 case BINGET:
3714 if (load_binget(self) < 0)
3715 break;
3716 continue;
3717
3718 case LONG_BINGET:
3719 if (load_long_binget(self) < 0)
3720 break;
3721 continue;
3722
3723 case GET:
3724 if (load_get(self) < 0)
3725 break;
3726 continue;
3727
3728 case MARK:
3729 if (load_mark(self) < 0)
3730 break;
3731 continue;
3732
3733 case BINPUT:
3734 if (load_binput(self) < 0)
3735 break;
3736 continue;
3737
3738 case LONG_BINPUT:
3739 if (load_long_binput(self) < 0)
3740 break;
3741 continue;
3742
3743 case PUT:
3744 if (load_put(self) < 0)
3745 break;
3746 continue;
3747
3748 case POP:
3749 if (load_pop(self) < 0)
3750 break;
3751 continue;
3752
3753 case POP_MARK:
3754 if (load_pop_mark(self) < 0)
3755 break;
3756 continue;
3757
3758 case SETITEM:
3759 if (load_setitem(self) < 0)
3760 break;
3761 continue;
3762
3763 case SETITEMS:
3764 if (load_setitems(self) < 0)
3765 break;
3766 continue;
3767
3768 case STOP:
3769 break;
3770
3771 case PERSID:
3772 if (load_persid(self) < 0)
3773 break;
3774 continue;
3775
3776 case BINPERSID:
3777 if (load_binpersid(self) < 0)
3778 break;
3779 continue;
3780
3781 case REDUCE:
3782 if (noload_reduce(self) < 0)
3783 break;
3784 continue;
3785
3786 default:
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00003787 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003788 "c", s[0]);
3789 goto err;
3790 }
3791
3792 break;
3793 }
3794
Fred Drake764b9841998-05-28 04:33:37 +00003795 err = PyErr_Occurred();
3796 if (err && PyErr_ExceptionMatches(PyExc_EOFError)) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003797 PyErr_SetNone(PyExc_EOFError);
3798 goto err;
3799 }
3800
3801 if (err) goto err;
3802
3803 if ((len = PyList_Size(stack)) < 0) goto err;
3804
3805 UNLESS(val = PyList_GetItem(stack, len - 1)) goto err;
3806 Py_INCREF(val);
3807
3808 Py_DECREF(stack);
3809
3810 self->stack=NULL;
3811 return val;
3812
3813err:
3814 self->stack=NULL;
3815 Py_XDECREF(stack);
3816
3817 return NULL;
3818}
3819
3820
Guido van Rossum60456fd1997-04-09 17:36:32 +00003821static PyObject *
3822Unpickler_load(Unpicklerobject *self, PyObject *args) {
3823 UNLESS(PyArg_ParseTuple(args, ""))
3824 return NULL;
3825
3826 return load(self);
3827}
3828
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003829static PyObject *
3830Unpickler_noload(Unpicklerobject *self, PyObject *args) {
3831 UNLESS(PyArg_ParseTuple(args, ""))
3832 return NULL;
3833
3834 return noload(self);
3835}
3836
Guido van Rossum60456fd1997-04-09 17:36:32 +00003837
3838static struct PyMethodDef Unpickler_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003839 {"load", (PyCFunction)Unpickler_load, 1,
3840 "load() -- Load a pickle"
3841 },
3842 {"noload", (PyCFunction)Unpickler_noload, 1,
3843 "noload() -- not load a pickle, but go through most of the motions\n"
3844 "\n"
3845 "This function can be used to read past a pickle without instantiating\n"
3846 "any objects or importing any modules. It can also be used to find all\n"
3847 "persistent references without instantiating any objects or importing\n"
3848 "any modules.\n"
3849 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00003850 {NULL, NULL} /* sentinel */
3851};
3852
3853
3854static Unpicklerobject *
3855newUnpicklerobject(PyObject *f) {
3856 Unpicklerobject *self;
3857
3858 UNLESS(self = PyObject_NEW(Unpicklerobject, &Unpicklertype))
3859 return NULL;
3860
3861 self->file = NULL;
3862 self->arg = NULL;
3863 self->stack = NULL;
3864 self->pers_func = NULL;
3865 self->last_string = NULL;
3866 self->marks = NULL;
3867 self->num_marks = 0;
3868 self->marks_size = 0;
3869 self->buf_size = 0;
3870 self->read = NULL;
Guido van Rossum8a6dba31998-03-06 01:39:39 +00003871 self->readline = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003872
3873 UNLESS(self->memo = PyDict_New()) {
3874 Py_XDECREF((PyObject *)self);
3875 return NULL;
3876 }
3877
3878 Py_INCREF(f);
3879 self->file = f;
3880
3881 /* Set read, readline based on type of f */
3882 if (PyFile_Check(f)) {
3883 self->fp = PyFile_AsFile(f);
3884 self->read_func = read_file;
3885 self->readline_func = readline_file;
3886 }
3887 else if (PycStringIO_InputCheck(f)) {
3888 self->fp = NULL;
3889 self->read_func = read_cStringIO;
3890 self->readline_func = readline_cStringIO;
3891 }
3892 else {
3893
3894 self->fp = NULL;
3895 self->read_func = read_other;
3896 self->readline_func = readline_other;
3897
3898 UNLESS((self->readline = PyObject_GetAttr(f, readline_str)) &&
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003899 (self->read = PyObject_GetAttr(f, read_str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003900 PyErr_Clear();
3901 PyErr_SetString( PyExc_TypeError, "argument must have 'read' and "
3902 "'readline' attributes" );
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003903 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003904 }
3905 }
3906
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003907 if(PyEval_GetRestricted()) {
3908 /* Restricted execution, get private tables */
3909 PyObject *m;
3910
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003911 UNLESS(m=PyImport_Import(copy_reg_str)) goto err;
3912 self->safe_constructors=PyObject_GetAttr(m, safe_constructors_str);
3913 Py_DECREF(m);
3914 UNLESS(self->safe_constructors) goto err;
3915 }
3916 else {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003917 self->safe_constructors=safe_constructors;
3918 Py_INCREF(safe_constructors);
3919 }
3920
Guido van Rossum60456fd1997-04-09 17:36:32 +00003921 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003922
3923err:
3924 Py_DECREF((PyObject *)self);
3925 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003926}
3927
3928
3929static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00003930get_Unpickler(PyObject *self, PyObject *args) {
3931 PyObject *file;
3932
3933 UNLESS(PyArg_ParseTuple(args, "O", &file))
3934 return NULL;
3935 return (PyObject *)newUnpicklerobject(file);
3936}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003937
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003938
Guido van Rossum60456fd1997-04-09 17:36:32 +00003939static void
3940Unpickler_dealloc(Unpicklerobject *self) {
3941 Py_XDECREF(self->readline);
3942 Py_XDECREF(self->read);
3943 Py_XDECREF(self->file);
3944 Py_XDECREF(self->memo);
3945 Py_XDECREF(self->stack);
3946 Py_XDECREF(self->pers_func);
3947 Py_XDECREF(self->arg);
3948 Py_XDECREF(self->last_string);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003949 Py_XDECREF(self->safe_constructors);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003950
Guido van Rossum60456fd1997-04-09 17:36:32 +00003951 if (self->marks) {
3952 free(self->marks);
3953 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003954
Guido van Rossum60456fd1997-04-09 17:36:32 +00003955 if (self->buf_size) {
3956 free(self->buf);
3957 }
3958
3959 PyMem_DEL(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003960}
3961
3962
3963static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00003964Unpickler_getattr(Unpicklerobject *self, char *name) {
3965 if (!strcmp(name, "persistent_load")) {
3966 if (!self->pers_func) {
3967 PyErr_SetString(PyExc_AttributeError, name);
3968 return NULL;
3969 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003970
Guido van Rossum60456fd1997-04-09 17:36:32 +00003971 Py_INCREF(self->pers_func);
3972 return self->pers_func;
3973 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003974
Guido van Rossum60456fd1997-04-09 17:36:32 +00003975 if (!strcmp(name, "memo")) {
3976 if (!self->memo) {
3977 PyErr_SetString(PyExc_AttributeError, name);
3978 return NULL;
3979 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003980
Guido van Rossum60456fd1997-04-09 17:36:32 +00003981 Py_INCREF(self->memo);
3982 return self->memo;
3983 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003984
Guido van Rossum60456fd1997-04-09 17:36:32 +00003985 if (!strcmp(name, "stack")) {
3986 if (!self->stack) {
3987 PyErr_SetString(PyExc_AttributeError, name);
3988 return NULL;
3989 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003990
Guido van Rossum60456fd1997-04-09 17:36:32 +00003991 Py_INCREF(self->stack);
3992 return self->stack;
3993 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003994
Guido van Rossum60456fd1997-04-09 17:36:32 +00003995 if (!strcmp(name, "UnpicklingError")) {
3996 Py_INCREF(UnpicklingError);
3997 return UnpicklingError;
3998 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003999
Guido van Rossum60456fd1997-04-09 17:36:32 +00004000 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
4001}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004002
Guido van Rossum60456fd1997-04-09 17:36:32 +00004003
4004static int
4005Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) {
4006 if (!strcmp(name, "persistent_load")) {
4007 Py_XDECREF(self->pers_func);
4008 self->pers_func = value;
4009 Py_INCREF(value);
4010 return 0;
4011 }
4012
4013 PyErr_SetString(PyExc_AttributeError, name);
4014 return -1;
4015}
4016
4017
4018static PyObject *
4019cpm_dump(PyObject *self, PyObject *args) {
4020 PyObject *ob, *file, *res = NULL;
4021 Picklerobject *pickler = 0;
4022 int bin = 0;
4023
4024 UNLESS(PyArg_ParseTuple(args, "OO|i", &ob, &file, &bin))
4025 goto finally;
4026
4027 UNLESS(pickler = newPicklerobject(file, bin))
4028 goto finally;
4029
4030 if (dump(pickler, ob) < 0)
4031 goto finally;
4032
4033 Py_INCREF(Py_None);
4034 res = Py_None;
4035
4036finally:
4037 Py_XDECREF(pickler);
4038
4039 return res;
4040}
4041
4042
4043static PyObject *
4044cpm_dumps(PyObject *self, PyObject *args) {
4045 PyObject *ob, *file = 0, *res = NULL;
4046 Picklerobject *pickler = 0;
4047 int bin = 0;
4048
4049 UNLESS(PyArg_ParseTuple(args, "O|i", &ob, &bin))
4050 goto finally;
4051
4052 UNLESS(file = PycStringIO->NewOutput(128))
4053 goto finally;
4054
4055 UNLESS(pickler = newPicklerobject(file, bin))
4056 goto finally;
4057
4058 if (dump(pickler, ob) < 0)
4059 goto finally;
4060
4061 res = PycStringIO->cgetvalue(file);
4062
4063finally:
4064 Py_XDECREF(pickler);
4065 Py_XDECREF(file);
4066
4067 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004068}
4069
4070
4071static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004072cpm_load(PyObject *self, PyObject *args) {
4073 Unpicklerobject *unpickler = 0;
4074 PyObject *ob, *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004075
Guido van Rossum60456fd1997-04-09 17:36:32 +00004076 UNLESS(PyArg_ParseTuple(args, "O", &ob))
4077 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004078
Guido van Rossum60456fd1997-04-09 17:36:32 +00004079 UNLESS(unpickler = newUnpicklerobject(ob))
4080 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004081
Guido van Rossum60456fd1997-04-09 17:36:32 +00004082 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004083
Guido van Rossum60456fd1997-04-09 17:36:32 +00004084finally:
4085 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004086
Guido van Rossum60456fd1997-04-09 17:36:32 +00004087 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004088}
4089
4090
4091static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004092cpm_loads(PyObject *self, PyObject *args) {
4093 PyObject *ob, *file = 0, *res = NULL;
4094 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004095
Guido van Rossum60456fd1997-04-09 17:36:32 +00004096 UNLESS(PyArg_ParseTuple(args, "O", &ob))
4097 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004098
Guido van Rossum60456fd1997-04-09 17:36:32 +00004099 UNLESS(file = PycStringIO->NewInput(ob))
4100 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004101
Guido van Rossum60456fd1997-04-09 17:36:32 +00004102 UNLESS(unpickler = newUnpicklerobject(file))
4103 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004104
Guido van Rossum60456fd1997-04-09 17:36:32 +00004105 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004106
Guido van Rossum60456fd1997-04-09 17:36:32 +00004107finally:
4108 Py_XDECREF(file);
4109 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004110
Guido van Rossum60456fd1997-04-09 17:36:32 +00004111 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004112}
4113
4114
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004115static char Unpicklertype__doc__[] =
4116"Objects that know how to unpickle";
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004117
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004118static PyTypeObject Unpicklertype = {
4119 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004120 0, /*ob_size*/
4121 "Unpickler", /*tp_name*/
4122 sizeof(Unpicklerobject), /*tp_basicsize*/
4123 0, /*tp_itemsize*/
4124 /* methods */
4125 (destructor)Unpickler_dealloc, /*tp_dealloc*/
4126 (printfunc)0, /*tp_print*/
4127 (getattrfunc)Unpickler_getattr, /*tp_getattr*/
4128 (setattrfunc)Unpickler_setattr, /*tp_setattr*/
4129 (cmpfunc)0, /*tp_compare*/
4130 (reprfunc)0, /*tp_repr*/
4131 0, /*tp_as_number*/
4132 0, /*tp_as_sequence*/
4133 0, /*tp_as_mapping*/
4134 (hashfunc)0, /*tp_hash*/
4135 (ternaryfunc)0, /*tp_call*/
4136 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004137
Guido van Rossum60456fd1997-04-09 17:36:32 +00004138 /* Space for future expansion */
4139 0L,0L,0L,0L,
4140 Unpicklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004141};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004142
Guido van Rossum60456fd1997-04-09 17:36:32 +00004143static struct PyMethodDef cPickle_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004144 {"dump", (PyCFunction)cpm_dump, 1,
4145 "dump(object, file, [binary]) --"
4146 "Write an object in pickle format to the given file\n"
4147 "\n"
4148 "If the optional argument, binary, is provided and is true, then the\n"
4149 "pickle will be written in binary format, which is more space and\n"
4150 "computationally efficient. \n"
4151 },
4152 {"dumps", (PyCFunction)cpm_dumps, 1,
4153 "dumps(object, [binary]) --"
4154 "Return a string containing an object in pickle format\n"
4155 "\n"
4156 "If the optional argument, binary, is provided and is true, then the\n"
4157 "pickle will be written in binary format, which is more space and\n"
4158 "computationally efficient. \n"
4159 },
4160 {"load", (PyCFunction)cpm_load, 1,
4161 "load(file) -- Load a pickle from the given file"},
4162 {"loads", (PyCFunction)cpm_loads, 1,
4163 "loads(string) -- Load a pickle from the given string"},
4164 {"Pickler", (PyCFunction)get_Pickler, 1,
4165 "Pickler(file, [binary]) -- Create a pickler\n"
4166 "\n"
4167 "If the optional argument, binary, is provided and is true, then\n"
4168 "pickles will be written in binary format, which is more space and\n"
4169 "computationally efficient. \n"
4170 },
4171 {"Unpickler", (PyCFunction)get_Unpickler, 1,
4172 "Unpickler(file) -- Create an unpickler"},
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004173 { NULL, NULL }
4174};
4175
4176
Guido van Rossum60456fd1997-04-09 17:36:32 +00004177#define CHECK_FOR_ERRORS(MESS) \
4178if(PyErr_Occurred()) { \
4179 PyObject *__sys_exc_type, *__sys_exc_value, *__sys_exc_traceback; \
4180 PyErr_Fetch( &__sys_exc_type, &__sys_exc_value, &__sys_exc_traceback); \
4181 fprintf(stderr, # MESS ":\n\t"); \
4182 PyObject_Print(__sys_exc_type, stderr,0); \
4183 fprintf(stderr,", "); \
4184 PyObject_Print(__sys_exc_value, stderr,0); \
4185 fprintf(stderr,"\n"); \
4186 fflush(stderr); \
4187 Py_FatalError(# MESS); \
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004188}
4189
4190
Guido van Rossum60456fd1997-04-09 17:36:32 +00004191static int
4192init_stuff(PyObject *module, PyObject *module_dict) {
4193 PyObject *string, *copy_reg;
4194
4195#define INIT_STR(S) UNLESS(S ## _str=PyString_FromString(#S)) return -1;
4196
4197 INIT_STR(__class__);
4198 INIT_STR(__getinitargs__);
4199 INIT_STR(__dict__);
4200 INIT_STR(__getstate__);
4201 INIT_STR(__setstate__);
4202 INIT_STR(__name__);
Guido van Rossum142eeb81997-08-13 03:14:41 +00004203 INIT_STR(__main__);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004204 INIT_STR(__reduce__);
4205 INIT_STR(write);
4206 INIT_STR(__safe_for_unpickling__);
4207 INIT_STR(append);
4208 INIT_STR(read);
4209 INIT_STR(readline);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004210 INIT_STR(copy_reg);
4211 INIT_STR(dispatch_table);
4212 INIT_STR(safe_constructors);
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004213 INIT_STR(__basicnew__);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004214
4215 UNLESS(copy_reg = PyImport_ImportModule("copy_reg"))
4216 return -1;
4217
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004218 /* These next few are special because we want to use different
4219 ones in restricted mode. */
4220
4221 UNLESS(dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004222 return -1;
4223
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004224 UNLESS(safe_constructors = PyObject_GetAttr(copy_reg,
4225 safe_constructors_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004226 return -1;
4227
4228 Py_DECREF(copy_reg);
4229
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004230 /* Down to here ********************************** */
4231
Guido van Rossum60456fd1997-04-09 17:36:32 +00004232 UNLESS(string = PyImport_ImportModule("string"))
4233 return -1;
4234
4235 UNLESS(atol_func = PyObject_GetAttrString(string, "atol"))
4236 return -1;
4237
4238 Py_DECREF(string);
4239
4240 UNLESS(empty_tuple = PyTuple_New(0))
4241 return -1;
4242
Guido van Rossum60456fd1997-04-09 17:36:32 +00004243 UNLESS(PicklingError = PyString_FromString("cPickle.PicklingError"))
4244 return -1;
4245
4246 if (PyDict_SetItemString(module_dict, "PicklingError",
4247 PicklingError) < 0)
4248 return -1;
4249
4250 UNLESS(UnpicklingError = PyString_FromString("cPickle.UnpicklingError"))
4251 return -1;
4252
4253 if (PyDict_SetItemString(module_dict, "UnpicklingError",
4254 UnpicklingError) < 0)
4255 return -1;
4256
4257 PycString_IMPORT;
4258
4259 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004260}
4261
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004262void
Guido van Rossum60456fd1997-04-09 17:36:32 +00004263initcPickle() {
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004264 PyObject *m, *d, *v;
Guido van Rossume2d81cd1998-08-08 19:40:10 +00004265 char *rev="1.53";
Guido van Rossum60456fd1997-04-09 17:36:32 +00004266 PyObject *format_version;
4267 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004268
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004269 Picklertype.ob_type = &PyType_Type;
4270 Unpicklertype.ob_type = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004271
Guido van Rossum60456fd1997-04-09 17:36:32 +00004272 /* Create the module and add the functions */
4273 m = Py_InitModule4("cPickle", cPickle_methods,
4274 cPickle_module_documentation,
4275 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004276
Guido van Rossum60456fd1997-04-09 17:36:32 +00004277 /* Add some symbolic constants to the module */
4278 d = PyModule_GetDict(m);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004279 PyDict_SetItemString(d,"__version__", v = PyString_FromString(rev));
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004280 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00004281
Guido van Rossum60456fd1997-04-09 17:36:32 +00004282#ifdef FORMAT_1_3
4283 format_version = PyString_FromString("1.3");
4284 compatible_formats = Py_BuildValue("[sss]", "1.0", "1.1", "1.2");
4285#else
4286 format_version = PyString_FromString("1.2");
4287 compatible_formats = Py_BuildValue("[ss]", "1.0", "1.1");
4288#endif
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004289
Guido van Rossum60456fd1997-04-09 17:36:32 +00004290 PyDict_SetItemString(d, "format_version", format_version);
4291 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004292 Py_XDECREF(format_version);
4293 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004294
Guido van Rossum60456fd1997-04-09 17:36:32 +00004295 init_stuff(m, d);
4296 CHECK_FOR_ERRORS("can't initialize module cPickle");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004297}