blob: b87f4983f21d46d4b7ec03150aca286a938a3b17 [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 }
Tim Petersbfa18f72001-04-10 01:54:42 +00002531#if SIZEOF_LONG > 4
2532 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
2533 * is signed, so on a box with longs bigger than 4 bytes we need
2534 * to extend a BININT's sign bit to the full width.
2535 */
2536 if (x == 4 && l & (1L << 31))
2537 l |= (~0L) << 32;
2538#endif
Guido van Rossum60456fd1997-04-09 17:36:32 +00002539 return l;
2540}
2541
2542
2543static int
2544load_binintx(Unpicklerobject *self, char *s, int x) {
2545 PyObject *py_int = 0;
2546 long l;
2547
2548 l = calc_binint(s, x);
2549
Guido van Rossum053b8df1998-11-25 16:18:00 +00002550 UNLESS (py_int = PyInt_FromLong(l))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002551 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002552
Guido van Rossum053b8df1998-11-25 16:18:00 +00002553 PDATA_PUSH(self->stack, py_int, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002554 return 0;
2555}
2556
2557
2558static int
2559load_binint(Unpicklerobject *self) {
2560 char *s;
2561
2562 if ((*self->read_func)(self, &s, 4) < 0)
2563 return -1;
2564
2565 return load_binintx(self, s, 4);
2566}
2567
2568
2569static int
2570load_binint1(Unpicklerobject *self) {
2571 char *s;
2572
2573 if ((*self->read_func)(self, &s, 1) < 0)
2574 return -1;
2575
2576 return load_binintx(self, s, 1);
2577}
2578
2579
2580static int
2581load_binint2(Unpicklerobject *self) {
2582 char *s;
2583
2584 if ((*self->read_func)(self, &s, 2) < 0)
2585 return -1;
2586
2587 return load_binintx(self, s, 2);
2588}
Tim Peters84e87f32001-03-17 04:50:51 +00002589
Guido van Rossum60456fd1997-04-09 17:36:32 +00002590static int
2591load_long(Unpicklerobject *self) {
2592 PyObject *l = 0;
2593 char *end, *s;
2594 int len, res = -1;
2595
Guido van Rossum60456fd1997-04-09 17:36:32 +00002596 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002597 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002598 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002599
Guido van Rossum053b8df1998-11-25 16:18:00 +00002600 UNLESS (l = PyLong_FromString(s, &end, 0))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002601 goto finally;
2602
Guido van Rossum053b8df1998-11-25 16:18:00 +00002603 free(s);
2604 PDATA_PUSH(self->stack, l, -1);
2605 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002606
2607finally:
2608 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002609
2610 return res;
2611}
2612
Tim Peters84e87f32001-03-17 04:50:51 +00002613
Guido van Rossum60456fd1997-04-09 17:36:32 +00002614static int
2615load_float(Unpicklerobject *self) {
2616 PyObject *py_float = 0;
2617 char *endptr, *s;
2618 int len, res = -1;
2619 double d;
2620
2621 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002622 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002623 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002624
2625 errno = 0;
2626 d = strtod(s, &endptr);
2627
2628 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
Tim Peters84e87f32001-03-17 04:50:51 +00002629 PyErr_SetString(PyExc_ValueError,
Guido van Rossum60456fd1997-04-09 17:36:32 +00002630 "could not convert string to float");
2631 goto finally;
2632 }
2633
Guido van Rossum053b8df1998-11-25 16:18:00 +00002634 UNLESS (py_float = PyFloat_FromDouble(d))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002635 goto finally;
2636
Guido van Rossum053b8df1998-11-25 16:18:00 +00002637 free(s);
2638 PDATA_PUSH(self->stack, py_float, -1);
2639 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002640
2641finally:
2642 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002643
2644 return res;
2645}
2646
Guido van Rossum60456fd1997-04-09 17:36:32 +00002647static int
2648load_binfloat(Unpicklerobject *self) {
2649 PyObject *py_float = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002650 int s, e;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002651 long fhi, flo;
2652 double x;
2653 char *p;
2654
2655 if ((*self->read_func)(self, &p, 8) < 0)
2656 return -1;
2657
2658 /* First byte */
2659 s = (*p>>7) & 1;
2660 e = (*p & 0x7F) << 4;
2661 p++;
2662
2663 /* Second byte */
2664 e |= (*p>>4) & 0xF;
2665 fhi = (*p & 0xF) << 24;
2666 p++;
2667
2668 /* Third byte */
2669 fhi |= (*p & 0xFF) << 16;
2670 p++;
2671
2672 /* Fourth byte */
2673 fhi |= (*p & 0xFF) << 8;
2674 p++;
2675
2676 /* Fifth byte */
2677 fhi |= *p & 0xFF;
2678 p++;
2679
2680 /* Sixth byte */
2681 flo = (*p & 0xFF) << 16;
2682 p++;
2683
2684 /* Seventh byte */
2685 flo |= (*p & 0xFF) << 8;
2686 p++;
2687
2688 /* Eighth byte */
2689 flo |= *p & 0xFF;
2690
2691 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
2692 x /= 268435456.0; /* 2**28 */
2693
2694 /* XXX This sadly ignores Inf/NaN */
2695 if (e == 0)
2696 e = -1022;
2697 else {
2698 x += 1.0;
2699 e -= 1023;
2700 }
2701 x = ldexp(x, e);
2702
2703 if (s)
2704 x = -x;
2705
Guido van Rossum053b8df1998-11-25 16:18:00 +00002706 UNLESS (py_float = PyFloat_FromDouble(x)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002707
Guido van Rossum053b8df1998-11-25 16:18:00 +00002708 PDATA_PUSH(self->stack, py_float, -1);
2709 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002710}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002711
2712static int
2713load_string(Unpicklerobject *self) {
2714 PyObject *str = 0;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002715 int len, res = -1, nslash;
2716 char *s, q, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002717
2718 static PyObject *eval_dict = 0;
2719
2720 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002721 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002722 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002723
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002724 /* Check for unquoted quotes (evil strings) */
2725 q=*s;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002726 if (q != '"' && q != '\'') goto insecure;
2727 for (p=s+1, nslash=0; *p; p++) {
2728 if (*p==q && nslash%2==0) break;
2729 if (*p=='\\') nslash++;
2730 else nslash=0;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002731 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002732 if (*p==q)
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002733 {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002734 for (p++; *p; p++) if (*p > ' ') goto insecure;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002735 }
2736 else goto insecure;
2737 /********************************************/
2738
Guido van Rossum053b8df1998-11-25 16:18:00 +00002739 UNLESS (eval_dict)
2740 UNLESS (eval_dict = Py_BuildValue("{s{}}", "__builtins__"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002741 goto finally;
2742
Guido van Rossum053b8df1998-11-25 16:18:00 +00002743 UNLESS (str = PyRun_String(s, Py_eval_input, eval_dict, eval_dict))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002744 goto finally;
2745
Guido van Rossum053b8df1998-11-25 16:18:00 +00002746 free(s);
2747 PDATA_PUSH(self->stack, str, -1);
2748 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002749
2750finally:
2751 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002752
2753 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00002754
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002755insecure:
2756 free(s);
2757 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
2758 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00002759}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002760
2761
2762static int
2763load_binstring(Unpicklerobject *self) {
2764 PyObject *py_string = 0;
2765 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002766 char *s;
2767
Guido van Rossum053b8df1998-11-25 16:18:00 +00002768 if ((*self->read_func)(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002769
2770 l = calc_binint(s, 4);
2771
2772 if ((*self->read_func)(self, &s, l) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002773 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002774
Guido van Rossum053b8df1998-11-25 16:18:00 +00002775 UNLESS (py_string = PyString_FromStringAndSize(s, l))
2776 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002777
Guido van Rossum053b8df1998-11-25 16:18:00 +00002778 PDATA_PUSH(self->stack, py_string, -1);
2779 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002780}
2781
2782
2783static int
2784load_short_binstring(Unpicklerobject *self) {
2785 PyObject *py_string = 0;
Tim Peters84e87f32001-03-17 04:50:51 +00002786 unsigned char l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002787 char *s;
2788
2789 if ((*self->read_func)(self, &s, 1) < 0)
2790 return -1;
2791
2792 l = (unsigned char)s[0];
2793
Guido van Rossum053b8df1998-11-25 16:18:00 +00002794 if ((*self->read_func)(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002795
Guido van Rossum053b8df1998-11-25 16:18:00 +00002796 UNLESS (py_string = PyString_FromStringAndSize(s, l)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002797
Guido van Rossum053b8df1998-11-25 16:18:00 +00002798 PDATA_PUSH(self->stack, py_string, -1);
2799 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00002800}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002801
2802
2803static int
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002804load_unicode(Unpicklerobject *self) {
2805 PyObject *str = 0;
2806 int len, res = -1;
2807 char *s;
2808
2809 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00002810 if (len < 1) return bad_readline();
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002811
2812 UNLESS (str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL))
2813 goto finally;
2814
2815 PDATA_PUSH(self->stack, str, -1);
2816 return 0;
2817
2818finally:
2819 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00002820}
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002821
2822
2823static int
2824load_binunicode(Unpicklerobject *self) {
2825 PyObject *unicode;
2826 long l;
2827 char *s;
2828
2829 if ((*self->read_func)(self, &s, 4) < 0) return -1;
2830
2831 l = calc_binint(s, 4);
2832
2833 if ((*self->read_func)(self, &s, l) < 0)
2834 return -1;
2835
2836 UNLESS (unicode = PyUnicode_DecodeUTF8(s, l, NULL))
2837 return -1;
2838
2839 PDATA_PUSH(self->stack, unicode, -1);
2840 return 0;
2841}
2842
2843
2844static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002845load_tuple(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002846 PyObject *tup;
2847 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002848
Guido van Rossum053b8df1998-11-25 16:18:00 +00002849 if ((i = marker(self)) < 0) return -1;
2850 UNLESS (tup=Pdata_popTuple(self->stack, i)) return -1;
2851 PDATA_PUSH(self->stack, tup, -1);
2852 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002853}
2854
2855static int
2856load_empty_tuple(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002857 PyObject *tup;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002858
Guido van Rossum053b8df1998-11-25 16:18:00 +00002859 UNLESS (tup=PyTuple_New(0)) return -1;
2860 PDATA_PUSH(self->stack, tup, -1);
2861 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002862}
2863
2864static int
2865load_empty_list(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002866 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002867
Guido van Rossum053b8df1998-11-25 16:18:00 +00002868 UNLESS (list=PyList_New(0)) return -1;
2869 PDATA_PUSH(self->stack, list, -1);
2870 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002871}
2872
2873static int
2874load_empty_dict(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002875 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002876
Guido van Rossum053b8df1998-11-25 16:18:00 +00002877 UNLESS (dict=PyDict_New()) return -1;
2878 PDATA_PUSH(self->stack, dict, -1);
2879 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002880}
2881
2882
2883static int
2884load_list(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002885 PyObject *list = 0;
2886 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002887
Guido van Rossum053b8df1998-11-25 16:18:00 +00002888 if ((i = marker(self)) < 0) return -1;
2889 UNLESS (list=Pdata_popList(self->stack, i)) return -1;
2890 PDATA_PUSH(self->stack, list, -1);
2891 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002892}
2893
2894static int
2895load_dict(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002896 PyObject *dict, *key, *value;
2897 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002898
Guido van Rossum053b8df1998-11-25 16:18:00 +00002899 if ((i = marker(self)) < 0) return -1;
2900 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002901
Guido van Rossum053b8df1998-11-25 16:18:00 +00002902 UNLESS (dict = PyDict_New()) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002903
Guido van Rossum053b8df1998-11-25 16:18:00 +00002904 for (k = i+1; k < j; k += 2) {
2905 key =self->stack->data[k-1];
2906 value=self->stack->data[k ];
2907 if (PyDict_SetItem(dict, key, value) < 0) {
2908 Py_DECREF(dict);
2909 return -1;
2910 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002911 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002912 Pdata_clear(self->stack, i);
2913 PDATA_PUSH(self->stack, dict, -1);
2914 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002915}
2916
2917static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002918Instance_New(PyObject *cls, PyObject *args) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002919 int has_key;
2920 PyObject *safe=0, *r=0;
2921
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002922 if (PyClass_Check(cls)) {
2923 int l;
Tim Peters84e87f32001-03-17 04:50:51 +00002924
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002925 if ((l=PyObject_Size(args)) < 0) goto err;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002926 UNLESS (l) {
2927 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002928
Guido van Rossum053b8df1998-11-25 16:18:00 +00002929 UNLESS (__getinitargs__=PyObject_GetAttr(cls, __getinitargs___str)) {
2930 /* We have a class with no __getinitargs__, so bypass usual
2931 construction */
Fred Drake2c773552001-03-22 17:52:17 +00002932 PyObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002933
Guido van Rossum053b8df1998-11-25 16:18:00 +00002934 PyErr_Clear();
Fred Drake2c773552001-03-22 17:52:17 +00002935 UNLESS (inst=PyInstance_NewRaw(cls, NULL))
Guido van Rossum053b8df1998-11-25 16:18:00 +00002936 goto err;
Fred Drake2c773552001-03-22 17:52:17 +00002937 return inst;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002938 }
2939 Py_DECREF(__getinitargs__);
2940 }
Tim Peters84e87f32001-03-17 04:50:51 +00002941
Guido van Rossum053b8df1998-11-25 16:18:00 +00002942 if ((r=PyInstance_New(cls, args, NULL))) return r;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002943 else goto err;
2944 }
Tim Peters84e87f32001-03-17 04:50:51 +00002945
2946
Guido van Rossum60456fd1997-04-09 17:36:32 +00002947 if ((has_key = cPickle_PyMapping_HasKey(safe_constructors, cls)) < 0)
2948 goto err;
Tim Peters84e87f32001-03-17 04:50:51 +00002949
Guido van Rossum60456fd1997-04-09 17:36:32 +00002950 if (!has_key)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002951 if (!(safe = PyObject_GetAttr(cls, __safe_for_unpickling___str)) ||
Guido van Rossum60456fd1997-04-09 17:36:32 +00002952 !PyObject_IsTrue(safe)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002953 cPickle_ErrFormat(UnpicklingError,
2954 "%s is not safe for unpickling", "O", cls);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002955 Py_XDECREF(safe);
2956 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002957 }
2958
Guido van Rossum053b8df1998-11-25 16:18:00 +00002959 if (args==Py_None) {
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002960 /* Special case, call cls.__basicnew__() */
2961 PyObject *basicnew;
Tim Peters84e87f32001-03-17 04:50:51 +00002962
Guido van Rossum053b8df1998-11-25 16:18:00 +00002963 UNLESS (basicnew=PyObject_GetAttr(cls, __basicnew___str)) return NULL;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002964 r=PyObject_CallObject(basicnew, NULL);
2965 Py_DECREF(basicnew);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002966 if (r) return r;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002967 }
2968
Guido van Rossum053b8df1998-11-25 16:18:00 +00002969 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00002970
Guido van Rossum60456fd1997-04-09 17:36:32 +00002971err:
2972 {
2973 PyObject *tp, *v, *tb;
2974
2975 PyErr_Fetch(&tp, &v, &tb);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002976 if ((r=Py_BuildValue("OOO",v,cls,args))) {
2977 Py_XDECREF(v);
2978 v=r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002979 }
2980 PyErr_Restore(tp,v,tb);
2981 }
2982 return NULL;
2983}
Tim Peters84e87f32001-03-17 04:50:51 +00002984
Guido van Rossum60456fd1997-04-09 17:36:32 +00002985
2986static int
2987load_obj(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002988 PyObject *class, *tup, *obj=0;
2989 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002990
Guido van Rossum053b8df1998-11-25 16:18:00 +00002991 if ((i = marker(self)) < 0) return -1;
2992 UNLESS (tup=Pdata_popTuple(self->stack, i+1)) return -1;
2993 PDATA_POP(self->stack, class);
2994 if (class) {
2995 obj = Instance_New(class, tup);
2996 Py_DECREF(class);
2997 }
2998 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002999
Guido van Rossum053b8df1998-11-25 16:18:00 +00003000 if (! obj) return -1;
3001 PDATA_PUSH(self->stack, obj, -1);
3002 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003003}
3004
3005
3006static int
3007load_inst(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003008 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003009 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003010 char *s;
3011
Guido van Rossum053b8df1998-11-25 16:18:00 +00003012 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003013
Guido van Rossum053b8df1998-11-25 16:18:00 +00003014 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003015 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00003016 UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003017
Guido van Rossum053b8df1998-11-25 16:18:00 +00003018 if ((len = (*self->readline_func)(self, &s)) >= 0) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003019 if (len < 2) return bad_readline();
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003020 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003021 class = find_class(module_name, class_name, self->find_class);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003022 Py_DECREF(class_name);
3023 }
3024 }
3025 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003026
Guido van Rossum053b8df1998-11-25 16:18:00 +00003027 if (! class) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003028
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003029 if ((tup=Pdata_popTuple(self->stack, i))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003030 obj = Instance_New(class, tup);
3031 Py_DECREF(tup);
3032 }
3033 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003034
Guido van Rossum053b8df1998-11-25 16:18:00 +00003035 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003036
Guido van Rossum053b8df1998-11-25 16:18:00 +00003037 PDATA_PUSH(self->stack, obj, -1);
3038 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003039}
3040
3041
3042static int
3043load_global(Unpicklerobject *self) {
3044 PyObject *class = 0, *module_name = 0, *class_name = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003045 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003046 char *s;
3047
Guido van Rossum053b8df1998-11-25 16:18:00 +00003048 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003049 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00003050 UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003051
Guido van Rossum053b8df1998-11-25 16:18:00 +00003052 if ((len = (*self->readline_func)(self, &s)) >= 0) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003053 if (len < 2) return bad_readline();
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003054 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003055 class = find_class(module_name, class_name, self->find_class);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003056 Py_DECREF(class_name);
3057 }
3058 }
3059 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003060
Guido van Rossum053b8df1998-11-25 16:18:00 +00003061 if (! class) return -1;
3062 PDATA_PUSH(self->stack, class, -1);
3063 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003064}
3065
3066
3067static int
3068load_persid(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003069 PyObject *pid = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003070 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003071 char *s;
3072
3073 if (self->pers_func) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003074 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003075 if (len < 2) return bad_readline();
Tim Peters84e87f32001-03-17 04:50:51 +00003076
Guido van Rossum053b8df1998-11-25 16:18:00 +00003077 UNLESS (pid = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003078
Guido van Rossum053b8df1998-11-25 16:18:00 +00003079 if (PyList_Check(self->pers_func)) {
3080 if (PyList_Append(self->pers_func, pid) < 0) {
3081 Py_DECREF(pid);
3082 return -1;
3083 }
3084 }
3085 else {
3086 ARG_TUP(self, pid);
3087 if (self->arg) {
3088 pid = PyObject_CallObject(self->pers_func, self->arg);
3089 FREE_ARG_TUP(self);
3090 }
3091 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003092
Guido van Rossum053b8df1998-11-25 16:18:00 +00003093 if (! pid) return -1;
3094
3095 PDATA_PUSH(self->stack, pid, -1);
3096 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003097 }
3098 else {
3099 PyErr_SetString(UnpicklingError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00003100 "A load persistent id instruction was encountered,\n"
3101 "but no persistent_load function was specified.");
3102 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003103 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003104}
3105
Guido van Rossum60456fd1997-04-09 17:36:32 +00003106static int
3107load_binpersid(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003108 PyObject *pid = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003109
3110 if (self->pers_func) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003111 PDATA_POP(self->stack, pid);
3112 if (! pid) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003113
Guido van Rossum053b8df1998-11-25 16:18:00 +00003114 if (PyList_Check(self->pers_func)) {
3115 if (PyList_Append(self->pers_func, pid) < 0) {
3116 Py_DECREF(pid);
3117 return -1;
3118 }
3119 }
3120 else {
3121 ARG_TUP(self, pid);
3122 if (self->arg) {
3123 pid = PyObject_CallObject(self->pers_func, self->arg);
3124 FREE_ARG_TUP(self);
3125 }
3126 if (! pid) return -1;
3127 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003128
Guido van Rossum053b8df1998-11-25 16:18:00 +00003129 PDATA_PUSH(self->stack, pid, -1);
3130 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003131 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003132 else {
3133 PyErr_SetString(UnpicklingError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00003134 "A load persistent id instruction was encountered,\n"
3135 "but no persistent_load function was specified.");
3136 return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003137 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003138}
3139
3140
3141static int
3142load_pop(Unpicklerobject *self) {
3143 int len;
3144
Guido van Rossum053b8df1998-11-25 16:18:00 +00003145 UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003146
Tim Peters84e87f32001-03-17 04:50:51 +00003147 /* Note that we split the (pickle.py) stack into two stacks,
Guido van Rossumea2b7152000-05-09 18:14:50 +00003148 an object stack and a mark stack. We have to be clever and
3149 pop the right one. We do this by looking at the top of the
3150 mark stack.
3151 */
3152
Tim Peters84e87f32001-03-17 04:50:51 +00003153 if ((self->num_marks > 0) &&
Guido van Rossum60456fd1997-04-09 17:36:32 +00003154 (self->marks[self->num_marks - 1] == len))
3155 self->num_marks--;
Tim Peters84e87f32001-03-17 04:50:51 +00003156 else {
Guido van Rossumea2b7152000-05-09 18:14:50 +00003157 len--;
3158 Py_DECREF(self->stack->data[len]);
3159 self->stack->length=len;
3160 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003161
3162 return 0;
3163}
3164
3165
3166static int
3167load_pop_mark(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003168 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003169
3170 if ((i = marker(self)) < 0)
3171 return -1;
3172
Guido van Rossum053b8df1998-11-25 16:18:00 +00003173 Pdata_clear(self->stack, i);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003174
3175 return 0;
3176}
3177
3178
3179static int
3180load_dup(Unpicklerobject *self) {
3181 PyObject *last;
3182 int len;
3183
Guido van Rossum053b8df1998-11-25 16:18:00 +00003184 if ((len = self->stack->length) <= 0) return stackUnderflow();
3185 last=self->stack->data[len-1];
3186 Py_INCREF(last);
3187 PDATA_PUSH(self->stack, last, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003188 return 0;
3189}
3190
3191
3192static int
3193load_get(Unpicklerobject *self) {
3194 PyObject *py_str = 0, *value = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003195 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003196 char *s;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003197 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003198
Guido van Rossum053b8df1998-11-25 16:18:00 +00003199 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003200 if (len < 2) return bad_readline();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003201
Guido van Rossum053b8df1998-11-25 16:18:00 +00003202 UNLESS (py_str = PyString_FromStringAndSize(s, len - 1)) return -1;
3203
3204 value = PyDict_GetItem(self->memo, py_str);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003205 if (! value) {
3206 PyErr_SetObject(BadPickleGet, py_str);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003207 rc = -1;
3208 } else {
3209 PDATA_APPEND(self->stack, value, -1);
3210 rc = 0;
3211 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003212
Guido van Rossum2f80d961999-07-13 15:18:58 +00003213 Py_DECREF(py_str);
3214 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003215}
3216
3217
3218static int
3219load_binget(Unpicklerobject *self) {
3220 PyObject *py_key = 0, *value = 0;
3221 unsigned char key;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003222 char *s;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003223 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003224
Guido van Rossum053b8df1998-11-25 16:18:00 +00003225 if ((*self->read_func)(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003226
3227 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00003228 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003229
Guido van Rossum053b8df1998-11-25 16:18:00 +00003230 value = PyDict_GetItem(self->memo, py_key);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003231 if (! value) {
3232 PyErr_SetObject(BadPickleGet, py_key);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003233 rc = -1;
3234 } else {
3235 PDATA_APPEND(self->stack, value, -1);
3236 rc = 0;
3237 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003238
Guido van Rossum2f80d961999-07-13 15:18:58 +00003239 Py_DECREF(py_key);
3240 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003241}
3242
3243
3244static int
3245load_long_binget(Unpicklerobject *self) {
3246 PyObject *py_key = 0, *value = 0;
Thomas Wouters3b6448f2000-07-22 23:56:07 +00003247 unsigned char c;
3248 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003249 long key;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003250 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003251
Guido van Rossum053b8df1998-11-25 16:18:00 +00003252 if ((*self->read_func)(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003253
3254 c = (unsigned char)s[0];
3255 key = (long)c;
3256 c = (unsigned char)s[1];
3257 key |= (long)c << 8;
3258 c = (unsigned char)s[2];
3259 key |= (long)c << 16;
3260 c = (unsigned char)s[3];
3261 key |= (long)c << 24;
3262
Guido van Rossum053b8df1998-11-25 16:18:00 +00003263 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003264
Guido van Rossum053b8df1998-11-25 16:18:00 +00003265 value = PyDict_GetItem(self->memo, py_key);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003266 if (! value) {
3267 PyErr_SetObject(BadPickleGet, py_key);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003268 rc = -1;
3269 } else {
3270 PDATA_APPEND(self->stack, value, -1);
3271 rc = 0;
3272 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003273
Guido van Rossum2f80d961999-07-13 15:18:58 +00003274 Py_DECREF(py_key);
3275 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003276}
3277
3278
3279static int
3280load_put(Unpicklerobject *self) {
3281 PyObject *py_str = 0, *value = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003282 int len, l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003283 char *s;
3284
Guido van Rossum053b8df1998-11-25 16:18:00 +00003285 if ((l = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumd1f66dc1999-02-08 22:38:25 +00003286 if (l < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00003287 UNLESS (len=self->stack->length) return stackUnderflow();
3288 UNLESS (py_str = PyString_FromStringAndSize(s, l - 1)) return -1;
3289 value=self->stack->data[len-1];
3290 l=PyDict_SetItem(self->memo, py_str, value);
3291 Py_DECREF(py_str);
3292 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003293}
3294
3295
3296static int
3297load_binput(Unpicklerobject *self) {
3298 PyObject *py_key = 0, *value = 0;
Thomas Wouters3b6448f2000-07-22 23:56:07 +00003299 unsigned char key;
3300 char *s;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003301 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003302
Guido van Rossum053b8df1998-11-25 16:18:00 +00003303 if ((*self->read_func)(self, &s, 1) < 0) return -1;
3304 UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003305
3306 key = (unsigned char)s[0];
3307
Guido van Rossum053b8df1998-11-25 16:18:00 +00003308 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3309 value=self->stack->data[len-1];
3310 len=PyDict_SetItem(self->memo, py_key, value);
3311 Py_DECREF(py_key);
3312 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003313}
3314
3315
3316static int
3317load_long_binput(Unpicklerobject *self) {
3318 PyObject *py_key = 0, *value = 0;
3319 long key;
Thomas Wouters3b6448f2000-07-22 23:56:07 +00003320 unsigned char c;
3321 char *s;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003322 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003323
Guido van Rossum053b8df1998-11-25 16:18:00 +00003324 if ((*self->read_func)(self, &s, 4) < 0) return -1;
3325 UNLESS (len=self->stack->length) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003326
3327 c = (unsigned char)s[0];
3328 key = (long)c;
3329 c = (unsigned char)s[1];
3330 key |= (long)c << 8;
3331 c = (unsigned char)s[2];
3332 key |= (long)c << 16;
3333 c = (unsigned char)s[3];
3334 key |= (long)c << 24;
3335
Guido van Rossum053b8df1998-11-25 16:18:00 +00003336 UNLESS (py_key = PyInt_FromLong(key)) return -1;
3337 value=self->stack->data[len-1];
3338 len=PyDict_SetItem(self->memo, py_key, value);
3339 Py_DECREF(py_key);
3340 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003341}
3342
3343
Tim Peters84e87f32001-03-17 04:50:51 +00003344static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00003345do_append(Unpicklerobject *self, int x) {
3346 PyObject *value = 0, *list = 0, *append_method = 0;
3347 int len, i;
3348
Guido van Rossum053b8df1998-11-25 16:18:00 +00003349 UNLESS ((len=self->stack->length) >= x && x > 0) return stackUnderflow();
3350 if (len==x) return 0; /* nothing to do */
Guido van Rossum60456fd1997-04-09 17:36:32 +00003351
Guido van Rossum053b8df1998-11-25 16:18:00 +00003352 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003353
3354 if (PyList_Check(list)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003355 PyObject *slice;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003356 int list_len;
Tim Peters84e87f32001-03-17 04:50:51 +00003357
Guido van Rossum053b8df1998-11-25 16:18:00 +00003358 slice=Pdata_popList(self->stack, x);
3359 list_len = PyList_GET_SIZE(list);
3360 i=PyList_SetSlice(list, list_len, list_len, slice);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003361 Py_DECREF(slice);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003362 return i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003363 }
3364 else {
3365
Guido van Rossum053b8df1998-11-25 16:18:00 +00003366 UNLESS (append_method = PyObject_GetAttr(list, append_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00003367 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003368
Guido van Rossum60456fd1997-04-09 17:36:32 +00003369 for (i = x; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003370 PyObject *junk;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003371
Guido van Rossum053b8df1998-11-25 16:18:00 +00003372 value=self->stack->data[i];
3373 junk=0;
3374 ARG_TUP(self, value);
3375 if (self->arg) {
3376 junk = PyObject_CallObject(append_method, self->arg);
3377 FREE_ARG_TUP(self);
3378 }
3379 if (! junk) {
3380 Pdata_clear(self->stack, i+1);
3381 self->stack->length=x;
3382 Py_DECREF(append_method);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003383 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003384 }
3385 Py_DECREF(junk);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003386 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003387 self->stack->length=x;
3388 Py_DECREF(append_method);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003389 }
3390
Guido van Rossum60456fd1997-04-09 17:36:32 +00003391 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003392}
3393
Tim Peters84e87f32001-03-17 04:50:51 +00003394
Guido van Rossum60456fd1997-04-09 17:36:32 +00003395static int
3396load_append(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003397 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003398}
3399
3400
3401static int
3402load_appends(Unpicklerobject *self) {
3403 return do_append(self, marker(self));
3404}
3405
3406
3407static int
3408do_setitems(Unpicklerobject *self, int x) {
3409 PyObject *value = 0, *key = 0, *dict = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003410 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003411
Guido van Rossum053b8df1998-11-25 16:18:00 +00003412 UNLESS ((len=self->stack->length) >= x
3413 && x > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003414
Guido van Rossum053b8df1998-11-25 16:18:00 +00003415 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003416
Guido van Rossum053b8df1998-11-25 16:18:00 +00003417 for (i = x+1; i < len; i += 2) {
3418 key =self->stack->data[i-1];
3419 value=self->stack->data[i ];
3420 if (PyObject_SetItem(dict, key, value) < 0) {
3421 r=-1;
3422 break;
3423 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003424 }
3425
Guido van Rossum053b8df1998-11-25 16:18:00 +00003426 Pdata_clear(self->stack, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003427
Guido van Rossum053b8df1998-11-25 16:18:00 +00003428 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003429}
3430
3431
3432static int
3433load_setitem(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003434 return do_setitems(self, self->stack->length - 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003435}
3436
Guido van Rossum60456fd1997-04-09 17:36:32 +00003437static int
3438load_setitems(Unpicklerobject *self) {
3439 return do_setitems(self, marker(self));
3440}
3441
3442
3443static int
3444load_build(Unpicklerobject *self) {
Tim Peters84e87f32001-03-17 04:50:51 +00003445 PyObject *value = 0, *inst = 0, *instdict = 0, *d_key = 0, *d_value = 0,
Guido van Rossum60456fd1997-04-09 17:36:32 +00003446 *junk = 0, *__setstate__ = 0;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003447 int i, r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003448
Guido van Rossum053b8df1998-11-25 16:18:00 +00003449 if (self->stack->length < 2) return stackUnderflow();
3450 PDATA_POP(self->stack, value);
3451 if (! value) return -1;
3452 inst=self->stack->data[self->stack->length-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003453
Guido van Rossum053b8df1998-11-25 16:18:00 +00003454 if ((__setstate__ = PyObject_GetAttr(inst, __setstate___str))) {
3455 ARG_TUP(self, value);
3456 if (self->arg) {
3457 junk = PyObject_CallObject(__setstate__, self->arg);
3458 FREE_ARG_TUP(self);
3459 }
3460 Py_DECREF(__setstate__);
3461 if (! junk) return -1;
3462 Py_DECREF(junk);
3463 return 0;
3464 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003465
Guido van Rossum053b8df1998-11-25 16:18:00 +00003466 PyErr_Clear();
3467 if ((instdict = PyObject_GetAttr(inst, __dict___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003468 i = 0;
3469 while (PyDict_Next(value, &i, &d_key, &d_value)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003470 if (PyObject_SetItem(instdict, d_key, d_value) < 0) {
3471 r=-1;
3472 break;
3473 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003474 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003475 Py_DECREF(instdict);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003476 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003477 else r=-1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003478
Guido van Rossum053b8df1998-11-25 16:18:00 +00003479 Py_XDECREF(value);
Tim Peters84e87f32001-03-17 04:50:51 +00003480
Guido van Rossum053b8df1998-11-25 16:18:00 +00003481 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003482}
3483
3484
3485static int
3486load_mark(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003487 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003488
Guido van Rossumea2b7152000-05-09 18:14:50 +00003489 /* Note that we split the (pickle.py) stack into two stacks, an
3490 object stack and a mark stack. Here we push a mark onto the
Tim Peters84e87f32001-03-17 04:50:51 +00003491 mark stack.
Guido van Rossumea2b7152000-05-09 18:14:50 +00003492 */
3493
Guido van Rossum053b8df1998-11-25 16:18:00 +00003494 if ((self->num_marks + 1) >= self->marks_size) {
3495 s=self->marks_size+20;
3496 if (s <= self->num_marks) s=self->num_marks + 1;
Guido van Rossum761fcd01999-04-12 22:51:20 +00003497 if (self->marks == NULL)
Guido van Rossumaa8d1671999-01-25 21:43:51 +00003498 self->marks=(int *)malloc(s * sizeof(int));
3499 else
3500 self->marks=(int *)realloc(self->marks, s * sizeof(int));
Guido van Rossum053b8df1998-11-25 16:18:00 +00003501 if (! self->marks) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003502 PyErr_NoMemory();
3503 return -1;
3504 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003505 self->marks_size = s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003506 }
3507
Guido van Rossum053b8df1998-11-25 16:18:00 +00003508 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003509
3510 return 0;
3511}
3512
3513static int
3514load_reduce(Unpicklerobject *self) {
3515 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003516
Guido van Rossum053b8df1998-11-25 16:18:00 +00003517 PDATA_POP(self->stack, arg_tup);
3518 if (! arg_tup) return -1;
3519 PDATA_POP(self->stack, callable);
3520 if (callable) {
3521 ob = Instance_New(callable, arg_tup);
3522 Py_DECREF(callable);
3523 }
3524 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003525
Guido van Rossum053b8df1998-11-25 16:18:00 +00003526 if (! ob) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003527
Guido van Rossum053b8df1998-11-25 16:18:00 +00003528 PDATA_PUSH(self->stack, ob, -1);
3529 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003530}
Tim Peters84e87f32001-03-17 04:50:51 +00003531
Guido van Rossum60456fd1997-04-09 17:36:32 +00003532static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003533load(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003534 PyObject *err = 0, *val = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003535 char *s;
3536
Guido van Rossum60456fd1997-04-09 17:36:32 +00003537 self->num_marks = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003538 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003539
3540 while (1) {
3541 if ((*self->read_func)(self, &s, 1) < 0)
3542 break;
3543
3544 switch (s[0]) {
3545 case NONE:
3546 if (load_none(self) < 0)
3547 break;
3548 continue;
3549
3550 case BININT:
3551 if (load_binint(self) < 0)
3552 break;
3553 continue;
3554
3555 case BININT1:
3556 if (load_binint1(self) < 0)
3557 break;
3558 continue;
3559
3560 case BININT2:
3561 if (load_binint2(self) < 0)
3562 break;
3563 continue;
3564
3565 case INT:
3566 if (load_int(self) < 0)
3567 break;
3568 continue;
3569
3570 case LONG:
3571 if (load_long(self) < 0)
3572 break;
3573 continue;
3574
3575 case FLOAT:
3576 if (load_float(self) < 0)
3577 break;
3578 continue;
3579
Guido van Rossum60456fd1997-04-09 17:36:32 +00003580 case BINFLOAT:
3581 if (load_binfloat(self) < 0)
3582 break;
3583 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003584
3585 case BINSTRING:
3586 if (load_binstring(self) < 0)
3587 break;
3588 continue;
3589
3590 case SHORT_BINSTRING:
3591 if (load_short_binstring(self) < 0)
3592 break;
3593 continue;
3594
3595 case STRING:
3596 if (load_string(self) < 0)
3597 break;
3598 continue;
3599
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003600 case UNICODE:
3601 if (load_unicode(self) < 0)
3602 break;
3603 continue;
3604
3605 case BINUNICODE:
3606 if (load_binunicode(self) < 0)
3607 break;
3608 continue;
3609
Guido van Rossum60456fd1997-04-09 17:36:32 +00003610 case EMPTY_TUPLE:
3611 if (load_empty_tuple(self) < 0)
3612 break;
3613 continue;
3614
3615 case TUPLE:
3616 if (load_tuple(self) < 0)
3617 break;
3618 continue;
3619
3620 case EMPTY_LIST:
3621 if (load_empty_list(self) < 0)
3622 break;
3623 continue;
3624
3625 case LIST:
3626 if (load_list(self) < 0)
3627 break;
3628 continue;
3629
3630 case EMPTY_DICT:
3631 if (load_empty_dict(self) < 0)
3632 break;
3633 continue;
3634
3635 case DICT:
3636 if (load_dict(self) < 0)
3637 break;
3638 continue;
3639
3640 case OBJ:
3641 if (load_obj(self) < 0)
3642 break;
3643 continue;
3644
3645 case INST:
3646 if (load_inst(self) < 0)
3647 break;
3648 continue;
3649
3650 case GLOBAL:
3651 if (load_global(self) < 0)
3652 break;
3653 continue;
3654
3655 case APPEND:
3656 if (load_append(self) < 0)
3657 break;
3658 continue;
3659
3660 case APPENDS:
3661 if (load_appends(self) < 0)
3662 break;
3663 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003664
Guido van Rossum60456fd1997-04-09 17:36:32 +00003665 case BUILD:
3666 if (load_build(self) < 0)
3667 break;
3668 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003669
Guido van Rossum60456fd1997-04-09 17:36:32 +00003670 case DUP:
3671 if (load_dup(self) < 0)
3672 break;
3673 continue;
3674
3675 case BINGET:
3676 if (load_binget(self) < 0)
3677 break;
3678 continue;
3679
3680 case LONG_BINGET:
3681 if (load_long_binget(self) < 0)
3682 break;
3683 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003684
Guido van Rossum60456fd1997-04-09 17:36:32 +00003685 case GET:
3686 if (load_get(self) < 0)
3687 break;
3688 continue;
3689
3690 case MARK:
3691 if (load_mark(self) < 0)
3692 break;
3693 continue;
3694
3695 case BINPUT:
3696 if (load_binput(self) < 0)
3697 break;
3698 continue;
3699
3700 case LONG_BINPUT:
3701 if (load_long_binput(self) < 0)
3702 break;
3703 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003704
Guido van Rossum60456fd1997-04-09 17:36:32 +00003705 case PUT:
3706 if (load_put(self) < 0)
3707 break;
3708 continue;
3709
3710 case POP:
3711 if (load_pop(self) < 0)
3712 break;
3713 continue;
3714
3715 case POP_MARK:
3716 if (load_pop_mark(self) < 0)
3717 break;
3718 continue;
3719
3720 case SETITEM:
3721 if (load_setitem(self) < 0)
3722 break;
3723 continue;
3724
3725 case SETITEMS:
3726 if (load_setitems(self) < 0)
3727 break;
3728 continue;
3729
3730 case STOP:
3731 break;
3732
3733 case PERSID:
3734 if (load_persid(self) < 0)
3735 break;
3736 continue;
3737
3738 case BINPERSID:
3739 if (load_binpersid(self) < 0)
3740 break;
3741 continue;
3742
3743 case REDUCE:
3744 if (load_reduce(self) < 0)
3745 break;
3746 continue;
3747
Tim Peters84e87f32001-03-17 04:50:51 +00003748 default:
3749 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossum60456fd1997-04-09 17:36:32 +00003750 "c", s[0]);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003751 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003752 }
3753
3754 break;
3755 }
3756
Guido van Rossum053b8df1998-11-25 16:18:00 +00003757 if ((err = PyErr_Occurred())) {
3758 if (err == PyExc_EOFError) {
3759 PyErr_SetNone(PyExc_EOFError);
Tim Peters84e87f32001-03-17 04:50:51 +00003760 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003761 return NULL;
3762 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003763
Tim Peters84e87f32001-03-17 04:50:51 +00003764 PDATA_POP(self->stack, val);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003765 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003766}
Tim Peters84e87f32001-03-17 04:50:51 +00003767
Guido van Rossum60456fd1997-04-09 17:36:32 +00003768
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003769/* No-load functions to support noload, which is used to
3770 find persistent references. */
3771
3772static int
3773noload_obj(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003774 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003775
3776 if ((i = marker(self)) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003777 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003778}
3779
3780
3781static int
3782noload_inst(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003783 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003784 char *s;
3785
3786 if ((i = marker(self)) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003787 Pdata_clear(self->stack, i);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003788 if ((*self->readline_func)(self, &s) < 0) return -1;
3789 if ((*self->readline_func)(self, &s) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003790 PDATA_APPEND(self->stack, Py_None,-1);
3791 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003792}
3793
3794static int
3795noload_global(Unpicklerobject *self) {
3796 char *s;
3797
3798 if ((*self->readline_func)(self, &s) < 0) return -1;
3799 if ((*self->readline_func)(self, &s) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003800 PDATA_APPEND(self->stack, Py_None,-1);
3801 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003802}
3803
3804static int
3805noload_reduce(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003806
Guido van Rossum053b8df1998-11-25 16:18:00 +00003807 if (self->stack->length < 2) return stackUnderflow();
3808 Pdata_clear(self->stack, self->stack->length-2);
3809 PDATA_APPEND(self->stack, Py_None,-1);
3810 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003811}
3812
3813static int
3814noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003815
Guido van Rossum053b8df1998-11-25 16:18:00 +00003816 if (self->stack->length < 1) return stackUnderflow();
3817 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00003818 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003819}
3820
3821
3822static PyObject *
3823noload(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003824 PyObject *err = 0, *val = 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003825 char *s;
3826
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003827 self->num_marks = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003828 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003829
3830 while (1) {
3831 if ((*self->read_func)(self, &s, 1) < 0)
3832 break;
3833
3834 switch (s[0]) {
3835 case NONE:
3836 if (load_none(self) < 0)
3837 break;
3838 continue;
3839
3840 case BININT:
3841 if (load_binint(self) < 0)
3842 break;
3843 continue;
3844
3845 case BININT1:
3846 if (load_binint1(self) < 0)
3847 break;
3848 continue;
3849
3850 case BININT2:
3851 if (load_binint2(self) < 0)
3852 break;
3853 continue;
3854
3855 case INT:
3856 if (load_int(self) < 0)
3857 break;
3858 continue;
3859
3860 case LONG:
3861 if (load_long(self) < 0)
3862 break;
3863 continue;
3864
3865 case FLOAT:
3866 if (load_float(self) < 0)
3867 break;
3868 continue;
3869
3870 case BINFLOAT:
3871 if (load_binfloat(self) < 0)
3872 break;
3873 continue;
3874
3875 case BINSTRING:
3876 if (load_binstring(self) < 0)
3877 break;
3878 continue;
3879
3880 case SHORT_BINSTRING:
3881 if (load_short_binstring(self) < 0)
3882 break;
3883 continue;
3884
3885 case STRING:
3886 if (load_string(self) < 0)
3887 break;
3888 continue;
3889
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003890 case UNICODE:
3891 if (load_unicode(self) < 0)
3892 break;
3893 continue;
3894
3895 case BINUNICODE:
3896 if (load_binunicode(self) < 0)
3897 break;
3898 continue;
3899
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003900 case EMPTY_TUPLE:
3901 if (load_empty_tuple(self) < 0)
3902 break;
3903 continue;
3904
3905 case TUPLE:
3906 if (load_tuple(self) < 0)
3907 break;
3908 continue;
3909
3910 case EMPTY_LIST:
3911 if (load_empty_list(self) < 0)
3912 break;
3913 continue;
3914
3915 case LIST:
3916 if (load_list(self) < 0)
3917 break;
3918 continue;
3919
3920 case EMPTY_DICT:
3921 if (load_empty_dict(self) < 0)
3922 break;
3923 continue;
3924
3925 case DICT:
3926 if (load_dict(self) < 0)
3927 break;
3928 continue;
3929
3930 case OBJ:
3931 if (noload_obj(self) < 0)
3932 break;
3933 continue;
3934
3935 case INST:
3936 if (noload_inst(self) < 0)
3937 break;
3938 continue;
3939
3940 case GLOBAL:
3941 if (noload_global(self) < 0)
3942 break;
3943 continue;
3944
3945 case APPEND:
3946 if (load_append(self) < 0)
3947 break;
3948 continue;
3949
3950 case APPENDS:
3951 if (load_appends(self) < 0)
3952 break;
3953 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003954
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003955 case BUILD:
3956 if (noload_build(self) < 0)
3957 break;
3958 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003959
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003960 case DUP:
3961 if (load_dup(self) < 0)
3962 break;
3963 continue;
3964
3965 case BINGET:
3966 if (load_binget(self) < 0)
3967 break;
3968 continue;
3969
3970 case LONG_BINGET:
3971 if (load_long_binget(self) < 0)
3972 break;
3973 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003974
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003975 case GET:
3976 if (load_get(self) < 0)
3977 break;
3978 continue;
3979
3980 case MARK:
3981 if (load_mark(self) < 0)
3982 break;
3983 continue;
3984
3985 case BINPUT:
3986 if (load_binput(self) < 0)
3987 break;
3988 continue;
3989
3990 case LONG_BINPUT:
3991 if (load_long_binput(self) < 0)
3992 break;
3993 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003994
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003995 case PUT:
3996 if (load_put(self) < 0)
3997 break;
3998 continue;
3999
4000 case POP:
4001 if (load_pop(self) < 0)
4002 break;
4003 continue;
4004
4005 case POP_MARK:
4006 if (load_pop_mark(self) < 0)
4007 break;
4008 continue;
4009
4010 case SETITEM:
4011 if (load_setitem(self) < 0)
4012 break;
4013 continue;
4014
4015 case SETITEMS:
4016 if (load_setitems(self) < 0)
4017 break;
4018 continue;
4019
4020 case STOP:
4021 break;
4022
4023 case PERSID:
4024 if (load_persid(self) < 0)
4025 break;
4026 continue;
4027
4028 case BINPERSID:
4029 if (load_binpersid(self) < 0)
4030 break;
4031 continue;
4032
4033 case REDUCE:
4034 if (noload_reduce(self) < 0)
4035 break;
4036 continue;
4037
Tim Peters84e87f32001-03-17 04:50:51 +00004038 default:
4039 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004040 "c", s[0]);
Guido van Rossum053b8df1998-11-25 16:18:00 +00004041 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004042 }
4043
4044 break;
4045 }
4046
Guido van Rossum053b8df1998-11-25 16:18:00 +00004047 if ((err = PyErr_Occurred())) {
4048 if (err == PyExc_EOFError) {
4049 PyErr_SetNone(PyExc_EOFError);
Tim Peters84e87f32001-03-17 04:50:51 +00004050 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00004051 return NULL;
4052 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004053
Tim Peters84e87f32001-03-17 04:50:51 +00004054 PDATA_POP(self->stack, val);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004055 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004056}
Tim Peters84e87f32001-03-17 04:50:51 +00004057
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004058
Guido van Rossum60456fd1997-04-09 17:36:32 +00004059static PyObject *
4060Unpickler_load(Unpicklerobject *self, PyObject *args) {
Tim Peters84e87f32001-03-17 04:50:51 +00004061 UNLESS (PyArg_ParseTuple(args, ":load"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004062 return NULL;
4063
4064 return load(self);
4065}
4066
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004067static PyObject *
4068Unpickler_noload(Unpicklerobject *self, PyObject *args) {
Tim Peters84e87f32001-03-17 04:50:51 +00004069 UNLESS (PyArg_ParseTuple(args, ":noload"))
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004070 return NULL;
4071
4072 return noload(self);
4073}
4074
Guido van Rossum60456fd1997-04-09 17:36:32 +00004075
4076static struct PyMethodDef Unpickler_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004077 {"load", (PyCFunction)Unpickler_load, 1,
4078 "load() -- Load a pickle"
4079 },
4080 {"noload", (PyCFunction)Unpickler_noload, 1,
4081 "noload() -- not load a pickle, but go through most of the motions\n"
4082 "\n"
4083 "This function can be used to read past a pickle without instantiating\n"
4084 "any objects or importing any modules. It can also be used to find all\n"
4085 "persistent references without instantiating any objects or importing\n"
4086 "any modules.\n"
4087 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00004088 {NULL, NULL} /* sentinel */
4089};
4090
4091
4092static Unpicklerobject *
4093newUnpicklerobject(PyObject *f) {
4094 Unpicklerobject *self;
4095
Guido van Rossumb18618d2000-05-03 23:44:39 +00004096 UNLESS (self = PyObject_New(Unpicklerobject, &Unpicklertype))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004097 return NULL;
4098
4099 self->file = NULL;
4100 self->arg = NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00004101 self->stack = (Pdata*)Pdata_New();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004102 self->pers_func = NULL;
4103 self->last_string = NULL;
4104 self->marks = NULL;
4105 self->num_marks = 0;
4106 self->marks_size = 0;
4107 self->buf_size = 0;
4108 self->read = NULL;
Guido van Rossum8a6dba31998-03-06 01:39:39 +00004109 self->readline = NULL;
Guido van Rossum21ef0881998-12-11 03:20:00 +00004110 self->safe_constructors = NULL;
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004111 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004112
Tim Peters84e87f32001-03-17 04:50:51 +00004113 UNLESS (self->memo = PyDict_New())
Guido van Rossum83addc72000-04-21 20:49:36 +00004114 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004115
4116 Py_INCREF(f);
4117 self->file = f;
4118
4119 /* Set read, readline based on type of f */
4120 if (PyFile_Check(f)) {
4121 self->fp = PyFile_AsFile(f);
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00004122 if (self->fp == NULL) {
Guido van Rossum83addc72000-04-21 20:49:36 +00004123 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
4124 goto err;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00004125 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004126 self->read_func = read_file;
4127 self->readline_func = readline_file;
4128 }
4129 else if (PycStringIO_InputCheck(f)) {
4130 self->fp = NULL;
4131 self->read_func = read_cStringIO;
4132 self->readline_func = readline_cStringIO;
4133 }
4134 else {
4135
4136 self->fp = NULL;
4137 self->read_func = read_other;
4138 self->readline_func = readline_other;
4139
Guido van Rossum053b8df1998-11-25 16:18:00 +00004140 UNLESS ((self->readline = PyObject_GetAttr(f, readline_str)) &&
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004141 (self->read = PyObject_GetAttr(f, read_str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004142 PyErr_Clear();
4143 PyErr_SetString( PyExc_TypeError, "argument must have 'read' and "
4144 "'readline' attributes" );
Guido van Rossum053b8df1998-11-25 16:18:00 +00004145 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004146 }
4147 }
4148
Guido van Rossum053b8df1998-11-25 16:18:00 +00004149 if (PyEval_GetRestricted()) {
4150 /* Restricted execution, get private tables */
4151 PyObject *m;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004152
Guido van Rossum053b8df1998-11-25 16:18:00 +00004153 UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
4154 self->safe_constructors=PyObject_GetAttr(m, safe_constructors_str);
4155 Py_DECREF(m);
4156 UNLESS (self->safe_constructors) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004157 }
4158 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00004159 self->safe_constructors=safe_constructors;
4160 Py_INCREF(safe_constructors);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004161 }
4162
Guido van Rossum60456fd1997-04-09 17:36:32 +00004163 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004164
4165err:
4166 Py_DECREF((PyObject *)self);
4167 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004168}
4169
4170
4171static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004172get_Unpickler(PyObject *self, PyObject *args) {
4173 PyObject *file;
Tim Peters84e87f32001-03-17 04:50:51 +00004174
Guido van Rossum43713e52000-02-29 13:59:29 +00004175 UNLESS (PyArg_ParseTuple(args, "O:Unpickler", &file))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004176 return NULL;
4177 return (PyObject *)newUnpicklerobject(file);
4178}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004179
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004180
Guido van Rossum60456fd1997-04-09 17:36:32 +00004181static void
4182Unpickler_dealloc(Unpicklerobject *self) {
4183 Py_XDECREF(self->readline);
4184 Py_XDECREF(self->read);
4185 Py_XDECREF(self->file);
4186 Py_XDECREF(self->memo);
4187 Py_XDECREF(self->stack);
4188 Py_XDECREF(self->pers_func);
4189 Py_XDECREF(self->arg);
4190 Py_XDECREF(self->last_string);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004191 Py_XDECREF(self->safe_constructors);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004192
Guido van Rossum60456fd1997-04-09 17:36:32 +00004193 if (self->marks) {
4194 free(self->marks);
4195 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004196
Guido van Rossum60456fd1997-04-09 17:36:32 +00004197 if (self->buf_size) {
4198 free(self->buf);
4199 }
Tim Peters84e87f32001-03-17 04:50:51 +00004200
Guido van Rossumb18618d2000-05-03 23:44:39 +00004201 PyObject_Del(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004202}
4203
4204
4205static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004206Unpickler_getattr(Unpicklerobject *self, char *name) {
4207 if (!strcmp(name, "persistent_load")) {
4208 if (!self->pers_func) {
4209 PyErr_SetString(PyExc_AttributeError, name);
4210 return NULL;
4211 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004212
Guido van Rossum60456fd1997-04-09 17:36:32 +00004213 Py_INCREF(self->pers_func);
4214 return self->pers_func;
4215 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004216
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004217 if (!strcmp(name, "find_global")) {
4218 if (!self->find_class) {
4219 PyErr_SetString(PyExc_AttributeError, name);
4220 return NULL;
4221 }
4222
4223 Py_INCREF(self->find_class);
4224 return self->find_class;
4225 }
4226
Guido van Rossum60456fd1997-04-09 17:36:32 +00004227 if (!strcmp(name, "memo")) {
4228 if (!self->memo) {
4229 PyErr_SetString(PyExc_AttributeError, name);
4230 return NULL;
4231 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004232
Guido van Rossum60456fd1997-04-09 17:36:32 +00004233 Py_INCREF(self->memo);
4234 return self->memo;
4235 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004236
Guido van Rossum60456fd1997-04-09 17:36:32 +00004237 if (!strcmp(name, "UnpicklingError")) {
4238 Py_INCREF(UnpicklingError);
4239 return UnpicklingError;
4240 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004241
Guido van Rossum60456fd1997-04-09 17:36:32 +00004242 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
4243}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004244
Guido van Rossum60456fd1997-04-09 17:36:32 +00004245
4246static int
4247Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004248
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004249 if (!strcmp(name, "persistent_load")) {
4250 Py_XDECREF(self->pers_func);
4251 self->pers_func = value;
4252 Py_XINCREF(value);
4253 return 0;
4254 }
4255
4256 if (!strcmp(name, "find_global")) {
4257 Py_XDECREF(self->find_class);
4258 self->find_class = value;
4259 Py_XINCREF(value);
4260 return 0;
4261 }
4262
Guido van Rossum053b8df1998-11-25 16:18:00 +00004263 if (! value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004264 PyErr_SetString(PyExc_TypeError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00004265 "attribute deletion is not supported");
4266 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004267 }
4268
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004269 if (strcmp(name, "memo") == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00004270 if (! PyDict_Check(value)) {
4271 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
4272 return -1;
4273 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004274 Py_XDECREF(self->memo);
4275 self->memo = value;
4276 Py_INCREF(value);
4277 return 0;
4278 }
4279
Guido van Rossum60456fd1997-04-09 17:36:32 +00004280 PyErr_SetString(PyExc_AttributeError, name);
4281 return -1;
4282}
4283
4284
4285static PyObject *
4286cpm_dump(PyObject *self, PyObject *args) {
4287 PyObject *ob, *file, *res = NULL;
4288 Picklerobject *pickler = 0;
4289 int bin = 0;
4290
Guido van Rossum053b8df1998-11-25 16:18:00 +00004291 UNLESS (PyArg_ParseTuple(args, "OO|i", &ob, &file, &bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004292 goto finally;
4293
Guido van Rossum053b8df1998-11-25 16:18:00 +00004294 UNLESS (pickler = newPicklerobject(file, bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004295 goto finally;
4296
4297 if (dump(pickler, ob) < 0)
4298 goto finally;
4299
4300 Py_INCREF(Py_None);
4301 res = Py_None;
4302
4303finally:
4304 Py_XDECREF(pickler);
4305
4306 return res;
4307}
4308
4309
4310static PyObject *
4311cpm_dumps(PyObject *self, PyObject *args) {
4312 PyObject *ob, *file = 0, *res = NULL;
4313 Picklerobject *pickler = 0;
4314 int bin = 0;
4315
Guido van Rossum43713e52000-02-29 13:59:29 +00004316 UNLESS (PyArg_ParseTuple(args, "O|i:dumps", &ob, &bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004317 goto finally;
4318
Guido van Rossum053b8df1998-11-25 16:18:00 +00004319 UNLESS (file = PycStringIO->NewOutput(128))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004320 goto finally;
4321
Guido van Rossum053b8df1998-11-25 16:18:00 +00004322 UNLESS (pickler = newPicklerobject(file, bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004323 goto finally;
4324
4325 if (dump(pickler, ob) < 0)
4326 goto finally;
4327
4328 res = PycStringIO->cgetvalue(file);
4329
4330finally:
4331 Py_XDECREF(pickler);
4332 Py_XDECREF(file);
4333
4334 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00004335}
4336
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004337
4338static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004339cpm_load(PyObject *self, PyObject *args) {
4340 Unpicklerobject *unpickler = 0;
4341 PyObject *ob, *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004342
Guido van Rossum43713e52000-02-29 13:59:29 +00004343 UNLESS (PyArg_ParseTuple(args, "O:load", &ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004344 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004345
Guido van Rossum053b8df1998-11-25 16:18:00 +00004346 UNLESS (unpickler = newUnpicklerobject(ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004347 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004348
Guido van Rossum60456fd1997-04-09 17:36:32 +00004349 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004350
Guido van Rossum60456fd1997-04-09 17:36:32 +00004351finally:
4352 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004353
Guido van Rossum60456fd1997-04-09 17:36:32 +00004354 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004355}
4356
4357
4358static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004359cpm_loads(PyObject *self, PyObject *args) {
4360 PyObject *ob, *file = 0, *res = NULL;
4361 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004362
Guido van Rossum43713e52000-02-29 13:59:29 +00004363 UNLESS (PyArg_ParseTuple(args, "S:loads", &ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004364 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004365
Guido van Rossum053b8df1998-11-25 16:18:00 +00004366 UNLESS (file = PycStringIO->NewInput(ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004367 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00004368
Guido van Rossum053b8df1998-11-25 16:18:00 +00004369 UNLESS (unpickler = newUnpicklerobject(file))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004370 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004371
Guido van Rossum60456fd1997-04-09 17:36:32 +00004372 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004373
Guido van Rossum60456fd1997-04-09 17:36:32 +00004374finally:
4375 Py_XDECREF(file);
4376 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004377
Guido van Rossum60456fd1997-04-09 17:36:32 +00004378 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004379}
4380
4381
Tim Peters84e87f32001-03-17 04:50:51 +00004382static char Unpicklertype__doc__[] =
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004383"Objects that know how to unpickle";
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004384
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004385static PyTypeObject Unpicklertype = {
4386 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004387 0, /*ob_size*/
4388 "Unpickler", /*tp_name*/
4389 sizeof(Unpicklerobject), /*tp_basicsize*/
4390 0, /*tp_itemsize*/
4391 /* methods */
4392 (destructor)Unpickler_dealloc, /*tp_dealloc*/
4393 (printfunc)0, /*tp_print*/
4394 (getattrfunc)Unpickler_getattr, /*tp_getattr*/
4395 (setattrfunc)Unpickler_setattr, /*tp_setattr*/
4396 (cmpfunc)0, /*tp_compare*/
4397 (reprfunc)0, /*tp_repr*/
4398 0, /*tp_as_number*/
4399 0, /*tp_as_sequence*/
4400 0, /*tp_as_mapping*/
4401 (hashfunc)0, /*tp_hash*/
4402 (ternaryfunc)0, /*tp_call*/
4403 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004404
Guido van Rossum60456fd1997-04-09 17:36:32 +00004405 /* Space for future expansion */
4406 0L,0L,0L,0L,
4407 Unpicklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004408};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004409
Guido van Rossum60456fd1997-04-09 17:36:32 +00004410static struct PyMethodDef cPickle_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004411 {"dump", (PyCFunction)cpm_dump, 1,
4412 "dump(object, file, [binary]) --"
4413 "Write an object in pickle format to the given file\n"
4414 "\n"
4415 "If the optional argument, binary, is provided and is true, then the\n"
4416 "pickle will be written in binary format, which is more space and\n"
4417 "computationally efficient. \n"
4418 },
4419 {"dumps", (PyCFunction)cpm_dumps, 1,
4420 "dumps(object, [binary]) --"
4421 "Return a string containing an object in pickle format\n"
4422 "\n"
4423 "If the optional argument, binary, is provided and is true, then the\n"
4424 "pickle will be written in binary format, which is more space and\n"
4425 "computationally efficient. \n"
4426 },
4427 {"load", (PyCFunction)cpm_load, 1,
4428 "load(file) -- Load a pickle from the given file"},
4429 {"loads", (PyCFunction)cpm_loads, 1,
4430 "loads(string) -- Load a pickle from the given string"},
4431 {"Pickler", (PyCFunction)get_Pickler, 1,
4432 "Pickler(file, [binary]) -- Create a pickler\n"
4433 "\n"
4434 "If the optional argument, binary, is provided and is true, then\n"
4435 "pickles will be written in binary format, which is more space and\n"
4436 "computationally efficient. \n"
4437 },
4438 {"Unpickler", (PyCFunction)get_Unpickler, 1,
4439 "Unpickler(file) -- Create an unpickler"},
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004440 { NULL, NULL }
4441};
4442
Guido van Rossum60456fd1997-04-09 17:36:32 +00004443static int
Guido van Rossumebba4202000-09-07 14:35:37 +00004444init_stuff(PyObject *module_dict) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004445 PyObject *string, *copy_reg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004446
4447#define INIT_STR(S) UNLESS(S ## _str=PyString_FromString(#S)) return -1;
4448
4449 INIT_STR(__class__);
4450 INIT_STR(__getinitargs__);
4451 INIT_STR(__dict__);
4452 INIT_STR(__getstate__);
4453 INIT_STR(__setstate__);
4454 INIT_STR(__name__);
Guido van Rossum142eeb81997-08-13 03:14:41 +00004455 INIT_STR(__main__);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004456 INIT_STR(__reduce__);
4457 INIT_STR(write);
4458 INIT_STR(__safe_for_unpickling__);
4459 INIT_STR(append);
4460 INIT_STR(read);
4461 INIT_STR(readline);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004462 INIT_STR(copy_reg);
4463 INIT_STR(dispatch_table);
4464 INIT_STR(safe_constructors);
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004465 INIT_STR(__basicnew__);
Guido van Rossum053b8df1998-11-25 16:18:00 +00004466 UNLESS (empty_str=PyString_FromString("")) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004467
Guido van Rossum053b8df1998-11-25 16:18:00 +00004468 UNLESS (copy_reg = PyImport_ImportModule("copy_reg"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004469 return -1;
4470
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004471 /* These next few are special because we want to use different
4472 ones in restricted mode. */
4473
Guido van Rossum053b8df1998-11-25 16:18:00 +00004474 UNLESS (dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004475 return -1;
4476
Guido van Rossum053b8df1998-11-25 16:18:00 +00004477 UNLESS (safe_constructors = PyObject_GetAttr(copy_reg,
4478 safe_constructors_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004479 return -1;
4480
4481 Py_DECREF(copy_reg);
4482
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004483 /* Down to here ********************************** */
4484
Guido van Rossum053b8df1998-11-25 16:18:00 +00004485 UNLESS (string = PyImport_ImportModule("string"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004486 return -1;
4487
Guido van Rossum053b8df1998-11-25 16:18:00 +00004488 UNLESS (atol_func = PyObject_GetAttrString(string, "atol"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004489 return -1;
4490
4491 Py_DECREF(string);
4492
Guido van Rossum053b8df1998-11-25 16:18:00 +00004493 UNLESS (empty_tuple = PyTuple_New(0))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004494 return -1;
4495
Guido van Rossumc03158b1999-06-09 15:23:31 +00004496 /* Ugh */
4497 UNLESS (t=PyImport_ImportModule("__builtin__")) return -1;
4498 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
4499 return -1;
4500
4501 UNLESS (t=PyDict_New()) return -1;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004502 UNLESS (r=PyRun_String(
4503 "def __init__(self, *args): self.args=args\n\n"
4504 "def __str__(self):\n"
4505 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
4506 Py_file_input,
4507 module_dict, t) ) return -1;
4508 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00004509
4510 UNLESS (PickleError = PyErr_NewException("cPickle.PickleError", NULL, t))
4511 return -1;
4512
4513 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00004514
Tim Peters84e87f32001-03-17 04:50:51 +00004515
4516 UNLESS (PicklingError = PyErr_NewException("cPickle.PicklingError",
Guido van Rossumc03158b1999-06-09 15:23:31 +00004517 PickleError, NULL))
4518 return -1;
4519
4520 UNLESS (t=PyDict_New()) return -1;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004521 UNLESS (r=PyRun_String(
4522 "def __init__(self, *args): self.args=args\n\n"
4523 "def __str__(self):\n"
4524 " a=self.args\n"
4525 " a=a and type(a[0]) or '(what)'\n"
4526 " return 'Cannot pickle %s objects' % a\n"
4527 , Py_file_input,
4528 module_dict, t) ) return -1;
4529 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00004530
4531 UNLESS (UnpickleableError = PyErr_NewException(
4532 "cPickle.UnpickleableError", PicklingError, t))
4533 return -1;
4534
4535 Py_DECREF(t);
4536
Tim Peters84e87f32001-03-17 04:50:51 +00004537 UNLESS (UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
Guido van Rossumc03158b1999-06-09 15:23:31 +00004538 PickleError, NULL))
4539 return -1;
4540
Tim Peters84e87f32001-03-17 04:50:51 +00004541 if (PyDict_SetItemString(module_dict, "PickleError",
Guido van Rossumc03158b1999-06-09 15:23:31 +00004542 PickleError) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004543 return -1;
4544
Tim Peters84e87f32001-03-17 04:50:51 +00004545 if (PyDict_SetItemString(module_dict, "PicklingError",
Guido van Rossum60456fd1997-04-09 17:36:32 +00004546 PicklingError) < 0)
4547 return -1;
4548
Guido van Rossum60456fd1997-04-09 17:36:32 +00004549 if (PyDict_SetItemString(module_dict, "UnpicklingError",
4550 UnpicklingError) < 0)
4551 return -1;
4552
Guido van Rossumc03158b1999-06-09 15:23:31 +00004553 if (PyDict_SetItemString(module_dict, "UnpickleableError",
4554 UnpickleableError) < 0)
4555 return -1;
4556
Guido van Rossum053b8df1998-11-25 16:18:00 +00004557 UNLESS (BadPickleGet = PyString_FromString("cPickle.BadPickleGet"))
4558 return -1;
4559
4560 if (PyDict_SetItemString(module_dict, "BadPickleGet",
4561 BadPickleGet) < 0)
4562 return -1;
4563
Guido van Rossum60456fd1997-04-09 17:36:32 +00004564 PycString_IMPORT;
Tim Peters84e87f32001-03-17 04:50:51 +00004565
Guido van Rossum60456fd1997-04-09 17:36:32 +00004566 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004567}
4568
Guido van Rossumf9ffb031999-02-04 14:54:04 +00004569#ifndef DL_EXPORT /* declarations for DLL import/export */
4570#define DL_EXPORT(RTYPE) RTYPE
4571#endif
Guido van Rossum50f385c1998-12-04 18:48:44 +00004572DL_EXPORT(void)
Thomas Wouters58d05102000-07-24 14:43:35 +00004573initcPickle(void) {
Guido van Rossumebba4202000-09-07 14:35:37 +00004574 PyObject *m, *d, *di, *v, *k;
4575 int i;
Guido van Rossum2f80d961999-07-13 15:18:58 +00004576 char *rev="1.71";
Guido van Rossum60456fd1997-04-09 17:36:32 +00004577 PyObject *format_version;
4578 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004579
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004580 Picklertype.ob_type = &PyType_Type;
4581 Unpicklertype.ob_type = &PyType_Type;
Guido van Rossum053b8df1998-11-25 16:18:00 +00004582 PdataType.ob_type = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004583
Tim Peters84e87f32001-03-17 04:50:51 +00004584 /* Initialize some pieces. We need to do this before module creation,
4585 so we're forced to use a temporary dictionary. :(
Guido van Rossumebba4202000-09-07 14:35:37 +00004586 */
4587 di=PyDict_New();
4588 if (!di) return;
4589 if (init_stuff(di) < 0) return;
4590
Guido van Rossum60456fd1997-04-09 17:36:32 +00004591 /* Create the module and add the functions */
4592 m = Py_InitModule4("cPickle", cPickle_methods,
4593 cPickle_module_documentation,
4594 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004595
Guido van Rossum60456fd1997-04-09 17:36:32 +00004596 /* Add some symbolic constants to the module */
4597 d = PyModule_GetDict(m);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004598 PyDict_SetItemString(d,"__version__", v = PyString_FromString(rev));
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004599 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00004600
Guido van Rossumebba4202000-09-07 14:35:37 +00004601 /* Copy data from di. Waaa. */
4602 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
4603 if (PyObject_SetItem(d, k, v) < 0) {
4604 Py_DECREF(di);
4605 return;
4606 }
4607 }
4608 Py_DECREF(di);
4609
Guido van Rossum60456fd1997-04-09 17:36:32 +00004610 format_version = PyString_FromString("1.3");
4611 compatible_formats = Py_BuildValue("[sss]", "1.0", "1.1", "1.2");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004612
Guido van Rossum60456fd1997-04-09 17:36:32 +00004613 PyDict_SetItemString(d, "format_version", format_version);
4614 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004615 Py_XDECREF(format_version);
4616 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004617}