blob: 2b058ef446385676ca3dc36b0b616e352ce2497f [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
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001175#ifdef Py_USING_UNICODE
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001176/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1177 backslash and newline characters to \uXXXX escapes. */
1178static PyObject *
1179modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, int size)
1180{
1181 PyObject *repr;
1182 char *p;
1183 char *q;
1184
1185 static const char *hexdigit = "0123456789ABCDEF";
1186
1187 repr = PyString_FromStringAndSize(NULL, 6 * size);
1188 if (repr == NULL)
1189 return NULL;
1190 if (size == 0)
1191 return repr;
1192
1193 p = q = PyString_AS_STRING(repr);
1194 while (size-- > 0) {
1195 Py_UNICODE ch = *s++;
1196 /* Map 16-bit characters to '\uxxxx' */
1197 if (ch >= 256 || ch == '\\' || ch == '\n') {
1198 *p++ = '\\';
1199 *p++ = 'u';
1200 *p++ = hexdigit[(ch >> 12) & 0xf];
1201 *p++ = hexdigit[(ch >> 8) & 0xf];
1202 *p++ = hexdigit[(ch >> 4) & 0xf];
1203 *p++ = hexdigit[ch & 15];
1204 }
1205 /* Copy everything else as-is */
1206 else
1207 *p++ = (char) ch;
1208 }
1209 *p = '\0';
1210 if (_PyString_Resize(&repr, p - q))
1211 goto onError;
1212
1213 return repr;
1214
1215 onError:
1216 Py_DECREF(repr);
1217 return NULL;
1218}
1219
1220
Guido van Rossum60456fd1997-04-09 17:36:32 +00001221static int
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001222save_unicode(Picklerobject *self, PyObject *args, int doput) {
1223 int size, len;
1224 PyObject *repr=0;
1225
1226 if (!PyUnicode_Check(args))
1227 return -1;
1228
1229 if (!self->bin) {
1230 char *repr_str;
1231 static char string = UNICODE;
1232
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001233 UNLESS(repr = modified_EncodeRawUnicodeEscape(
1234 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args)))
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001235 return -1;
1236
1237 if ((len = PyString_Size(repr)) < 0)
1238 goto err;
1239 repr_str = PyString_AS_STRING((PyStringObject *)repr);
1240
1241 if ((*self->write_func)(self, &string, 1) < 0)
1242 goto err;
1243
1244 if ((*self->write_func)(self, repr_str, len) < 0)
1245 goto err;
1246
1247 if ((*self->write_func)(self, "\n", 1) < 0)
1248 goto err;
1249
1250 Py_XDECREF(repr);
1251 }
1252 else {
1253 int i;
1254 char c_str[5];
1255
1256 UNLESS (repr = PyUnicode_AsUTF8String(args))
1257 return -1;
1258
1259 if ((size = PyString_Size(repr)) < 0)
1260 goto err;
1261
1262 c_str[0] = BINUNICODE;
1263 for (i = 1; i < 5; i++)
1264 c_str[i] = (int)(size >> ((i - 1) * 8));
1265 len = 5;
1266
1267 if ((*self->write_func)(self, c_str, len) < 0)
1268 goto err;
1269
1270 if (size > 128 && Pdata_Check(self->file)) {
1271 if (write_other(self, NULL, 0) < 0)
1272 goto err;
1273 PDATA_APPEND(self->file, repr, -1);
1274 }
1275 else {
1276 if ((*self->write_func)(self, PyString_AS_STRING(repr), size) < 0)
1277 goto err;
1278 }
1279
1280 Py_DECREF(repr);
1281 }
1282
1283 if (doput)
1284 if (put(self, args) < 0)
1285 return -1;
1286
1287 return 0;
1288
1289err:
1290 Py_XDECREF(repr);
1291 return -1;
1292}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001293#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001294
1295
1296static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00001297save_tuple(Picklerobject *self, PyObject *args) {
1298 PyObject *element = 0, *py_tuple_id = 0;
1299 int len, i, has_key, res = -1;
1300
1301 static char tuple = TUPLE;
1302
1303 if ((*self->write_func)(self, &MARKv, 1) < 0)
1304 goto finally;
1305
Tim Peters84e87f32001-03-17 04:50:51 +00001306 if ((len = PyTuple_Size(args)) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001307 goto finally;
1308
1309 for (i = 0; i < len; i++) {
Tim Peters84e87f32001-03-17 04:50:51 +00001310 UNLESS (element = PyTuple_GET_ITEM((PyTupleObject *)args, i))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001311 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001312
Guido van Rossum60456fd1997-04-09 17:36:32 +00001313 if (save(self, element, 0) < 0)
1314 goto finally;
1315 }
1316
Guido van Rossum534b7c52000-06-28 22:23:56 +00001317 UNLESS (py_tuple_id = PyLong_FromVoidPtr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001318 goto finally;
1319
1320 if (len) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001321 if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_tuple_id)) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001322 goto finally;
1323
1324 if (has_key) {
1325 if (self->bin) {
1326 static char pop_mark = POP_MARK;
Tim Peters84e87f32001-03-17 04:50:51 +00001327
Guido van Rossum60456fd1997-04-09 17:36:32 +00001328 if ((*self->write_func)(self, &pop_mark, 1) < 0)
1329 goto finally;
1330 }
1331 else {
1332 static char pop = POP;
Tim Peters84e87f32001-03-17 04:50:51 +00001333
Guido van Rossum60456fd1997-04-09 17:36:32 +00001334 for (i = 0; i <= len; i++) {
1335 if ((*self->write_func)(self, &pop, 1) < 0)
1336 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001337 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001338 }
Tim Peters84e87f32001-03-17 04:50:51 +00001339
Guido van Rossum60456fd1997-04-09 17:36:32 +00001340 if (get(self, py_tuple_id) < 0)
1341 goto finally;
1342
1343 res = 0;
1344 goto finally;
1345 }
1346 }
1347
1348 if ((*self->write_func)(self, &tuple, 1) < 0) {
1349 goto finally;
1350 }
1351
1352 if (put(self, args) < 0)
1353 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001354
Guido van Rossum60456fd1997-04-09 17:36:32 +00001355 res = 0;
1356
1357finally:
1358 Py_XDECREF(py_tuple_id);
1359
1360 return res;
1361}
1362
1363static int
1364save_empty_tuple(Picklerobject *self, PyObject *args) {
1365 static char tuple = EMPTY_TUPLE;
1366
1367 return (*self->write_func)(self, &tuple, 1);
1368}
1369
1370
1371static int
1372save_list(Picklerobject *self, PyObject *args) {
1373 PyObject *element = 0;
1374 int s_len, len, i, using_appends, res = -1;
1375 char s[3];
1376
1377 static char append = APPEND, appends = APPENDS;
1378
Guido van Rossum053b8df1998-11-25 16:18:00 +00001379 if (self->bin) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001380 s[0] = EMPTY_LIST;
1381 s_len = 1;
Tim Peters84e87f32001-03-17 04:50:51 +00001382 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001383 else {
1384 s[0] = MARK;
1385 s[1] = LIST;
1386 s_len = 2;
1387 }
1388
1389 if ((len = PyList_Size(args)) < 0)
1390 goto finally;
1391
1392 if ((*self->write_func)(self, s, s_len) < 0)
1393 goto finally;
1394
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001395 if (len == 0) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001396 if (put(self, args) < 0)
1397 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001398 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001399 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001400 if (put2(self, args) < 0)
1401 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001402 }
1403
Guido van Rossum142eeb81997-08-13 03:14:41 +00001404 if ((using_appends = (self->bin && (len > 1))))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001405 if ((*self->write_func)(self, &MARKv, 1) < 0)
1406 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001407
Guido van Rossum60456fd1997-04-09 17:36:32 +00001408 for (i = 0; i < len; i++) {
Tim Peters84e87f32001-03-17 04:50:51 +00001409 UNLESS (element = PyList_GET_ITEM((PyListObject *)args, i))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001410 goto finally;
1411
Tim Peters84e87f32001-03-17 04:50:51 +00001412 if (save(self, element, 0) < 0)
1413 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001414
1415 if (!using_appends) {
1416 if ((*self->write_func)(self, &append, 1) < 0)
1417 goto finally;
1418 }
1419 }
1420
1421 if (using_appends) {
1422 if ((*self->write_func)(self, &appends, 1) < 0)
1423 goto finally;
1424 }
1425
1426 res = 0;
1427
1428finally:
1429
1430 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001431}
1432
1433
Guido van Rossum60456fd1997-04-09 17:36:32 +00001434static int
1435save_dict(Picklerobject *self, PyObject *args) {
1436 PyObject *key = 0, *value = 0;
1437 int i, len, res = -1, using_setitems;
1438 char s[3];
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001439
Guido van Rossum60456fd1997-04-09 17:36:32 +00001440 static char setitem = SETITEM, setitems = SETITEMS;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001441
Guido van Rossum60456fd1997-04-09 17:36:32 +00001442 if (self->bin) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001443 s[0] = EMPTY_DICT;
1444 len = 1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001445 }
1446 else {
1447 s[0] = MARK;
1448 s[1] = DICT;
1449 len = 2;
1450 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001451
Guido van Rossum60456fd1997-04-09 17:36:32 +00001452 if ((*self->write_func)(self, s, len) < 0)
1453 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001454
Guido van Rossum60456fd1997-04-09 17:36:32 +00001455 if ((len = PyDict_Size(args)) < 0)
1456 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001457
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001458 if (len == 0) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001459 if (put(self, args) < 0)
1460 goto finally;
1461 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001462 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001463 if (put2(self, args) < 0)
1464 goto finally;
1465 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001466
Guido van Rossum142eeb81997-08-13 03:14:41 +00001467 if ((using_setitems = (self->bin && (PyDict_Size(args) > 1))))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001468 if ((*self->write_func)(self, &MARKv, 1) < 0)
1469 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001470
Guido van Rossum60456fd1997-04-09 17:36:32 +00001471 i = 0;
1472 while (PyDict_Next(args, &i, &key, &value)) {
1473 if (save(self, key, 0) < 0)
1474 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001475
Guido van Rossum60456fd1997-04-09 17:36:32 +00001476 if (save(self, value, 0) < 0)
1477 goto finally;
1478
1479 if (!using_setitems) {
1480 if ((*self->write_func)(self, &setitem, 1) < 0)
1481 goto finally;
1482 }
1483 }
1484
1485 if (using_setitems) {
1486 if ((*self->write_func)(self, &setitems, 1) < 0)
1487 goto finally;
1488 }
1489
1490 res = 0;
1491
1492finally:
1493
1494 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001495}
1496
1497
Tim Peters84e87f32001-03-17 04:50:51 +00001498static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00001499save_inst(Picklerobject *self, PyObject *args) {
Tim Peters84e87f32001-03-17 04:50:51 +00001500 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
Guido van Rossum60456fd1997-04-09 17:36:32 +00001501 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1502 char *module_str, *name_str;
1503 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001504
Guido van Rossum60456fd1997-04-09 17:36:32 +00001505 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001506
Guido van Rossum60456fd1997-04-09 17:36:32 +00001507 if ((*self->write_func)(self, &MARKv, 1) < 0)
1508 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001509
Guido van Rossum053b8df1998-11-25 16:18:00 +00001510 UNLESS (class = PyObject_GetAttr(args, __class___str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001511 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001512
Guido van Rossum60456fd1997-04-09 17:36:32 +00001513 if (self->bin) {
1514 if (save(self, class, 0) < 0)
1515 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001516 }
1517
Guido van Rossum142eeb81997-08-13 03:14:41 +00001518 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001519 PyObject *element = 0;
1520 int i, len;
1521
Tim Peters84e87f32001-03-17 04:50:51 +00001522 UNLESS (class_args =
Guido van Rossum60456fd1997-04-09 17:36:32 +00001523 PyObject_CallObject(getinitargs_func, empty_tuple))
1524 goto finally;
1525
Tim Peters84e87f32001-03-17 04:50:51 +00001526 if ((len = PyObject_Size(class_args)) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001527 goto finally;
1528
1529 for (i = 0; i < len; i++) {
Tim Peters84e87f32001-03-17 04:50:51 +00001530 UNLESS (element = PySequence_GetItem(class_args, i))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001531 goto finally;
1532
1533 if (save(self, element, 0) < 0) {
1534 Py_DECREF(element);
1535 goto finally;
1536 }
1537
1538 Py_DECREF(element);
1539 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001540 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001541 else {
1542 PyErr_Clear();
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001543 }
1544
Guido van Rossum60456fd1997-04-09 17:36:32 +00001545 if (!self->bin) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001546 UNLESS (name = ((PyClassObject *)class)->cl_name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001547 PyErr_SetString(PicklingError, "class has no name");
1548 goto finally;
1549 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001550
Guido van Rossum053b8df1998-11-25 16:18:00 +00001551 UNLESS (module = whichmodule(class, name))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001552 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001553
Tim Peters84e87f32001-03-17 04:50:51 +00001554
Guido van Rossum053b8df1998-11-25 16:18:00 +00001555 if ((module_size = PyString_Size(module)) < 0 ||
1556 (name_size = PyString_Size(name)) < 0)
1557 goto finally;
1558
Guido van Rossum60456fd1997-04-09 17:36:32 +00001559 module_str = PyString_AS_STRING((PyStringObject *)module);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001560 name_str = PyString_AS_STRING((PyStringObject *)name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001561
Guido van Rossum60456fd1997-04-09 17:36:32 +00001562 if ((*self->write_func)(self, &inst, 1) < 0)
1563 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001564
Guido van Rossum60456fd1997-04-09 17:36:32 +00001565 if ((*self->write_func)(self, module_str, module_size) < 0)
1566 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001567
Guido van Rossum60456fd1997-04-09 17:36:32 +00001568 if ((*self->write_func)(self, "\n", 1) < 0)
1569 goto finally;
1570
1571 if ((*self->write_func)(self, name_str, name_size) < 0)
1572 goto finally;
1573
1574 if ((*self->write_func)(self, "\n", 1) < 0)
1575 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001576 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001577 else if ((*self->write_func)(self, &obj, 1) < 0) {
1578 goto finally;
1579 }
1580
Guido van Rossum142eeb81997-08-13 03:14:41 +00001581 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001582 UNLESS (state = PyObject_CallObject(getstate_func, empty_tuple))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001583 goto finally;
1584 }
1585 else {
1586 PyErr_Clear();
1587
Guido van Rossum053b8df1998-11-25 16:18:00 +00001588 UNLESS (state = PyObject_GetAttr(args, __dict___str)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001589 PyErr_Clear();
1590 res = 0;
1591 goto finally;
1592 }
1593 }
1594
1595 if (!PyDict_Check(state)) {
1596 if (put2(self, args) < 0)
1597 goto finally;
1598 }
1599 else {
1600 if (put(self, args) < 0)
1601 goto finally;
1602 }
Tim Peters84e87f32001-03-17 04:50:51 +00001603
Guido van Rossum60456fd1997-04-09 17:36:32 +00001604 if (save(self, state, 0) < 0)
1605 goto finally;
1606
1607 if ((*self->write_func)(self, &build, 1) < 0)
1608 goto finally;
1609
1610 res = 0;
1611
1612finally:
1613 Py_XDECREF(module);
1614 Py_XDECREF(class);
1615 Py_XDECREF(state);
1616 Py_XDECREF(getinitargs_func);
1617 Py_XDECREF(getstate_func);
1618 Py_XDECREF(class_args);
1619
1620 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001621}
1622
1623
Guido van Rossum60456fd1997-04-09 17:36:32 +00001624static int
1625save_global(Picklerobject *self, PyObject *args, PyObject *name) {
Guido van Rossuma92d16a2001-08-18 21:22:07 +00001626 PyObject *global_name = 0, *module = 0, *mod = 0, *moddict = 0, *klass = 0;
Tim Peters84e87f32001-03-17 04:50:51 +00001627 char *name_str, *module_str;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001628 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001629
Guido van Rossum60456fd1997-04-09 17:36:32 +00001630 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001631
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001632 if (name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001633 global_name = name;
1634 Py_INCREF(global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001635 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001636 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001637 UNLESS (global_name = PyObject_GetAttr(args, __name___str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001638 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001639 }
1640
Guido van Rossum053b8df1998-11-25 16:18:00 +00001641 UNLESS (module = whichmodule(args, global_name))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001642 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001643
Guido van Rossum053b8df1998-11-25 16:18:00 +00001644 if ((module_size = PyString_Size(module)) < 0 ||
1645 (name_size = PyString_Size(global_name)) < 0)
1646 goto finally;
1647
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001648 module_str = PyString_AS_STRING((PyStringObject *)module);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001649 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001650
Guido van Rossuma92d16a2001-08-18 21:22:07 +00001651 mod = PyImport_ImportModule(module_str);
1652 if (mod == NULL) {
1653 /* Py_ErrClear(); ?? */
1654 cPickle_ErrFormat(PicklingError,
1655 "Can't pickle %s: it's not found as %s.%s",
1656 "OSS", args, module, global_name);
1657 goto finally;
1658 }
1659 moddict = PyModule_GetDict(mod); /* borrowed ref */
1660 klass = PyDict_GetItemString(moddict, name_str); /* borrowed ref */
1661 if (klass == NULL) {
1662 cPickle_ErrFormat(PicklingError,
1663 "Can't pickle %s: it's not found as %s.%s",
1664 "OSS", args, module, global_name);
1665 goto finally;
1666 }
1667 if (klass != args) {
1668 cPickle_ErrFormat(PicklingError,
1669 "Can't pickle %s: it's not the same object as %s.%s",
1670 "OSS", args, module, global_name);
1671 goto finally;
1672 }
1673
Guido van Rossum60456fd1997-04-09 17:36:32 +00001674 if ((*self->write_func)(self, &global, 1) < 0)
1675 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001676
Guido van Rossum60456fd1997-04-09 17:36:32 +00001677 if ((*self->write_func)(self, module_str, module_size) < 0)
1678 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001679
Guido van Rossum60456fd1997-04-09 17:36:32 +00001680 if ((*self->write_func)(self, "\n", 1) < 0)
1681 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001682
Guido van Rossum60456fd1997-04-09 17:36:32 +00001683 if ((*self->write_func)(self, name_str, name_size) < 0)
1684 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001685
Guido van Rossum60456fd1997-04-09 17:36:32 +00001686 if ((*self->write_func)(self, "\n", 1) < 0)
1687 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001688
Guido van Rossum60456fd1997-04-09 17:36:32 +00001689 if (put(self, args) < 0)
1690 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001691
Guido van Rossum60456fd1997-04-09 17:36:32 +00001692 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001693
Guido van Rossum60456fd1997-04-09 17:36:32 +00001694finally:
1695 Py_XDECREF(module);
1696 Py_XDECREF(global_name);
Guido van Rossuma92d16a2001-08-18 21:22:07 +00001697 Py_XDECREF(mod);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001698
Guido van Rossum60456fd1997-04-09 17:36:32 +00001699 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001700}
1701
Guido van Rossum60456fd1997-04-09 17:36:32 +00001702static int
1703save_pers(Picklerobject *self, PyObject *args, PyObject *f) {
1704 PyObject *pid = 0;
1705 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001706
Guido van Rossum60456fd1997-04-09 17:36:32 +00001707 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001708
Guido van Rossum053b8df1998-11-25 16:18:00 +00001709 Py_INCREF(args);
1710 ARG_TUP(self, args);
1711 if (self->arg) {
1712 pid = PyObject_CallObject(f, self->arg);
1713 FREE_ARG_TUP(self);
1714 }
1715 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001716
Guido van Rossum60456fd1997-04-09 17:36:32 +00001717 if (pid != Py_None) {
1718 if (!self->bin) {
1719 if (!PyString_Check(pid)) {
Tim Peters84e87f32001-03-17 04:50:51 +00001720 PyErr_SetString(PicklingError,
Guido van Rossum60456fd1997-04-09 17:36:32 +00001721 "persistent id must be string");
1722 goto finally;
1723 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001724
Guido van Rossum60456fd1997-04-09 17:36:32 +00001725 if ((*self->write_func)(self, &persid, 1) < 0)
1726 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001727
Guido van Rossum60456fd1997-04-09 17:36:32 +00001728 if ((size = PyString_Size(pid)) < 0)
1729 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001730
Tim Peters84e87f32001-03-17 04:50:51 +00001731 if ((*self->write_func)(self,
Guido van Rossum60456fd1997-04-09 17:36:32 +00001732 PyString_AS_STRING((PyStringObject *)pid), size) < 0)
1733 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001734
Guido van Rossum60456fd1997-04-09 17:36:32 +00001735 if ((*self->write_func)(self, "\n", 1) < 0)
1736 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001737
Guido van Rossum60456fd1997-04-09 17:36:32 +00001738 res = 1;
1739 goto finally;
1740 }
1741 else if (save(self, pid, 1) >= 0) {
1742 if ((*self->write_func)(self, &binpersid, 1) < 0)
1743 res = -1;
1744 else
1745 res = 1;
1746 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001747
Tim Peters84e87f32001-03-17 04:50:51 +00001748 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001749 }
1750
Guido van Rossum60456fd1997-04-09 17:36:32 +00001751 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001752
Guido van Rossum60456fd1997-04-09 17:36:32 +00001753finally:
1754 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001755
Guido van Rossum60456fd1997-04-09 17:36:32 +00001756 return res;
1757}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001758
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001759
Tim Peters84e87f32001-03-17 04:50:51 +00001760static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00001761save_reduce(Picklerobject *self, PyObject *callable,
1762 PyObject *tup, PyObject *state, PyObject *ob) {
1763 static char reduce = REDUCE, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001764
Guido van Rossum60456fd1997-04-09 17:36:32 +00001765 if (save(self, callable, 0) < 0)
1766 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001767
Guido van Rossum60456fd1997-04-09 17:36:32 +00001768 if (save(self, tup, 0) < 0)
1769 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001770
Guido van Rossum60456fd1997-04-09 17:36:32 +00001771 if ((*self->write_func)(self, &reduce, 1) < 0)
1772 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001773
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001774 if (ob != NULL) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001775 if (state && !PyDict_Check(state)) {
1776 if (put2(self, ob) < 0)
1777 return -1;
1778 }
1779 else {
1780 if (put(self, ob) < 0)
1781 return -1;
1782 }
1783 }
Tim Peters84e87f32001-03-17 04:50:51 +00001784
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001785 if (state) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001786 if (save(self, state, 0) < 0)
1787 return -1;
1788
1789 if ((*self->write_func)(self, &build, 1) < 0)
1790 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001791 }
1792
Guido van Rossum60456fd1997-04-09 17:36:32 +00001793 return 0;
1794}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001795
Guido van Rossum60456fd1997-04-09 17:36:32 +00001796static int
1797save(Picklerobject *self, PyObject *args, int pers_save) {
1798 PyTypeObject *type;
1799 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0, *arg_tup = 0,
Guido van Rossum142eeb81997-08-13 03:14:41 +00001800 *callable = 0, *state = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001801 int res = -1, tmp, size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001802
Guido van Rossum60456fd1997-04-09 17:36:32 +00001803 if (!pers_save && self->pers_func) {
1804 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
1805 res = tmp;
1806 goto finally;
1807 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001808 }
1809
Guido van Rossum60456fd1997-04-09 17:36:32 +00001810 if (args == Py_None) {
1811 res = save_none(self, args);
1812 goto finally;
1813 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001814
Guido van Rossum60456fd1997-04-09 17:36:32 +00001815 type = args->ob_type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001816
Guido van Rossum60456fd1997-04-09 17:36:32 +00001817 switch (type->tp_name[0]) {
1818 case 'i':
1819 if (type == &PyInt_Type) {
1820 res = save_int(self, args);
1821 goto finally;
1822 }
1823 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001824
Guido van Rossum60456fd1997-04-09 17:36:32 +00001825 case 'l':
1826 if (type == &PyLong_Type) {
1827 res = save_long(self, args);
1828 goto finally;
1829 }
1830 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001831
Guido van Rossum60456fd1997-04-09 17:36:32 +00001832 case 'f':
1833 if (type == &PyFloat_Type) {
1834 res = save_float(self, args);
1835 goto finally;
1836 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001837 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001838
Guido van Rossum60456fd1997-04-09 17:36:32 +00001839 case 't':
1840 if (type == &PyTuple_Type && PyTuple_Size(args)==0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001841 if (self->bin) res = save_empty_tuple(self, args);
1842 else res = save_tuple(self, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001843 goto finally;
1844 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001845 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001846
Guido van Rossum60456fd1997-04-09 17:36:32 +00001847 case 's':
Guido van Rossum053b8df1998-11-25 16:18:00 +00001848 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001849 res = save_string(self, args, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001850 goto finally;
1851 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001852
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001853#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001854 case 'u':
1855 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
1856 res = save_unicode(self, args, 0);
1857 goto finally;
1858 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001859#endif
Guido van Rossum60456fd1997-04-09 17:36:32 +00001860 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001861
Guido van Rossum60456fd1997-04-09 17:36:32 +00001862 if (args->ob_refcnt > 1) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001863 int has_key;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001864
Guido van Rossum534b7c52000-06-28 22:23:56 +00001865 UNLESS (py_ob_id = PyLong_FromVoidPtr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001866 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001867
Guido van Rossum60456fd1997-04-09 17:36:32 +00001868 if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_ob_id)) < 0)
1869 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001870
Guido van Rossum60456fd1997-04-09 17:36:32 +00001871 if (has_key) {
1872 if (get(self, py_ob_id) < 0)
1873 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001874
Guido van Rossum60456fd1997-04-09 17:36:32 +00001875 res = 0;
1876 goto finally;
1877 }
1878 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001879
Guido van Rossum60456fd1997-04-09 17:36:32 +00001880 switch (type->tp_name[0]) {
1881 case 's':
1882 if (type == &PyString_Type) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001883 res = save_string(self, args, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001884 goto finally;
1885 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001886 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001887
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001888#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001889 case 'u':
1890 if (type == &PyUnicode_Type) {
1891 res = save_unicode(self, args, 1);
1892 goto finally;
1893 }
1894 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001895#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001896
Guido van Rossum60456fd1997-04-09 17:36:32 +00001897 case 't':
1898 if (type == &PyTuple_Type) {
1899 res = save_tuple(self, args);
1900 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001901 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001902 if (type == &PyType_Type) {
1903 res = save_global(self, args, NULL);
1904 goto finally;
1905 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001906 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001907
Guido van Rossum60456fd1997-04-09 17:36:32 +00001908 case 'l':
1909 if (type == &PyList_Type) {
1910 res = save_list(self, args);
1911 goto finally;
1912 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001913 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001914
1915 case 'd':
1916 if (type == &PyDict_Type) {
1917 res = save_dict(self, args);
Tim Peters84e87f32001-03-17 04:50:51 +00001918 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001919 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001920 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001921
1922 case 'i':
1923 if (type == &PyInstance_Type) {
1924 res = save_inst(self, args);
1925 goto finally;
1926 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001927 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001928
1929 case 'c':
1930 if (type == &PyClass_Type) {
1931 res = save_global(self, args, NULL);
1932 goto finally;
1933 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001934 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001935
1936 case 'f':
1937 if (type == &PyFunction_Type) {
1938 res = save_global(self, args, NULL);
1939 goto finally;
1940 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001941 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001942
1943 case 'b':
1944 if (type == &PyCFunction_Type) {
1945 res = save_global(self, args, NULL);
1946 goto finally;
1947 }
1948 }
1949
1950 if (!pers_save && self->inst_pers_func) {
1951 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
1952 res = tmp;
1953 goto finally;
1954 }
1955 }
1956
Guido van Rossum142eeb81997-08-13 03:14:41 +00001957 if ((__reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001958 Py_INCREF(__reduce__);
1959
Guido van Rossum60456fd1997-04-09 17:36:32 +00001960 Py_INCREF(args);
Guido van Rossum053b8df1998-11-25 16:18:00 +00001961 ARG_TUP(self, args);
1962 if (self->arg) {
1963 t = PyObject_CallObject(__reduce__, self->arg);
1964 FREE_ARG_TUP(self);
1965 }
1966 if (! t) goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001967 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001968 else {
1969 PyErr_Clear();
1970
Guido van Rossum142eeb81997-08-13 03:14:41 +00001971 if ((__reduce__ = PyObject_GetAttr(args, __reduce___str))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001972 UNLESS (t = PyObject_CallObject(__reduce__, empty_tuple))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001973 goto finally;
1974 }
1975 else {
1976 PyErr_Clear();
1977 }
1978 }
1979
1980 if (t) {
1981 if (PyString_Check(t)) {
1982 res = save_global(self, args, t);
1983 goto finally;
1984 }
Tim Peters84e87f32001-03-17 04:50:51 +00001985
Guido van Rossum60456fd1997-04-09 17:36:32 +00001986 if (!PyTuple_Check(t)) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001987 cPickle_ErrFormat(PicklingError, "Value returned by %s must "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001988 "be a tuple", "O", __reduce__);
1989 goto finally;
1990 }
1991
1992 size = PyTuple_Size(t);
Tim Peters84e87f32001-03-17 04:50:51 +00001993
Guido van Rossum60456fd1997-04-09 17:36:32 +00001994 if ((size != 3) && (size != 2)) {
Tim Peters84e87f32001-03-17 04:50:51 +00001995 cPickle_ErrFormat(PicklingError, "tuple returned by %s must "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001996 "contain only two or three elements", "O", __reduce__);
1997 goto finally;
1998 }
Tim Peters84e87f32001-03-17 04:50:51 +00001999
Guido van Rossum60456fd1997-04-09 17:36:32 +00002000 callable = PyTuple_GET_ITEM(t, 0);
Guido van Rossum142eeb81997-08-13 03:14:41 +00002001
Guido van Rossum60456fd1997-04-09 17:36:32 +00002002 arg_tup = PyTuple_GET_ITEM(t, 1);
2003
2004 if (size > 2) {
2005 state = PyTuple_GET_ITEM(t, 2);
2006 }
2007
Guido van Rossum053b8df1998-11-25 16:18:00 +00002008 UNLESS (PyTuple_Check(arg_tup) || arg_tup==Py_None) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00002009 cPickle_ErrFormat(PicklingError, "Second element of tuple "
Guido van Rossum60456fd1997-04-09 17:36:32 +00002010 "returned by %s must be a tuple", "O", __reduce__);
2011 goto finally;
2012 }
2013
2014 res = save_reduce(self, callable, arg_tup, state, args);
2015 goto finally;
2016 }
2017
Guido van Rossumc03158b1999-06-09 15:23:31 +00002018 PyErr_SetObject(UnpickleableError, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002019
2020finally:
2021 Py_XDECREF(py_ob_id);
2022 Py_XDECREF(__reduce__);
2023 Py_XDECREF(t);
Tim Peters84e87f32001-03-17 04:50:51 +00002024
Guido van Rossum60456fd1997-04-09 17:36:32 +00002025 return res;
2026}
2027
2028
2029static int
2030dump(Picklerobject *self, PyObject *args) {
2031 static char stop = STOP;
2032
2033 if (save(self, args, 0) < 0)
2034 return -1;
2035
2036 if ((*self->write_func)(self, &stop, 1) < 0)
2037 return -1;
2038
2039 if ((*self->write_func)(self, NULL, 0) < 0)
2040 return -1;
2041
2042 return 0;
2043}
2044
2045static PyObject *
Guido van Rossum053b8df1998-11-25 16:18:00 +00002046Pickle_clear_memo(Picklerobject *self, PyObject *args) {
Guido van Rossum43713e52000-02-29 13:59:29 +00002047 if (args && ! PyArg_ParseTuple(args,":clear_memo")) return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002048 if (self->memo) PyDict_Clear(self->memo);
2049 Py_INCREF(Py_None);
2050 return Py_None;
2051}
2052
2053static PyObject *
2054Pickle_getvalue(Picklerobject *self, PyObject *args) {
2055 int l, i, rsize, ssize, clear=1, lm;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00002056 long ik;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002057 PyObject *k, *r;
2058 char *s, *p, *have_get;
2059 Pdata *data;
2060
Guido van Rossum43713e52000-02-29 13:59:29 +00002061 if (args && ! PyArg_ParseTuple(args,"|i:getvalue",&clear)) return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002062
2063 /* Check to make sure we are based on a list */
2064 if (! Pdata_Check(self->file)) {
2065 PyErr_SetString(PicklingError,
2066 "Attempt to getvalue a non-list-based pickler");
2067 return NULL;
2068 }
2069
2070 /* flush write buffer */
2071 if (write_other(self, NULL, 0) < 0) return NULL;
2072
2073 data=(Pdata*)self->file;
2074 l=data->length;
2075
2076 /* set up an array to hold get/put status */
2077 if ((lm=PyDict_Size(self->memo)) < 0) return NULL;
2078 lm++;
Tim Peters84e87f32001-03-17 04:50:51 +00002079 if (! (have_get=malloc((lm)*sizeof(char)))) return PyErr_NoMemory();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002080 memset(have_get,0,lm);
2081
2082 /* Scan for gets. */
2083 for (rsize=0, i=l; --i >= 0; ) {
2084 k=data->data[i];
Tim Peters84e87f32001-03-17 04:50:51 +00002085
Guido van Rossum053b8df1998-11-25 16:18:00 +00002086 if (PyString_Check(k)) {
2087 rsize += PyString_GET_SIZE(k);
2088 }
2089
2090 else if (PyInt_Check(k)) { /* put */
2091 ik=PyInt_AS_LONG((PyIntObject*)k);
2092 if (ik >= lm || ik==0) {
2093 PyErr_SetString(PicklingError,
2094 "Invalid get data");
2095 return NULL;
Tim Peters84e87f32001-03-17 04:50:51 +00002096 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002097 if (have_get[ik]) { /* with matching get */
2098 if (ik < 256) rsize += 2;
2099 else rsize+=5;
2100 }
2101 }
2102
2103 else if (! (PyTuple_Check(k) &&
2104 PyTuple_GET_SIZE(k) == 2 &&
2105 PyInt_Check((k=PyTuple_GET_ITEM(k,0))))
2106 ) {
2107 PyErr_SetString(PicklingError,
2108 "Unexpected data in internal list");
2109 return NULL;
2110 }
2111
2112 else { /* put */
2113 ik=PyInt_AS_LONG((PyIntObject*)k);
2114 if (ik >= lm || ik==0) {
2115 PyErr_SetString(PicklingError,
2116 "Invalid get data");
2117 return NULL;
Tim Peters84e87f32001-03-17 04:50:51 +00002118 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002119 have_get[ik]=1;
2120 if (ik < 256) rsize += 2;
2121 else rsize+=5;
2122 }
2123
2124 }
2125
2126 /* Now generate the result */
2127 UNLESS (r=PyString_FromStringAndSize(NULL,rsize)) goto err;
2128 s=PyString_AS_STRING((PyStringObject*)r);
2129
2130 for (i=0; i<l; i++) {
2131 k=data->data[i];
2132
2133 if (PyString_Check(k)) {
2134 ssize=PyString_GET_SIZE(k);
2135 if (ssize) {
2136 p=PyString_AS_STRING((PyStringObject*)k);
2137 while (--ssize >= 0) *s++=*p++;
2138 }
2139 }
2140
2141 else if (PyTuple_Check(k)) { /* get */
2142 ik=PyInt_AS_LONG((PyIntObject*)PyTuple_GET_ITEM(k,0));
2143 if (ik < 256) {
2144 *s++ = BINGET;
2145 *s++ = (int)(ik & 0xff);
2146 }
2147 else {
2148 *s++ = LONG_BINGET;
2149 *s++ = (int)(ik & 0xff);
2150 *s++ = (int)((ik >> 8) & 0xff);
2151 *s++ = (int)((ik >> 16) & 0xff);
2152 *s++ = (int)((ik >> 24) & 0xff);
2153 }
2154 }
2155
2156 else { /* put */
2157 ik=PyInt_AS_LONG((PyIntObject*)k);
2158
2159 if (have_get[ik]) { /* with matching get */
2160 if (ik < 256) {
2161 *s++ = BINPUT;
2162 *s++ = (int)(ik & 0xff);
2163 }
2164 else {
2165 *s++ = LONG_BINPUT;
2166 *s++ = (int)(ik & 0xff);
2167 *s++ = (int)((ik >> 8) & 0xff);
2168 *s++ = (int)((ik >> 16) & 0xff);
2169 *s++ = (int)((ik >> 24) & 0xff);
2170 }
2171 }
2172 }
2173
2174 }
2175
2176 if (clear) {
2177 PyDict_Clear(self->memo);
2178 Pdata_clear(data,0);
2179 }
Tim Peters84e87f32001-03-17 04:50:51 +00002180
Guido van Rossum053b8df1998-11-25 16:18:00 +00002181 free(have_get);
2182 return r;
2183err:
2184 free(have_get);
2185 return NULL;
2186}
2187
2188static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002189Pickler_dump(Picklerobject *self, PyObject *args) {
2190 PyObject *ob;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002191 int get=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002192
Guido van Rossum43713e52000-02-29 13:59:29 +00002193 UNLESS (PyArg_ParseTuple(args, "O|i:dump", &ob, &get))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002194 return NULL;
2195
2196 if (dump(self, ob) < 0)
2197 return NULL;
2198
Guido van Rossum053b8df1998-11-25 16:18:00 +00002199 if (get) return Pickle_getvalue(self, NULL);
2200
2201 Py_INCREF(self);
2202 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002203}
2204
2205
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002206static struct PyMethodDef Pickler_methods[] = {
Guido van Rossum142eeb81997-08-13 03:14:41 +00002207 {"dump", (PyCFunction)Pickler_dump, 1,
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002208 "dump(object) --"
2209 "Write an object in pickle format to the object's pickle stream\n"
2210 },
Guido van Rossum142eeb81997-08-13 03:14:41 +00002211 {"clear_memo", (PyCFunction)Pickle_clear_memo, 1,
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002212 "clear_memo() -- Clear the picklers memo"},
Guido van Rossum053b8df1998-11-25 16:18:00 +00002213 {"getvalue", (PyCFunction)Pickle_getvalue, 1,
2214 "getvalue() -- Finish picking a list-based pickle"},
Guido van Rossum60456fd1997-04-09 17:36:32 +00002215 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002216};
2217
2218
2219static Picklerobject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002220newPicklerobject(PyObject *file, int bin) {
2221 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002222
Guido van Rossumb18618d2000-05-03 23:44:39 +00002223 UNLESS (self = PyObject_New(Picklerobject, &Picklertype))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002224 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002225
2226 self->fp = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002227 self->write = NULL;
2228 self->memo = NULL;
2229 self->arg = NULL;
2230 self->pers_func = NULL;
2231 self->inst_pers_func = NULL;
2232 self->write_buf = NULL;
2233 self->bin = bin;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002234 self->fast = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002235 self->buf_size = 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002236 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002237
Guido van Rossum053b8df1998-11-25 16:18:00 +00002238 if (file)
2239 Py_INCREF(file);
2240 else
Guido van Rossum50f385c1998-12-04 18:48:44 +00002241 file=Pdata_New();
Tim Peters84e87f32001-03-17 04:50:51 +00002242
2243 UNLESS (self->file = file)
Guido van Rossum83addc72000-04-21 20:49:36 +00002244 goto err;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002245
Tim Peters84e87f32001-03-17 04:50:51 +00002246 UNLESS (self->memo = PyDict_New())
Guido van Rossum83addc72000-04-21 20:49:36 +00002247 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002248
Guido van Rossum60456fd1997-04-09 17:36:32 +00002249 if (PyFile_Check(file)) {
2250 self->fp = PyFile_AsFile(file);
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002251 if (self->fp == NULL) {
Guido van Rossum83addc72000-04-21 20:49:36 +00002252 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
2253 goto err;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002254 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002255 self->write_func = write_file;
2256 }
2257 else if (PycStringIO_OutputCheck(file)) {
2258 self->write_func = write_cStringIO;
2259 }
Guido van Rossum142eeb81997-08-13 03:14:41 +00002260 else if (file == Py_None) {
2261 self->write_func = write_none;
2262 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002263 else {
2264 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002265
Guido van Rossum053b8df1998-11-25 16:18:00 +00002266 if (! Pdata_Check(file)) {
2267 UNLESS (self->write = PyObject_GetAttr(file, write_str)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002268 PyErr_Clear();
2269 PyErr_SetString(PyExc_TypeError, "argument must have 'write' "
Guido van Rossum053b8df1998-11-25 16:18:00 +00002270 "attribute");
2271 goto err;
2272 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002273 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002274
Tim Peters84e87f32001-03-17 04:50:51 +00002275 UNLESS (self->write_buf =
2276 (char *)malloc(WRITE_BUF_SIZE * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002277 PyErr_NoMemory();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002278 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002279 }
2280 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002281
Guido van Rossum053b8df1998-11-25 16:18:00 +00002282 if (PyEval_GetRestricted()) {
2283 /* Restricted execution, get private tables */
2284 PyObject *m;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002285
Guido van Rossum053b8df1998-11-25 16:18:00 +00002286 UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
2287 self->dispatch_table=PyObject_GetAttr(m, dispatch_table_str);
2288 Py_DECREF(m);
2289 UNLESS (self->dispatch_table) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002290 }
2291 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002292 self->dispatch_table=dispatch_table;
2293 Py_INCREF(dispatch_table);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002294 }
2295
Guido van Rossum60456fd1997-04-09 17:36:32 +00002296 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002297
2298err:
2299 Py_DECREF((PyObject *)self);
2300 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002301}
2302
2303
2304static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002305get_Pickler(PyObject *self, PyObject *args) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002306 PyObject *file=NULL;
2307 int bin;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002308
Guido van Rossum053b8df1998-11-25 16:18:00 +00002309 bin=1;
Guido van Rossum43713e52000-02-29 13:59:29 +00002310 if (! PyArg_ParseTuple(args, "|i:Pickler", &bin)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002311 PyErr_Clear();
2312 bin=0;
Guido van Rossum43713e52000-02-29 13:59:29 +00002313 if (! PyArg_ParseTuple(args, "O|i:Pickler", &file, &bin))
Guido van Rossum053b8df1998-11-25 16:18:00 +00002314 return NULL;
2315 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002316 return (PyObject *)newPicklerobject(file, bin);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002317}
2318
2319
2320static void
Guido van Rossum60456fd1997-04-09 17:36:32 +00002321Pickler_dealloc(Picklerobject *self) {
2322 Py_XDECREF(self->write);
2323 Py_XDECREF(self->memo);
2324 Py_XDECREF(self->arg);
2325 Py_XDECREF(self->file);
2326 Py_XDECREF(self->pers_func);
2327 Py_XDECREF(self->inst_pers_func);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002328 Py_XDECREF(self->dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002329
Tim Peters84e87f32001-03-17 04:50:51 +00002330 if (self->write_buf) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002331 free(self->write_buf);
2332 }
2333
Guido van Rossumb18618d2000-05-03 23:44:39 +00002334 PyObject_Del(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002335}
2336
2337
2338static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002339Pickler_getattr(Picklerobject *self, char *name) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002340
2341 switch (*name) {
2342 case 'p':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002343 if (strcmp(name, "persistent_id") == 0) {
2344 if (!self->pers_func) {
2345 PyErr_SetString(PyExc_AttributeError, name);
2346 return NULL;
2347 }
2348
2349 Py_INCREF(self->pers_func);
2350 return self->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002351 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002352 break;
2353 case 'm':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002354 if (strcmp(name, "memo") == 0) {
2355 if (!self->memo) {
2356 PyErr_SetString(PyExc_AttributeError, name);
2357 return NULL;
2358 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002359
Guido van Rossum60456fd1997-04-09 17:36:32 +00002360 Py_INCREF(self->memo);
2361 return self->memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002362 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002363 break;
2364 case 'P':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002365 if (strcmp(name, "PicklingError") == 0) {
2366 Py_INCREF(PicklingError);
2367 return PicklingError;
2368 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002369 break;
2370 case 'b':
2371 if (strcmp(name, "binary")==0)
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002372 return PyInt_FromLong(self->bin);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002373 break;
2374 case 'f':
2375 if (strcmp(name, "fast")==0)
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002376 return PyInt_FromLong(self->fast);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002377 break;
2378 case 'g':
2379 if (strcmp(name, "getvalue")==0 && ! Pdata_Check(self->file)) {
2380 PyErr_SetString(PyExc_AttributeError, name);
2381 return NULL;
2382 }
2383 break;
2384 }
2385 return Py_FindMethod(Pickler_methods, (PyObject *)self, name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002386}
2387
2388
Tim Peters84e87f32001-03-17 04:50:51 +00002389int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002390Pickler_setattr(Picklerobject *self, char *name, PyObject *value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002391
Guido van Rossum053b8df1998-11-25 16:18:00 +00002392 if (! value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002393 PyErr_SetString(PyExc_TypeError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00002394 "attribute deletion is not supported");
2395 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002396 }
Tim Peters84e87f32001-03-17 04:50:51 +00002397
Guido van Rossum60456fd1997-04-09 17:36:32 +00002398 if (strcmp(name, "persistent_id") == 0) {
2399 Py_XDECREF(self->pers_func);
2400 self->pers_func = value;
2401 Py_INCREF(value);
2402 return 0;
2403 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002404
Guido van Rossum60456fd1997-04-09 17:36:32 +00002405 if (strcmp(name, "inst_persistent_id") == 0) {
2406 Py_XDECREF(self->inst_pers_func);
2407 self->inst_pers_func = value;
2408 Py_INCREF(value);
2409 return 0;
2410 }
2411
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002412 if (strcmp(name, "memo") == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002413 if (! PyDict_Check(value)) {
2414 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
2415 return -1;
2416 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002417 Py_XDECREF(self->memo);
2418 self->memo = value;
2419 Py_INCREF(value);
2420 return 0;
2421 }
2422
Guido van Rossum053b8df1998-11-25 16:18:00 +00002423 if (strcmp(name, "binary")==0) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002424 self->bin=PyObject_IsTrue(value);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002425 return 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002426 }
2427
Guido van Rossum053b8df1998-11-25 16:18:00 +00002428 if (strcmp(name, "fast")==0) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002429 self->fast=PyObject_IsTrue(value);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002430 return 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002431 }
2432
Guido van Rossum60456fd1997-04-09 17:36:32 +00002433 PyErr_SetString(PyExc_AttributeError, name);
2434 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002435}
2436
2437
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002438static char Picklertype__doc__[] =
2439"Objects that know how to pickle objects\n"
2440;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002441
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002442static PyTypeObject Picklertype = {
2443 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00002444 0, /*ob_size*/
2445 "Pickler", /*tp_name*/
2446 sizeof(Picklerobject), /*tp_basicsize*/
2447 0, /*tp_itemsize*/
2448 /* methods */
2449 (destructor)Pickler_dealloc, /*tp_dealloc*/
2450 (printfunc)0, /*tp_print*/
2451 (getattrfunc)Pickler_getattr, /*tp_getattr*/
2452 (setattrfunc)Pickler_setattr, /*tp_setattr*/
2453 (cmpfunc)0, /*tp_compare*/
2454 (reprfunc)0, /*tp_repr*/
2455 0, /*tp_as_number*/
2456 0, /*tp_as_sequence*/
2457 0, /*tp_as_mapping*/
2458 (hashfunc)0, /*tp_hash*/
2459 (ternaryfunc)0, /*tp_call*/
2460 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002461
Guido van Rossum60456fd1997-04-09 17:36:32 +00002462 /* Space for future expansion */
2463 0L,0L,0L,0L,
2464 Picklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002465};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002466
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002467static PyObject *
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002468find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc) {
Guido van Rossume2d81cd1998-08-08 19:40:10 +00002469 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002470
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002471 if (fc) {
2472 if (fc==Py_None) {
Tim Peters84e87f32001-03-17 04:50:51 +00002473 PyErr_SetString(UnpicklingError,
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002474 "Global and instance pickles are not supported.");
2475 return NULL;
2476 }
2477 return PyObject_CallFunction(fc, "OO", py_module_name, py_global_name);
2478 }
2479
Jeremy Hyltond1055231998-08-11 19:52:51 +00002480 module = PySys_GetObject("modules");
2481 if (module == NULL)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002482 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00002483
2484 module = PyDict_GetItem(module, py_module_name);
2485 if (module == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002486 module = PyImport_Import(py_module_name);
2487 if (!module)
2488 return NULL;
2489 global = PyObject_GetAttr(module, py_global_name);
2490 Py_DECREF(module);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002491 }
Jeremy Hyltond1055231998-08-11 19:52:51 +00002492 else
Guido van Rossum053b8df1998-11-25 16:18:00 +00002493 global = PyObject_GetAttr(module, py_global_name);
Jeremy Hyltond1055231998-08-11 19:52:51 +00002494 if (global == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002495 char buf[256 + 37];
2496 sprintf(buf, "Failed to import class %.128s from module %.128s",
2497 PyString_AS_STRING((PyStringObject*)py_global_name),
Tim Peters84e87f32001-03-17 04:50:51 +00002498 PyString_AS_STRING((PyStringObject*)py_module_name));
Guido van Rossum053b8df1998-11-25 16:18:00 +00002499 PyErr_SetString(PyExc_SystemError, buf);
2500 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00002501 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002502 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002503}
2504
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002505static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002506marker(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002507 if (self->num_marks < 1) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002508 PyErr_SetString(UnpicklingError, "could not find MARK");
2509 return -1;
2510 }
2511
2512 return self->marks[--self->num_marks];
2513}
2514
Tim Peters84e87f32001-03-17 04:50:51 +00002515
Guido van Rossum60456fd1997-04-09 17:36:32 +00002516static int
2517load_none(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002518 PDATA_APPEND(self->stack, Py_None, -1);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002519 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002520}
2521
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002522static int
Thomas Wouters58d05102000-07-24 14:43:35 +00002523bad_readline(void) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002524 PyErr_SetString(UnpicklingError, "pickle data was truncated");
2525 return -1;
2526}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002527
2528static int
2529load_int(Unpicklerobject *self) {
2530 PyObject *py_int = 0;
2531 char *endptr, *s;
2532 int len, res = -1;
2533 long l;
2534
2535 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002536 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002537 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002538
2539 errno = 0;
2540 l = strtol(s, &endptr, 0);
2541
2542 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
2543 /* Hm, maybe we've got something long. Let's try reading
Guido van Rossum053b8df1998-11-25 16:18:00 +00002544 it as a Python long object. */
Guido van Rossum60456fd1997-04-09 17:36:32 +00002545 errno=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002546 UNLESS (py_int=PyLong_FromString(s,&endptr,0)) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002547
Guido van Rossum053b8df1998-11-25 16:18:00 +00002548 if ((*endptr != '\n') || (endptr[1] != '\0')) {
2549 PyErr_SetString(PyExc_ValueError,
2550 "could not convert string to int");
2551 goto finally;
2552 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002553 }
2554 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002555 UNLESS (py_int = PyInt_FromLong(l)) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002556 }
2557
Guido van Rossum053b8df1998-11-25 16:18:00 +00002558 free(s);
2559 PDATA_PUSH(self->stack, py_int, -1);
2560 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002561
2562finally:
2563 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002564
2565 return res;
2566}
2567
2568
Tim Peters84e87f32001-03-17 04:50:51 +00002569static long
Guido van Rossum60456fd1997-04-09 17:36:32 +00002570calc_binint(char *s, int x) {
2571 unsigned char c;
2572 int i;
2573 long l;
2574
2575 for (i = 0, l = 0L; i < x; i++) {
2576 c = (unsigned char)s[i];
2577 l |= (long)c << (i * 8);
2578 }
Tim Petersbfa18f72001-04-10 01:54:42 +00002579#if SIZEOF_LONG > 4
2580 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
2581 * is signed, so on a box with longs bigger than 4 bytes we need
2582 * to extend a BININT's sign bit to the full width.
2583 */
2584 if (x == 4 && l & (1L << 31))
2585 l |= (~0L) << 32;
2586#endif
Guido van Rossum60456fd1997-04-09 17:36:32 +00002587 return l;
2588}
2589
2590
2591static int
2592load_binintx(Unpicklerobject *self, char *s, int x) {
2593 PyObject *py_int = 0;
2594 long l;
2595
2596 l = calc_binint(s, x);
2597
Guido van Rossum053b8df1998-11-25 16:18:00 +00002598 UNLESS (py_int = PyInt_FromLong(l))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002599 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002600
Guido van Rossum053b8df1998-11-25 16:18:00 +00002601 PDATA_PUSH(self->stack, py_int, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002602 return 0;
2603}
2604
2605
2606static int
2607load_binint(Unpicklerobject *self) {
2608 char *s;
2609
2610 if ((*self->read_func)(self, &s, 4) < 0)
2611 return -1;
2612
2613 return load_binintx(self, s, 4);
2614}
2615
2616
2617static int
2618load_binint1(Unpicklerobject *self) {
2619 char *s;
2620
2621 if ((*self->read_func)(self, &s, 1) < 0)
2622 return -1;
2623
2624 return load_binintx(self, s, 1);
2625}
2626
2627
2628static int
2629load_binint2(Unpicklerobject *self) {
2630 char *s;
2631
2632 if ((*self->read_func)(self, &s, 2) < 0)
2633 return -1;
2634
2635 return load_binintx(self, s, 2);
2636}
Tim Peters84e87f32001-03-17 04:50:51 +00002637
Guido van Rossum60456fd1997-04-09 17:36:32 +00002638static int
2639load_long(Unpicklerobject *self) {
2640 PyObject *l = 0;
2641 char *end, *s;
2642 int len, res = -1;
2643
Guido van Rossum60456fd1997-04-09 17:36:32 +00002644 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002645 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002646 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002647
Guido van Rossum053b8df1998-11-25 16:18:00 +00002648 UNLESS (l = PyLong_FromString(s, &end, 0))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002649 goto finally;
2650
Guido van Rossum053b8df1998-11-25 16:18:00 +00002651 free(s);
2652 PDATA_PUSH(self->stack, l, -1);
2653 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002654
2655finally:
2656 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002657
2658 return res;
2659}
2660
Tim Peters84e87f32001-03-17 04:50:51 +00002661
Guido van Rossum60456fd1997-04-09 17:36:32 +00002662static int
2663load_float(Unpicklerobject *self) {
2664 PyObject *py_float = 0;
2665 char *endptr, *s;
2666 int len, res = -1;
2667 double d;
2668
2669 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002670 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002671 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002672
2673 errno = 0;
2674 d = strtod(s, &endptr);
2675
2676 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
Tim Peters84e87f32001-03-17 04:50:51 +00002677 PyErr_SetString(PyExc_ValueError,
Guido van Rossum60456fd1997-04-09 17:36:32 +00002678 "could not convert string to float");
2679 goto finally;
2680 }
2681
Guido van Rossum053b8df1998-11-25 16:18:00 +00002682 UNLESS (py_float = PyFloat_FromDouble(d))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002683 goto finally;
2684
Guido van Rossum053b8df1998-11-25 16:18:00 +00002685 free(s);
2686 PDATA_PUSH(self->stack, py_float, -1);
2687 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002688
2689finally:
2690 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002691
2692 return res;
2693}
2694
Guido van Rossum60456fd1997-04-09 17:36:32 +00002695static int
2696load_binfloat(Unpicklerobject *self) {
2697 PyObject *py_float = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002698 int s, e;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002699 long fhi, flo;
2700 double x;
2701 char *p;
2702
2703 if ((*self->read_func)(self, &p, 8) < 0)
2704 return -1;
2705
2706 /* First byte */
2707 s = (*p>>7) & 1;
2708 e = (*p & 0x7F) << 4;
2709 p++;
2710
2711 /* Second byte */
2712 e |= (*p>>4) & 0xF;
2713 fhi = (*p & 0xF) << 24;
2714 p++;
2715
2716 /* Third byte */
2717 fhi |= (*p & 0xFF) << 16;
2718 p++;
2719
2720 /* Fourth byte */
2721 fhi |= (*p & 0xFF) << 8;
2722 p++;
2723
2724 /* Fifth byte */
2725 fhi |= *p & 0xFF;
2726 p++;
2727
2728 /* Sixth byte */
2729 flo = (*p & 0xFF) << 16;
2730 p++;
2731
2732 /* Seventh byte */
2733 flo |= (*p & 0xFF) << 8;
2734 p++;
2735
2736 /* Eighth byte */
2737 flo |= *p & 0xFF;
2738
2739 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
2740 x /= 268435456.0; /* 2**28 */
2741
2742 /* XXX This sadly ignores Inf/NaN */
2743 if (e == 0)
2744 e = -1022;
2745 else {
2746 x += 1.0;
2747 e -= 1023;
2748 }
2749 x = ldexp(x, e);
2750
2751 if (s)
2752 x = -x;
2753
Guido van Rossum053b8df1998-11-25 16:18:00 +00002754 UNLESS (py_float = PyFloat_FromDouble(x)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002755
Guido van Rossum053b8df1998-11-25 16:18:00 +00002756 PDATA_PUSH(self->stack, py_float, -1);
2757 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002758}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002759
2760static int
2761load_string(Unpicklerobject *self) {
2762 PyObject *str = 0;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002763 int len, res = -1, nslash;
2764 char *s, q, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002765
2766 static PyObject *eval_dict = 0;
2767
2768 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002769 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002770 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002771
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002772 /* Check for unquoted quotes (evil strings) */
2773 q=*s;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002774 if (q != '"' && q != '\'') goto insecure;
2775 for (p=s+1, nslash=0; *p; p++) {
2776 if (*p==q && nslash%2==0) break;
2777 if (*p=='\\') nslash++;
2778 else nslash=0;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002779 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002780 if (*p==q)
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002781 {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002782 for (p++; *p; p++) if (*p > ' ') goto insecure;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002783 }
2784 else goto insecure;
2785 /********************************************/
2786
Guido van Rossum053b8df1998-11-25 16:18:00 +00002787 UNLESS (eval_dict)
2788 UNLESS (eval_dict = Py_BuildValue("{s{}}", "__builtins__"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002789 goto finally;
2790
Guido van Rossum053b8df1998-11-25 16:18:00 +00002791 UNLESS (str = PyRun_String(s, Py_eval_input, eval_dict, eval_dict))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002792 goto finally;
2793
Guido van Rossum053b8df1998-11-25 16:18:00 +00002794 free(s);
2795 PDATA_PUSH(self->stack, str, -1);
2796 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002797
2798finally:
2799 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002800
2801 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00002802
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002803insecure:
2804 free(s);
2805 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
2806 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00002807}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002808
2809
2810static int
2811load_binstring(Unpicklerobject *self) {
2812 PyObject *py_string = 0;
2813 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002814 char *s;
2815
Guido van Rossum053b8df1998-11-25 16:18:00 +00002816 if ((*self->read_func)(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002817
2818 l = calc_binint(s, 4);
2819
2820 if ((*self->read_func)(self, &s, l) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002821 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002822
Guido van Rossum053b8df1998-11-25 16:18:00 +00002823 UNLESS (py_string = PyString_FromStringAndSize(s, l))
2824 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002825
Guido van Rossum053b8df1998-11-25 16:18:00 +00002826 PDATA_PUSH(self->stack, py_string, -1);
2827 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002828}
2829
2830
2831static int
2832load_short_binstring(Unpicklerobject *self) {
2833 PyObject *py_string = 0;
Tim Peters84e87f32001-03-17 04:50:51 +00002834 unsigned char l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002835 char *s;
2836
2837 if ((*self->read_func)(self, &s, 1) < 0)
2838 return -1;
2839
2840 l = (unsigned char)s[0];
2841
Guido van Rossum053b8df1998-11-25 16:18:00 +00002842 if ((*self->read_func)(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002843
Guido van Rossum053b8df1998-11-25 16:18:00 +00002844 UNLESS (py_string = PyString_FromStringAndSize(s, l)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002845
Guido van Rossum053b8df1998-11-25 16:18:00 +00002846 PDATA_PUSH(self->stack, py_string, -1);
2847 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00002848}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002849
2850
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002851#ifdef Py_USING_UNICODE
Guido van Rossum60456fd1997-04-09 17:36:32 +00002852static int
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002853load_unicode(Unpicklerobject *self) {
2854 PyObject *str = 0;
2855 int len, res = -1;
2856 char *s;
2857
2858 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00002859 if (len < 1) return bad_readline();
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002860
2861 UNLESS (str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL))
2862 goto finally;
2863
2864 PDATA_PUSH(self->stack, str, -1);
2865 return 0;
2866
2867finally:
2868 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00002869}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002870#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002871
2872
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002873#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002874static int
2875load_binunicode(Unpicklerobject *self) {
2876 PyObject *unicode;
2877 long l;
2878 char *s;
2879
2880 if ((*self->read_func)(self, &s, 4) < 0) return -1;
2881
2882 l = calc_binint(s, 4);
2883
2884 if ((*self->read_func)(self, &s, l) < 0)
2885 return -1;
2886
2887 UNLESS (unicode = PyUnicode_DecodeUTF8(s, l, NULL))
2888 return -1;
2889
2890 PDATA_PUSH(self->stack, unicode, -1);
2891 return 0;
2892}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002893#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002894
2895
2896static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002897load_tuple(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002898 PyObject *tup;
2899 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002900
Guido van Rossum053b8df1998-11-25 16:18:00 +00002901 if ((i = marker(self)) < 0) return -1;
2902 UNLESS (tup=Pdata_popTuple(self->stack, i)) return -1;
2903 PDATA_PUSH(self->stack, tup, -1);
2904 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002905}
2906
2907static int
2908load_empty_tuple(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002909 PyObject *tup;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002910
Guido van Rossum053b8df1998-11-25 16:18:00 +00002911 UNLESS (tup=PyTuple_New(0)) return -1;
2912 PDATA_PUSH(self->stack, tup, -1);
2913 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002914}
2915
2916static int
2917load_empty_list(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002918 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002919
Guido van Rossum053b8df1998-11-25 16:18:00 +00002920 UNLESS (list=PyList_New(0)) return -1;
2921 PDATA_PUSH(self->stack, list, -1);
2922 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002923}
2924
2925static int
2926load_empty_dict(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002927 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002928
Guido van Rossum053b8df1998-11-25 16:18:00 +00002929 UNLESS (dict=PyDict_New()) return -1;
2930 PDATA_PUSH(self->stack, dict, -1);
2931 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002932}
2933
2934
2935static int
2936load_list(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002937 PyObject *list = 0;
2938 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002939
Guido van Rossum053b8df1998-11-25 16:18:00 +00002940 if ((i = marker(self)) < 0) return -1;
2941 UNLESS (list=Pdata_popList(self->stack, i)) return -1;
2942 PDATA_PUSH(self->stack, list, -1);
2943 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002944}
2945
2946static int
2947load_dict(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002948 PyObject *dict, *key, *value;
2949 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002950
Guido van Rossum053b8df1998-11-25 16:18:00 +00002951 if ((i = marker(self)) < 0) return -1;
2952 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002953
Guido van Rossum053b8df1998-11-25 16:18:00 +00002954 UNLESS (dict = PyDict_New()) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002955
Guido van Rossum053b8df1998-11-25 16:18:00 +00002956 for (k = i+1; k < j; k += 2) {
2957 key =self->stack->data[k-1];
2958 value=self->stack->data[k ];
2959 if (PyDict_SetItem(dict, key, value) < 0) {
2960 Py_DECREF(dict);
2961 return -1;
2962 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002963 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002964 Pdata_clear(self->stack, i);
2965 PDATA_PUSH(self->stack, dict, -1);
2966 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002967}
2968
2969static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002970Instance_New(PyObject *cls, PyObject *args) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002971 int has_key;
2972 PyObject *safe=0, *r=0;
2973
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002974 if (PyClass_Check(cls)) {
2975 int l;
Tim Peters84e87f32001-03-17 04:50:51 +00002976
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002977 if ((l=PyObject_Size(args)) < 0) goto err;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002978 UNLESS (l) {
2979 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002980
Guido van Rossum053b8df1998-11-25 16:18:00 +00002981 UNLESS (__getinitargs__=PyObject_GetAttr(cls, __getinitargs___str)) {
2982 /* We have a class with no __getinitargs__, so bypass usual
2983 construction */
Fred Drake2c773552001-03-22 17:52:17 +00002984 PyObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002985
Guido van Rossum053b8df1998-11-25 16:18:00 +00002986 PyErr_Clear();
Fred Drake2c773552001-03-22 17:52:17 +00002987 UNLESS (inst=PyInstance_NewRaw(cls, NULL))
Guido van Rossum053b8df1998-11-25 16:18:00 +00002988 goto err;
Fred Drake2c773552001-03-22 17:52:17 +00002989 return inst;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002990 }
2991 Py_DECREF(__getinitargs__);
2992 }
Tim Peters84e87f32001-03-17 04:50:51 +00002993
Guido van Rossum053b8df1998-11-25 16:18:00 +00002994 if ((r=PyInstance_New(cls, args, NULL))) return r;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002995 else goto err;
2996 }
Tim Peters84e87f32001-03-17 04:50:51 +00002997
2998
Guido van Rossum60456fd1997-04-09 17:36:32 +00002999 if ((has_key = cPickle_PyMapping_HasKey(safe_constructors, cls)) < 0)
3000 goto err;
Tim Peters84e87f32001-03-17 04:50:51 +00003001
Guido van Rossum60456fd1997-04-09 17:36:32 +00003002 if (!has_key)
Guido van Rossum053b8df1998-11-25 16:18:00 +00003003 if (!(safe = PyObject_GetAttr(cls, __safe_for_unpickling___str)) ||
Guido van Rossum60456fd1997-04-09 17:36:32 +00003004 !PyObject_IsTrue(safe)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003005 cPickle_ErrFormat(UnpicklingError,
3006 "%s is not safe for unpickling", "O", cls);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003007 Py_XDECREF(safe);
3008 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003009 }
3010
Guido van Rossum053b8df1998-11-25 16:18:00 +00003011 if (args==Py_None) {
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003012 /* Special case, call cls.__basicnew__() */
3013 PyObject *basicnew;
Tim Peters84e87f32001-03-17 04:50:51 +00003014
Guido van Rossum053b8df1998-11-25 16:18:00 +00003015 UNLESS (basicnew=PyObject_GetAttr(cls, __basicnew___str)) return NULL;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003016 r=PyObject_CallObject(basicnew, NULL);
3017 Py_DECREF(basicnew);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003018 if (r) return r;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003019 }
3020
Guido van Rossum053b8df1998-11-25 16:18:00 +00003021 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00003022
Guido van Rossum60456fd1997-04-09 17:36:32 +00003023err:
3024 {
3025 PyObject *tp, *v, *tb;
3026
3027 PyErr_Fetch(&tp, &v, &tb);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003028 if ((r=Py_BuildValue("OOO",v,cls,args))) {
3029 Py_XDECREF(v);
3030 v=r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003031 }
3032 PyErr_Restore(tp,v,tb);
3033 }
3034 return NULL;
3035}
Tim Peters84e87f32001-03-17 04:50:51 +00003036
Guido van Rossum60456fd1997-04-09 17:36:32 +00003037
3038static int
3039load_obj(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003040 PyObject *class, *tup, *obj=0;
3041 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003042
Guido van Rossum053b8df1998-11-25 16:18:00 +00003043 if ((i = marker(self)) < 0) return -1;
3044 UNLESS (tup=Pdata_popTuple(self->stack, i+1)) return -1;
3045 PDATA_POP(self->stack, class);
3046 if (class) {
3047 obj = Instance_New(class, tup);
3048 Py_DECREF(class);
3049 }
3050 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003051
Guido van Rossum053b8df1998-11-25 16:18:00 +00003052 if (! obj) return -1;
3053 PDATA_PUSH(self->stack, obj, -1);
3054 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003055}
3056
3057
3058static int
3059load_inst(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003060 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003061 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003062 char *s;
3063
Guido van Rossum053b8df1998-11-25 16:18:00 +00003064 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003065
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 Rossumfdde96c1997-12-04 01:13:01 +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 Rossumfdde96c1997-12-04 01:13:01 +00003078
Guido van Rossum053b8df1998-11-25 16:18:00 +00003079 if (! class) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003080
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003081 if ((tup=Pdata_popTuple(self->stack, i))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003082 obj = Instance_New(class, tup);
3083 Py_DECREF(tup);
3084 }
3085 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003086
Guido van Rossum053b8df1998-11-25 16:18:00 +00003087 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003088
Guido van Rossum053b8df1998-11-25 16:18:00 +00003089 PDATA_PUSH(self->stack, obj, -1);
3090 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003091}
3092
3093
3094static int
3095load_global(Unpicklerobject *self) {
3096 PyObject *class = 0, *module_name = 0, *class_name = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003097 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003098 char *s;
3099
Guido van Rossum053b8df1998-11-25 16:18:00 +00003100 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003101 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00003102 UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003103
Guido van Rossum053b8df1998-11-25 16:18:00 +00003104 if ((len = (*self->readline_func)(self, &s)) >= 0) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003105 if (len < 2) return bad_readline();
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003106 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003107 class = find_class(module_name, class_name, self->find_class);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003108 Py_DECREF(class_name);
3109 }
3110 }
3111 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003112
Guido van Rossum053b8df1998-11-25 16:18:00 +00003113 if (! class) return -1;
3114 PDATA_PUSH(self->stack, class, -1);
3115 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003116}
3117
3118
3119static int
3120load_persid(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003121 PyObject *pid = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003122 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003123 char *s;
3124
3125 if (self->pers_func) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003126 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003127 if (len < 2) return bad_readline();
Tim Peters84e87f32001-03-17 04:50:51 +00003128
Guido van Rossum053b8df1998-11-25 16:18:00 +00003129 UNLESS (pid = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003130
Guido van Rossum053b8df1998-11-25 16:18:00 +00003131 if (PyList_Check(self->pers_func)) {
3132 if (PyList_Append(self->pers_func, pid) < 0) {
3133 Py_DECREF(pid);
3134 return -1;
3135 }
3136 }
3137 else {
3138 ARG_TUP(self, pid);
3139 if (self->arg) {
3140 pid = PyObject_CallObject(self->pers_func, self->arg);
3141 FREE_ARG_TUP(self);
3142 }
3143 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003144
Guido van Rossum053b8df1998-11-25 16:18:00 +00003145 if (! pid) return -1;
3146
3147 PDATA_PUSH(self->stack, pid, -1);
3148 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003149 }
3150 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 Rossum60456fd1997-04-09 17:36:32 +00003155 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003156}
3157
Guido van Rossum60456fd1997-04-09 17:36:32 +00003158static int
3159load_binpersid(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003160 PyObject *pid = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003161
3162 if (self->pers_func) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003163 PDATA_POP(self->stack, pid);
3164 if (! pid) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003165
Guido van Rossum053b8df1998-11-25 16:18:00 +00003166 if (PyList_Check(self->pers_func)) {
3167 if (PyList_Append(self->pers_func, pid) < 0) {
3168 Py_DECREF(pid);
3169 return -1;
3170 }
3171 }
3172 else {
3173 ARG_TUP(self, pid);
3174 if (self->arg) {
3175 pid = PyObject_CallObject(self->pers_func, self->arg);
3176 FREE_ARG_TUP(self);
3177 }
3178 if (! pid) return -1;
3179 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003180
Guido van Rossum053b8df1998-11-25 16:18:00 +00003181 PDATA_PUSH(self->stack, pid, -1);
3182 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003183 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003184 else {
3185 PyErr_SetString(UnpicklingError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00003186 "A load persistent id instruction was encountered,\n"
3187 "but no persistent_load function was specified.");
3188 return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003189 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003190}
3191
3192
3193static int
3194load_pop(Unpicklerobject *self) {
3195 int len;
3196
Guido van Rossum053b8df1998-11-25 16:18:00 +00003197 UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003198
Tim Peters84e87f32001-03-17 04:50:51 +00003199 /* Note that we split the (pickle.py) stack into two stacks,
Guido van Rossumea2b7152000-05-09 18:14:50 +00003200 an object stack and a mark stack. We have to be clever and
3201 pop the right one. We do this by looking at the top of the
3202 mark stack.
3203 */
3204
Tim Peters84e87f32001-03-17 04:50:51 +00003205 if ((self->num_marks > 0) &&
Guido van Rossum60456fd1997-04-09 17:36:32 +00003206 (self->marks[self->num_marks - 1] == len))
3207 self->num_marks--;
Tim Peters84e87f32001-03-17 04:50:51 +00003208 else {
Guido van Rossumea2b7152000-05-09 18:14:50 +00003209 len--;
3210 Py_DECREF(self->stack->data[len]);
3211 self->stack->length=len;
3212 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003213
3214 return 0;
3215}
3216
3217
3218static int
3219load_pop_mark(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003220 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003221
3222 if ((i = marker(self)) < 0)
3223 return -1;
3224
Guido van Rossum053b8df1998-11-25 16:18:00 +00003225 Pdata_clear(self->stack, i);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003226
3227 return 0;
3228}
3229
3230
3231static int
3232load_dup(Unpicklerobject *self) {
3233 PyObject *last;
3234 int len;
3235
Guido van Rossum053b8df1998-11-25 16:18:00 +00003236 if ((len = self->stack->length) <= 0) return stackUnderflow();
3237 last=self->stack->data[len-1];
3238 Py_INCREF(last);
3239 PDATA_PUSH(self->stack, last, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003240 return 0;
3241}
3242
3243
3244static int
3245load_get(Unpicklerobject *self) {
3246 PyObject *py_str = 0, *value = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003247 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003248 char *s;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003249 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003250
Guido van Rossum053b8df1998-11-25 16:18:00 +00003251 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003252 if (len < 2) return bad_readline();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003253
Guido van Rossum053b8df1998-11-25 16:18:00 +00003254 UNLESS (py_str = PyString_FromStringAndSize(s, len - 1)) return -1;
3255
3256 value = PyDict_GetItem(self->memo, py_str);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003257 if (! value) {
3258 PyErr_SetObject(BadPickleGet, py_str);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003259 rc = -1;
3260 } else {
3261 PDATA_APPEND(self->stack, value, -1);
3262 rc = 0;
3263 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003264
Guido van Rossum2f80d961999-07-13 15:18:58 +00003265 Py_DECREF(py_str);
3266 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003267}
3268
3269
3270static int
3271load_binget(Unpicklerobject *self) {
3272 PyObject *py_key = 0, *value = 0;
3273 unsigned char key;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003274 char *s;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003275 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003276
Guido van Rossum053b8df1998-11-25 16:18:00 +00003277 if ((*self->read_func)(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003278
3279 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00003280 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003281
Guido van Rossum053b8df1998-11-25 16:18:00 +00003282 value = PyDict_GetItem(self->memo, py_key);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003283 if (! value) {
3284 PyErr_SetObject(BadPickleGet, py_key);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003285 rc = -1;
3286 } else {
3287 PDATA_APPEND(self->stack, value, -1);
3288 rc = 0;
3289 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003290
Guido van Rossum2f80d961999-07-13 15:18:58 +00003291 Py_DECREF(py_key);
3292 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003293}
3294
3295
3296static int
3297load_long_binget(Unpicklerobject *self) {
3298 PyObject *py_key = 0, *value = 0;
Thomas Wouters3b6448f2000-07-22 23:56:07 +00003299 unsigned char c;
3300 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003301 long key;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003302 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003303
Guido van Rossum053b8df1998-11-25 16:18:00 +00003304 if ((*self->read_func)(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003305
3306 c = (unsigned char)s[0];
3307 key = (long)c;
3308 c = (unsigned char)s[1];
3309 key |= (long)c << 8;
3310 c = (unsigned char)s[2];
3311 key |= (long)c << 16;
3312 c = (unsigned char)s[3];
3313 key |= (long)c << 24;
3314
Guido van Rossum053b8df1998-11-25 16:18:00 +00003315 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003316
Guido van Rossum053b8df1998-11-25 16:18:00 +00003317 value = PyDict_GetItem(self->memo, py_key);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003318 if (! value) {
3319 PyErr_SetObject(BadPickleGet, py_key);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003320 rc = -1;
3321 } else {
3322 PDATA_APPEND(self->stack, value, -1);
3323 rc = 0;
3324 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003325
Guido van Rossum2f80d961999-07-13 15:18:58 +00003326 Py_DECREF(py_key);
3327 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003328}
3329
3330
3331static int
3332load_put(Unpicklerobject *self) {
3333 PyObject *py_str = 0, *value = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003334 int len, l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003335 char *s;
3336
Guido van Rossum053b8df1998-11-25 16:18:00 +00003337 if ((l = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumd1f66dc1999-02-08 22:38:25 +00003338 if (l < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00003339 UNLESS (len=self->stack->length) return stackUnderflow();
3340 UNLESS (py_str = PyString_FromStringAndSize(s, l - 1)) return -1;
3341 value=self->stack->data[len-1];
3342 l=PyDict_SetItem(self->memo, py_str, value);
3343 Py_DECREF(py_str);
3344 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003345}
3346
3347
3348static int
3349load_binput(Unpicklerobject *self) {
3350 PyObject *py_key = 0, *value = 0;
Thomas Wouters3b6448f2000-07-22 23:56:07 +00003351 unsigned char key;
3352 char *s;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003353 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003354
Guido van Rossum053b8df1998-11-25 16:18:00 +00003355 if ((*self->read_func)(self, &s, 1) < 0) return -1;
3356 UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003357
3358 key = (unsigned char)s[0];
3359
Guido van Rossum053b8df1998-11-25 16:18:00 +00003360 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3361 value=self->stack->data[len-1];
3362 len=PyDict_SetItem(self->memo, py_key, value);
3363 Py_DECREF(py_key);
3364 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003365}
3366
3367
3368static int
3369load_long_binput(Unpicklerobject *self) {
3370 PyObject *py_key = 0, *value = 0;
3371 long key;
Thomas Wouters3b6448f2000-07-22 23:56:07 +00003372 unsigned char c;
3373 char *s;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003374 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003375
Guido van Rossum053b8df1998-11-25 16:18:00 +00003376 if ((*self->read_func)(self, &s, 4) < 0) return -1;
3377 UNLESS (len=self->stack->length) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003378
3379 c = (unsigned char)s[0];
3380 key = (long)c;
3381 c = (unsigned char)s[1];
3382 key |= (long)c << 8;
3383 c = (unsigned char)s[2];
3384 key |= (long)c << 16;
3385 c = (unsigned char)s[3];
3386 key |= (long)c << 24;
3387
Guido van Rossum053b8df1998-11-25 16:18:00 +00003388 UNLESS (py_key = PyInt_FromLong(key)) return -1;
3389 value=self->stack->data[len-1];
3390 len=PyDict_SetItem(self->memo, py_key, value);
3391 Py_DECREF(py_key);
3392 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003393}
3394
3395
Tim Peters84e87f32001-03-17 04:50:51 +00003396static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00003397do_append(Unpicklerobject *self, int x) {
3398 PyObject *value = 0, *list = 0, *append_method = 0;
3399 int len, i;
3400
Guido van Rossum053b8df1998-11-25 16:18:00 +00003401 UNLESS ((len=self->stack->length) >= x && x > 0) return stackUnderflow();
3402 if (len==x) return 0; /* nothing to do */
Guido van Rossum60456fd1997-04-09 17:36:32 +00003403
Guido van Rossum053b8df1998-11-25 16:18:00 +00003404 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003405
3406 if (PyList_Check(list)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003407 PyObject *slice;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003408 int list_len;
Tim Peters84e87f32001-03-17 04:50:51 +00003409
Guido van Rossum053b8df1998-11-25 16:18:00 +00003410 slice=Pdata_popList(self->stack, x);
3411 list_len = PyList_GET_SIZE(list);
3412 i=PyList_SetSlice(list, list_len, list_len, slice);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003413 Py_DECREF(slice);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003414 return i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003415 }
3416 else {
3417
Guido van Rossum053b8df1998-11-25 16:18:00 +00003418 UNLESS (append_method = PyObject_GetAttr(list, append_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00003419 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003420
Guido van Rossum60456fd1997-04-09 17:36:32 +00003421 for (i = x; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003422 PyObject *junk;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003423
Guido van Rossum053b8df1998-11-25 16:18:00 +00003424 value=self->stack->data[i];
3425 junk=0;
3426 ARG_TUP(self, value);
3427 if (self->arg) {
3428 junk = PyObject_CallObject(append_method, self->arg);
3429 FREE_ARG_TUP(self);
3430 }
3431 if (! junk) {
3432 Pdata_clear(self->stack, i+1);
3433 self->stack->length=x;
3434 Py_DECREF(append_method);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003435 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003436 }
3437 Py_DECREF(junk);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003438 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003439 self->stack->length=x;
3440 Py_DECREF(append_method);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003441 }
3442
Guido van Rossum60456fd1997-04-09 17:36:32 +00003443 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003444}
3445
Tim Peters84e87f32001-03-17 04:50:51 +00003446
Guido van Rossum60456fd1997-04-09 17:36:32 +00003447static int
3448load_append(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003449 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003450}
3451
3452
3453static int
3454load_appends(Unpicklerobject *self) {
3455 return do_append(self, marker(self));
3456}
3457
3458
3459static int
3460do_setitems(Unpicklerobject *self, int x) {
3461 PyObject *value = 0, *key = 0, *dict = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003462 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003463
Guido van Rossum053b8df1998-11-25 16:18:00 +00003464 UNLESS ((len=self->stack->length) >= x
3465 && x > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003466
Guido van Rossum053b8df1998-11-25 16:18:00 +00003467 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003468
Guido van Rossum053b8df1998-11-25 16:18:00 +00003469 for (i = x+1; i < len; i += 2) {
3470 key =self->stack->data[i-1];
3471 value=self->stack->data[i ];
3472 if (PyObject_SetItem(dict, key, value) < 0) {
3473 r=-1;
3474 break;
3475 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003476 }
3477
Guido van Rossum053b8df1998-11-25 16:18:00 +00003478 Pdata_clear(self->stack, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003479
Guido van Rossum053b8df1998-11-25 16:18:00 +00003480 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003481}
3482
3483
3484static int
3485load_setitem(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003486 return do_setitems(self, self->stack->length - 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003487}
3488
Guido van Rossum60456fd1997-04-09 17:36:32 +00003489static int
3490load_setitems(Unpicklerobject *self) {
3491 return do_setitems(self, marker(self));
3492}
3493
3494
3495static int
3496load_build(Unpicklerobject *self) {
Tim Peters84e87f32001-03-17 04:50:51 +00003497 PyObject *value = 0, *inst = 0, *instdict = 0, *d_key = 0, *d_value = 0,
Guido van Rossum60456fd1997-04-09 17:36:32 +00003498 *junk = 0, *__setstate__ = 0;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003499 int i, r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003500
Guido van Rossum053b8df1998-11-25 16:18:00 +00003501 if (self->stack->length < 2) return stackUnderflow();
3502 PDATA_POP(self->stack, value);
3503 if (! value) return -1;
3504 inst=self->stack->data[self->stack->length-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003505
Guido van Rossum053b8df1998-11-25 16:18:00 +00003506 if ((__setstate__ = PyObject_GetAttr(inst, __setstate___str))) {
3507 ARG_TUP(self, value);
3508 if (self->arg) {
3509 junk = PyObject_CallObject(__setstate__, self->arg);
3510 FREE_ARG_TUP(self);
3511 }
3512 Py_DECREF(__setstate__);
3513 if (! junk) return -1;
3514 Py_DECREF(junk);
3515 return 0;
3516 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003517
Guido van Rossum053b8df1998-11-25 16:18:00 +00003518 PyErr_Clear();
3519 if ((instdict = PyObject_GetAttr(inst, __dict___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003520 i = 0;
3521 while (PyDict_Next(value, &i, &d_key, &d_value)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003522 if (PyObject_SetItem(instdict, d_key, d_value) < 0) {
3523 r=-1;
3524 break;
3525 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003526 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003527 Py_DECREF(instdict);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003528 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003529 else r=-1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003530
Guido van Rossum053b8df1998-11-25 16:18:00 +00003531 Py_XDECREF(value);
Tim Peters84e87f32001-03-17 04:50:51 +00003532
Guido van Rossum053b8df1998-11-25 16:18:00 +00003533 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003534}
3535
3536
3537static int
3538load_mark(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003539 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003540
Guido van Rossumea2b7152000-05-09 18:14:50 +00003541 /* Note that we split the (pickle.py) stack into two stacks, an
3542 object stack and a mark stack. Here we push a mark onto the
Tim Peters84e87f32001-03-17 04:50:51 +00003543 mark stack.
Guido van Rossumea2b7152000-05-09 18:14:50 +00003544 */
3545
Guido van Rossum053b8df1998-11-25 16:18:00 +00003546 if ((self->num_marks + 1) >= self->marks_size) {
3547 s=self->marks_size+20;
3548 if (s <= self->num_marks) s=self->num_marks + 1;
Guido van Rossum761fcd01999-04-12 22:51:20 +00003549 if (self->marks == NULL)
Guido van Rossumaa8d1671999-01-25 21:43:51 +00003550 self->marks=(int *)malloc(s * sizeof(int));
3551 else
3552 self->marks=(int *)realloc(self->marks, s * sizeof(int));
Guido van Rossum053b8df1998-11-25 16:18:00 +00003553 if (! self->marks) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003554 PyErr_NoMemory();
3555 return -1;
3556 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003557 self->marks_size = s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003558 }
3559
Guido van Rossum053b8df1998-11-25 16:18:00 +00003560 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003561
3562 return 0;
3563}
3564
3565static int
3566load_reduce(Unpicklerobject *self) {
3567 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003568
Guido van Rossum053b8df1998-11-25 16:18:00 +00003569 PDATA_POP(self->stack, arg_tup);
3570 if (! arg_tup) return -1;
3571 PDATA_POP(self->stack, callable);
3572 if (callable) {
3573 ob = Instance_New(callable, arg_tup);
3574 Py_DECREF(callable);
3575 }
3576 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003577
Guido van Rossum053b8df1998-11-25 16:18:00 +00003578 if (! ob) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003579
Guido van Rossum053b8df1998-11-25 16:18:00 +00003580 PDATA_PUSH(self->stack, ob, -1);
3581 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003582}
Tim Peters84e87f32001-03-17 04:50:51 +00003583
Guido van Rossum60456fd1997-04-09 17:36:32 +00003584static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003585load(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003586 PyObject *err = 0, *val = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003587 char *s;
3588
Guido van Rossum60456fd1997-04-09 17:36:32 +00003589 self->num_marks = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003590 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003591
3592 while (1) {
3593 if ((*self->read_func)(self, &s, 1) < 0)
3594 break;
3595
3596 switch (s[0]) {
3597 case NONE:
3598 if (load_none(self) < 0)
3599 break;
3600 continue;
3601
3602 case BININT:
3603 if (load_binint(self) < 0)
3604 break;
3605 continue;
3606
3607 case BININT1:
3608 if (load_binint1(self) < 0)
3609 break;
3610 continue;
3611
3612 case BININT2:
3613 if (load_binint2(self) < 0)
3614 break;
3615 continue;
3616
3617 case INT:
3618 if (load_int(self) < 0)
3619 break;
3620 continue;
3621
3622 case LONG:
3623 if (load_long(self) < 0)
3624 break;
3625 continue;
3626
3627 case FLOAT:
3628 if (load_float(self) < 0)
3629 break;
3630 continue;
3631
Guido van Rossum60456fd1997-04-09 17:36:32 +00003632 case BINFLOAT:
3633 if (load_binfloat(self) < 0)
3634 break;
3635 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003636
3637 case BINSTRING:
3638 if (load_binstring(self) < 0)
3639 break;
3640 continue;
3641
3642 case SHORT_BINSTRING:
3643 if (load_short_binstring(self) < 0)
3644 break;
3645 continue;
3646
3647 case STRING:
3648 if (load_string(self) < 0)
3649 break;
3650 continue;
3651
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003652#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003653 case UNICODE:
3654 if (load_unicode(self) < 0)
3655 break;
3656 continue;
3657
3658 case BINUNICODE:
3659 if (load_binunicode(self) < 0)
3660 break;
3661 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003662#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003663
Guido van Rossum60456fd1997-04-09 17:36:32 +00003664 case EMPTY_TUPLE:
3665 if (load_empty_tuple(self) < 0)
3666 break;
3667 continue;
3668
3669 case TUPLE:
3670 if (load_tuple(self) < 0)
3671 break;
3672 continue;
3673
3674 case EMPTY_LIST:
3675 if (load_empty_list(self) < 0)
3676 break;
3677 continue;
3678
3679 case LIST:
3680 if (load_list(self) < 0)
3681 break;
3682 continue;
3683
3684 case EMPTY_DICT:
3685 if (load_empty_dict(self) < 0)
3686 break;
3687 continue;
3688
3689 case DICT:
3690 if (load_dict(self) < 0)
3691 break;
3692 continue;
3693
3694 case OBJ:
3695 if (load_obj(self) < 0)
3696 break;
3697 continue;
3698
3699 case INST:
3700 if (load_inst(self) < 0)
3701 break;
3702 continue;
3703
3704 case GLOBAL:
3705 if (load_global(self) < 0)
3706 break;
3707 continue;
3708
3709 case APPEND:
3710 if (load_append(self) < 0)
3711 break;
3712 continue;
3713
3714 case APPENDS:
3715 if (load_appends(self) < 0)
3716 break;
3717 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003718
Guido van Rossum60456fd1997-04-09 17:36:32 +00003719 case BUILD:
3720 if (load_build(self) < 0)
3721 break;
3722 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003723
Guido van Rossum60456fd1997-04-09 17:36:32 +00003724 case DUP:
3725 if (load_dup(self) < 0)
3726 break;
3727 continue;
3728
3729 case BINGET:
3730 if (load_binget(self) < 0)
3731 break;
3732 continue;
3733
3734 case LONG_BINGET:
3735 if (load_long_binget(self) < 0)
3736 break;
3737 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003738
Guido van Rossum60456fd1997-04-09 17:36:32 +00003739 case GET:
3740 if (load_get(self) < 0)
3741 break;
3742 continue;
3743
3744 case MARK:
3745 if (load_mark(self) < 0)
3746 break;
3747 continue;
3748
3749 case BINPUT:
3750 if (load_binput(self) < 0)
3751 break;
3752 continue;
3753
3754 case LONG_BINPUT:
3755 if (load_long_binput(self) < 0)
3756 break;
3757 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003758
Guido van Rossum60456fd1997-04-09 17:36:32 +00003759 case PUT:
3760 if (load_put(self) < 0)
3761 break;
3762 continue;
3763
3764 case POP:
3765 if (load_pop(self) < 0)
3766 break;
3767 continue;
3768
3769 case POP_MARK:
3770 if (load_pop_mark(self) < 0)
3771 break;
3772 continue;
3773
3774 case SETITEM:
3775 if (load_setitem(self) < 0)
3776 break;
3777 continue;
3778
3779 case SETITEMS:
3780 if (load_setitems(self) < 0)
3781 break;
3782 continue;
3783
3784 case STOP:
3785 break;
3786
3787 case PERSID:
3788 if (load_persid(self) < 0)
3789 break;
3790 continue;
3791
3792 case BINPERSID:
3793 if (load_binpersid(self) < 0)
3794 break;
3795 continue;
3796
3797 case REDUCE:
3798 if (load_reduce(self) < 0)
3799 break;
3800 continue;
3801
Tim Peters84e87f32001-03-17 04:50:51 +00003802 default:
3803 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossum60456fd1997-04-09 17:36:32 +00003804 "c", s[0]);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003805 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003806 }
3807
3808 break;
3809 }
3810
Guido van Rossum053b8df1998-11-25 16:18:00 +00003811 if ((err = PyErr_Occurred())) {
3812 if (err == PyExc_EOFError) {
3813 PyErr_SetNone(PyExc_EOFError);
Tim Peters84e87f32001-03-17 04:50:51 +00003814 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003815 return NULL;
3816 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003817
Tim Peters84e87f32001-03-17 04:50:51 +00003818 PDATA_POP(self->stack, val);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003819 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003820}
Tim Peters84e87f32001-03-17 04:50:51 +00003821
Guido van Rossum60456fd1997-04-09 17:36:32 +00003822
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003823/* No-load functions to support noload, which is used to
3824 find persistent references. */
3825
3826static int
3827noload_obj(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003828 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003829
3830 if ((i = marker(self)) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003831 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003832}
3833
3834
3835static int
3836noload_inst(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003837 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003838 char *s;
3839
3840 if ((i = marker(self)) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003841 Pdata_clear(self->stack, i);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003842 if ((*self->readline_func)(self, &s) < 0) return -1;
3843 if ((*self->readline_func)(self, &s) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003844 PDATA_APPEND(self->stack, Py_None,-1);
3845 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003846}
3847
3848static int
3849noload_global(Unpicklerobject *self) {
3850 char *s;
3851
3852 if ((*self->readline_func)(self, &s) < 0) return -1;
3853 if ((*self->readline_func)(self, &s) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003854 PDATA_APPEND(self->stack, Py_None,-1);
3855 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003856}
3857
3858static int
3859noload_reduce(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003860
Guido van Rossum053b8df1998-11-25 16:18:00 +00003861 if (self->stack->length < 2) return stackUnderflow();
3862 Pdata_clear(self->stack, self->stack->length-2);
3863 PDATA_APPEND(self->stack, Py_None,-1);
3864 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003865}
3866
3867static int
3868noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003869
Guido van Rossum053b8df1998-11-25 16:18:00 +00003870 if (self->stack->length < 1) return stackUnderflow();
3871 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00003872 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003873}
3874
3875
3876static PyObject *
3877noload(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003878 PyObject *err = 0, *val = 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003879 char *s;
3880
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003881 self->num_marks = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003882 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003883
3884 while (1) {
3885 if ((*self->read_func)(self, &s, 1) < 0)
3886 break;
3887
3888 switch (s[0]) {
3889 case NONE:
3890 if (load_none(self) < 0)
3891 break;
3892 continue;
3893
3894 case BININT:
3895 if (load_binint(self) < 0)
3896 break;
3897 continue;
3898
3899 case BININT1:
3900 if (load_binint1(self) < 0)
3901 break;
3902 continue;
3903
3904 case BININT2:
3905 if (load_binint2(self) < 0)
3906 break;
3907 continue;
3908
3909 case INT:
3910 if (load_int(self) < 0)
3911 break;
3912 continue;
3913
3914 case LONG:
3915 if (load_long(self) < 0)
3916 break;
3917 continue;
3918
3919 case FLOAT:
3920 if (load_float(self) < 0)
3921 break;
3922 continue;
3923
3924 case BINFLOAT:
3925 if (load_binfloat(self) < 0)
3926 break;
3927 continue;
3928
3929 case BINSTRING:
3930 if (load_binstring(self) < 0)
3931 break;
3932 continue;
3933
3934 case SHORT_BINSTRING:
3935 if (load_short_binstring(self) < 0)
3936 break;
3937 continue;
3938
3939 case STRING:
3940 if (load_string(self) < 0)
3941 break;
3942 continue;
3943
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003944#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003945 case UNICODE:
3946 if (load_unicode(self) < 0)
3947 break;
3948 continue;
3949
3950 case BINUNICODE:
3951 if (load_binunicode(self) < 0)
3952 break;
3953 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003954#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003955
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003956 case EMPTY_TUPLE:
3957 if (load_empty_tuple(self) < 0)
3958 break;
3959 continue;
3960
3961 case TUPLE:
3962 if (load_tuple(self) < 0)
3963 break;
3964 continue;
3965
3966 case EMPTY_LIST:
3967 if (load_empty_list(self) < 0)
3968 break;
3969 continue;
3970
3971 case LIST:
3972 if (load_list(self) < 0)
3973 break;
3974 continue;
3975
3976 case EMPTY_DICT:
3977 if (load_empty_dict(self) < 0)
3978 break;
3979 continue;
3980
3981 case DICT:
3982 if (load_dict(self) < 0)
3983 break;
3984 continue;
3985
3986 case OBJ:
3987 if (noload_obj(self) < 0)
3988 break;
3989 continue;
3990
3991 case INST:
3992 if (noload_inst(self) < 0)
3993 break;
3994 continue;
3995
3996 case GLOBAL:
3997 if (noload_global(self) < 0)
3998 break;
3999 continue;
4000
4001 case APPEND:
4002 if (load_append(self) < 0)
4003 break;
4004 continue;
4005
4006 case APPENDS:
4007 if (load_appends(self) < 0)
4008 break;
4009 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004010
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004011 case BUILD:
4012 if (noload_build(self) < 0)
4013 break;
4014 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004015
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004016 case DUP:
4017 if (load_dup(self) < 0)
4018 break;
4019 continue;
4020
4021 case BINGET:
4022 if (load_binget(self) < 0)
4023 break;
4024 continue;
4025
4026 case LONG_BINGET:
4027 if (load_long_binget(self) < 0)
4028 break;
4029 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004030
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004031 case GET:
4032 if (load_get(self) < 0)
4033 break;
4034 continue;
4035
4036 case MARK:
4037 if (load_mark(self) < 0)
4038 break;
4039 continue;
4040
4041 case BINPUT:
4042 if (load_binput(self) < 0)
4043 break;
4044 continue;
4045
4046 case LONG_BINPUT:
4047 if (load_long_binput(self) < 0)
4048 break;
4049 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004050
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004051 case PUT:
4052 if (load_put(self) < 0)
4053 break;
4054 continue;
4055
4056 case POP:
4057 if (load_pop(self) < 0)
4058 break;
4059 continue;
4060
4061 case POP_MARK:
4062 if (load_pop_mark(self) < 0)
4063 break;
4064 continue;
4065
4066 case SETITEM:
4067 if (load_setitem(self) < 0)
4068 break;
4069 continue;
4070
4071 case SETITEMS:
4072 if (load_setitems(self) < 0)
4073 break;
4074 continue;
4075
4076 case STOP:
4077 break;
4078
4079 case PERSID:
4080 if (load_persid(self) < 0)
4081 break;
4082 continue;
4083
4084 case BINPERSID:
4085 if (load_binpersid(self) < 0)
4086 break;
4087 continue;
4088
4089 case REDUCE:
4090 if (noload_reduce(self) < 0)
4091 break;
4092 continue;
4093
Tim Peters84e87f32001-03-17 04:50:51 +00004094 default:
4095 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004096 "c", s[0]);
Guido van Rossum053b8df1998-11-25 16:18:00 +00004097 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004098 }
4099
4100 break;
4101 }
4102
Guido van Rossum053b8df1998-11-25 16:18:00 +00004103 if ((err = PyErr_Occurred())) {
4104 if (err == PyExc_EOFError) {
4105 PyErr_SetNone(PyExc_EOFError);
Tim Peters84e87f32001-03-17 04:50:51 +00004106 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00004107 return NULL;
4108 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004109
Tim Peters84e87f32001-03-17 04:50:51 +00004110 PDATA_POP(self->stack, val);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004111 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004112}
Tim Peters84e87f32001-03-17 04:50:51 +00004113
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004114
Guido van Rossum60456fd1997-04-09 17:36:32 +00004115static PyObject *
4116Unpickler_load(Unpicklerobject *self, PyObject *args) {
Tim Peters84e87f32001-03-17 04:50:51 +00004117 UNLESS (PyArg_ParseTuple(args, ":load"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004118 return NULL;
4119
4120 return load(self);
4121}
4122
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004123static PyObject *
4124Unpickler_noload(Unpicklerobject *self, PyObject *args) {
Tim Peters84e87f32001-03-17 04:50:51 +00004125 UNLESS (PyArg_ParseTuple(args, ":noload"))
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004126 return NULL;
4127
4128 return noload(self);
4129}
4130
Guido van Rossum60456fd1997-04-09 17:36:32 +00004131
4132static struct PyMethodDef Unpickler_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004133 {"load", (PyCFunction)Unpickler_load, 1,
4134 "load() -- Load a pickle"
4135 },
4136 {"noload", (PyCFunction)Unpickler_noload, 1,
4137 "noload() -- not load a pickle, but go through most of the motions\n"
4138 "\n"
4139 "This function can be used to read past a pickle without instantiating\n"
4140 "any objects or importing any modules. It can also be used to find all\n"
4141 "persistent references without instantiating any objects or importing\n"
4142 "any modules.\n"
4143 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00004144 {NULL, NULL} /* sentinel */
4145};
4146
4147
4148static Unpicklerobject *
4149newUnpicklerobject(PyObject *f) {
4150 Unpicklerobject *self;
4151
Guido van Rossumb18618d2000-05-03 23:44:39 +00004152 UNLESS (self = PyObject_New(Unpicklerobject, &Unpicklertype))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004153 return NULL;
4154
4155 self->file = NULL;
4156 self->arg = NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00004157 self->stack = (Pdata*)Pdata_New();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004158 self->pers_func = NULL;
4159 self->last_string = NULL;
4160 self->marks = NULL;
4161 self->num_marks = 0;
4162 self->marks_size = 0;
4163 self->buf_size = 0;
4164 self->read = NULL;
Guido van Rossum8a6dba31998-03-06 01:39:39 +00004165 self->readline = NULL;
Guido van Rossum21ef0881998-12-11 03:20:00 +00004166 self->safe_constructors = NULL;
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004167 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004168
Tim Peters84e87f32001-03-17 04:50:51 +00004169 UNLESS (self->memo = PyDict_New())
Guido van Rossum83addc72000-04-21 20:49:36 +00004170 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004171
4172 Py_INCREF(f);
4173 self->file = f;
4174
4175 /* Set read, readline based on type of f */
4176 if (PyFile_Check(f)) {
4177 self->fp = PyFile_AsFile(f);
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00004178 if (self->fp == NULL) {
Guido van Rossum83addc72000-04-21 20:49:36 +00004179 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
4180 goto err;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00004181 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004182 self->read_func = read_file;
4183 self->readline_func = readline_file;
4184 }
4185 else if (PycStringIO_InputCheck(f)) {
4186 self->fp = NULL;
4187 self->read_func = read_cStringIO;
4188 self->readline_func = readline_cStringIO;
4189 }
4190 else {
4191
4192 self->fp = NULL;
4193 self->read_func = read_other;
4194 self->readline_func = readline_other;
4195
Guido van Rossum053b8df1998-11-25 16:18:00 +00004196 UNLESS ((self->readline = PyObject_GetAttr(f, readline_str)) &&
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004197 (self->read = PyObject_GetAttr(f, read_str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004198 PyErr_Clear();
4199 PyErr_SetString( PyExc_TypeError, "argument must have 'read' and "
4200 "'readline' attributes" );
Guido van Rossum053b8df1998-11-25 16:18:00 +00004201 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004202 }
4203 }
4204
Guido van Rossum053b8df1998-11-25 16:18:00 +00004205 if (PyEval_GetRestricted()) {
4206 /* Restricted execution, get private tables */
4207 PyObject *m;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004208
Guido van Rossum053b8df1998-11-25 16:18:00 +00004209 UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
4210 self->safe_constructors=PyObject_GetAttr(m, safe_constructors_str);
4211 Py_DECREF(m);
4212 UNLESS (self->safe_constructors) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004213 }
4214 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00004215 self->safe_constructors=safe_constructors;
4216 Py_INCREF(safe_constructors);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004217 }
4218
Guido van Rossum60456fd1997-04-09 17:36:32 +00004219 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004220
4221err:
4222 Py_DECREF((PyObject *)self);
4223 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004224}
4225
4226
4227static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004228get_Unpickler(PyObject *self, PyObject *args) {
4229 PyObject *file;
Tim Peters84e87f32001-03-17 04:50:51 +00004230
Guido van Rossum43713e52000-02-29 13:59:29 +00004231 UNLESS (PyArg_ParseTuple(args, "O:Unpickler", &file))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004232 return NULL;
4233 return (PyObject *)newUnpicklerobject(file);
4234}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004235
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004236
Guido van Rossum60456fd1997-04-09 17:36:32 +00004237static void
4238Unpickler_dealloc(Unpicklerobject *self) {
4239 Py_XDECREF(self->readline);
4240 Py_XDECREF(self->read);
4241 Py_XDECREF(self->file);
4242 Py_XDECREF(self->memo);
4243 Py_XDECREF(self->stack);
4244 Py_XDECREF(self->pers_func);
4245 Py_XDECREF(self->arg);
4246 Py_XDECREF(self->last_string);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004247 Py_XDECREF(self->safe_constructors);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004248
Guido van Rossum60456fd1997-04-09 17:36:32 +00004249 if (self->marks) {
4250 free(self->marks);
4251 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004252
Guido van Rossum60456fd1997-04-09 17:36:32 +00004253 if (self->buf_size) {
4254 free(self->buf);
4255 }
Tim Peters84e87f32001-03-17 04:50:51 +00004256
Guido van Rossumb18618d2000-05-03 23:44:39 +00004257 PyObject_Del(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004258}
4259
4260
4261static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004262Unpickler_getattr(Unpicklerobject *self, char *name) {
4263 if (!strcmp(name, "persistent_load")) {
4264 if (!self->pers_func) {
4265 PyErr_SetString(PyExc_AttributeError, name);
4266 return NULL;
4267 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004268
Guido van Rossum60456fd1997-04-09 17:36:32 +00004269 Py_INCREF(self->pers_func);
4270 return self->pers_func;
4271 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004272
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004273 if (!strcmp(name, "find_global")) {
4274 if (!self->find_class) {
4275 PyErr_SetString(PyExc_AttributeError, name);
4276 return NULL;
4277 }
4278
4279 Py_INCREF(self->find_class);
4280 return self->find_class;
4281 }
4282
Guido van Rossum60456fd1997-04-09 17:36:32 +00004283 if (!strcmp(name, "memo")) {
4284 if (!self->memo) {
4285 PyErr_SetString(PyExc_AttributeError, name);
4286 return NULL;
4287 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004288
Guido van Rossum60456fd1997-04-09 17:36:32 +00004289 Py_INCREF(self->memo);
4290 return self->memo;
4291 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004292
Guido van Rossum60456fd1997-04-09 17:36:32 +00004293 if (!strcmp(name, "UnpicklingError")) {
4294 Py_INCREF(UnpicklingError);
4295 return UnpicklingError;
4296 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004297
Guido van Rossum60456fd1997-04-09 17:36:32 +00004298 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
4299}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004300
Guido van Rossum60456fd1997-04-09 17:36:32 +00004301
4302static int
4303Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004304
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004305 if (!strcmp(name, "persistent_load")) {
4306 Py_XDECREF(self->pers_func);
4307 self->pers_func = value;
4308 Py_XINCREF(value);
4309 return 0;
4310 }
4311
4312 if (!strcmp(name, "find_global")) {
4313 Py_XDECREF(self->find_class);
4314 self->find_class = value;
4315 Py_XINCREF(value);
4316 return 0;
4317 }
4318
Guido van Rossum053b8df1998-11-25 16:18:00 +00004319 if (! value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004320 PyErr_SetString(PyExc_TypeError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00004321 "attribute deletion is not supported");
4322 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004323 }
4324
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004325 if (strcmp(name, "memo") == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00004326 if (! PyDict_Check(value)) {
4327 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
4328 return -1;
4329 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004330 Py_XDECREF(self->memo);
4331 self->memo = value;
4332 Py_INCREF(value);
4333 return 0;
4334 }
4335
Guido van Rossum60456fd1997-04-09 17:36:32 +00004336 PyErr_SetString(PyExc_AttributeError, name);
4337 return -1;
4338}
4339
4340
4341static PyObject *
4342cpm_dump(PyObject *self, PyObject *args) {
4343 PyObject *ob, *file, *res = NULL;
4344 Picklerobject *pickler = 0;
4345 int bin = 0;
4346
Guido van Rossum053b8df1998-11-25 16:18:00 +00004347 UNLESS (PyArg_ParseTuple(args, "OO|i", &ob, &file, &bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004348 goto finally;
4349
Guido van Rossum053b8df1998-11-25 16:18:00 +00004350 UNLESS (pickler = newPicklerobject(file, bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004351 goto finally;
4352
4353 if (dump(pickler, ob) < 0)
4354 goto finally;
4355
4356 Py_INCREF(Py_None);
4357 res = Py_None;
4358
4359finally:
4360 Py_XDECREF(pickler);
4361
4362 return res;
4363}
4364
4365
4366static PyObject *
4367cpm_dumps(PyObject *self, PyObject *args) {
4368 PyObject *ob, *file = 0, *res = NULL;
4369 Picklerobject *pickler = 0;
4370 int bin = 0;
4371
Guido van Rossum43713e52000-02-29 13:59:29 +00004372 UNLESS (PyArg_ParseTuple(args, "O|i:dumps", &ob, &bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004373 goto finally;
4374
Guido van Rossum053b8df1998-11-25 16:18:00 +00004375 UNLESS (file = PycStringIO->NewOutput(128))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004376 goto finally;
4377
Guido van Rossum053b8df1998-11-25 16:18:00 +00004378 UNLESS (pickler = newPicklerobject(file, bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004379 goto finally;
4380
4381 if (dump(pickler, ob) < 0)
4382 goto finally;
4383
4384 res = PycStringIO->cgetvalue(file);
4385
4386finally:
4387 Py_XDECREF(pickler);
4388 Py_XDECREF(file);
4389
4390 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00004391}
4392
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004393
4394static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004395cpm_load(PyObject *self, PyObject *args) {
4396 Unpicklerobject *unpickler = 0;
4397 PyObject *ob, *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004398
Guido van Rossum43713e52000-02-29 13:59:29 +00004399 UNLESS (PyArg_ParseTuple(args, "O:load", &ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004400 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004401
Guido van Rossum053b8df1998-11-25 16:18:00 +00004402 UNLESS (unpickler = newUnpicklerobject(ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004403 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004404
Guido van Rossum60456fd1997-04-09 17:36:32 +00004405 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004406
Guido van Rossum60456fd1997-04-09 17:36:32 +00004407finally:
4408 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004409
Guido van Rossum60456fd1997-04-09 17:36:32 +00004410 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004411}
4412
4413
4414static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004415cpm_loads(PyObject *self, PyObject *args) {
4416 PyObject *ob, *file = 0, *res = NULL;
4417 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004418
Guido van Rossum43713e52000-02-29 13:59:29 +00004419 UNLESS (PyArg_ParseTuple(args, "S:loads", &ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004420 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004421
Guido van Rossum053b8df1998-11-25 16:18:00 +00004422 UNLESS (file = PycStringIO->NewInput(ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004423 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00004424
Guido van Rossum053b8df1998-11-25 16:18:00 +00004425 UNLESS (unpickler = newUnpicklerobject(file))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004426 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004427
Guido van Rossum60456fd1997-04-09 17:36:32 +00004428 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004429
Guido van Rossum60456fd1997-04-09 17:36:32 +00004430finally:
4431 Py_XDECREF(file);
4432 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004433
Guido van Rossum60456fd1997-04-09 17:36:32 +00004434 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004435}
4436
4437
Tim Peters84e87f32001-03-17 04:50:51 +00004438static char Unpicklertype__doc__[] =
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004439"Objects that know how to unpickle";
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004440
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004441static PyTypeObject Unpicklertype = {
4442 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004443 0, /*ob_size*/
4444 "Unpickler", /*tp_name*/
4445 sizeof(Unpicklerobject), /*tp_basicsize*/
4446 0, /*tp_itemsize*/
4447 /* methods */
4448 (destructor)Unpickler_dealloc, /*tp_dealloc*/
4449 (printfunc)0, /*tp_print*/
4450 (getattrfunc)Unpickler_getattr, /*tp_getattr*/
4451 (setattrfunc)Unpickler_setattr, /*tp_setattr*/
4452 (cmpfunc)0, /*tp_compare*/
4453 (reprfunc)0, /*tp_repr*/
4454 0, /*tp_as_number*/
4455 0, /*tp_as_sequence*/
4456 0, /*tp_as_mapping*/
4457 (hashfunc)0, /*tp_hash*/
4458 (ternaryfunc)0, /*tp_call*/
4459 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004460
Guido van Rossum60456fd1997-04-09 17:36:32 +00004461 /* Space for future expansion */
4462 0L,0L,0L,0L,
4463 Unpicklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004464};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004465
Guido van Rossum60456fd1997-04-09 17:36:32 +00004466static struct PyMethodDef cPickle_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004467 {"dump", (PyCFunction)cpm_dump, 1,
4468 "dump(object, file, [binary]) --"
4469 "Write an object in pickle format to the given file\n"
4470 "\n"
4471 "If the optional argument, binary, is provided and is true, then the\n"
4472 "pickle will be written in binary format, which is more space and\n"
4473 "computationally efficient. \n"
4474 },
4475 {"dumps", (PyCFunction)cpm_dumps, 1,
4476 "dumps(object, [binary]) --"
4477 "Return a string containing an object in pickle format\n"
4478 "\n"
4479 "If the optional argument, binary, is provided and is true, then the\n"
4480 "pickle will be written in binary format, which is more space and\n"
4481 "computationally efficient. \n"
4482 },
4483 {"load", (PyCFunction)cpm_load, 1,
4484 "load(file) -- Load a pickle from the given file"},
4485 {"loads", (PyCFunction)cpm_loads, 1,
4486 "loads(string) -- Load a pickle from the given string"},
4487 {"Pickler", (PyCFunction)get_Pickler, 1,
4488 "Pickler(file, [binary]) -- Create a pickler\n"
4489 "\n"
4490 "If the optional argument, binary, is provided and is true, then\n"
4491 "pickles will be written in binary format, which is more space and\n"
4492 "computationally efficient. \n"
4493 },
4494 {"Unpickler", (PyCFunction)get_Unpickler, 1,
4495 "Unpickler(file) -- Create an unpickler"},
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004496 { NULL, NULL }
4497};
4498
Guido van Rossum60456fd1997-04-09 17:36:32 +00004499static int
Guido van Rossumebba4202000-09-07 14:35:37 +00004500init_stuff(PyObject *module_dict) {
Fred Drake2c7a6852001-07-17 18:34:03 +00004501 PyObject *copy_reg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004502
4503#define INIT_STR(S) UNLESS(S ## _str=PyString_FromString(#S)) return -1;
4504
4505 INIT_STR(__class__);
4506 INIT_STR(__getinitargs__);
4507 INIT_STR(__dict__);
4508 INIT_STR(__getstate__);
4509 INIT_STR(__setstate__);
4510 INIT_STR(__name__);
Guido van Rossum142eeb81997-08-13 03:14:41 +00004511 INIT_STR(__main__);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004512 INIT_STR(__reduce__);
4513 INIT_STR(write);
4514 INIT_STR(__safe_for_unpickling__);
4515 INIT_STR(append);
4516 INIT_STR(read);
4517 INIT_STR(readline);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004518 INIT_STR(copy_reg);
4519 INIT_STR(dispatch_table);
4520 INIT_STR(safe_constructors);
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004521 INIT_STR(__basicnew__);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004522
Guido van Rossum053b8df1998-11-25 16:18:00 +00004523 UNLESS (copy_reg = PyImport_ImportModule("copy_reg"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004524 return -1;
4525
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004526 /* These next few are special because we want to use different
4527 ones in restricted mode. */
4528
Guido van Rossum053b8df1998-11-25 16:18:00 +00004529 UNLESS (dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004530 return -1;
4531
Guido van Rossum053b8df1998-11-25 16:18:00 +00004532 UNLESS (safe_constructors = PyObject_GetAttr(copy_reg,
4533 safe_constructors_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004534 return -1;
4535
4536 Py_DECREF(copy_reg);
4537
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004538 /* Down to here ********************************** */
4539
Guido van Rossum053b8df1998-11-25 16:18:00 +00004540 UNLESS (empty_tuple = PyTuple_New(0))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004541 return -1;
4542
Guido van Rossumc03158b1999-06-09 15:23:31 +00004543 /* Ugh */
4544 UNLESS (t=PyImport_ImportModule("__builtin__")) return -1;
4545 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
4546 return -1;
4547
4548 UNLESS (t=PyDict_New()) return -1;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004549 UNLESS (r=PyRun_String(
4550 "def __init__(self, *args): self.args=args\n\n"
4551 "def __str__(self):\n"
4552 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
4553 Py_file_input,
4554 module_dict, t) ) return -1;
4555 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00004556
4557 UNLESS (PickleError = PyErr_NewException("cPickle.PickleError", NULL, t))
4558 return -1;
4559
4560 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00004561
Tim Peters84e87f32001-03-17 04:50:51 +00004562
4563 UNLESS (PicklingError = PyErr_NewException("cPickle.PicklingError",
Guido van Rossumc03158b1999-06-09 15:23:31 +00004564 PickleError, NULL))
4565 return -1;
4566
4567 UNLESS (t=PyDict_New()) return -1;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004568 UNLESS (r=PyRun_String(
4569 "def __init__(self, *args): self.args=args\n\n"
4570 "def __str__(self):\n"
4571 " a=self.args\n"
4572 " a=a and type(a[0]) or '(what)'\n"
4573 " return 'Cannot pickle %s objects' % a\n"
4574 , Py_file_input,
4575 module_dict, t) ) return -1;
4576 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00004577
4578 UNLESS (UnpickleableError = PyErr_NewException(
4579 "cPickle.UnpickleableError", PicklingError, t))
4580 return -1;
4581
4582 Py_DECREF(t);
4583
Tim Peters84e87f32001-03-17 04:50:51 +00004584 UNLESS (UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
Guido van Rossumc03158b1999-06-09 15:23:31 +00004585 PickleError, NULL))
4586 return -1;
4587
Tim Peters84e87f32001-03-17 04:50:51 +00004588 if (PyDict_SetItemString(module_dict, "PickleError",
Guido van Rossumc03158b1999-06-09 15:23:31 +00004589 PickleError) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004590 return -1;
4591
Tim Peters84e87f32001-03-17 04:50:51 +00004592 if (PyDict_SetItemString(module_dict, "PicklingError",
Guido van Rossum60456fd1997-04-09 17:36:32 +00004593 PicklingError) < 0)
4594 return -1;
4595
Guido van Rossum60456fd1997-04-09 17:36:32 +00004596 if (PyDict_SetItemString(module_dict, "UnpicklingError",
4597 UnpicklingError) < 0)
4598 return -1;
4599
Guido van Rossumc03158b1999-06-09 15:23:31 +00004600 if (PyDict_SetItemString(module_dict, "UnpickleableError",
4601 UnpickleableError) < 0)
4602 return -1;
4603
Guido van Rossum053b8df1998-11-25 16:18:00 +00004604 UNLESS (BadPickleGet = PyString_FromString("cPickle.BadPickleGet"))
4605 return -1;
4606
4607 if (PyDict_SetItemString(module_dict, "BadPickleGet",
4608 BadPickleGet) < 0)
4609 return -1;
4610
Guido van Rossum60456fd1997-04-09 17:36:32 +00004611 PycString_IMPORT;
Tim Peters84e87f32001-03-17 04:50:51 +00004612
Guido van Rossum60456fd1997-04-09 17:36:32 +00004613 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004614}
4615
Guido van Rossumf9ffb031999-02-04 14:54:04 +00004616#ifndef DL_EXPORT /* declarations for DLL import/export */
4617#define DL_EXPORT(RTYPE) RTYPE
4618#endif
Guido van Rossum50f385c1998-12-04 18:48:44 +00004619DL_EXPORT(void)
Thomas Wouters58d05102000-07-24 14:43:35 +00004620initcPickle(void) {
Guido van Rossumebba4202000-09-07 14:35:37 +00004621 PyObject *m, *d, *di, *v, *k;
4622 int i;
Guido van Rossum2f80d961999-07-13 15:18:58 +00004623 char *rev="1.71";
Guido van Rossum60456fd1997-04-09 17:36:32 +00004624 PyObject *format_version;
4625 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004626
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004627 Picklertype.ob_type = &PyType_Type;
4628 Unpicklertype.ob_type = &PyType_Type;
Guido van Rossum053b8df1998-11-25 16:18:00 +00004629 PdataType.ob_type = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004630
Tim Peters84e87f32001-03-17 04:50:51 +00004631 /* Initialize some pieces. We need to do this before module creation,
4632 so we're forced to use a temporary dictionary. :(
Guido van Rossumebba4202000-09-07 14:35:37 +00004633 */
4634 di=PyDict_New();
4635 if (!di) return;
4636 if (init_stuff(di) < 0) return;
4637
Guido van Rossum60456fd1997-04-09 17:36:32 +00004638 /* Create the module and add the functions */
4639 m = Py_InitModule4("cPickle", cPickle_methods,
4640 cPickle_module_documentation,
4641 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004642
Guido van Rossum60456fd1997-04-09 17:36:32 +00004643 /* Add some symbolic constants to the module */
4644 d = PyModule_GetDict(m);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004645 PyDict_SetItemString(d,"__version__", v = PyString_FromString(rev));
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004646 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00004647
Guido van Rossumebba4202000-09-07 14:35:37 +00004648 /* Copy data from di. Waaa. */
4649 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
4650 if (PyObject_SetItem(d, k, v) < 0) {
4651 Py_DECREF(di);
4652 return;
4653 }
4654 }
4655 Py_DECREF(di);
4656
Guido van Rossum60456fd1997-04-09 17:36:32 +00004657 format_version = PyString_FromString("1.3");
4658 compatible_formats = Py_BuildValue("[sss]", "1.0", "1.1", "1.2");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004659
Guido van Rossum60456fd1997-04-09 17:36:32 +00004660 PyDict_SetItemString(d, "format_version", format_version);
4661 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004662 Py_XDECREF(format_version);
4663 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004664}