blob: 405456d480550181ec0781b62f2b23da36d82715 [file] [log] [blame]
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001/*
Guido van Rossum2f80d961999-07-13 15:18:58 +00002 * cPickle.c,v 1.71 1999/07/11 13:30:34 jim Exp
Tim Peters84e87f32001-03-17 04:50:51 +00003 *
4 * Copyright (c) 1996-1998, Digital Creations, Fredericksburg, VA, USA.
Guido van Rossum053b8df1998-11-25 16:18:00 +00005 * All rights reserved.
Tim Peters84e87f32001-03-17 04:50:51 +00006 *
Guido van Rossum053b8df1998-11-25 16:18:00 +00007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
Tim Peters84e87f32001-03-17 04:50:51 +000010 *
Guido van Rossum053b8df1998-11-25 16:18:00 +000011 * o Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the disclaimer that follows.
Tim Peters84e87f32001-03-17 04:50:51 +000013 *
Guido van Rossum053b8df1998-11-25 16:18:00 +000014 * o Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions, and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
Tim Peters84e87f32001-03-17 04:50:51 +000018 *
Guido van Rossum053b8df1998-11-25 16:18:00 +000019 * o Neither the name of Digital Creations nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
Tim Peters84e87f32001-03-17 04:50:51 +000022 *
23 *
Guido van Rossum053b8df1998-11-25 16:18:00 +000024 * THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS AND CONTRIBUTORS *AS
25 * IS* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL
28 * CREATIONS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
31 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
33 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
34 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
35 * DAMAGE.
Tim Peters84e87f32001-03-17 04:50:51 +000036 *
37 #
Guido van Rossum053b8df1998-11-25 16:18:00 +000038 # If you have questions regarding this software, contact:
39 #
40 # Digital Creations, L.C.
41 # 910 Princess Ann Street
42 # Fredericksburge, Virginia 22401
43 #
44 # info@digicool.com
45 #
46 # (540) 371-6909
47 */
Guido van Rossum2f4caa41997-01-06 22:59:08 +000048
Tim Peters84e87f32001-03-17 04:50:51 +000049static char cPickle_module_documentation[] =
Guido van Rossum142eeb81997-08-13 03:14:41 +000050"C implementation and optimization of the Python pickle module\n"
51"\n"
Guido van Rossum2f80d961999-07-13 15:18:58 +000052"cPickle.c,v 1.71 1999/07/11 13:30:34 jim Exp\n"
Guido van Rossum2f4caa41997-01-06 22:59:08 +000053;
54
55#include "Python.h"
56#include "cStringIO.h"
Guido van Rossum2f4caa41997-01-06 22:59:08 +000057
Guido van Rossum142eeb81997-08-13 03:14:41 +000058#ifndef Py_eval_input
59#include <graminit.h>
60#define Py_eval_input eval_input
Guido van Rossumc6ef2041997-08-21 02:30:45 +000061#endif /* Py_eval_input */
Guido van Rossum142eeb81997-08-13 03:14:41 +000062
Guido van Rossum2f4caa41997-01-06 22:59:08 +000063#include <errno.h>
64
Guido van Rossum2f4caa41997-01-06 22:59:08 +000065#define UNLESS(E) if (!(E))
Guido van Rossum2f4caa41997-01-06 22:59:08 +000066
Guido van Rossum60456fd1997-04-09 17:36:32 +000067#define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL))
Guido van Rossum2f4caa41997-01-06 22:59:08 +000068
Guido van Rossum60456fd1997-04-09 17:36:32 +000069#define WRITE_BUF_SIZE 256
70
71
72#define MARK '('
73#define STOP '.'
74#define POP '0'
75#define POP_MARK '1'
76#define DUP '2'
77#define FLOAT 'F'
Guido van Rossum60456fd1997-04-09 17:36:32 +000078#define BINFLOAT 'G'
Guido van Rossum60456fd1997-04-09 17:36:32 +000079#define INT 'I'
80#define BININT 'J'
81#define BININT1 'K'
82#define LONG 'L'
83#define BININT2 'M'
84#define NONE 'N'
85#define PERSID 'P'
86#define BINPERSID 'Q'
87#define REDUCE 'R'
88#define STRING 'S'
89#define BINSTRING 'T'
Guido van Rossum2f4caa41997-01-06 22:59:08 +000090#define SHORT_BINSTRING 'U'
Guido van Rossum5fccb7c2000-03-10 23:11:40 +000091#define UNICODE 'V'
92#define BINUNICODE 'X'
Guido van Rossum60456fd1997-04-09 17:36:32 +000093#define APPEND 'a'
94#define BUILD 'b'
95#define GLOBAL 'c'
96#define DICT 'd'
97#define EMPTY_DICT '}'
98#define APPENDS 'e'
99#define GET 'g'
100#define BINGET 'h'
101#define INST 'i'
102#define LONG_BINGET 'j'
103#define LIST 'l'
104#define EMPTY_LIST ']'
105#define OBJ 'o'
106#define PUT 'p'
107#define BINPUT 'q'
108#define LONG_BINPUT 'r'
109#define SETITEM 's'
110#define TUPLE 't'
111#define EMPTY_TUPLE ')'
112#define SETITEMS 'u'
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000113
Guido van Rossum60456fd1997-04-09 17:36:32 +0000114static char MARKv = MARK;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000115
116/* atol function from string module */
117static PyObject *atol_func;
118
Guido van Rossumc03158b1999-06-09 15:23:31 +0000119static PyObject *PickleError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000120static PyObject *PicklingError;
Guido van Rossumc03158b1999-06-09 15:23:31 +0000121static PyObject *UnpickleableError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000122static PyObject *UnpicklingError;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000123static PyObject *BadPickleGet;
124
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000125
Guido van Rossum60456fd1997-04-09 17:36:32 +0000126static PyObject *dispatch_table;
127static PyObject *safe_constructors;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000128static PyObject *empty_tuple;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000129
Guido van Rossum60456fd1997-04-09 17:36:32 +0000130static PyObject *__class___str, *__getinitargs___str, *__dict___str,
131 *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
132 *write_str, *__safe_for_unpickling___str, *append_str,
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000133 *read_str, *readline_str, *__main___str, *__basicnew___str,
Guido van Rossum053b8df1998-11-25 16:18:00 +0000134 *copy_reg_str, *dispatch_table_str, *safe_constructors_str, *empty_str;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000135
Guido van Rossum053b8df1998-11-25 16:18:00 +0000136#ifndef PyList_SET_ITEM
137#define PyList_SET_ITEM(op, i, v) (((PyListObject *)(op))->ob_item[i] = (v))
138#endif
139#ifndef PyList_GET_SIZE
140#define PyList_GET_SIZE(op) (((PyListObject *)(op))->ob_size)
141#endif
142#ifndef PyTuple_SET_ITEM
143#define PyTuple_SET_ITEM(op, i, v) (((PyTupleObject *)(op))->ob_item[i] = (v))
144#endif
145#ifndef PyTuple_GET_SIZE
146#define PyTuple_GET_SIZE(op) (((PyTupleObject *)(op))->ob_size)
147#endif
148#ifndef PyString_GET_SIZE
149#define PyString_GET_SIZE(op) (((PyStringObject *)(op))->ob_size)
150#endif
151
152/*************************************************************************
153 Internal Data type for pickle data. */
154
155typedef struct {
156 PyObject_HEAD
157 int length, size;
158 PyObject **data;
159} Pdata;
160
Tim Peters84e87f32001-03-17 04:50:51 +0000161static void
Guido van Rossum053b8df1998-11-25 16:18:00 +0000162Pdata_dealloc(Pdata *self) {
163 int i;
164 PyObject **p;
165
166 for (i=self->length, p=self->data; --i >= 0; p++) Py_DECREF(*p);
167
168 if (self->data) free(self->data);
169
Guido van Rossumb18618d2000-05-03 23:44:39 +0000170 PyObject_Del(self);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000171}
172
173static PyTypeObject PdataType = {
174 PyObject_HEAD_INIT(NULL) 0, "Pdata", sizeof(Pdata), 0,
175 (destructor)Pdata_dealloc,
176 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
177};
178
179#define Pdata_Check(O) ((O)->ob_type == &PdataType)
180
181static PyObject *
Thomas Wouters58d05102000-07-24 14:43:35 +0000182Pdata_New(void) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000183 Pdata *self;
184
Guido van Rossumb18618d2000-05-03 23:44:39 +0000185 UNLESS (self = PyObject_New(Pdata, &PdataType)) return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000186 self->size=8;
187 self->length=0;
188 self->data=malloc(self->size * sizeof(PyObject*));
189 if (self->data) return (PyObject*)self;
190 Py_DECREF(self);
191 return PyErr_NoMemory();
192}
193
Tim Peters84e87f32001-03-17 04:50:51 +0000194static int
Thomas Wouters58d05102000-07-24 14:43:35 +0000195stackUnderflow(void) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000196 PyErr_SetString(UnpicklingError, "unpickling stack underflow");
197 return -1;
198}
199
200static int
201Pdata_clear(Pdata *self, int clearto) {
202 int i;
203 PyObject **p;
204
205 if (clearto < 0) return stackUnderflow();
206 if (clearto >= self->length) return 0;
207
208 for (i=self->length, p=self->data+clearto; --i >= clearto; p++)
209 Py_DECREF(*p);
210 self->length=clearto;
211
212 return 0;
213}
214
215
Tim Peters84e87f32001-03-17 04:50:51 +0000216static int
Guido van Rossum053b8df1998-11-25 16:18:00 +0000217Pdata_grow(Pdata *self) {
218 if (! self->size) {
219 PyErr_NoMemory();
220 return -1;
221 }
Tim Peters84e87f32001-03-17 04:50:51 +0000222 self->size *= 2;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000223 self->data = realloc(self->data, self->size*sizeof(PyObject*));
224 if (! self->data) {
Tim Peters84e87f32001-03-17 04:50:51 +0000225 self->size = 0;
226 PyErr_NoMemory();
227 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000228 }
229 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +0000230}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000231
232#define PDATA_POP(D,V) { \
233 if ((D)->length) V=D->data[--((D)->length)]; \
234 else { \
235 PyErr_SetString(UnpicklingError, "bad pickle data"); \
236 V=NULL; \
237 } \
238}
239
240
241static PyObject *
242Pdata_popTuple(Pdata *self, int start) {
243 PyObject *r;
244 int i, j, l;
245
246 l=self->length-start;
247 UNLESS (r=PyTuple_New(l)) return NULL;
Guido van Rossumea2b7152000-05-09 18:14:50 +0000248 for (i=start, j=0 ; j < l; i++, j++)
249 PyTuple_SET_ITEM(r, j, self->data[i]);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000250
251 self->length=start;
252 return r;
253}
254
255static PyObject *
256Pdata_popList(Pdata *self, int start) {
257 PyObject *r;
258 int i, j, l;
259
260 l=self->length-start;
261 UNLESS (r=PyList_New(l)) return NULL;
Guido van Rossumea2b7152000-05-09 18:14:50 +0000262 for (i=start, j=0 ; j < l; i++, j++)
263 PyList_SET_ITEM(r, j, self->data[i]);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000264
265 self->length=start;
266 return r;
267}
268
269#define PDATA_APPEND_(D,O,ER) { \
270 if (Pdata_Append(((Pdata*)(D)), O) < 0) return ER; \
271}
272
273#define PDATA_APPEND(D,O,ER) { \
274 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
275 Pdata_grow((Pdata*)(D)) < 0) \
276 return ER; \
277 Py_INCREF(O); \
278 ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O; \
279}
280
281#define PDATA_PUSH(D,O,ER) { \
282 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
283 Pdata_grow((Pdata*)(D)) < 0) { \
284 Py_DECREF(O); \
285 return ER; \
286 } \
287 ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O; \
288}
289
290/*************************************************************************/
291
292#define ARG_TUP(self, o) { \
293 if (self->arg || (self->arg=PyTuple_New(1))) { \
294 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
295 PyTuple_SET_ITEM(self->arg,0,o); \
296 } \
297 else { \
298 Py_DECREF(o); \
299 } \
300}
301
302#define FREE_ARG_TUP(self) { \
303 if (self->arg->ob_refcnt > 1) { \
304 Py_DECREF(self->arg); \
305 self->arg=NULL; \
306 } \
307 }
308
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000309typedef struct Picklerobject {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000310 PyObject_HEAD
311 FILE *fp;
312 PyObject *write;
313 PyObject *file;
314 PyObject *memo;
315 PyObject *arg;
316 PyObject *pers_func;
317 PyObject *inst_pers_func;
318 int bin;
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000319 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000320 int (*write_func)(struct Picklerobject *, char *, int);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000321 char *write_buf;
322 int buf_size;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000323 PyObject *dispatch_table;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000324} Picklerobject;
325
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000326staticforward PyTypeObject Picklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000327
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000328typedef struct Unpicklerobject {
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000329 PyObject_HEAD
330 FILE *fp;
331 PyObject *file;
332 PyObject *readline;
333 PyObject *read;
334 PyObject *memo;
335 PyObject *arg;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000336 Pdata *stack;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000337 PyObject *mark;
338 PyObject *pers_func;
339 PyObject *last_string;
340 int *marks;
341 int num_marks;
342 int marks_size;
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000343 int (*read_func)(struct Unpicklerobject *, char **, int);
344 int (*readline_func)(struct Unpicklerobject *, char **);
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000345 int buf_size;
346 char *buf;
347 PyObject *safe_constructors;
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +0000348 PyObject *find_class;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000349} Unpicklerobject;
Tim Peters84e87f32001-03-17 04:50:51 +0000350
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000351staticforward PyTypeObject Unpicklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000352
Thomas Wouters4789b3a2000-07-24 11:36:47 +0000353/* Forward decls that need the above structs */
354static int save(Picklerobject *, PyObject *, int);
355static int put2(Picklerobject *, PyObject *);
356
Tim Peters84e87f32001-03-17 04:50:51 +0000357int
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000358cPickle_PyMapping_HasKey(PyObject *o, PyObject *key) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000359 PyObject *v;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000360
Guido van Rossum053b8df1998-11-25 16:18:00 +0000361 if ((v = PyObject_GetItem(o,key))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000362 Py_DECREF(v);
363 return 1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000364 }
365
Guido van Rossum60456fd1997-04-09 17:36:32 +0000366 PyErr_Clear();
367 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000368}
369
Guido van Rossumd385d591997-04-09 17:47:47 +0000370static
Guido van Rossum60456fd1997-04-09 17:36:32 +0000371PyObject *
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000372cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
373{
Guido van Rossum60456fd1997-04-09 17:36:32 +0000374 va_list va;
375 PyObject *args=0, *retval=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000376 va_start(va, format);
Tim Peters84e87f32001-03-17 04:50:51 +0000377
Guido van Rossum053b8df1998-11-25 16:18:00 +0000378 if (format) args = Py_VaBuildValue(format, va);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000379 va_end(va);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000380 if (format && ! args) return NULL;
381 if (stringformat && !(retval=PyString_FromString(stringformat))) return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000382
Guido van Rossum053b8df1998-11-25 16:18:00 +0000383 if (retval) {
384 if (args) {
385 PyObject *v;
386 v=PyString_Format(retval, args);
387 Py_DECREF(retval);
388 Py_DECREF(args);
389 if (! v) return NULL;
390 retval=v;
391 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000392 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000393 else
Guido van Rossum053b8df1998-11-25 16:18:00 +0000394 if (args) retval=args;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000395 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000396 PyErr_SetObject(ErrType,Py_None);
397 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000398 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000399 PyErr_SetObject(ErrType,retval);
400 Py_DECREF(retval);
401 return NULL;
402}
403
Tim Peters84e87f32001-03-17 04:50:51 +0000404static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000405write_file(Picklerobject *self, char *s, int n) {
Tim Peters84e87f32001-03-17 04:50:51 +0000406 size_t nbyteswritten;
407
Guido van Rossum60456fd1997-04-09 17:36:32 +0000408 if (s == NULL) {
409 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000410 }
411
Tim Peters84e87f32001-03-17 04:50:51 +0000412 Py_BEGIN_ALLOW_THREADS
413 nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
414 Py_END_ALLOW_THREADS
415 if (nbyteswritten != (size_t)n) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000416 PyErr_SetFromErrno(PyExc_IOError);
417 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000418 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000419
420 return n;
421}
422
Tim Peters84e87f32001-03-17 04:50:51 +0000423static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000424write_cStringIO(Picklerobject *self, char *s, int n) {
425 if (s == NULL) {
426 return 0;
427 }
428
429 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
430 return -1;
431 }
432
433 return n;
434}
435
Tim Peters84e87f32001-03-17 04:50:51 +0000436static int
Guido van Rossum142eeb81997-08-13 03:14:41 +0000437write_none(Picklerobject *self, char *s, int n) {
438 if (s == NULL) return 0;
439 return n;
440}
441
Tim Peters84e87f32001-03-17 04:50:51 +0000442static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000443write_other(Picklerobject *self, char *s, int n) {
444 PyObject *py_str = 0, *junk = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000445
446 if (s == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000447 UNLESS (self->buf_size) return 0;
Tim Peters84e87f32001-03-17 04:50:51 +0000448 UNLESS (py_str =
Guido van Rossum60456fd1997-04-09 17:36:32 +0000449 PyString_FromStringAndSize(self->write_buf, self->buf_size))
Guido van Rossum053b8df1998-11-25 16:18:00 +0000450 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000451 }
452 else {
453 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
454 if (write_other(self, NULL, 0) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +0000455 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000456 }
457
Tim Peters84e87f32001-03-17 04:50:51 +0000458 if (n > WRITE_BUF_SIZE) {
459 UNLESS (py_str =
Guido van Rossum60456fd1997-04-09 17:36:32 +0000460 PyString_FromStringAndSize(s, n))
Guido van Rossum053b8df1998-11-25 16:18:00 +0000461 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000462 }
463 else {
464 memcpy(self->write_buf + self->buf_size, s, n);
465 self->buf_size += n;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000466 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000467 }
468 }
469
Guido van Rossum053b8df1998-11-25 16:18:00 +0000470 if (self->write) {
471 /* object with write method */
472 ARG_TUP(self, py_str);
473 if (self->arg) {
474 junk = PyObject_CallObject(self->write, self->arg);
475 FREE_ARG_TUP(self);
476 }
477 if (junk) Py_DECREF(junk);
478 else return -1;
479 }
Tim Peters84e87f32001-03-17 04:50:51 +0000480 else
Guido van Rossum053b8df1998-11-25 16:18:00 +0000481 PDATA_PUSH(self->file, py_str, -1);
Tim Peters84e87f32001-03-17 04:50:51 +0000482
483 self->buf_size = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000484 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000485}
486
487
Tim Peters84e87f32001-03-17 04:50:51 +0000488static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000489read_file(Unpicklerobject *self, char **s, int n) {
Tim Peters84e87f32001-03-17 04:50:51 +0000490 size_t nbytesread;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000491
492 if (self->buf_size == 0) {
493 int size;
494
Tim Peters84e87f32001-03-17 04:50:51 +0000495 size = ((n < 32) ? 32 : n);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000496 UNLESS (self->buf = (char *)malloc(size * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000497 PyErr_NoMemory();
498 return -1;
499 }
500
501 self->buf_size = size;
502 }
503 else if (n > self->buf_size) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000504 UNLESS (self->buf = (char *)realloc(self->buf, n * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000505 PyErr_NoMemory();
506 return -1;
507 }
Tim Peters84e87f32001-03-17 04:50:51 +0000508
Guido van Rossum60456fd1997-04-09 17:36:32 +0000509 self->buf_size = n;
510 }
Tim Peters84e87f32001-03-17 04:50:51 +0000511
512 Py_BEGIN_ALLOW_THREADS
513 nbytesread = fread(self->buf, sizeof(char), n, self->fp);
514 Py_END_ALLOW_THREADS
515 if (nbytesread != (size_t)n) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000516 if (feof(self->fp)) {
517 PyErr_SetNone(PyExc_EOFError);
518 return -1;
519 }
520
521 PyErr_SetFromErrno(PyExc_IOError);
522 return -1;
523 }
524
525 *s = self->buf;
526
527 return n;
528}
529
530
Tim Peters84e87f32001-03-17 04:50:51 +0000531static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000532readline_file(Unpicklerobject *self, char **s) {
533 int i;
534
535 if (self->buf_size == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000536 UNLESS (self->buf = (char *)malloc(40 * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000537 PyErr_NoMemory();
538 return -1;
539 }
Tim Peters84e87f32001-03-17 04:50:51 +0000540
Guido van Rossum60456fd1997-04-09 17:36:32 +0000541 self->buf_size = 40;
542 }
543
544 i = 0;
545 while (1) {
546 for (; i < (self->buf_size - 1); i++) {
547 if (feof(self->fp) || (self->buf[i] = getc(self->fp)) == '\n') {
548 self->buf[i + 1] = '\0';
549 *s = self->buf;
550 return i + 1;
551 }
552 }
553
Tim Peters84e87f32001-03-17 04:50:51 +0000554 UNLESS (self->buf = (char *)realloc(self->buf,
Guido van Rossum60456fd1997-04-09 17:36:32 +0000555 (self->buf_size * 2) * sizeof(char))) {
556 PyErr_NoMemory();
557 return -1;
558 }
559
560 self->buf_size *= 2;
561 }
562
Tim Peters84e87f32001-03-17 04:50:51 +0000563}
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000564
565
Tim Peters84e87f32001-03-17 04:50:51 +0000566static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000567read_cStringIO(Unpicklerobject *self, char **s, int n) {
568 char *ptr;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000569
Guido van Rossum60456fd1997-04-09 17:36:32 +0000570 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
571 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000572 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000573 }
574
Guido van Rossum60456fd1997-04-09 17:36:32 +0000575 *s = ptr;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000576
Guido van Rossum60456fd1997-04-09 17:36:32 +0000577 return n;
578}
579
580
Tim Peters84e87f32001-03-17 04:50:51 +0000581static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000582readline_cStringIO(Unpicklerobject *self, char **s) {
583 int n;
584 char *ptr;
585
586 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
587 return -1;
588 }
589
590 *s = ptr;
591
592 return n;
593}
594
595
Tim Peters84e87f32001-03-17 04:50:51 +0000596static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000597read_other(Unpicklerobject *self, char **s, int n) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000598 PyObject *bytes, *str=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000599
Guido van Rossum053b8df1998-11-25 16:18:00 +0000600 UNLESS (bytes = PyInt_FromLong(n)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000601
Guido van Rossum053b8df1998-11-25 16:18:00 +0000602 ARG_TUP(self, bytes);
603 if (self->arg) {
604 str = PyObject_CallObject(self->read, self->arg);
605 FREE_ARG_TUP(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000606 }
Guido van Rossum053b8df1998-11-25 16:18:00 +0000607 if (! str) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000608
609 Py_XDECREF(self->last_string);
610 self->last_string = str;
611
Guido van Rossum053b8df1998-11-25 16:18:00 +0000612 if (! (*s = PyString_AsString(str))) return -1;
613 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000614}
615
616
Tim Peters84e87f32001-03-17 04:50:51 +0000617static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000618readline_other(Unpicklerobject *self, char **s) {
619 PyObject *str;
620 int str_size;
621
Guido van Rossum053b8df1998-11-25 16:18:00 +0000622 UNLESS (str = PyObject_CallObject(self->readline, empty_tuple)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000623 return -1;
624 }
625
Guido van Rossum053b8df1998-11-25 16:18:00 +0000626 if ((str_size = PyString_Size(str)) < 0)
627 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000628
629 Py_XDECREF(self->last_string);
630 self->last_string = str;
631
Guido van Rossum053b8df1998-11-25 16:18:00 +0000632 if (! (*s = PyString_AsString(str)))
633 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000634
635 return str_size;
636}
637
638
639static char *
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000640pystrndup(char *s, int l) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000641 char *r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000642 UNLESS (r=malloc((l+1)*sizeof(char))) return (char*)PyErr_NoMemory();
Guido van Rossum60456fd1997-04-09 17:36:32 +0000643 memcpy(r,s,l);
644 r[l]=0;
645 return r;
646}
647
648
649static int
650get(Picklerobject *self, PyObject *id) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000651 PyObject *value, *mv;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000652 long c_value;
653 char s[30];
Guido van Rossum534b7c52000-06-28 22:23:56 +0000654 size_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000655
Guido van Rossum053b8df1998-11-25 16:18:00 +0000656 UNLESS (mv = PyDict_GetItem(self->memo, id)) {
657 PyErr_SetObject(PyExc_KeyError, id);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000658 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000659 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000660
Guido van Rossum053b8df1998-11-25 16:18:00 +0000661 UNLESS (value = PyTuple_GetItem(mv, 0))
Guido van Rossum60456fd1997-04-09 17:36:32 +0000662 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000663
Guido van Rossum053b8df1998-11-25 16:18:00 +0000664 UNLESS (PyInt_Check(value)) {
665 PyErr_SetString(PicklingError, "no int where int expected in memo");
666 return -1;
667 }
668 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000669
670 if (!self->bin) {
671 s[0] = GET;
672 sprintf(s + 1, "%ld\n", c_value);
673 len = strlen(s);
674 }
Guido van Rossum053b8df1998-11-25 16:18:00 +0000675 else if (Pdata_Check(self->file)) {
676 if (write_other(self, NULL, 0) < 0) return -1;
677 PDATA_APPEND(self->file, mv, -1);
678 return 0;
679 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000680 else {
681 if (c_value < 256) {
682 s[0] = BINGET;
683 s[1] = (int)(c_value & 0xff);
684 len = 2;
685 }
686 else {
687 s[0] = LONG_BINGET;
688 s[1] = (int)(c_value & 0xff);
689 s[2] = (int)((c_value >> 8) & 0xff);
690 s[3] = (int)((c_value >> 16) & 0xff);
691 s[4] = (int)((c_value >> 24) & 0xff);
692 len = 5;
693 }
694 }
695
696 if ((*self->write_func)(self, s, len) < 0)
697 return -1;
698
699 return 0;
700}
Tim Peters84e87f32001-03-17 04:50:51 +0000701
Guido van Rossum60456fd1997-04-09 17:36:32 +0000702
703static int
704put(Picklerobject *self, PyObject *ob) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000705 if (ob->ob_refcnt < 2 || self->fast)
Guido van Rossum60456fd1997-04-09 17:36:32 +0000706 return 0;
707
708 return put2(self, ob);
709}
710
Tim Peters84e87f32001-03-17 04:50:51 +0000711
Guido van Rossum60456fd1997-04-09 17:36:32 +0000712static int
713put2(Picklerobject *self, PyObject *ob) {
714 char c_str[30];
Guido van Rossum534b7c52000-06-28 22:23:56 +0000715 int p;
716 size_t len;
717 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000718 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000719
720 if (self->fast) return 0;
721
Guido van Rossum60456fd1997-04-09 17:36:32 +0000722 if ((p = PyDict_Size(self->memo)) < 0)
723 goto finally;
724
Guido van Rossum053b8df1998-11-25 16:18:00 +0000725 p++; /* Make sure memo keys are positive! */
726
Guido van Rossum534b7c52000-06-28 22:23:56 +0000727 UNLESS (py_ob_id = PyLong_FromVoidPtr(ob))
Guido van Rossum053b8df1998-11-25 16:18:00 +0000728 goto finally;
729
730 UNLESS (memo_len = PyInt_FromLong(p))
731 goto finally;
732
733 UNLESS (t = PyTuple_New(2))
734 goto finally;
735
736 PyTuple_SET_ITEM(t, 0, memo_len);
737 Py_INCREF(memo_len);
738 PyTuple_SET_ITEM(t, 1, ob);
739 Py_INCREF(ob);
740
741 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
742 goto finally;
743
Guido van Rossum60456fd1997-04-09 17:36:32 +0000744 if (!self->bin) {
745 c_str[0] = PUT;
746 sprintf(c_str + 1, "%d\n", p);
747 len = strlen(c_str);
748 }
Guido van Rossum053b8df1998-11-25 16:18:00 +0000749 else if (Pdata_Check(self->file)) {
750 if (write_other(self, NULL, 0) < 0) return -1;
751 PDATA_APPEND(self->file, memo_len, -1);
752 res=0; /* Job well done ;) */
753 goto finally;
754 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000755 else {
756 if (p >= 256) {
757 c_str[0] = LONG_BINPUT;
758 c_str[1] = (int)(p & 0xff);
759 c_str[2] = (int)((p >> 8) & 0xff);
760 c_str[3] = (int)((p >> 16) & 0xff);
761 c_str[4] = (int)((p >> 24) & 0xff);
762 len = 5;
763 }
764 else {
765 c_str[0] = BINPUT;
766 c_str[1] = p;
Tim Peters84e87f32001-03-17 04:50:51 +0000767 len = 2;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000768 }
769 }
770
771 if ((*self->write_func)(self, c_str, len) < 0)
772 goto finally;
773
Guido van Rossum60456fd1997-04-09 17:36:32 +0000774 res = 0;
775
776finally:
777 Py_XDECREF(py_ob_id);
778 Py_XDECREF(memo_len);
779 Py_XDECREF(t);
780
781 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000782}
783
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000784#define PyImport_Import cPickle_Import
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000785
786static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000787PyImport_Import(PyObject *module_name) {
788 static PyObject *silly_list=0, *__builtins___str=0, *__import___str;
789 static PyObject *standard_builtins=0;
790 PyObject *globals=0, *__import__=0, *__builtins__=0, *r=0;
791
Guido van Rossum053b8df1998-11-25 16:18:00 +0000792 UNLESS (silly_list) {
793 UNLESS (__import___str=PyString_FromString("__import__"))
794 return NULL;
795 UNLESS (__builtins___str=PyString_FromString("__builtins__"))
796 return NULL;
797 UNLESS (silly_list=Py_BuildValue("[s]","__doc__"))
798 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000799 }
800
Guido van Rossum053b8df1998-11-25 16:18:00 +0000801 if ((globals=PyEval_GetGlobals())) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000802 Py_INCREF(globals);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000803 UNLESS (__builtins__=PyObject_GetItem(globals,__builtins___str))
804 goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000805 }
806 else {
807 PyErr_Clear();
808
Guido van Rossum053b8df1998-11-25 16:18:00 +0000809 UNLESS (standard_builtins ||
810 (standard_builtins=PyImport_ImportModule("__builtin__")))
811 return NULL;
Tim Peters84e87f32001-03-17 04:50:51 +0000812
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000813 __builtins__=standard_builtins;
814 Py_INCREF(__builtins__);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000815 UNLESS (globals = Py_BuildValue("{sO}", "__builtins__", __builtins__))
816 goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000817 }
818
Guido van Rossum053b8df1998-11-25 16:18:00 +0000819 if (PyDict_Check(__builtins__)) {
820 UNLESS (__import__=PyObject_GetItem(__builtins__,__import___str)) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000821 }
822 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000823 UNLESS (__import__=PyObject_GetAttr(__builtins__,__import___str)) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000824 }
825
Guido van Rossum053b8df1998-11-25 16:18:00 +0000826 UNLESS (r=PyObject_CallFunction(__import__,"OOOO",
827 module_name, globals, globals, silly_list))
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000828 goto err;
829
830 Py_DECREF(globals);
831 Py_DECREF(__builtins__);
832 Py_DECREF(__import__);
Tim Peters84e87f32001-03-17 04:50:51 +0000833
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000834 return r;
835err:
836 Py_XDECREF(globals);
837 Py_XDECREF(__builtins__);
838 Py_XDECREF(__import__);
839 return NULL;
840}
841
842static PyObject *
Guido van Rossume2d81cd1998-08-08 19:40:10 +0000843whichmodule(PyObject *global, PyObject *global_name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000844 int i, j;
845 PyObject *module = 0, *modules_dict = 0,
846 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000847
Guido van Rossum45188231997-09-28 05:38:51 +0000848 module = PyObject_GetAttrString(global, "__module__");
849 if (module) return module;
850 PyErr_Clear();
851
Guido van Rossum053b8df1998-11-25 16:18:00 +0000852 UNLESS (modules_dict = PySys_GetObject("modules"))
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000853 return NULL;
854
Guido van Rossum60456fd1997-04-09 17:36:32 +0000855 i = 0;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000856 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
857
Guido van Rossum053b8df1998-11-25 16:18:00 +0000858 if (PyObject_Compare(name, __main___str)==0) continue;
Tim Peters84e87f32001-03-17 04:50:51 +0000859
Guido van Rossum053b8df1998-11-25 16:18:00 +0000860 UNLESS (global_name_attr = PyObject_GetAttr(module, global_name)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000861 PyErr_Clear();
862 continue;
863 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000864
Guido van Rossum60456fd1997-04-09 17:36:32 +0000865 if (global_name_attr != global) {
866 Py_DECREF(global_name_attr);
867 continue;
868 }
869
870 Py_DECREF(global_name_attr);
871
872 break;
873 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000874
875 /* The following implements the rule in pickle.py added in 1.5
876 that used __main__ if no module is found. I don't actually
877 like this rule. jlf
878 */
Guido van Rossum053b8df1998-11-25 16:18:00 +0000879 if (!j) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000880 j=1;
881 name=__main___str;
882 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000883
884 Py_INCREF(name);
885 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000886}
887
888
Guido van Rossum60456fd1997-04-09 17:36:32 +0000889static int
890save_none(Picklerobject *self, PyObject *args) {
891 static char none = NONE;
Tim Peters84e87f32001-03-17 04:50:51 +0000892 if ((*self->write_func)(self, &none, 1) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +0000893 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000894
Guido van Rossum60456fd1997-04-09 17:36:32 +0000895 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000896}
897
Tim Peters84e87f32001-03-17 04:50:51 +0000898
Guido van Rossum60456fd1997-04-09 17:36:32 +0000899static int
900save_int(Picklerobject *self, PyObject *args) {
901 char c_str[32];
902 long l = PyInt_AS_LONG((PyIntObject *)args);
903 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000904
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000905 if (!self->bin
906#if SIZEOF_LONG > 4
Guido van Rossum053b8df1998-11-25 16:18:00 +0000907 || (l >> 32)
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000908#endif
Guido van Rossum053b8df1998-11-25 16:18:00 +0000909 ) {
910 /* Save extra-long ints in non-binary mode, so that
911 we can use python long parsing code to restore,
912 if necessary. */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000913 c_str[0] = INT;
914 sprintf(c_str + 1, "%ld\n", l);
915 if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
916 return -1;
917 }
918 else {
919 c_str[1] = (int)( l & 0xff);
920 c_str[2] = (int)((l >> 8) & 0xff);
921 c_str[3] = (int)((l >> 16) & 0xff);
922 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000923
Guido van Rossum60456fd1997-04-09 17:36:32 +0000924 if ((c_str[4] == 0) && (c_str[3] == 0)) {
925 if (c_str[2] == 0) {
926 c_str[0] = BININT1;
927 len = 2;
928 }
929 else {
930 c_str[0] = BININT2;
931 len = 3;
932 }
933 }
934 else {
935 c_str[0] = BININT;
936 len = 5;
937 }
938
939 if ((*self->write_func)(self, c_str, len) < 0)
940 return -1;
941 }
942
943 return 0;
944}
945
946
947static int
948save_long(Picklerobject *self, PyObject *args) {
949 int size, res = -1;
950 PyObject *repr = 0;
951
952 static char l = LONG;
953
Guido van Rossum053b8df1998-11-25 16:18:00 +0000954 UNLESS (repr = PyObject_Repr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +0000955 goto finally;
956
957 if ((size = PyString_Size(repr)) < 0)
958 goto finally;
959
960 if ((*self->write_func)(self, &l, 1) < 0)
961 goto finally;
962
Tim Peters84e87f32001-03-17 04:50:51 +0000963 if ((*self->write_func)(self,
Guido van Rossum60456fd1997-04-09 17:36:32 +0000964 PyString_AS_STRING((PyStringObject *)repr), size) < 0)
965 goto finally;
966
967 if ((*self->write_func)(self, "\n", 1) < 0)
968 goto finally;
969
970 res = 0;
971
972finally:
973 Py_XDECREF(repr);
974
975 return res;
976}
977
978
979static int
980save_float(Picklerobject *self, PyObject *args) {
981 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
982
Guido van Rossum60456fd1997-04-09 17:36:32 +0000983 if (self->bin) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000984 int s, e;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000985 double f;
986 long fhi, flo;
987 char str[9], *p = str;
988
989 *p = BINFLOAT;
990 p++;
991
992 if (x < 0) {
993 s = 1;
994 x = -x;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000995 }
996 else
Guido van Rossum60456fd1997-04-09 17:36:32 +0000997 s = 0;
998
999 f = frexp(x, &e);
1000
1001 /* Normalize f to be in the range [1.0, 2.0) */
1002 if (0.5 <= f && f < 1.0) {
1003 f *= 2.0;
1004 e--;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001005 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001006 else if (f == 0.0) {
1007 e = 0;
1008 }
1009 else {
1010 PyErr_SetString(PyExc_SystemError,
1011 "frexp() result out of range");
1012 return -1;
1013 }
1014
1015 if (e >= 1024) {
1016 /* XXX 1024 itself is reserved for Inf/NaN */
1017 PyErr_SetString(PyExc_OverflowError,
1018 "float too large to pack with d format");
1019 return -1;
1020 }
1021 else if (e < -1022) {
1022 /* Gradual underflow */
1023 f = ldexp(f, 1022 + e);
1024 e = 0;
1025 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001026 else if (!(e == 0 && f == 0.0)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001027 e += 1023;
1028 f -= 1.0; /* Get rid of leading 1 */
1029 }
1030
1031 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
1032 f *= 268435456.0; /* 2**28 */
1033 fhi = (long) floor(f); /* Truncate */
1034 f -= (double)fhi;
1035 f *= 16777216.0; /* 2**24 */
1036 flo = (long) floor(f + 0.5); /* Round */
1037
1038 /* First byte */
1039 *p = (s<<7) | (e>>4);
1040 p++;
1041
1042 /* Second byte */
Guido van Rossume94e3fb1998-12-08 17:37:19 +00001043 *p = (char) (((e&0xF)<<4) | (fhi>>24));
Guido van Rossum60456fd1997-04-09 17:36:32 +00001044 p++;
1045
1046 /* Third byte */
1047 *p = (fhi>>16) & 0xFF;
1048 p++;
1049
1050 /* Fourth byte */
1051 *p = (fhi>>8) & 0xFF;
1052 p++;
1053
1054 /* Fifth byte */
1055 *p = fhi & 0xFF;
1056 p++;
1057
1058 /* Sixth byte */
1059 *p = (flo>>16) & 0xFF;
1060 p++;
1061
1062 /* Seventh byte */
1063 *p = (flo>>8) & 0xFF;
1064 p++;
1065
1066 /* Eighth byte */
1067 *p = flo & 0xFF;
1068
1069 if ((*self->write_func)(self, str, 9) < 0)
1070 return -1;
1071 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001072 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001073 char c_str[250];
1074 c_str[0] = FLOAT;
Guido van Rossum104be4a1998-04-03 21:13:02 +00001075 sprintf(c_str + 1, "%.17g\n", x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001076
1077 if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
1078 return -1;
1079 }
1080
1081 return 0;
1082}
1083
1084
1085static int
Guido van Rossum142eeb81997-08-13 03:14:41 +00001086save_string(Picklerobject *self, PyObject *args, int doput) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001087 int size, len;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001088 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001089
Guido van Rossum053b8df1998-11-25 16:18:00 +00001090 if ((size = PyString_Size(args)) < 0)
1091 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001092
1093 if (!self->bin) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001094 char *repr_str;
1095
1096 static char string = STRING;
1097
Guido van Rossum053b8df1998-11-25 16:18:00 +00001098 UNLESS (repr = PyObject_Repr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001099 return -1;
1100
Guido van Rossum053b8df1998-11-25 16:18:00 +00001101 if ((len = PyString_Size(repr)) < 0)
1102 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001103 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001104
1105 if ((*self->write_func)(self, &string, 1) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001106 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001107
1108 if ((*self->write_func)(self, repr_str, len) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001109 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001110
1111 if ((*self->write_func)(self, "\n", 1) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001112 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001113
1114 Py_XDECREF(repr);
1115 }
1116 else {
1117 int i;
1118 char c_str[5];
1119
Guido van Rossum053b8df1998-11-25 16:18:00 +00001120 if ((size = PyString_Size(args)) < 0)
1121 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001122
1123 if (size < 256) {
1124 c_str[0] = SHORT_BINSTRING;
1125 c_str[1] = size;
1126 len = 2;
1127 }
1128 else {
1129 c_str[0] = BINSTRING;
1130 for (i = 1; i < 5; i++)
1131 c_str[i] = (int)(size >> ((i - 1) * 8));
1132 len = 5;
1133 }
1134
1135 if ((*self->write_func)(self, c_str, len) < 0)
1136 return -1;
1137
Guido van Rossum053b8df1998-11-25 16:18:00 +00001138 if (size > 128 && Pdata_Check(self->file)) {
1139 if (write_other(self, NULL, 0) < 0) return -1;
1140 PDATA_APPEND(self->file, args, -1);
1141 }
1142 else {
Tim Peters84e87f32001-03-17 04:50:51 +00001143 if ((*self->write_func)(self,
Guido van Rossum053b8df1998-11-25 16:18:00 +00001144 PyString_AS_STRING((PyStringObject *)args), size) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001145 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001146 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001147 }
1148
Guido van Rossum142eeb81997-08-13 03:14:41 +00001149 if (doput)
1150 if (put(self, args) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001151 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001152
1153 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001154
1155err:
1156 Py_XDECREF(repr);
1157 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001158}
1159
1160
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001161/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1162 backslash and newline characters to \uXXXX escapes. */
1163static PyObject *
1164modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, int size)
1165{
1166 PyObject *repr;
1167 char *p;
1168 char *q;
1169
1170 static const char *hexdigit = "0123456789ABCDEF";
1171
1172 repr = PyString_FromStringAndSize(NULL, 6 * size);
1173 if (repr == NULL)
1174 return NULL;
1175 if (size == 0)
1176 return repr;
1177
1178 p = q = PyString_AS_STRING(repr);
1179 while (size-- > 0) {
1180 Py_UNICODE ch = *s++;
1181 /* Map 16-bit characters to '\uxxxx' */
1182 if (ch >= 256 || ch == '\\' || ch == '\n') {
1183 *p++ = '\\';
1184 *p++ = 'u';
1185 *p++ = hexdigit[(ch >> 12) & 0xf];
1186 *p++ = hexdigit[(ch >> 8) & 0xf];
1187 *p++ = hexdigit[(ch >> 4) & 0xf];
1188 *p++ = hexdigit[ch & 15];
1189 }
1190 /* Copy everything else as-is */
1191 else
1192 *p++ = (char) ch;
1193 }
1194 *p = '\0';
1195 if (_PyString_Resize(&repr, p - q))
1196 goto onError;
1197
1198 return repr;
1199
1200 onError:
1201 Py_DECREF(repr);
1202 return NULL;
1203}
1204
1205
Guido van Rossum60456fd1997-04-09 17:36:32 +00001206static int
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001207save_unicode(Picklerobject *self, PyObject *args, int doput) {
1208 int size, len;
1209 PyObject *repr=0;
1210
1211 if (!PyUnicode_Check(args))
1212 return -1;
1213
1214 if (!self->bin) {
1215 char *repr_str;
1216 static char string = UNICODE;
1217
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001218 UNLESS(repr = modified_EncodeRawUnicodeEscape(
1219 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args)))
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001220 return -1;
1221
1222 if ((len = PyString_Size(repr)) < 0)
1223 goto err;
1224 repr_str = PyString_AS_STRING((PyStringObject *)repr);
1225
1226 if ((*self->write_func)(self, &string, 1) < 0)
1227 goto err;
1228
1229 if ((*self->write_func)(self, repr_str, len) < 0)
1230 goto err;
1231
1232 if ((*self->write_func)(self, "\n", 1) < 0)
1233 goto err;
1234
1235 Py_XDECREF(repr);
1236 }
1237 else {
1238 int i;
1239 char c_str[5];
1240
1241 UNLESS (repr = PyUnicode_AsUTF8String(args))
1242 return -1;
1243
1244 if ((size = PyString_Size(repr)) < 0)
1245 goto err;
1246
1247 c_str[0] = BINUNICODE;
1248 for (i = 1; i < 5; i++)
1249 c_str[i] = (int)(size >> ((i - 1) * 8));
1250 len = 5;
1251
1252 if ((*self->write_func)(self, c_str, len) < 0)
1253 goto err;
1254
1255 if (size > 128 && Pdata_Check(self->file)) {
1256 if (write_other(self, NULL, 0) < 0)
1257 goto err;
1258 PDATA_APPEND(self->file, repr, -1);
1259 }
1260 else {
1261 if ((*self->write_func)(self, PyString_AS_STRING(repr), size) < 0)
1262 goto err;
1263 }
1264
1265 Py_DECREF(repr);
1266 }
1267
1268 if (doput)
1269 if (put(self, args) < 0)
1270 return -1;
1271
1272 return 0;
1273
1274err:
1275 Py_XDECREF(repr);
1276 return -1;
1277}
1278
1279
1280static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00001281save_tuple(Picklerobject *self, PyObject *args) {
1282 PyObject *element = 0, *py_tuple_id = 0;
1283 int len, i, has_key, res = -1;
1284
1285 static char tuple = TUPLE;
1286
1287 if ((*self->write_func)(self, &MARKv, 1) < 0)
1288 goto finally;
1289
Tim Peters84e87f32001-03-17 04:50:51 +00001290 if ((len = PyTuple_Size(args)) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001291 goto finally;
1292
1293 for (i = 0; i < len; i++) {
Tim Peters84e87f32001-03-17 04:50:51 +00001294 UNLESS (element = PyTuple_GET_ITEM((PyTupleObject *)args, i))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001295 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001296
Guido van Rossum60456fd1997-04-09 17:36:32 +00001297 if (save(self, element, 0) < 0)
1298 goto finally;
1299 }
1300
Guido van Rossum534b7c52000-06-28 22:23:56 +00001301 UNLESS (py_tuple_id = PyLong_FromVoidPtr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001302 goto finally;
1303
1304 if (len) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001305 if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_tuple_id)) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001306 goto finally;
1307
1308 if (has_key) {
1309 if (self->bin) {
1310 static char pop_mark = POP_MARK;
Tim Peters84e87f32001-03-17 04:50:51 +00001311
Guido van Rossum60456fd1997-04-09 17:36:32 +00001312 if ((*self->write_func)(self, &pop_mark, 1) < 0)
1313 goto finally;
1314 }
1315 else {
1316 static char pop = POP;
Tim Peters84e87f32001-03-17 04:50:51 +00001317
Guido van Rossum60456fd1997-04-09 17:36:32 +00001318 for (i = 0; i <= len; i++) {
1319 if ((*self->write_func)(self, &pop, 1) < 0)
1320 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001321 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001322 }
Tim Peters84e87f32001-03-17 04:50:51 +00001323
Guido van Rossum60456fd1997-04-09 17:36:32 +00001324 if (get(self, py_tuple_id) < 0)
1325 goto finally;
1326
1327 res = 0;
1328 goto finally;
1329 }
1330 }
1331
1332 if ((*self->write_func)(self, &tuple, 1) < 0) {
1333 goto finally;
1334 }
1335
1336 if (put(self, args) < 0)
1337 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001338
Guido van Rossum60456fd1997-04-09 17:36:32 +00001339 res = 0;
1340
1341finally:
1342 Py_XDECREF(py_tuple_id);
1343
1344 return res;
1345}
1346
1347static int
1348save_empty_tuple(Picklerobject *self, PyObject *args) {
1349 static char tuple = EMPTY_TUPLE;
1350
1351 return (*self->write_func)(self, &tuple, 1);
1352}
1353
1354
1355static int
1356save_list(Picklerobject *self, PyObject *args) {
1357 PyObject *element = 0;
1358 int s_len, len, i, using_appends, res = -1;
1359 char s[3];
1360
1361 static char append = APPEND, appends = APPENDS;
1362
Guido van Rossum053b8df1998-11-25 16:18:00 +00001363 if (self->bin) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001364 s[0] = EMPTY_LIST;
1365 s_len = 1;
Tim Peters84e87f32001-03-17 04:50:51 +00001366 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001367 else {
1368 s[0] = MARK;
1369 s[1] = LIST;
1370 s_len = 2;
1371 }
1372
1373 if ((len = PyList_Size(args)) < 0)
1374 goto finally;
1375
1376 if ((*self->write_func)(self, s, s_len) < 0)
1377 goto finally;
1378
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001379 if (len == 0) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001380 if (put(self, args) < 0)
1381 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001382 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001383 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001384 if (put2(self, args) < 0)
1385 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001386 }
1387
Guido van Rossum142eeb81997-08-13 03:14:41 +00001388 if ((using_appends = (self->bin && (len > 1))))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001389 if ((*self->write_func)(self, &MARKv, 1) < 0)
1390 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001391
Guido van Rossum60456fd1997-04-09 17:36:32 +00001392 for (i = 0; i < len; i++) {
Tim Peters84e87f32001-03-17 04:50:51 +00001393 UNLESS (element = PyList_GET_ITEM((PyListObject *)args, i))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001394 goto finally;
1395
Tim Peters84e87f32001-03-17 04:50:51 +00001396 if (save(self, element, 0) < 0)
1397 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001398
1399 if (!using_appends) {
1400 if ((*self->write_func)(self, &append, 1) < 0)
1401 goto finally;
1402 }
1403 }
1404
1405 if (using_appends) {
1406 if ((*self->write_func)(self, &appends, 1) < 0)
1407 goto finally;
1408 }
1409
1410 res = 0;
1411
1412finally:
1413
1414 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001415}
1416
1417
Guido van Rossum60456fd1997-04-09 17:36:32 +00001418static int
1419save_dict(Picklerobject *self, PyObject *args) {
1420 PyObject *key = 0, *value = 0;
1421 int i, len, res = -1, using_setitems;
1422 char s[3];
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001423
Guido van Rossum60456fd1997-04-09 17:36:32 +00001424 static char setitem = SETITEM, setitems = SETITEMS;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001425
Guido van Rossum60456fd1997-04-09 17:36:32 +00001426 if (self->bin) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001427 s[0] = EMPTY_DICT;
1428 len = 1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001429 }
1430 else {
1431 s[0] = MARK;
1432 s[1] = DICT;
1433 len = 2;
1434 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001435
Guido van Rossum60456fd1997-04-09 17:36:32 +00001436 if ((*self->write_func)(self, s, len) < 0)
1437 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001438
Guido van Rossum60456fd1997-04-09 17:36:32 +00001439 if ((len = PyDict_Size(args)) < 0)
1440 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001441
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001442 if (len == 0) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001443 if (put(self, args) < 0)
1444 goto finally;
1445 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001446 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001447 if (put2(self, args) < 0)
1448 goto finally;
1449 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001450
Guido van Rossum142eeb81997-08-13 03:14:41 +00001451 if ((using_setitems = (self->bin && (PyDict_Size(args) > 1))))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001452 if ((*self->write_func)(self, &MARKv, 1) < 0)
1453 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001454
Guido van Rossum60456fd1997-04-09 17:36:32 +00001455 i = 0;
1456 while (PyDict_Next(args, &i, &key, &value)) {
1457 if (save(self, key, 0) < 0)
1458 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001459
Guido van Rossum60456fd1997-04-09 17:36:32 +00001460 if (save(self, value, 0) < 0)
1461 goto finally;
1462
1463 if (!using_setitems) {
1464 if ((*self->write_func)(self, &setitem, 1) < 0)
1465 goto finally;
1466 }
1467 }
1468
1469 if (using_setitems) {
1470 if ((*self->write_func)(self, &setitems, 1) < 0)
1471 goto finally;
1472 }
1473
1474 res = 0;
1475
1476finally:
1477
1478 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001479}
1480
1481
Tim Peters84e87f32001-03-17 04:50:51 +00001482static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00001483save_inst(Picklerobject *self, PyObject *args) {
Tim Peters84e87f32001-03-17 04:50:51 +00001484 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
Guido van Rossum60456fd1997-04-09 17:36:32 +00001485 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1486 char *module_str, *name_str;
1487 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001488
Guido van Rossum60456fd1997-04-09 17:36:32 +00001489 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001490
Guido van Rossum60456fd1997-04-09 17:36:32 +00001491 if ((*self->write_func)(self, &MARKv, 1) < 0)
1492 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001493
Guido van Rossum053b8df1998-11-25 16:18:00 +00001494 UNLESS (class = PyObject_GetAttr(args, __class___str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001495 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001496
Guido van Rossum60456fd1997-04-09 17:36:32 +00001497 if (self->bin) {
1498 if (save(self, class, 0) < 0)
1499 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001500 }
1501
Guido van Rossum142eeb81997-08-13 03:14:41 +00001502 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001503 PyObject *element = 0;
1504 int i, len;
1505
Tim Peters84e87f32001-03-17 04:50:51 +00001506 UNLESS (class_args =
Guido van Rossum60456fd1997-04-09 17:36:32 +00001507 PyObject_CallObject(getinitargs_func, empty_tuple))
1508 goto finally;
1509
Tim Peters84e87f32001-03-17 04:50:51 +00001510 if ((len = PyObject_Size(class_args)) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001511 goto finally;
1512
1513 for (i = 0; i < len; i++) {
Tim Peters84e87f32001-03-17 04:50:51 +00001514 UNLESS (element = PySequence_GetItem(class_args, i))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001515 goto finally;
1516
1517 if (save(self, element, 0) < 0) {
1518 Py_DECREF(element);
1519 goto finally;
1520 }
1521
1522 Py_DECREF(element);
1523 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001524 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001525 else {
1526 PyErr_Clear();
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001527 }
1528
Guido van Rossum60456fd1997-04-09 17:36:32 +00001529 if (!self->bin) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001530 UNLESS (name = ((PyClassObject *)class)->cl_name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001531 PyErr_SetString(PicklingError, "class has no name");
1532 goto finally;
1533 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001534
Guido van Rossum053b8df1998-11-25 16:18:00 +00001535 UNLESS (module = whichmodule(class, name))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001536 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001537
Tim Peters84e87f32001-03-17 04:50:51 +00001538
Guido van Rossum053b8df1998-11-25 16:18:00 +00001539 if ((module_size = PyString_Size(module)) < 0 ||
1540 (name_size = PyString_Size(name)) < 0)
1541 goto finally;
1542
Guido van Rossum60456fd1997-04-09 17:36:32 +00001543 module_str = PyString_AS_STRING((PyStringObject *)module);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001544 name_str = PyString_AS_STRING((PyStringObject *)name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001545
Guido van Rossum60456fd1997-04-09 17:36:32 +00001546 if ((*self->write_func)(self, &inst, 1) < 0)
1547 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001548
Guido van Rossum60456fd1997-04-09 17:36:32 +00001549 if ((*self->write_func)(self, module_str, module_size) < 0)
1550 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001551
Guido van Rossum60456fd1997-04-09 17:36:32 +00001552 if ((*self->write_func)(self, "\n", 1) < 0)
1553 goto finally;
1554
1555 if ((*self->write_func)(self, name_str, name_size) < 0)
1556 goto finally;
1557
1558 if ((*self->write_func)(self, "\n", 1) < 0)
1559 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001560 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001561 else if ((*self->write_func)(self, &obj, 1) < 0) {
1562 goto finally;
1563 }
1564
Guido van Rossum142eeb81997-08-13 03:14:41 +00001565 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001566 UNLESS (state = PyObject_CallObject(getstate_func, empty_tuple))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001567 goto finally;
1568 }
1569 else {
1570 PyErr_Clear();
1571
Guido van Rossum053b8df1998-11-25 16:18:00 +00001572 UNLESS (state = PyObject_GetAttr(args, __dict___str)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001573 PyErr_Clear();
1574 res = 0;
1575 goto finally;
1576 }
1577 }
1578
1579 if (!PyDict_Check(state)) {
1580 if (put2(self, args) < 0)
1581 goto finally;
1582 }
1583 else {
1584 if (put(self, args) < 0)
1585 goto finally;
1586 }
Tim Peters84e87f32001-03-17 04:50:51 +00001587
Guido van Rossum60456fd1997-04-09 17:36:32 +00001588 if (save(self, state, 0) < 0)
1589 goto finally;
1590
1591 if ((*self->write_func)(self, &build, 1) < 0)
1592 goto finally;
1593
1594 res = 0;
1595
1596finally:
1597 Py_XDECREF(module);
1598 Py_XDECREF(class);
1599 Py_XDECREF(state);
1600 Py_XDECREF(getinitargs_func);
1601 Py_XDECREF(getstate_func);
1602 Py_XDECREF(class_args);
1603
1604 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001605}
1606
1607
Guido van Rossum60456fd1997-04-09 17:36:32 +00001608static int
1609save_global(Picklerobject *self, PyObject *args, PyObject *name) {
1610 PyObject *global_name = 0, *module = 0;
Tim Peters84e87f32001-03-17 04:50:51 +00001611 char *name_str, *module_str;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001612 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001613
Guido van Rossum60456fd1997-04-09 17:36:32 +00001614 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001615
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001616 if (name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001617 global_name = name;
1618 Py_INCREF(global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001619 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001620 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001621 UNLESS (global_name = PyObject_GetAttr(args, __name___str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001622 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001623 }
1624
Guido van Rossum053b8df1998-11-25 16:18:00 +00001625 UNLESS (module = whichmodule(args, global_name))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001626 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001627
Guido van Rossum053b8df1998-11-25 16:18:00 +00001628 if ((module_size = PyString_Size(module)) < 0 ||
1629 (name_size = PyString_Size(global_name)) < 0)
1630 goto finally;
1631
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001632 module_str = PyString_AS_STRING((PyStringObject *)module);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001633 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001634
Guido van Rossum60456fd1997-04-09 17:36:32 +00001635 if ((*self->write_func)(self, &global, 1) < 0)
1636 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001637
Guido van Rossum60456fd1997-04-09 17:36:32 +00001638 if ((*self->write_func)(self, module_str, module_size) < 0)
1639 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001640
Guido van Rossum60456fd1997-04-09 17:36:32 +00001641 if ((*self->write_func)(self, "\n", 1) < 0)
1642 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001643
Guido van Rossum60456fd1997-04-09 17:36:32 +00001644 if ((*self->write_func)(self, name_str, name_size) < 0)
1645 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001646
Guido van Rossum60456fd1997-04-09 17:36:32 +00001647 if ((*self->write_func)(self, "\n", 1) < 0)
1648 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001649
Guido van Rossum60456fd1997-04-09 17:36:32 +00001650 if (put(self, args) < 0)
1651 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001652
Guido van Rossum60456fd1997-04-09 17:36:32 +00001653 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001654
Guido van Rossum60456fd1997-04-09 17:36:32 +00001655finally:
1656 Py_XDECREF(module);
1657 Py_XDECREF(global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001658
Guido van Rossum60456fd1997-04-09 17:36:32 +00001659 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001660}
1661
Guido van Rossum60456fd1997-04-09 17:36:32 +00001662static int
1663save_pers(Picklerobject *self, PyObject *args, PyObject *f) {
1664 PyObject *pid = 0;
1665 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001666
Guido van Rossum60456fd1997-04-09 17:36:32 +00001667 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001668
Guido van Rossum053b8df1998-11-25 16:18:00 +00001669 Py_INCREF(args);
1670 ARG_TUP(self, args);
1671 if (self->arg) {
1672 pid = PyObject_CallObject(f, self->arg);
1673 FREE_ARG_TUP(self);
1674 }
1675 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001676
Guido van Rossum60456fd1997-04-09 17:36:32 +00001677 if (pid != Py_None) {
1678 if (!self->bin) {
1679 if (!PyString_Check(pid)) {
Tim Peters84e87f32001-03-17 04:50:51 +00001680 PyErr_SetString(PicklingError,
Guido van Rossum60456fd1997-04-09 17:36:32 +00001681 "persistent id must be string");
1682 goto finally;
1683 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001684
Guido van Rossum60456fd1997-04-09 17:36:32 +00001685 if ((*self->write_func)(self, &persid, 1) < 0)
1686 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001687
Guido van Rossum60456fd1997-04-09 17:36:32 +00001688 if ((size = PyString_Size(pid)) < 0)
1689 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001690
Tim Peters84e87f32001-03-17 04:50:51 +00001691 if ((*self->write_func)(self,
Guido van Rossum60456fd1997-04-09 17:36:32 +00001692 PyString_AS_STRING((PyStringObject *)pid), size) < 0)
1693 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001694
Guido van Rossum60456fd1997-04-09 17:36:32 +00001695 if ((*self->write_func)(self, "\n", 1) < 0)
1696 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001697
Guido van Rossum60456fd1997-04-09 17:36:32 +00001698 res = 1;
1699 goto finally;
1700 }
1701 else if (save(self, pid, 1) >= 0) {
1702 if ((*self->write_func)(self, &binpersid, 1) < 0)
1703 res = -1;
1704 else
1705 res = 1;
1706 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001707
Tim Peters84e87f32001-03-17 04:50:51 +00001708 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001709 }
1710
Guido van Rossum60456fd1997-04-09 17:36:32 +00001711 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001712
Guido van Rossum60456fd1997-04-09 17:36:32 +00001713finally:
1714 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001715
Guido van Rossum60456fd1997-04-09 17:36:32 +00001716 return res;
1717}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001718
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001719
Tim Peters84e87f32001-03-17 04:50:51 +00001720static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00001721save_reduce(Picklerobject *self, PyObject *callable,
1722 PyObject *tup, PyObject *state, PyObject *ob) {
1723 static char reduce = REDUCE, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001724
Guido van Rossum60456fd1997-04-09 17:36:32 +00001725 if (save(self, callable, 0) < 0)
1726 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001727
Guido van Rossum60456fd1997-04-09 17:36:32 +00001728 if (save(self, tup, 0) < 0)
1729 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001730
Guido van Rossum60456fd1997-04-09 17:36:32 +00001731 if ((*self->write_func)(self, &reduce, 1) < 0)
1732 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001733
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001734 if (ob != NULL) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001735 if (state && !PyDict_Check(state)) {
1736 if (put2(self, ob) < 0)
1737 return -1;
1738 }
1739 else {
1740 if (put(self, ob) < 0)
1741 return -1;
1742 }
1743 }
Tim Peters84e87f32001-03-17 04:50:51 +00001744
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001745 if (state) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001746 if (save(self, state, 0) < 0)
1747 return -1;
1748
1749 if ((*self->write_func)(self, &build, 1) < 0)
1750 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001751 }
1752
Guido van Rossum60456fd1997-04-09 17:36:32 +00001753 return 0;
1754}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001755
Guido van Rossum60456fd1997-04-09 17:36:32 +00001756static int
1757save(Picklerobject *self, PyObject *args, int pers_save) {
1758 PyTypeObject *type;
1759 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0, *arg_tup = 0,
Guido van Rossum142eeb81997-08-13 03:14:41 +00001760 *callable = 0, *state = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001761 int res = -1, tmp, size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001762
Guido van Rossum60456fd1997-04-09 17:36:32 +00001763 if (!pers_save && self->pers_func) {
1764 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
1765 res = tmp;
1766 goto finally;
1767 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001768 }
1769
Guido van Rossum60456fd1997-04-09 17:36:32 +00001770 if (args == Py_None) {
1771 res = save_none(self, args);
1772 goto finally;
1773 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001774
Guido van Rossum60456fd1997-04-09 17:36:32 +00001775 type = args->ob_type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001776
Guido van Rossum60456fd1997-04-09 17:36:32 +00001777 switch (type->tp_name[0]) {
1778 case 'i':
1779 if (type == &PyInt_Type) {
1780 res = save_int(self, args);
1781 goto finally;
1782 }
1783 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001784
Guido van Rossum60456fd1997-04-09 17:36:32 +00001785 case 'l':
1786 if (type == &PyLong_Type) {
1787 res = save_long(self, args);
1788 goto finally;
1789 }
1790 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001791
Guido van Rossum60456fd1997-04-09 17:36:32 +00001792 case 'f':
1793 if (type == &PyFloat_Type) {
1794 res = save_float(self, args);
1795 goto finally;
1796 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001797 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001798
Guido van Rossum60456fd1997-04-09 17:36:32 +00001799 case 't':
1800 if (type == &PyTuple_Type && PyTuple_Size(args)==0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001801 if (self->bin) res = save_empty_tuple(self, args);
1802 else res = save_tuple(self, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001803 goto finally;
1804 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001805 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001806
Guido van Rossum60456fd1997-04-09 17:36:32 +00001807 case 's':
Guido van Rossum053b8df1998-11-25 16:18:00 +00001808 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001809 res = save_string(self, args, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001810 goto finally;
1811 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001812
1813 case 'u':
1814 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
1815 res = save_unicode(self, args, 0);
1816 goto finally;
1817 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001818 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001819
Guido van Rossum60456fd1997-04-09 17:36:32 +00001820 if (args->ob_refcnt > 1) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001821 int has_key;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001822
Guido van Rossum534b7c52000-06-28 22:23:56 +00001823 UNLESS (py_ob_id = PyLong_FromVoidPtr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001824 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001825
Guido van Rossum60456fd1997-04-09 17:36:32 +00001826 if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_ob_id)) < 0)
1827 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001828
Guido van Rossum60456fd1997-04-09 17:36:32 +00001829 if (has_key) {
1830 if (get(self, py_ob_id) < 0)
1831 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001832
Guido van Rossum60456fd1997-04-09 17:36:32 +00001833 res = 0;
1834 goto finally;
1835 }
1836 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001837
Guido van Rossum60456fd1997-04-09 17:36:32 +00001838 switch (type->tp_name[0]) {
1839 case 's':
1840 if (type == &PyString_Type) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001841 res = save_string(self, args, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001842 goto finally;
1843 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001844 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001845
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001846 case 'u':
1847 if (type == &PyUnicode_Type) {
1848 res = save_unicode(self, args, 1);
1849 goto finally;
1850 }
1851 break;
1852
Guido van Rossum60456fd1997-04-09 17:36:32 +00001853 case 't':
1854 if (type == &PyTuple_Type) {
1855 res = save_tuple(self, args);
1856 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001857 }
1858 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001859
Guido van Rossum60456fd1997-04-09 17:36:32 +00001860 case 'l':
1861 if (type == &PyList_Type) {
1862 res = save_list(self, args);
1863 goto finally;
1864 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001865 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001866
1867 case 'd':
1868 if (type == &PyDict_Type) {
1869 res = save_dict(self, args);
Tim Peters84e87f32001-03-17 04:50:51 +00001870 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001871 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001872 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001873
1874 case 'i':
1875 if (type == &PyInstance_Type) {
1876 res = save_inst(self, args);
1877 goto finally;
1878 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001879 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001880
1881 case 'c':
1882 if (type == &PyClass_Type) {
1883 res = save_global(self, args, NULL);
1884 goto finally;
1885 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001886 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001887
1888 case 'f':
1889 if (type == &PyFunction_Type) {
1890 res = save_global(self, args, NULL);
1891 goto finally;
1892 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001893 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001894
1895 case 'b':
1896 if (type == &PyCFunction_Type) {
1897 res = save_global(self, args, NULL);
1898 goto finally;
1899 }
1900 }
1901
1902 if (!pers_save && self->inst_pers_func) {
1903 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
1904 res = tmp;
1905 goto finally;
1906 }
1907 }
1908
Guido van Rossum142eeb81997-08-13 03:14:41 +00001909 if ((__reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001910 Py_INCREF(__reduce__);
1911
Guido van Rossum60456fd1997-04-09 17:36:32 +00001912 Py_INCREF(args);
Guido van Rossum053b8df1998-11-25 16:18:00 +00001913 ARG_TUP(self, args);
1914 if (self->arg) {
1915 t = PyObject_CallObject(__reduce__, self->arg);
1916 FREE_ARG_TUP(self);
1917 }
1918 if (! t) goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001919 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001920 else {
1921 PyErr_Clear();
1922
Guido van Rossum142eeb81997-08-13 03:14:41 +00001923 if ((__reduce__ = PyObject_GetAttr(args, __reduce___str))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001924 UNLESS (t = PyObject_CallObject(__reduce__, empty_tuple))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001925 goto finally;
1926 }
1927 else {
1928 PyErr_Clear();
1929 }
1930 }
1931
1932 if (t) {
1933 if (PyString_Check(t)) {
1934 res = save_global(self, args, t);
1935 goto finally;
1936 }
Tim Peters84e87f32001-03-17 04:50:51 +00001937
Guido van Rossum60456fd1997-04-09 17:36:32 +00001938 if (!PyTuple_Check(t)) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001939 cPickle_ErrFormat(PicklingError, "Value returned by %s must "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001940 "be a tuple", "O", __reduce__);
1941 goto finally;
1942 }
1943
1944 size = PyTuple_Size(t);
Tim Peters84e87f32001-03-17 04:50:51 +00001945
Guido van Rossum60456fd1997-04-09 17:36:32 +00001946 if ((size != 3) && (size != 2)) {
Tim Peters84e87f32001-03-17 04:50:51 +00001947 cPickle_ErrFormat(PicklingError, "tuple returned by %s must "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001948 "contain only two or three elements", "O", __reduce__);
1949 goto finally;
1950 }
Tim Peters84e87f32001-03-17 04:50:51 +00001951
Guido van Rossum60456fd1997-04-09 17:36:32 +00001952 callable = PyTuple_GET_ITEM(t, 0);
Guido van Rossum142eeb81997-08-13 03:14:41 +00001953
Guido van Rossum60456fd1997-04-09 17:36:32 +00001954 arg_tup = PyTuple_GET_ITEM(t, 1);
1955
1956 if (size > 2) {
1957 state = PyTuple_GET_ITEM(t, 2);
1958 }
1959
Guido van Rossum053b8df1998-11-25 16:18:00 +00001960 UNLESS (PyTuple_Check(arg_tup) || arg_tup==Py_None) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001961 cPickle_ErrFormat(PicklingError, "Second element of tuple "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001962 "returned by %s must be a tuple", "O", __reduce__);
1963 goto finally;
1964 }
1965
1966 res = save_reduce(self, callable, arg_tup, state, args);
1967 goto finally;
1968 }
1969
Guido van Rossumc03158b1999-06-09 15:23:31 +00001970 PyErr_SetObject(UnpickleableError, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001971
1972finally:
1973 Py_XDECREF(py_ob_id);
1974 Py_XDECREF(__reduce__);
1975 Py_XDECREF(t);
Tim Peters84e87f32001-03-17 04:50:51 +00001976
Guido van Rossum60456fd1997-04-09 17:36:32 +00001977 return res;
1978}
1979
1980
1981static int
1982dump(Picklerobject *self, PyObject *args) {
1983 static char stop = STOP;
1984
1985 if (save(self, args, 0) < 0)
1986 return -1;
1987
1988 if ((*self->write_func)(self, &stop, 1) < 0)
1989 return -1;
1990
1991 if ((*self->write_func)(self, NULL, 0) < 0)
1992 return -1;
1993
1994 return 0;
1995}
1996
1997static PyObject *
Guido van Rossum053b8df1998-11-25 16:18:00 +00001998Pickle_clear_memo(Picklerobject *self, PyObject *args) {
Guido van Rossum43713e52000-02-29 13:59:29 +00001999 if (args && ! PyArg_ParseTuple(args,":clear_memo")) return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002000 if (self->memo) PyDict_Clear(self->memo);
2001 Py_INCREF(Py_None);
2002 return Py_None;
2003}
2004
2005static PyObject *
2006Pickle_getvalue(Picklerobject *self, PyObject *args) {
2007 int l, i, rsize, ssize, clear=1, lm;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00002008 long ik;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002009 PyObject *k, *r;
2010 char *s, *p, *have_get;
2011 Pdata *data;
2012
Guido van Rossum43713e52000-02-29 13:59:29 +00002013 if (args && ! PyArg_ParseTuple(args,"|i:getvalue",&clear)) return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002014
2015 /* Check to make sure we are based on a list */
2016 if (! Pdata_Check(self->file)) {
2017 PyErr_SetString(PicklingError,
2018 "Attempt to getvalue a non-list-based pickler");
2019 return NULL;
2020 }
2021
2022 /* flush write buffer */
2023 if (write_other(self, NULL, 0) < 0) return NULL;
2024
2025 data=(Pdata*)self->file;
2026 l=data->length;
2027
2028 /* set up an array to hold get/put status */
2029 if ((lm=PyDict_Size(self->memo)) < 0) return NULL;
2030 lm++;
Tim Peters84e87f32001-03-17 04:50:51 +00002031 if (! (have_get=malloc((lm)*sizeof(char)))) return PyErr_NoMemory();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002032 memset(have_get,0,lm);
2033
2034 /* Scan for gets. */
2035 for (rsize=0, i=l; --i >= 0; ) {
2036 k=data->data[i];
Tim Peters84e87f32001-03-17 04:50:51 +00002037
Guido van Rossum053b8df1998-11-25 16:18:00 +00002038 if (PyString_Check(k)) {
2039 rsize += PyString_GET_SIZE(k);
2040 }
2041
2042 else if (PyInt_Check(k)) { /* put */
2043 ik=PyInt_AS_LONG((PyIntObject*)k);
2044 if (ik >= lm || ik==0) {
2045 PyErr_SetString(PicklingError,
2046 "Invalid get data");
2047 return NULL;
Tim Peters84e87f32001-03-17 04:50:51 +00002048 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002049 if (have_get[ik]) { /* with matching get */
2050 if (ik < 256) rsize += 2;
2051 else rsize+=5;
2052 }
2053 }
2054
2055 else if (! (PyTuple_Check(k) &&
2056 PyTuple_GET_SIZE(k) == 2 &&
2057 PyInt_Check((k=PyTuple_GET_ITEM(k,0))))
2058 ) {
2059 PyErr_SetString(PicklingError,
2060 "Unexpected data in internal list");
2061 return NULL;
2062 }
2063
2064 else { /* put */
2065 ik=PyInt_AS_LONG((PyIntObject*)k);
2066 if (ik >= lm || ik==0) {
2067 PyErr_SetString(PicklingError,
2068 "Invalid get data");
2069 return NULL;
Tim Peters84e87f32001-03-17 04:50:51 +00002070 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002071 have_get[ik]=1;
2072 if (ik < 256) rsize += 2;
2073 else rsize+=5;
2074 }
2075
2076 }
2077
2078 /* Now generate the result */
2079 UNLESS (r=PyString_FromStringAndSize(NULL,rsize)) goto err;
2080 s=PyString_AS_STRING((PyStringObject*)r);
2081
2082 for (i=0; i<l; i++) {
2083 k=data->data[i];
2084
2085 if (PyString_Check(k)) {
2086 ssize=PyString_GET_SIZE(k);
2087 if (ssize) {
2088 p=PyString_AS_STRING((PyStringObject*)k);
2089 while (--ssize >= 0) *s++=*p++;
2090 }
2091 }
2092
2093 else if (PyTuple_Check(k)) { /* get */
2094 ik=PyInt_AS_LONG((PyIntObject*)PyTuple_GET_ITEM(k,0));
2095 if (ik < 256) {
2096 *s++ = BINGET;
2097 *s++ = (int)(ik & 0xff);
2098 }
2099 else {
2100 *s++ = LONG_BINGET;
2101 *s++ = (int)(ik & 0xff);
2102 *s++ = (int)((ik >> 8) & 0xff);
2103 *s++ = (int)((ik >> 16) & 0xff);
2104 *s++ = (int)((ik >> 24) & 0xff);
2105 }
2106 }
2107
2108 else { /* put */
2109 ik=PyInt_AS_LONG((PyIntObject*)k);
2110
2111 if (have_get[ik]) { /* with matching get */
2112 if (ik < 256) {
2113 *s++ = BINPUT;
2114 *s++ = (int)(ik & 0xff);
2115 }
2116 else {
2117 *s++ = LONG_BINPUT;
2118 *s++ = (int)(ik & 0xff);
2119 *s++ = (int)((ik >> 8) & 0xff);
2120 *s++ = (int)((ik >> 16) & 0xff);
2121 *s++ = (int)((ik >> 24) & 0xff);
2122 }
2123 }
2124 }
2125
2126 }
2127
2128 if (clear) {
2129 PyDict_Clear(self->memo);
2130 Pdata_clear(data,0);
2131 }
Tim Peters84e87f32001-03-17 04:50:51 +00002132
Guido van Rossum053b8df1998-11-25 16:18:00 +00002133 free(have_get);
2134 return r;
2135err:
2136 free(have_get);
2137 return NULL;
2138}
2139
2140static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002141Pickler_dump(Picklerobject *self, PyObject *args) {
2142 PyObject *ob;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002143 int get=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002144
Guido van Rossum43713e52000-02-29 13:59:29 +00002145 UNLESS (PyArg_ParseTuple(args, "O|i:dump", &ob, &get))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002146 return NULL;
2147
2148 if (dump(self, ob) < 0)
2149 return NULL;
2150
Guido van Rossum053b8df1998-11-25 16:18:00 +00002151 if (get) return Pickle_getvalue(self, NULL);
2152
2153 Py_INCREF(self);
2154 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002155}
2156
2157
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002158static struct PyMethodDef Pickler_methods[] = {
Guido van Rossum142eeb81997-08-13 03:14:41 +00002159 {"dump", (PyCFunction)Pickler_dump, 1,
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002160 "dump(object) --"
2161 "Write an object in pickle format to the object's pickle stream\n"
2162 },
Guido van Rossum142eeb81997-08-13 03:14:41 +00002163 {"clear_memo", (PyCFunction)Pickle_clear_memo, 1,
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002164 "clear_memo() -- Clear the picklers memo"},
Guido van Rossum053b8df1998-11-25 16:18:00 +00002165 {"getvalue", (PyCFunction)Pickle_getvalue, 1,
2166 "getvalue() -- Finish picking a list-based pickle"},
Guido van Rossum60456fd1997-04-09 17:36:32 +00002167 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002168};
2169
2170
2171static Picklerobject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002172newPicklerobject(PyObject *file, int bin) {
2173 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002174
Guido van Rossumb18618d2000-05-03 23:44:39 +00002175 UNLESS (self = PyObject_New(Picklerobject, &Picklertype))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002176 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002177
2178 self->fp = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002179 self->write = NULL;
2180 self->memo = NULL;
2181 self->arg = NULL;
2182 self->pers_func = NULL;
2183 self->inst_pers_func = NULL;
2184 self->write_buf = NULL;
2185 self->bin = bin;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002186 self->fast = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002187 self->buf_size = 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002188 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002189
Guido van Rossum053b8df1998-11-25 16:18:00 +00002190 if (file)
2191 Py_INCREF(file);
2192 else
Guido van Rossum50f385c1998-12-04 18:48:44 +00002193 file=Pdata_New();
Tim Peters84e87f32001-03-17 04:50:51 +00002194
2195 UNLESS (self->file = file)
Guido van Rossum83addc72000-04-21 20:49:36 +00002196 goto err;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002197
Tim Peters84e87f32001-03-17 04:50:51 +00002198 UNLESS (self->memo = PyDict_New())
Guido van Rossum83addc72000-04-21 20:49:36 +00002199 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002200
Guido van Rossum60456fd1997-04-09 17:36:32 +00002201 if (PyFile_Check(file)) {
2202 self->fp = PyFile_AsFile(file);
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002203 if (self->fp == NULL) {
Guido van Rossum83addc72000-04-21 20:49:36 +00002204 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
2205 goto err;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002206 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002207 self->write_func = write_file;
2208 }
2209 else if (PycStringIO_OutputCheck(file)) {
2210 self->write_func = write_cStringIO;
2211 }
Guido van Rossum142eeb81997-08-13 03:14:41 +00002212 else if (file == Py_None) {
2213 self->write_func = write_none;
2214 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002215 else {
2216 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002217
Guido van Rossum053b8df1998-11-25 16:18:00 +00002218 if (! Pdata_Check(file)) {
2219 UNLESS (self->write = PyObject_GetAttr(file, write_str)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002220 PyErr_Clear();
2221 PyErr_SetString(PyExc_TypeError, "argument must have 'write' "
Guido van Rossum053b8df1998-11-25 16:18:00 +00002222 "attribute");
2223 goto err;
2224 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002225 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002226
Tim Peters84e87f32001-03-17 04:50:51 +00002227 UNLESS (self->write_buf =
2228 (char *)malloc(WRITE_BUF_SIZE * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002229 PyErr_NoMemory();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002230 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002231 }
2232 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002233
Guido van Rossum053b8df1998-11-25 16:18:00 +00002234 if (PyEval_GetRestricted()) {
2235 /* Restricted execution, get private tables */
2236 PyObject *m;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002237
Guido van Rossum053b8df1998-11-25 16:18:00 +00002238 UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
2239 self->dispatch_table=PyObject_GetAttr(m, dispatch_table_str);
2240 Py_DECREF(m);
2241 UNLESS (self->dispatch_table) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002242 }
2243 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002244 self->dispatch_table=dispatch_table;
2245 Py_INCREF(dispatch_table);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002246 }
2247
Guido van Rossum60456fd1997-04-09 17:36:32 +00002248 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002249
2250err:
2251 Py_DECREF((PyObject *)self);
2252 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002253}
2254
2255
2256static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002257get_Pickler(PyObject *self, PyObject *args) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002258 PyObject *file=NULL;
2259 int bin;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002260
Guido van Rossum053b8df1998-11-25 16:18:00 +00002261 bin=1;
Guido van Rossum43713e52000-02-29 13:59:29 +00002262 if (! PyArg_ParseTuple(args, "|i:Pickler", &bin)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002263 PyErr_Clear();
2264 bin=0;
Guido van Rossum43713e52000-02-29 13:59:29 +00002265 if (! PyArg_ParseTuple(args, "O|i:Pickler", &file, &bin))
Guido van Rossum053b8df1998-11-25 16:18:00 +00002266 return NULL;
2267 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002268 return (PyObject *)newPicklerobject(file, bin);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002269}
2270
2271
2272static void
Guido van Rossum60456fd1997-04-09 17:36:32 +00002273Pickler_dealloc(Picklerobject *self) {
2274 Py_XDECREF(self->write);
2275 Py_XDECREF(self->memo);
2276 Py_XDECREF(self->arg);
2277 Py_XDECREF(self->file);
2278 Py_XDECREF(self->pers_func);
2279 Py_XDECREF(self->inst_pers_func);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002280 Py_XDECREF(self->dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002281
Tim Peters84e87f32001-03-17 04:50:51 +00002282 if (self->write_buf) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002283 free(self->write_buf);
2284 }
2285
Guido van Rossumb18618d2000-05-03 23:44:39 +00002286 PyObject_Del(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002287}
2288
2289
2290static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002291Pickler_getattr(Picklerobject *self, char *name) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002292
2293 switch (*name) {
2294 case 'p':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002295 if (strcmp(name, "persistent_id") == 0) {
2296 if (!self->pers_func) {
2297 PyErr_SetString(PyExc_AttributeError, name);
2298 return NULL;
2299 }
2300
2301 Py_INCREF(self->pers_func);
2302 return self->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002303 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002304 break;
2305 case 'm':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002306 if (strcmp(name, "memo") == 0) {
2307 if (!self->memo) {
2308 PyErr_SetString(PyExc_AttributeError, name);
2309 return NULL;
2310 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002311
Guido van Rossum60456fd1997-04-09 17:36:32 +00002312 Py_INCREF(self->memo);
2313 return self->memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002314 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002315 break;
2316 case 'P':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002317 if (strcmp(name, "PicklingError") == 0) {
2318 Py_INCREF(PicklingError);
2319 return PicklingError;
2320 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002321 break;
2322 case 'b':
2323 if (strcmp(name, "binary")==0)
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002324 return PyInt_FromLong(self->bin);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002325 break;
2326 case 'f':
2327 if (strcmp(name, "fast")==0)
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002328 return PyInt_FromLong(self->fast);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002329 break;
2330 case 'g':
2331 if (strcmp(name, "getvalue")==0 && ! Pdata_Check(self->file)) {
2332 PyErr_SetString(PyExc_AttributeError, name);
2333 return NULL;
2334 }
2335 break;
2336 }
2337 return Py_FindMethod(Pickler_methods, (PyObject *)self, name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002338}
2339
2340
Tim Peters84e87f32001-03-17 04:50:51 +00002341int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002342Pickler_setattr(Picklerobject *self, char *name, PyObject *value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002343
Guido van Rossum053b8df1998-11-25 16:18:00 +00002344 if (! value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002345 PyErr_SetString(PyExc_TypeError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00002346 "attribute deletion is not supported");
2347 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002348 }
Tim Peters84e87f32001-03-17 04:50:51 +00002349
Guido van Rossum60456fd1997-04-09 17:36:32 +00002350 if (strcmp(name, "persistent_id") == 0) {
2351 Py_XDECREF(self->pers_func);
2352 self->pers_func = value;
2353 Py_INCREF(value);
2354 return 0;
2355 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002356
Guido van Rossum60456fd1997-04-09 17:36:32 +00002357 if (strcmp(name, "inst_persistent_id") == 0) {
2358 Py_XDECREF(self->inst_pers_func);
2359 self->inst_pers_func = value;
2360 Py_INCREF(value);
2361 return 0;
2362 }
2363
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002364 if (strcmp(name, "memo") == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002365 if (! PyDict_Check(value)) {
2366 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
2367 return -1;
2368 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002369 Py_XDECREF(self->memo);
2370 self->memo = value;
2371 Py_INCREF(value);
2372 return 0;
2373 }
2374
Guido van Rossum053b8df1998-11-25 16:18:00 +00002375 if (strcmp(name, "binary")==0) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002376 self->bin=PyObject_IsTrue(value);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002377 return 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002378 }
2379
Guido van Rossum053b8df1998-11-25 16:18:00 +00002380 if (strcmp(name, "fast")==0) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002381 self->fast=PyObject_IsTrue(value);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002382 return 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002383 }
2384
Guido van Rossum60456fd1997-04-09 17:36:32 +00002385 PyErr_SetString(PyExc_AttributeError, name);
2386 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002387}
2388
2389
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002390static char Picklertype__doc__[] =
2391"Objects that know how to pickle objects\n"
2392;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002393
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002394static PyTypeObject Picklertype = {
2395 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00002396 0, /*ob_size*/
2397 "Pickler", /*tp_name*/
2398 sizeof(Picklerobject), /*tp_basicsize*/
2399 0, /*tp_itemsize*/
2400 /* methods */
2401 (destructor)Pickler_dealloc, /*tp_dealloc*/
2402 (printfunc)0, /*tp_print*/
2403 (getattrfunc)Pickler_getattr, /*tp_getattr*/
2404 (setattrfunc)Pickler_setattr, /*tp_setattr*/
2405 (cmpfunc)0, /*tp_compare*/
2406 (reprfunc)0, /*tp_repr*/
2407 0, /*tp_as_number*/
2408 0, /*tp_as_sequence*/
2409 0, /*tp_as_mapping*/
2410 (hashfunc)0, /*tp_hash*/
2411 (ternaryfunc)0, /*tp_call*/
2412 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002413
Guido van Rossum60456fd1997-04-09 17:36:32 +00002414 /* Space for future expansion */
2415 0L,0L,0L,0L,
2416 Picklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002417};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002418
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002419static PyObject *
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002420find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc) {
Guido van Rossume2d81cd1998-08-08 19:40:10 +00002421 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002422
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002423 if (fc) {
2424 if (fc==Py_None) {
Tim Peters84e87f32001-03-17 04:50:51 +00002425 PyErr_SetString(UnpicklingError,
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002426 "Global and instance pickles are not supported.");
2427 return NULL;
2428 }
2429 return PyObject_CallFunction(fc, "OO", py_module_name, py_global_name);
2430 }
2431
Jeremy Hyltond1055231998-08-11 19:52:51 +00002432 module = PySys_GetObject("modules");
2433 if (module == NULL)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002434 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00002435
2436 module = PyDict_GetItem(module, py_module_name);
2437 if (module == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002438 module = PyImport_Import(py_module_name);
2439 if (!module)
2440 return NULL;
2441 global = PyObject_GetAttr(module, py_global_name);
2442 Py_DECREF(module);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002443 }
Jeremy Hyltond1055231998-08-11 19:52:51 +00002444 else
Guido van Rossum053b8df1998-11-25 16:18:00 +00002445 global = PyObject_GetAttr(module, py_global_name);
Jeremy Hyltond1055231998-08-11 19:52:51 +00002446 if (global == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002447 char buf[256 + 37];
2448 sprintf(buf, "Failed to import class %.128s from module %.128s",
2449 PyString_AS_STRING((PyStringObject*)py_global_name),
Tim Peters84e87f32001-03-17 04:50:51 +00002450 PyString_AS_STRING((PyStringObject*)py_module_name));
Guido van Rossum053b8df1998-11-25 16:18:00 +00002451 PyErr_SetString(PyExc_SystemError, buf);
2452 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00002453 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002454 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002455}
2456
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002457static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002458marker(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002459 if (self->num_marks < 1) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002460 PyErr_SetString(UnpicklingError, "could not find MARK");
2461 return -1;
2462 }
2463
2464 return self->marks[--self->num_marks];
2465}
2466
Tim Peters84e87f32001-03-17 04:50:51 +00002467
Guido van Rossum60456fd1997-04-09 17:36:32 +00002468static int
2469load_none(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002470 PDATA_APPEND(self->stack, Py_None, -1);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002471 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002472}
2473
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002474static int
Thomas Wouters58d05102000-07-24 14:43:35 +00002475bad_readline(void) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002476 PyErr_SetString(UnpicklingError, "pickle data was truncated");
2477 return -1;
2478}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002479
2480static int
2481load_int(Unpicklerobject *self) {
2482 PyObject *py_int = 0;
2483 char *endptr, *s;
2484 int len, res = -1;
2485 long l;
2486
2487 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002488 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002489 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002490
2491 errno = 0;
2492 l = strtol(s, &endptr, 0);
2493
2494 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
2495 /* Hm, maybe we've got something long. Let's try reading
Guido van Rossum053b8df1998-11-25 16:18:00 +00002496 it as a Python long object. */
Guido van Rossum60456fd1997-04-09 17:36:32 +00002497 errno=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002498 UNLESS (py_int=PyLong_FromString(s,&endptr,0)) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002499
Guido van Rossum053b8df1998-11-25 16:18:00 +00002500 if ((*endptr != '\n') || (endptr[1] != '\0')) {
2501 PyErr_SetString(PyExc_ValueError,
2502 "could not convert string to int");
2503 goto finally;
2504 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002505 }
2506 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002507 UNLESS (py_int = PyInt_FromLong(l)) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002508 }
2509
Guido van Rossum053b8df1998-11-25 16:18:00 +00002510 free(s);
2511 PDATA_PUSH(self->stack, py_int, -1);
2512 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002513
2514finally:
2515 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002516
2517 return res;
2518}
2519
2520
Tim Peters84e87f32001-03-17 04:50:51 +00002521static long
Guido van Rossum60456fd1997-04-09 17:36:32 +00002522calc_binint(char *s, int x) {
2523 unsigned char c;
2524 int i;
2525 long l;
2526
2527 for (i = 0, l = 0L; i < x; i++) {
2528 c = (unsigned char)s[i];
2529 l |= (long)c << (i * 8);
2530 }
2531
2532 return l;
2533}
2534
2535
2536static int
2537load_binintx(Unpicklerobject *self, char *s, int x) {
2538 PyObject *py_int = 0;
2539 long l;
2540
2541 l = calc_binint(s, x);
2542
Guido van Rossum053b8df1998-11-25 16:18:00 +00002543 UNLESS (py_int = PyInt_FromLong(l))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002544 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002545
Guido van Rossum053b8df1998-11-25 16:18:00 +00002546 PDATA_PUSH(self->stack, py_int, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002547 return 0;
2548}
2549
2550
2551static int
2552load_binint(Unpicklerobject *self) {
2553 char *s;
2554
2555 if ((*self->read_func)(self, &s, 4) < 0)
2556 return -1;
2557
2558 return load_binintx(self, s, 4);
2559}
2560
2561
2562static int
2563load_binint1(Unpicklerobject *self) {
2564 char *s;
2565
2566 if ((*self->read_func)(self, &s, 1) < 0)
2567 return -1;
2568
2569 return load_binintx(self, s, 1);
2570}
2571
2572
2573static int
2574load_binint2(Unpicklerobject *self) {
2575 char *s;
2576
2577 if ((*self->read_func)(self, &s, 2) < 0)
2578 return -1;
2579
2580 return load_binintx(self, s, 2);
2581}
Tim Peters84e87f32001-03-17 04:50:51 +00002582
Guido van Rossum60456fd1997-04-09 17:36:32 +00002583static int
2584load_long(Unpicklerobject *self) {
2585 PyObject *l = 0;
2586 char *end, *s;
2587 int len, res = -1;
2588
Guido van Rossum60456fd1997-04-09 17:36:32 +00002589 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002590 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002591 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002592
Guido van Rossum053b8df1998-11-25 16:18:00 +00002593 UNLESS (l = PyLong_FromString(s, &end, 0))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002594 goto finally;
2595
Guido van Rossum053b8df1998-11-25 16:18:00 +00002596 free(s);
2597 PDATA_PUSH(self->stack, l, -1);
2598 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002599
2600finally:
2601 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002602
2603 return res;
2604}
2605
Tim Peters84e87f32001-03-17 04:50:51 +00002606
Guido van Rossum60456fd1997-04-09 17:36:32 +00002607static int
2608load_float(Unpicklerobject *self) {
2609 PyObject *py_float = 0;
2610 char *endptr, *s;
2611 int len, res = -1;
2612 double d;
2613
2614 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002615 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002616 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002617
2618 errno = 0;
2619 d = strtod(s, &endptr);
2620
2621 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
Tim Peters84e87f32001-03-17 04:50:51 +00002622 PyErr_SetString(PyExc_ValueError,
Guido van Rossum60456fd1997-04-09 17:36:32 +00002623 "could not convert string to float");
2624 goto finally;
2625 }
2626
Guido van Rossum053b8df1998-11-25 16:18:00 +00002627 UNLESS (py_float = PyFloat_FromDouble(d))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002628 goto finally;
2629
Guido van Rossum053b8df1998-11-25 16:18:00 +00002630 free(s);
2631 PDATA_PUSH(self->stack, py_float, -1);
2632 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002633
2634finally:
2635 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002636
2637 return res;
2638}
2639
Guido van Rossum60456fd1997-04-09 17:36:32 +00002640static int
2641load_binfloat(Unpicklerobject *self) {
2642 PyObject *py_float = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002643 int s, e;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002644 long fhi, flo;
2645 double x;
2646 char *p;
2647
2648 if ((*self->read_func)(self, &p, 8) < 0)
2649 return -1;
2650
2651 /* First byte */
2652 s = (*p>>7) & 1;
2653 e = (*p & 0x7F) << 4;
2654 p++;
2655
2656 /* Second byte */
2657 e |= (*p>>4) & 0xF;
2658 fhi = (*p & 0xF) << 24;
2659 p++;
2660
2661 /* Third byte */
2662 fhi |= (*p & 0xFF) << 16;
2663 p++;
2664
2665 /* Fourth byte */
2666 fhi |= (*p & 0xFF) << 8;
2667 p++;
2668
2669 /* Fifth byte */
2670 fhi |= *p & 0xFF;
2671 p++;
2672
2673 /* Sixth byte */
2674 flo = (*p & 0xFF) << 16;
2675 p++;
2676
2677 /* Seventh byte */
2678 flo |= (*p & 0xFF) << 8;
2679 p++;
2680
2681 /* Eighth byte */
2682 flo |= *p & 0xFF;
2683
2684 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
2685 x /= 268435456.0; /* 2**28 */
2686
2687 /* XXX This sadly ignores Inf/NaN */
2688 if (e == 0)
2689 e = -1022;
2690 else {
2691 x += 1.0;
2692 e -= 1023;
2693 }
2694 x = ldexp(x, e);
2695
2696 if (s)
2697 x = -x;
2698
Guido van Rossum053b8df1998-11-25 16:18:00 +00002699 UNLESS (py_float = PyFloat_FromDouble(x)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002700
Guido van Rossum053b8df1998-11-25 16:18:00 +00002701 PDATA_PUSH(self->stack, py_float, -1);
2702 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002703}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002704
2705static int
2706load_string(Unpicklerobject *self) {
2707 PyObject *str = 0;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002708 int len, res = -1, nslash;
2709 char *s, q, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002710
2711 static PyObject *eval_dict = 0;
2712
2713 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002714 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002715 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002716
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002717 /* Check for unquoted quotes (evil strings) */
2718 q=*s;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002719 if (q != '"' && q != '\'') goto insecure;
2720 for (p=s+1, nslash=0; *p; p++) {
2721 if (*p==q && nslash%2==0) break;
2722 if (*p=='\\') nslash++;
2723 else nslash=0;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002724 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002725 if (*p==q)
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002726 {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002727 for (p++; *p; p++) if (*p > ' ') goto insecure;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002728 }
2729 else goto insecure;
2730 /********************************************/
2731
Guido van Rossum053b8df1998-11-25 16:18:00 +00002732 UNLESS (eval_dict)
2733 UNLESS (eval_dict = Py_BuildValue("{s{}}", "__builtins__"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002734 goto finally;
2735
Guido van Rossum053b8df1998-11-25 16:18:00 +00002736 UNLESS (str = PyRun_String(s, Py_eval_input, eval_dict, eval_dict))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002737 goto finally;
2738
Guido van Rossum053b8df1998-11-25 16:18:00 +00002739 free(s);
2740 PDATA_PUSH(self->stack, str, -1);
2741 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002742
2743finally:
2744 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002745
2746 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00002747
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002748insecure:
2749 free(s);
2750 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
2751 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00002752}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002753
2754
2755static int
2756load_binstring(Unpicklerobject *self) {
2757 PyObject *py_string = 0;
2758 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002759 char *s;
2760
Guido van Rossum053b8df1998-11-25 16:18:00 +00002761 if ((*self->read_func)(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002762
2763 l = calc_binint(s, 4);
2764
2765 if ((*self->read_func)(self, &s, l) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002766 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002767
Guido van Rossum053b8df1998-11-25 16:18:00 +00002768 UNLESS (py_string = PyString_FromStringAndSize(s, l))
2769 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002770
Guido van Rossum053b8df1998-11-25 16:18:00 +00002771 PDATA_PUSH(self->stack, py_string, -1);
2772 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002773}
2774
2775
2776static int
2777load_short_binstring(Unpicklerobject *self) {
2778 PyObject *py_string = 0;
Tim Peters84e87f32001-03-17 04:50:51 +00002779 unsigned char l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002780 char *s;
2781
2782 if ((*self->read_func)(self, &s, 1) < 0)
2783 return -1;
2784
2785 l = (unsigned char)s[0];
2786
Guido van Rossum053b8df1998-11-25 16:18:00 +00002787 if ((*self->read_func)(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002788
Guido van Rossum053b8df1998-11-25 16:18:00 +00002789 UNLESS (py_string = PyString_FromStringAndSize(s, l)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002790
Guido van Rossum053b8df1998-11-25 16:18:00 +00002791 PDATA_PUSH(self->stack, py_string, -1);
2792 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00002793}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002794
2795
2796static int
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002797load_unicode(Unpicklerobject *self) {
2798 PyObject *str = 0;
2799 int len, res = -1;
2800 char *s;
2801
2802 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00002803 if (len < 1) return bad_readline();
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002804
2805 UNLESS (str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL))
2806 goto finally;
2807
2808 PDATA_PUSH(self->stack, str, -1);
2809 return 0;
2810
2811finally:
2812 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00002813}
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002814
2815
2816static int
2817load_binunicode(Unpicklerobject *self) {
2818 PyObject *unicode;
2819 long l;
2820 char *s;
2821
2822 if ((*self->read_func)(self, &s, 4) < 0) return -1;
2823
2824 l = calc_binint(s, 4);
2825
2826 if ((*self->read_func)(self, &s, l) < 0)
2827 return -1;
2828
2829 UNLESS (unicode = PyUnicode_DecodeUTF8(s, l, NULL))
2830 return -1;
2831
2832 PDATA_PUSH(self->stack, unicode, -1);
2833 return 0;
2834}
2835
2836
2837static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002838load_tuple(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002839 PyObject *tup;
2840 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002841
Guido van Rossum053b8df1998-11-25 16:18:00 +00002842 if ((i = marker(self)) < 0) return -1;
2843 UNLESS (tup=Pdata_popTuple(self->stack, i)) return -1;
2844 PDATA_PUSH(self->stack, tup, -1);
2845 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002846}
2847
2848static int
2849load_empty_tuple(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002850 PyObject *tup;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002851
Guido van Rossum053b8df1998-11-25 16:18:00 +00002852 UNLESS (tup=PyTuple_New(0)) return -1;
2853 PDATA_PUSH(self->stack, tup, -1);
2854 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002855}
2856
2857static int
2858load_empty_list(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002859 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002860
Guido van Rossum053b8df1998-11-25 16:18:00 +00002861 UNLESS (list=PyList_New(0)) return -1;
2862 PDATA_PUSH(self->stack, list, -1);
2863 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002864}
2865
2866static int
2867load_empty_dict(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002868 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002869
Guido van Rossum053b8df1998-11-25 16:18:00 +00002870 UNLESS (dict=PyDict_New()) return -1;
2871 PDATA_PUSH(self->stack, dict, -1);
2872 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002873}
2874
2875
2876static int
2877load_list(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002878 PyObject *list = 0;
2879 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002880
Guido van Rossum053b8df1998-11-25 16:18:00 +00002881 if ((i = marker(self)) < 0) return -1;
2882 UNLESS (list=Pdata_popList(self->stack, i)) return -1;
2883 PDATA_PUSH(self->stack, list, -1);
2884 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002885}
2886
2887static int
2888load_dict(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002889 PyObject *dict, *key, *value;
2890 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002891
Guido van Rossum053b8df1998-11-25 16:18:00 +00002892 if ((i = marker(self)) < 0) return -1;
2893 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002894
Guido van Rossum053b8df1998-11-25 16:18:00 +00002895 UNLESS (dict = PyDict_New()) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002896
Guido van Rossum053b8df1998-11-25 16:18:00 +00002897 for (k = i+1; k < j; k += 2) {
2898 key =self->stack->data[k-1];
2899 value=self->stack->data[k ];
2900 if (PyDict_SetItem(dict, key, value) < 0) {
2901 Py_DECREF(dict);
2902 return -1;
2903 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002904 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002905 Pdata_clear(self->stack, i);
2906 PDATA_PUSH(self->stack, dict, -1);
2907 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002908}
2909
2910static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002911Instance_New(PyObject *cls, PyObject *args) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002912 int has_key;
2913 PyObject *safe=0, *r=0;
2914
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002915 if (PyClass_Check(cls)) {
2916 int l;
Tim Peters84e87f32001-03-17 04:50:51 +00002917
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002918 if ((l=PyObject_Size(args)) < 0) goto err;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002919 UNLESS (l) {
2920 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002921
Guido van Rossum053b8df1998-11-25 16:18:00 +00002922 UNLESS (__getinitargs__=PyObject_GetAttr(cls, __getinitargs___str)) {
2923 /* We have a class with no __getinitargs__, so bypass usual
2924 construction */
2925 PyInstanceObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002926
Guido van Rossum053b8df1998-11-25 16:18:00 +00002927 PyErr_Clear();
Guido van Rossumb18618d2000-05-03 23:44:39 +00002928 UNLESS (inst=PyObject_New(PyInstanceObject, &PyInstance_Type))
Guido van Rossum053b8df1998-11-25 16:18:00 +00002929 goto err;
2930 inst->in_class=(PyClassObject*)cls;
2931 Py_INCREF(cls);
2932 UNLESS (inst->in_dict=PyDict_New()) {
Neil Schemenauer5196c582000-10-04 16:22:26 +00002933 inst = (PyInstanceObject *) PyObject_AS_GC(inst);
2934 PyObject_DEL(inst);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002935 goto err;
2936 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00002937 PyObject_GC_Init(inst);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002938 return (PyObject *)inst;
2939 }
2940 Py_DECREF(__getinitargs__);
2941 }
Tim Peters84e87f32001-03-17 04:50:51 +00002942
Guido van Rossum053b8df1998-11-25 16:18:00 +00002943 if ((r=PyInstance_New(cls, args, NULL))) return r;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002944 else goto err;
2945 }
Tim Peters84e87f32001-03-17 04:50:51 +00002946
2947
Guido van Rossum60456fd1997-04-09 17:36:32 +00002948 if ((has_key = cPickle_PyMapping_HasKey(safe_constructors, cls)) < 0)
2949 goto err;
Tim Peters84e87f32001-03-17 04:50:51 +00002950
Guido van Rossum60456fd1997-04-09 17:36:32 +00002951 if (!has_key)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002952 if (!(safe = PyObject_GetAttr(cls, __safe_for_unpickling___str)) ||
Guido van Rossum60456fd1997-04-09 17:36:32 +00002953 !PyObject_IsTrue(safe)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002954 cPickle_ErrFormat(UnpicklingError,
2955 "%s is not safe for unpickling", "O", cls);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002956 Py_XDECREF(safe);
2957 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002958 }
2959
Guido van Rossum053b8df1998-11-25 16:18:00 +00002960 if (args==Py_None) {
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002961 /* Special case, call cls.__basicnew__() */
2962 PyObject *basicnew;
Tim Peters84e87f32001-03-17 04:50:51 +00002963
Guido van Rossum053b8df1998-11-25 16:18:00 +00002964 UNLESS (basicnew=PyObject_GetAttr(cls, __basicnew___str)) return NULL;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002965 r=PyObject_CallObject(basicnew, NULL);
2966 Py_DECREF(basicnew);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002967 if (r) return r;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002968 }
2969
Guido van Rossum053b8df1998-11-25 16:18:00 +00002970 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00002971
Guido van Rossum60456fd1997-04-09 17:36:32 +00002972err:
2973 {
2974 PyObject *tp, *v, *tb;
2975
2976 PyErr_Fetch(&tp, &v, &tb);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002977 if ((r=Py_BuildValue("OOO",v,cls,args))) {
2978 Py_XDECREF(v);
2979 v=r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002980 }
2981 PyErr_Restore(tp,v,tb);
2982 }
2983 return NULL;
2984}
Tim Peters84e87f32001-03-17 04:50:51 +00002985
Guido van Rossum60456fd1997-04-09 17:36:32 +00002986
2987static int
2988load_obj(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002989 PyObject *class, *tup, *obj=0;
2990 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002991
Guido van Rossum053b8df1998-11-25 16:18:00 +00002992 if ((i = marker(self)) < 0) return -1;
2993 UNLESS (tup=Pdata_popTuple(self->stack, i+1)) return -1;
2994 PDATA_POP(self->stack, class);
2995 if (class) {
2996 obj = Instance_New(class, tup);
2997 Py_DECREF(class);
2998 }
2999 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003000
Guido van Rossum053b8df1998-11-25 16:18:00 +00003001 if (! obj) return -1;
3002 PDATA_PUSH(self->stack, obj, -1);
3003 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003004}
3005
3006
3007static int
3008load_inst(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003009 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003010 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003011 char *s;
3012
Guido van Rossum053b8df1998-11-25 16:18:00 +00003013 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003014
Guido van Rossum053b8df1998-11-25 16:18:00 +00003015 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003016 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00003017 UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003018
Guido van Rossum053b8df1998-11-25 16:18:00 +00003019 if ((len = (*self->readline_func)(self, &s)) >= 0) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003020 if (len < 2) return bad_readline();
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003021 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003022 class = find_class(module_name, class_name, self->find_class);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003023 Py_DECREF(class_name);
3024 }
3025 }
3026 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003027
Guido van Rossum053b8df1998-11-25 16:18:00 +00003028 if (! class) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003029
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003030 if ((tup=Pdata_popTuple(self->stack, i))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003031 obj = Instance_New(class, tup);
3032 Py_DECREF(tup);
3033 }
3034 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003035
Guido van Rossum053b8df1998-11-25 16:18:00 +00003036 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003037
Guido van Rossum053b8df1998-11-25 16:18:00 +00003038 PDATA_PUSH(self->stack, obj, -1);
3039 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003040}
3041
3042
3043static int
3044load_global(Unpicklerobject *self) {
3045 PyObject *class = 0, *module_name = 0, *class_name = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003046 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003047 char *s;
3048
Guido van Rossum053b8df1998-11-25 16:18:00 +00003049 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003050 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00003051 UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003052
Guido van Rossum053b8df1998-11-25 16:18:00 +00003053 if ((len = (*self->readline_func)(self, &s)) >= 0) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003054 if (len < 2) return bad_readline();
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003055 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003056 class = find_class(module_name, class_name, self->find_class);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003057 Py_DECREF(class_name);
3058 }
3059 }
3060 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003061
Guido van Rossum053b8df1998-11-25 16:18:00 +00003062 if (! class) return -1;
3063 PDATA_PUSH(self->stack, class, -1);
3064 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003065}
3066
3067
3068static int
3069load_persid(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003070 PyObject *pid = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003071 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003072 char *s;
3073
3074 if (self->pers_func) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003075 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003076 if (len < 2) return bad_readline();
Tim Peters84e87f32001-03-17 04:50:51 +00003077
Guido van Rossum053b8df1998-11-25 16:18:00 +00003078 UNLESS (pid = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003079
Guido van Rossum053b8df1998-11-25 16:18:00 +00003080 if (PyList_Check(self->pers_func)) {
3081 if (PyList_Append(self->pers_func, pid) < 0) {
3082 Py_DECREF(pid);
3083 return -1;
3084 }
3085 }
3086 else {
3087 ARG_TUP(self, pid);
3088 if (self->arg) {
3089 pid = PyObject_CallObject(self->pers_func, self->arg);
3090 FREE_ARG_TUP(self);
3091 }
3092 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003093
Guido van Rossum053b8df1998-11-25 16:18:00 +00003094 if (! pid) return -1;
3095
3096 PDATA_PUSH(self->stack, pid, -1);
3097 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003098 }
3099 else {
3100 PyErr_SetString(UnpicklingError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00003101 "A load persistent id instruction was encountered,\n"
3102 "but no persistent_load function was specified.");
3103 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003104 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003105}
3106
Guido van Rossum60456fd1997-04-09 17:36:32 +00003107static int
3108load_binpersid(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003109 PyObject *pid = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003110
3111 if (self->pers_func) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003112 PDATA_POP(self->stack, pid);
3113 if (! pid) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003114
Guido van Rossum053b8df1998-11-25 16:18:00 +00003115 if (PyList_Check(self->pers_func)) {
3116 if (PyList_Append(self->pers_func, pid) < 0) {
3117 Py_DECREF(pid);
3118 return -1;
3119 }
3120 }
3121 else {
3122 ARG_TUP(self, pid);
3123 if (self->arg) {
3124 pid = PyObject_CallObject(self->pers_func, self->arg);
3125 FREE_ARG_TUP(self);
3126 }
3127 if (! pid) return -1;
3128 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003129
Guido van Rossum053b8df1998-11-25 16:18:00 +00003130 PDATA_PUSH(self->stack, pid, -1);
3131 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003132 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003133 else {
3134 PyErr_SetString(UnpicklingError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00003135 "A load persistent id instruction was encountered,\n"
3136 "but no persistent_load function was specified.");
3137 return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003138 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003139}
3140
3141
3142static int
3143load_pop(Unpicklerobject *self) {
3144 int len;
3145
Guido van Rossum053b8df1998-11-25 16:18:00 +00003146 UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003147
Tim Peters84e87f32001-03-17 04:50:51 +00003148 /* Note that we split the (pickle.py) stack into two stacks,
Guido van Rossumea2b7152000-05-09 18:14:50 +00003149 an object stack and a mark stack. We have to be clever and
3150 pop the right one. We do this by looking at the top of the
3151 mark stack.
3152 */
3153
Tim Peters84e87f32001-03-17 04:50:51 +00003154 if ((self->num_marks > 0) &&
Guido van Rossum60456fd1997-04-09 17:36:32 +00003155 (self->marks[self->num_marks - 1] == len))
3156 self->num_marks--;
Tim Peters84e87f32001-03-17 04:50:51 +00003157 else {
Guido van Rossumea2b7152000-05-09 18:14:50 +00003158 len--;
3159 Py_DECREF(self->stack->data[len]);
3160 self->stack->length=len;
3161 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003162
3163 return 0;
3164}
3165
3166
3167static int
3168load_pop_mark(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003169 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003170
3171 if ((i = marker(self)) < 0)
3172 return -1;
3173
Guido van Rossum053b8df1998-11-25 16:18:00 +00003174 Pdata_clear(self->stack, i);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003175
3176 return 0;
3177}
3178
3179
3180static int
3181load_dup(Unpicklerobject *self) {
3182 PyObject *last;
3183 int len;
3184
Guido van Rossum053b8df1998-11-25 16:18:00 +00003185 if ((len = self->stack->length) <= 0) return stackUnderflow();
3186 last=self->stack->data[len-1];
3187 Py_INCREF(last);
3188 PDATA_PUSH(self->stack, last, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003189 return 0;
3190}
3191
3192
3193static int
3194load_get(Unpicklerobject *self) {
3195 PyObject *py_str = 0, *value = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003196 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003197 char *s;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003198 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003199
Guido van Rossum053b8df1998-11-25 16:18:00 +00003200 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003201 if (len < 2) return bad_readline();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003202
Guido van Rossum053b8df1998-11-25 16:18:00 +00003203 UNLESS (py_str = PyString_FromStringAndSize(s, len - 1)) return -1;
3204
3205 value = PyDict_GetItem(self->memo, py_str);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003206 if (! value) {
3207 PyErr_SetObject(BadPickleGet, py_str);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003208 rc = -1;
3209 } else {
3210 PDATA_APPEND(self->stack, value, -1);
3211 rc = 0;
3212 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003213
Guido van Rossum2f80d961999-07-13 15:18:58 +00003214 Py_DECREF(py_str);
3215 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003216}
3217
3218
3219static int
3220load_binget(Unpicklerobject *self) {
3221 PyObject *py_key = 0, *value = 0;
3222 unsigned char key;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003223 char *s;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003224 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003225
Guido van Rossum053b8df1998-11-25 16:18:00 +00003226 if ((*self->read_func)(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003227
3228 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00003229 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003230
Guido van Rossum053b8df1998-11-25 16:18:00 +00003231 value = PyDict_GetItem(self->memo, py_key);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003232 if (! value) {
3233 PyErr_SetObject(BadPickleGet, py_key);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003234 rc = -1;
3235 } else {
3236 PDATA_APPEND(self->stack, value, -1);
3237 rc = 0;
3238 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003239
Guido van Rossum2f80d961999-07-13 15:18:58 +00003240 Py_DECREF(py_key);
3241 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003242}
3243
3244
3245static int
3246load_long_binget(Unpicklerobject *self) {
3247 PyObject *py_key = 0, *value = 0;
Thomas Wouters3b6448f2000-07-22 23:56:07 +00003248 unsigned char c;
3249 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003250 long key;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003251 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003252
Guido van Rossum053b8df1998-11-25 16:18:00 +00003253 if ((*self->read_func)(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003254
3255 c = (unsigned char)s[0];
3256 key = (long)c;
3257 c = (unsigned char)s[1];
3258 key |= (long)c << 8;
3259 c = (unsigned char)s[2];
3260 key |= (long)c << 16;
3261 c = (unsigned char)s[3];
3262 key |= (long)c << 24;
3263
Guido van Rossum053b8df1998-11-25 16:18:00 +00003264 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003265
Guido van Rossum053b8df1998-11-25 16:18:00 +00003266 value = PyDict_GetItem(self->memo, py_key);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003267 if (! value) {
3268 PyErr_SetObject(BadPickleGet, py_key);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003269 rc = -1;
3270 } else {
3271 PDATA_APPEND(self->stack, value, -1);
3272 rc = 0;
3273 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003274
Guido van Rossum2f80d961999-07-13 15:18:58 +00003275 Py_DECREF(py_key);
3276 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003277}
3278
3279
3280static int
3281load_put(Unpicklerobject *self) {
3282 PyObject *py_str = 0, *value = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003283 int len, l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003284 char *s;
3285
Guido van Rossum053b8df1998-11-25 16:18:00 +00003286 if ((l = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumd1f66dc1999-02-08 22:38:25 +00003287 if (l < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00003288 UNLESS (len=self->stack->length) return stackUnderflow();
3289 UNLESS (py_str = PyString_FromStringAndSize(s, l - 1)) return -1;
3290 value=self->stack->data[len-1];
3291 l=PyDict_SetItem(self->memo, py_str, value);
3292 Py_DECREF(py_str);
3293 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003294}
3295
3296
3297static int
3298load_binput(Unpicklerobject *self) {
3299 PyObject *py_key = 0, *value = 0;
Thomas Wouters3b6448f2000-07-22 23:56:07 +00003300 unsigned char key;
3301 char *s;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003302 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003303
Guido van Rossum053b8df1998-11-25 16:18:00 +00003304 if ((*self->read_func)(self, &s, 1) < 0) return -1;
3305 UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003306
3307 key = (unsigned char)s[0];
3308
Guido van Rossum053b8df1998-11-25 16:18:00 +00003309 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3310 value=self->stack->data[len-1];
3311 len=PyDict_SetItem(self->memo, py_key, value);
3312 Py_DECREF(py_key);
3313 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003314}
3315
3316
3317static int
3318load_long_binput(Unpicklerobject *self) {
3319 PyObject *py_key = 0, *value = 0;
3320 long key;
Thomas Wouters3b6448f2000-07-22 23:56:07 +00003321 unsigned char c;
3322 char *s;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003323 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003324
Guido van Rossum053b8df1998-11-25 16:18:00 +00003325 if ((*self->read_func)(self, &s, 4) < 0) return -1;
3326 UNLESS (len=self->stack->length) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003327
3328 c = (unsigned char)s[0];
3329 key = (long)c;
3330 c = (unsigned char)s[1];
3331 key |= (long)c << 8;
3332 c = (unsigned char)s[2];
3333 key |= (long)c << 16;
3334 c = (unsigned char)s[3];
3335 key |= (long)c << 24;
3336
Guido van Rossum053b8df1998-11-25 16:18:00 +00003337 UNLESS (py_key = PyInt_FromLong(key)) return -1;
3338 value=self->stack->data[len-1];
3339 len=PyDict_SetItem(self->memo, py_key, value);
3340 Py_DECREF(py_key);
3341 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003342}
3343
3344
Tim Peters84e87f32001-03-17 04:50:51 +00003345static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00003346do_append(Unpicklerobject *self, int x) {
3347 PyObject *value = 0, *list = 0, *append_method = 0;
3348 int len, i;
3349
Guido van Rossum053b8df1998-11-25 16:18:00 +00003350 UNLESS ((len=self->stack->length) >= x && x > 0) return stackUnderflow();
3351 if (len==x) return 0; /* nothing to do */
Guido van Rossum60456fd1997-04-09 17:36:32 +00003352
Guido van Rossum053b8df1998-11-25 16:18:00 +00003353 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003354
3355 if (PyList_Check(list)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003356 PyObject *slice;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003357 int list_len;
Tim Peters84e87f32001-03-17 04:50:51 +00003358
Guido van Rossum053b8df1998-11-25 16:18:00 +00003359 slice=Pdata_popList(self->stack, x);
3360 list_len = PyList_GET_SIZE(list);
3361 i=PyList_SetSlice(list, list_len, list_len, slice);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003362 Py_DECREF(slice);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003363 return i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003364 }
3365 else {
3366
Guido van Rossum053b8df1998-11-25 16:18:00 +00003367 UNLESS (append_method = PyObject_GetAttr(list, append_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00003368 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003369
Guido van Rossum60456fd1997-04-09 17:36:32 +00003370 for (i = x; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003371 PyObject *junk;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003372
Guido van Rossum053b8df1998-11-25 16:18:00 +00003373 value=self->stack->data[i];
3374 junk=0;
3375 ARG_TUP(self, value);
3376 if (self->arg) {
3377 junk = PyObject_CallObject(append_method, self->arg);
3378 FREE_ARG_TUP(self);
3379 }
3380 if (! junk) {
3381 Pdata_clear(self->stack, i+1);
3382 self->stack->length=x;
3383 Py_DECREF(append_method);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003384 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003385 }
3386 Py_DECREF(junk);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003387 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003388 self->stack->length=x;
3389 Py_DECREF(append_method);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003390 }
3391
Guido van Rossum60456fd1997-04-09 17:36:32 +00003392 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003393}
3394
Tim Peters84e87f32001-03-17 04:50:51 +00003395
Guido van Rossum60456fd1997-04-09 17:36:32 +00003396static int
3397load_append(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003398 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003399}
3400
3401
3402static int
3403load_appends(Unpicklerobject *self) {
3404 return do_append(self, marker(self));
3405}
3406
3407
3408static int
3409do_setitems(Unpicklerobject *self, int x) {
3410 PyObject *value = 0, *key = 0, *dict = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003411 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003412
Guido van Rossum053b8df1998-11-25 16:18:00 +00003413 UNLESS ((len=self->stack->length) >= x
3414 && x > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003415
Guido van Rossum053b8df1998-11-25 16:18:00 +00003416 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003417
Guido van Rossum053b8df1998-11-25 16:18:00 +00003418 for (i = x+1; i < len; i += 2) {
3419 key =self->stack->data[i-1];
3420 value=self->stack->data[i ];
3421 if (PyObject_SetItem(dict, key, value) < 0) {
3422 r=-1;
3423 break;
3424 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003425 }
3426
Guido van Rossum053b8df1998-11-25 16:18:00 +00003427 Pdata_clear(self->stack, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003428
Guido van Rossum053b8df1998-11-25 16:18:00 +00003429 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003430}
3431
3432
3433static int
3434load_setitem(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003435 return do_setitems(self, self->stack->length - 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003436}
3437
Guido van Rossum60456fd1997-04-09 17:36:32 +00003438static int
3439load_setitems(Unpicklerobject *self) {
3440 return do_setitems(self, marker(self));
3441}
3442
3443
3444static int
3445load_build(Unpicklerobject *self) {
Tim Peters84e87f32001-03-17 04:50:51 +00003446 PyObject *value = 0, *inst = 0, *instdict = 0, *d_key = 0, *d_value = 0,
Guido van Rossum60456fd1997-04-09 17:36:32 +00003447 *junk = 0, *__setstate__ = 0;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003448 int i, r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003449
Guido van Rossum053b8df1998-11-25 16:18:00 +00003450 if (self->stack->length < 2) return stackUnderflow();
3451 PDATA_POP(self->stack, value);
3452 if (! value) return -1;
3453 inst=self->stack->data[self->stack->length-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003454
Guido van Rossum053b8df1998-11-25 16:18:00 +00003455 if ((__setstate__ = PyObject_GetAttr(inst, __setstate___str))) {
3456 ARG_TUP(self, value);
3457 if (self->arg) {
3458 junk = PyObject_CallObject(__setstate__, self->arg);
3459 FREE_ARG_TUP(self);
3460 }
3461 Py_DECREF(__setstate__);
3462 if (! junk) return -1;
3463 Py_DECREF(junk);
3464 return 0;
3465 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003466
Guido van Rossum053b8df1998-11-25 16:18:00 +00003467 PyErr_Clear();
3468 if ((instdict = PyObject_GetAttr(inst, __dict___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003469 i = 0;
3470 while (PyDict_Next(value, &i, &d_key, &d_value)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003471 if (PyObject_SetItem(instdict, d_key, d_value) < 0) {
3472 r=-1;
3473 break;
3474 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003475 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003476 Py_DECREF(instdict);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003477 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003478 else r=-1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003479
Guido van Rossum053b8df1998-11-25 16:18:00 +00003480 Py_XDECREF(value);
Tim Peters84e87f32001-03-17 04:50:51 +00003481
Guido van Rossum053b8df1998-11-25 16:18:00 +00003482 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003483}
3484
3485
3486static int
3487load_mark(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003488 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003489
Guido van Rossumea2b7152000-05-09 18:14:50 +00003490 /* Note that we split the (pickle.py) stack into two stacks, an
3491 object stack and a mark stack. Here we push a mark onto the
Tim Peters84e87f32001-03-17 04:50:51 +00003492 mark stack.
Guido van Rossumea2b7152000-05-09 18:14:50 +00003493 */
3494
Guido van Rossum053b8df1998-11-25 16:18:00 +00003495 if ((self->num_marks + 1) >= self->marks_size) {
3496 s=self->marks_size+20;
3497 if (s <= self->num_marks) s=self->num_marks + 1;
Guido van Rossum761fcd01999-04-12 22:51:20 +00003498 if (self->marks == NULL)
Guido van Rossumaa8d1671999-01-25 21:43:51 +00003499 self->marks=(int *)malloc(s * sizeof(int));
3500 else
3501 self->marks=(int *)realloc(self->marks, s * sizeof(int));
Guido van Rossum053b8df1998-11-25 16:18:00 +00003502 if (! self->marks) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003503 PyErr_NoMemory();
3504 return -1;
3505 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003506 self->marks_size = s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003507 }
3508
Guido van Rossum053b8df1998-11-25 16:18:00 +00003509 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003510
3511 return 0;
3512}
3513
3514static int
3515load_reduce(Unpicklerobject *self) {
3516 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003517
Guido van Rossum053b8df1998-11-25 16:18:00 +00003518 PDATA_POP(self->stack, arg_tup);
3519 if (! arg_tup) return -1;
3520 PDATA_POP(self->stack, callable);
3521 if (callable) {
3522 ob = Instance_New(callable, arg_tup);
3523 Py_DECREF(callable);
3524 }
3525 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003526
Guido van Rossum053b8df1998-11-25 16:18:00 +00003527 if (! ob) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003528
Guido van Rossum053b8df1998-11-25 16:18:00 +00003529 PDATA_PUSH(self->stack, ob, -1);
3530 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003531}
Tim Peters84e87f32001-03-17 04:50:51 +00003532
Guido van Rossum60456fd1997-04-09 17:36:32 +00003533static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003534load(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003535 PyObject *err = 0, *val = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003536 char *s;
3537
Guido van Rossum60456fd1997-04-09 17:36:32 +00003538 self->num_marks = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003539 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003540
3541 while (1) {
3542 if ((*self->read_func)(self, &s, 1) < 0)
3543 break;
3544
3545 switch (s[0]) {
3546 case NONE:
3547 if (load_none(self) < 0)
3548 break;
3549 continue;
3550
3551 case BININT:
3552 if (load_binint(self) < 0)
3553 break;
3554 continue;
3555
3556 case BININT1:
3557 if (load_binint1(self) < 0)
3558 break;
3559 continue;
3560
3561 case BININT2:
3562 if (load_binint2(self) < 0)
3563 break;
3564 continue;
3565
3566 case INT:
3567 if (load_int(self) < 0)
3568 break;
3569 continue;
3570
3571 case LONG:
3572 if (load_long(self) < 0)
3573 break;
3574 continue;
3575
3576 case FLOAT:
3577 if (load_float(self) < 0)
3578 break;
3579 continue;
3580
Guido van Rossum60456fd1997-04-09 17:36:32 +00003581 case BINFLOAT:
3582 if (load_binfloat(self) < 0)
3583 break;
3584 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003585
3586 case BINSTRING:
3587 if (load_binstring(self) < 0)
3588 break;
3589 continue;
3590
3591 case SHORT_BINSTRING:
3592 if (load_short_binstring(self) < 0)
3593 break;
3594 continue;
3595
3596 case STRING:
3597 if (load_string(self) < 0)
3598 break;
3599 continue;
3600
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003601 case UNICODE:
3602 if (load_unicode(self) < 0)
3603 break;
3604 continue;
3605
3606 case BINUNICODE:
3607 if (load_binunicode(self) < 0)
3608 break;
3609 continue;
3610
Guido van Rossum60456fd1997-04-09 17:36:32 +00003611 case EMPTY_TUPLE:
3612 if (load_empty_tuple(self) < 0)
3613 break;
3614 continue;
3615
3616 case TUPLE:
3617 if (load_tuple(self) < 0)
3618 break;
3619 continue;
3620
3621 case EMPTY_LIST:
3622 if (load_empty_list(self) < 0)
3623 break;
3624 continue;
3625
3626 case LIST:
3627 if (load_list(self) < 0)
3628 break;
3629 continue;
3630
3631 case EMPTY_DICT:
3632 if (load_empty_dict(self) < 0)
3633 break;
3634 continue;
3635
3636 case DICT:
3637 if (load_dict(self) < 0)
3638 break;
3639 continue;
3640
3641 case OBJ:
3642 if (load_obj(self) < 0)
3643 break;
3644 continue;
3645
3646 case INST:
3647 if (load_inst(self) < 0)
3648 break;
3649 continue;
3650
3651 case GLOBAL:
3652 if (load_global(self) < 0)
3653 break;
3654 continue;
3655
3656 case APPEND:
3657 if (load_append(self) < 0)
3658 break;
3659 continue;
3660
3661 case APPENDS:
3662 if (load_appends(self) < 0)
3663 break;
3664 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003665
Guido van Rossum60456fd1997-04-09 17:36:32 +00003666 case BUILD:
3667 if (load_build(self) < 0)
3668 break;
3669 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003670
Guido van Rossum60456fd1997-04-09 17:36:32 +00003671 case DUP:
3672 if (load_dup(self) < 0)
3673 break;
3674 continue;
3675
3676 case BINGET:
3677 if (load_binget(self) < 0)
3678 break;
3679 continue;
3680
3681 case LONG_BINGET:
3682 if (load_long_binget(self) < 0)
3683 break;
3684 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003685
Guido van Rossum60456fd1997-04-09 17:36:32 +00003686 case GET:
3687 if (load_get(self) < 0)
3688 break;
3689 continue;
3690
3691 case MARK:
3692 if (load_mark(self) < 0)
3693 break;
3694 continue;
3695
3696 case BINPUT:
3697 if (load_binput(self) < 0)
3698 break;
3699 continue;
3700
3701 case LONG_BINPUT:
3702 if (load_long_binput(self) < 0)
3703 break;
3704 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003705
Guido van Rossum60456fd1997-04-09 17:36:32 +00003706 case PUT:
3707 if (load_put(self) < 0)
3708 break;
3709 continue;
3710
3711 case POP:
3712 if (load_pop(self) < 0)
3713 break;
3714 continue;
3715
3716 case POP_MARK:
3717 if (load_pop_mark(self) < 0)
3718 break;
3719 continue;
3720
3721 case SETITEM:
3722 if (load_setitem(self) < 0)
3723 break;
3724 continue;
3725
3726 case SETITEMS:
3727 if (load_setitems(self) < 0)
3728 break;
3729 continue;
3730
3731 case STOP:
3732 break;
3733
3734 case PERSID:
3735 if (load_persid(self) < 0)
3736 break;
3737 continue;
3738
3739 case BINPERSID:
3740 if (load_binpersid(self) < 0)
3741 break;
3742 continue;
3743
3744 case REDUCE:
3745 if (load_reduce(self) < 0)
3746 break;
3747 continue;
3748
Tim Peters84e87f32001-03-17 04:50:51 +00003749 default:
3750 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossum60456fd1997-04-09 17:36:32 +00003751 "c", s[0]);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003752 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003753 }
3754
3755 break;
3756 }
3757
Guido van Rossum053b8df1998-11-25 16:18:00 +00003758 if ((err = PyErr_Occurred())) {
3759 if (err == PyExc_EOFError) {
3760 PyErr_SetNone(PyExc_EOFError);
Tim Peters84e87f32001-03-17 04:50:51 +00003761 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003762 return NULL;
3763 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003764
Tim Peters84e87f32001-03-17 04:50:51 +00003765 PDATA_POP(self->stack, val);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003766 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003767}
Tim Peters84e87f32001-03-17 04:50:51 +00003768
Guido van Rossum60456fd1997-04-09 17:36:32 +00003769
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003770/* No-load functions to support noload, which is used to
3771 find persistent references. */
3772
3773static int
3774noload_obj(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003775 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003776
3777 if ((i = marker(self)) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003778 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003779}
3780
3781
3782static int
3783noload_inst(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003784 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003785 char *s;
3786
3787 if ((i = marker(self)) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003788 Pdata_clear(self->stack, i);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003789 if ((*self->readline_func)(self, &s) < 0) return -1;
3790 if ((*self->readline_func)(self, &s) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003791 PDATA_APPEND(self->stack, Py_None,-1);
3792 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003793}
3794
3795static int
3796noload_global(Unpicklerobject *self) {
3797 char *s;
3798
3799 if ((*self->readline_func)(self, &s) < 0) return -1;
3800 if ((*self->readline_func)(self, &s) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003801 PDATA_APPEND(self->stack, Py_None,-1);
3802 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003803}
3804
3805static int
3806noload_reduce(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003807
Guido van Rossum053b8df1998-11-25 16:18:00 +00003808 if (self->stack->length < 2) return stackUnderflow();
3809 Pdata_clear(self->stack, self->stack->length-2);
3810 PDATA_APPEND(self->stack, Py_None,-1);
3811 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003812}
3813
3814static int
3815noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003816
Guido van Rossum053b8df1998-11-25 16:18:00 +00003817 if (self->stack->length < 1) return stackUnderflow();
3818 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00003819 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003820}
3821
3822
3823static PyObject *
3824noload(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003825 PyObject *err = 0, *val = 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003826 char *s;
3827
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003828 self->num_marks = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003829 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003830
3831 while (1) {
3832 if ((*self->read_func)(self, &s, 1) < 0)
3833 break;
3834
3835 switch (s[0]) {
3836 case NONE:
3837 if (load_none(self) < 0)
3838 break;
3839 continue;
3840
3841 case BININT:
3842 if (load_binint(self) < 0)
3843 break;
3844 continue;
3845
3846 case BININT1:
3847 if (load_binint1(self) < 0)
3848 break;
3849 continue;
3850
3851 case BININT2:
3852 if (load_binint2(self) < 0)
3853 break;
3854 continue;
3855
3856 case INT:
3857 if (load_int(self) < 0)
3858 break;
3859 continue;
3860
3861 case LONG:
3862 if (load_long(self) < 0)
3863 break;
3864 continue;
3865
3866 case FLOAT:
3867 if (load_float(self) < 0)
3868 break;
3869 continue;
3870
3871 case BINFLOAT:
3872 if (load_binfloat(self) < 0)
3873 break;
3874 continue;
3875
3876 case BINSTRING:
3877 if (load_binstring(self) < 0)
3878 break;
3879 continue;
3880
3881 case SHORT_BINSTRING:
3882 if (load_short_binstring(self) < 0)
3883 break;
3884 continue;
3885
3886 case STRING:
3887 if (load_string(self) < 0)
3888 break;
3889 continue;
3890
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003891 case UNICODE:
3892 if (load_unicode(self) < 0)
3893 break;
3894 continue;
3895
3896 case BINUNICODE:
3897 if (load_binunicode(self) < 0)
3898 break;
3899 continue;
3900
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003901 case EMPTY_TUPLE:
3902 if (load_empty_tuple(self) < 0)
3903 break;
3904 continue;
3905
3906 case TUPLE:
3907 if (load_tuple(self) < 0)
3908 break;
3909 continue;
3910
3911 case EMPTY_LIST:
3912 if (load_empty_list(self) < 0)
3913 break;
3914 continue;
3915
3916 case LIST:
3917 if (load_list(self) < 0)
3918 break;
3919 continue;
3920
3921 case EMPTY_DICT:
3922 if (load_empty_dict(self) < 0)
3923 break;
3924 continue;
3925
3926 case DICT:
3927 if (load_dict(self) < 0)
3928 break;
3929 continue;
3930
3931 case OBJ:
3932 if (noload_obj(self) < 0)
3933 break;
3934 continue;
3935
3936 case INST:
3937 if (noload_inst(self) < 0)
3938 break;
3939 continue;
3940
3941 case GLOBAL:
3942 if (noload_global(self) < 0)
3943 break;
3944 continue;
3945
3946 case APPEND:
3947 if (load_append(self) < 0)
3948 break;
3949 continue;
3950
3951 case APPENDS:
3952 if (load_appends(self) < 0)
3953 break;
3954 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003955
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003956 case BUILD:
3957 if (noload_build(self) < 0)
3958 break;
3959 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003960
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003961 case DUP:
3962 if (load_dup(self) < 0)
3963 break;
3964 continue;
3965
3966 case BINGET:
3967 if (load_binget(self) < 0)
3968 break;
3969 continue;
3970
3971 case LONG_BINGET:
3972 if (load_long_binget(self) < 0)
3973 break;
3974 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003975
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003976 case GET:
3977 if (load_get(self) < 0)
3978 break;
3979 continue;
3980
3981 case MARK:
3982 if (load_mark(self) < 0)
3983 break;
3984 continue;
3985
3986 case BINPUT:
3987 if (load_binput(self) < 0)
3988 break;
3989 continue;
3990
3991 case LONG_BINPUT:
3992 if (load_long_binput(self) < 0)
3993 break;
3994 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003995
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003996 case PUT:
3997 if (load_put(self) < 0)
3998 break;
3999 continue;
4000
4001 case POP:
4002 if (load_pop(self) < 0)
4003 break;
4004 continue;
4005
4006 case POP_MARK:
4007 if (load_pop_mark(self) < 0)
4008 break;
4009 continue;
4010
4011 case SETITEM:
4012 if (load_setitem(self) < 0)
4013 break;
4014 continue;
4015
4016 case SETITEMS:
4017 if (load_setitems(self) < 0)
4018 break;
4019 continue;
4020
4021 case STOP:
4022 break;
4023
4024 case PERSID:
4025 if (load_persid(self) < 0)
4026 break;
4027 continue;
4028
4029 case BINPERSID:
4030 if (load_binpersid(self) < 0)
4031 break;
4032 continue;
4033
4034 case REDUCE:
4035 if (noload_reduce(self) < 0)
4036 break;
4037 continue;
4038
Tim Peters84e87f32001-03-17 04:50:51 +00004039 default:
4040 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004041 "c", s[0]);
Guido van Rossum053b8df1998-11-25 16:18:00 +00004042 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004043 }
4044
4045 break;
4046 }
4047
Guido van Rossum053b8df1998-11-25 16:18:00 +00004048 if ((err = PyErr_Occurred())) {
4049 if (err == PyExc_EOFError) {
4050 PyErr_SetNone(PyExc_EOFError);
Tim Peters84e87f32001-03-17 04:50:51 +00004051 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00004052 return NULL;
4053 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004054
Tim Peters84e87f32001-03-17 04:50:51 +00004055 PDATA_POP(self->stack, val);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004056 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004057}
Tim Peters84e87f32001-03-17 04:50:51 +00004058
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004059
Guido van Rossum60456fd1997-04-09 17:36:32 +00004060static PyObject *
4061Unpickler_load(Unpicklerobject *self, PyObject *args) {
Tim Peters84e87f32001-03-17 04:50:51 +00004062 UNLESS (PyArg_ParseTuple(args, ":load"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004063 return NULL;
4064
4065 return load(self);
4066}
4067
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004068static PyObject *
4069Unpickler_noload(Unpicklerobject *self, PyObject *args) {
Tim Peters84e87f32001-03-17 04:50:51 +00004070 UNLESS (PyArg_ParseTuple(args, ":noload"))
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004071 return NULL;
4072
4073 return noload(self);
4074}
4075
Guido van Rossum60456fd1997-04-09 17:36:32 +00004076
4077static struct PyMethodDef Unpickler_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004078 {"load", (PyCFunction)Unpickler_load, 1,
4079 "load() -- Load a pickle"
4080 },
4081 {"noload", (PyCFunction)Unpickler_noload, 1,
4082 "noload() -- not load a pickle, but go through most of the motions\n"
4083 "\n"
4084 "This function can be used to read past a pickle without instantiating\n"
4085 "any objects or importing any modules. It can also be used to find all\n"
4086 "persistent references without instantiating any objects or importing\n"
4087 "any modules.\n"
4088 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00004089 {NULL, NULL} /* sentinel */
4090};
4091
4092
4093static Unpicklerobject *
4094newUnpicklerobject(PyObject *f) {
4095 Unpicklerobject *self;
4096
Guido van Rossumb18618d2000-05-03 23:44:39 +00004097 UNLESS (self = PyObject_New(Unpicklerobject, &Unpicklertype))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004098 return NULL;
4099
4100 self->file = NULL;
4101 self->arg = NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00004102 self->stack = (Pdata*)Pdata_New();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004103 self->pers_func = NULL;
4104 self->last_string = NULL;
4105 self->marks = NULL;
4106 self->num_marks = 0;
4107 self->marks_size = 0;
4108 self->buf_size = 0;
4109 self->read = NULL;
Guido van Rossum8a6dba31998-03-06 01:39:39 +00004110 self->readline = NULL;
Guido van Rossum21ef0881998-12-11 03:20:00 +00004111 self->safe_constructors = NULL;
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004112 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004113
Tim Peters84e87f32001-03-17 04:50:51 +00004114 UNLESS (self->memo = PyDict_New())
Guido van Rossum83addc72000-04-21 20:49:36 +00004115 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004116
4117 Py_INCREF(f);
4118 self->file = f;
4119
4120 /* Set read, readline based on type of f */
4121 if (PyFile_Check(f)) {
4122 self->fp = PyFile_AsFile(f);
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00004123 if (self->fp == NULL) {
Guido van Rossum83addc72000-04-21 20:49:36 +00004124 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
4125 goto err;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00004126 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004127 self->read_func = read_file;
4128 self->readline_func = readline_file;
4129 }
4130 else if (PycStringIO_InputCheck(f)) {
4131 self->fp = NULL;
4132 self->read_func = read_cStringIO;
4133 self->readline_func = readline_cStringIO;
4134 }
4135 else {
4136
4137 self->fp = NULL;
4138 self->read_func = read_other;
4139 self->readline_func = readline_other;
4140
Guido van Rossum053b8df1998-11-25 16:18:00 +00004141 UNLESS ((self->readline = PyObject_GetAttr(f, readline_str)) &&
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004142 (self->read = PyObject_GetAttr(f, read_str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004143 PyErr_Clear();
4144 PyErr_SetString( PyExc_TypeError, "argument must have 'read' and "
4145 "'readline' attributes" );
Guido van Rossum053b8df1998-11-25 16:18:00 +00004146 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004147 }
4148 }
4149
Guido van Rossum053b8df1998-11-25 16:18:00 +00004150 if (PyEval_GetRestricted()) {
4151 /* Restricted execution, get private tables */
4152 PyObject *m;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004153
Guido van Rossum053b8df1998-11-25 16:18:00 +00004154 UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
4155 self->safe_constructors=PyObject_GetAttr(m, safe_constructors_str);
4156 Py_DECREF(m);
4157 UNLESS (self->safe_constructors) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004158 }
4159 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00004160 self->safe_constructors=safe_constructors;
4161 Py_INCREF(safe_constructors);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004162 }
4163
Guido van Rossum60456fd1997-04-09 17:36:32 +00004164 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004165
4166err:
4167 Py_DECREF((PyObject *)self);
4168 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004169}
4170
4171
4172static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004173get_Unpickler(PyObject *self, PyObject *args) {
4174 PyObject *file;
Tim Peters84e87f32001-03-17 04:50:51 +00004175
Guido van Rossum43713e52000-02-29 13:59:29 +00004176 UNLESS (PyArg_ParseTuple(args, "O:Unpickler", &file))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004177 return NULL;
4178 return (PyObject *)newUnpicklerobject(file);
4179}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004180
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004181
Guido van Rossum60456fd1997-04-09 17:36:32 +00004182static void
4183Unpickler_dealloc(Unpicklerobject *self) {
4184 Py_XDECREF(self->readline);
4185 Py_XDECREF(self->read);
4186 Py_XDECREF(self->file);
4187 Py_XDECREF(self->memo);
4188 Py_XDECREF(self->stack);
4189 Py_XDECREF(self->pers_func);
4190 Py_XDECREF(self->arg);
4191 Py_XDECREF(self->last_string);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004192 Py_XDECREF(self->safe_constructors);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004193
Guido van Rossum60456fd1997-04-09 17:36:32 +00004194 if (self->marks) {
4195 free(self->marks);
4196 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004197
Guido van Rossum60456fd1997-04-09 17:36:32 +00004198 if (self->buf_size) {
4199 free(self->buf);
4200 }
Tim Peters84e87f32001-03-17 04:50:51 +00004201
Guido van Rossumb18618d2000-05-03 23:44:39 +00004202 PyObject_Del(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004203}
4204
4205
4206static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004207Unpickler_getattr(Unpicklerobject *self, char *name) {
4208 if (!strcmp(name, "persistent_load")) {
4209 if (!self->pers_func) {
4210 PyErr_SetString(PyExc_AttributeError, name);
4211 return NULL;
4212 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004213
Guido van Rossum60456fd1997-04-09 17:36:32 +00004214 Py_INCREF(self->pers_func);
4215 return self->pers_func;
4216 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004217
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004218 if (!strcmp(name, "find_global")) {
4219 if (!self->find_class) {
4220 PyErr_SetString(PyExc_AttributeError, name);
4221 return NULL;
4222 }
4223
4224 Py_INCREF(self->find_class);
4225 return self->find_class;
4226 }
4227
Guido van Rossum60456fd1997-04-09 17:36:32 +00004228 if (!strcmp(name, "memo")) {
4229 if (!self->memo) {
4230 PyErr_SetString(PyExc_AttributeError, name);
4231 return NULL;
4232 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004233
Guido van Rossum60456fd1997-04-09 17:36:32 +00004234 Py_INCREF(self->memo);
4235 return self->memo;
4236 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004237
Guido van Rossum60456fd1997-04-09 17:36:32 +00004238 if (!strcmp(name, "UnpicklingError")) {
4239 Py_INCREF(UnpicklingError);
4240 return UnpicklingError;
4241 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004242
Guido van Rossum60456fd1997-04-09 17:36:32 +00004243 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
4244}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004245
Guido van Rossum60456fd1997-04-09 17:36:32 +00004246
4247static int
4248Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004249
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004250 if (!strcmp(name, "persistent_load")) {
4251 Py_XDECREF(self->pers_func);
4252 self->pers_func = value;
4253 Py_XINCREF(value);
4254 return 0;
4255 }
4256
4257 if (!strcmp(name, "find_global")) {
4258 Py_XDECREF(self->find_class);
4259 self->find_class = value;
4260 Py_XINCREF(value);
4261 return 0;
4262 }
4263
Guido van Rossum053b8df1998-11-25 16:18:00 +00004264 if (! value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004265 PyErr_SetString(PyExc_TypeError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00004266 "attribute deletion is not supported");
4267 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004268 }
4269
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004270 if (strcmp(name, "memo") == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00004271 if (! PyDict_Check(value)) {
4272 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
4273 return -1;
4274 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004275 Py_XDECREF(self->memo);
4276 self->memo = value;
4277 Py_INCREF(value);
4278 return 0;
4279 }
4280
Guido van Rossum60456fd1997-04-09 17:36:32 +00004281 PyErr_SetString(PyExc_AttributeError, name);
4282 return -1;
4283}
4284
4285
4286static PyObject *
4287cpm_dump(PyObject *self, PyObject *args) {
4288 PyObject *ob, *file, *res = NULL;
4289 Picklerobject *pickler = 0;
4290 int bin = 0;
4291
Guido van Rossum053b8df1998-11-25 16:18:00 +00004292 UNLESS (PyArg_ParseTuple(args, "OO|i", &ob, &file, &bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004293 goto finally;
4294
Guido van Rossum053b8df1998-11-25 16:18:00 +00004295 UNLESS (pickler = newPicklerobject(file, bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004296 goto finally;
4297
4298 if (dump(pickler, ob) < 0)
4299 goto finally;
4300
4301 Py_INCREF(Py_None);
4302 res = Py_None;
4303
4304finally:
4305 Py_XDECREF(pickler);
4306
4307 return res;
4308}
4309
4310
4311static PyObject *
4312cpm_dumps(PyObject *self, PyObject *args) {
4313 PyObject *ob, *file = 0, *res = NULL;
4314 Picklerobject *pickler = 0;
4315 int bin = 0;
4316
Guido van Rossum43713e52000-02-29 13:59:29 +00004317 UNLESS (PyArg_ParseTuple(args, "O|i:dumps", &ob, &bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004318 goto finally;
4319
Guido van Rossum053b8df1998-11-25 16:18:00 +00004320 UNLESS (file = PycStringIO->NewOutput(128))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004321 goto finally;
4322
Guido van Rossum053b8df1998-11-25 16:18:00 +00004323 UNLESS (pickler = newPicklerobject(file, bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004324 goto finally;
4325
4326 if (dump(pickler, ob) < 0)
4327 goto finally;
4328
4329 res = PycStringIO->cgetvalue(file);
4330
4331finally:
4332 Py_XDECREF(pickler);
4333 Py_XDECREF(file);
4334
4335 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00004336}
4337
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004338
4339static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004340cpm_load(PyObject *self, PyObject *args) {
4341 Unpicklerobject *unpickler = 0;
4342 PyObject *ob, *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004343
Guido van Rossum43713e52000-02-29 13:59:29 +00004344 UNLESS (PyArg_ParseTuple(args, "O:load", &ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004345 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004346
Guido van Rossum053b8df1998-11-25 16:18:00 +00004347 UNLESS (unpickler = newUnpicklerobject(ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004348 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004349
Guido van Rossum60456fd1997-04-09 17:36:32 +00004350 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004351
Guido van Rossum60456fd1997-04-09 17:36:32 +00004352finally:
4353 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004354
Guido van Rossum60456fd1997-04-09 17:36:32 +00004355 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004356}
4357
4358
4359static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004360cpm_loads(PyObject *self, PyObject *args) {
4361 PyObject *ob, *file = 0, *res = NULL;
4362 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004363
Guido van Rossum43713e52000-02-29 13:59:29 +00004364 UNLESS (PyArg_ParseTuple(args, "S:loads", &ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004365 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004366
Guido van Rossum053b8df1998-11-25 16:18:00 +00004367 UNLESS (file = PycStringIO->NewInput(ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004368 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00004369
Guido van Rossum053b8df1998-11-25 16:18:00 +00004370 UNLESS (unpickler = newUnpicklerobject(file))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004371 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004372
Guido van Rossum60456fd1997-04-09 17:36:32 +00004373 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004374
Guido van Rossum60456fd1997-04-09 17:36:32 +00004375finally:
4376 Py_XDECREF(file);
4377 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004378
Guido van Rossum60456fd1997-04-09 17:36:32 +00004379 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004380}
4381
4382
Tim Peters84e87f32001-03-17 04:50:51 +00004383static char Unpicklertype__doc__[] =
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004384"Objects that know how to unpickle";
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004385
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004386static PyTypeObject Unpicklertype = {
4387 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004388 0, /*ob_size*/
4389 "Unpickler", /*tp_name*/
4390 sizeof(Unpicklerobject), /*tp_basicsize*/
4391 0, /*tp_itemsize*/
4392 /* methods */
4393 (destructor)Unpickler_dealloc, /*tp_dealloc*/
4394 (printfunc)0, /*tp_print*/
4395 (getattrfunc)Unpickler_getattr, /*tp_getattr*/
4396 (setattrfunc)Unpickler_setattr, /*tp_setattr*/
4397 (cmpfunc)0, /*tp_compare*/
4398 (reprfunc)0, /*tp_repr*/
4399 0, /*tp_as_number*/
4400 0, /*tp_as_sequence*/
4401 0, /*tp_as_mapping*/
4402 (hashfunc)0, /*tp_hash*/
4403 (ternaryfunc)0, /*tp_call*/
4404 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004405
Guido van Rossum60456fd1997-04-09 17:36:32 +00004406 /* Space for future expansion */
4407 0L,0L,0L,0L,
4408 Unpicklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004409};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004410
Guido van Rossum60456fd1997-04-09 17:36:32 +00004411static struct PyMethodDef cPickle_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004412 {"dump", (PyCFunction)cpm_dump, 1,
4413 "dump(object, file, [binary]) --"
4414 "Write an object in pickle format to the given file\n"
4415 "\n"
4416 "If the optional argument, binary, is provided and is true, then the\n"
4417 "pickle will be written in binary format, which is more space and\n"
4418 "computationally efficient. \n"
4419 },
4420 {"dumps", (PyCFunction)cpm_dumps, 1,
4421 "dumps(object, [binary]) --"
4422 "Return a string containing an object in pickle format\n"
4423 "\n"
4424 "If the optional argument, binary, is provided and is true, then the\n"
4425 "pickle will be written in binary format, which is more space and\n"
4426 "computationally efficient. \n"
4427 },
4428 {"load", (PyCFunction)cpm_load, 1,
4429 "load(file) -- Load a pickle from the given file"},
4430 {"loads", (PyCFunction)cpm_loads, 1,
4431 "loads(string) -- Load a pickle from the given string"},
4432 {"Pickler", (PyCFunction)get_Pickler, 1,
4433 "Pickler(file, [binary]) -- Create a pickler\n"
4434 "\n"
4435 "If the optional argument, binary, is provided and is true, then\n"
4436 "pickles will be written in binary format, which is more space and\n"
4437 "computationally efficient. \n"
4438 },
4439 {"Unpickler", (PyCFunction)get_Unpickler, 1,
4440 "Unpickler(file) -- Create an unpickler"},
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004441 { NULL, NULL }
4442};
4443
Guido van Rossum60456fd1997-04-09 17:36:32 +00004444static int
Guido van Rossumebba4202000-09-07 14:35:37 +00004445init_stuff(PyObject *module_dict) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004446 PyObject *string, *copy_reg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004447
4448#define INIT_STR(S) UNLESS(S ## _str=PyString_FromString(#S)) return -1;
4449
4450 INIT_STR(__class__);
4451 INIT_STR(__getinitargs__);
4452 INIT_STR(__dict__);
4453 INIT_STR(__getstate__);
4454 INIT_STR(__setstate__);
4455 INIT_STR(__name__);
Guido van Rossum142eeb81997-08-13 03:14:41 +00004456 INIT_STR(__main__);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004457 INIT_STR(__reduce__);
4458 INIT_STR(write);
4459 INIT_STR(__safe_for_unpickling__);
4460 INIT_STR(append);
4461 INIT_STR(read);
4462 INIT_STR(readline);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004463 INIT_STR(copy_reg);
4464 INIT_STR(dispatch_table);
4465 INIT_STR(safe_constructors);
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004466 INIT_STR(__basicnew__);
Guido van Rossum053b8df1998-11-25 16:18:00 +00004467 UNLESS (empty_str=PyString_FromString("")) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004468
Guido van Rossum053b8df1998-11-25 16:18:00 +00004469 UNLESS (copy_reg = PyImport_ImportModule("copy_reg"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004470 return -1;
4471
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004472 /* These next few are special because we want to use different
4473 ones in restricted mode. */
4474
Guido van Rossum053b8df1998-11-25 16:18:00 +00004475 UNLESS (dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004476 return -1;
4477
Guido van Rossum053b8df1998-11-25 16:18:00 +00004478 UNLESS (safe_constructors = PyObject_GetAttr(copy_reg,
4479 safe_constructors_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004480 return -1;
4481
4482 Py_DECREF(copy_reg);
4483
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004484 /* Down to here ********************************** */
4485
Guido van Rossum053b8df1998-11-25 16:18:00 +00004486 UNLESS (string = PyImport_ImportModule("string"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004487 return -1;
4488
Guido van Rossum053b8df1998-11-25 16:18:00 +00004489 UNLESS (atol_func = PyObject_GetAttrString(string, "atol"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004490 return -1;
4491
4492 Py_DECREF(string);
4493
Guido van Rossum053b8df1998-11-25 16:18:00 +00004494 UNLESS (empty_tuple = PyTuple_New(0))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004495 return -1;
4496
Guido van Rossumc03158b1999-06-09 15:23:31 +00004497 /* Ugh */
4498 UNLESS (t=PyImport_ImportModule("__builtin__")) return -1;
4499 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
4500 return -1;
4501
4502 UNLESS (t=PyDict_New()) return -1;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004503 UNLESS (r=PyRun_String(
4504 "def __init__(self, *args): self.args=args\n\n"
4505 "def __str__(self):\n"
4506 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
4507 Py_file_input,
4508 module_dict, t) ) return -1;
4509 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00004510
4511 UNLESS (PickleError = PyErr_NewException("cPickle.PickleError", NULL, t))
4512 return -1;
4513
4514 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00004515
Tim Peters84e87f32001-03-17 04:50:51 +00004516
4517 UNLESS (PicklingError = PyErr_NewException("cPickle.PicklingError",
Guido van Rossumc03158b1999-06-09 15:23:31 +00004518 PickleError, NULL))
4519 return -1;
4520
4521 UNLESS (t=PyDict_New()) return -1;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004522 UNLESS (r=PyRun_String(
4523 "def __init__(self, *args): self.args=args\n\n"
4524 "def __str__(self):\n"
4525 " a=self.args\n"
4526 " a=a and type(a[0]) or '(what)'\n"
4527 " return 'Cannot pickle %s objects' % a\n"
4528 , Py_file_input,
4529 module_dict, t) ) return -1;
4530 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00004531
4532 UNLESS (UnpickleableError = PyErr_NewException(
4533 "cPickle.UnpickleableError", PicklingError, t))
4534 return -1;
4535
4536 Py_DECREF(t);
4537
Tim Peters84e87f32001-03-17 04:50:51 +00004538 UNLESS (UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
Guido van Rossumc03158b1999-06-09 15:23:31 +00004539 PickleError, NULL))
4540 return -1;
4541
Tim Peters84e87f32001-03-17 04:50:51 +00004542 if (PyDict_SetItemString(module_dict, "PickleError",
Guido van Rossumc03158b1999-06-09 15:23:31 +00004543 PickleError) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004544 return -1;
4545
Tim Peters84e87f32001-03-17 04:50:51 +00004546 if (PyDict_SetItemString(module_dict, "PicklingError",
Guido van Rossum60456fd1997-04-09 17:36:32 +00004547 PicklingError) < 0)
4548 return -1;
4549
Guido van Rossum60456fd1997-04-09 17:36:32 +00004550 if (PyDict_SetItemString(module_dict, "UnpicklingError",
4551 UnpicklingError) < 0)
4552 return -1;
4553
Guido van Rossumc03158b1999-06-09 15:23:31 +00004554 if (PyDict_SetItemString(module_dict, "UnpickleableError",
4555 UnpickleableError) < 0)
4556 return -1;
4557
Guido van Rossum053b8df1998-11-25 16:18:00 +00004558 UNLESS (BadPickleGet = PyString_FromString("cPickle.BadPickleGet"))
4559 return -1;
4560
4561 if (PyDict_SetItemString(module_dict, "BadPickleGet",
4562 BadPickleGet) < 0)
4563 return -1;
4564
Guido van Rossum60456fd1997-04-09 17:36:32 +00004565 PycString_IMPORT;
Tim Peters84e87f32001-03-17 04:50:51 +00004566
Guido van Rossum60456fd1997-04-09 17:36:32 +00004567 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004568}
4569
Guido van Rossumf9ffb031999-02-04 14:54:04 +00004570#ifndef DL_EXPORT /* declarations for DLL import/export */
4571#define DL_EXPORT(RTYPE) RTYPE
4572#endif
Guido van Rossum50f385c1998-12-04 18:48:44 +00004573DL_EXPORT(void)
Thomas Wouters58d05102000-07-24 14:43:35 +00004574initcPickle(void) {
Guido van Rossumebba4202000-09-07 14:35:37 +00004575 PyObject *m, *d, *di, *v, *k;
4576 int i;
Guido van Rossum2f80d961999-07-13 15:18:58 +00004577 char *rev="1.71";
Guido van Rossum60456fd1997-04-09 17:36:32 +00004578 PyObject *format_version;
4579 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004580
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004581 Picklertype.ob_type = &PyType_Type;
4582 Unpicklertype.ob_type = &PyType_Type;
Guido van Rossum053b8df1998-11-25 16:18:00 +00004583 PdataType.ob_type = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004584
Tim Peters84e87f32001-03-17 04:50:51 +00004585 /* Initialize some pieces. We need to do this before module creation,
4586 so we're forced to use a temporary dictionary. :(
Guido van Rossumebba4202000-09-07 14:35:37 +00004587 */
4588 di=PyDict_New();
4589 if (!di) return;
4590 if (init_stuff(di) < 0) return;
4591
Guido van Rossum60456fd1997-04-09 17:36:32 +00004592 /* Create the module and add the functions */
4593 m = Py_InitModule4("cPickle", cPickle_methods,
4594 cPickle_module_documentation,
4595 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004596
Guido van Rossum60456fd1997-04-09 17:36:32 +00004597 /* Add some symbolic constants to the module */
4598 d = PyModule_GetDict(m);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004599 PyDict_SetItemString(d,"__version__", v = PyString_FromString(rev));
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004600 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00004601
Guido van Rossumebba4202000-09-07 14:35:37 +00004602 /* Copy data from di. Waaa. */
4603 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
4604 if (PyObject_SetItem(d, k, v) < 0) {
4605 Py_DECREF(di);
4606 return;
4607 }
4608 }
4609 Py_DECREF(di);
4610
Guido van Rossum60456fd1997-04-09 17:36:32 +00004611 format_version = PyString_FromString("1.3");
4612 compatible_formats = Py_BuildValue("[sss]", "1.0", "1.1", "1.2");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004613
Guido van Rossum60456fd1997-04-09 17:36:32 +00004614 PyDict_SetItemString(d, "format_version", format_version);
4615 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004616 Py_XDECREF(format_version);
4617 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004618}