blob: b27339f568cab7db9ef07245cfecc2f3ce958449 [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 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001872 if (type == &PyType_Type) {
1873 res = save_global(self, args, NULL);
1874 goto finally;
1875 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001876 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001877
Guido van Rossum60456fd1997-04-09 17:36:32 +00001878 case 'l':
1879 if (type == &PyList_Type) {
1880 res = save_list(self, args);
1881 goto finally;
1882 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001883 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001884
1885 case 'd':
1886 if (type == &PyDict_Type) {
1887 res = save_dict(self, args);
Tim Peters84e87f32001-03-17 04:50:51 +00001888 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001889 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001890 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001891
1892 case 'i':
1893 if (type == &PyInstance_Type) {
1894 res = save_inst(self, args);
1895 goto finally;
1896 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001897 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001898
1899 case 'c':
1900 if (type == &PyClass_Type) {
1901 res = save_global(self, args, NULL);
1902 goto finally;
1903 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001904 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001905
1906 case 'f':
1907 if (type == &PyFunction_Type) {
1908 res = save_global(self, args, NULL);
1909 goto finally;
1910 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001911 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001912
1913 case 'b':
1914 if (type == &PyCFunction_Type) {
1915 res = save_global(self, args, NULL);
1916 goto finally;
1917 }
1918 }
1919
1920 if (!pers_save && self->inst_pers_func) {
1921 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
1922 res = tmp;
1923 goto finally;
1924 }
1925 }
1926
Guido van Rossum142eeb81997-08-13 03:14:41 +00001927 if ((__reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001928 Py_INCREF(__reduce__);
1929
Guido van Rossum60456fd1997-04-09 17:36:32 +00001930 Py_INCREF(args);
Guido van Rossum053b8df1998-11-25 16:18:00 +00001931 ARG_TUP(self, args);
1932 if (self->arg) {
1933 t = PyObject_CallObject(__reduce__, self->arg);
1934 FREE_ARG_TUP(self);
1935 }
1936 if (! t) goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001937 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001938 else {
1939 PyErr_Clear();
1940
Guido van Rossum142eeb81997-08-13 03:14:41 +00001941 if ((__reduce__ = PyObject_GetAttr(args, __reduce___str))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001942 UNLESS (t = PyObject_CallObject(__reduce__, empty_tuple))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001943 goto finally;
1944 }
1945 else {
1946 PyErr_Clear();
1947 }
1948 }
1949
1950 if (t) {
1951 if (PyString_Check(t)) {
1952 res = save_global(self, args, t);
1953 goto finally;
1954 }
Tim Peters84e87f32001-03-17 04:50:51 +00001955
Guido van Rossum60456fd1997-04-09 17:36:32 +00001956 if (!PyTuple_Check(t)) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001957 cPickle_ErrFormat(PicklingError, "Value returned by %s must "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001958 "be a tuple", "O", __reduce__);
1959 goto finally;
1960 }
1961
1962 size = PyTuple_Size(t);
Tim Peters84e87f32001-03-17 04:50:51 +00001963
Guido van Rossum60456fd1997-04-09 17:36:32 +00001964 if ((size != 3) && (size != 2)) {
Tim Peters84e87f32001-03-17 04:50:51 +00001965 cPickle_ErrFormat(PicklingError, "tuple returned by %s must "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001966 "contain only two or three elements", "O", __reduce__);
1967 goto finally;
1968 }
Tim Peters84e87f32001-03-17 04:50:51 +00001969
Guido van Rossum60456fd1997-04-09 17:36:32 +00001970 callable = PyTuple_GET_ITEM(t, 0);
Guido van Rossum142eeb81997-08-13 03:14:41 +00001971
Guido van Rossum60456fd1997-04-09 17:36:32 +00001972 arg_tup = PyTuple_GET_ITEM(t, 1);
1973
1974 if (size > 2) {
1975 state = PyTuple_GET_ITEM(t, 2);
1976 }
1977
Guido van Rossum053b8df1998-11-25 16:18:00 +00001978 UNLESS (PyTuple_Check(arg_tup) || arg_tup==Py_None) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001979 cPickle_ErrFormat(PicklingError, "Second element of tuple "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001980 "returned by %s must be a tuple", "O", __reduce__);
1981 goto finally;
1982 }
1983
1984 res = save_reduce(self, callable, arg_tup, state, args);
1985 goto finally;
1986 }
1987
Guido van Rossumc03158b1999-06-09 15:23:31 +00001988 PyErr_SetObject(UnpickleableError, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001989
1990finally:
1991 Py_XDECREF(py_ob_id);
1992 Py_XDECREF(__reduce__);
1993 Py_XDECREF(t);
Tim Peters84e87f32001-03-17 04:50:51 +00001994
Guido van Rossum60456fd1997-04-09 17:36:32 +00001995 return res;
1996}
1997
1998
1999static int
2000dump(Picklerobject *self, PyObject *args) {
2001 static char stop = STOP;
2002
2003 if (save(self, args, 0) < 0)
2004 return -1;
2005
2006 if ((*self->write_func)(self, &stop, 1) < 0)
2007 return -1;
2008
2009 if ((*self->write_func)(self, NULL, 0) < 0)
2010 return -1;
2011
2012 return 0;
2013}
2014
2015static PyObject *
Guido van Rossum053b8df1998-11-25 16:18:00 +00002016Pickle_clear_memo(Picklerobject *self, PyObject *args) {
Guido van Rossum43713e52000-02-29 13:59:29 +00002017 if (args && ! PyArg_ParseTuple(args,":clear_memo")) return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002018 if (self->memo) PyDict_Clear(self->memo);
2019 Py_INCREF(Py_None);
2020 return Py_None;
2021}
2022
2023static PyObject *
2024Pickle_getvalue(Picklerobject *self, PyObject *args) {
2025 int l, i, rsize, ssize, clear=1, lm;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00002026 long ik;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002027 PyObject *k, *r;
2028 char *s, *p, *have_get;
2029 Pdata *data;
2030
Guido van Rossum43713e52000-02-29 13:59:29 +00002031 if (args && ! PyArg_ParseTuple(args,"|i:getvalue",&clear)) return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002032
2033 /* Check to make sure we are based on a list */
2034 if (! Pdata_Check(self->file)) {
2035 PyErr_SetString(PicklingError,
2036 "Attempt to getvalue a non-list-based pickler");
2037 return NULL;
2038 }
2039
2040 /* flush write buffer */
2041 if (write_other(self, NULL, 0) < 0) return NULL;
2042
2043 data=(Pdata*)self->file;
2044 l=data->length;
2045
2046 /* set up an array to hold get/put status */
2047 if ((lm=PyDict_Size(self->memo)) < 0) return NULL;
2048 lm++;
Tim Peters84e87f32001-03-17 04:50:51 +00002049 if (! (have_get=malloc((lm)*sizeof(char)))) return PyErr_NoMemory();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002050 memset(have_get,0,lm);
2051
2052 /* Scan for gets. */
2053 for (rsize=0, i=l; --i >= 0; ) {
2054 k=data->data[i];
Tim Peters84e87f32001-03-17 04:50:51 +00002055
Guido van Rossum053b8df1998-11-25 16:18:00 +00002056 if (PyString_Check(k)) {
2057 rsize += PyString_GET_SIZE(k);
2058 }
2059
2060 else if (PyInt_Check(k)) { /* put */
2061 ik=PyInt_AS_LONG((PyIntObject*)k);
2062 if (ik >= lm || ik==0) {
2063 PyErr_SetString(PicklingError,
2064 "Invalid get data");
2065 return NULL;
Tim Peters84e87f32001-03-17 04:50:51 +00002066 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002067 if (have_get[ik]) { /* with matching get */
2068 if (ik < 256) rsize += 2;
2069 else rsize+=5;
2070 }
2071 }
2072
2073 else if (! (PyTuple_Check(k) &&
2074 PyTuple_GET_SIZE(k) == 2 &&
2075 PyInt_Check((k=PyTuple_GET_ITEM(k,0))))
2076 ) {
2077 PyErr_SetString(PicklingError,
2078 "Unexpected data in internal list");
2079 return NULL;
2080 }
2081
2082 else { /* put */
2083 ik=PyInt_AS_LONG((PyIntObject*)k);
2084 if (ik >= lm || ik==0) {
2085 PyErr_SetString(PicklingError,
2086 "Invalid get data");
2087 return NULL;
Tim Peters84e87f32001-03-17 04:50:51 +00002088 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002089 have_get[ik]=1;
2090 if (ik < 256) rsize += 2;
2091 else rsize+=5;
2092 }
2093
2094 }
2095
2096 /* Now generate the result */
2097 UNLESS (r=PyString_FromStringAndSize(NULL,rsize)) goto err;
2098 s=PyString_AS_STRING((PyStringObject*)r);
2099
2100 for (i=0; i<l; i++) {
2101 k=data->data[i];
2102
2103 if (PyString_Check(k)) {
2104 ssize=PyString_GET_SIZE(k);
2105 if (ssize) {
2106 p=PyString_AS_STRING((PyStringObject*)k);
2107 while (--ssize >= 0) *s++=*p++;
2108 }
2109 }
2110
2111 else if (PyTuple_Check(k)) { /* get */
2112 ik=PyInt_AS_LONG((PyIntObject*)PyTuple_GET_ITEM(k,0));
2113 if (ik < 256) {
2114 *s++ = BINGET;
2115 *s++ = (int)(ik & 0xff);
2116 }
2117 else {
2118 *s++ = LONG_BINGET;
2119 *s++ = (int)(ik & 0xff);
2120 *s++ = (int)((ik >> 8) & 0xff);
2121 *s++ = (int)((ik >> 16) & 0xff);
2122 *s++ = (int)((ik >> 24) & 0xff);
2123 }
2124 }
2125
2126 else { /* put */
2127 ik=PyInt_AS_LONG((PyIntObject*)k);
2128
2129 if (have_get[ik]) { /* with matching get */
2130 if (ik < 256) {
2131 *s++ = BINPUT;
2132 *s++ = (int)(ik & 0xff);
2133 }
2134 else {
2135 *s++ = LONG_BINPUT;
2136 *s++ = (int)(ik & 0xff);
2137 *s++ = (int)((ik >> 8) & 0xff);
2138 *s++ = (int)((ik >> 16) & 0xff);
2139 *s++ = (int)((ik >> 24) & 0xff);
2140 }
2141 }
2142 }
2143
2144 }
2145
2146 if (clear) {
2147 PyDict_Clear(self->memo);
2148 Pdata_clear(data,0);
2149 }
Tim Peters84e87f32001-03-17 04:50:51 +00002150
Guido van Rossum053b8df1998-11-25 16:18:00 +00002151 free(have_get);
2152 return r;
2153err:
2154 free(have_get);
2155 return NULL;
2156}
2157
2158static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002159Pickler_dump(Picklerobject *self, PyObject *args) {
2160 PyObject *ob;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002161 int get=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002162
Guido van Rossum43713e52000-02-29 13:59:29 +00002163 UNLESS (PyArg_ParseTuple(args, "O|i:dump", &ob, &get))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002164 return NULL;
2165
2166 if (dump(self, ob) < 0)
2167 return NULL;
2168
Guido van Rossum053b8df1998-11-25 16:18:00 +00002169 if (get) return Pickle_getvalue(self, NULL);
2170
2171 Py_INCREF(self);
2172 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002173}
2174
2175
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002176static struct PyMethodDef Pickler_methods[] = {
Guido van Rossum142eeb81997-08-13 03:14:41 +00002177 {"dump", (PyCFunction)Pickler_dump, 1,
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002178 "dump(object) --"
2179 "Write an object in pickle format to the object's pickle stream\n"
2180 },
Guido van Rossum142eeb81997-08-13 03:14:41 +00002181 {"clear_memo", (PyCFunction)Pickle_clear_memo, 1,
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002182 "clear_memo() -- Clear the picklers memo"},
Guido van Rossum053b8df1998-11-25 16:18:00 +00002183 {"getvalue", (PyCFunction)Pickle_getvalue, 1,
2184 "getvalue() -- Finish picking a list-based pickle"},
Guido van Rossum60456fd1997-04-09 17:36:32 +00002185 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002186};
2187
2188
2189static Picklerobject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002190newPicklerobject(PyObject *file, int bin) {
2191 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002192
Guido van Rossumb18618d2000-05-03 23:44:39 +00002193 UNLESS (self = PyObject_New(Picklerobject, &Picklertype))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002194 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002195
2196 self->fp = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002197 self->write = NULL;
2198 self->memo = NULL;
2199 self->arg = NULL;
2200 self->pers_func = NULL;
2201 self->inst_pers_func = NULL;
2202 self->write_buf = NULL;
2203 self->bin = bin;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002204 self->fast = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002205 self->buf_size = 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002206 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002207
Guido van Rossum053b8df1998-11-25 16:18:00 +00002208 if (file)
2209 Py_INCREF(file);
2210 else
Guido van Rossum50f385c1998-12-04 18:48:44 +00002211 file=Pdata_New();
Tim Peters84e87f32001-03-17 04:50:51 +00002212
2213 UNLESS (self->file = file)
Guido van Rossum83addc72000-04-21 20:49:36 +00002214 goto err;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002215
Tim Peters84e87f32001-03-17 04:50:51 +00002216 UNLESS (self->memo = PyDict_New())
Guido van Rossum83addc72000-04-21 20:49:36 +00002217 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002218
Guido van Rossum60456fd1997-04-09 17:36:32 +00002219 if (PyFile_Check(file)) {
2220 self->fp = PyFile_AsFile(file);
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002221 if (self->fp == NULL) {
Guido van Rossum83addc72000-04-21 20:49:36 +00002222 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
2223 goto err;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002224 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002225 self->write_func = write_file;
2226 }
2227 else if (PycStringIO_OutputCheck(file)) {
2228 self->write_func = write_cStringIO;
2229 }
Guido van Rossum142eeb81997-08-13 03:14:41 +00002230 else if (file == Py_None) {
2231 self->write_func = write_none;
2232 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002233 else {
2234 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002235
Guido van Rossum053b8df1998-11-25 16:18:00 +00002236 if (! Pdata_Check(file)) {
2237 UNLESS (self->write = PyObject_GetAttr(file, write_str)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002238 PyErr_Clear();
2239 PyErr_SetString(PyExc_TypeError, "argument must have 'write' "
Guido van Rossum053b8df1998-11-25 16:18:00 +00002240 "attribute");
2241 goto err;
2242 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002243 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002244
Tim Peters84e87f32001-03-17 04:50:51 +00002245 UNLESS (self->write_buf =
2246 (char *)malloc(WRITE_BUF_SIZE * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002247 PyErr_NoMemory();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002248 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002249 }
2250 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002251
Guido van Rossum053b8df1998-11-25 16:18:00 +00002252 if (PyEval_GetRestricted()) {
2253 /* Restricted execution, get private tables */
2254 PyObject *m;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002255
Guido van Rossum053b8df1998-11-25 16:18:00 +00002256 UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
2257 self->dispatch_table=PyObject_GetAttr(m, dispatch_table_str);
2258 Py_DECREF(m);
2259 UNLESS (self->dispatch_table) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002260 }
2261 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002262 self->dispatch_table=dispatch_table;
2263 Py_INCREF(dispatch_table);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002264 }
2265
Guido van Rossum60456fd1997-04-09 17:36:32 +00002266 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002267
2268err:
2269 Py_DECREF((PyObject *)self);
2270 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002271}
2272
2273
2274static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002275get_Pickler(PyObject *self, PyObject *args) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002276 PyObject *file=NULL;
2277 int bin;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002278
Guido van Rossum053b8df1998-11-25 16:18:00 +00002279 bin=1;
Guido van Rossum43713e52000-02-29 13:59:29 +00002280 if (! PyArg_ParseTuple(args, "|i:Pickler", &bin)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002281 PyErr_Clear();
2282 bin=0;
Guido van Rossum43713e52000-02-29 13:59:29 +00002283 if (! PyArg_ParseTuple(args, "O|i:Pickler", &file, &bin))
Guido van Rossum053b8df1998-11-25 16:18:00 +00002284 return NULL;
2285 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002286 return (PyObject *)newPicklerobject(file, bin);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002287}
2288
2289
2290static void
Guido van Rossum60456fd1997-04-09 17:36:32 +00002291Pickler_dealloc(Picklerobject *self) {
2292 Py_XDECREF(self->write);
2293 Py_XDECREF(self->memo);
2294 Py_XDECREF(self->arg);
2295 Py_XDECREF(self->file);
2296 Py_XDECREF(self->pers_func);
2297 Py_XDECREF(self->inst_pers_func);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002298 Py_XDECREF(self->dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002299
Tim Peters84e87f32001-03-17 04:50:51 +00002300 if (self->write_buf) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002301 free(self->write_buf);
2302 }
2303
Guido van Rossumb18618d2000-05-03 23:44:39 +00002304 PyObject_Del(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002305}
2306
2307
2308static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002309Pickler_getattr(Picklerobject *self, char *name) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002310
2311 switch (*name) {
2312 case 'p':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002313 if (strcmp(name, "persistent_id") == 0) {
2314 if (!self->pers_func) {
2315 PyErr_SetString(PyExc_AttributeError, name);
2316 return NULL;
2317 }
2318
2319 Py_INCREF(self->pers_func);
2320 return self->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002321 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002322 break;
2323 case 'm':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002324 if (strcmp(name, "memo") == 0) {
2325 if (!self->memo) {
2326 PyErr_SetString(PyExc_AttributeError, name);
2327 return NULL;
2328 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002329
Guido van Rossum60456fd1997-04-09 17:36:32 +00002330 Py_INCREF(self->memo);
2331 return self->memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002332 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002333 break;
2334 case 'P':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002335 if (strcmp(name, "PicklingError") == 0) {
2336 Py_INCREF(PicklingError);
2337 return PicklingError;
2338 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002339 break;
2340 case 'b':
2341 if (strcmp(name, "binary")==0)
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002342 return PyInt_FromLong(self->bin);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002343 break;
2344 case 'f':
2345 if (strcmp(name, "fast")==0)
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002346 return PyInt_FromLong(self->fast);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002347 break;
2348 case 'g':
2349 if (strcmp(name, "getvalue")==0 && ! Pdata_Check(self->file)) {
2350 PyErr_SetString(PyExc_AttributeError, name);
2351 return NULL;
2352 }
2353 break;
2354 }
2355 return Py_FindMethod(Pickler_methods, (PyObject *)self, name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002356}
2357
2358
Tim Peters84e87f32001-03-17 04:50:51 +00002359int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002360Pickler_setattr(Picklerobject *self, char *name, PyObject *value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002361
Guido van Rossum053b8df1998-11-25 16:18:00 +00002362 if (! value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002363 PyErr_SetString(PyExc_TypeError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00002364 "attribute deletion is not supported");
2365 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002366 }
Tim Peters84e87f32001-03-17 04:50:51 +00002367
Guido van Rossum60456fd1997-04-09 17:36:32 +00002368 if (strcmp(name, "persistent_id") == 0) {
2369 Py_XDECREF(self->pers_func);
2370 self->pers_func = value;
2371 Py_INCREF(value);
2372 return 0;
2373 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002374
Guido van Rossum60456fd1997-04-09 17:36:32 +00002375 if (strcmp(name, "inst_persistent_id") == 0) {
2376 Py_XDECREF(self->inst_pers_func);
2377 self->inst_pers_func = value;
2378 Py_INCREF(value);
2379 return 0;
2380 }
2381
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002382 if (strcmp(name, "memo") == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002383 if (! PyDict_Check(value)) {
2384 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
2385 return -1;
2386 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002387 Py_XDECREF(self->memo);
2388 self->memo = value;
2389 Py_INCREF(value);
2390 return 0;
2391 }
2392
Guido van Rossum053b8df1998-11-25 16:18:00 +00002393 if (strcmp(name, "binary")==0) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002394 self->bin=PyObject_IsTrue(value);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002395 return 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002396 }
2397
Guido van Rossum053b8df1998-11-25 16:18:00 +00002398 if (strcmp(name, "fast")==0) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002399 self->fast=PyObject_IsTrue(value);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002400 return 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002401 }
2402
Guido van Rossum60456fd1997-04-09 17:36:32 +00002403 PyErr_SetString(PyExc_AttributeError, name);
2404 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002405}
2406
2407
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002408static char Picklertype__doc__[] =
2409"Objects that know how to pickle objects\n"
2410;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002411
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002412static PyTypeObject Picklertype = {
2413 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00002414 0, /*ob_size*/
2415 "Pickler", /*tp_name*/
2416 sizeof(Picklerobject), /*tp_basicsize*/
2417 0, /*tp_itemsize*/
2418 /* methods */
2419 (destructor)Pickler_dealloc, /*tp_dealloc*/
2420 (printfunc)0, /*tp_print*/
2421 (getattrfunc)Pickler_getattr, /*tp_getattr*/
2422 (setattrfunc)Pickler_setattr, /*tp_setattr*/
2423 (cmpfunc)0, /*tp_compare*/
2424 (reprfunc)0, /*tp_repr*/
2425 0, /*tp_as_number*/
2426 0, /*tp_as_sequence*/
2427 0, /*tp_as_mapping*/
2428 (hashfunc)0, /*tp_hash*/
2429 (ternaryfunc)0, /*tp_call*/
2430 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002431
Guido van Rossum60456fd1997-04-09 17:36:32 +00002432 /* Space for future expansion */
2433 0L,0L,0L,0L,
2434 Picklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002435};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002436
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002437static PyObject *
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002438find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc) {
Guido van Rossume2d81cd1998-08-08 19:40:10 +00002439 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002440
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002441 if (fc) {
2442 if (fc==Py_None) {
Tim Peters84e87f32001-03-17 04:50:51 +00002443 PyErr_SetString(UnpicklingError,
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002444 "Global and instance pickles are not supported.");
2445 return NULL;
2446 }
2447 return PyObject_CallFunction(fc, "OO", py_module_name, py_global_name);
2448 }
2449
Jeremy Hyltond1055231998-08-11 19:52:51 +00002450 module = PySys_GetObject("modules");
2451 if (module == NULL)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002452 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00002453
2454 module = PyDict_GetItem(module, py_module_name);
2455 if (module == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002456 module = PyImport_Import(py_module_name);
2457 if (!module)
2458 return NULL;
2459 global = PyObject_GetAttr(module, py_global_name);
2460 Py_DECREF(module);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002461 }
Jeremy Hyltond1055231998-08-11 19:52:51 +00002462 else
Guido van Rossum053b8df1998-11-25 16:18:00 +00002463 global = PyObject_GetAttr(module, py_global_name);
Jeremy Hyltond1055231998-08-11 19:52:51 +00002464 if (global == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002465 char buf[256 + 37];
2466 sprintf(buf, "Failed to import class %.128s from module %.128s",
2467 PyString_AS_STRING((PyStringObject*)py_global_name),
Tim Peters84e87f32001-03-17 04:50:51 +00002468 PyString_AS_STRING((PyStringObject*)py_module_name));
Guido van Rossum053b8df1998-11-25 16:18:00 +00002469 PyErr_SetString(PyExc_SystemError, buf);
2470 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00002471 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002472 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002473}
2474
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002475static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002476marker(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002477 if (self->num_marks < 1) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002478 PyErr_SetString(UnpicklingError, "could not find MARK");
2479 return -1;
2480 }
2481
2482 return self->marks[--self->num_marks];
2483}
2484
Tim Peters84e87f32001-03-17 04:50:51 +00002485
Guido van Rossum60456fd1997-04-09 17:36:32 +00002486static int
2487load_none(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002488 PDATA_APPEND(self->stack, Py_None, -1);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002489 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002490}
2491
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002492static int
Thomas Wouters58d05102000-07-24 14:43:35 +00002493bad_readline(void) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002494 PyErr_SetString(UnpicklingError, "pickle data was truncated");
2495 return -1;
2496}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002497
2498static int
2499load_int(Unpicklerobject *self) {
2500 PyObject *py_int = 0;
2501 char *endptr, *s;
2502 int len, res = -1;
2503 long l;
2504
2505 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002506 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002507 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002508
2509 errno = 0;
2510 l = strtol(s, &endptr, 0);
2511
2512 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
2513 /* Hm, maybe we've got something long. Let's try reading
Guido van Rossum053b8df1998-11-25 16:18:00 +00002514 it as a Python long object. */
Guido van Rossum60456fd1997-04-09 17:36:32 +00002515 errno=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002516 UNLESS (py_int=PyLong_FromString(s,&endptr,0)) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002517
Guido van Rossum053b8df1998-11-25 16:18:00 +00002518 if ((*endptr != '\n') || (endptr[1] != '\0')) {
2519 PyErr_SetString(PyExc_ValueError,
2520 "could not convert string to int");
2521 goto finally;
2522 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002523 }
2524 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002525 UNLESS (py_int = PyInt_FromLong(l)) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002526 }
2527
Guido van Rossum053b8df1998-11-25 16:18:00 +00002528 free(s);
2529 PDATA_PUSH(self->stack, py_int, -1);
2530 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002531
2532finally:
2533 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002534
2535 return res;
2536}
2537
2538
Tim Peters84e87f32001-03-17 04:50:51 +00002539static long
Guido van Rossum60456fd1997-04-09 17:36:32 +00002540calc_binint(char *s, int x) {
2541 unsigned char c;
2542 int i;
2543 long l;
2544
2545 for (i = 0, l = 0L; i < x; i++) {
2546 c = (unsigned char)s[i];
2547 l |= (long)c << (i * 8);
2548 }
Tim Petersbfa18f72001-04-10 01:54:42 +00002549#if SIZEOF_LONG > 4
2550 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
2551 * is signed, so on a box with longs bigger than 4 bytes we need
2552 * to extend a BININT's sign bit to the full width.
2553 */
2554 if (x == 4 && l & (1L << 31))
2555 l |= (~0L) << 32;
2556#endif
Guido van Rossum60456fd1997-04-09 17:36:32 +00002557 return l;
2558}
2559
2560
2561static int
2562load_binintx(Unpicklerobject *self, char *s, int x) {
2563 PyObject *py_int = 0;
2564 long l;
2565
2566 l = calc_binint(s, x);
2567
Guido van Rossum053b8df1998-11-25 16:18:00 +00002568 UNLESS (py_int = PyInt_FromLong(l))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002569 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002570
Guido van Rossum053b8df1998-11-25 16:18:00 +00002571 PDATA_PUSH(self->stack, py_int, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002572 return 0;
2573}
2574
2575
2576static int
2577load_binint(Unpicklerobject *self) {
2578 char *s;
2579
2580 if ((*self->read_func)(self, &s, 4) < 0)
2581 return -1;
2582
2583 return load_binintx(self, s, 4);
2584}
2585
2586
2587static int
2588load_binint1(Unpicklerobject *self) {
2589 char *s;
2590
2591 if ((*self->read_func)(self, &s, 1) < 0)
2592 return -1;
2593
2594 return load_binintx(self, s, 1);
2595}
2596
2597
2598static int
2599load_binint2(Unpicklerobject *self) {
2600 char *s;
2601
2602 if ((*self->read_func)(self, &s, 2) < 0)
2603 return -1;
2604
2605 return load_binintx(self, s, 2);
2606}
Tim Peters84e87f32001-03-17 04:50:51 +00002607
Guido van Rossum60456fd1997-04-09 17:36:32 +00002608static int
2609load_long(Unpicklerobject *self) {
2610 PyObject *l = 0;
2611 char *end, *s;
2612 int len, res = -1;
2613
Guido van Rossum60456fd1997-04-09 17:36:32 +00002614 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002615 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002616 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002617
Guido van Rossum053b8df1998-11-25 16:18:00 +00002618 UNLESS (l = PyLong_FromString(s, &end, 0))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002619 goto finally;
2620
Guido van Rossum053b8df1998-11-25 16:18:00 +00002621 free(s);
2622 PDATA_PUSH(self->stack, l, -1);
2623 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002624
2625finally:
2626 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002627
2628 return res;
2629}
2630
Tim Peters84e87f32001-03-17 04:50:51 +00002631
Guido van Rossum60456fd1997-04-09 17:36:32 +00002632static int
2633load_float(Unpicklerobject *self) {
2634 PyObject *py_float = 0;
2635 char *endptr, *s;
2636 int len, res = -1;
2637 double d;
2638
2639 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002640 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002641 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002642
2643 errno = 0;
2644 d = strtod(s, &endptr);
2645
2646 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
Tim Peters84e87f32001-03-17 04:50:51 +00002647 PyErr_SetString(PyExc_ValueError,
Guido van Rossum60456fd1997-04-09 17:36:32 +00002648 "could not convert string to float");
2649 goto finally;
2650 }
2651
Guido van Rossum053b8df1998-11-25 16:18:00 +00002652 UNLESS (py_float = PyFloat_FromDouble(d))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002653 goto finally;
2654
Guido van Rossum053b8df1998-11-25 16:18:00 +00002655 free(s);
2656 PDATA_PUSH(self->stack, py_float, -1);
2657 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002658
2659finally:
2660 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002661
2662 return res;
2663}
2664
Guido van Rossum60456fd1997-04-09 17:36:32 +00002665static int
2666load_binfloat(Unpicklerobject *self) {
2667 PyObject *py_float = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002668 int s, e;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002669 long fhi, flo;
2670 double x;
2671 char *p;
2672
2673 if ((*self->read_func)(self, &p, 8) < 0)
2674 return -1;
2675
2676 /* First byte */
2677 s = (*p>>7) & 1;
2678 e = (*p & 0x7F) << 4;
2679 p++;
2680
2681 /* Second byte */
2682 e |= (*p>>4) & 0xF;
2683 fhi = (*p & 0xF) << 24;
2684 p++;
2685
2686 /* Third byte */
2687 fhi |= (*p & 0xFF) << 16;
2688 p++;
2689
2690 /* Fourth byte */
2691 fhi |= (*p & 0xFF) << 8;
2692 p++;
2693
2694 /* Fifth byte */
2695 fhi |= *p & 0xFF;
2696 p++;
2697
2698 /* Sixth byte */
2699 flo = (*p & 0xFF) << 16;
2700 p++;
2701
2702 /* Seventh byte */
2703 flo |= (*p & 0xFF) << 8;
2704 p++;
2705
2706 /* Eighth byte */
2707 flo |= *p & 0xFF;
2708
2709 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
2710 x /= 268435456.0; /* 2**28 */
2711
2712 /* XXX This sadly ignores Inf/NaN */
2713 if (e == 0)
2714 e = -1022;
2715 else {
2716 x += 1.0;
2717 e -= 1023;
2718 }
2719 x = ldexp(x, e);
2720
2721 if (s)
2722 x = -x;
2723
Guido van Rossum053b8df1998-11-25 16:18:00 +00002724 UNLESS (py_float = PyFloat_FromDouble(x)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002725
Guido van Rossum053b8df1998-11-25 16:18:00 +00002726 PDATA_PUSH(self->stack, py_float, -1);
2727 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002728}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002729
2730static int
2731load_string(Unpicklerobject *self) {
2732 PyObject *str = 0;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002733 int len, res = -1, nslash;
2734 char *s, q, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002735
2736 static PyObject *eval_dict = 0;
2737
2738 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002739 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002740 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002741
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002742 /* Check for unquoted quotes (evil strings) */
2743 q=*s;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002744 if (q != '"' && q != '\'') goto insecure;
2745 for (p=s+1, nslash=0; *p; p++) {
2746 if (*p==q && nslash%2==0) break;
2747 if (*p=='\\') nslash++;
2748 else nslash=0;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002749 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002750 if (*p==q)
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002751 {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002752 for (p++; *p; p++) if (*p > ' ') goto insecure;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002753 }
2754 else goto insecure;
2755 /********************************************/
2756
Guido van Rossum053b8df1998-11-25 16:18:00 +00002757 UNLESS (eval_dict)
2758 UNLESS (eval_dict = Py_BuildValue("{s{}}", "__builtins__"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002759 goto finally;
2760
Guido van Rossum053b8df1998-11-25 16:18:00 +00002761 UNLESS (str = PyRun_String(s, Py_eval_input, eval_dict, eval_dict))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002762 goto finally;
2763
Guido van Rossum053b8df1998-11-25 16:18:00 +00002764 free(s);
2765 PDATA_PUSH(self->stack, str, -1);
2766 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002767
2768finally:
2769 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002770
2771 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00002772
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002773insecure:
2774 free(s);
2775 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
2776 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00002777}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002778
2779
2780static int
2781load_binstring(Unpicklerobject *self) {
2782 PyObject *py_string = 0;
2783 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002784 char *s;
2785
Guido van Rossum053b8df1998-11-25 16:18:00 +00002786 if ((*self->read_func)(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002787
2788 l = calc_binint(s, 4);
2789
2790 if ((*self->read_func)(self, &s, l) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002791 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002792
Guido van Rossum053b8df1998-11-25 16:18:00 +00002793 UNLESS (py_string = PyString_FromStringAndSize(s, l))
2794 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002795
Guido van Rossum053b8df1998-11-25 16:18:00 +00002796 PDATA_PUSH(self->stack, py_string, -1);
2797 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002798}
2799
2800
2801static int
2802load_short_binstring(Unpicklerobject *self) {
2803 PyObject *py_string = 0;
Tim Peters84e87f32001-03-17 04:50:51 +00002804 unsigned char l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002805 char *s;
2806
2807 if ((*self->read_func)(self, &s, 1) < 0)
2808 return -1;
2809
2810 l = (unsigned char)s[0];
2811
Guido van Rossum053b8df1998-11-25 16:18:00 +00002812 if ((*self->read_func)(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002813
Guido van Rossum053b8df1998-11-25 16:18:00 +00002814 UNLESS (py_string = PyString_FromStringAndSize(s, l)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002815
Guido van Rossum053b8df1998-11-25 16:18:00 +00002816 PDATA_PUSH(self->stack, py_string, -1);
2817 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00002818}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002819
2820
2821static int
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002822load_unicode(Unpicklerobject *self) {
2823 PyObject *str = 0;
2824 int len, res = -1;
2825 char *s;
2826
2827 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00002828 if (len < 1) return bad_readline();
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002829
2830 UNLESS (str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL))
2831 goto finally;
2832
2833 PDATA_PUSH(self->stack, str, -1);
2834 return 0;
2835
2836finally:
2837 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00002838}
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002839
2840
2841static int
2842load_binunicode(Unpicklerobject *self) {
2843 PyObject *unicode;
2844 long l;
2845 char *s;
2846
2847 if ((*self->read_func)(self, &s, 4) < 0) return -1;
2848
2849 l = calc_binint(s, 4);
2850
2851 if ((*self->read_func)(self, &s, l) < 0)
2852 return -1;
2853
2854 UNLESS (unicode = PyUnicode_DecodeUTF8(s, l, NULL))
2855 return -1;
2856
2857 PDATA_PUSH(self->stack, unicode, -1);
2858 return 0;
2859}
2860
2861
2862static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002863load_tuple(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002864 PyObject *tup;
2865 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002866
Guido van Rossum053b8df1998-11-25 16:18:00 +00002867 if ((i = marker(self)) < 0) return -1;
2868 UNLESS (tup=Pdata_popTuple(self->stack, i)) return -1;
2869 PDATA_PUSH(self->stack, tup, -1);
2870 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002871}
2872
2873static int
2874load_empty_tuple(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002875 PyObject *tup;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002876
Guido van Rossum053b8df1998-11-25 16:18:00 +00002877 UNLESS (tup=PyTuple_New(0)) return -1;
2878 PDATA_PUSH(self->stack, tup, -1);
2879 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002880}
2881
2882static int
2883load_empty_list(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002884 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002885
Guido van Rossum053b8df1998-11-25 16:18:00 +00002886 UNLESS (list=PyList_New(0)) return -1;
2887 PDATA_PUSH(self->stack, list, -1);
2888 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002889}
2890
2891static int
2892load_empty_dict(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002893 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002894
Guido van Rossum053b8df1998-11-25 16:18:00 +00002895 UNLESS (dict=PyDict_New()) return -1;
2896 PDATA_PUSH(self->stack, dict, -1);
2897 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002898}
2899
2900
2901static int
2902load_list(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002903 PyObject *list = 0;
2904 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002905
Guido van Rossum053b8df1998-11-25 16:18:00 +00002906 if ((i = marker(self)) < 0) return -1;
2907 UNLESS (list=Pdata_popList(self->stack, i)) return -1;
2908 PDATA_PUSH(self->stack, list, -1);
2909 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002910}
2911
2912static int
2913load_dict(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002914 PyObject *dict, *key, *value;
2915 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002916
Guido van Rossum053b8df1998-11-25 16:18:00 +00002917 if ((i = marker(self)) < 0) return -1;
2918 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002919
Guido van Rossum053b8df1998-11-25 16:18:00 +00002920 UNLESS (dict = PyDict_New()) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002921
Guido van Rossum053b8df1998-11-25 16:18:00 +00002922 for (k = i+1; k < j; k += 2) {
2923 key =self->stack->data[k-1];
2924 value=self->stack->data[k ];
2925 if (PyDict_SetItem(dict, key, value) < 0) {
2926 Py_DECREF(dict);
2927 return -1;
2928 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002929 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002930 Pdata_clear(self->stack, i);
2931 PDATA_PUSH(self->stack, dict, -1);
2932 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002933}
2934
2935static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002936Instance_New(PyObject *cls, PyObject *args) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002937 int has_key;
2938 PyObject *safe=0, *r=0;
2939
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002940 if (PyClass_Check(cls)) {
2941 int l;
Tim Peters84e87f32001-03-17 04:50:51 +00002942
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002943 if ((l=PyObject_Size(args)) < 0) goto err;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002944 UNLESS (l) {
2945 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002946
Guido van Rossum053b8df1998-11-25 16:18:00 +00002947 UNLESS (__getinitargs__=PyObject_GetAttr(cls, __getinitargs___str)) {
2948 /* We have a class with no __getinitargs__, so bypass usual
2949 construction */
Fred Drake2c773552001-03-22 17:52:17 +00002950 PyObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002951
Guido van Rossum053b8df1998-11-25 16:18:00 +00002952 PyErr_Clear();
Fred Drake2c773552001-03-22 17:52:17 +00002953 UNLESS (inst=PyInstance_NewRaw(cls, NULL))
Guido van Rossum053b8df1998-11-25 16:18:00 +00002954 goto err;
Fred Drake2c773552001-03-22 17:52:17 +00002955 return inst;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002956 }
2957 Py_DECREF(__getinitargs__);
2958 }
Tim Peters84e87f32001-03-17 04:50:51 +00002959
Guido van Rossum053b8df1998-11-25 16:18:00 +00002960 if ((r=PyInstance_New(cls, args, NULL))) return r;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002961 else goto err;
2962 }
Tim Peters84e87f32001-03-17 04:50:51 +00002963
2964
Guido van Rossum60456fd1997-04-09 17:36:32 +00002965 if ((has_key = cPickle_PyMapping_HasKey(safe_constructors, cls)) < 0)
2966 goto err;
Tim Peters84e87f32001-03-17 04:50:51 +00002967
Guido van Rossum60456fd1997-04-09 17:36:32 +00002968 if (!has_key)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002969 if (!(safe = PyObject_GetAttr(cls, __safe_for_unpickling___str)) ||
Guido van Rossum60456fd1997-04-09 17:36:32 +00002970 !PyObject_IsTrue(safe)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002971 cPickle_ErrFormat(UnpicklingError,
2972 "%s is not safe for unpickling", "O", cls);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002973 Py_XDECREF(safe);
2974 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002975 }
2976
Guido van Rossum053b8df1998-11-25 16:18:00 +00002977 if (args==Py_None) {
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002978 /* Special case, call cls.__basicnew__() */
2979 PyObject *basicnew;
Tim Peters84e87f32001-03-17 04:50:51 +00002980
Guido van Rossum053b8df1998-11-25 16:18:00 +00002981 UNLESS (basicnew=PyObject_GetAttr(cls, __basicnew___str)) return NULL;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002982 r=PyObject_CallObject(basicnew, NULL);
2983 Py_DECREF(basicnew);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002984 if (r) return r;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002985 }
2986
Guido van Rossum053b8df1998-11-25 16:18:00 +00002987 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00002988
Guido van Rossum60456fd1997-04-09 17:36:32 +00002989err:
2990 {
2991 PyObject *tp, *v, *tb;
2992
2993 PyErr_Fetch(&tp, &v, &tb);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002994 if ((r=Py_BuildValue("OOO",v,cls,args))) {
2995 Py_XDECREF(v);
2996 v=r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002997 }
2998 PyErr_Restore(tp,v,tb);
2999 }
3000 return NULL;
3001}
Tim Peters84e87f32001-03-17 04:50:51 +00003002
Guido van Rossum60456fd1997-04-09 17:36:32 +00003003
3004static int
3005load_obj(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003006 PyObject *class, *tup, *obj=0;
3007 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003008
Guido van Rossum053b8df1998-11-25 16:18:00 +00003009 if ((i = marker(self)) < 0) return -1;
3010 UNLESS (tup=Pdata_popTuple(self->stack, i+1)) return -1;
3011 PDATA_POP(self->stack, class);
3012 if (class) {
3013 obj = Instance_New(class, tup);
3014 Py_DECREF(class);
3015 }
3016 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003017
Guido van Rossum053b8df1998-11-25 16:18:00 +00003018 if (! obj) return -1;
3019 PDATA_PUSH(self->stack, obj, -1);
3020 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003021}
3022
3023
3024static int
3025load_inst(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003026 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003027 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003028 char *s;
3029
Guido van Rossum053b8df1998-11-25 16:18:00 +00003030 if ((i = marker(self)) < 0) 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) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003033 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00003034 UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003035
Guido van Rossum053b8df1998-11-25 16:18:00 +00003036 if ((len = (*self->readline_func)(self, &s)) >= 0) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003037 if (len < 2) return bad_readline();
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003038 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003039 class = find_class(module_name, class_name, self->find_class);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003040 Py_DECREF(class_name);
3041 }
3042 }
3043 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003044
Guido van Rossum053b8df1998-11-25 16:18:00 +00003045 if (! class) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003046
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003047 if ((tup=Pdata_popTuple(self->stack, i))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003048 obj = Instance_New(class, tup);
3049 Py_DECREF(tup);
3050 }
3051 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003052
Guido van Rossum053b8df1998-11-25 16:18:00 +00003053 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003054
Guido van Rossum053b8df1998-11-25 16:18:00 +00003055 PDATA_PUSH(self->stack, obj, -1);
3056 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003057}
3058
3059
3060static int
3061load_global(Unpicklerobject *self) {
3062 PyObject *class = 0, *module_name = 0, *class_name = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003063 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003064 char *s;
3065
Guido van Rossum053b8df1998-11-25 16:18:00 +00003066 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003067 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00003068 UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003069
Guido van Rossum053b8df1998-11-25 16:18:00 +00003070 if ((len = (*self->readline_func)(self, &s)) >= 0) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003071 if (len < 2) return bad_readline();
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003072 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003073 class = find_class(module_name, class_name, self->find_class);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003074 Py_DECREF(class_name);
3075 }
3076 }
3077 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003078
Guido van Rossum053b8df1998-11-25 16:18:00 +00003079 if (! class) return -1;
3080 PDATA_PUSH(self->stack, class, -1);
3081 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003082}
3083
3084
3085static int
3086load_persid(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003087 PyObject *pid = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003088 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003089 char *s;
3090
3091 if (self->pers_func) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003092 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003093 if (len < 2) return bad_readline();
Tim Peters84e87f32001-03-17 04:50:51 +00003094
Guido van Rossum053b8df1998-11-25 16:18:00 +00003095 UNLESS (pid = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003096
Guido van Rossum053b8df1998-11-25 16:18:00 +00003097 if (PyList_Check(self->pers_func)) {
3098 if (PyList_Append(self->pers_func, pid) < 0) {
3099 Py_DECREF(pid);
3100 return -1;
3101 }
3102 }
3103 else {
3104 ARG_TUP(self, pid);
3105 if (self->arg) {
3106 pid = PyObject_CallObject(self->pers_func, self->arg);
3107 FREE_ARG_TUP(self);
3108 }
3109 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003110
Guido van Rossum053b8df1998-11-25 16:18:00 +00003111 if (! pid) return -1;
3112
3113 PDATA_PUSH(self->stack, pid, -1);
3114 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003115 }
3116 else {
3117 PyErr_SetString(UnpicklingError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00003118 "A load persistent id instruction was encountered,\n"
3119 "but no persistent_load function was specified.");
3120 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003121 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003122}
3123
Guido van Rossum60456fd1997-04-09 17:36:32 +00003124static int
3125load_binpersid(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003126 PyObject *pid = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003127
3128 if (self->pers_func) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003129 PDATA_POP(self->stack, pid);
3130 if (! pid) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003131
Guido van Rossum053b8df1998-11-25 16:18:00 +00003132 if (PyList_Check(self->pers_func)) {
3133 if (PyList_Append(self->pers_func, pid) < 0) {
3134 Py_DECREF(pid);
3135 return -1;
3136 }
3137 }
3138 else {
3139 ARG_TUP(self, pid);
3140 if (self->arg) {
3141 pid = PyObject_CallObject(self->pers_func, self->arg);
3142 FREE_ARG_TUP(self);
3143 }
3144 if (! pid) return -1;
3145 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003146
Guido van Rossum053b8df1998-11-25 16:18:00 +00003147 PDATA_PUSH(self->stack, pid, -1);
3148 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003149 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003150 else {
3151 PyErr_SetString(UnpicklingError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00003152 "A load persistent id instruction was encountered,\n"
3153 "but no persistent_load function was specified.");
3154 return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003155 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003156}
3157
3158
3159static int
3160load_pop(Unpicklerobject *self) {
3161 int len;
3162
Guido van Rossum053b8df1998-11-25 16:18:00 +00003163 UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003164
Tim Peters84e87f32001-03-17 04:50:51 +00003165 /* Note that we split the (pickle.py) stack into two stacks,
Guido van Rossumea2b7152000-05-09 18:14:50 +00003166 an object stack and a mark stack. We have to be clever and
3167 pop the right one. We do this by looking at the top of the
3168 mark stack.
3169 */
3170
Tim Peters84e87f32001-03-17 04:50:51 +00003171 if ((self->num_marks > 0) &&
Guido van Rossum60456fd1997-04-09 17:36:32 +00003172 (self->marks[self->num_marks - 1] == len))
3173 self->num_marks--;
Tim Peters84e87f32001-03-17 04:50:51 +00003174 else {
Guido van Rossumea2b7152000-05-09 18:14:50 +00003175 len--;
3176 Py_DECREF(self->stack->data[len]);
3177 self->stack->length=len;
3178 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003179
3180 return 0;
3181}
3182
3183
3184static int
3185load_pop_mark(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003186 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003187
3188 if ((i = marker(self)) < 0)
3189 return -1;
3190
Guido van Rossum053b8df1998-11-25 16:18:00 +00003191 Pdata_clear(self->stack, i);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003192
3193 return 0;
3194}
3195
3196
3197static int
3198load_dup(Unpicklerobject *self) {
3199 PyObject *last;
3200 int len;
3201
Guido van Rossum053b8df1998-11-25 16:18:00 +00003202 if ((len = self->stack->length) <= 0) return stackUnderflow();
3203 last=self->stack->data[len-1];
3204 Py_INCREF(last);
3205 PDATA_PUSH(self->stack, last, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003206 return 0;
3207}
3208
3209
3210static int
3211load_get(Unpicklerobject *self) {
3212 PyObject *py_str = 0, *value = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003213 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003214 char *s;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003215 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003216
Guido van Rossum053b8df1998-11-25 16:18:00 +00003217 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003218 if (len < 2) return bad_readline();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003219
Guido van Rossum053b8df1998-11-25 16:18:00 +00003220 UNLESS (py_str = PyString_FromStringAndSize(s, len - 1)) return -1;
3221
3222 value = PyDict_GetItem(self->memo, py_str);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003223 if (! value) {
3224 PyErr_SetObject(BadPickleGet, py_str);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003225 rc = -1;
3226 } else {
3227 PDATA_APPEND(self->stack, value, -1);
3228 rc = 0;
3229 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003230
Guido van Rossum2f80d961999-07-13 15:18:58 +00003231 Py_DECREF(py_str);
3232 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003233}
3234
3235
3236static int
3237load_binget(Unpicklerobject *self) {
3238 PyObject *py_key = 0, *value = 0;
3239 unsigned char key;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003240 char *s;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003241 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003242
Guido van Rossum053b8df1998-11-25 16:18:00 +00003243 if ((*self->read_func)(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003244
3245 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00003246 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003247
Guido van Rossum053b8df1998-11-25 16:18:00 +00003248 value = PyDict_GetItem(self->memo, py_key);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003249 if (! value) {
3250 PyErr_SetObject(BadPickleGet, py_key);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003251 rc = -1;
3252 } else {
3253 PDATA_APPEND(self->stack, value, -1);
3254 rc = 0;
3255 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003256
Guido van Rossum2f80d961999-07-13 15:18:58 +00003257 Py_DECREF(py_key);
3258 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003259}
3260
3261
3262static int
3263load_long_binget(Unpicklerobject *self) {
3264 PyObject *py_key = 0, *value = 0;
Thomas Wouters3b6448f2000-07-22 23:56:07 +00003265 unsigned char c;
3266 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003267 long key;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003268 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003269
Guido van Rossum053b8df1998-11-25 16:18:00 +00003270 if ((*self->read_func)(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003271
3272 c = (unsigned char)s[0];
3273 key = (long)c;
3274 c = (unsigned char)s[1];
3275 key |= (long)c << 8;
3276 c = (unsigned char)s[2];
3277 key |= (long)c << 16;
3278 c = (unsigned char)s[3];
3279 key |= (long)c << 24;
3280
Guido van Rossum053b8df1998-11-25 16:18:00 +00003281 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003282
Guido van Rossum053b8df1998-11-25 16:18:00 +00003283 value = PyDict_GetItem(self->memo, py_key);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003284 if (! value) {
3285 PyErr_SetObject(BadPickleGet, py_key);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003286 rc = -1;
3287 } else {
3288 PDATA_APPEND(self->stack, value, -1);
3289 rc = 0;
3290 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003291
Guido van Rossum2f80d961999-07-13 15:18:58 +00003292 Py_DECREF(py_key);
3293 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003294}
3295
3296
3297static int
3298load_put(Unpicklerobject *self) {
3299 PyObject *py_str = 0, *value = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003300 int len, l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003301 char *s;
3302
Guido van Rossum053b8df1998-11-25 16:18:00 +00003303 if ((l = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumd1f66dc1999-02-08 22:38:25 +00003304 if (l < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00003305 UNLESS (len=self->stack->length) return stackUnderflow();
3306 UNLESS (py_str = PyString_FromStringAndSize(s, l - 1)) return -1;
3307 value=self->stack->data[len-1];
3308 l=PyDict_SetItem(self->memo, py_str, value);
3309 Py_DECREF(py_str);
3310 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003311}
3312
3313
3314static int
3315load_binput(Unpicklerobject *self) {
3316 PyObject *py_key = 0, *value = 0;
Thomas Wouters3b6448f2000-07-22 23:56:07 +00003317 unsigned char key;
3318 char *s;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003319 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003320
Guido van Rossum053b8df1998-11-25 16:18:00 +00003321 if ((*self->read_func)(self, &s, 1) < 0) return -1;
3322 UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003323
3324 key = (unsigned char)s[0];
3325
Guido van Rossum053b8df1998-11-25 16:18:00 +00003326 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3327 value=self->stack->data[len-1];
3328 len=PyDict_SetItem(self->memo, py_key, value);
3329 Py_DECREF(py_key);
3330 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003331}
3332
3333
3334static int
3335load_long_binput(Unpicklerobject *self) {
3336 PyObject *py_key = 0, *value = 0;
3337 long key;
Thomas Wouters3b6448f2000-07-22 23:56:07 +00003338 unsigned char c;
3339 char *s;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003340 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003341
Guido van Rossum053b8df1998-11-25 16:18:00 +00003342 if ((*self->read_func)(self, &s, 4) < 0) return -1;
3343 UNLESS (len=self->stack->length) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003344
3345 c = (unsigned char)s[0];
3346 key = (long)c;
3347 c = (unsigned char)s[1];
3348 key |= (long)c << 8;
3349 c = (unsigned char)s[2];
3350 key |= (long)c << 16;
3351 c = (unsigned char)s[3];
3352 key |= (long)c << 24;
3353
Guido van Rossum053b8df1998-11-25 16:18:00 +00003354 UNLESS (py_key = PyInt_FromLong(key)) return -1;
3355 value=self->stack->data[len-1];
3356 len=PyDict_SetItem(self->memo, py_key, value);
3357 Py_DECREF(py_key);
3358 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003359}
3360
3361
Tim Peters84e87f32001-03-17 04:50:51 +00003362static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00003363do_append(Unpicklerobject *self, int x) {
3364 PyObject *value = 0, *list = 0, *append_method = 0;
3365 int len, i;
3366
Guido van Rossum053b8df1998-11-25 16:18:00 +00003367 UNLESS ((len=self->stack->length) >= x && x > 0) return stackUnderflow();
3368 if (len==x) return 0; /* nothing to do */
Guido van Rossum60456fd1997-04-09 17:36:32 +00003369
Guido van Rossum053b8df1998-11-25 16:18:00 +00003370 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003371
3372 if (PyList_Check(list)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003373 PyObject *slice;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003374 int list_len;
Tim Peters84e87f32001-03-17 04:50:51 +00003375
Guido van Rossum053b8df1998-11-25 16:18:00 +00003376 slice=Pdata_popList(self->stack, x);
3377 list_len = PyList_GET_SIZE(list);
3378 i=PyList_SetSlice(list, list_len, list_len, slice);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003379 Py_DECREF(slice);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003380 return i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003381 }
3382 else {
3383
Guido van Rossum053b8df1998-11-25 16:18:00 +00003384 UNLESS (append_method = PyObject_GetAttr(list, append_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00003385 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003386
Guido van Rossum60456fd1997-04-09 17:36:32 +00003387 for (i = x; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003388 PyObject *junk;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003389
Guido van Rossum053b8df1998-11-25 16:18:00 +00003390 value=self->stack->data[i];
3391 junk=0;
3392 ARG_TUP(self, value);
3393 if (self->arg) {
3394 junk = PyObject_CallObject(append_method, self->arg);
3395 FREE_ARG_TUP(self);
3396 }
3397 if (! junk) {
3398 Pdata_clear(self->stack, i+1);
3399 self->stack->length=x;
3400 Py_DECREF(append_method);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003401 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003402 }
3403 Py_DECREF(junk);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003404 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003405 self->stack->length=x;
3406 Py_DECREF(append_method);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003407 }
3408
Guido van Rossum60456fd1997-04-09 17:36:32 +00003409 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003410}
3411
Tim Peters84e87f32001-03-17 04:50:51 +00003412
Guido van Rossum60456fd1997-04-09 17:36:32 +00003413static int
3414load_append(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003415 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003416}
3417
3418
3419static int
3420load_appends(Unpicklerobject *self) {
3421 return do_append(self, marker(self));
3422}
3423
3424
3425static int
3426do_setitems(Unpicklerobject *self, int x) {
3427 PyObject *value = 0, *key = 0, *dict = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003428 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003429
Guido van Rossum053b8df1998-11-25 16:18:00 +00003430 UNLESS ((len=self->stack->length) >= x
3431 && x > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003432
Guido van Rossum053b8df1998-11-25 16:18:00 +00003433 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003434
Guido van Rossum053b8df1998-11-25 16:18:00 +00003435 for (i = x+1; i < len; i += 2) {
3436 key =self->stack->data[i-1];
3437 value=self->stack->data[i ];
3438 if (PyObject_SetItem(dict, key, value) < 0) {
3439 r=-1;
3440 break;
3441 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003442 }
3443
Guido van Rossum053b8df1998-11-25 16:18:00 +00003444 Pdata_clear(self->stack, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003445
Guido van Rossum053b8df1998-11-25 16:18:00 +00003446 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003447}
3448
3449
3450static int
3451load_setitem(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003452 return do_setitems(self, self->stack->length - 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003453}
3454
Guido van Rossum60456fd1997-04-09 17:36:32 +00003455static int
3456load_setitems(Unpicklerobject *self) {
3457 return do_setitems(self, marker(self));
3458}
3459
3460
3461static int
3462load_build(Unpicklerobject *self) {
Tim Peters84e87f32001-03-17 04:50:51 +00003463 PyObject *value = 0, *inst = 0, *instdict = 0, *d_key = 0, *d_value = 0,
Guido van Rossum60456fd1997-04-09 17:36:32 +00003464 *junk = 0, *__setstate__ = 0;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003465 int i, r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003466
Guido van Rossum053b8df1998-11-25 16:18:00 +00003467 if (self->stack->length < 2) return stackUnderflow();
3468 PDATA_POP(self->stack, value);
3469 if (! value) return -1;
3470 inst=self->stack->data[self->stack->length-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003471
Guido van Rossum053b8df1998-11-25 16:18:00 +00003472 if ((__setstate__ = PyObject_GetAttr(inst, __setstate___str))) {
3473 ARG_TUP(self, value);
3474 if (self->arg) {
3475 junk = PyObject_CallObject(__setstate__, self->arg);
3476 FREE_ARG_TUP(self);
3477 }
3478 Py_DECREF(__setstate__);
3479 if (! junk) return -1;
3480 Py_DECREF(junk);
3481 return 0;
3482 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003483
Guido van Rossum053b8df1998-11-25 16:18:00 +00003484 PyErr_Clear();
3485 if ((instdict = PyObject_GetAttr(inst, __dict___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003486 i = 0;
3487 while (PyDict_Next(value, &i, &d_key, &d_value)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003488 if (PyObject_SetItem(instdict, d_key, d_value) < 0) {
3489 r=-1;
3490 break;
3491 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003492 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003493 Py_DECREF(instdict);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003494 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003495 else r=-1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003496
Guido van Rossum053b8df1998-11-25 16:18:00 +00003497 Py_XDECREF(value);
Tim Peters84e87f32001-03-17 04:50:51 +00003498
Guido van Rossum053b8df1998-11-25 16:18:00 +00003499 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003500}
3501
3502
3503static int
3504load_mark(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003505 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003506
Guido van Rossumea2b7152000-05-09 18:14:50 +00003507 /* Note that we split the (pickle.py) stack into two stacks, an
3508 object stack and a mark stack. Here we push a mark onto the
Tim Peters84e87f32001-03-17 04:50:51 +00003509 mark stack.
Guido van Rossumea2b7152000-05-09 18:14:50 +00003510 */
3511
Guido van Rossum053b8df1998-11-25 16:18:00 +00003512 if ((self->num_marks + 1) >= self->marks_size) {
3513 s=self->marks_size+20;
3514 if (s <= self->num_marks) s=self->num_marks + 1;
Guido van Rossum761fcd01999-04-12 22:51:20 +00003515 if (self->marks == NULL)
Guido van Rossumaa8d1671999-01-25 21:43:51 +00003516 self->marks=(int *)malloc(s * sizeof(int));
3517 else
3518 self->marks=(int *)realloc(self->marks, s * sizeof(int));
Guido van Rossum053b8df1998-11-25 16:18:00 +00003519 if (! self->marks) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003520 PyErr_NoMemory();
3521 return -1;
3522 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003523 self->marks_size = s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003524 }
3525
Guido van Rossum053b8df1998-11-25 16:18:00 +00003526 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003527
3528 return 0;
3529}
3530
3531static int
3532load_reduce(Unpicklerobject *self) {
3533 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003534
Guido van Rossum053b8df1998-11-25 16:18:00 +00003535 PDATA_POP(self->stack, arg_tup);
3536 if (! arg_tup) return -1;
3537 PDATA_POP(self->stack, callable);
3538 if (callable) {
3539 ob = Instance_New(callable, arg_tup);
3540 Py_DECREF(callable);
3541 }
3542 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003543
Guido van Rossum053b8df1998-11-25 16:18:00 +00003544 if (! ob) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003545
Guido van Rossum053b8df1998-11-25 16:18:00 +00003546 PDATA_PUSH(self->stack, ob, -1);
3547 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003548}
Tim Peters84e87f32001-03-17 04:50:51 +00003549
Guido van Rossum60456fd1997-04-09 17:36:32 +00003550static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003551load(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003552 PyObject *err = 0, *val = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003553 char *s;
3554
Guido van Rossum60456fd1997-04-09 17:36:32 +00003555 self->num_marks = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003556 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003557
3558 while (1) {
3559 if ((*self->read_func)(self, &s, 1) < 0)
3560 break;
3561
3562 switch (s[0]) {
3563 case NONE:
3564 if (load_none(self) < 0)
3565 break;
3566 continue;
3567
3568 case BININT:
3569 if (load_binint(self) < 0)
3570 break;
3571 continue;
3572
3573 case BININT1:
3574 if (load_binint1(self) < 0)
3575 break;
3576 continue;
3577
3578 case BININT2:
3579 if (load_binint2(self) < 0)
3580 break;
3581 continue;
3582
3583 case INT:
3584 if (load_int(self) < 0)
3585 break;
3586 continue;
3587
3588 case LONG:
3589 if (load_long(self) < 0)
3590 break;
3591 continue;
3592
3593 case FLOAT:
3594 if (load_float(self) < 0)
3595 break;
3596 continue;
3597
Guido van Rossum60456fd1997-04-09 17:36:32 +00003598 case BINFLOAT:
3599 if (load_binfloat(self) < 0)
3600 break;
3601 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003602
3603 case BINSTRING:
3604 if (load_binstring(self) < 0)
3605 break;
3606 continue;
3607
3608 case SHORT_BINSTRING:
3609 if (load_short_binstring(self) < 0)
3610 break;
3611 continue;
3612
3613 case STRING:
3614 if (load_string(self) < 0)
3615 break;
3616 continue;
3617
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003618 case UNICODE:
3619 if (load_unicode(self) < 0)
3620 break;
3621 continue;
3622
3623 case BINUNICODE:
3624 if (load_binunicode(self) < 0)
3625 break;
3626 continue;
3627
Guido van Rossum60456fd1997-04-09 17:36:32 +00003628 case EMPTY_TUPLE:
3629 if (load_empty_tuple(self) < 0)
3630 break;
3631 continue;
3632
3633 case TUPLE:
3634 if (load_tuple(self) < 0)
3635 break;
3636 continue;
3637
3638 case EMPTY_LIST:
3639 if (load_empty_list(self) < 0)
3640 break;
3641 continue;
3642
3643 case LIST:
3644 if (load_list(self) < 0)
3645 break;
3646 continue;
3647
3648 case EMPTY_DICT:
3649 if (load_empty_dict(self) < 0)
3650 break;
3651 continue;
3652
3653 case DICT:
3654 if (load_dict(self) < 0)
3655 break;
3656 continue;
3657
3658 case OBJ:
3659 if (load_obj(self) < 0)
3660 break;
3661 continue;
3662
3663 case INST:
3664 if (load_inst(self) < 0)
3665 break;
3666 continue;
3667
3668 case GLOBAL:
3669 if (load_global(self) < 0)
3670 break;
3671 continue;
3672
3673 case APPEND:
3674 if (load_append(self) < 0)
3675 break;
3676 continue;
3677
3678 case APPENDS:
3679 if (load_appends(self) < 0)
3680 break;
3681 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003682
Guido van Rossum60456fd1997-04-09 17:36:32 +00003683 case BUILD:
3684 if (load_build(self) < 0)
3685 break;
3686 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003687
Guido van Rossum60456fd1997-04-09 17:36:32 +00003688 case DUP:
3689 if (load_dup(self) < 0)
3690 break;
3691 continue;
3692
3693 case BINGET:
3694 if (load_binget(self) < 0)
3695 break;
3696 continue;
3697
3698 case LONG_BINGET:
3699 if (load_long_binget(self) < 0)
3700 break;
3701 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003702
Guido van Rossum60456fd1997-04-09 17:36:32 +00003703 case GET:
3704 if (load_get(self) < 0)
3705 break;
3706 continue;
3707
3708 case MARK:
3709 if (load_mark(self) < 0)
3710 break;
3711 continue;
3712
3713 case BINPUT:
3714 if (load_binput(self) < 0)
3715 break;
3716 continue;
3717
3718 case LONG_BINPUT:
3719 if (load_long_binput(self) < 0)
3720 break;
3721 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003722
Guido van Rossum60456fd1997-04-09 17:36:32 +00003723 case PUT:
3724 if (load_put(self) < 0)
3725 break;
3726 continue;
3727
3728 case POP:
3729 if (load_pop(self) < 0)
3730 break;
3731 continue;
3732
3733 case POP_MARK:
3734 if (load_pop_mark(self) < 0)
3735 break;
3736 continue;
3737
3738 case SETITEM:
3739 if (load_setitem(self) < 0)
3740 break;
3741 continue;
3742
3743 case SETITEMS:
3744 if (load_setitems(self) < 0)
3745 break;
3746 continue;
3747
3748 case STOP:
3749 break;
3750
3751 case PERSID:
3752 if (load_persid(self) < 0)
3753 break;
3754 continue;
3755
3756 case BINPERSID:
3757 if (load_binpersid(self) < 0)
3758 break;
3759 continue;
3760
3761 case REDUCE:
3762 if (load_reduce(self) < 0)
3763 break;
3764 continue;
3765
Tim Peters84e87f32001-03-17 04:50:51 +00003766 default:
3767 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossum60456fd1997-04-09 17:36:32 +00003768 "c", s[0]);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003769 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003770 }
3771
3772 break;
3773 }
3774
Guido van Rossum053b8df1998-11-25 16:18:00 +00003775 if ((err = PyErr_Occurred())) {
3776 if (err == PyExc_EOFError) {
3777 PyErr_SetNone(PyExc_EOFError);
Tim Peters84e87f32001-03-17 04:50:51 +00003778 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003779 return NULL;
3780 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003781
Tim Peters84e87f32001-03-17 04:50:51 +00003782 PDATA_POP(self->stack, val);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003783 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003784}
Tim Peters84e87f32001-03-17 04:50:51 +00003785
Guido van Rossum60456fd1997-04-09 17:36:32 +00003786
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003787/* No-load functions to support noload, which is used to
3788 find persistent references. */
3789
3790static int
3791noload_obj(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003792 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003793
3794 if ((i = marker(self)) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003795 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003796}
3797
3798
3799static int
3800noload_inst(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003801 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003802 char *s;
3803
3804 if ((i = marker(self)) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003805 Pdata_clear(self->stack, i);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003806 if ((*self->readline_func)(self, &s) < 0) return -1;
3807 if ((*self->readline_func)(self, &s) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003808 PDATA_APPEND(self->stack, Py_None,-1);
3809 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003810}
3811
3812static int
3813noload_global(Unpicklerobject *self) {
3814 char *s;
3815
3816 if ((*self->readline_func)(self, &s) < 0) return -1;
3817 if ((*self->readline_func)(self, &s) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003818 PDATA_APPEND(self->stack, Py_None,-1);
3819 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003820}
3821
3822static int
3823noload_reduce(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003824
Guido van Rossum053b8df1998-11-25 16:18:00 +00003825 if (self->stack->length < 2) return stackUnderflow();
3826 Pdata_clear(self->stack, self->stack->length-2);
3827 PDATA_APPEND(self->stack, Py_None,-1);
3828 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003829}
3830
3831static int
3832noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003833
Guido van Rossum053b8df1998-11-25 16:18:00 +00003834 if (self->stack->length < 1) return stackUnderflow();
3835 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00003836 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003837}
3838
3839
3840static PyObject *
3841noload(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003842 PyObject *err = 0, *val = 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003843 char *s;
3844
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003845 self->num_marks = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003846 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003847
3848 while (1) {
3849 if ((*self->read_func)(self, &s, 1) < 0)
3850 break;
3851
3852 switch (s[0]) {
3853 case NONE:
3854 if (load_none(self) < 0)
3855 break;
3856 continue;
3857
3858 case BININT:
3859 if (load_binint(self) < 0)
3860 break;
3861 continue;
3862
3863 case BININT1:
3864 if (load_binint1(self) < 0)
3865 break;
3866 continue;
3867
3868 case BININT2:
3869 if (load_binint2(self) < 0)
3870 break;
3871 continue;
3872
3873 case INT:
3874 if (load_int(self) < 0)
3875 break;
3876 continue;
3877
3878 case LONG:
3879 if (load_long(self) < 0)
3880 break;
3881 continue;
3882
3883 case FLOAT:
3884 if (load_float(self) < 0)
3885 break;
3886 continue;
3887
3888 case BINFLOAT:
3889 if (load_binfloat(self) < 0)
3890 break;
3891 continue;
3892
3893 case BINSTRING:
3894 if (load_binstring(self) < 0)
3895 break;
3896 continue;
3897
3898 case SHORT_BINSTRING:
3899 if (load_short_binstring(self) < 0)
3900 break;
3901 continue;
3902
3903 case STRING:
3904 if (load_string(self) < 0)
3905 break;
3906 continue;
3907
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003908 case UNICODE:
3909 if (load_unicode(self) < 0)
3910 break;
3911 continue;
3912
3913 case BINUNICODE:
3914 if (load_binunicode(self) < 0)
3915 break;
3916 continue;
3917
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003918 case EMPTY_TUPLE:
3919 if (load_empty_tuple(self) < 0)
3920 break;
3921 continue;
3922
3923 case TUPLE:
3924 if (load_tuple(self) < 0)
3925 break;
3926 continue;
3927
3928 case EMPTY_LIST:
3929 if (load_empty_list(self) < 0)
3930 break;
3931 continue;
3932
3933 case LIST:
3934 if (load_list(self) < 0)
3935 break;
3936 continue;
3937
3938 case EMPTY_DICT:
3939 if (load_empty_dict(self) < 0)
3940 break;
3941 continue;
3942
3943 case DICT:
3944 if (load_dict(self) < 0)
3945 break;
3946 continue;
3947
3948 case OBJ:
3949 if (noload_obj(self) < 0)
3950 break;
3951 continue;
3952
3953 case INST:
3954 if (noload_inst(self) < 0)
3955 break;
3956 continue;
3957
3958 case GLOBAL:
3959 if (noload_global(self) < 0)
3960 break;
3961 continue;
3962
3963 case APPEND:
3964 if (load_append(self) < 0)
3965 break;
3966 continue;
3967
3968 case APPENDS:
3969 if (load_appends(self) < 0)
3970 break;
3971 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003972
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003973 case BUILD:
3974 if (noload_build(self) < 0)
3975 break;
3976 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003977
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003978 case DUP:
3979 if (load_dup(self) < 0)
3980 break;
3981 continue;
3982
3983 case BINGET:
3984 if (load_binget(self) < 0)
3985 break;
3986 continue;
3987
3988 case LONG_BINGET:
3989 if (load_long_binget(self) < 0)
3990 break;
3991 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003992
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003993 case GET:
3994 if (load_get(self) < 0)
3995 break;
3996 continue;
3997
3998 case MARK:
3999 if (load_mark(self) < 0)
4000 break;
4001 continue;
4002
4003 case BINPUT:
4004 if (load_binput(self) < 0)
4005 break;
4006 continue;
4007
4008 case LONG_BINPUT:
4009 if (load_long_binput(self) < 0)
4010 break;
4011 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004012
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004013 case PUT:
4014 if (load_put(self) < 0)
4015 break;
4016 continue;
4017
4018 case POP:
4019 if (load_pop(self) < 0)
4020 break;
4021 continue;
4022
4023 case POP_MARK:
4024 if (load_pop_mark(self) < 0)
4025 break;
4026 continue;
4027
4028 case SETITEM:
4029 if (load_setitem(self) < 0)
4030 break;
4031 continue;
4032
4033 case SETITEMS:
4034 if (load_setitems(self) < 0)
4035 break;
4036 continue;
4037
4038 case STOP:
4039 break;
4040
4041 case PERSID:
4042 if (load_persid(self) < 0)
4043 break;
4044 continue;
4045
4046 case BINPERSID:
4047 if (load_binpersid(self) < 0)
4048 break;
4049 continue;
4050
4051 case REDUCE:
4052 if (noload_reduce(self) < 0)
4053 break;
4054 continue;
4055
Tim Peters84e87f32001-03-17 04:50:51 +00004056 default:
4057 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004058 "c", s[0]);
Guido van Rossum053b8df1998-11-25 16:18:00 +00004059 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004060 }
4061
4062 break;
4063 }
4064
Guido van Rossum053b8df1998-11-25 16:18:00 +00004065 if ((err = PyErr_Occurred())) {
4066 if (err == PyExc_EOFError) {
4067 PyErr_SetNone(PyExc_EOFError);
Tim Peters84e87f32001-03-17 04:50:51 +00004068 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00004069 return NULL;
4070 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004071
Tim Peters84e87f32001-03-17 04:50:51 +00004072 PDATA_POP(self->stack, val);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004073 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004074}
Tim Peters84e87f32001-03-17 04:50:51 +00004075
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004076
Guido van Rossum60456fd1997-04-09 17:36:32 +00004077static PyObject *
4078Unpickler_load(Unpicklerobject *self, PyObject *args) {
Tim Peters84e87f32001-03-17 04:50:51 +00004079 UNLESS (PyArg_ParseTuple(args, ":load"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004080 return NULL;
4081
4082 return load(self);
4083}
4084
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004085static PyObject *
4086Unpickler_noload(Unpicklerobject *self, PyObject *args) {
Tim Peters84e87f32001-03-17 04:50:51 +00004087 UNLESS (PyArg_ParseTuple(args, ":noload"))
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004088 return NULL;
4089
4090 return noload(self);
4091}
4092
Guido van Rossum60456fd1997-04-09 17:36:32 +00004093
4094static struct PyMethodDef Unpickler_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004095 {"load", (PyCFunction)Unpickler_load, 1,
4096 "load() -- Load a pickle"
4097 },
4098 {"noload", (PyCFunction)Unpickler_noload, 1,
4099 "noload() -- not load a pickle, but go through most of the motions\n"
4100 "\n"
4101 "This function can be used to read past a pickle without instantiating\n"
4102 "any objects or importing any modules. It can also be used to find all\n"
4103 "persistent references without instantiating any objects or importing\n"
4104 "any modules.\n"
4105 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00004106 {NULL, NULL} /* sentinel */
4107};
4108
4109
4110static Unpicklerobject *
4111newUnpicklerobject(PyObject *f) {
4112 Unpicklerobject *self;
4113
Guido van Rossumb18618d2000-05-03 23:44:39 +00004114 UNLESS (self = PyObject_New(Unpicklerobject, &Unpicklertype))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004115 return NULL;
4116
4117 self->file = NULL;
4118 self->arg = NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00004119 self->stack = (Pdata*)Pdata_New();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004120 self->pers_func = NULL;
4121 self->last_string = NULL;
4122 self->marks = NULL;
4123 self->num_marks = 0;
4124 self->marks_size = 0;
4125 self->buf_size = 0;
4126 self->read = NULL;
Guido van Rossum8a6dba31998-03-06 01:39:39 +00004127 self->readline = NULL;
Guido van Rossum21ef0881998-12-11 03:20:00 +00004128 self->safe_constructors = NULL;
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004129 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004130
Tim Peters84e87f32001-03-17 04:50:51 +00004131 UNLESS (self->memo = PyDict_New())
Guido van Rossum83addc72000-04-21 20:49:36 +00004132 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004133
4134 Py_INCREF(f);
4135 self->file = f;
4136
4137 /* Set read, readline based on type of f */
4138 if (PyFile_Check(f)) {
4139 self->fp = PyFile_AsFile(f);
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00004140 if (self->fp == NULL) {
Guido van Rossum83addc72000-04-21 20:49:36 +00004141 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
4142 goto err;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00004143 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004144 self->read_func = read_file;
4145 self->readline_func = readline_file;
4146 }
4147 else if (PycStringIO_InputCheck(f)) {
4148 self->fp = NULL;
4149 self->read_func = read_cStringIO;
4150 self->readline_func = readline_cStringIO;
4151 }
4152 else {
4153
4154 self->fp = NULL;
4155 self->read_func = read_other;
4156 self->readline_func = readline_other;
4157
Guido van Rossum053b8df1998-11-25 16:18:00 +00004158 UNLESS ((self->readline = PyObject_GetAttr(f, readline_str)) &&
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004159 (self->read = PyObject_GetAttr(f, read_str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004160 PyErr_Clear();
4161 PyErr_SetString( PyExc_TypeError, "argument must have 'read' and "
4162 "'readline' attributes" );
Guido van Rossum053b8df1998-11-25 16:18:00 +00004163 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004164 }
4165 }
4166
Guido van Rossum053b8df1998-11-25 16:18:00 +00004167 if (PyEval_GetRestricted()) {
4168 /* Restricted execution, get private tables */
4169 PyObject *m;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004170
Guido van Rossum053b8df1998-11-25 16:18:00 +00004171 UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
4172 self->safe_constructors=PyObject_GetAttr(m, safe_constructors_str);
4173 Py_DECREF(m);
4174 UNLESS (self->safe_constructors) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004175 }
4176 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00004177 self->safe_constructors=safe_constructors;
4178 Py_INCREF(safe_constructors);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004179 }
4180
Guido van Rossum60456fd1997-04-09 17:36:32 +00004181 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004182
4183err:
4184 Py_DECREF((PyObject *)self);
4185 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004186}
4187
4188
4189static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004190get_Unpickler(PyObject *self, PyObject *args) {
4191 PyObject *file;
Tim Peters84e87f32001-03-17 04:50:51 +00004192
Guido van Rossum43713e52000-02-29 13:59:29 +00004193 UNLESS (PyArg_ParseTuple(args, "O:Unpickler", &file))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004194 return NULL;
4195 return (PyObject *)newUnpicklerobject(file);
4196}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004197
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004198
Guido van Rossum60456fd1997-04-09 17:36:32 +00004199static void
4200Unpickler_dealloc(Unpicklerobject *self) {
4201 Py_XDECREF(self->readline);
4202 Py_XDECREF(self->read);
4203 Py_XDECREF(self->file);
4204 Py_XDECREF(self->memo);
4205 Py_XDECREF(self->stack);
4206 Py_XDECREF(self->pers_func);
4207 Py_XDECREF(self->arg);
4208 Py_XDECREF(self->last_string);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004209 Py_XDECREF(self->safe_constructors);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004210
Guido van Rossum60456fd1997-04-09 17:36:32 +00004211 if (self->marks) {
4212 free(self->marks);
4213 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004214
Guido van Rossum60456fd1997-04-09 17:36:32 +00004215 if (self->buf_size) {
4216 free(self->buf);
4217 }
Tim Peters84e87f32001-03-17 04:50:51 +00004218
Guido van Rossumb18618d2000-05-03 23:44:39 +00004219 PyObject_Del(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004220}
4221
4222
4223static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004224Unpickler_getattr(Unpicklerobject *self, char *name) {
4225 if (!strcmp(name, "persistent_load")) {
4226 if (!self->pers_func) {
4227 PyErr_SetString(PyExc_AttributeError, name);
4228 return NULL;
4229 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004230
Guido van Rossum60456fd1997-04-09 17:36:32 +00004231 Py_INCREF(self->pers_func);
4232 return self->pers_func;
4233 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004234
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004235 if (!strcmp(name, "find_global")) {
4236 if (!self->find_class) {
4237 PyErr_SetString(PyExc_AttributeError, name);
4238 return NULL;
4239 }
4240
4241 Py_INCREF(self->find_class);
4242 return self->find_class;
4243 }
4244
Guido van Rossum60456fd1997-04-09 17:36:32 +00004245 if (!strcmp(name, "memo")) {
4246 if (!self->memo) {
4247 PyErr_SetString(PyExc_AttributeError, name);
4248 return NULL;
4249 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004250
Guido van Rossum60456fd1997-04-09 17:36:32 +00004251 Py_INCREF(self->memo);
4252 return self->memo;
4253 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004254
Guido van Rossum60456fd1997-04-09 17:36:32 +00004255 if (!strcmp(name, "UnpicklingError")) {
4256 Py_INCREF(UnpicklingError);
4257 return UnpicklingError;
4258 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004259
Guido van Rossum60456fd1997-04-09 17:36:32 +00004260 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
4261}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004262
Guido van Rossum60456fd1997-04-09 17:36:32 +00004263
4264static int
4265Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004266
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004267 if (!strcmp(name, "persistent_load")) {
4268 Py_XDECREF(self->pers_func);
4269 self->pers_func = value;
4270 Py_XINCREF(value);
4271 return 0;
4272 }
4273
4274 if (!strcmp(name, "find_global")) {
4275 Py_XDECREF(self->find_class);
4276 self->find_class = value;
4277 Py_XINCREF(value);
4278 return 0;
4279 }
4280
Guido van Rossum053b8df1998-11-25 16:18:00 +00004281 if (! value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004282 PyErr_SetString(PyExc_TypeError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00004283 "attribute deletion is not supported");
4284 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004285 }
4286
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004287 if (strcmp(name, "memo") == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00004288 if (! PyDict_Check(value)) {
4289 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
4290 return -1;
4291 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004292 Py_XDECREF(self->memo);
4293 self->memo = value;
4294 Py_INCREF(value);
4295 return 0;
4296 }
4297
Guido van Rossum60456fd1997-04-09 17:36:32 +00004298 PyErr_SetString(PyExc_AttributeError, name);
4299 return -1;
4300}
4301
4302
4303static PyObject *
4304cpm_dump(PyObject *self, PyObject *args) {
4305 PyObject *ob, *file, *res = NULL;
4306 Picklerobject *pickler = 0;
4307 int bin = 0;
4308
Guido van Rossum053b8df1998-11-25 16:18:00 +00004309 UNLESS (PyArg_ParseTuple(args, "OO|i", &ob, &file, &bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004310 goto finally;
4311
Guido van Rossum053b8df1998-11-25 16:18:00 +00004312 UNLESS (pickler = newPicklerobject(file, bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004313 goto finally;
4314
4315 if (dump(pickler, ob) < 0)
4316 goto finally;
4317
4318 Py_INCREF(Py_None);
4319 res = Py_None;
4320
4321finally:
4322 Py_XDECREF(pickler);
4323
4324 return res;
4325}
4326
4327
4328static PyObject *
4329cpm_dumps(PyObject *self, PyObject *args) {
4330 PyObject *ob, *file = 0, *res = NULL;
4331 Picklerobject *pickler = 0;
4332 int bin = 0;
4333
Guido van Rossum43713e52000-02-29 13:59:29 +00004334 UNLESS (PyArg_ParseTuple(args, "O|i:dumps", &ob, &bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004335 goto finally;
4336
Guido van Rossum053b8df1998-11-25 16:18:00 +00004337 UNLESS (file = PycStringIO->NewOutput(128))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004338 goto finally;
4339
Guido van Rossum053b8df1998-11-25 16:18:00 +00004340 UNLESS (pickler = newPicklerobject(file, bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004341 goto finally;
4342
4343 if (dump(pickler, ob) < 0)
4344 goto finally;
4345
4346 res = PycStringIO->cgetvalue(file);
4347
4348finally:
4349 Py_XDECREF(pickler);
4350 Py_XDECREF(file);
4351
4352 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00004353}
4354
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004355
4356static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004357cpm_load(PyObject *self, PyObject *args) {
4358 Unpicklerobject *unpickler = 0;
4359 PyObject *ob, *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004360
Guido van Rossum43713e52000-02-29 13:59:29 +00004361 UNLESS (PyArg_ParseTuple(args, "O:load", &ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004362 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004363
Guido van Rossum053b8df1998-11-25 16:18:00 +00004364 UNLESS (unpickler = newUnpicklerobject(ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004365 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004366
Guido van Rossum60456fd1997-04-09 17:36:32 +00004367 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004368
Guido van Rossum60456fd1997-04-09 17:36:32 +00004369finally:
4370 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004371
Guido van Rossum60456fd1997-04-09 17:36:32 +00004372 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004373}
4374
4375
4376static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004377cpm_loads(PyObject *self, PyObject *args) {
4378 PyObject *ob, *file = 0, *res = NULL;
4379 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004380
Guido van Rossum43713e52000-02-29 13:59:29 +00004381 UNLESS (PyArg_ParseTuple(args, "S:loads", &ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004382 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004383
Guido van Rossum053b8df1998-11-25 16:18:00 +00004384 UNLESS (file = PycStringIO->NewInput(ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004385 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00004386
Guido van Rossum053b8df1998-11-25 16:18:00 +00004387 UNLESS (unpickler = newUnpicklerobject(file))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004388 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004389
Guido van Rossum60456fd1997-04-09 17:36:32 +00004390 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004391
Guido van Rossum60456fd1997-04-09 17:36:32 +00004392finally:
4393 Py_XDECREF(file);
4394 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004395
Guido van Rossum60456fd1997-04-09 17:36:32 +00004396 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004397}
4398
4399
Tim Peters84e87f32001-03-17 04:50:51 +00004400static char Unpicklertype__doc__[] =
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004401"Objects that know how to unpickle";
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004402
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004403static PyTypeObject Unpicklertype = {
4404 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004405 0, /*ob_size*/
4406 "Unpickler", /*tp_name*/
4407 sizeof(Unpicklerobject), /*tp_basicsize*/
4408 0, /*tp_itemsize*/
4409 /* methods */
4410 (destructor)Unpickler_dealloc, /*tp_dealloc*/
4411 (printfunc)0, /*tp_print*/
4412 (getattrfunc)Unpickler_getattr, /*tp_getattr*/
4413 (setattrfunc)Unpickler_setattr, /*tp_setattr*/
4414 (cmpfunc)0, /*tp_compare*/
4415 (reprfunc)0, /*tp_repr*/
4416 0, /*tp_as_number*/
4417 0, /*tp_as_sequence*/
4418 0, /*tp_as_mapping*/
4419 (hashfunc)0, /*tp_hash*/
4420 (ternaryfunc)0, /*tp_call*/
4421 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004422
Guido van Rossum60456fd1997-04-09 17:36:32 +00004423 /* Space for future expansion */
4424 0L,0L,0L,0L,
4425 Unpicklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004426};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004427
Guido van Rossum60456fd1997-04-09 17:36:32 +00004428static struct PyMethodDef cPickle_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004429 {"dump", (PyCFunction)cpm_dump, 1,
4430 "dump(object, file, [binary]) --"
4431 "Write an object in pickle format to the given file\n"
4432 "\n"
4433 "If the optional argument, binary, is provided and is true, then the\n"
4434 "pickle will be written in binary format, which is more space and\n"
4435 "computationally efficient. \n"
4436 },
4437 {"dumps", (PyCFunction)cpm_dumps, 1,
4438 "dumps(object, [binary]) --"
4439 "Return a string containing an object in pickle format\n"
4440 "\n"
4441 "If the optional argument, binary, is provided and is true, then the\n"
4442 "pickle will be written in binary format, which is more space and\n"
4443 "computationally efficient. \n"
4444 },
4445 {"load", (PyCFunction)cpm_load, 1,
4446 "load(file) -- Load a pickle from the given file"},
4447 {"loads", (PyCFunction)cpm_loads, 1,
4448 "loads(string) -- Load a pickle from the given string"},
4449 {"Pickler", (PyCFunction)get_Pickler, 1,
4450 "Pickler(file, [binary]) -- Create a pickler\n"
4451 "\n"
4452 "If the optional argument, binary, is provided and is true, then\n"
4453 "pickles will be written in binary format, which is more space and\n"
4454 "computationally efficient. \n"
4455 },
4456 {"Unpickler", (PyCFunction)get_Unpickler, 1,
4457 "Unpickler(file) -- Create an unpickler"},
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004458 { NULL, NULL }
4459};
4460
Guido van Rossum60456fd1997-04-09 17:36:32 +00004461static int
Guido van Rossumebba4202000-09-07 14:35:37 +00004462init_stuff(PyObject *module_dict) {
Fred Drake2c7a6852001-07-17 18:34:03 +00004463 PyObject *copy_reg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004464
4465#define INIT_STR(S) UNLESS(S ## _str=PyString_FromString(#S)) return -1;
4466
4467 INIT_STR(__class__);
4468 INIT_STR(__getinitargs__);
4469 INIT_STR(__dict__);
4470 INIT_STR(__getstate__);
4471 INIT_STR(__setstate__);
4472 INIT_STR(__name__);
Guido van Rossum142eeb81997-08-13 03:14:41 +00004473 INIT_STR(__main__);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004474 INIT_STR(__reduce__);
4475 INIT_STR(write);
4476 INIT_STR(__safe_for_unpickling__);
4477 INIT_STR(append);
4478 INIT_STR(read);
4479 INIT_STR(readline);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004480 INIT_STR(copy_reg);
4481 INIT_STR(dispatch_table);
4482 INIT_STR(safe_constructors);
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004483 INIT_STR(__basicnew__);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004484
Guido van Rossum053b8df1998-11-25 16:18:00 +00004485 UNLESS (copy_reg = PyImport_ImportModule("copy_reg"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004486 return -1;
4487
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004488 /* These next few are special because we want to use different
4489 ones in restricted mode. */
4490
Guido van Rossum053b8df1998-11-25 16:18:00 +00004491 UNLESS (dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004492 return -1;
4493
Guido van Rossum053b8df1998-11-25 16:18:00 +00004494 UNLESS (safe_constructors = PyObject_GetAttr(copy_reg,
4495 safe_constructors_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004496 return -1;
4497
4498 Py_DECREF(copy_reg);
4499
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004500 /* Down to here ********************************** */
4501
Guido van Rossum053b8df1998-11-25 16:18:00 +00004502 UNLESS (empty_tuple = PyTuple_New(0))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004503 return -1;
4504
Guido van Rossumc03158b1999-06-09 15:23:31 +00004505 /* Ugh */
4506 UNLESS (t=PyImport_ImportModule("__builtin__")) return -1;
4507 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
4508 return -1;
4509
4510 UNLESS (t=PyDict_New()) return -1;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004511 UNLESS (r=PyRun_String(
4512 "def __init__(self, *args): self.args=args\n\n"
4513 "def __str__(self):\n"
4514 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
4515 Py_file_input,
4516 module_dict, t) ) return -1;
4517 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00004518
4519 UNLESS (PickleError = PyErr_NewException("cPickle.PickleError", NULL, t))
4520 return -1;
4521
4522 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00004523
Tim Peters84e87f32001-03-17 04:50:51 +00004524
4525 UNLESS (PicklingError = PyErr_NewException("cPickle.PicklingError",
Guido van Rossumc03158b1999-06-09 15:23:31 +00004526 PickleError, NULL))
4527 return -1;
4528
4529 UNLESS (t=PyDict_New()) return -1;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004530 UNLESS (r=PyRun_String(
4531 "def __init__(self, *args): self.args=args\n\n"
4532 "def __str__(self):\n"
4533 " a=self.args\n"
4534 " a=a and type(a[0]) or '(what)'\n"
4535 " return 'Cannot pickle %s objects' % a\n"
4536 , Py_file_input,
4537 module_dict, t) ) return -1;
4538 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00004539
4540 UNLESS (UnpickleableError = PyErr_NewException(
4541 "cPickle.UnpickleableError", PicklingError, t))
4542 return -1;
4543
4544 Py_DECREF(t);
4545
Tim Peters84e87f32001-03-17 04:50:51 +00004546 UNLESS (UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
Guido van Rossumc03158b1999-06-09 15:23:31 +00004547 PickleError, NULL))
4548 return -1;
4549
Tim Peters84e87f32001-03-17 04:50:51 +00004550 if (PyDict_SetItemString(module_dict, "PickleError",
Guido van Rossumc03158b1999-06-09 15:23:31 +00004551 PickleError) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004552 return -1;
4553
Tim Peters84e87f32001-03-17 04:50:51 +00004554 if (PyDict_SetItemString(module_dict, "PicklingError",
Guido van Rossum60456fd1997-04-09 17:36:32 +00004555 PicklingError) < 0)
4556 return -1;
4557
Guido van Rossum60456fd1997-04-09 17:36:32 +00004558 if (PyDict_SetItemString(module_dict, "UnpicklingError",
4559 UnpicklingError) < 0)
4560 return -1;
4561
Guido van Rossumc03158b1999-06-09 15:23:31 +00004562 if (PyDict_SetItemString(module_dict, "UnpickleableError",
4563 UnpickleableError) < 0)
4564 return -1;
4565
Guido van Rossum053b8df1998-11-25 16:18:00 +00004566 UNLESS (BadPickleGet = PyString_FromString("cPickle.BadPickleGet"))
4567 return -1;
4568
4569 if (PyDict_SetItemString(module_dict, "BadPickleGet",
4570 BadPickleGet) < 0)
4571 return -1;
4572
Guido van Rossum60456fd1997-04-09 17:36:32 +00004573 PycString_IMPORT;
Tim Peters84e87f32001-03-17 04:50:51 +00004574
Guido van Rossum60456fd1997-04-09 17:36:32 +00004575 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004576}
4577
Guido van Rossumf9ffb031999-02-04 14:54:04 +00004578#ifndef DL_EXPORT /* declarations for DLL import/export */
4579#define DL_EXPORT(RTYPE) RTYPE
4580#endif
Guido van Rossum50f385c1998-12-04 18:48:44 +00004581DL_EXPORT(void)
Thomas Wouters58d05102000-07-24 14:43:35 +00004582initcPickle(void) {
Guido van Rossumebba4202000-09-07 14:35:37 +00004583 PyObject *m, *d, *di, *v, *k;
4584 int i;
Guido van Rossum2f80d961999-07-13 15:18:58 +00004585 char *rev="1.71";
Guido van Rossum60456fd1997-04-09 17:36:32 +00004586 PyObject *format_version;
4587 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004588
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004589 Picklertype.ob_type = &PyType_Type;
4590 Unpicklertype.ob_type = &PyType_Type;
Guido van Rossum053b8df1998-11-25 16:18:00 +00004591 PdataType.ob_type = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004592
Tim Peters84e87f32001-03-17 04:50:51 +00004593 /* Initialize some pieces. We need to do this before module creation,
4594 so we're forced to use a temporary dictionary. :(
Guido van Rossumebba4202000-09-07 14:35:37 +00004595 */
4596 di=PyDict_New();
4597 if (!di) return;
4598 if (init_stuff(di) < 0) return;
4599
Guido van Rossum60456fd1997-04-09 17:36:32 +00004600 /* Create the module and add the functions */
4601 m = Py_InitModule4("cPickle", cPickle_methods,
4602 cPickle_module_documentation,
4603 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004604
Guido van Rossum60456fd1997-04-09 17:36:32 +00004605 /* Add some symbolic constants to the module */
4606 d = PyModule_GetDict(m);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004607 PyDict_SetItemString(d,"__version__", v = PyString_FromString(rev));
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004608 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00004609
Guido van Rossumebba4202000-09-07 14:35:37 +00004610 /* Copy data from di. Waaa. */
4611 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
4612 if (PyObject_SetItem(d, k, v) < 0) {
4613 Py_DECREF(di);
4614 return;
4615 }
4616 }
4617 Py_DECREF(di);
4618
Guido van Rossum60456fd1997-04-09 17:36:32 +00004619 format_version = PyString_FromString("1.3");
4620 compatible_formats = Py_BuildValue("[sss]", "1.0", "1.1", "1.2");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004621
Guido van Rossum60456fd1997-04-09 17:36:32 +00004622 PyDict_SetItemString(d, "format_version", format_version);
4623 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004624 Py_XDECREF(format_version);
4625 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004626}