blob: e3cc58d07ef070c63b79bd4f04ada493d0de4b81 [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
Tim Peters3906eb82001-04-10 04:22:00 +000071/* --------------------------------------------------------------------------
72NOTES on format codes.
73XXX much more is needed here
74
75Integer types
Tim Petersd8ae7c22001-04-10 04:35:28 +000076BININT1 8-bit unsigned integer; followed by 1 byte.
Tim Peters3906eb82001-04-10 04:22:00 +000077BININT2 16-bit unsigned integer; followed by 2 bytes, little-endian.
Tim Petersd8ae7c22001-04-10 04:35:28 +000078BININT 32-bit signed integer; followed by 4 bytes, little-endian.
79INT Integer; natural decimal string conversion, then newline.
Tim Peters3906eb82001-04-10 04:22:00 +000080 CAUTION: INT-reading code can't assume that what follows
81 fits in a Python int, because the size of Python ints varies
82 across platforms.
Tim Petersd8ae7c22001-04-10 04:35:28 +000083LONG Long (unbounded) integer; repr(i), then newline.
Tim Peters3906eb82001-04-10 04:22:00 +000084-------------------------------------------------------------------------- */
Guido van Rossum60456fd1997-04-09 17:36:32 +000085
86#define MARK '('
87#define STOP '.'
88#define POP '0'
89#define POP_MARK '1'
90#define DUP '2'
91#define FLOAT 'F'
Guido van Rossum60456fd1997-04-09 17:36:32 +000092#define BINFLOAT 'G'
Guido van Rossum60456fd1997-04-09 17:36:32 +000093#define INT 'I'
94#define BININT 'J'
95#define BININT1 'K'
96#define LONG 'L'
97#define BININT2 'M'
98#define NONE 'N'
99#define PERSID 'P'
100#define BINPERSID 'Q'
101#define REDUCE 'R'
102#define STRING 'S'
103#define BINSTRING 'T'
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000104#define SHORT_BINSTRING 'U'
Guido van Rossum5fccb7c2000-03-10 23:11:40 +0000105#define UNICODE 'V'
106#define BINUNICODE 'X'
Guido van Rossum60456fd1997-04-09 17:36:32 +0000107#define APPEND 'a'
108#define BUILD 'b'
109#define GLOBAL 'c'
110#define DICT 'd'
111#define EMPTY_DICT '}'
112#define APPENDS 'e'
113#define GET 'g'
114#define BINGET 'h'
115#define INST 'i'
116#define LONG_BINGET 'j'
117#define LIST 'l'
118#define EMPTY_LIST ']'
119#define OBJ 'o'
120#define PUT 'p'
121#define BINPUT 'q'
122#define LONG_BINPUT 'r'
123#define SETITEM 's'
124#define TUPLE 't'
125#define EMPTY_TUPLE ')'
126#define SETITEMS 'u'
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000127
Guido van Rossum60456fd1997-04-09 17:36:32 +0000128static char MARKv = MARK;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000129
Guido van Rossumc03158b1999-06-09 15:23:31 +0000130static PyObject *PickleError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000131static PyObject *PicklingError;
Guido van Rossumc03158b1999-06-09 15:23:31 +0000132static PyObject *UnpickleableError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000133static PyObject *UnpicklingError;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000134static PyObject *BadPickleGet;
135
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000136
Guido van Rossum60456fd1997-04-09 17:36:32 +0000137static PyObject *dispatch_table;
138static PyObject *safe_constructors;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000139static PyObject *empty_tuple;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000140
Guido van Rossum60456fd1997-04-09 17:36:32 +0000141static PyObject *__class___str, *__getinitargs___str, *__dict___str,
142 *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
143 *write_str, *__safe_for_unpickling___str, *append_str,
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000144 *read_str, *readline_str, *__main___str, *__basicnew___str,
Fred Drake2c7a6852001-07-17 18:34:03 +0000145 *copy_reg_str, *dispatch_table_str, *safe_constructors_str;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000146
Guido van Rossum053b8df1998-11-25 16:18:00 +0000147#ifndef PyList_SET_ITEM
148#define PyList_SET_ITEM(op, i, v) (((PyListObject *)(op))->ob_item[i] = (v))
149#endif
150#ifndef PyList_GET_SIZE
151#define PyList_GET_SIZE(op) (((PyListObject *)(op))->ob_size)
152#endif
153#ifndef PyTuple_SET_ITEM
154#define PyTuple_SET_ITEM(op, i, v) (((PyTupleObject *)(op))->ob_item[i] = (v))
155#endif
156#ifndef PyTuple_GET_SIZE
157#define PyTuple_GET_SIZE(op) (((PyTupleObject *)(op))->ob_size)
158#endif
159#ifndef PyString_GET_SIZE
160#define PyString_GET_SIZE(op) (((PyStringObject *)(op))->ob_size)
161#endif
162
163/*************************************************************************
164 Internal Data type for pickle data. */
165
166typedef struct {
167 PyObject_HEAD
168 int length, size;
169 PyObject **data;
170} Pdata;
171
Tim Peters84e87f32001-03-17 04:50:51 +0000172static void
Guido van Rossum053b8df1998-11-25 16:18:00 +0000173Pdata_dealloc(Pdata *self) {
174 int i;
175 PyObject **p;
176
177 for (i=self->length, p=self->data; --i >= 0; p++) Py_DECREF(*p);
178
179 if (self->data) free(self->data);
180
Guido van Rossumb18618d2000-05-03 23:44:39 +0000181 PyObject_Del(self);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000182}
183
184static PyTypeObject PdataType = {
185 PyObject_HEAD_INIT(NULL) 0, "Pdata", sizeof(Pdata), 0,
186 (destructor)Pdata_dealloc,
187 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
188};
189
190#define Pdata_Check(O) ((O)->ob_type == &PdataType)
191
192static PyObject *
Thomas Wouters58d05102000-07-24 14:43:35 +0000193Pdata_New(void) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000194 Pdata *self;
195
Guido van Rossumb18618d2000-05-03 23:44:39 +0000196 UNLESS (self = PyObject_New(Pdata, &PdataType)) return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000197 self->size=8;
198 self->length=0;
199 self->data=malloc(self->size * sizeof(PyObject*));
200 if (self->data) return (PyObject*)self;
201 Py_DECREF(self);
202 return PyErr_NoMemory();
203}
204
Tim Peters84e87f32001-03-17 04:50:51 +0000205static int
Thomas Wouters58d05102000-07-24 14:43:35 +0000206stackUnderflow(void) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000207 PyErr_SetString(UnpicklingError, "unpickling stack underflow");
208 return -1;
209}
210
211static int
212Pdata_clear(Pdata *self, int clearto) {
213 int i;
214 PyObject **p;
215
216 if (clearto < 0) return stackUnderflow();
217 if (clearto >= self->length) return 0;
218
219 for (i=self->length, p=self->data+clearto; --i >= clearto; p++)
220 Py_DECREF(*p);
221 self->length=clearto;
222
223 return 0;
224}
225
226
Tim Peters84e87f32001-03-17 04:50:51 +0000227static int
Guido van Rossum053b8df1998-11-25 16:18:00 +0000228Pdata_grow(Pdata *self) {
229 if (! self->size) {
230 PyErr_NoMemory();
231 return -1;
232 }
Tim Peters84e87f32001-03-17 04:50:51 +0000233 self->size *= 2;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000234 self->data = realloc(self->data, self->size*sizeof(PyObject*));
235 if (! self->data) {
Tim Peters84e87f32001-03-17 04:50:51 +0000236 self->size = 0;
237 PyErr_NoMemory();
238 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000239 }
240 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +0000241}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000242
243#define PDATA_POP(D,V) { \
244 if ((D)->length) V=D->data[--((D)->length)]; \
245 else { \
246 PyErr_SetString(UnpicklingError, "bad pickle data"); \
247 V=NULL; \
248 } \
249}
250
251
252static PyObject *
253Pdata_popTuple(Pdata *self, int start) {
254 PyObject *r;
255 int i, j, l;
256
257 l=self->length-start;
258 UNLESS (r=PyTuple_New(l)) return NULL;
Guido van Rossumea2b7152000-05-09 18:14:50 +0000259 for (i=start, j=0 ; j < l; i++, j++)
260 PyTuple_SET_ITEM(r, j, self->data[i]);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000261
262 self->length=start;
263 return r;
264}
265
266static PyObject *
267Pdata_popList(Pdata *self, int start) {
268 PyObject *r;
269 int i, j, l;
270
271 l=self->length-start;
272 UNLESS (r=PyList_New(l)) return NULL;
Guido van Rossumea2b7152000-05-09 18:14:50 +0000273 for (i=start, j=0 ; j < l; i++, j++)
274 PyList_SET_ITEM(r, j, self->data[i]);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000275
276 self->length=start;
277 return r;
278}
279
280#define PDATA_APPEND_(D,O,ER) { \
281 if (Pdata_Append(((Pdata*)(D)), O) < 0) return ER; \
282}
283
284#define PDATA_APPEND(D,O,ER) { \
285 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
286 Pdata_grow((Pdata*)(D)) < 0) \
287 return ER; \
288 Py_INCREF(O); \
289 ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O; \
290}
291
292#define PDATA_PUSH(D,O,ER) { \
293 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
294 Pdata_grow((Pdata*)(D)) < 0) { \
295 Py_DECREF(O); \
296 return ER; \
297 } \
298 ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O; \
299}
300
301/*************************************************************************/
302
303#define ARG_TUP(self, o) { \
304 if (self->arg || (self->arg=PyTuple_New(1))) { \
305 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
306 PyTuple_SET_ITEM(self->arg,0,o); \
307 } \
308 else { \
309 Py_DECREF(o); \
310 } \
311}
312
313#define FREE_ARG_TUP(self) { \
314 if (self->arg->ob_refcnt > 1) { \
315 Py_DECREF(self->arg); \
316 self->arg=NULL; \
317 } \
318 }
319
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000320typedef struct Picklerobject {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000321 PyObject_HEAD
322 FILE *fp;
323 PyObject *write;
324 PyObject *file;
325 PyObject *memo;
326 PyObject *arg;
327 PyObject *pers_func;
328 PyObject *inst_pers_func;
329 int bin;
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000330 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000331 int (*write_func)(struct Picklerobject *, char *, int);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000332 char *write_buf;
333 int buf_size;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000334 PyObject *dispatch_table;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000335} Picklerobject;
336
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000337staticforward PyTypeObject Picklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000338
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000339typedef struct Unpicklerobject {
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000340 PyObject_HEAD
341 FILE *fp;
342 PyObject *file;
343 PyObject *readline;
344 PyObject *read;
345 PyObject *memo;
346 PyObject *arg;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000347 Pdata *stack;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000348 PyObject *mark;
349 PyObject *pers_func;
350 PyObject *last_string;
351 int *marks;
352 int num_marks;
353 int marks_size;
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000354 int (*read_func)(struct Unpicklerobject *, char **, int);
355 int (*readline_func)(struct Unpicklerobject *, char **);
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000356 int buf_size;
357 char *buf;
358 PyObject *safe_constructors;
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +0000359 PyObject *find_class;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000360} Unpicklerobject;
Tim Peters84e87f32001-03-17 04:50:51 +0000361
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000362staticforward PyTypeObject Unpicklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000363
Thomas Wouters4789b3a2000-07-24 11:36:47 +0000364/* Forward decls that need the above structs */
365static int save(Picklerobject *, PyObject *, int);
366static int put2(Picklerobject *, PyObject *);
367
Tim Peters84e87f32001-03-17 04:50:51 +0000368int
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000369cPickle_PyMapping_HasKey(PyObject *o, PyObject *key) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000370 PyObject *v;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000371
Guido van Rossum053b8df1998-11-25 16:18:00 +0000372 if ((v = PyObject_GetItem(o,key))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000373 Py_DECREF(v);
374 return 1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000375 }
376
Guido van Rossum60456fd1997-04-09 17:36:32 +0000377 PyErr_Clear();
378 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000379}
380
Guido van Rossumd385d591997-04-09 17:47:47 +0000381static
Guido van Rossum60456fd1997-04-09 17:36:32 +0000382PyObject *
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000383cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
384{
Guido van Rossum60456fd1997-04-09 17:36:32 +0000385 va_list va;
386 PyObject *args=0, *retval=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000387 va_start(va, format);
Tim Peters84e87f32001-03-17 04:50:51 +0000388
Guido van Rossum053b8df1998-11-25 16:18:00 +0000389 if (format) args = Py_VaBuildValue(format, va);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000390 va_end(va);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000391 if (format && ! args) return NULL;
392 if (stringformat && !(retval=PyString_FromString(stringformat))) return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000393
Guido van Rossum053b8df1998-11-25 16:18:00 +0000394 if (retval) {
395 if (args) {
396 PyObject *v;
397 v=PyString_Format(retval, args);
398 Py_DECREF(retval);
399 Py_DECREF(args);
400 if (! v) return NULL;
401 retval=v;
402 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000403 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000404 else
Guido van Rossum053b8df1998-11-25 16:18:00 +0000405 if (args) retval=args;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000406 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000407 PyErr_SetObject(ErrType,Py_None);
408 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000409 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000410 PyErr_SetObject(ErrType,retval);
411 Py_DECREF(retval);
412 return NULL;
413}
414
Tim Peters84e87f32001-03-17 04:50:51 +0000415static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000416write_file(Picklerobject *self, char *s, int n) {
Tim Peters84e87f32001-03-17 04:50:51 +0000417 size_t nbyteswritten;
418
Guido van Rossum60456fd1997-04-09 17:36:32 +0000419 if (s == NULL) {
420 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000421 }
422
Tim Peters84e87f32001-03-17 04:50:51 +0000423 Py_BEGIN_ALLOW_THREADS
424 nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
425 Py_END_ALLOW_THREADS
426 if (nbyteswritten != (size_t)n) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000427 PyErr_SetFromErrno(PyExc_IOError);
428 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000429 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000430
431 return n;
432}
433
Tim Peters84e87f32001-03-17 04:50:51 +0000434static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000435write_cStringIO(Picklerobject *self, char *s, int n) {
436 if (s == NULL) {
437 return 0;
438 }
439
440 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
441 return -1;
442 }
443
444 return n;
445}
446
Tim Peters84e87f32001-03-17 04:50:51 +0000447static int
Guido van Rossum142eeb81997-08-13 03:14:41 +0000448write_none(Picklerobject *self, char *s, int n) {
449 if (s == NULL) return 0;
450 return n;
451}
452
Tim Peters84e87f32001-03-17 04:50:51 +0000453static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000454write_other(Picklerobject *self, char *s, int n) {
455 PyObject *py_str = 0, *junk = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000456
457 if (s == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000458 UNLESS (self->buf_size) return 0;
Tim Peters84e87f32001-03-17 04:50:51 +0000459 UNLESS (py_str =
Guido van Rossum60456fd1997-04-09 17:36:32 +0000460 PyString_FromStringAndSize(self->write_buf, self->buf_size))
Guido van Rossum053b8df1998-11-25 16:18:00 +0000461 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000462 }
463 else {
464 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
465 if (write_other(self, NULL, 0) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +0000466 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000467 }
468
Tim Peters84e87f32001-03-17 04:50:51 +0000469 if (n > WRITE_BUF_SIZE) {
470 UNLESS (py_str =
Guido van Rossum60456fd1997-04-09 17:36:32 +0000471 PyString_FromStringAndSize(s, n))
Guido van Rossum053b8df1998-11-25 16:18:00 +0000472 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000473 }
474 else {
475 memcpy(self->write_buf + self->buf_size, s, n);
476 self->buf_size += n;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000477 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000478 }
479 }
480
Guido van Rossum053b8df1998-11-25 16:18:00 +0000481 if (self->write) {
482 /* object with write method */
483 ARG_TUP(self, py_str);
484 if (self->arg) {
485 junk = PyObject_CallObject(self->write, self->arg);
486 FREE_ARG_TUP(self);
487 }
488 if (junk) Py_DECREF(junk);
489 else return -1;
490 }
Tim Peters84e87f32001-03-17 04:50:51 +0000491 else
Guido van Rossum053b8df1998-11-25 16:18:00 +0000492 PDATA_PUSH(self->file, py_str, -1);
Tim Peters84e87f32001-03-17 04:50:51 +0000493
494 self->buf_size = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000495 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000496}
497
498
Tim Peters84e87f32001-03-17 04:50:51 +0000499static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000500read_file(Unpicklerobject *self, char **s, int n) {
Tim Peters84e87f32001-03-17 04:50:51 +0000501 size_t nbytesread;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000502
503 if (self->buf_size == 0) {
504 int size;
505
Tim Peters84e87f32001-03-17 04:50:51 +0000506 size = ((n < 32) ? 32 : n);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000507 UNLESS (self->buf = (char *)malloc(size * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000508 PyErr_NoMemory();
509 return -1;
510 }
511
512 self->buf_size = size;
513 }
514 else if (n > self->buf_size) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000515 UNLESS (self->buf = (char *)realloc(self->buf, n * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000516 PyErr_NoMemory();
517 return -1;
518 }
Tim Peters84e87f32001-03-17 04:50:51 +0000519
Guido van Rossum60456fd1997-04-09 17:36:32 +0000520 self->buf_size = n;
521 }
Tim Peters84e87f32001-03-17 04:50:51 +0000522
523 Py_BEGIN_ALLOW_THREADS
524 nbytesread = fread(self->buf, sizeof(char), n, self->fp);
525 Py_END_ALLOW_THREADS
526 if (nbytesread != (size_t)n) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000527 if (feof(self->fp)) {
528 PyErr_SetNone(PyExc_EOFError);
529 return -1;
530 }
531
532 PyErr_SetFromErrno(PyExc_IOError);
533 return -1;
534 }
535
536 *s = self->buf;
537
538 return n;
539}
540
541
Tim Peters84e87f32001-03-17 04:50:51 +0000542static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000543readline_file(Unpicklerobject *self, char **s) {
544 int i;
545
546 if (self->buf_size == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000547 UNLESS (self->buf = (char *)malloc(40 * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000548 PyErr_NoMemory();
549 return -1;
550 }
Tim Peters84e87f32001-03-17 04:50:51 +0000551
Guido van Rossum60456fd1997-04-09 17:36:32 +0000552 self->buf_size = 40;
553 }
554
555 i = 0;
556 while (1) {
557 for (; i < (self->buf_size - 1); i++) {
558 if (feof(self->fp) || (self->buf[i] = getc(self->fp)) == '\n') {
559 self->buf[i + 1] = '\0';
560 *s = self->buf;
561 return i + 1;
562 }
563 }
564
Tim Peters84e87f32001-03-17 04:50:51 +0000565 UNLESS (self->buf = (char *)realloc(self->buf,
Guido van Rossum60456fd1997-04-09 17:36:32 +0000566 (self->buf_size * 2) * sizeof(char))) {
567 PyErr_NoMemory();
568 return -1;
569 }
570
571 self->buf_size *= 2;
572 }
573
Tim Peters84e87f32001-03-17 04:50:51 +0000574}
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000575
576
Tim Peters84e87f32001-03-17 04:50:51 +0000577static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000578read_cStringIO(Unpicklerobject *self, char **s, int n) {
579 char *ptr;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000580
Guido van Rossum60456fd1997-04-09 17:36:32 +0000581 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
582 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000583 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000584 }
585
Guido van Rossum60456fd1997-04-09 17:36:32 +0000586 *s = ptr;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000587
Guido van Rossum60456fd1997-04-09 17:36:32 +0000588 return n;
589}
590
591
Tim Peters84e87f32001-03-17 04:50:51 +0000592static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000593readline_cStringIO(Unpicklerobject *self, char **s) {
594 int n;
595 char *ptr;
596
597 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
598 return -1;
599 }
600
601 *s = ptr;
602
603 return n;
604}
605
606
Tim Peters84e87f32001-03-17 04:50:51 +0000607static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000608read_other(Unpicklerobject *self, char **s, int n) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000609 PyObject *bytes, *str=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000610
Guido van Rossum053b8df1998-11-25 16:18:00 +0000611 UNLESS (bytes = PyInt_FromLong(n)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000612
Guido van Rossum053b8df1998-11-25 16:18:00 +0000613 ARG_TUP(self, bytes);
614 if (self->arg) {
615 str = PyObject_CallObject(self->read, self->arg);
616 FREE_ARG_TUP(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000617 }
Guido van Rossum053b8df1998-11-25 16:18:00 +0000618 if (! str) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000619
620 Py_XDECREF(self->last_string);
621 self->last_string = str;
622
Guido van Rossum053b8df1998-11-25 16:18:00 +0000623 if (! (*s = PyString_AsString(str))) return -1;
624 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000625}
626
627
Tim Peters84e87f32001-03-17 04:50:51 +0000628static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000629readline_other(Unpicklerobject *self, char **s) {
630 PyObject *str;
631 int str_size;
632
Guido van Rossum053b8df1998-11-25 16:18:00 +0000633 UNLESS (str = PyObject_CallObject(self->readline, empty_tuple)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000634 return -1;
635 }
636
Guido van Rossum053b8df1998-11-25 16:18:00 +0000637 if ((str_size = PyString_Size(str)) < 0)
638 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000639
640 Py_XDECREF(self->last_string);
641 self->last_string = str;
642
Guido van Rossum053b8df1998-11-25 16:18:00 +0000643 if (! (*s = PyString_AsString(str)))
644 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000645
646 return str_size;
647}
648
649
650static char *
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000651pystrndup(char *s, int l) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000652 char *r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000653 UNLESS (r=malloc((l+1)*sizeof(char))) return (char*)PyErr_NoMemory();
Guido van Rossum60456fd1997-04-09 17:36:32 +0000654 memcpy(r,s,l);
655 r[l]=0;
656 return r;
657}
658
659
660static int
661get(Picklerobject *self, PyObject *id) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000662 PyObject *value, *mv;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000663 long c_value;
664 char s[30];
Guido van Rossum534b7c52000-06-28 22:23:56 +0000665 size_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000666
Guido van Rossum053b8df1998-11-25 16:18:00 +0000667 UNLESS (mv = PyDict_GetItem(self->memo, id)) {
668 PyErr_SetObject(PyExc_KeyError, id);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000669 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000670 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000671
Guido van Rossum053b8df1998-11-25 16:18:00 +0000672 UNLESS (value = PyTuple_GetItem(mv, 0))
Guido van Rossum60456fd1997-04-09 17:36:32 +0000673 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000674
Guido van Rossum053b8df1998-11-25 16:18:00 +0000675 UNLESS (PyInt_Check(value)) {
676 PyErr_SetString(PicklingError, "no int where int expected in memo");
677 return -1;
678 }
679 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000680
681 if (!self->bin) {
682 s[0] = GET;
683 sprintf(s + 1, "%ld\n", c_value);
684 len = strlen(s);
685 }
Guido van Rossum053b8df1998-11-25 16:18:00 +0000686 else if (Pdata_Check(self->file)) {
687 if (write_other(self, NULL, 0) < 0) return -1;
688 PDATA_APPEND(self->file, mv, -1);
689 return 0;
690 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000691 else {
692 if (c_value < 256) {
693 s[0] = BINGET;
694 s[1] = (int)(c_value & 0xff);
695 len = 2;
696 }
697 else {
698 s[0] = LONG_BINGET;
699 s[1] = (int)(c_value & 0xff);
700 s[2] = (int)((c_value >> 8) & 0xff);
701 s[3] = (int)((c_value >> 16) & 0xff);
702 s[4] = (int)((c_value >> 24) & 0xff);
703 len = 5;
704 }
705 }
706
707 if ((*self->write_func)(self, s, len) < 0)
708 return -1;
709
710 return 0;
711}
Tim Peters84e87f32001-03-17 04:50:51 +0000712
Guido van Rossum60456fd1997-04-09 17:36:32 +0000713
714static int
715put(Picklerobject *self, PyObject *ob) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000716 if (ob->ob_refcnt < 2 || self->fast)
Guido van Rossum60456fd1997-04-09 17:36:32 +0000717 return 0;
718
719 return put2(self, ob);
720}
721
Tim Peters84e87f32001-03-17 04:50:51 +0000722
Guido van Rossum60456fd1997-04-09 17:36:32 +0000723static int
724put2(Picklerobject *self, PyObject *ob) {
725 char c_str[30];
Guido van Rossum534b7c52000-06-28 22:23:56 +0000726 int p;
727 size_t len;
728 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000729 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000730
731 if (self->fast) return 0;
732
Guido van Rossum60456fd1997-04-09 17:36:32 +0000733 if ((p = PyDict_Size(self->memo)) < 0)
734 goto finally;
735
Guido van Rossum053b8df1998-11-25 16:18:00 +0000736 p++; /* Make sure memo keys are positive! */
737
Guido van Rossum534b7c52000-06-28 22:23:56 +0000738 UNLESS (py_ob_id = PyLong_FromVoidPtr(ob))
Guido van Rossum053b8df1998-11-25 16:18:00 +0000739 goto finally;
740
741 UNLESS (memo_len = PyInt_FromLong(p))
742 goto finally;
743
744 UNLESS (t = PyTuple_New(2))
745 goto finally;
746
747 PyTuple_SET_ITEM(t, 0, memo_len);
748 Py_INCREF(memo_len);
749 PyTuple_SET_ITEM(t, 1, ob);
750 Py_INCREF(ob);
751
752 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
753 goto finally;
754
Guido van Rossum60456fd1997-04-09 17:36:32 +0000755 if (!self->bin) {
756 c_str[0] = PUT;
757 sprintf(c_str + 1, "%d\n", p);
758 len = strlen(c_str);
759 }
Guido van Rossum053b8df1998-11-25 16:18:00 +0000760 else if (Pdata_Check(self->file)) {
761 if (write_other(self, NULL, 0) < 0) return -1;
762 PDATA_APPEND(self->file, memo_len, -1);
763 res=0; /* Job well done ;) */
764 goto finally;
765 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000766 else {
767 if (p >= 256) {
768 c_str[0] = LONG_BINPUT;
769 c_str[1] = (int)(p & 0xff);
770 c_str[2] = (int)((p >> 8) & 0xff);
771 c_str[3] = (int)((p >> 16) & 0xff);
772 c_str[4] = (int)((p >> 24) & 0xff);
773 len = 5;
774 }
775 else {
776 c_str[0] = BINPUT;
777 c_str[1] = p;
Tim Peters84e87f32001-03-17 04:50:51 +0000778 len = 2;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000779 }
780 }
781
782 if ((*self->write_func)(self, c_str, len) < 0)
783 goto finally;
784
Guido van Rossum60456fd1997-04-09 17:36:32 +0000785 res = 0;
786
787finally:
788 Py_XDECREF(py_ob_id);
789 Py_XDECREF(memo_len);
790 Py_XDECREF(t);
791
792 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000793}
794
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000795#define PyImport_Import cPickle_Import
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000796
797static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000798PyImport_Import(PyObject *module_name) {
799 static PyObject *silly_list=0, *__builtins___str=0, *__import___str;
800 static PyObject *standard_builtins=0;
801 PyObject *globals=0, *__import__=0, *__builtins__=0, *r=0;
802
Guido van Rossum053b8df1998-11-25 16:18:00 +0000803 UNLESS (silly_list) {
804 UNLESS (__import___str=PyString_FromString("__import__"))
805 return NULL;
806 UNLESS (__builtins___str=PyString_FromString("__builtins__"))
807 return NULL;
808 UNLESS (silly_list=Py_BuildValue("[s]","__doc__"))
809 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000810 }
811
Guido van Rossum053b8df1998-11-25 16:18:00 +0000812 if ((globals=PyEval_GetGlobals())) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000813 Py_INCREF(globals);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000814 UNLESS (__builtins__=PyObject_GetItem(globals,__builtins___str))
815 goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000816 }
817 else {
818 PyErr_Clear();
819
Guido van Rossum053b8df1998-11-25 16:18:00 +0000820 UNLESS (standard_builtins ||
821 (standard_builtins=PyImport_ImportModule("__builtin__")))
822 return NULL;
Tim Peters84e87f32001-03-17 04:50:51 +0000823
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000824 __builtins__=standard_builtins;
825 Py_INCREF(__builtins__);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000826 UNLESS (globals = Py_BuildValue("{sO}", "__builtins__", __builtins__))
827 goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000828 }
829
Guido van Rossum053b8df1998-11-25 16:18:00 +0000830 if (PyDict_Check(__builtins__)) {
831 UNLESS (__import__=PyObject_GetItem(__builtins__,__import___str)) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000832 }
833 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000834 UNLESS (__import__=PyObject_GetAttr(__builtins__,__import___str)) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000835 }
836
Guido van Rossum053b8df1998-11-25 16:18:00 +0000837 UNLESS (r=PyObject_CallFunction(__import__,"OOOO",
838 module_name, globals, globals, silly_list))
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000839 goto err;
840
841 Py_DECREF(globals);
842 Py_DECREF(__builtins__);
843 Py_DECREF(__import__);
Tim Peters84e87f32001-03-17 04:50:51 +0000844
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000845 return r;
846err:
847 Py_XDECREF(globals);
848 Py_XDECREF(__builtins__);
849 Py_XDECREF(__import__);
850 return NULL;
851}
852
853static PyObject *
Guido van Rossume2d81cd1998-08-08 19:40:10 +0000854whichmodule(PyObject *global, PyObject *global_name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000855 int i, j;
856 PyObject *module = 0, *modules_dict = 0,
857 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000858
Guido van Rossum45188231997-09-28 05:38:51 +0000859 module = PyObject_GetAttrString(global, "__module__");
860 if (module) return module;
861 PyErr_Clear();
862
Guido van Rossum053b8df1998-11-25 16:18:00 +0000863 UNLESS (modules_dict = PySys_GetObject("modules"))
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000864 return NULL;
865
Guido van Rossum60456fd1997-04-09 17:36:32 +0000866 i = 0;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000867 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
868
Guido van Rossum053b8df1998-11-25 16:18:00 +0000869 if (PyObject_Compare(name, __main___str)==0) continue;
Tim Peters84e87f32001-03-17 04:50:51 +0000870
Guido van Rossum053b8df1998-11-25 16:18:00 +0000871 UNLESS (global_name_attr = PyObject_GetAttr(module, global_name)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000872 PyErr_Clear();
873 continue;
874 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000875
Guido van Rossum60456fd1997-04-09 17:36:32 +0000876 if (global_name_attr != global) {
877 Py_DECREF(global_name_attr);
878 continue;
879 }
880
881 Py_DECREF(global_name_attr);
882
883 break;
884 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000885
886 /* The following implements the rule in pickle.py added in 1.5
887 that used __main__ if no module is found. I don't actually
888 like this rule. jlf
889 */
Guido van Rossum053b8df1998-11-25 16:18:00 +0000890 if (!j) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000891 j=1;
892 name=__main___str;
893 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000894
895 Py_INCREF(name);
896 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000897}
898
899
Guido van Rossum60456fd1997-04-09 17:36:32 +0000900static int
901save_none(Picklerobject *self, PyObject *args) {
902 static char none = NONE;
Tim Peters84e87f32001-03-17 04:50:51 +0000903 if ((*self->write_func)(self, &none, 1) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +0000904 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000905
Guido van Rossum60456fd1997-04-09 17:36:32 +0000906 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000907}
908
Tim Peters84e87f32001-03-17 04:50:51 +0000909
Guido van Rossum60456fd1997-04-09 17:36:32 +0000910static int
911save_int(Picklerobject *self, PyObject *args) {
912 char c_str[32];
913 long l = PyInt_AS_LONG((PyIntObject *)args);
914 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000915
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000916 if (!self->bin
917#if SIZEOF_LONG > 4
Tim Peters3906eb82001-04-10 04:22:00 +0000918 || l > 0x7fffffffL
919 || l < -0x80000000L
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000920#endif
Tim Peters3906eb82001-04-10 04:22:00 +0000921 ) {
922 /* Text-mode pickle, or long too big to fit in the 4-byte
923 * signed BININT format: store as a string.
924 */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000925 c_str[0] = INT;
926 sprintf(c_str + 1, "%ld\n", l);
927 if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
928 return -1;
929 }
930 else {
Tim Petersd8ae7c22001-04-10 04:35:28 +0000931 /* Binary pickle and l fits in a signed 4-byte int. */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000932 c_str[1] = (int)( l & 0xff);
933 c_str[2] = (int)((l >> 8) & 0xff);
934 c_str[3] = (int)((l >> 16) & 0xff);
935 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000936
Guido van Rossum60456fd1997-04-09 17:36:32 +0000937 if ((c_str[4] == 0) && (c_str[3] == 0)) {
938 if (c_str[2] == 0) {
939 c_str[0] = BININT1;
940 len = 2;
941 }
942 else {
943 c_str[0] = BININT2;
944 len = 3;
945 }
946 }
947 else {
948 c_str[0] = BININT;
949 len = 5;
950 }
951
952 if ((*self->write_func)(self, c_str, len) < 0)
953 return -1;
954 }
955
956 return 0;
957}
958
959
960static int
961save_long(Picklerobject *self, PyObject *args) {
962 int size, res = -1;
963 PyObject *repr = 0;
964
965 static char l = LONG;
966
Guido van Rossum053b8df1998-11-25 16:18:00 +0000967 UNLESS (repr = PyObject_Repr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +0000968 goto finally;
969
970 if ((size = PyString_Size(repr)) < 0)
971 goto finally;
972
973 if ((*self->write_func)(self, &l, 1) < 0)
974 goto finally;
975
Tim Peters84e87f32001-03-17 04:50:51 +0000976 if ((*self->write_func)(self,
Guido van Rossum60456fd1997-04-09 17:36:32 +0000977 PyString_AS_STRING((PyStringObject *)repr), size) < 0)
978 goto finally;
979
980 if ((*self->write_func)(self, "\n", 1) < 0)
981 goto finally;
982
983 res = 0;
984
985finally:
986 Py_XDECREF(repr);
987
988 return res;
989}
990
991
992static int
993save_float(Picklerobject *self, PyObject *args) {
994 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
995
Guido van Rossum60456fd1997-04-09 17:36:32 +0000996 if (self->bin) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000997 int s, e;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000998 double f;
999 long fhi, flo;
Fred Drake2c7a6852001-07-17 18:34:03 +00001000 char str[9];
1001 unsigned char *p = (unsigned char *)str;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001002
1003 *p = BINFLOAT;
1004 p++;
1005
1006 if (x < 0) {
1007 s = 1;
1008 x = -x;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001009 }
1010 else
Guido van Rossum60456fd1997-04-09 17:36:32 +00001011 s = 0;
1012
1013 f = frexp(x, &e);
1014
1015 /* Normalize f to be in the range [1.0, 2.0) */
1016 if (0.5 <= f && f < 1.0) {
1017 f *= 2.0;
1018 e--;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001019 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001020 else if (f == 0.0) {
1021 e = 0;
1022 }
1023 else {
1024 PyErr_SetString(PyExc_SystemError,
1025 "frexp() result out of range");
1026 return -1;
1027 }
1028
1029 if (e >= 1024) {
1030 /* XXX 1024 itself is reserved for Inf/NaN */
1031 PyErr_SetString(PyExc_OverflowError,
1032 "float too large to pack with d format");
1033 return -1;
1034 }
1035 else if (e < -1022) {
1036 /* Gradual underflow */
1037 f = ldexp(f, 1022 + e);
1038 e = 0;
1039 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001040 else if (!(e == 0 && f == 0.0)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001041 e += 1023;
1042 f -= 1.0; /* Get rid of leading 1 */
1043 }
1044
1045 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
1046 f *= 268435456.0; /* 2**28 */
1047 fhi = (long) floor(f); /* Truncate */
1048 f -= (double)fhi;
1049 f *= 16777216.0; /* 2**24 */
1050 flo = (long) floor(f + 0.5); /* Round */
1051
1052 /* First byte */
1053 *p = (s<<7) | (e>>4);
1054 p++;
1055
1056 /* Second byte */
Fred Drake2c7a6852001-07-17 18:34:03 +00001057 *p = (unsigned char) (((e&0xF)<<4) | (fhi>>24));
Guido van Rossum60456fd1997-04-09 17:36:32 +00001058 p++;
1059
1060 /* Third byte */
Fred Drake2c7a6852001-07-17 18:34:03 +00001061 *p = (unsigned char) ((fhi>>16) & 0xFF);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001062 p++;
1063
1064 /* Fourth byte */
Fred Drake2c7a6852001-07-17 18:34:03 +00001065 *p = (unsigned char) ((fhi>>8) & 0xFF);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001066 p++;
1067
1068 /* Fifth byte */
Fred Drake2c7a6852001-07-17 18:34:03 +00001069 *p = (unsigned char) (fhi & 0xFF);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001070 p++;
1071
1072 /* Sixth byte */
Fred Drake2c7a6852001-07-17 18:34:03 +00001073 *p = (unsigned char) ((flo>>16) & 0xFF);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001074 p++;
1075
1076 /* Seventh byte */
Fred Drake2c7a6852001-07-17 18:34:03 +00001077 *p = (unsigned char) ((flo>>8) & 0xFF);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001078 p++;
1079
1080 /* Eighth byte */
Fred Drake2c7a6852001-07-17 18:34:03 +00001081 *p = (unsigned char) (flo & 0xFF);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001082
1083 if ((*self->write_func)(self, str, 9) < 0)
1084 return -1;
1085 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001086 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001087 char c_str[250];
1088 c_str[0] = FLOAT;
Guido van Rossum104be4a1998-04-03 21:13:02 +00001089 sprintf(c_str + 1, "%.17g\n", x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001090
1091 if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
1092 return -1;
1093 }
1094
1095 return 0;
1096}
1097
1098
1099static int
Guido van Rossum142eeb81997-08-13 03:14:41 +00001100save_string(Picklerobject *self, PyObject *args, int doput) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001101 int size, len;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001102 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001103
Guido van Rossum053b8df1998-11-25 16:18:00 +00001104 if ((size = PyString_Size(args)) < 0)
1105 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001106
1107 if (!self->bin) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001108 char *repr_str;
1109
1110 static char string = STRING;
1111
Guido van Rossum053b8df1998-11-25 16:18:00 +00001112 UNLESS (repr = PyObject_Repr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001113 return -1;
1114
Guido van Rossum053b8df1998-11-25 16:18:00 +00001115 if ((len = PyString_Size(repr)) < 0)
1116 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001117 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001118
1119 if ((*self->write_func)(self, &string, 1) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001120 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001121
1122 if ((*self->write_func)(self, repr_str, len) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001123 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001124
1125 if ((*self->write_func)(self, "\n", 1) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001126 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001127
1128 Py_XDECREF(repr);
1129 }
1130 else {
1131 int i;
1132 char c_str[5];
1133
Guido van Rossum053b8df1998-11-25 16:18:00 +00001134 if ((size = PyString_Size(args)) < 0)
1135 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001136
1137 if (size < 256) {
1138 c_str[0] = SHORT_BINSTRING;
1139 c_str[1] = size;
1140 len = 2;
1141 }
1142 else {
1143 c_str[0] = BINSTRING;
1144 for (i = 1; i < 5; i++)
1145 c_str[i] = (int)(size >> ((i - 1) * 8));
1146 len = 5;
1147 }
1148
1149 if ((*self->write_func)(self, c_str, len) < 0)
1150 return -1;
1151
Guido van Rossum053b8df1998-11-25 16:18:00 +00001152 if (size > 128 && Pdata_Check(self->file)) {
1153 if (write_other(self, NULL, 0) < 0) return -1;
1154 PDATA_APPEND(self->file, args, -1);
1155 }
1156 else {
Tim Peters84e87f32001-03-17 04:50:51 +00001157 if ((*self->write_func)(self,
Guido van Rossum053b8df1998-11-25 16:18:00 +00001158 PyString_AS_STRING((PyStringObject *)args), size) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001159 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001160 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001161 }
1162
Guido van Rossum142eeb81997-08-13 03:14:41 +00001163 if (doput)
1164 if (put(self, args) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001165 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001166
1167 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001168
1169err:
1170 Py_XDECREF(repr);
1171 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001172}
1173
1174
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001175/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1176 backslash and newline characters to \uXXXX escapes. */
1177static PyObject *
1178modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, int size)
1179{
1180 PyObject *repr;
1181 char *p;
1182 char *q;
1183
1184 static const char *hexdigit = "0123456789ABCDEF";
1185
1186 repr = PyString_FromStringAndSize(NULL, 6 * size);
1187 if (repr == NULL)
1188 return NULL;
1189 if (size == 0)
1190 return repr;
1191
1192 p = q = PyString_AS_STRING(repr);
1193 while (size-- > 0) {
1194 Py_UNICODE ch = *s++;
1195 /* Map 16-bit characters to '\uxxxx' */
1196 if (ch >= 256 || ch == '\\' || ch == '\n') {
1197 *p++ = '\\';
1198 *p++ = 'u';
1199 *p++ = hexdigit[(ch >> 12) & 0xf];
1200 *p++ = hexdigit[(ch >> 8) & 0xf];
1201 *p++ = hexdigit[(ch >> 4) & 0xf];
1202 *p++ = hexdigit[ch & 15];
1203 }
1204 /* Copy everything else as-is */
1205 else
1206 *p++ = (char) ch;
1207 }
1208 *p = '\0';
1209 if (_PyString_Resize(&repr, p - q))
1210 goto onError;
1211
1212 return repr;
1213
1214 onError:
1215 Py_DECREF(repr);
1216 return NULL;
1217}
1218
1219
Guido van Rossum60456fd1997-04-09 17:36:32 +00001220static int
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001221save_unicode(Picklerobject *self, PyObject *args, int doput) {
1222 int size, len;
1223 PyObject *repr=0;
1224
1225 if (!PyUnicode_Check(args))
1226 return -1;
1227
1228 if (!self->bin) {
1229 char *repr_str;
1230 static char string = UNICODE;
1231
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001232 UNLESS(repr = modified_EncodeRawUnicodeEscape(
1233 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args)))
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001234 return -1;
1235
1236 if ((len = PyString_Size(repr)) < 0)
1237 goto err;
1238 repr_str = PyString_AS_STRING((PyStringObject *)repr);
1239
1240 if ((*self->write_func)(self, &string, 1) < 0)
1241 goto err;
1242
1243 if ((*self->write_func)(self, repr_str, len) < 0)
1244 goto err;
1245
1246 if ((*self->write_func)(self, "\n", 1) < 0)
1247 goto err;
1248
1249 Py_XDECREF(repr);
1250 }
1251 else {
1252 int i;
1253 char c_str[5];
1254
1255 UNLESS (repr = PyUnicode_AsUTF8String(args))
1256 return -1;
1257
1258 if ((size = PyString_Size(repr)) < 0)
1259 goto err;
1260
1261 c_str[0] = BINUNICODE;
1262 for (i = 1; i < 5; i++)
1263 c_str[i] = (int)(size >> ((i - 1) * 8));
1264 len = 5;
1265
1266 if ((*self->write_func)(self, c_str, len) < 0)
1267 goto err;
1268
1269 if (size > 128 && Pdata_Check(self->file)) {
1270 if (write_other(self, NULL, 0) < 0)
1271 goto err;
1272 PDATA_APPEND(self->file, repr, -1);
1273 }
1274 else {
1275 if ((*self->write_func)(self, PyString_AS_STRING(repr), size) < 0)
1276 goto err;
1277 }
1278
1279 Py_DECREF(repr);
1280 }
1281
1282 if (doput)
1283 if (put(self, args) < 0)
1284 return -1;
1285
1286 return 0;
1287
1288err:
1289 Py_XDECREF(repr);
1290 return -1;
1291}
1292
1293
1294static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00001295save_tuple(Picklerobject *self, PyObject *args) {
1296 PyObject *element = 0, *py_tuple_id = 0;
1297 int len, i, has_key, res = -1;
1298
1299 static char tuple = TUPLE;
1300
1301 if ((*self->write_func)(self, &MARKv, 1) < 0)
1302 goto finally;
1303
Tim Peters84e87f32001-03-17 04:50:51 +00001304 if ((len = PyTuple_Size(args)) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001305 goto finally;
1306
1307 for (i = 0; i < len; i++) {
Tim Peters84e87f32001-03-17 04:50:51 +00001308 UNLESS (element = PyTuple_GET_ITEM((PyTupleObject *)args, i))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001309 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001310
Guido van Rossum60456fd1997-04-09 17:36:32 +00001311 if (save(self, element, 0) < 0)
1312 goto finally;
1313 }
1314
Guido van Rossum534b7c52000-06-28 22:23:56 +00001315 UNLESS (py_tuple_id = PyLong_FromVoidPtr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001316 goto finally;
1317
1318 if (len) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001319 if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_tuple_id)) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001320 goto finally;
1321
1322 if (has_key) {
1323 if (self->bin) {
1324 static char pop_mark = POP_MARK;
Tim Peters84e87f32001-03-17 04:50:51 +00001325
Guido van Rossum60456fd1997-04-09 17:36:32 +00001326 if ((*self->write_func)(self, &pop_mark, 1) < 0)
1327 goto finally;
1328 }
1329 else {
1330 static char pop = POP;
Tim Peters84e87f32001-03-17 04:50:51 +00001331
Guido van Rossum60456fd1997-04-09 17:36:32 +00001332 for (i = 0; i <= len; i++) {
1333 if ((*self->write_func)(self, &pop, 1) < 0)
1334 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001335 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001336 }
Tim Peters84e87f32001-03-17 04:50:51 +00001337
Guido van Rossum60456fd1997-04-09 17:36:32 +00001338 if (get(self, py_tuple_id) < 0)
1339 goto finally;
1340
1341 res = 0;
1342 goto finally;
1343 }
1344 }
1345
1346 if ((*self->write_func)(self, &tuple, 1) < 0) {
1347 goto finally;
1348 }
1349
1350 if (put(self, args) < 0)
1351 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001352
Guido van Rossum60456fd1997-04-09 17:36:32 +00001353 res = 0;
1354
1355finally:
1356 Py_XDECREF(py_tuple_id);
1357
1358 return res;
1359}
1360
1361static int
1362save_empty_tuple(Picklerobject *self, PyObject *args) {
1363 static char tuple = EMPTY_TUPLE;
1364
1365 return (*self->write_func)(self, &tuple, 1);
1366}
1367
1368
1369static int
1370save_list(Picklerobject *self, PyObject *args) {
1371 PyObject *element = 0;
1372 int s_len, len, i, using_appends, res = -1;
1373 char s[3];
1374
1375 static char append = APPEND, appends = APPENDS;
1376
Guido van Rossum053b8df1998-11-25 16:18:00 +00001377 if (self->bin) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001378 s[0] = EMPTY_LIST;
1379 s_len = 1;
Tim Peters84e87f32001-03-17 04:50:51 +00001380 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001381 else {
1382 s[0] = MARK;
1383 s[1] = LIST;
1384 s_len = 2;
1385 }
1386
1387 if ((len = PyList_Size(args)) < 0)
1388 goto finally;
1389
1390 if ((*self->write_func)(self, s, s_len) < 0)
1391 goto finally;
1392
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001393 if (len == 0) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001394 if (put(self, args) < 0)
1395 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001396 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001397 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001398 if (put2(self, args) < 0)
1399 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001400 }
1401
Guido van Rossum142eeb81997-08-13 03:14:41 +00001402 if ((using_appends = (self->bin && (len > 1))))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001403 if ((*self->write_func)(self, &MARKv, 1) < 0)
1404 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001405
Guido van Rossum60456fd1997-04-09 17:36:32 +00001406 for (i = 0; i < len; i++) {
Tim Peters84e87f32001-03-17 04:50:51 +00001407 UNLESS (element = PyList_GET_ITEM((PyListObject *)args, i))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001408 goto finally;
1409
Tim Peters84e87f32001-03-17 04:50:51 +00001410 if (save(self, element, 0) < 0)
1411 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001412
1413 if (!using_appends) {
1414 if ((*self->write_func)(self, &append, 1) < 0)
1415 goto finally;
1416 }
1417 }
1418
1419 if (using_appends) {
1420 if ((*self->write_func)(self, &appends, 1) < 0)
1421 goto finally;
1422 }
1423
1424 res = 0;
1425
1426finally:
1427
1428 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001429}
1430
1431
Guido van Rossum60456fd1997-04-09 17:36:32 +00001432static int
1433save_dict(Picklerobject *self, PyObject *args) {
1434 PyObject *key = 0, *value = 0;
1435 int i, len, res = -1, using_setitems;
1436 char s[3];
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001437
Guido van Rossum60456fd1997-04-09 17:36:32 +00001438 static char setitem = SETITEM, setitems = SETITEMS;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001439
Guido van Rossum60456fd1997-04-09 17:36:32 +00001440 if (self->bin) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001441 s[0] = EMPTY_DICT;
1442 len = 1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001443 }
1444 else {
1445 s[0] = MARK;
1446 s[1] = DICT;
1447 len = 2;
1448 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001449
Guido van Rossum60456fd1997-04-09 17:36:32 +00001450 if ((*self->write_func)(self, s, len) < 0)
1451 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001452
Guido van Rossum60456fd1997-04-09 17:36:32 +00001453 if ((len = PyDict_Size(args)) < 0)
1454 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001455
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001456 if (len == 0) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001457 if (put(self, args) < 0)
1458 goto finally;
1459 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001460 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001461 if (put2(self, args) < 0)
1462 goto finally;
1463 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001464
Guido van Rossum142eeb81997-08-13 03:14:41 +00001465 if ((using_setitems = (self->bin && (PyDict_Size(args) > 1))))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001466 if ((*self->write_func)(self, &MARKv, 1) < 0)
1467 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001468
Guido van Rossum60456fd1997-04-09 17:36:32 +00001469 i = 0;
1470 while (PyDict_Next(args, &i, &key, &value)) {
1471 if (save(self, key, 0) < 0)
1472 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001473
Guido van Rossum60456fd1997-04-09 17:36:32 +00001474 if (save(self, value, 0) < 0)
1475 goto finally;
1476
1477 if (!using_setitems) {
1478 if ((*self->write_func)(self, &setitem, 1) < 0)
1479 goto finally;
1480 }
1481 }
1482
1483 if (using_setitems) {
1484 if ((*self->write_func)(self, &setitems, 1) < 0)
1485 goto finally;
1486 }
1487
1488 res = 0;
1489
1490finally:
1491
1492 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001493}
1494
1495
Tim Peters84e87f32001-03-17 04:50:51 +00001496static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00001497save_inst(Picklerobject *self, PyObject *args) {
Tim Peters84e87f32001-03-17 04:50:51 +00001498 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
Guido van Rossum60456fd1997-04-09 17:36:32 +00001499 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1500 char *module_str, *name_str;
1501 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001502
Guido van Rossum60456fd1997-04-09 17:36:32 +00001503 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001504
Guido van Rossum60456fd1997-04-09 17:36:32 +00001505 if ((*self->write_func)(self, &MARKv, 1) < 0)
1506 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001507
Guido van Rossum053b8df1998-11-25 16:18:00 +00001508 UNLESS (class = PyObject_GetAttr(args, __class___str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001509 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001510
Guido van Rossum60456fd1997-04-09 17:36:32 +00001511 if (self->bin) {
1512 if (save(self, class, 0) < 0)
1513 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001514 }
1515
Guido van Rossum142eeb81997-08-13 03:14:41 +00001516 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001517 PyObject *element = 0;
1518 int i, len;
1519
Tim Peters84e87f32001-03-17 04:50:51 +00001520 UNLESS (class_args =
Guido van Rossum60456fd1997-04-09 17:36:32 +00001521 PyObject_CallObject(getinitargs_func, empty_tuple))
1522 goto finally;
1523
Tim Peters84e87f32001-03-17 04:50:51 +00001524 if ((len = PyObject_Size(class_args)) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001525 goto finally;
1526
1527 for (i = 0; i < len; i++) {
Tim Peters84e87f32001-03-17 04:50:51 +00001528 UNLESS (element = PySequence_GetItem(class_args, i))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001529 goto finally;
1530
1531 if (save(self, element, 0) < 0) {
1532 Py_DECREF(element);
1533 goto finally;
1534 }
1535
1536 Py_DECREF(element);
1537 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001538 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001539 else {
1540 PyErr_Clear();
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001541 }
1542
Guido van Rossum60456fd1997-04-09 17:36:32 +00001543 if (!self->bin) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001544 UNLESS (name = ((PyClassObject *)class)->cl_name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001545 PyErr_SetString(PicklingError, "class has no name");
1546 goto finally;
1547 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001548
Guido van Rossum053b8df1998-11-25 16:18:00 +00001549 UNLESS (module = whichmodule(class, name))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001550 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001551
Tim Peters84e87f32001-03-17 04:50:51 +00001552
Guido van Rossum053b8df1998-11-25 16:18:00 +00001553 if ((module_size = PyString_Size(module)) < 0 ||
1554 (name_size = PyString_Size(name)) < 0)
1555 goto finally;
1556
Guido van Rossum60456fd1997-04-09 17:36:32 +00001557 module_str = PyString_AS_STRING((PyStringObject *)module);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001558 name_str = PyString_AS_STRING((PyStringObject *)name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001559
Guido van Rossum60456fd1997-04-09 17:36:32 +00001560 if ((*self->write_func)(self, &inst, 1) < 0)
1561 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001562
Guido van Rossum60456fd1997-04-09 17:36:32 +00001563 if ((*self->write_func)(self, module_str, module_size) < 0)
1564 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001565
Guido van Rossum60456fd1997-04-09 17:36:32 +00001566 if ((*self->write_func)(self, "\n", 1) < 0)
1567 goto finally;
1568
1569 if ((*self->write_func)(self, name_str, name_size) < 0)
1570 goto finally;
1571
1572 if ((*self->write_func)(self, "\n", 1) < 0)
1573 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001574 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001575 else if ((*self->write_func)(self, &obj, 1) < 0) {
1576 goto finally;
1577 }
1578
Guido van Rossum142eeb81997-08-13 03:14:41 +00001579 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001580 UNLESS (state = PyObject_CallObject(getstate_func, empty_tuple))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001581 goto finally;
1582 }
1583 else {
1584 PyErr_Clear();
1585
Guido van Rossum053b8df1998-11-25 16:18:00 +00001586 UNLESS (state = PyObject_GetAttr(args, __dict___str)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001587 PyErr_Clear();
1588 res = 0;
1589 goto finally;
1590 }
1591 }
1592
1593 if (!PyDict_Check(state)) {
1594 if (put2(self, args) < 0)
1595 goto finally;
1596 }
1597 else {
1598 if (put(self, args) < 0)
1599 goto finally;
1600 }
Tim Peters84e87f32001-03-17 04:50:51 +00001601
Guido van Rossum60456fd1997-04-09 17:36:32 +00001602 if (save(self, state, 0) < 0)
1603 goto finally;
1604
1605 if ((*self->write_func)(self, &build, 1) < 0)
1606 goto finally;
1607
1608 res = 0;
1609
1610finally:
1611 Py_XDECREF(module);
1612 Py_XDECREF(class);
1613 Py_XDECREF(state);
1614 Py_XDECREF(getinitargs_func);
1615 Py_XDECREF(getstate_func);
1616 Py_XDECREF(class_args);
1617
1618 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001619}
1620
1621
Guido van Rossum60456fd1997-04-09 17:36:32 +00001622static int
1623save_global(Picklerobject *self, PyObject *args, PyObject *name) {
1624 PyObject *global_name = 0, *module = 0;
Tim Peters84e87f32001-03-17 04:50:51 +00001625 char *name_str, *module_str;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001626 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001627
Guido van Rossum60456fd1997-04-09 17:36:32 +00001628 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001629
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001630 if (name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001631 global_name = name;
1632 Py_INCREF(global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001633 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001634 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001635 UNLESS (global_name = PyObject_GetAttr(args, __name___str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001636 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001637 }
1638
Guido van Rossum053b8df1998-11-25 16:18:00 +00001639 UNLESS (module = whichmodule(args, global_name))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001640 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001641
Guido van Rossum053b8df1998-11-25 16:18:00 +00001642 if ((module_size = PyString_Size(module)) < 0 ||
1643 (name_size = PyString_Size(global_name)) < 0)
1644 goto finally;
1645
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001646 module_str = PyString_AS_STRING((PyStringObject *)module);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001647 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001648
Guido van Rossum60456fd1997-04-09 17:36:32 +00001649 if ((*self->write_func)(self, &global, 1) < 0)
1650 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001651
Guido van Rossum60456fd1997-04-09 17:36:32 +00001652 if ((*self->write_func)(self, module_str, module_size) < 0)
1653 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001654
Guido van Rossum60456fd1997-04-09 17:36:32 +00001655 if ((*self->write_func)(self, "\n", 1) < 0)
1656 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001657
Guido van Rossum60456fd1997-04-09 17:36:32 +00001658 if ((*self->write_func)(self, name_str, name_size) < 0)
1659 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001660
Guido van Rossum60456fd1997-04-09 17:36:32 +00001661 if ((*self->write_func)(self, "\n", 1) < 0)
1662 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001663
Guido van Rossum60456fd1997-04-09 17:36:32 +00001664 if (put(self, args) < 0)
1665 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001666
Guido van Rossum60456fd1997-04-09 17:36:32 +00001667 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001668
Guido van Rossum60456fd1997-04-09 17:36:32 +00001669finally:
1670 Py_XDECREF(module);
1671 Py_XDECREF(global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001672
Guido van Rossum60456fd1997-04-09 17:36:32 +00001673 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001674}
1675
Guido van Rossum60456fd1997-04-09 17:36:32 +00001676static int
1677save_pers(Picklerobject *self, PyObject *args, PyObject *f) {
1678 PyObject *pid = 0;
1679 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001680
Guido van Rossum60456fd1997-04-09 17:36:32 +00001681 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001682
Guido van Rossum053b8df1998-11-25 16:18:00 +00001683 Py_INCREF(args);
1684 ARG_TUP(self, args);
1685 if (self->arg) {
1686 pid = PyObject_CallObject(f, self->arg);
1687 FREE_ARG_TUP(self);
1688 }
1689 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001690
Guido van Rossum60456fd1997-04-09 17:36:32 +00001691 if (pid != Py_None) {
1692 if (!self->bin) {
1693 if (!PyString_Check(pid)) {
Tim Peters84e87f32001-03-17 04:50:51 +00001694 PyErr_SetString(PicklingError,
Guido van Rossum60456fd1997-04-09 17:36:32 +00001695 "persistent id must be string");
1696 goto finally;
1697 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001698
Guido van Rossum60456fd1997-04-09 17:36:32 +00001699 if ((*self->write_func)(self, &persid, 1) < 0)
1700 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001701
Guido van Rossum60456fd1997-04-09 17:36:32 +00001702 if ((size = PyString_Size(pid)) < 0)
1703 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001704
Tim Peters84e87f32001-03-17 04:50:51 +00001705 if ((*self->write_func)(self,
Guido van Rossum60456fd1997-04-09 17:36:32 +00001706 PyString_AS_STRING((PyStringObject *)pid), size) < 0)
1707 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001708
Guido van Rossum60456fd1997-04-09 17:36:32 +00001709 if ((*self->write_func)(self, "\n", 1) < 0)
1710 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001711
Guido van Rossum60456fd1997-04-09 17:36:32 +00001712 res = 1;
1713 goto finally;
1714 }
1715 else if (save(self, pid, 1) >= 0) {
1716 if ((*self->write_func)(self, &binpersid, 1) < 0)
1717 res = -1;
1718 else
1719 res = 1;
1720 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001721
Tim Peters84e87f32001-03-17 04:50:51 +00001722 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001723 }
1724
Guido van Rossum60456fd1997-04-09 17:36:32 +00001725 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001726
Guido van Rossum60456fd1997-04-09 17:36:32 +00001727finally:
1728 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001729
Guido van Rossum60456fd1997-04-09 17:36:32 +00001730 return res;
1731}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001732
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001733
Tim Peters84e87f32001-03-17 04:50:51 +00001734static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00001735save_reduce(Picklerobject *self, PyObject *callable,
1736 PyObject *tup, PyObject *state, PyObject *ob) {
1737 static char reduce = REDUCE, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001738
Guido van Rossum60456fd1997-04-09 17:36:32 +00001739 if (save(self, callable, 0) < 0)
1740 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001741
Guido van Rossum60456fd1997-04-09 17:36:32 +00001742 if (save(self, tup, 0) < 0)
1743 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001744
Guido van Rossum60456fd1997-04-09 17:36:32 +00001745 if ((*self->write_func)(self, &reduce, 1) < 0)
1746 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001747
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001748 if (ob != NULL) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001749 if (state && !PyDict_Check(state)) {
1750 if (put2(self, ob) < 0)
1751 return -1;
1752 }
1753 else {
1754 if (put(self, ob) < 0)
1755 return -1;
1756 }
1757 }
Tim Peters84e87f32001-03-17 04:50:51 +00001758
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001759 if (state) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001760 if (save(self, state, 0) < 0)
1761 return -1;
1762
1763 if ((*self->write_func)(self, &build, 1) < 0)
1764 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001765 }
1766
Guido van Rossum60456fd1997-04-09 17:36:32 +00001767 return 0;
1768}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001769
Guido van Rossum60456fd1997-04-09 17:36:32 +00001770static int
1771save(Picklerobject *self, PyObject *args, int pers_save) {
1772 PyTypeObject *type;
1773 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0, *arg_tup = 0,
Guido van Rossum142eeb81997-08-13 03:14:41 +00001774 *callable = 0, *state = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001775 int res = -1, tmp, size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001776
Guido van Rossum60456fd1997-04-09 17:36:32 +00001777 if (!pers_save && self->pers_func) {
1778 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
1779 res = tmp;
1780 goto finally;
1781 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001782 }
1783
Guido van Rossum60456fd1997-04-09 17:36:32 +00001784 if (args == Py_None) {
1785 res = save_none(self, args);
1786 goto finally;
1787 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001788
Guido van Rossum60456fd1997-04-09 17:36:32 +00001789 type = args->ob_type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001790
Guido van Rossum60456fd1997-04-09 17:36:32 +00001791 switch (type->tp_name[0]) {
1792 case 'i':
1793 if (type == &PyInt_Type) {
1794 res = save_int(self, args);
1795 goto finally;
1796 }
1797 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001798
Guido van Rossum60456fd1997-04-09 17:36:32 +00001799 case 'l':
1800 if (type == &PyLong_Type) {
1801 res = save_long(self, args);
1802 goto finally;
1803 }
1804 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001805
Guido van Rossum60456fd1997-04-09 17:36:32 +00001806 case 'f':
1807 if (type == &PyFloat_Type) {
1808 res = save_float(self, args);
1809 goto finally;
1810 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001811 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001812
Guido van Rossum60456fd1997-04-09 17:36:32 +00001813 case 't':
1814 if (type == &PyTuple_Type && PyTuple_Size(args)==0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001815 if (self->bin) res = save_empty_tuple(self, args);
1816 else res = save_tuple(self, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001817 goto finally;
1818 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001819 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001820
Guido van Rossum60456fd1997-04-09 17:36:32 +00001821 case 's':
Guido van Rossum053b8df1998-11-25 16:18:00 +00001822 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001823 res = save_string(self, args, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001824 goto finally;
1825 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001826
1827 case 'u':
1828 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
1829 res = save_unicode(self, args, 0);
1830 goto finally;
1831 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001832 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001833
Guido van Rossum60456fd1997-04-09 17:36:32 +00001834 if (args->ob_refcnt > 1) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001835 int has_key;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001836
Guido van Rossum534b7c52000-06-28 22:23:56 +00001837 UNLESS (py_ob_id = PyLong_FromVoidPtr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001838 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001839
Guido van Rossum60456fd1997-04-09 17:36:32 +00001840 if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_ob_id)) < 0)
1841 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001842
Guido van Rossum60456fd1997-04-09 17:36:32 +00001843 if (has_key) {
1844 if (get(self, py_ob_id) < 0)
1845 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001846
Guido van Rossum60456fd1997-04-09 17:36:32 +00001847 res = 0;
1848 goto finally;
1849 }
1850 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001851
Guido van Rossum60456fd1997-04-09 17:36:32 +00001852 switch (type->tp_name[0]) {
1853 case 's':
1854 if (type == &PyString_Type) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001855 res = save_string(self, args, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001856 goto finally;
1857 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001858 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001859
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001860 case 'u':
1861 if (type == &PyUnicode_Type) {
1862 res = save_unicode(self, args, 1);
1863 goto finally;
1864 }
1865 break;
1866
Guido van Rossum60456fd1997-04-09 17:36:32 +00001867 case 't':
1868 if (type == &PyTuple_Type) {
1869 res = save_tuple(self, args);
1870 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001871 }
1872 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001873
Guido van Rossum60456fd1997-04-09 17:36:32 +00001874 case 'l':
1875 if (type == &PyList_Type) {
1876 res = save_list(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 'd':
1882 if (type == &PyDict_Type) {
1883 res = save_dict(self, args);
Tim Peters84e87f32001-03-17 04:50:51 +00001884 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001885 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001886 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001887
1888 case 'i':
1889 if (type == &PyInstance_Type) {
1890 res = save_inst(self, args);
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 'c':
1896 if (type == &PyClass_Type) {
1897 res = save_global(self, args, NULL);
1898 goto finally;
1899 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001900 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001901
1902 case 'f':
1903 if (type == &PyFunction_Type) {
1904 res = save_global(self, args, NULL);
1905 goto finally;
1906 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001907 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001908
1909 case 'b':
1910 if (type == &PyCFunction_Type) {
1911 res = save_global(self, args, NULL);
1912 goto finally;
1913 }
1914 }
1915
1916 if (!pers_save && self->inst_pers_func) {
1917 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
1918 res = tmp;
1919 goto finally;
1920 }
1921 }
1922
Guido van Rossum142eeb81997-08-13 03:14:41 +00001923 if ((__reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001924 Py_INCREF(__reduce__);
1925
Guido van Rossum60456fd1997-04-09 17:36:32 +00001926 Py_INCREF(args);
Guido van Rossum053b8df1998-11-25 16:18:00 +00001927 ARG_TUP(self, args);
1928 if (self->arg) {
1929 t = PyObject_CallObject(__reduce__, self->arg);
1930 FREE_ARG_TUP(self);
1931 }
1932 if (! t) goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001933 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001934 else {
1935 PyErr_Clear();
1936
Guido van Rossum142eeb81997-08-13 03:14:41 +00001937 if ((__reduce__ = PyObject_GetAttr(args, __reduce___str))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001938 UNLESS (t = PyObject_CallObject(__reduce__, empty_tuple))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001939 goto finally;
1940 }
1941 else {
1942 PyErr_Clear();
1943 }
1944 }
1945
1946 if (t) {
1947 if (PyString_Check(t)) {
1948 res = save_global(self, args, t);
1949 goto finally;
1950 }
Tim Peters84e87f32001-03-17 04:50:51 +00001951
Guido van Rossum60456fd1997-04-09 17:36:32 +00001952 if (!PyTuple_Check(t)) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001953 cPickle_ErrFormat(PicklingError, "Value returned by %s must "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001954 "be a tuple", "O", __reduce__);
1955 goto finally;
1956 }
1957
1958 size = PyTuple_Size(t);
Tim Peters84e87f32001-03-17 04:50:51 +00001959
Guido van Rossum60456fd1997-04-09 17:36:32 +00001960 if ((size != 3) && (size != 2)) {
Tim Peters84e87f32001-03-17 04:50:51 +00001961 cPickle_ErrFormat(PicklingError, "tuple returned by %s must "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001962 "contain only two or three elements", "O", __reduce__);
1963 goto finally;
1964 }
Tim Peters84e87f32001-03-17 04:50:51 +00001965
Guido van Rossum60456fd1997-04-09 17:36:32 +00001966 callable = PyTuple_GET_ITEM(t, 0);
Guido van Rossum142eeb81997-08-13 03:14:41 +00001967
Guido van Rossum60456fd1997-04-09 17:36:32 +00001968 arg_tup = PyTuple_GET_ITEM(t, 1);
1969
1970 if (size > 2) {
1971 state = PyTuple_GET_ITEM(t, 2);
1972 }
1973
Guido van Rossum053b8df1998-11-25 16:18:00 +00001974 UNLESS (PyTuple_Check(arg_tup) || arg_tup==Py_None) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001975 cPickle_ErrFormat(PicklingError, "Second element of tuple "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001976 "returned by %s must be a tuple", "O", __reduce__);
1977 goto finally;
1978 }
1979
1980 res = save_reduce(self, callable, arg_tup, state, args);
1981 goto finally;
1982 }
1983
Guido van Rossumc03158b1999-06-09 15:23:31 +00001984 PyErr_SetObject(UnpickleableError, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001985
1986finally:
1987 Py_XDECREF(py_ob_id);
1988 Py_XDECREF(__reduce__);
1989 Py_XDECREF(t);
Tim Peters84e87f32001-03-17 04:50:51 +00001990
Guido van Rossum60456fd1997-04-09 17:36:32 +00001991 return res;
1992}
1993
1994
1995static int
1996dump(Picklerobject *self, PyObject *args) {
1997 static char stop = STOP;
1998
1999 if (save(self, args, 0) < 0)
2000 return -1;
2001
2002 if ((*self->write_func)(self, &stop, 1) < 0)
2003 return -1;
2004
2005 if ((*self->write_func)(self, NULL, 0) < 0)
2006 return -1;
2007
2008 return 0;
2009}
2010
2011static PyObject *
Guido van Rossum053b8df1998-11-25 16:18:00 +00002012Pickle_clear_memo(Picklerobject *self, PyObject *args) {
Guido van Rossum43713e52000-02-29 13:59:29 +00002013 if (args && ! PyArg_ParseTuple(args,":clear_memo")) return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002014 if (self->memo) PyDict_Clear(self->memo);
2015 Py_INCREF(Py_None);
2016 return Py_None;
2017}
2018
2019static PyObject *
2020Pickle_getvalue(Picklerobject *self, PyObject *args) {
2021 int l, i, rsize, ssize, clear=1, lm;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00002022 long ik;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002023 PyObject *k, *r;
2024 char *s, *p, *have_get;
2025 Pdata *data;
2026
Guido van Rossum43713e52000-02-29 13:59:29 +00002027 if (args && ! PyArg_ParseTuple(args,"|i:getvalue",&clear)) return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002028
2029 /* Check to make sure we are based on a list */
2030 if (! Pdata_Check(self->file)) {
2031 PyErr_SetString(PicklingError,
2032 "Attempt to getvalue a non-list-based pickler");
2033 return NULL;
2034 }
2035
2036 /* flush write buffer */
2037 if (write_other(self, NULL, 0) < 0) return NULL;
2038
2039 data=(Pdata*)self->file;
2040 l=data->length;
2041
2042 /* set up an array to hold get/put status */
2043 if ((lm=PyDict_Size(self->memo)) < 0) return NULL;
2044 lm++;
Tim Peters84e87f32001-03-17 04:50:51 +00002045 if (! (have_get=malloc((lm)*sizeof(char)))) return PyErr_NoMemory();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002046 memset(have_get,0,lm);
2047
2048 /* Scan for gets. */
2049 for (rsize=0, i=l; --i >= 0; ) {
2050 k=data->data[i];
Tim Peters84e87f32001-03-17 04:50:51 +00002051
Guido van Rossum053b8df1998-11-25 16:18:00 +00002052 if (PyString_Check(k)) {
2053 rsize += PyString_GET_SIZE(k);
2054 }
2055
2056 else if (PyInt_Check(k)) { /* put */
2057 ik=PyInt_AS_LONG((PyIntObject*)k);
2058 if (ik >= lm || ik==0) {
2059 PyErr_SetString(PicklingError,
2060 "Invalid get data");
2061 return NULL;
Tim Peters84e87f32001-03-17 04:50:51 +00002062 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002063 if (have_get[ik]) { /* with matching get */
2064 if (ik < 256) rsize += 2;
2065 else rsize+=5;
2066 }
2067 }
2068
2069 else if (! (PyTuple_Check(k) &&
2070 PyTuple_GET_SIZE(k) == 2 &&
2071 PyInt_Check((k=PyTuple_GET_ITEM(k,0))))
2072 ) {
2073 PyErr_SetString(PicklingError,
2074 "Unexpected data in internal list");
2075 return NULL;
2076 }
2077
2078 else { /* put */
2079 ik=PyInt_AS_LONG((PyIntObject*)k);
2080 if (ik >= lm || ik==0) {
2081 PyErr_SetString(PicklingError,
2082 "Invalid get data");
2083 return NULL;
Tim Peters84e87f32001-03-17 04:50:51 +00002084 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002085 have_get[ik]=1;
2086 if (ik < 256) rsize += 2;
2087 else rsize+=5;
2088 }
2089
2090 }
2091
2092 /* Now generate the result */
2093 UNLESS (r=PyString_FromStringAndSize(NULL,rsize)) goto err;
2094 s=PyString_AS_STRING((PyStringObject*)r);
2095
2096 for (i=0; i<l; i++) {
2097 k=data->data[i];
2098
2099 if (PyString_Check(k)) {
2100 ssize=PyString_GET_SIZE(k);
2101 if (ssize) {
2102 p=PyString_AS_STRING((PyStringObject*)k);
2103 while (--ssize >= 0) *s++=*p++;
2104 }
2105 }
2106
2107 else if (PyTuple_Check(k)) { /* get */
2108 ik=PyInt_AS_LONG((PyIntObject*)PyTuple_GET_ITEM(k,0));
2109 if (ik < 256) {
2110 *s++ = BINGET;
2111 *s++ = (int)(ik & 0xff);
2112 }
2113 else {
2114 *s++ = LONG_BINGET;
2115 *s++ = (int)(ik & 0xff);
2116 *s++ = (int)((ik >> 8) & 0xff);
2117 *s++ = (int)((ik >> 16) & 0xff);
2118 *s++ = (int)((ik >> 24) & 0xff);
2119 }
2120 }
2121
2122 else { /* put */
2123 ik=PyInt_AS_LONG((PyIntObject*)k);
2124
2125 if (have_get[ik]) { /* with matching get */
2126 if (ik < 256) {
2127 *s++ = BINPUT;
2128 *s++ = (int)(ik & 0xff);
2129 }
2130 else {
2131 *s++ = LONG_BINPUT;
2132 *s++ = (int)(ik & 0xff);
2133 *s++ = (int)((ik >> 8) & 0xff);
2134 *s++ = (int)((ik >> 16) & 0xff);
2135 *s++ = (int)((ik >> 24) & 0xff);
2136 }
2137 }
2138 }
2139
2140 }
2141
2142 if (clear) {
2143 PyDict_Clear(self->memo);
2144 Pdata_clear(data,0);
2145 }
Tim Peters84e87f32001-03-17 04:50:51 +00002146
Guido van Rossum053b8df1998-11-25 16:18:00 +00002147 free(have_get);
2148 return r;
2149err:
2150 free(have_get);
2151 return NULL;
2152}
2153
2154static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002155Pickler_dump(Picklerobject *self, PyObject *args) {
2156 PyObject *ob;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002157 int get=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002158
Guido van Rossum43713e52000-02-29 13:59:29 +00002159 UNLESS (PyArg_ParseTuple(args, "O|i:dump", &ob, &get))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002160 return NULL;
2161
2162 if (dump(self, ob) < 0)
2163 return NULL;
2164
Guido van Rossum053b8df1998-11-25 16:18:00 +00002165 if (get) return Pickle_getvalue(self, NULL);
2166
2167 Py_INCREF(self);
2168 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002169}
2170
2171
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002172static struct PyMethodDef Pickler_methods[] = {
Guido van Rossum142eeb81997-08-13 03:14:41 +00002173 {"dump", (PyCFunction)Pickler_dump, 1,
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002174 "dump(object) --"
2175 "Write an object in pickle format to the object's pickle stream\n"
2176 },
Guido van Rossum142eeb81997-08-13 03:14:41 +00002177 {"clear_memo", (PyCFunction)Pickle_clear_memo, 1,
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002178 "clear_memo() -- Clear the picklers memo"},
Guido van Rossum053b8df1998-11-25 16:18:00 +00002179 {"getvalue", (PyCFunction)Pickle_getvalue, 1,
2180 "getvalue() -- Finish picking a list-based pickle"},
Guido van Rossum60456fd1997-04-09 17:36:32 +00002181 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002182};
2183
2184
2185static Picklerobject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002186newPicklerobject(PyObject *file, int bin) {
2187 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002188
Guido van Rossumb18618d2000-05-03 23:44:39 +00002189 UNLESS (self = PyObject_New(Picklerobject, &Picklertype))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002190 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002191
2192 self->fp = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002193 self->write = NULL;
2194 self->memo = NULL;
2195 self->arg = NULL;
2196 self->pers_func = NULL;
2197 self->inst_pers_func = NULL;
2198 self->write_buf = NULL;
2199 self->bin = bin;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002200 self->fast = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002201 self->buf_size = 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002202 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002203
Guido van Rossum053b8df1998-11-25 16:18:00 +00002204 if (file)
2205 Py_INCREF(file);
2206 else
Guido van Rossum50f385c1998-12-04 18:48:44 +00002207 file=Pdata_New();
Tim Peters84e87f32001-03-17 04:50:51 +00002208
2209 UNLESS (self->file = file)
Guido van Rossum83addc72000-04-21 20:49:36 +00002210 goto err;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002211
Tim Peters84e87f32001-03-17 04:50:51 +00002212 UNLESS (self->memo = PyDict_New())
Guido van Rossum83addc72000-04-21 20:49:36 +00002213 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002214
Guido van Rossum60456fd1997-04-09 17:36:32 +00002215 if (PyFile_Check(file)) {
2216 self->fp = PyFile_AsFile(file);
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002217 if (self->fp == NULL) {
Guido van Rossum83addc72000-04-21 20:49:36 +00002218 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
2219 goto err;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002220 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002221 self->write_func = write_file;
2222 }
2223 else if (PycStringIO_OutputCheck(file)) {
2224 self->write_func = write_cStringIO;
2225 }
Guido van Rossum142eeb81997-08-13 03:14:41 +00002226 else if (file == Py_None) {
2227 self->write_func = write_none;
2228 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002229 else {
2230 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002231
Guido van Rossum053b8df1998-11-25 16:18:00 +00002232 if (! Pdata_Check(file)) {
2233 UNLESS (self->write = PyObject_GetAttr(file, write_str)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002234 PyErr_Clear();
2235 PyErr_SetString(PyExc_TypeError, "argument must have 'write' "
Guido van Rossum053b8df1998-11-25 16:18:00 +00002236 "attribute");
2237 goto err;
2238 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002239 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002240
Tim Peters84e87f32001-03-17 04:50:51 +00002241 UNLESS (self->write_buf =
2242 (char *)malloc(WRITE_BUF_SIZE * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002243 PyErr_NoMemory();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002244 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002245 }
2246 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002247
Guido van Rossum053b8df1998-11-25 16:18:00 +00002248 if (PyEval_GetRestricted()) {
2249 /* Restricted execution, get private tables */
2250 PyObject *m;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002251
Guido van Rossum053b8df1998-11-25 16:18:00 +00002252 UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
2253 self->dispatch_table=PyObject_GetAttr(m, dispatch_table_str);
2254 Py_DECREF(m);
2255 UNLESS (self->dispatch_table) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002256 }
2257 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002258 self->dispatch_table=dispatch_table;
2259 Py_INCREF(dispatch_table);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002260 }
2261
Guido van Rossum60456fd1997-04-09 17:36:32 +00002262 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002263
2264err:
2265 Py_DECREF((PyObject *)self);
2266 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002267}
2268
2269
2270static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002271get_Pickler(PyObject *self, PyObject *args) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002272 PyObject *file=NULL;
2273 int bin;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002274
Guido van Rossum053b8df1998-11-25 16:18:00 +00002275 bin=1;
Guido van Rossum43713e52000-02-29 13:59:29 +00002276 if (! PyArg_ParseTuple(args, "|i:Pickler", &bin)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002277 PyErr_Clear();
2278 bin=0;
Guido van Rossum43713e52000-02-29 13:59:29 +00002279 if (! PyArg_ParseTuple(args, "O|i:Pickler", &file, &bin))
Guido van Rossum053b8df1998-11-25 16:18:00 +00002280 return NULL;
2281 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002282 return (PyObject *)newPicklerobject(file, bin);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002283}
2284
2285
2286static void
Guido van Rossum60456fd1997-04-09 17:36:32 +00002287Pickler_dealloc(Picklerobject *self) {
2288 Py_XDECREF(self->write);
2289 Py_XDECREF(self->memo);
2290 Py_XDECREF(self->arg);
2291 Py_XDECREF(self->file);
2292 Py_XDECREF(self->pers_func);
2293 Py_XDECREF(self->inst_pers_func);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002294 Py_XDECREF(self->dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002295
Tim Peters84e87f32001-03-17 04:50:51 +00002296 if (self->write_buf) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002297 free(self->write_buf);
2298 }
2299
Guido van Rossumb18618d2000-05-03 23:44:39 +00002300 PyObject_Del(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002301}
2302
2303
2304static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002305Pickler_getattr(Picklerobject *self, char *name) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002306
2307 switch (*name) {
2308 case 'p':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002309 if (strcmp(name, "persistent_id") == 0) {
2310 if (!self->pers_func) {
2311 PyErr_SetString(PyExc_AttributeError, name);
2312 return NULL;
2313 }
2314
2315 Py_INCREF(self->pers_func);
2316 return self->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002317 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002318 break;
2319 case 'm':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002320 if (strcmp(name, "memo") == 0) {
2321 if (!self->memo) {
2322 PyErr_SetString(PyExc_AttributeError, name);
2323 return NULL;
2324 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002325
Guido van Rossum60456fd1997-04-09 17:36:32 +00002326 Py_INCREF(self->memo);
2327 return self->memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002328 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002329 break;
2330 case 'P':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002331 if (strcmp(name, "PicklingError") == 0) {
2332 Py_INCREF(PicklingError);
2333 return PicklingError;
2334 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002335 break;
2336 case 'b':
2337 if (strcmp(name, "binary")==0)
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002338 return PyInt_FromLong(self->bin);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002339 break;
2340 case 'f':
2341 if (strcmp(name, "fast")==0)
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002342 return PyInt_FromLong(self->fast);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002343 break;
2344 case 'g':
2345 if (strcmp(name, "getvalue")==0 && ! Pdata_Check(self->file)) {
2346 PyErr_SetString(PyExc_AttributeError, name);
2347 return NULL;
2348 }
2349 break;
2350 }
2351 return Py_FindMethod(Pickler_methods, (PyObject *)self, name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002352}
2353
2354
Tim Peters84e87f32001-03-17 04:50:51 +00002355int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002356Pickler_setattr(Picklerobject *self, char *name, PyObject *value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002357
Guido van Rossum053b8df1998-11-25 16:18:00 +00002358 if (! value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002359 PyErr_SetString(PyExc_TypeError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00002360 "attribute deletion is not supported");
2361 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002362 }
Tim Peters84e87f32001-03-17 04:50:51 +00002363
Guido van Rossum60456fd1997-04-09 17:36:32 +00002364 if (strcmp(name, "persistent_id") == 0) {
2365 Py_XDECREF(self->pers_func);
2366 self->pers_func = value;
2367 Py_INCREF(value);
2368 return 0;
2369 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002370
Guido van Rossum60456fd1997-04-09 17:36:32 +00002371 if (strcmp(name, "inst_persistent_id") == 0) {
2372 Py_XDECREF(self->inst_pers_func);
2373 self->inst_pers_func = value;
2374 Py_INCREF(value);
2375 return 0;
2376 }
2377
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002378 if (strcmp(name, "memo") == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002379 if (! PyDict_Check(value)) {
2380 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
2381 return -1;
2382 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002383 Py_XDECREF(self->memo);
2384 self->memo = value;
2385 Py_INCREF(value);
2386 return 0;
2387 }
2388
Guido van Rossum053b8df1998-11-25 16:18:00 +00002389 if (strcmp(name, "binary")==0) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002390 self->bin=PyObject_IsTrue(value);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002391 return 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002392 }
2393
Guido van Rossum053b8df1998-11-25 16:18:00 +00002394 if (strcmp(name, "fast")==0) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002395 self->fast=PyObject_IsTrue(value);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002396 return 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002397 }
2398
Guido van Rossum60456fd1997-04-09 17:36:32 +00002399 PyErr_SetString(PyExc_AttributeError, name);
2400 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002401}
2402
2403
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002404static char Picklertype__doc__[] =
2405"Objects that know how to pickle objects\n"
2406;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002407
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002408static PyTypeObject Picklertype = {
2409 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00002410 0, /*ob_size*/
2411 "Pickler", /*tp_name*/
2412 sizeof(Picklerobject), /*tp_basicsize*/
2413 0, /*tp_itemsize*/
2414 /* methods */
2415 (destructor)Pickler_dealloc, /*tp_dealloc*/
2416 (printfunc)0, /*tp_print*/
2417 (getattrfunc)Pickler_getattr, /*tp_getattr*/
2418 (setattrfunc)Pickler_setattr, /*tp_setattr*/
2419 (cmpfunc)0, /*tp_compare*/
2420 (reprfunc)0, /*tp_repr*/
2421 0, /*tp_as_number*/
2422 0, /*tp_as_sequence*/
2423 0, /*tp_as_mapping*/
2424 (hashfunc)0, /*tp_hash*/
2425 (ternaryfunc)0, /*tp_call*/
2426 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002427
Guido van Rossum60456fd1997-04-09 17:36:32 +00002428 /* Space for future expansion */
2429 0L,0L,0L,0L,
2430 Picklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002431};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002432
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002433static PyObject *
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002434find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc) {
Guido van Rossume2d81cd1998-08-08 19:40:10 +00002435 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002436
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002437 if (fc) {
2438 if (fc==Py_None) {
Tim Peters84e87f32001-03-17 04:50:51 +00002439 PyErr_SetString(UnpicklingError,
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002440 "Global and instance pickles are not supported.");
2441 return NULL;
2442 }
2443 return PyObject_CallFunction(fc, "OO", py_module_name, py_global_name);
2444 }
2445
Jeremy Hyltond1055231998-08-11 19:52:51 +00002446 module = PySys_GetObject("modules");
2447 if (module == NULL)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002448 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00002449
2450 module = PyDict_GetItem(module, py_module_name);
2451 if (module == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002452 module = PyImport_Import(py_module_name);
2453 if (!module)
2454 return NULL;
2455 global = PyObject_GetAttr(module, py_global_name);
2456 Py_DECREF(module);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002457 }
Jeremy Hyltond1055231998-08-11 19:52:51 +00002458 else
Guido van Rossum053b8df1998-11-25 16:18:00 +00002459 global = PyObject_GetAttr(module, py_global_name);
Jeremy Hyltond1055231998-08-11 19:52:51 +00002460 if (global == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002461 char buf[256 + 37];
2462 sprintf(buf, "Failed to import class %.128s from module %.128s",
2463 PyString_AS_STRING((PyStringObject*)py_global_name),
Tim Peters84e87f32001-03-17 04:50:51 +00002464 PyString_AS_STRING((PyStringObject*)py_module_name));
Guido van Rossum053b8df1998-11-25 16:18:00 +00002465 PyErr_SetString(PyExc_SystemError, buf);
2466 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00002467 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002468 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002469}
2470
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002471static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002472marker(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002473 if (self->num_marks < 1) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002474 PyErr_SetString(UnpicklingError, "could not find MARK");
2475 return -1;
2476 }
2477
2478 return self->marks[--self->num_marks];
2479}
2480
Tim Peters84e87f32001-03-17 04:50:51 +00002481
Guido van Rossum60456fd1997-04-09 17:36:32 +00002482static int
2483load_none(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002484 PDATA_APPEND(self->stack, Py_None, -1);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002485 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002486}
2487
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002488static int
Thomas Wouters58d05102000-07-24 14:43:35 +00002489bad_readline(void) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002490 PyErr_SetString(UnpicklingError, "pickle data was truncated");
2491 return -1;
2492}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002493
2494static int
2495load_int(Unpicklerobject *self) {
2496 PyObject *py_int = 0;
2497 char *endptr, *s;
2498 int len, res = -1;
2499 long l;
2500
2501 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002502 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002503 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002504
2505 errno = 0;
2506 l = strtol(s, &endptr, 0);
2507
2508 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
2509 /* Hm, maybe we've got something long. Let's try reading
Guido van Rossum053b8df1998-11-25 16:18:00 +00002510 it as a Python long object. */
Guido van Rossum60456fd1997-04-09 17:36:32 +00002511 errno=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002512 UNLESS (py_int=PyLong_FromString(s,&endptr,0)) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002513
Guido van Rossum053b8df1998-11-25 16:18:00 +00002514 if ((*endptr != '\n') || (endptr[1] != '\0')) {
2515 PyErr_SetString(PyExc_ValueError,
2516 "could not convert string to int");
2517 goto finally;
2518 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002519 }
2520 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002521 UNLESS (py_int = PyInt_FromLong(l)) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002522 }
2523
Guido van Rossum053b8df1998-11-25 16:18:00 +00002524 free(s);
2525 PDATA_PUSH(self->stack, py_int, -1);
2526 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002527
2528finally:
2529 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002530
2531 return res;
2532}
2533
2534
Tim Peters84e87f32001-03-17 04:50:51 +00002535static long
Guido van Rossum60456fd1997-04-09 17:36:32 +00002536calc_binint(char *s, int x) {
2537 unsigned char c;
2538 int i;
2539 long l;
2540
2541 for (i = 0, l = 0L; i < x; i++) {
2542 c = (unsigned char)s[i];
2543 l |= (long)c << (i * 8);
2544 }
Tim Petersbfa18f72001-04-10 01:54:42 +00002545#if SIZEOF_LONG > 4
2546 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
2547 * is signed, so on a box with longs bigger than 4 bytes we need
2548 * to extend a BININT's sign bit to the full width.
2549 */
2550 if (x == 4 && l & (1L << 31))
2551 l |= (~0L) << 32;
2552#endif
Guido van Rossum60456fd1997-04-09 17:36:32 +00002553 return l;
2554}
2555
2556
2557static int
2558load_binintx(Unpicklerobject *self, char *s, int x) {
2559 PyObject *py_int = 0;
2560 long l;
2561
2562 l = calc_binint(s, x);
2563
Guido van Rossum053b8df1998-11-25 16:18:00 +00002564 UNLESS (py_int = PyInt_FromLong(l))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002565 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002566
Guido van Rossum053b8df1998-11-25 16:18:00 +00002567 PDATA_PUSH(self->stack, py_int, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002568 return 0;
2569}
2570
2571
2572static int
2573load_binint(Unpicklerobject *self) {
2574 char *s;
2575
2576 if ((*self->read_func)(self, &s, 4) < 0)
2577 return -1;
2578
2579 return load_binintx(self, s, 4);
2580}
2581
2582
2583static int
2584load_binint1(Unpicklerobject *self) {
2585 char *s;
2586
2587 if ((*self->read_func)(self, &s, 1) < 0)
2588 return -1;
2589
2590 return load_binintx(self, s, 1);
2591}
2592
2593
2594static int
2595load_binint2(Unpicklerobject *self) {
2596 char *s;
2597
2598 if ((*self->read_func)(self, &s, 2) < 0)
2599 return -1;
2600
2601 return load_binintx(self, s, 2);
2602}
Tim Peters84e87f32001-03-17 04:50:51 +00002603
Guido van Rossum60456fd1997-04-09 17:36:32 +00002604static int
2605load_long(Unpicklerobject *self) {
2606 PyObject *l = 0;
2607 char *end, *s;
2608 int len, res = -1;
2609
Guido van Rossum60456fd1997-04-09 17:36:32 +00002610 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002611 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002612 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002613
Guido van Rossum053b8df1998-11-25 16:18:00 +00002614 UNLESS (l = PyLong_FromString(s, &end, 0))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002615 goto finally;
2616
Guido van Rossum053b8df1998-11-25 16:18:00 +00002617 free(s);
2618 PDATA_PUSH(self->stack, l, -1);
2619 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002620
2621finally:
2622 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002623
2624 return res;
2625}
2626
Tim Peters84e87f32001-03-17 04:50:51 +00002627
Guido van Rossum60456fd1997-04-09 17:36:32 +00002628static int
2629load_float(Unpicklerobject *self) {
2630 PyObject *py_float = 0;
2631 char *endptr, *s;
2632 int len, res = -1;
2633 double d;
2634
2635 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002636 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002637 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002638
2639 errno = 0;
2640 d = strtod(s, &endptr);
2641
2642 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
Tim Peters84e87f32001-03-17 04:50:51 +00002643 PyErr_SetString(PyExc_ValueError,
Guido van Rossum60456fd1997-04-09 17:36:32 +00002644 "could not convert string to float");
2645 goto finally;
2646 }
2647
Guido van Rossum053b8df1998-11-25 16:18:00 +00002648 UNLESS (py_float = PyFloat_FromDouble(d))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002649 goto finally;
2650
Guido van Rossum053b8df1998-11-25 16:18:00 +00002651 free(s);
2652 PDATA_PUSH(self->stack, py_float, -1);
2653 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002654
2655finally:
2656 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002657
2658 return res;
2659}
2660
Guido van Rossum60456fd1997-04-09 17:36:32 +00002661static int
2662load_binfloat(Unpicklerobject *self) {
2663 PyObject *py_float = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002664 int s, e;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002665 long fhi, flo;
2666 double x;
2667 char *p;
2668
2669 if ((*self->read_func)(self, &p, 8) < 0)
2670 return -1;
2671
2672 /* First byte */
2673 s = (*p>>7) & 1;
2674 e = (*p & 0x7F) << 4;
2675 p++;
2676
2677 /* Second byte */
2678 e |= (*p>>4) & 0xF;
2679 fhi = (*p & 0xF) << 24;
2680 p++;
2681
2682 /* Third byte */
2683 fhi |= (*p & 0xFF) << 16;
2684 p++;
2685
2686 /* Fourth byte */
2687 fhi |= (*p & 0xFF) << 8;
2688 p++;
2689
2690 /* Fifth byte */
2691 fhi |= *p & 0xFF;
2692 p++;
2693
2694 /* Sixth byte */
2695 flo = (*p & 0xFF) << 16;
2696 p++;
2697
2698 /* Seventh byte */
2699 flo |= (*p & 0xFF) << 8;
2700 p++;
2701
2702 /* Eighth byte */
2703 flo |= *p & 0xFF;
2704
2705 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
2706 x /= 268435456.0; /* 2**28 */
2707
2708 /* XXX This sadly ignores Inf/NaN */
2709 if (e == 0)
2710 e = -1022;
2711 else {
2712 x += 1.0;
2713 e -= 1023;
2714 }
2715 x = ldexp(x, e);
2716
2717 if (s)
2718 x = -x;
2719
Guido van Rossum053b8df1998-11-25 16:18:00 +00002720 UNLESS (py_float = PyFloat_FromDouble(x)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002721
Guido van Rossum053b8df1998-11-25 16:18:00 +00002722 PDATA_PUSH(self->stack, py_float, -1);
2723 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002724}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002725
2726static int
2727load_string(Unpicklerobject *self) {
2728 PyObject *str = 0;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002729 int len, res = -1, nslash;
2730 char *s, q, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002731
2732 static PyObject *eval_dict = 0;
2733
2734 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002735 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002736 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002737
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002738 /* Check for unquoted quotes (evil strings) */
2739 q=*s;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002740 if (q != '"' && q != '\'') goto insecure;
2741 for (p=s+1, nslash=0; *p; p++) {
2742 if (*p==q && nslash%2==0) break;
2743 if (*p=='\\') nslash++;
2744 else nslash=0;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002745 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002746 if (*p==q)
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002747 {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002748 for (p++; *p; p++) if (*p > ' ') goto insecure;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002749 }
2750 else goto insecure;
2751 /********************************************/
2752
Guido van Rossum053b8df1998-11-25 16:18:00 +00002753 UNLESS (eval_dict)
2754 UNLESS (eval_dict = Py_BuildValue("{s{}}", "__builtins__"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002755 goto finally;
2756
Guido van Rossum053b8df1998-11-25 16:18:00 +00002757 UNLESS (str = PyRun_String(s, Py_eval_input, eval_dict, eval_dict))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002758 goto finally;
2759
Guido van Rossum053b8df1998-11-25 16:18:00 +00002760 free(s);
2761 PDATA_PUSH(self->stack, str, -1);
2762 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002763
2764finally:
2765 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002766
2767 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00002768
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002769insecure:
2770 free(s);
2771 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
2772 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00002773}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002774
2775
2776static int
2777load_binstring(Unpicklerobject *self) {
2778 PyObject *py_string = 0;
2779 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002780 char *s;
2781
Guido van Rossum053b8df1998-11-25 16:18:00 +00002782 if ((*self->read_func)(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002783
2784 l = calc_binint(s, 4);
2785
2786 if ((*self->read_func)(self, &s, l) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002787 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002788
Guido van Rossum053b8df1998-11-25 16:18:00 +00002789 UNLESS (py_string = PyString_FromStringAndSize(s, l))
2790 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002791
Guido van Rossum053b8df1998-11-25 16:18:00 +00002792 PDATA_PUSH(self->stack, py_string, -1);
2793 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002794}
2795
2796
2797static int
2798load_short_binstring(Unpicklerobject *self) {
2799 PyObject *py_string = 0;
Tim Peters84e87f32001-03-17 04:50:51 +00002800 unsigned char l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002801 char *s;
2802
2803 if ((*self->read_func)(self, &s, 1) < 0)
2804 return -1;
2805
2806 l = (unsigned char)s[0];
2807
Guido van Rossum053b8df1998-11-25 16:18:00 +00002808 if ((*self->read_func)(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002809
Guido van Rossum053b8df1998-11-25 16:18:00 +00002810 UNLESS (py_string = PyString_FromStringAndSize(s, l)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002811
Guido van Rossum053b8df1998-11-25 16:18:00 +00002812 PDATA_PUSH(self->stack, py_string, -1);
2813 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00002814}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002815
2816
2817static int
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002818load_unicode(Unpicklerobject *self) {
2819 PyObject *str = 0;
2820 int len, res = -1;
2821 char *s;
2822
2823 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00002824 if (len < 1) return bad_readline();
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002825
2826 UNLESS (str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL))
2827 goto finally;
2828
2829 PDATA_PUSH(self->stack, str, -1);
2830 return 0;
2831
2832finally:
2833 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00002834}
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002835
2836
2837static int
2838load_binunicode(Unpicklerobject *self) {
2839 PyObject *unicode;
2840 long l;
2841 char *s;
2842
2843 if ((*self->read_func)(self, &s, 4) < 0) return -1;
2844
2845 l = calc_binint(s, 4);
2846
2847 if ((*self->read_func)(self, &s, l) < 0)
2848 return -1;
2849
2850 UNLESS (unicode = PyUnicode_DecodeUTF8(s, l, NULL))
2851 return -1;
2852
2853 PDATA_PUSH(self->stack, unicode, -1);
2854 return 0;
2855}
2856
2857
2858static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002859load_tuple(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002860 PyObject *tup;
2861 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002862
Guido van Rossum053b8df1998-11-25 16:18:00 +00002863 if ((i = marker(self)) < 0) return -1;
2864 UNLESS (tup=Pdata_popTuple(self->stack, i)) return -1;
2865 PDATA_PUSH(self->stack, tup, -1);
2866 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002867}
2868
2869static int
2870load_empty_tuple(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002871 PyObject *tup;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002872
Guido van Rossum053b8df1998-11-25 16:18:00 +00002873 UNLESS (tup=PyTuple_New(0)) return -1;
2874 PDATA_PUSH(self->stack, tup, -1);
2875 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002876}
2877
2878static int
2879load_empty_list(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002880 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002881
Guido van Rossum053b8df1998-11-25 16:18:00 +00002882 UNLESS (list=PyList_New(0)) return -1;
2883 PDATA_PUSH(self->stack, list, -1);
2884 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002885}
2886
2887static int
2888load_empty_dict(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002889 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002890
Guido van Rossum053b8df1998-11-25 16:18:00 +00002891 UNLESS (dict=PyDict_New()) return -1;
2892 PDATA_PUSH(self->stack, dict, -1);
2893 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002894}
2895
2896
2897static int
2898load_list(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002899 PyObject *list = 0;
2900 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002901
Guido van Rossum053b8df1998-11-25 16:18:00 +00002902 if ((i = marker(self)) < 0) return -1;
2903 UNLESS (list=Pdata_popList(self->stack, i)) return -1;
2904 PDATA_PUSH(self->stack, list, -1);
2905 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002906}
2907
2908static int
2909load_dict(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002910 PyObject *dict, *key, *value;
2911 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002912
Guido van Rossum053b8df1998-11-25 16:18:00 +00002913 if ((i = marker(self)) < 0) return -1;
2914 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002915
Guido van Rossum053b8df1998-11-25 16:18:00 +00002916 UNLESS (dict = PyDict_New()) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002917
Guido van Rossum053b8df1998-11-25 16:18:00 +00002918 for (k = i+1; k < j; k += 2) {
2919 key =self->stack->data[k-1];
2920 value=self->stack->data[k ];
2921 if (PyDict_SetItem(dict, key, value) < 0) {
2922 Py_DECREF(dict);
2923 return -1;
2924 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002925 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002926 Pdata_clear(self->stack, i);
2927 PDATA_PUSH(self->stack, dict, -1);
2928 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002929}
2930
2931static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002932Instance_New(PyObject *cls, PyObject *args) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002933 int has_key;
2934 PyObject *safe=0, *r=0;
2935
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002936 if (PyClass_Check(cls)) {
2937 int l;
Tim Peters84e87f32001-03-17 04:50:51 +00002938
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002939 if ((l=PyObject_Size(args)) < 0) goto err;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002940 UNLESS (l) {
2941 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002942
Guido van Rossum053b8df1998-11-25 16:18:00 +00002943 UNLESS (__getinitargs__=PyObject_GetAttr(cls, __getinitargs___str)) {
2944 /* We have a class with no __getinitargs__, so bypass usual
2945 construction */
Fred Drake2c773552001-03-22 17:52:17 +00002946 PyObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002947
Guido van Rossum053b8df1998-11-25 16:18:00 +00002948 PyErr_Clear();
Fred Drake2c773552001-03-22 17:52:17 +00002949 UNLESS (inst=PyInstance_NewRaw(cls, NULL))
Guido van Rossum053b8df1998-11-25 16:18:00 +00002950 goto err;
Fred Drake2c773552001-03-22 17:52:17 +00002951 return inst;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002952 }
2953 Py_DECREF(__getinitargs__);
2954 }
Tim Peters84e87f32001-03-17 04:50:51 +00002955
Guido van Rossum053b8df1998-11-25 16:18:00 +00002956 if ((r=PyInstance_New(cls, args, NULL))) return r;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002957 else goto err;
2958 }
Tim Peters84e87f32001-03-17 04:50:51 +00002959
2960
Guido van Rossum60456fd1997-04-09 17:36:32 +00002961 if ((has_key = cPickle_PyMapping_HasKey(safe_constructors, cls)) < 0)
2962 goto err;
Tim Peters84e87f32001-03-17 04:50:51 +00002963
Guido van Rossum60456fd1997-04-09 17:36:32 +00002964 if (!has_key)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002965 if (!(safe = PyObject_GetAttr(cls, __safe_for_unpickling___str)) ||
Guido van Rossum60456fd1997-04-09 17:36:32 +00002966 !PyObject_IsTrue(safe)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002967 cPickle_ErrFormat(UnpicklingError,
2968 "%s is not safe for unpickling", "O", cls);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002969 Py_XDECREF(safe);
2970 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002971 }
2972
Guido van Rossum053b8df1998-11-25 16:18:00 +00002973 if (args==Py_None) {
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002974 /* Special case, call cls.__basicnew__() */
2975 PyObject *basicnew;
Tim Peters84e87f32001-03-17 04:50:51 +00002976
Guido van Rossum053b8df1998-11-25 16:18:00 +00002977 UNLESS (basicnew=PyObject_GetAttr(cls, __basicnew___str)) return NULL;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002978 r=PyObject_CallObject(basicnew, NULL);
2979 Py_DECREF(basicnew);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002980 if (r) return r;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002981 }
2982
Guido van Rossum053b8df1998-11-25 16:18:00 +00002983 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00002984
Guido van Rossum60456fd1997-04-09 17:36:32 +00002985err:
2986 {
2987 PyObject *tp, *v, *tb;
2988
2989 PyErr_Fetch(&tp, &v, &tb);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002990 if ((r=Py_BuildValue("OOO",v,cls,args))) {
2991 Py_XDECREF(v);
2992 v=r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002993 }
2994 PyErr_Restore(tp,v,tb);
2995 }
2996 return NULL;
2997}
Tim Peters84e87f32001-03-17 04:50:51 +00002998
Guido van Rossum60456fd1997-04-09 17:36:32 +00002999
3000static int
3001load_obj(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003002 PyObject *class, *tup, *obj=0;
3003 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003004
Guido van Rossum053b8df1998-11-25 16:18:00 +00003005 if ((i = marker(self)) < 0) return -1;
3006 UNLESS (tup=Pdata_popTuple(self->stack, i+1)) return -1;
3007 PDATA_POP(self->stack, class);
3008 if (class) {
3009 obj = Instance_New(class, tup);
3010 Py_DECREF(class);
3011 }
3012 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003013
Guido van Rossum053b8df1998-11-25 16:18:00 +00003014 if (! obj) return -1;
3015 PDATA_PUSH(self->stack, obj, -1);
3016 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003017}
3018
3019
3020static int
3021load_inst(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003022 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003023 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003024 char *s;
3025
Guido van Rossum053b8df1998-11-25 16:18:00 +00003026 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003027
Guido van Rossum053b8df1998-11-25 16:18:00 +00003028 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003029 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00003030 UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003031
Guido van Rossum053b8df1998-11-25 16:18:00 +00003032 if ((len = (*self->readline_func)(self, &s)) >= 0) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003033 if (len < 2) return bad_readline();
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003034 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003035 class = find_class(module_name, class_name, self->find_class);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003036 Py_DECREF(class_name);
3037 }
3038 }
3039 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003040
Guido van Rossum053b8df1998-11-25 16:18:00 +00003041 if (! class) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003042
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003043 if ((tup=Pdata_popTuple(self->stack, i))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003044 obj = Instance_New(class, tup);
3045 Py_DECREF(tup);
3046 }
3047 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003048
Guido van Rossum053b8df1998-11-25 16:18:00 +00003049 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003050
Guido van Rossum053b8df1998-11-25 16:18:00 +00003051 PDATA_PUSH(self->stack, obj, -1);
3052 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003053}
3054
3055
3056static int
3057load_global(Unpicklerobject *self) {
3058 PyObject *class = 0, *module_name = 0, *class_name = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003059 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003060 char *s;
3061
Guido van Rossum053b8df1998-11-25 16:18:00 +00003062 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003063 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00003064 UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003065
Guido van Rossum053b8df1998-11-25 16:18:00 +00003066 if ((len = (*self->readline_func)(self, &s)) >= 0) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003067 if (len < 2) return bad_readline();
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003068 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003069 class = find_class(module_name, class_name, self->find_class);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003070 Py_DECREF(class_name);
3071 }
3072 }
3073 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003074
Guido van Rossum053b8df1998-11-25 16:18:00 +00003075 if (! class) return -1;
3076 PDATA_PUSH(self->stack, class, -1);
3077 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003078}
3079
3080
3081static int
3082load_persid(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003083 PyObject *pid = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003084 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003085 char *s;
3086
3087 if (self->pers_func) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003088 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003089 if (len < 2) return bad_readline();
Tim Peters84e87f32001-03-17 04:50:51 +00003090
Guido van Rossum053b8df1998-11-25 16:18:00 +00003091 UNLESS (pid = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003092
Guido van Rossum053b8df1998-11-25 16:18:00 +00003093 if (PyList_Check(self->pers_func)) {
3094 if (PyList_Append(self->pers_func, pid) < 0) {
3095 Py_DECREF(pid);
3096 return -1;
3097 }
3098 }
3099 else {
3100 ARG_TUP(self, pid);
3101 if (self->arg) {
3102 pid = PyObject_CallObject(self->pers_func, self->arg);
3103 FREE_ARG_TUP(self);
3104 }
3105 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003106
Guido van Rossum053b8df1998-11-25 16:18:00 +00003107 if (! pid) return -1;
3108
3109 PDATA_PUSH(self->stack, pid, -1);
3110 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003111 }
3112 else {
3113 PyErr_SetString(UnpicklingError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00003114 "A load persistent id instruction was encountered,\n"
3115 "but no persistent_load function was specified.");
3116 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003117 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003118}
3119
Guido van Rossum60456fd1997-04-09 17:36:32 +00003120static int
3121load_binpersid(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003122 PyObject *pid = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003123
3124 if (self->pers_func) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003125 PDATA_POP(self->stack, pid);
3126 if (! pid) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003127
Guido van Rossum053b8df1998-11-25 16:18:00 +00003128 if (PyList_Check(self->pers_func)) {
3129 if (PyList_Append(self->pers_func, pid) < 0) {
3130 Py_DECREF(pid);
3131 return -1;
3132 }
3133 }
3134 else {
3135 ARG_TUP(self, pid);
3136 if (self->arg) {
3137 pid = PyObject_CallObject(self->pers_func, self->arg);
3138 FREE_ARG_TUP(self);
3139 }
3140 if (! pid) return -1;
3141 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003142
Guido van Rossum053b8df1998-11-25 16:18:00 +00003143 PDATA_PUSH(self->stack, pid, -1);
3144 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003145 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003146 else {
3147 PyErr_SetString(UnpicklingError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00003148 "A load persistent id instruction was encountered,\n"
3149 "but no persistent_load function was specified.");
3150 return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003151 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003152}
3153
3154
3155static int
3156load_pop(Unpicklerobject *self) {
3157 int len;
3158
Guido van Rossum053b8df1998-11-25 16:18:00 +00003159 UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003160
Tim Peters84e87f32001-03-17 04:50:51 +00003161 /* Note that we split the (pickle.py) stack into two stacks,
Guido van Rossumea2b7152000-05-09 18:14:50 +00003162 an object stack and a mark stack. We have to be clever and
3163 pop the right one. We do this by looking at the top of the
3164 mark stack.
3165 */
3166
Tim Peters84e87f32001-03-17 04:50:51 +00003167 if ((self->num_marks > 0) &&
Guido van Rossum60456fd1997-04-09 17:36:32 +00003168 (self->marks[self->num_marks - 1] == len))
3169 self->num_marks--;
Tim Peters84e87f32001-03-17 04:50:51 +00003170 else {
Guido van Rossumea2b7152000-05-09 18:14:50 +00003171 len--;
3172 Py_DECREF(self->stack->data[len]);
3173 self->stack->length=len;
3174 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003175
3176 return 0;
3177}
3178
3179
3180static int
3181load_pop_mark(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003182 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003183
3184 if ((i = marker(self)) < 0)
3185 return -1;
3186
Guido van Rossum053b8df1998-11-25 16:18:00 +00003187 Pdata_clear(self->stack, i);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003188
3189 return 0;
3190}
3191
3192
3193static int
3194load_dup(Unpicklerobject *self) {
3195 PyObject *last;
3196 int len;
3197
Guido van Rossum053b8df1998-11-25 16:18:00 +00003198 if ((len = self->stack->length) <= 0) return stackUnderflow();
3199 last=self->stack->data[len-1];
3200 Py_INCREF(last);
3201 PDATA_PUSH(self->stack, last, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003202 return 0;
3203}
3204
3205
3206static int
3207load_get(Unpicklerobject *self) {
3208 PyObject *py_str = 0, *value = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003209 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003210 char *s;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003211 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003212
Guido van Rossum053b8df1998-11-25 16:18:00 +00003213 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003214 if (len < 2) return bad_readline();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003215
Guido van Rossum053b8df1998-11-25 16:18:00 +00003216 UNLESS (py_str = PyString_FromStringAndSize(s, len - 1)) return -1;
3217
3218 value = PyDict_GetItem(self->memo, py_str);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003219 if (! value) {
3220 PyErr_SetObject(BadPickleGet, py_str);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003221 rc = -1;
3222 } else {
3223 PDATA_APPEND(self->stack, value, -1);
3224 rc = 0;
3225 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003226
Guido van Rossum2f80d961999-07-13 15:18:58 +00003227 Py_DECREF(py_str);
3228 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003229}
3230
3231
3232static int
3233load_binget(Unpicklerobject *self) {
3234 PyObject *py_key = 0, *value = 0;
3235 unsigned char key;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003236 char *s;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003237 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003238
Guido van Rossum053b8df1998-11-25 16:18:00 +00003239 if ((*self->read_func)(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003240
3241 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00003242 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003243
Guido van Rossum053b8df1998-11-25 16:18:00 +00003244 value = PyDict_GetItem(self->memo, py_key);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003245 if (! value) {
3246 PyErr_SetObject(BadPickleGet, py_key);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003247 rc = -1;
3248 } else {
3249 PDATA_APPEND(self->stack, value, -1);
3250 rc = 0;
3251 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003252
Guido van Rossum2f80d961999-07-13 15:18:58 +00003253 Py_DECREF(py_key);
3254 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003255}
3256
3257
3258static int
3259load_long_binget(Unpicklerobject *self) {
3260 PyObject *py_key = 0, *value = 0;
Thomas Wouters3b6448f2000-07-22 23:56:07 +00003261 unsigned char c;
3262 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003263 long key;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003264 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003265
Guido van Rossum053b8df1998-11-25 16:18:00 +00003266 if ((*self->read_func)(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003267
3268 c = (unsigned char)s[0];
3269 key = (long)c;
3270 c = (unsigned char)s[1];
3271 key |= (long)c << 8;
3272 c = (unsigned char)s[2];
3273 key |= (long)c << 16;
3274 c = (unsigned char)s[3];
3275 key |= (long)c << 24;
3276
Guido van Rossum053b8df1998-11-25 16:18:00 +00003277 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003278
Guido van Rossum053b8df1998-11-25 16:18:00 +00003279 value = PyDict_GetItem(self->memo, py_key);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003280 if (! value) {
3281 PyErr_SetObject(BadPickleGet, py_key);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003282 rc = -1;
3283 } else {
3284 PDATA_APPEND(self->stack, value, -1);
3285 rc = 0;
3286 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003287
Guido van Rossum2f80d961999-07-13 15:18:58 +00003288 Py_DECREF(py_key);
3289 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003290}
3291
3292
3293static int
3294load_put(Unpicklerobject *self) {
3295 PyObject *py_str = 0, *value = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003296 int len, l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003297 char *s;
3298
Guido van Rossum053b8df1998-11-25 16:18:00 +00003299 if ((l = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumd1f66dc1999-02-08 22:38:25 +00003300 if (l < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00003301 UNLESS (len=self->stack->length) return stackUnderflow();
3302 UNLESS (py_str = PyString_FromStringAndSize(s, l - 1)) return -1;
3303 value=self->stack->data[len-1];
3304 l=PyDict_SetItem(self->memo, py_str, value);
3305 Py_DECREF(py_str);
3306 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003307}
3308
3309
3310static int
3311load_binput(Unpicklerobject *self) {
3312 PyObject *py_key = 0, *value = 0;
Thomas Wouters3b6448f2000-07-22 23:56:07 +00003313 unsigned char key;
3314 char *s;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003315 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003316
Guido van Rossum053b8df1998-11-25 16:18:00 +00003317 if ((*self->read_func)(self, &s, 1) < 0) return -1;
3318 UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003319
3320 key = (unsigned char)s[0];
3321
Guido van Rossum053b8df1998-11-25 16:18:00 +00003322 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3323 value=self->stack->data[len-1];
3324 len=PyDict_SetItem(self->memo, py_key, value);
3325 Py_DECREF(py_key);
3326 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003327}
3328
3329
3330static int
3331load_long_binput(Unpicklerobject *self) {
3332 PyObject *py_key = 0, *value = 0;
3333 long key;
Thomas Wouters3b6448f2000-07-22 23:56:07 +00003334 unsigned char c;
3335 char *s;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003336 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003337
Guido van Rossum053b8df1998-11-25 16:18:00 +00003338 if ((*self->read_func)(self, &s, 4) < 0) return -1;
3339 UNLESS (len=self->stack->length) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003340
3341 c = (unsigned char)s[0];
3342 key = (long)c;
3343 c = (unsigned char)s[1];
3344 key |= (long)c << 8;
3345 c = (unsigned char)s[2];
3346 key |= (long)c << 16;
3347 c = (unsigned char)s[3];
3348 key |= (long)c << 24;
3349
Guido van Rossum053b8df1998-11-25 16:18:00 +00003350 UNLESS (py_key = PyInt_FromLong(key)) return -1;
3351 value=self->stack->data[len-1];
3352 len=PyDict_SetItem(self->memo, py_key, value);
3353 Py_DECREF(py_key);
3354 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003355}
3356
3357
Tim Peters84e87f32001-03-17 04:50:51 +00003358static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00003359do_append(Unpicklerobject *self, int x) {
3360 PyObject *value = 0, *list = 0, *append_method = 0;
3361 int len, i;
3362
Guido van Rossum053b8df1998-11-25 16:18:00 +00003363 UNLESS ((len=self->stack->length) >= x && x > 0) return stackUnderflow();
3364 if (len==x) return 0; /* nothing to do */
Guido van Rossum60456fd1997-04-09 17:36:32 +00003365
Guido van Rossum053b8df1998-11-25 16:18:00 +00003366 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003367
3368 if (PyList_Check(list)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003369 PyObject *slice;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003370 int list_len;
Tim Peters84e87f32001-03-17 04:50:51 +00003371
Guido van Rossum053b8df1998-11-25 16:18:00 +00003372 slice=Pdata_popList(self->stack, x);
3373 list_len = PyList_GET_SIZE(list);
3374 i=PyList_SetSlice(list, list_len, list_len, slice);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003375 Py_DECREF(slice);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003376 return i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003377 }
3378 else {
3379
Guido van Rossum053b8df1998-11-25 16:18:00 +00003380 UNLESS (append_method = PyObject_GetAttr(list, append_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00003381 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003382
Guido van Rossum60456fd1997-04-09 17:36:32 +00003383 for (i = x; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003384 PyObject *junk;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003385
Guido van Rossum053b8df1998-11-25 16:18:00 +00003386 value=self->stack->data[i];
3387 junk=0;
3388 ARG_TUP(self, value);
3389 if (self->arg) {
3390 junk = PyObject_CallObject(append_method, self->arg);
3391 FREE_ARG_TUP(self);
3392 }
3393 if (! junk) {
3394 Pdata_clear(self->stack, i+1);
3395 self->stack->length=x;
3396 Py_DECREF(append_method);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003397 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003398 }
3399 Py_DECREF(junk);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003400 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003401 self->stack->length=x;
3402 Py_DECREF(append_method);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003403 }
3404
Guido van Rossum60456fd1997-04-09 17:36:32 +00003405 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003406}
3407
Tim Peters84e87f32001-03-17 04:50:51 +00003408
Guido van Rossum60456fd1997-04-09 17:36:32 +00003409static int
3410load_append(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003411 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003412}
3413
3414
3415static int
3416load_appends(Unpicklerobject *self) {
3417 return do_append(self, marker(self));
3418}
3419
3420
3421static int
3422do_setitems(Unpicklerobject *self, int x) {
3423 PyObject *value = 0, *key = 0, *dict = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003424 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003425
Guido van Rossum053b8df1998-11-25 16:18:00 +00003426 UNLESS ((len=self->stack->length) >= x
3427 && x > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003428
Guido van Rossum053b8df1998-11-25 16:18:00 +00003429 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003430
Guido van Rossum053b8df1998-11-25 16:18:00 +00003431 for (i = x+1; i < len; i += 2) {
3432 key =self->stack->data[i-1];
3433 value=self->stack->data[i ];
3434 if (PyObject_SetItem(dict, key, value) < 0) {
3435 r=-1;
3436 break;
3437 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003438 }
3439
Guido van Rossum053b8df1998-11-25 16:18:00 +00003440 Pdata_clear(self->stack, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003441
Guido van Rossum053b8df1998-11-25 16:18:00 +00003442 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003443}
3444
3445
3446static int
3447load_setitem(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003448 return do_setitems(self, self->stack->length - 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003449}
3450
Guido van Rossum60456fd1997-04-09 17:36:32 +00003451static int
3452load_setitems(Unpicklerobject *self) {
3453 return do_setitems(self, marker(self));
3454}
3455
3456
3457static int
3458load_build(Unpicklerobject *self) {
Tim Peters84e87f32001-03-17 04:50:51 +00003459 PyObject *value = 0, *inst = 0, *instdict = 0, *d_key = 0, *d_value = 0,
Guido van Rossum60456fd1997-04-09 17:36:32 +00003460 *junk = 0, *__setstate__ = 0;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003461 int i, r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003462
Guido van Rossum053b8df1998-11-25 16:18:00 +00003463 if (self->stack->length < 2) return stackUnderflow();
3464 PDATA_POP(self->stack, value);
3465 if (! value) return -1;
3466 inst=self->stack->data[self->stack->length-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003467
Guido van Rossum053b8df1998-11-25 16:18:00 +00003468 if ((__setstate__ = PyObject_GetAttr(inst, __setstate___str))) {
3469 ARG_TUP(self, value);
3470 if (self->arg) {
3471 junk = PyObject_CallObject(__setstate__, self->arg);
3472 FREE_ARG_TUP(self);
3473 }
3474 Py_DECREF(__setstate__);
3475 if (! junk) return -1;
3476 Py_DECREF(junk);
3477 return 0;
3478 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003479
Guido van Rossum053b8df1998-11-25 16:18:00 +00003480 PyErr_Clear();
3481 if ((instdict = PyObject_GetAttr(inst, __dict___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003482 i = 0;
3483 while (PyDict_Next(value, &i, &d_key, &d_value)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003484 if (PyObject_SetItem(instdict, d_key, d_value) < 0) {
3485 r=-1;
3486 break;
3487 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003488 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003489 Py_DECREF(instdict);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003490 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003491 else r=-1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003492
Guido van Rossum053b8df1998-11-25 16:18:00 +00003493 Py_XDECREF(value);
Tim Peters84e87f32001-03-17 04:50:51 +00003494
Guido van Rossum053b8df1998-11-25 16:18:00 +00003495 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003496}
3497
3498
3499static int
3500load_mark(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003501 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003502
Guido van Rossumea2b7152000-05-09 18:14:50 +00003503 /* Note that we split the (pickle.py) stack into two stacks, an
3504 object stack and a mark stack. Here we push a mark onto the
Tim Peters84e87f32001-03-17 04:50:51 +00003505 mark stack.
Guido van Rossumea2b7152000-05-09 18:14:50 +00003506 */
3507
Guido van Rossum053b8df1998-11-25 16:18:00 +00003508 if ((self->num_marks + 1) >= self->marks_size) {
3509 s=self->marks_size+20;
3510 if (s <= self->num_marks) s=self->num_marks + 1;
Guido van Rossum761fcd01999-04-12 22:51:20 +00003511 if (self->marks == NULL)
Guido van Rossumaa8d1671999-01-25 21:43:51 +00003512 self->marks=(int *)malloc(s * sizeof(int));
3513 else
3514 self->marks=(int *)realloc(self->marks, s * sizeof(int));
Guido van Rossum053b8df1998-11-25 16:18:00 +00003515 if (! self->marks) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003516 PyErr_NoMemory();
3517 return -1;
3518 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003519 self->marks_size = s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003520 }
3521
Guido van Rossum053b8df1998-11-25 16:18:00 +00003522 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003523
3524 return 0;
3525}
3526
3527static int
3528load_reduce(Unpicklerobject *self) {
3529 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003530
Guido van Rossum053b8df1998-11-25 16:18:00 +00003531 PDATA_POP(self->stack, arg_tup);
3532 if (! arg_tup) return -1;
3533 PDATA_POP(self->stack, callable);
3534 if (callable) {
3535 ob = Instance_New(callable, arg_tup);
3536 Py_DECREF(callable);
3537 }
3538 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003539
Guido van Rossum053b8df1998-11-25 16:18:00 +00003540 if (! ob) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003541
Guido van Rossum053b8df1998-11-25 16:18:00 +00003542 PDATA_PUSH(self->stack, ob, -1);
3543 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003544}
Tim Peters84e87f32001-03-17 04:50:51 +00003545
Guido van Rossum60456fd1997-04-09 17:36:32 +00003546static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003547load(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003548 PyObject *err = 0, *val = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003549 char *s;
3550
Guido van Rossum60456fd1997-04-09 17:36:32 +00003551 self->num_marks = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003552 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003553
3554 while (1) {
3555 if ((*self->read_func)(self, &s, 1) < 0)
3556 break;
3557
3558 switch (s[0]) {
3559 case NONE:
3560 if (load_none(self) < 0)
3561 break;
3562 continue;
3563
3564 case BININT:
3565 if (load_binint(self) < 0)
3566 break;
3567 continue;
3568
3569 case BININT1:
3570 if (load_binint1(self) < 0)
3571 break;
3572 continue;
3573
3574 case BININT2:
3575 if (load_binint2(self) < 0)
3576 break;
3577 continue;
3578
3579 case INT:
3580 if (load_int(self) < 0)
3581 break;
3582 continue;
3583
3584 case LONG:
3585 if (load_long(self) < 0)
3586 break;
3587 continue;
3588
3589 case FLOAT:
3590 if (load_float(self) < 0)
3591 break;
3592 continue;
3593
Guido van Rossum60456fd1997-04-09 17:36:32 +00003594 case BINFLOAT:
3595 if (load_binfloat(self) < 0)
3596 break;
3597 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003598
3599 case BINSTRING:
3600 if (load_binstring(self) < 0)
3601 break;
3602 continue;
3603
3604 case SHORT_BINSTRING:
3605 if (load_short_binstring(self) < 0)
3606 break;
3607 continue;
3608
3609 case STRING:
3610 if (load_string(self) < 0)
3611 break;
3612 continue;
3613
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003614 case UNICODE:
3615 if (load_unicode(self) < 0)
3616 break;
3617 continue;
3618
3619 case BINUNICODE:
3620 if (load_binunicode(self) < 0)
3621 break;
3622 continue;
3623
Guido van Rossum60456fd1997-04-09 17:36:32 +00003624 case EMPTY_TUPLE:
3625 if (load_empty_tuple(self) < 0)
3626 break;
3627 continue;
3628
3629 case TUPLE:
3630 if (load_tuple(self) < 0)
3631 break;
3632 continue;
3633
3634 case EMPTY_LIST:
3635 if (load_empty_list(self) < 0)
3636 break;
3637 continue;
3638
3639 case LIST:
3640 if (load_list(self) < 0)
3641 break;
3642 continue;
3643
3644 case EMPTY_DICT:
3645 if (load_empty_dict(self) < 0)
3646 break;
3647 continue;
3648
3649 case DICT:
3650 if (load_dict(self) < 0)
3651 break;
3652 continue;
3653
3654 case OBJ:
3655 if (load_obj(self) < 0)
3656 break;
3657 continue;
3658
3659 case INST:
3660 if (load_inst(self) < 0)
3661 break;
3662 continue;
3663
3664 case GLOBAL:
3665 if (load_global(self) < 0)
3666 break;
3667 continue;
3668
3669 case APPEND:
3670 if (load_append(self) < 0)
3671 break;
3672 continue;
3673
3674 case APPENDS:
3675 if (load_appends(self) < 0)
3676 break;
3677 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003678
Guido van Rossum60456fd1997-04-09 17:36:32 +00003679 case BUILD:
3680 if (load_build(self) < 0)
3681 break;
3682 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003683
Guido van Rossum60456fd1997-04-09 17:36:32 +00003684 case DUP:
3685 if (load_dup(self) < 0)
3686 break;
3687 continue;
3688
3689 case BINGET:
3690 if (load_binget(self) < 0)
3691 break;
3692 continue;
3693
3694 case LONG_BINGET:
3695 if (load_long_binget(self) < 0)
3696 break;
3697 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003698
Guido van Rossum60456fd1997-04-09 17:36:32 +00003699 case GET:
3700 if (load_get(self) < 0)
3701 break;
3702 continue;
3703
3704 case MARK:
3705 if (load_mark(self) < 0)
3706 break;
3707 continue;
3708
3709 case BINPUT:
3710 if (load_binput(self) < 0)
3711 break;
3712 continue;
3713
3714 case LONG_BINPUT:
3715 if (load_long_binput(self) < 0)
3716 break;
3717 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003718
Guido van Rossum60456fd1997-04-09 17:36:32 +00003719 case PUT:
3720 if (load_put(self) < 0)
3721 break;
3722 continue;
3723
3724 case POP:
3725 if (load_pop(self) < 0)
3726 break;
3727 continue;
3728
3729 case POP_MARK:
3730 if (load_pop_mark(self) < 0)
3731 break;
3732 continue;
3733
3734 case SETITEM:
3735 if (load_setitem(self) < 0)
3736 break;
3737 continue;
3738
3739 case SETITEMS:
3740 if (load_setitems(self) < 0)
3741 break;
3742 continue;
3743
3744 case STOP:
3745 break;
3746
3747 case PERSID:
3748 if (load_persid(self) < 0)
3749 break;
3750 continue;
3751
3752 case BINPERSID:
3753 if (load_binpersid(self) < 0)
3754 break;
3755 continue;
3756
3757 case REDUCE:
3758 if (load_reduce(self) < 0)
3759 break;
3760 continue;
3761
Tim Peters84e87f32001-03-17 04:50:51 +00003762 default:
3763 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossum60456fd1997-04-09 17:36:32 +00003764 "c", s[0]);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003765 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003766 }
3767
3768 break;
3769 }
3770
Guido van Rossum053b8df1998-11-25 16:18:00 +00003771 if ((err = PyErr_Occurred())) {
3772 if (err == PyExc_EOFError) {
3773 PyErr_SetNone(PyExc_EOFError);
Tim Peters84e87f32001-03-17 04:50:51 +00003774 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003775 return NULL;
3776 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003777
Tim Peters84e87f32001-03-17 04:50:51 +00003778 PDATA_POP(self->stack, val);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003779 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003780}
Tim Peters84e87f32001-03-17 04:50:51 +00003781
Guido van Rossum60456fd1997-04-09 17:36:32 +00003782
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003783/* No-load functions to support noload, which is used to
3784 find persistent references. */
3785
3786static int
3787noload_obj(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003788 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003789
3790 if ((i = marker(self)) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003791 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003792}
3793
3794
3795static int
3796noload_inst(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003797 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003798 char *s;
3799
3800 if ((i = marker(self)) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003801 Pdata_clear(self->stack, i);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003802 if ((*self->readline_func)(self, &s) < 0) return -1;
3803 if ((*self->readline_func)(self, &s) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003804 PDATA_APPEND(self->stack, Py_None,-1);
3805 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003806}
3807
3808static int
3809noload_global(Unpicklerobject *self) {
3810 char *s;
3811
3812 if ((*self->readline_func)(self, &s) < 0) return -1;
3813 if ((*self->readline_func)(self, &s) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003814 PDATA_APPEND(self->stack, Py_None,-1);
3815 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003816}
3817
3818static int
3819noload_reduce(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003820
Guido van Rossum053b8df1998-11-25 16:18:00 +00003821 if (self->stack->length < 2) return stackUnderflow();
3822 Pdata_clear(self->stack, self->stack->length-2);
3823 PDATA_APPEND(self->stack, Py_None,-1);
3824 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003825}
3826
3827static int
3828noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003829
Guido van Rossum053b8df1998-11-25 16:18:00 +00003830 if (self->stack->length < 1) return stackUnderflow();
3831 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00003832 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003833}
3834
3835
3836static PyObject *
3837noload(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003838 PyObject *err = 0, *val = 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003839 char *s;
3840
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003841 self->num_marks = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003842 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003843
3844 while (1) {
3845 if ((*self->read_func)(self, &s, 1) < 0)
3846 break;
3847
3848 switch (s[0]) {
3849 case NONE:
3850 if (load_none(self) < 0)
3851 break;
3852 continue;
3853
3854 case BININT:
3855 if (load_binint(self) < 0)
3856 break;
3857 continue;
3858
3859 case BININT1:
3860 if (load_binint1(self) < 0)
3861 break;
3862 continue;
3863
3864 case BININT2:
3865 if (load_binint2(self) < 0)
3866 break;
3867 continue;
3868
3869 case INT:
3870 if (load_int(self) < 0)
3871 break;
3872 continue;
3873
3874 case LONG:
3875 if (load_long(self) < 0)
3876 break;
3877 continue;
3878
3879 case FLOAT:
3880 if (load_float(self) < 0)
3881 break;
3882 continue;
3883
3884 case BINFLOAT:
3885 if (load_binfloat(self) < 0)
3886 break;
3887 continue;
3888
3889 case BINSTRING:
3890 if (load_binstring(self) < 0)
3891 break;
3892 continue;
3893
3894 case SHORT_BINSTRING:
3895 if (load_short_binstring(self) < 0)
3896 break;
3897 continue;
3898
3899 case STRING:
3900 if (load_string(self) < 0)
3901 break;
3902 continue;
3903
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003904 case UNICODE:
3905 if (load_unicode(self) < 0)
3906 break;
3907 continue;
3908
3909 case BINUNICODE:
3910 if (load_binunicode(self) < 0)
3911 break;
3912 continue;
3913
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003914 case EMPTY_TUPLE:
3915 if (load_empty_tuple(self) < 0)
3916 break;
3917 continue;
3918
3919 case TUPLE:
3920 if (load_tuple(self) < 0)
3921 break;
3922 continue;
3923
3924 case EMPTY_LIST:
3925 if (load_empty_list(self) < 0)
3926 break;
3927 continue;
3928
3929 case LIST:
3930 if (load_list(self) < 0)
3931 break;
3932 continue;
3933
3934 case EMPTY_DICT:
3935 if (load_empty_dict(self) < 0)
3936 break;
3937 continue;
3938
3939 case DICT:
3940 if (load_dict(self) < 0)
3941 break;
3942 continue;
3943
3944 case OBJ:
3945 if (noload_obj(self) < 0)
3946 break;
3947 continue;
3948
3949 case INST:
3950 if (noload_inst(self) < 0)
3951 break;
3952 continue;
3953
3954 case GLOBAL:
3955 if (noload_global(self) < 0)
3956 break;
3957 continue;
3958
3959 case APPEND:
3960 if (load_append(self) < 0)
3961 break;
3962 continue;
3963
3964 case APPENDS:
3965 if (load_appends(self) < 0)
3966 break;
3967 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003968
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003969 case BUILD:
3970 if (noload_build(self) < 0)
3971 break;
3972 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003973
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003974 case DUP:
3975 if (load_dup(self) < 0)
3976 break;
3977 continue;
3978
3979 case BINGET:
3980 if (load_binget(self) < 0)
3981 break;
3982 continue;
3983
3984 case LONG_BINGET:
3985 if (load_long_binget(self) < 0)
3986 break;
3987 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003988
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003989 case GET:
3990 if (load_get(self) < 0)
3991 break;
3992 continue;
3993
3994 case MARK:
3995 if (load_mark(self) < 0)
3996 break;
3997 continue;
3998
3999 case BINPUT:
4000 if (load_binput(self) < 0)
4001 break;
4002 continue;
4003
4004 case LONG_BINPUT:
4005 if (load_long_binput(self) < 0)
4006 break;
4007 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004008
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004009 case PUT:
4010 if (load_put(self) < 0)
4011 break;
4012 continue;
4013
4014 case POP:
4015 if (load_pop(self) < 0)
4016 break;
4017 continue;
4018
4019 case POP_MARK:
4020 if (load_pop_mark(self) < 0)
4021 break;
4022 continue;
4023
4024 case SETITEM:
4025 if (load_setitem(self) < 0)
4026 break;
4027 continue;
4028
4029 case SETITEMS:
4030 if (load_setitems(self) < 0)
4031 break;
4032 continue;
4033
4034 case STOP:
4035 break;
4036
4037 case PERSID:
4038 if (load_persid(self) < 0)
4039 break;
4040 continue;
4041
4042 case BINPERSID:
4043 if (load_binpersid(self) < 0)
4044 break;
4045 continue;
4046
4047 case REDUCE:
4048 if (noload_reduce(self) < 0)
4049 break;
4050 continue;
4051
Tim Peters84e87f32001-03-17 04:50:51 +00004052 default:
4053 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004054 "c", s[0]);
Guido van Rossum053b8df1998-11-25 16:18:00 +00004055 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004056 }
4057
4058 break;
4059 }
4060
Guido van Rossum053b8df1998-11-25 16:18:00 +00004061 if ((err = PyErr_Occurred())) {
4062 if (err == PyExc_EOFError) {
4063 PyErr_SetNone(PyExc_EOFError);
Tim Peters84e87f32001-03-17 04:50:51 +00004064 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00004065 return NULL;
4066 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004067
Tim Peters84e87f32001-03-17 04:50:51 +00004068 PDATA_POP(self->stack, val);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004069 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004070}
Tim Peters84e87f32001-03-17 04:50:51 +00004071
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004072
Guido van Rossum60456fd1997-04-09 17:36:32 +00004073static PyObject *
4074Unpickler_load(Unpicklerobject *self, PyObject *args) {
Tim Peters84e87f32001-03-17 04:50:51 +00004075 UNLESS (PyArg_ParseTuple(args, ":load"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004076 return NULL;
4077
4078 return load(self);
4079}
4080
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004081static PyObject *
4082Unpickler_noload(Unpicklerobject *self, PyObject *args) {
Tim Peters84e87f32001-03-17 04:50:51 +00004083 UNLESS (PyArg_ParseTuple(args, ":noload"))
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004084 return NULL;
4085
4086 return noload(self);
4087}
4088
Guido van Rossum60456fd1997-04-09 17:36:32 +00004089
4090static struct PyMethodDef Unpickler_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004091 {"load", (PyCFunction)Unpickler_load, 1,
4092 "load() -- Load a pickle"
4093 },
4094 {"noload", (PyCFunction)Unpickler_noload, 1,
4095 "noload() -- not load a pickle, but go through most of the motions\n"
4096 "\n"
4097 "This function can be used to read past a pickle without instantiating\n"
4098 "any objects or importing any modules. It can also be used to find all\n"
4099 "persistent references without instantiating any objects or importing\n"
4100 "any modules.\n"
4101 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00004102 {NULL, NULL} /* sentinel */
4103};
4104
4105
4106static Unpicklerobject *
4107newUnpicklerobject(PyObject *f) {
4108 Unpicklerobject *self;
4109
Guido van Rossumb18618d2000-05-03 23:44:39 +00004110 UNLESS (self = PyObject_New(Unpicklerobject, &Unpicklertype))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004111 return NULL;
4112
4113 self->file = NULL;
4114 self->arg = NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00004115 self->stack = (Pdata*)Pdata_New();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004116 self->pers_func = NULL;
4117 self->last_string = NULL;
4118 self->marks = NULL;
4119 self->num_marks = 0;
4120 self->marks_size = 0;
4121 self->buf_size = 0;
4122 self->read = NULL;
Guido van Rossum8a6dba31998-03-06 01:39:39 +00004123 self->readline = NULL;
Guido van Rossum21ef0881998-12-11 03:20:00 +00004124 self->safe_constructors = NULL;
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004125 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004126
Tim Peters84e87f32001-03-17 04:50:51 +00004127 UNLESS (self->memo = PyDict_New())
Guido van Rossum83addc72000-04-21 20:49:36 +00004128 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004129
4130 Py_INCREF(f);
4131 self->file = f;
4132
4133 /* Set read, readline based on type of f */
4134 if (PyFile_Check(f)) {
4135 self->fp = PyFile_AsFile(f);
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00004136 if (self->fp == NULL) {
Guido van Rossum83addc72000-04-21 20:49:36 +00004137 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
4138 goto err;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00004139 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004140 self->read_func = read_file;
4141 self->readline_func = readline_file;
4142 }
4143 else if (PycStringIO_InputCheck(f)) {
4144 self->fp = NULL;
4145 self->read_func = read_cStringIO;
4146 self->readline_func = readline_cStringIO;
4147 }
4148 else {
4149
4150 self->fp = NULL;
4151 self->read_func = read_other;
4152 self->readline_func = readline_other;
4153
Guido van Rossum053b8df1998-11-25 16:18:00 +00004154 UNLESS ((self->readline = PyObject_GetAttr(f, readline_str)) &&
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004155 (self->read = PyObject_GetAttr(f, read_str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004156 PyErr_Clear();
4157 PyErr_SetString( PyExc_TypeError, "argument must have 'read' and "
4158 "'readline' attributes" );
Guido van Rossum053b8df1998-11-25 16:18:00 +00004159 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004160 }
4161 }
4162
Guido van Rossum053b8df1998-11-25 16:18:00 +00004163 if (PyEval_GetRestricted()) {
4164 /* Restricted execution, get private tables */
4165 PyObject *m;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004166
Guido van Rossum053b8df1998-11-25 16:18:00 +00004167 UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
4168 self->safe_constructors=PyObject_GetAttr(m, safe_constructors_str);
4169 Py_DECREF(m);
4170 UNLESS (self->safe_constructors) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004171 }
4172 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00004173 self->safe_constructors=safe_constructors;
4174 Py_INCREF(safe_constructors);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004175 }
4176
Guido van Rossum60456fd1997-04-09 17:36:32 +00004177 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004178
4179err:
4180 Py_DECREF((PyObject *)self);
4181 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004182}
4183
4184
4185static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004186get_Unpickler(PyObject *self, PyObject *args) {
4187 PyObject *file;
Tim Peters84e87f32001-03-17 04:50:51 +00004188
Guido van Rossum43713e52000-02-29 13:59:29 +00004189 UNLESS (PyArg_ParseTuple(args, "O:Unpickler", &file))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004190 return NULL;
4191 return (PyObject *)newUnpicklerobject(file);
4192}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004193
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004194
Guido van Rossum60456fd1997-04-09 17:36:32 +00004195static void
4196Unpickler_dealloc(Unpicklerobject *self) {
4197 Py_XDECREF(self->readline);
4198 Py_XDECREF(self->read);
4199 Py_XDECREF(self->file);
4200 Py_XDECREF(self->memo);
4201 Py_XDECREF(self->stack);
4202 Py_XDECREF(self->pers_func);
4203 Py_XDECREF(self->arg);
4204 Py_XDECREF(self->last_string);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004205 Py_XDECREF(self->safe_constructors);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004206
Guido van Rossum60456fd1997-04-09 17:36:32 +00004207 if (self->marks) {
4208 free(self->marks);
4209 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004210
Guido van Rossum60456fd1997-04-09 17:36:32 +00004211 if (self->buf_size) {
4212 free(self->buf);
4213 }
Tim Peters84e87f32001-03-17 04:50:51 +00004214
Guido van Rossumb18618d2000-05-03 23:44:39 +00004215 PyObject_Del(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004216}
4217
4218
4219static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004220Unpickler_getattr(Unpicklerobject *self, char *name) {
4221 if (!strcmp(name, "persistent_load")) {
4222 if (!self->pers_func) {
4223 PyErr_SetString(PyExc_AttributeError, name);
4224 return NULL;
4225 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004226
Guido van Rossum60456fd1997-04-09 17:36:32 +00004227 Py_INCREF(self->pers_func);
4228 return self->pers_func;
4229 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004230
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004231 if (!strcmp(name, "find_global")) {
4232 if (!self->find_class) {
4233 PyErr_SetString(PyExc_AttributeError, name);
4234 return NULL;
4235 }
4236
4237 Py_INCREF(self->find_class);
4238 return self->find_class;
4239 }
4240
Guido van Rossum60456fd1997-04-09 17:36:32 +00004241 if (!strcmp(name, "memo")) {
4242 if (!self->memo) {
4243 PyErr_SetString(PyExc_AttributeError, name);
4244 return NULL;
4245 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004246
Guido van Rossum60456fd1997-04-09 17:36:32 +00004247 Py_INCREF(self->memo);
4248 return self->memo;
4249 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004250
Guido van Rossum60456fd1997-04-09 17:36:32 +00004251 if (!strcmp(name, "UnpicklingError")) {
4252 Py_INCREF(UnpicklingError);
4253 return UnpicklingError;
4254 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004255
Guido van Rossum60456fd1997-04-09 17:36:32 +00004256 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
4257}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004258
Guido van Rossum60456fd1997-04-09 17:36:32 +00004259
4260static int
4261Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004262
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004263 if (!strcmp(name, "persistent_load")) {
4264 Py_XDECREF(self->pers_func);
4265 self->pers_func = value;
4266 Py_XINCREF(value);
4267 return 0;
4268 }
4269
4270 if (!strcmp(name, "find_global")) {
4271 Py_XDECREF(self->find_class);
4272 self->find_class = value;
4273 Py_XINCREF(value);
4274 return 0;
4275 }
4276
Guido van Rossum053b8df1998-11-25 16:18:00 +00004277 if (! value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004278 PyErr_SetString(PyExc_TypeError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00004279 "attribute deletion is not supported");
4280 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004281 }
4282
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004283 if (strcmp(name, "memo") == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00004284 if (! PyDict_Check(value)) {
4285 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
4286 return -1;
4287 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004288 Py_XDECREF(self->memo);
4289 self->memo = value;
4290 Py_INCREF(value);
4291 return 0;
4292 }
4293
Guido van Rossum60456fd1997-04-09 17:36:32 +00004294 PyErr_SetString(PyExc_AttributeError, name);
4295 return -1;
4296}
4297
4298
4299static PyObject *
4300cpm_dump(PyObject *self, PyObject *args) {
4301 PyObject *ob, *file, *res = NULL;
4302 Picklerobject *pickler = 0;
4303 int bin = 0;
4304
Guido van Rossum053b8df1998-11-25 16:18:00 +00004305 UNLESS (PyArg_ParseTuple(args, "OO|i", &ob, &file, &bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004306 goto finally;
4307
Guido van Rossum053b8df1998-11-25 16:18:00 +00004308 UNLESS (pickler = newPicklerobject(file, bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004309 goto finally;
4310
4311 if (dump(pickler, ob) < 0)
4312 goto finally;
4313
4314 Py_INCREF(Py_None);
4315 res = Py_None;
4316
4317finally:
4318 Py_XDECREF(pickler);
4319
4320 return res;
4321}
4322
4323
4324static PyObject *
4325cpm_dumps(PyObject *self, PyObject *args) {
4326 PyObject *ob, *file = 0, *res = NULL;
4327 Picklerobject *pickler = 0;
4328 int bin = 0;
4329
Guido van Rossum43713e52000-02-29 13:59:29 +00004330 UNLESS (PyArg_ParseTuple(args, "O|i:dumps", &ob, &bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004331 goto finally;
4332
Guido van Rossum053b8df1998-11-25 16:18:00 +00004333 UNLESS (file = PycStringIO->NewOutput(128))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004334 goto finally;
4335
Guido van Rossum053b8df1998-11-25 16:18:00 +00004336 UNLESS (pickler = newPicklerobject(file, bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004337 goto finally;
4338
4339 if (dump(pickler, ob) < 0)
4340 goto finally;
4341
4342 res = PycStringIO->cgetvalue(file);
4343
4344finally:
4345 Py_XDECREF(pickler);
4346 Py_XDECREF(file);
4347
4348 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00004349}
4350
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004351
4352static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004353cpm_load(PyObject *self, PyObject *args) {
4354 Unpicklerobject *unpickler = 0;
4355 PyObject *ob, *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004356
Guido van Rossum43713e52000-02-29 13:59:29 +00004357 UNLESS (PyArg_ParseTuple(args, "O:load", &ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004358 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004359
Guido van Rossum053b8df1998-11-25 16:18:00 +00004360 UNLESS (unpickler = newUnpicklerobject(ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004361 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004362
Guido van Rossum60456fd1997-04-09 17:36:32 +00004363 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004364
Guido van Rossum60456fd1997-04-09 17:36:32 +00004365finally:
4366 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004367
Guido van Rossum60456fd1997-04-09 17:36:32 +00004368 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004369}
4370
4371
4372static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004373cpm_loads(PyObject *self, PyObject *args) {
4374 PyObject *ob, *file = 0, *res = NULL;
4375 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004376
Guido van Rossum43713e52000-02-29 13:59:29 +00004377 UNLESS (PyArg_ParseTuple(args, "S:loads", &ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004378 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004379
Guido van Rossum053b8df1998-11-25 16:18:00 +00004380 UNLESS (file = PycStringIO->NewInput(ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004381 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00004382
Guido van Rossum053b8df1998-11-25 16:18:00 +00004383 UNLESS (unpickler = newUnpicklerobject(file))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004384 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004385
Guido van Rossum60456fd1997-04-09 17:36:32 +00004386 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004387
Guido van Rossum60456fd1997-04-09 17:36:32 +00004388finally:
4389 Py_XDECREF(file);
4390 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004391
Guido van Rossum60456fd1997-04-09 17:36:32 +00004392 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004393}
4394
4395
Tim Peters84e87f32001-03-17 04:50:51 +00004396static char Unpicklertype__doc__[] =
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004397"Objects that know how to unpickle";
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004398
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004399static PyTypeObject Unpicklertype = {
4400 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004401 0, /*ob_size*/
4402 "Unpickler", /*tp_name*/
4403 sizeof(Unpicklerobject), /*tp_basicsize*/
4404 0, /*tp_itemsize*/
4405 /* methods */
4406 (destructor)Unpickler_dealloc, /*tp_dealloc*/
4407 (printfunc)0, /*tp_print*/
4408 (getattrfunc)Unpickler_getattr, /*tp_getattr*/
4409 (setattrfunc)Unpickler_setattr, /*tp_setattr*/
4410 (cmpfunc)0, /*tp_compare*/
4411 (reprfunc)0, /*tp_repr*/
4412 0, /*tp_as_number*/
4413 0, /*tp_as_sequence*/
4414 0, /*tp_as_mapping*/
4415 (hashfunc)0, /*tp_hash*/
4416 (ternaryfunc)0, /*tp_call*/
4417 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004418
Guido van Rossum60456fd1997-04-09 17:36:32 +00004419 /* Space for future expansion */
4420 0L,0L,0L,0L,
4421 Unpicklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004422};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004423
Guido van Rossum60456fd1997-04-09 17:36:32 +00004424static struct PyMethodDef cPickle_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004425 {"dump", (PyCFunction)cpm_dump, 1,
4426 "dump(object, file, [binary]) --"
4427 "Write an object in pickle format to the given file\n"
4428 "\n"
4429 "If the optional argument, binary, is provided and is true, then the\n"
4430 "pickle will be written in binary format, which is more space and\n"
4431 "computationally efficient. \n"
4432 },
4433 {"dumps", (PyCFunction)cpm_dumps, 1,
4434 "dumps(object, [binary]) --"
4435 "Return a string containing an object in pickle format\n"
4436 "\n"
4437 "If the optional argument, binary, is provided and is true, then the\n"
4438 "pickle will be written in binary format, which is more space and\n"
4439 "computationally efficient. \n"
4440 },
4441 {"load", (PyCFunction)cpm_load, 1,
4442 "load(file) -- Load a pickle from the given file"},
4443 {"loads", (PyCFunction)cpm_loads, 1,
4444 "loads(string) -- Load a pickle from the given string"},
4445 {"Pickler", (PyCFunction)get_Pickler, 1,
4446 "Pickler(file, [binary]) -- Create a pickler\n"
4447 "\n"
4448 "If the optional argument, binary, is provided and is true, then\n"
4449 "pickles will be written in binary format, which is more space and\n"
4450 "computationally efficient. \n"
4451 },
4452 {"Unpickler", (PyCFunction)get_Unpickler, 1,
4453 "Unpickler(file) -- Create an unpickler"},
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004454 { NULL, NULL }
4455};
4456
Guido van Rossum60456fd1997-04-09 17:36:32 +00004457static int
Guido van Rossumebba4202000-09-07 14:35:37 +00004458init_stuff(PyObject *module_dict) {
Fred Drake2c7a6852001-07-17 18:34:03 +00004459 PyObject *copy_reg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004460
4461#define INIT_STR(S) UNLESS(S ## _str=PyString_FromString(#S)) return -1;
4462
4463 INIT_STR(__class__);
4464 INIT_STR(__getinitargs__);
4465 INIT_STR(__dict__);
4466 INIT_STR(__getstate__);
4467 INIT_STR(__setstate__);
4468 INIT_STR(__name__);
Guido van Rossum142eeb81997-08-13 03:14:41 +00004469 INIT_STR(__main__);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004470 INIT_STR(__reduce__);
4471 INIT_STR(write);
4472 INIT_STR(__safe_for_unpickling__);
4473 INIT_STR(append);
4474 INIT_STR(read);
4475 INIT_STR(readline);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004476 INIT_STR(copy_reg);
4477 INIT_STR(dispatch_table);
4478 INIT_STR(safe_constructors);
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004479 INIT_STR(__basicnew__);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004480
Guido van Rossum053b8df1998-11-25 16:18:00 +00004481 UNLESS (copy_reg = PyImport_ImportModule("copy_reg"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004482 return -1;
4483
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004484 /* These next few are special because we want to use different
4485 ones in restricted mode. */
4486
Guido van Rossum053b8df1998-11-25 16:18:00 +00004487 UNLESS (dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004488 return -1;
4489
Guido van Rossum053b8df1998-11-25 16:18:00 +00004490 UNLESS (safe_constructors = PyObject_GetAttr(copy_reg,
4491 safe_constructors_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004492 return -1;
4493
4494 Py_DECREF(copy_reg);
4495
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004496 /* Down to here ********************************** */
4497
Guido van Rossum053b8df1998-11-25 16:18:00 +00004498 UNLESS (empty_tuple = PyTuple_New(0))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004499 return -1;
4500
Guido van Rossumc03158b1999-06-09 15:23:31 +00004501 /* Ugh */
4502 UNLESS (t=PyImport_ImportModule("__builtin__")) return -1;
4503 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
4504 return -1;
4505
4506 UNLESS (t=PyDict_New()) return -1;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004507 UNLESS (r=PyRun_String(
4508 "def __init__(self, *args): self.args=args\n\n"
4509 "def __str__(self):\n"
4510 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
4511 Py_file_input,
4512 module_dict, t) ) return -1;
4513 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00004514
4515 UNLESS (PickleError = PyErr_NewException("cPickle.PickleError", NULL, t))
4516 return -1;
4517
4518 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00004519
Tim Peters84e87f32001-03-17 04:50:51 +00004520
4521 UNLESS (PicklingError = PyErr_NewException("cPickle.PicklingError",
Guido van Rossumc03158b1999-06-09 15:23:31 +00004522 PickleError, NULL))
4523 return -1;
4524
4525 UNLESS (t=PyDict_New()) return -1;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004526 UNLESS (r=PyRun_String(
4527 "def __init__(self, *args): self.args=args\n\n"
4528 "def __str__(self):\n"
4529 " a=self.args\n"
4530 " a=a and type(a[0]) or '(what)'\n"
4531 " return 'Cannot pickle %s objects' % a\n"
4532 , Py_file_input,
4533 module_dict, t) ) return -1;
4534 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00004535
4536 UNLESS (UnpickleableError = PyErr_NewException(
4537 "cPickle.UnpickleableError", PicklingError, t))
4538 return -1;
4539
4540 Py_DECREF(t);
4541
Tim Peters84e87f32001-03-17 04:50:51 +00004542 UNLESS (UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
Guido van Rossumc03158b1999-06-09 15:23:31 +00004543 PickleError, NULL))
4544 return -1;
4545
Tim Peters84e87f32001-03-17 04:50:51 +00004546 if (PyDict_SetItemString(module_dict, "PickleError",
Guido van Rossumc03158b1999-06-09 15:23:31 +00004547 PickleError) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004548 return -1;
4549
Tim Peters84e87f32001-03-17 04:50:51 +00004550 if (PyDict_SetItemString(module_dict, "PicklingError",
Guido van Rossum60456fd1997-04-09 17:36:32 +00004551 PicklingError) < 0)
4552 return -1;
4553
Guido van Rossum60456fd1997-04-09 17:36:32 +00004554 if (PyDict_SetItemString(module_dict, "UnpicklingError",
4555 UnpicklingError) < 0)
4556 return -1;
4557
Guido van Rossumc03158b1999-06-09 15:23:31 +00004558 if (PyDict_SetItemString(module_dict, "UnpickleableError",
4559 UnpickleableError) < 0)
4560 return -1;
4561
Guido van Rossum053b8df1998-11-25 16:18:00 +00004562 UNLESS (BadPickleGet = PyString_FromString("cPickle.BadPickleGet"))
4563 return -1;
4564
4565 if (PyDict_SetItemString(module_dict, "BadPickleGet",
4566 BadPickleGet) < 0)
4567 return -1;
4568
Guido van Rossum60456fd1997-04-09 17:36:32 +00004569 PycString_IMPORT;
Tim Peters84e87f32001-03-17 04:50:51 +00004570
Guido van Rossum60456fd1997-04-09 17:36:32 +00004571 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004572}
4573
Guido van Rossumf9ffb031999-02-04 14:54:04 +00004574#ifndef DL_EXPORT /* declarations for DLL import/export */
4575#define DL_EXPORT(RTYPE) RTYPE
4576#endif
Guido van Rossum50f385c1998-12-04 18:48:44 +00004577DL_EXPORT(void)
Thomas Wouters58d05102000-07-24 14:43:35 +00004578initcPickle(void) {
Guido van Rossumebba4202000-09-07 14:35:37 +00004579 PyObject *m, *d, *di, *v, *k;
4580 int i;
Guido van Rossum2f80d961999-07-13 15:18:58 +00004581 char *rev="1.71";
Guido van Rossum60456fd1997-04-09 17:36:32 +00004582 PyObject *format_version;
4583 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004584
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004585 Picklertype.ob_type = &PyType_Type;
4586 Unpicklertype.ob_type = &PyType_Type;
Guido van Rossum053b8df1998-11-25 16:18:00 +00004587 PdataType.ob_type = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004588
Tim Peters84e87f32001-03-17 04:50:51 +00004589 /* Initialize some pieces. We need to do this before module creation,
4590 so we're forced to use a temporary dictionary. :(
Guido van Rossumebba4202000-09-07 14:35:37 +00004591 */
4592 di=PyDict_New();
4593 if (!di) return;
4594 if (init_stuff(di) < 0) return;
4595
Guido van Rossum60456fd1997-04-09 17:36:32 +00004596 /* Create the module and add the functions */
4597 m = Py_InitModule4("cPickle", cPickle_methods,
4598 cPickle_module_documentation,
4599 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004600
Guido van Rossum60456fd1997-04-09 17:36:32 +00004601 /* Add some symbolic constants to the module */
4602 d = PyModule_GetDict(m);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004603 PyDict_SetItemString(d,"__version__", v = PyString_FromString(rev));
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004604 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00004605
Guido van Rossumebba4202000-09-07 14:35:37 +00004606 /* Copy data from di. Waaa. */
4607 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
4608 if (PyObject_SetItem(d, k, v) < 0) {
4609 Py_DECREF(di);
4610 return;
4611 }
4612 }
4613 Py_DECREF(di);
4614
Guido van Rossum60456fd1997-04-09 17:36:32 +00004615 format_version = PyString_FromString("1.3");
4616 compatible_formats = Py_BuildValue("[sss]", "1.0", "1.1", "1.2");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004617
Guido van Rossum60456fd1997-04-09 17:36:32 +00004618 PyDict_SetItemString(d, "format_version", format_version);
4619 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004620 Py_XDECREF(format_version);
4621 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004622}