blob: 46f2cc00bfa4852dbd2db70535e01abb0d089f43 [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
130/* atol function from string module */
131static PyObject *atol_func;
132
Guido van Rossumc03158b1999-06-09 15:23:31 +0000133static PyObject *PickleError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000134static PyObject *PicklingError;
Guido van Rossumc03158b1999-06-09 15:23:31 +0000135static PyObject *UnpickleableError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000136static PyObject *UnpicklingError;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000137static PyObject *BadPickleGet;
138
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000139
Guido van Rossum60456fd1997-04-09 17:36:32 +0000140static PyObject *dispatch_table;
141static PyObject *safe_constructors;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000142static PyObject *empty_tuple;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000143
Guido van Rossum60456fd1997-04-09 17:36:32 +0000144static PyObject *__class___str, *__getinitargs___str, *__dict___str,
145 *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
146 *write_str, *__safe_for_unpickling___str, *append_str,
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000147 *read_str, *readline_str, *__main___str, *__basicnew___str,
Guido van Rossum053b8df1998-11-25 16:18:00 +0000148 *copy_reg_str, *dispatch_table_str, *safe_constructors_str, *empty_str;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000149
Guido van Rossum053b8df1998-11-25 16:18:00 +0000150#ifndef PyList_SET_ITEM
151#define PyList_SET_ITEM(op, i, v) (((PyListObject *)(op))->ob_item[i] = (v))
152#endif
153#ifndef PyList_GET_SIZE
154#define PyList_GET_SIZE(op) (((PyListObject *)(op))->ob_size)
155#endif
156#ifndef PyTuple_SET_ITEM
157#define PyTuple_SET_ITEM(op, i, v) (((PyTupleObject *)(op))->ob_item[i] = (v))
158#endif
159#ifndef PyTuple_GET_SIZE
160#define PyTuple_GET_SIZE(op) (((PyTupleObject *)(op))->ob_size)
161#endif
162#ifndef PyString_GET_SIZE
163#define PyString_GET_SIZE(op) (((PyStringObject *)(op))->ob_size)
164#endif
165
166/*************************************************************************
167 Internal Data type for pickle data. */
168
169typedef struct {
170 PyObject_HEAD
171 int length, size;
172 PyObject **data;
173} Pdata;
174
Tim Peters84e87f32001-03-17 04:50:51 +0000175static void
Guido van Rossum053b8df1998-11-25 16:18:00 +0000176Pdata_dealloc(Pdata *self) {
177 int i;
178 PyObject **p;
179
180 for (i=self->length, p=self->data; --i >= 0; p++) Py_DECREF(*p);
181
182 if (self->data) free(self->data);
183
Guido van Rossumb18618d2000-05-03 23:44:39 +0000184 PyObject_Del(self);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000185}
186
187static PyTypeObject PdataType = {
188 PyObject_HEAD_INIT(NULL) 0, "Pdata", sizeof(Pdata), 0,
189 (destructor)Pdata_dealloc,
190 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
191};
192
193#define Pdata_Check(O) ((O)->ob_type == &PdataType)
194
195static PyObject *
Thomas Wouters58d05102000-07-24 14:43:35 +0000196Pdata_New(void) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000197 Pdata *self;
198
Guido van Rossumb18618d2000-05-03 23:44:39 +0000199 UNLESS (self = PyObject_New(Pdata, &PdataType)) return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000200 self->size=8;
201 self->length=0;
202 self->data=malloc(self->size * sizeof(PyObject*));
203 if (self->data) return (PyObject*)self;
204 Py_DECREF(self);
205 return PyErr_NoMemory();
206}
207
Tim Peters84e87f32001-03-17 04:50:51 +0000208static int
Thomas Wouters58d05102000-07-24 14:43:35 +0000209stackUnderflow(void) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000210 PyErr_SetString(UnpicklingError, "unpickling stack underflow");
211 return -1;
212}
213
214static int
215Pdata_clear(Pdata *self, int clearto) {
216 int i;
217 PyObject **p;
218
219 if (clearto < 0) return stackUnderflow();
220 if (clearto >= self->length) return 0;
221
222 for (i=self->length, p=self->data+clearto; --i >= clearto; p++)
223 Py_DECREF(*p);
224 self->length=clearto;
225
226 return 0;
227}
228
229
Tim Peters84e87f32001-03-17 04:50:51 +0000230static int
Guido van Rossum053b8df1998-11-25 16:18:00 +0000231Pdata_grow(Pdata *self) {
232 if (! self->size) {
233 PyErr_NoMemory();
234 return -1;
235 }
Tim Peters84e87f32001-03-17 04:50:51 +0000236 self->size *= 2;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000237 self->data = realloc(self->data, self->size*sizeof(PyObject*));
238 if (! self->data) {
Tim Peters84e87f32001-03-17 04:50:51 +0000239 self->size = 0;
240 PyErr_NoMemory();
241 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000242 }
243 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +0000244}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000245
246#define PDATA_POP(D,V) { \
247 if ((D)->length) V=D->data[--((D)->length)]; \
248 else { \
249 PyErr_SetString(UnpicklingError, "bad pickle data"); \
250 V=NULL; \
251 } \
252}
253
254
255static PyObject *
256Pdata_popTuple(Pdata *self, int start) {
257 PyObject *r;
258 int i, j, l;
259
260 l=self->length-start;
261 UNLESS (r=PyTuple_New(l)) return NULL;
Guido van Rossumea2b7152000-05-09 18:14:50 +0000262 for (i=start, j=0 ; j < l; i++, j++)
263 PyTuple_SET_ITEM(r, j, self->data[i]);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000264
265 self->length=start;
266 return r;
267}
268
269static PyObject *
270Pdata_popList(Pdata *self, int start) {
271 PyObject *r;
272 int i, j, l;
273
274 l=self->length-start;
275 UNLESS (r=PyList_New(l)) return NULL;
Guido van Rossumea2b7152000-05-09 18:14:50 +0000276 for (i=start, j=0 ; j < l; i++, j++)
277 PyList_SET_ITEM(r, j, self->data[i]);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000278
279 self->length=start;
280 return r;
281}
282
283#define PDATA_APPEND_(D,O,ER) { \
284 if (Pdata_Append(((Pdata*)(D)), O) < 0) return ER; \
285}
286
287#define PDATA_APPEND(D,O,ER) { \
288 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
289 Pdata_grow((Pdata*)(D)) < 0) \
290 return ER; \
291 Py_INCREF(O); \
292 ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O; \
293}
294
295#define PDATA_PUSH(D,O,ER) { \
296 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
297 Pdata_grow((Pdata*)(D)) < 0) { \
298 Py_DECREF(O); \
299 return ER; \
300 } \
301 ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O; \
302}
303
304/*************************************************************************/
305
306#define ARG_TUP(self, o) { \
307 if (self->arg || (self->arg=PyTuple_New(1))) { \
308 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
309 PyTuple_SET_ITEM(self->arg,0,o); \
310 } \
311 else { \
312 Py_DECREF(o); \
313 } \
314}
315
316#define FREE_ARG_TUP(self) { \
317 if (self->arg->ob_refcnt > 1) { \
318 Py_DECREF(self->arg); \
319 self->arg=NULL; \
320 } \
321 }
322
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000323typedef struct Picklerobject {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000324 PyObject_HEAD
325 FILE *fp;
326 PyObject *write;
327 PyObject *file;
328 PyObject *memo;
329 PyObject *arg;
330 PyObject *pers_func;
331 PyObject *inst_pers_func;
332 int bin;
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000333 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000334 int (*write_func)(struct Picklerobject *, char *, int);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000335 char *write_buf;
336 int buf_size;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000337 PyObject *dispatch_table;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000338} Picklerobject;
339
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000340staticforward PyTypeObject Picklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000341
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000342typedef struct Unpicklerobject {
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000343 PyObject_HEAD
344 FILE *fp;
345 PyObject *file;
346 PyObject *readline;
347 PyObject *read;
348 PyObject *memo;
349 PyObject *arg;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000350 Pdata *stack;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000351 PyObject *mark;
352 PyObject *pers_func;
353 PyObject *last_string;
354 int *marks;
355 int num_marks;
356 int marks_size;
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000357 int (*read_func)(struct Unpicklerobject *, char **, int);
358 int (*readline_func)(struct Unpicklerobject *, char **);
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000359 int buf_size;
360 char *buf;
361 PyObject *safe_constructors;
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +0000362 PyObject *find_class;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000363} Unpicklerobject;
Tim Peters84e87f32001-03-17 04:50:51 +0000364
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000365staticforward PyTypeObject Unpicklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000366
Thomas Wouters4789b3a2000-07-24 11:36:47 +0000367/* Forward decls that need the above structs */
368static int save(Picklerobject *, PyObject *, int);
369static int put2(Picklerobject *, PyObject *);
370
Tim Peters84e87f32001-03-17 04:50:51 +0000371int
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000372cPickle_PyMapping_HasKey(PyObject *o, PyObject *key) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000373 PyObject *v;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000374
Guido van Rossum053b8df1998-11-25 16:18:00 +0000375 if ((v = PyObject_GetItem(o,key))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000376 Py_DECREF(v);
377 return 1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000378 }
379
Guido van Rossum60456fd1997-04-09 17:36:32 +0000380 PyErr_Clear();
381 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000382}
383
Guido van Rossumd385d591997-04-09 17:47:47 +0000384static
Guido van Rossum60456fd1997-04-09 17:36:32 +0000385PyObject *
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000386cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
387{
Guido van Rossum60456fd1997-04-09 17:36:32 +0000388 va_list va;
389 PyObject *args=0, *retval=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000390 va_start(va, format);
Tim Peters84e87f32001-03-17 04:50:51 +0000391
Guido van Rossum053b8df1998-11-25 16:18:00 +0000392 if (format) args = Py_VaBuildValue(format, va);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000393 va_end(va);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000394 if (format && ! args) return NULL;
395 if (stringformat && !(retval=PyString_FromString(stringformat))) return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000396
Guido van Rossum053b8df1998-11-25 16:18:00 +0000397 if (retval) {
398 if (args) {
399 PyObject *v;
400 v=PyString_Format(retval, args);
401 Py_DECREF(retval);
402 Py_DECREF(args);
403 if (! v) return NULL;
404 retval=v;
405 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000406 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000407 else
Guido van Rossum053b8df1998-11-25 16:18:00 +0000408 if (args) retval=args;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000409 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000410 PyErr_SetObject(ErrType,Py_None);
411 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000412 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000413 PyErr_SetObject(ErrType,retval);
414 Py_DECREF(retval);
415 return NULL;
416}
417
Tim Peters84e87f32001-03-17 04:50:51 +0000418static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000419write_file(Picklerobject *self, char *s, int n) {
Tim Peters84e87f32001-03-17 04:50:51 +0000420 size_t nbyteswritten;
421
Guido van Rossum60456fd1997-04-09 17:36:32 +0000422 if (s == NULL) {
423 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000424 }
425
Tim Peters84e87f32001-03-17 04:50:51 +0000426 Py_BEGIN_ALLOW_THREADS
427 nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
428 Py_END_ALLOW_THREADS
429 if (nbyteswritten != (size_t)n) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000430 PyErr_SetFromErrno(PyExc_IOError);
431 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000432 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000433
434 return n;
435}
436
Tim Peters84e87f32001-03-17 04:50:51 +0000437static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000438write_cStringIO(Picklerobject *self, char *s, int n) {
439 if (s == NULL) {
440 return 0;
441 }
442
443 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
444 return -1;
445 }
446
447 return n;
448}
449
Tim Peters84e87f32001-03-17 04:50:51 +0000450static int
Guido van Rossum142eeb81997-08-13 03:14:41 +0000451write_none(Picklerobject *self, char *s, int n) {
452 if (s == NULL) return 0;
453 return n;
454}
455
Tim Peters84e87f32001-03-17 04:50:51 +0000456static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000457write_other(Picklerobject *self, char *s, int n) {
458 PyObject *py_str = 0, *junk = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000459
460 if (s == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000461 UNLESS (self->buf_size) return 0;
Tim Peters84e87f32001-03-17 04:50:51 +0000462 UNLESS (py_str =
Guido van Rossum60456fd1997-04-09 17:36:32 +0000463 PyString_FromStringAndSize(self->write_buf, self->buf_size))
Guido van Rossum053b8df1998-11-25 16:18:00 +0000464 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000465 }
466 else {
467 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
468 if (write_other(self, NULL, 0) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +0000469 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000470 }
471
Tim Peters84e87f32001-03-17 04:50:51 +0000472 if (n > WRITE_BUF_SIZE) {
473 UNLESS (py_str =
Guido van Rossum60456fd1997-04-09 17:36:32 +0000474 PyString_FromStringAndSize(s, n))
Guido van Rossum053b8df1998-11-25 16:18:00 +0000475 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000476 }
477 else {
478 memcpy(self->write_buf + self->buf_size, s, n);
479 self->buf_size += n;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000480 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000481 }
482 }
483
Guido van Rossum053b8df1998-11-25 16:18:00 +0000484 if (self->write) {
485 /* object with write method */
486 ARG_TUP(self, py_str);
487 if (self->arg) {
488 junk = PyObject_CallObject(self->write, self->arg);
489 FREE_ARG_TUP(self);
490 }
491 if (junk) Py_DECREF(junk);
492 else return -1;
493 }
Tim Peters84e87f32001-03-17 04:50:51 +0000494 else
Guido van Rossum053b8df1998-11-25 16:18:00 +0000495 PDATA_PUSH(self->file, py_str, -1);
Tim Peters84e87f32001-03-17 04:50:51 +0000496
497 self->buf_size = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000498 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000499}
500
501
Tim Peters84e87f32001-03-17 04:50:51 +0000502static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000503read_file(Unpicklerobject *self, char **s, int n) {
Tim Peters84e87f32001-03-17 04:50:51 +0000504 size_t nbytesread;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000505
506 if (self->buf_size == 0) {
507 int size;
508
Tim Peters84e87f32001-03-17 04:50:51 +0000509 size = ((n < 32) ? 32 : n);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000510 UNLESS (self->buf = (char *)malloc(size * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000511 PyErr_NoMemory();
512 return -1;
513 }
514
515 self->buf_size = size;
516 }
517 else if (n > self->buf_size) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000518 UNLESS (self->buf = (char *)realloc(self->buf, n * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000519 PyErr_NoMemory();
520 return -1;
521 }
Tim Peters84e87f32001-03-17 04:50:51 +0000522
Guido van Rossum60456fd1997-04-09 17:36:32 +0000523 self->buf_size = n;
524 }
Tim Peters84e87f32001-03-17 04:50:51 +0000525
526 Py_BEGIN_ALLOW_THREADS
527 nbytesread = fread(self->buf, sizeof(char), n, self->fp);
528 Py_END_ALLOW_THREADS
529 if (nbytesread != (size_t)n) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000530 if (feof(self->fp)) {
531 PyErr_SetNone(PyExc_EOFError);
532 return -1;
533 }
534
535 PyErr_SetFromErrno(PyExc_IOError);
536 return -1;
537 }
538
539 *s = self->buf;
540
541 return n;
542}
543
544
Tim Peters84e87f32001-03-17 04:50:51 +0000545static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000546readline_file(Unpicklerobject *self, char **s) {
547 int i;
548
549 if (self->buf_size == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000550 UNLESS (self->buf = (char *)malloc(40 * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000551 PyErr_NoMemory();
552 return -1;
553 }
Tim Peters84e87f32001-03-17 04:50:51 +0000554
Guido van Rossum60456fd1997-04-09 17:36:32 +0000555 self->buf_size = 40;
556 }
557
558 i = 0;
559 while (1) {
560 for (; i < (self->buf_size - 1); i++) {
561 if (feof(self->fp) || (self->buf[i] = getc(self->fp)) == '\n') {
562 self->buf[i + 1] = '\0';
563 *s = self->buf;
564 return i + 1;
565 }
566 }
567
Tim Peters84e87f32001-03-17 04:50:51 +0000568 UNLESS (self->buf = (char *)realloc(self->buf,
Guido van Rossum60456fd1997-04-09 17:36:32 +0000569 (self->buf_size * 2) * sizeof(char))) {
570 PyErr_NoMemory();
571 return -1;
572 }
573
574 self->buf_size *= 2;
575 }
576
Tim Peters84e87f32001-03-17 04:50:51 +0000577}
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000578
579
Tim Peters84e87f32001-03-17 04:50:51 +0000580static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000581read_cStringIO(Unpicklerobject *self, char **s, int n) {
582 char *ptr;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000583
Guido van Rossum60456fd1997-04-09 17:36:32 +0000584 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
585 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000586 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000587 }
588
Guido van Rossum60456fd1997-04-09 17:36:32 +0000589 *s = ptr;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000590
Guido van Rossum60456fd1997-04-09 17:36:32 +0000591 return n;
592}
593
594
Tim Peters84e87f32001-03-17 04:50:51 +0000595static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000596readline_cStringIO(Unpicklerobject *self, char **s) {
597 int n;
598 char *ptr;
599
600 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
601 return -1;
602 }
603
604 *s = ptr;
605
606 return n;
607}
608
609
Tim Peters84e87f32001-03-17 04:50:51 +0000610static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000611read_other(Unpicklerobject *self, char **s, int n) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000612 PyObject *bytes, *str=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000613
Guido van Rossum053b8df1998-11-25 16:18:00 +0000614 UNLESS (bytes = PyInt_FromLong(n)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000615
Guido van Rossum053b8df1998-11-25 16:18:00 +0000616 ARG_TUP(self, bytes);
617 if (self->arg) {
618 str = PyObject_CallObject(self->read, self->arg);
619 FREE_ARG_TUP(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000620 }
Guido van Rossum053b8df1998-11-25 16:18:00 +0000621 if (! str) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000622
623 Py_XDECREF(self->last_string);
624 self->last_string = str;
625
Guido van Rossum053b8df1998-11-25 16:18:00 +0000626 if (! (*s = PyString_AsString(str))) return -1;
627 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000628}
629
630
Tim Peters84e87f32001-03-17 04:50:51 +0000631static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000632readline_other(Unpicklerobject *self, char **s) {
633 PyObject *str;
634 int str_size;
635
Guido van Rossum053b8df1998-11-25 16:18:00 +0000636 UNLESS (str = PyObject_CallObject(self->readline, empty_tuple)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000637 return -1;
638 }
639
Guido van Rossum053b8df1998-11-25 16:18:00 +0000640 if ((str_size = PyString_Size(str)) < 0)
641 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000642
643 Py_XDECREF(self->last_string);
644 self->last_string = str;
645
Guido van Rossum053b8df1998-11-25 16:18:00 +0000646 if (! (*s = PyString_AsString(str)))
647 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000648
649 return str_size;
650}
651
652
653static char *
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000654pystrndup(char *s, int l) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000655 char *r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000656 UNLESS (r=malloc((l+1)*sizeof(char))) return (char*)PyErr_NoMemory();
Guido van Rossum60456fd1997-04-09 17:36:32 +0000657 memcpy(r,s,l);
658 r[l]=0;
659 return r;
660}
661
662
663static int
664get(Picklerobject *self, PyObject *id) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000665 PyObject *value, *mv;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000666 long c_value;
667 char s[30];
Guido van Rossum534b7c52000-06-28 22:23:56 +0000668 size_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000669
Guido van Rossum053b8df1998-11-25 16:18:00 +0000670 UNLESS (mv = PyDict_GetItem(self->memo, id)) {
671 PyErr_SetObject(PyExc_KeyError, id);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000672 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000673 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000674
Guido van Rossum053b8df1998-11-25 16:18:00 +0000675 UNLESS (value = PyTuple_GetItem(mv, 0))
Guido van Rossum60456fd1997-04-09 17:36:32 +0000676 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000677
Guido van Rossum053b8df1998-11-25 16:18:00 +0000678 UNLESS (PyInt_Check(value)) {
679 PyErr_SetString(PicklingError, "no int where int expected in memo");
680 return -1;
681 }
682 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000683
684 if (!self->bin) {
685 s[0] = GET;
686 sprintf(s + 1, "%ld\n", c_value);
687 len = strlen(s);
688 }
Guido van Rossum053b8df1998-11-25 16:18:00 +0000689 else if (Pdata_Check(self->file)) {
690 if (write_other(self, NULL, 0) < 0) return -1;
691 PDATA_APPEND(self->file, mv, -1);
692 return 0;
693 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000694 else {
695 if (c_value < 256) {
696 s[0] = BINGET;
697 s[1] = (int)(c_value & 0xff);
698 len = 2;
699 }
700 else {
701 s[0] = LONG_BINGET;
702 s[1] = (int)(c_value & 0xff);
703 s[2] = (int)((c_value >> 8) & 0xff);
704 s[3] = (int)((c_value >> 16) & 0xff);
705 s[4] = (int)((c_value >> 24) & 0xff);
706 len = 5;
707 }
708 }
709
710 if ((*self->write_func)(self, s, len) < 0)
711 return -1;
712
713 return 0;
714}
Tim Peters84e87f32001-03-17 04:50:51 +0000715
Guido van Rossum60456fd1997-04-09 17:36:32 +0000716
717static int
718put(Picklerobject *self, PyObject *ob) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000719 if (ob->ob_refcnt < 2 || self->fast)
Guido van Rossum60456fd1997-04-09 17:36:32 +0000720 return 0;
721
722 return put2(self, ob);
723}
724
Tim Peters84e87f32001-03-17 04:50:51 +0000725
Guido van Rossum60456fd1997-04-09 17:36:32 +0000726static int
727put2(Picklerobject *self, PyObject *ob) {
728 char c_str[30];
Guido van Rossum534b7c52000-06-28 22:23:56 +0000729 int p;
730 size_t len;
731 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000732 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000733
734 if (self->fast) return 0;
735
Guido van Rossum60456fd1997-04-09 17:36:32 +0000736 if ((p = PyDict_Size(self->memo)) < 0)
737 goto finally;
738
Guido van Rossum053b8df1998-11-25 16:18:00 +0000739 p++; /* Make sure memo keys are positive! */
740
Guido van Rossum534b7c52000-06-28 22:23:56 +0000741 UNLESS (py_ob_id = PyLong_FromVoidPtr(ob))
Guido van Rossum053b8df1998-11-25 16:18:00 +0000742 goto finally;
743
744 UNLESS (memo_len = PyInt_FromLong(p))
745 goto finally;
746
747 UNLESS (t = PyTuple_New(2))
748 goto finally;
749
750 PyTuple_SET_ITEM(t, 0, memo_len);
751 Py_INCREF(memo_len);
752 PyTuple_SET_ITEM(t, 1, ob);
753 Py_INCREF(ob);
754
755 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
756 goto finally;
757
Guido van Rossum60456fd1997-04-09 17:36:32 +0000758 if (!self->bin) {
759 c_str[0] = PUT;
760 sprintf(c_str + 1, "%d\n", p);
761 len = strlen(c_str);
762 }
Guido van Rossum053b8df1998-11-25 16:18:00 +0000763 else if (Pdata_Check(self->file)) {
764 if (write_other(self, NULL, 0) < 0) return -1;
765 PDATA_APPEND(self->file, memo_len, -1);
766 res=0; /* Job well done ;) */
767 goto finally;
768 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000769 else {
770 if (p >= 256) {
771 c_str[0] = LONG_BINPUT;
772 c_str[1] = (int)(p & 0xff);
773 c_str[2] = (int)((p >> 8) & 0xff);
774 c_str[3] = (int)((p >> 16) & 0xff);
775 c_str[4] = (int)((p >> 24) & 0xff);
776 len = 5;
777 }
778 else {
779 c_str[0] = BINPUT;
780 c_str[1] = p;
Tim Peters84e87f32001-03-17 04:50:51 +0000781 len = 2;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000782 }
783 }
784
785 if ((*self->write_func)(self, c_str, len) < 0)
786 goto finally;
787
Guido van Rossum60456fd1997-04-09 17:36:32 +0000788 res = 0;
789
790finally:
791 Py_XDECREF(py_ob_id);
792 Py_XDECREF(memo_len);
793 Py_XDECREF(t);
794
795 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000796}
797
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000798#define PyImport_Import cPickle_Import
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000799
800static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000801PyImport_Import(PyObject *module_name) {
802 static PyObject *silly_list=0, *__builtins___str=0, *__import___str;
803 static PyObject *standard_builtins=0;
804 PyObject *globals=0, *__import__=0, *__builtins__=0, *r=0;
805
Guido van Rossum053b8df1998-11-25 16:18:00 +0000806 UNLESS (silly_list) {
807 UNLESS (__import___str=PyString_FromString("__import__"))
808 return NULL;
809 UNLESS (__builtins___str=PyString_FromString("__builtins__"))
810 return NULL;
811 UNLESS (silly_list=Py_BuildValue("[s]","__doc__"))
812 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000813 }
814
Guido van Rossum053b8df1998-11-25 16:18:00 +0000815 if ((globals=PyEval_GetGlobals())) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000816 Py_INCREF(globals);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000817 UNLESS (__builtins__=PyObject_GetItem(globals,__builtins___str))
818 goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000819 }
820 else {
821 PyErr_Clear();
822
Guido van Rossum053b8df1998-11-25 16:18:00 +0000823 UNLESS (standard_builtins ||
824 (standard_builtins=PyImport_ImportModule("__builtin__")))
825 return NULL;
Tim Peters84e87f32001-03-17 04:50:51 +0000826
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000827 __builtins__=standard_builtins;
828 Py_INCREF(__builtins__);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000829 UNLESS (globals = Py_BuildValue("{sO}", "__builtins__", __builtins__))
830 goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000831 }
832
Guido van Rossum053b8df1998-11-25 16:18:00 +0000833 if (PyDict_Check(__builtins__)) {
834 UNLESS (__import__=PyObject_GetItem(__builtins__,__import___str)) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000835 }
836 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000837 UNLESS (__import__=PyObject_GetAttr(__builtins__,__import___str)) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000838 }
839
Guido van Rossum053b8df1998-11-25 16:18:00 +0000840 UNLESS (r=PyObject_CallFunction(__import__,"OOOO",
841 module_name, globals, globals, silly_list))
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000842 goto err;
843
844 Py_DECREF(globals);
845 Py_DECREF(__builtins__);
846 Py_DECREF(__import__);
Tim Peters84e87f32001-03-17 04:50:51 +0000847
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000848 return r;
849err:
850 Py_XDECREF(globals);
851 Py_XDECREF(__builtins__);
852 Py_XDECREF(__import__);
853 return NULL;
854}
855
856static PyObject *
Guido van Rossume2d81cd1998-08-08 19:40:10 +0000857whichmodule(PyObject *global, PyObject *global_name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000858 int i, j;
859 PyObject *module = 0, *modules_dict = 0,
860 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000861
Guido van Rossum45188231997-09-28 05:38:51 +0000862 module = PyObject_GetAttrString(global, "__module__");
863 if (module) return module;
864 PyErr_Clear();
865
Guido van Rossum053b8df1998-11-25 16:18:00 +0000866 UNLESS (modules_dict = PySys_GetObject("modules"))
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000867 return NULL;
868
Guido van Rossum60456fd1997-04-09 17:36:32 +0000869 i = 0;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000870 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
871
Guido van Rossum053b8df1998-11-25 16:18:00 +0000872 if (PyObject_Compare(name, __main___str)==0) continue;
Tim Peters84e87f32001-03-17 04:50:51 +0000873
Guido van Rossum053b8df1998-11-25 16:18:00 +0000874 UNLESS (global_name_attr = PyObject_GetAttr(module, global_name)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000875 PyErr_Clear();
876 continue;
877 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000878
Guido van Rossum60456fd1997-04-09 17:36:32 +0000879 if (global_name_attr != global) {
880 Py_DECREF(global_name_attr);
881 continue;
882 }
883
884 Py_DECREF(global_name_attr);
885
886 break;
887 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000888
889 /* The following implements the rule in pickle.py added in 1.5
890 that used __main__ if no module is found. I don't actually
891 like this rule. jlf
892 */
Guido van Rossum053b8df1998-11-25 16:18:00 +0000893 if (!j) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000894 j=1;
895 name=__main___str;
896 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000897
898 Py_INCREF(name);
899 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000900}
901
902
Guido van Rossum60456fd1997-04-09 17:36:32 +0000903static int
904save_none(Picklerobject *self, PyObject *args) {
905 static char none = NONE;
Tim Peters84e87f32001-03-17 04:50:51 +0000906 if ((*self->write_func)(self, &none, 1) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +0000907 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000908
Guido van Rossum60456fd1997-04-09 17:36:32 +0000909 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000910}
911
Tim Peters84e87f32001-03-17 04:50:51 +0000912
Guido van Rossum60456fd1997-04-09 17:36:32 +0000913static int
914save_int(Picklerobject *self, PyObject *args) {
915 char c_str[32];
916 long l = PyInt_AS_LONG((PyIntObject *)args);
917 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000918
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000919 if (!self->bin
920#if SIZEOF_LONG > 4
Tim Peters3906eb82001-04-10 04:22:00 +0000921 || l > 0x7fffffffL
922 || l < -0x80000000L
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000923#endif
Tim Peters3906eb82001-04-10 04:22:00 +0000924 ) {
925 /* Text-mode pickle, or long too big to fit in the 4-byte
926 * signed BININT format: store as a string.
927 */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000928 c_str[0] = INT;
929 sprintf(c_str + 1, "%ld\n", l);
930 if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
931 return -1;
932 }
933 else {
Tim Petersd8ae7c22001-04-10 04:35:28 +0000934 /* Binary pickle and l fits in a signed 4-byte int. */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000935 c_str[1] = (int)( l & 0xff);
936 c_str[2] = (int)((l >> 8) & 0xff);
937 c_str[3] = (int)((l >> 16) & 0xff);
938 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000939
Guido van Rossum60456fd1997-04-09 17:36:32 +0000940 if ((c_str[4] == 0) && (c_str[3] == 0)) {
941 if (c_str[2] == 0) {
942 c_str[0] = BININT1;
943 len = 2;
944 }
945 else {
946 c_str[0] = BININT2;
947 len = 3;
948 }
949 }
950 else {
951 c_str[0] = BININT;
952 len = 5;
953 }
954
955 if ((*self->write_func)(self, c_str, len) < 0)
956 return -1;
957 }
958
959 return 0;
960}
961
962
963static int
964save_long(Picklerobject *self, PyObject *args) {
965 int size, res = -1;
966 PyObject *repr = 0;
967
968 static char l = LONG;
969
Guido van Rossum053b8df1998-11-25 16:18:00 +0000970 UNLESS (repr = PyObject_Repr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +0000971 goto finally;
972
973 if ((size = PyString_Size(repr)) < 0)
974 goto finally;
975
976 if ((*self->write_func)(self, &l, 1) < 0)
977 goto finally;
978
Tim Peters84e87f32001-03-17 04:50:51 +0000979 if ((*self->write_func)(self,
Guido van Rossum60456fd1997-04-09 17:36:32 +0000980 PyString_AS_STRING((PyStringObject *)repr), size) < 0)
981 goto finally;
982
983 if ((*self->write_func)(self, "\n", 1) < 0)
984 goto finally;
985
986 res = 0;
987
988finally:
989 Py_XDECREF(repr);
990
991 return res;
992}
993
994
995static int
996save_float(Picklerobject *self, PyObject *args) {
997 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
998
Guido van Rossum60456fd1997-04-09 17:36:32 +0000999 if (self->bin) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001000 int s, e;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001001 double f;
1002 long fhi, flo;
1003 char str[9], *p = str;
1004
1005 *p = BINFLOAT;
1006 p++;
1007
1008 if (x < 0) {
1009 s = 1;
1010 x = -x;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001011 }
1012 else
Guido van Rossum60456fd1997-04-09 17:36:32 +00001013 s = 0;
1014
1015 f = frexp(x, &e);
1016
1017 /* Normalize f to be in the range [1.0, 2.0) */
1018 if (0.5 <= f && f < 1.0) {
1019 f *= 2.0;
1020 e--;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001021 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001022 else if (f == 0.0) {
1023 e = 0;
1024 }
1025 else {
1026 PyErr_SetString(PyExc_SystemError,
1027 "frexp() result out of range");
1028 return -1;
1029 }
1030
1031 if (e >= 1024) {
1032 /* XXX 1024 itself is reserved for Inf/NaN */
1033 PyErr_SetString(PyExc_OverflowError,
1034 "float too large to pack with d format");
1035 return -1;
1036 }
1037 else if (e < -1022) {
1038 /* Gradual underflow */
1039 f = ldexp(f, 1022 + e);
1040 e = 0;
1041 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001042 else if (!(e == 0 && f == 0.0)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001043 e += 1023;
1044 f -= 1.0; /* Get rid of leading 1 */
1045 }
1046
1047 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
1048 f *= 268435456.0; /* 2**28 */
1049 fhi = (long) floor(f); /* Truncate */
1050 f -= (double)fhi;
1051 f *= 16777216.0; /* 2**24 */
1052 flo = (long) floor(f + 0.5); /* Round */
1053
1054 /* First byte */
1055 *p = (s<<7) | (e>>4);
1056 p++;
1057
1058 /* Second byte */
Guido van Rossume94e3fb1998-12-08 17:37:19 +00001059 *p = (char) (((e&0xF)<<4) | (fhi>>24));
Guido van Rossum60456fd1997-04-09 17:36:32 +00001060 p++;
1061
1062 /* Third byte */
1063 *p = (fhi>>16) & 0xFF;
1064 p++;
1065
1066 /* Fourth byte */
1067 *p = (fhi>>8) & 0xFF;
1068 p++;
1069
1070 /* Fifth byte */
1071 *p = fhi & 0xFF;
1072 p++;
1073
1074 /* Sixth byte */
1075 *p = (flo>>16) & 0xFF;
1076 p++;
1077
1078 /* Seventh byte */
1079 *p = (flo>>8) & 0xFF;
1080 p++;
1081
1082 /* Eighth byte */
1083 *p = flo & 0xFF;
1084
1085 if ((*self->write_func)(self, str, 9) < 0)
1086 return -1;
1087 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001088 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001089 char c_str[250];
1090 c_str[0] = FLOAT;
Guido van Rossum104be4a1998-04-03 21:13:02 +00001091 sprintf(c_str + 1, "%.17g\n", x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001092
1093 if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
1094 return -1;
1095 }
1096
1097 return 0;
1098}
1099
1100
1101static int
Guido van Rossum142eeb81997-08-13 03:14:41 +00001102save_string(Picklerobject *self, PyObject *args, int doput) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001103 int size, len;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001104 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001105
Guido van Rossum053b8df1998-11-25 16:18:00 +00001106 if ((size = PyString_Size(args)) < 0)
1107 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001108
1109 if (!self->bin) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001110 char *repr_str;
1111
1112 static char string = STRING;
1113
Guido van Rossum053b8df1998-11-25 16:18:00 +00001114 UNLESS (repr = PyObject_Repr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001115 return -1;
1116
Guido van Rossum053b8df1998-11-25 16:18:00 +00001117 if ((len = PyString_Size(repr)) < 0)
1118 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001119 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001120
1121 if ((*self->write_func)(self, &string, 1) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001122 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001123
1124 if ((*self->write_func)(self, repr_str, len) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001125 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001126
1127 if ((*self->write_func)(self, "\n", 1) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001128 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001129
1130 Py_XDECREF(repr);
1131 }
1132 else {
1133 int i;
1134 char c_str[5];
1135
Guido van Rossum053b8df1998-11-25 16:18:00 +00001136 if ((size = PyString_Size(args)) < 0)
1137 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001138
1139 if (size < 256) {
1140 c_str[0] = SHORT_BINSTRING;
1141 c_str[1] = size;
1142 len = 2;
1143 }
1144 else {
1145 c_str[0] = BINSTRING;
1146 for (i = 1; i < 5; i++)
1147 c_str[i] = (int)(size >> ((i - 1) * 8));
1148 len = 5;
1149 }
1150
1151 if ((*self->write_func)(self, c_str, len) < 0)
1152 return -1;
1153
Guido van Rossum053b8df1998-11-25 16:18:00 +00001154 if (size > 128 && Pdata_Check(self->file)) {
1155 if (write_other(self, NULL, 0) < 0) return -1;
1156 PDATA_APPEND(self->file, args, -1);
1157 }
1158 else {
Tim Peters84e87f32001-03-17 04:50:51 +00001159 if ((*self->write_func)(self,
Guido van Rossum053b8df1998-11-25 16:18:00 +00001160 PyString_AS_STRING((PyStringObject *)args), size) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001161 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001162 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001163 }
1164
Guido van Rossum142eeb81997-08-13 03:14:41 +00001165 if (doput)
1166 if (put(self, args) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001167 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001168
1169 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001170
1171err:
1172 Py_XDECREF(repr);
1173 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001174}
1175
1176
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001177/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1178 backslash and newline characters to \uXXXX escapes. */
1179static PyObject *
1180modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, int size)
1181{
1182 PyObject *repr;
1183 char *p;
1184 char *q;
1185
1186 static const char *hexdigit = "0123456789ABCDEF";
1187
1188 repr = PyString_FromStringAndSize(NULL, 6 * size);
1189 if (repr == NULL)
1190 return NULL;
1191 if (size == 0)
1192 return repr;
1193
1194 p = q = PyString_AS_STRING(repr);
1195 while (size-- > 0) {
1196 Py_UNICODE ch = *s++;
1197 /* Map 16-bit characters to '\uxxxx' */
1198 if (ch >= 256 || ch == '\\' || ch == '\n') {
1199 *p++ = '\\';
1200 *p++ = 'u';
1201 *p++ = hexdigit[(ch >> 12) & 0xf];
1202 *p++ = hexdigit[(ch >> 8) & 0xf];
1203 *p++ = hexdigit[(ch >> 4) & 0xf];
1204 *p++ = hexdigit[ch & 15];
1205 }
1206 /* Copy everything else as-is */
1207 else
1208 *p++ = (char) ch;
1209 }
1210 *p = '\0';
1211 if (_PyString_Resize(&repr, p - q))
1212 goto onError;
1213
1214 return repr;
1215
1216 onError:
1217 Py_DECREF(repr);
1218 return NULL;
1219}
1220
1221
Guido van Rossum60456fd1997-04-09 17:36:32 +00001222static int
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001223save_unicode(Picklerobject *self, PyObject *args, int doput) {
1224 int size, len;
1225 PyObject *repr=0;
1226
1227 if (!PyUnicode_Check(args))
1228 return -1;
1229
1230 if (!self->bin) {
1231 char *repr_str;
1232 static char string = UNICODE;
1233
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001234 UNLESS(repr = modified_EncodeRawUnicodeEscape(
1235 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args)))
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001236 return -1;
1237
1238 if ((len = PyString_Size(repr)) < 0)
1239 goto err;
1240 repr_str = PyString_AS_STRING((PyStringObject *)repr);
1241
1242 if ((*self->write_func)(self, &string, 1) < 0)
1243 goto err;
1244
1245 if ((*self->write_func)(self, repr_str, len) < 0)
1246 goto err;
1247
1248 if ((*self->write_func)(self, "\n", 1) < 0)
1249 goto err;
1250
1251 Py_XDECREF(repr);
1252 }
1253 else {
1254 int i;
1255 char c_str[5];
1256
1257 UNLESS (repr = PyUnicode_AsUTF8String(args))
1258 return -1;
1259
1260 if ((size = PyString_Size(repr)) < 0)
1261 goto err;
1262
1263 c_str[0] = BINUNICODE;
1264 for (i = 1; i < 5; i++)
1265 c_str[i] = (int)(size >> ((i - 1) * 8));
1266 len = 5;
1267
1268 if ((*self->write_func)(self, c_str, len) < 0)
1269 goto err;
1270
1271 if (size > 128 && Pdata_Check(self->file)) {
1272 if (write_other(self, NULL, 0) < 0)
1273 goto err;
1274 PDATA_APPEND(self->file, repr, -1);
1275 }
1276 else {
1277 if ((*self->write_func)(self, PyString_AS_STRING(repr), size) < 0)
1278 goto err;
1279 }
1280
1281 Py_DECREF(repr);
1282 }
1283
1284 if (doput)
1285 if (put(self, args) < 0)
1286 return -1;
1287
1288 return 0;
1289
1290err:
1291 Py_XDECREF(repr);
1292 return -1;
1293}
1294
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) {
1626 PyObject *global_name = 0, *module = 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 Rossum60456fd1997-04-09 17:36:32 +00001651 if ((*self->write_func)(self, &global, 1) < 0)
1652 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001653
Guido van Rossum60456fd1997-04-09 17:36:32 +00001654 if ((*self->write_func)(self, module_str, module_size) < 0)
1655 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001656
Guido van Rossum60456fd1997-04-09 17:36:32 +00001657 if ((*self->write_func)(self, "\n", 1) < 0)
1658 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001659
Guido van Rossum60456fd1997-04-09 17:36:32 +00001660 if ((*self->write_func)(self, name_str, name_size) < 0)
1661 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001662
Guido van Rossum60456fd1997-04-09 17:36:32 +00001663 if ((*self->write_func)(self, "\n", 1) < 0)
1664 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001665
Guido van Rossum60456fd1997-04-09 17:36:32 +00001666 if (put(self, args) < 0)
1667 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001668
Guido van Rossum60456fd1997-04-09 17:36:32 +00001669 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001670
Guido van Rossum60456fd1997-04-09 17:36:32 +00001671finally:
1672 Py_XDECREF(module);
1673 Py_XDECREF(global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001674
Guido van Rossum60456fd1997-04-09 17:36:32 +00001675 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001676}
1677
Guido van Rossum60456fd1997-04-09 17:36:32 +00001678static int
1679save_pers(Picklerobject *self, PyObject *args, PyObject *f) {
1680 PyObject *pid = 0;
1681 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001682
Guido van Rossum60456fd1997-04-09 17:36:32 +00001683 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001684
Guido van Rossum053b8df1998-11-25 16:18:00 +00001685 Py_INCREF(args);
1686 ARG_TUP(self, args);
1687 if (self->arg) {
1688 pid = PyObject_CallObject(f, self->arg);
1689 FREE_ARG_TUP(self);
1690 }
1691 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001692
Guido van Rossum60456fd1997-04-09 17:36:32 +00001693 if (pid != Py_None) {
1694 if (!self->bin) {
1695 if (!PyString_Check(pid)) {
Tim Peters84e87f32001-03-17 04:50:51 +00001696 PyErr_SetString(PicklingError,
Guido van Rossum60456fd1997-04-09 17:36:32 +00001697 "persistent id must be string");
1698 goto finally;
1699 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001700
Guido van Rossum60456fd1997-04-09 17:36:32 +00001701 if ((*self->write_func)(self, &persid, 1) < 0)
1702 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001703
Guido van Rossum60456fd1997-04-09 17:36:32 +00001704 if ((size = PyString_Size(pid)) < 0)
1705 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001706
Tim Peters84e87f32001-03-17 04:50:51 +00001707 if ((*self->write_func)(self,
Guido van Rossum60456fd1997-04-09 17:36:32 +00001708 PyString_AS_STRING((PyStringObject *)pid), size) < 0)
1709 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001710
Guido van Rossum60456fd1997-04-09 17:36:32 +00001711 if ((*self->write_func)(self, "\n", 1) < 0)
1712 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001713
Guido van Rossum60456fd1997-04-09 17:36:32 +00001714 res = 1;
1715 goto finally;
1716 }
1717 else if (save(self, pid, 1) >= 0) {
1718 if ((*self->write_func)(self, &binpersid, 1) < 0)
1719 res = -1;
1720 else
1721 res = 1;
1722 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001723
Tim Peters84e87f32001-03-17 04:50:51 +00001724 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001725 }
1726
Guido van Rossum60456fd1997-04-09 17:36:32 +00001727 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001728
Guido van Rossum60456fd1997-04-09 17:36:32 +00001729finally:
1730 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001731
Guido van Rossum60456fd1997-04-09 17:36:32 +00001732 return res;
1733}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001734
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001735
Tim Peters84e87f32001-03-17 04:50:51 +00001736static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00001737save_reduce(Picklerobject *self, PyObject *callable,
1738 PyObject *tup, PyObject *state, PyObject *ob) {
1739 static char reduce = REDUCE, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001740
Guido van Rossum60456fd1997-04-09 17:36:32 +00001741 if (save(self, callable, 0) < 0)
1742 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001743
Guido van Rossum60456fd1997-04-09 17:36:32 +00001744 if (save(self, tup, 0) < 0)
1745 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001746
Guido van Rossum60456fd1997-04-09 17:36:32 +00001747 if ((*self->write_func)(self, &reduce, 1) < 0)
1748 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001749
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001750 if (ob != NULL) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001751 if (state && !PyDict_Check(state)) {
1752 if (put2(self, ob) < 0)
1753 return -1;
1754 }
1755 else {
1756 if (put(self, ob) < 0)
1757 return -1;
1758 }
1759 }
Tim Peters84e87f32001-03-17 04:50:51 +00001760
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001761 if (state) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001762 if (save(self, state, 0) < 0)
1763 return -1;
1764
1765 if ((*self->write_func)(self, &build, 1) < 0)
1766 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001767 }
1768
Guido van Rossum60456fd1997-04-09 17:36:32 +00001769 return 0;
1770}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001771
Guido van Rossum60456fd1997-04-09 17:36:32 +00001772static int
1773save(Picklerobject *self, PyObject *args, int pers_save) {
1774 PyTypeObject *type;
1775 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0, *arg_tup = 0,
Guido van Rossum142eeb81997-08-13 03:14:41 +00001776 *callable = 0, *state = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001777 int res = -1, tmp, size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001778
Guido van Rossum60456fd1997-04-09 17:36:32 +00001779 if (!pers_save && self->pers_func) {
1780 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
1781 res = tmp;
1782 goto finally;
1783 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001784 }
1785
Guido van Rossum60456fd1997-04-09 17:36:32 +00001786 if (args == Py_None) {
1787 res = save_none(self, args);
1788 goto finally;
1789 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001790
Guido van Rossum60456fd1997-04-09 17:36:32 +00001791 type = args->ob_type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001792
Guido van Rossum60456fd1997-04-09 17:36:32 +00001793 switch (type->tp_name[0]) {
1794 case 'i':
1795 if (type == &PyInt_Type) {
1796 res = save_int(self, args);
1797 goto finally;
1798 }
1799 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001800
Guido van Rossum60456fd1997-04-09 17:36:32 +00001801 case 'l':
1802 if (type == &PyLong_Type) {
1803 res = save_long(self, args);
1804 goto finally;
1805 }
1806 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001807
Guido van Rossum60456fd1997-04-09 17:36:32 +00001808 case 'f':
1809 if (type == &PyFloat_Type) {
1810 res = save_float(self, args);
1811 goto finally;
1812 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001813 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001814
Guido van Rossum60456fd1997-04-09 17:36:32 +00001815 case 't':
1816 if (type == &PyTuple_Type && PyTuple_Size(args)==0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001817 if (self->bin) res = save_empty_tuple(self, args);
1818 else res = save_tuple(self, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001819 goto finally;
1820 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001821 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001822
Guido van Rossum60456fd1997-04-09 17:36:32 +00001823 case 's':
Guido van Rossum053b8df1998-11-25 16:18:00 +00001824 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001825 res = save_string(self, args, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001826 goto finally;
1827 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001828
1829 case 'u':
1830 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
1831 res = save_unicode(self, args, 0);
1832 goto finally;
1833 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001834 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001835
Guido van Rossum60456fd1997-04-09 17:36:32 +00001836 if (args->ob_refcnt > 1) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001837 int has_key;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001838
Guido van Rossum534b7c52000-06-28 22:23:56 +00001839 UNLESS (py_ob_id = PyLong_FromVoidPtr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001840 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001841
Guido van Rossum60456fd1997-04-09 17:36:32 +00001842 if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_ob_id)) < 0)
1843 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001844
Guido van Rossum60456fd1997-04-09 17:36:32 +00001845 if (has_key) {
1846 if (get(self, py_ob_id) < 0)
1847 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001848
Guido van Rossum60456fd1997-04-09 17:36:32 +00001849 res = 0;
1850 goto finally;
1851 }
1852 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001853
Guido van Rossum60456fd1997-04-09 17:36:32 +00001854 switch (type->tp_name[0]) {
1855 case 's':
1856 if (type == &PyString_Type) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001857 res = save_string(self, args, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001858 goto finally;
1859 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001860 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001861
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001862 case 'u':
1863 if (type == &PyUnicode_Type) {
1864 res = save_unicode(self, args, 1);
1865 goto finally;
1866 }
1867 break;
1868
Guido van Rossum60456fd1997-04-09 17:36:32 +00001869 case 't':
1870 if (type == &PyTuple_Type) {
1871 res = save_tuple(self, args);
1872 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001873 }
1874 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001875
Guido van Rossum60456fd1997-04-09 17:36:32 +00001876 case 'l':
1877 if (type == &PyList_Type) {
1878 res = save_list(self, args);
1879 goto finally;
1880 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001881 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001882
1883 case 'd':
1884 if (type == &PyDict_Type) {
1885 res = save_dict(self, args);
Tim Peters84e87f32001-03-17 04:50:51 +00001886 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001887 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001888 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001889
1890 case 'i':
1891 if (type == &PyInstance_Type) {
1892 res = save_inst(self, args);
1893 goto finally;
1894 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001895 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001896
1897 case 'c':
1898 if (type == &PyClass_Type) {
1899 res = save_global(self, args, NULL);
1900 goto finally;
1901 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001902 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001903
1904 case 'f':
1905 if (type == &PyFunction_Type) {
1906 res = save_global(self, args, NULL);
1907 goto finally;
1908 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001909 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001910
1911 case 'b':
1912 if (type == &PyCFunction_Type) {
1913 res = save_global(self, args, NULL);
1914 goto finally;
1915 }
1916 }
1917
1918 if (!pers_save && self->inst_pers_func) {
1919 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
1920 res = tmp;
1921 goto finally;
1922 }
1923 }
1924
Guido van Rossum142eeb81997-08-13 03:14:41 +00001925 if ((__reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001926 Py_INCREF(__reduce__);
1927
Guido van Rossum60456fd1997-04-09 17:36:32 +00001928 Py_INCREF(args);
Guido van Rossum053b8df1998-11-25 16:18:00 +00001929 ARG_TUP(self, args);
1930 if (self->arg) {
1931 t = PyObject_CallObject(__reduce__, self->arg);
1932 FREE_ARG_TUP(self);
1933 }
1934 if (! t) goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001935 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001936 else {
1937 PyErr_Clear();
1938
Guido van Rossum142eeb81997-08-13 03:14:41 +00001939 if ((__reduce__ = PyObject_GetAttr(args, __reduce___str))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001940 UNLESS (t = PyObject_CallObject(__reduce__, empty_tuple))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001941 goto finally;
1942 }
1943 else {
1944 PyErr_Clear();
1945 }
1946 }
1947
1948 if (t) {
1949 if (PyString_Check(t)) {
1950 res = save_global(self, args, t);
1951 goto finally;
1952 }
Tim Peters84e87f32001-03-17 04:50:51 +00001953
Guido van Rossum60456fd1997-04-09 17:36:32 +00001954 if (!PyTuple_Check(t)) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001955 cPickle_ErrFormat(PicklingError, "Value returned by %s must "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001956 "be a tuple", "O", __reduce__);
1957 goto finally;
1958 }
1959
1960 size = PyTuple_Size(t);
Tim Peters84e87f32001-03-17 04:50:51 +00001961
Guido van Rossum60456fd1997-04-09 17:36:32 +00001962 if ((size != 3) && (size != 2)) {
Tim Peters84e87f32001-03-17 04:50:51 +00001963 cPickle_ErrFormat(PicklingError, "tuple returned by %s must "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001964 "contain only two or three elements", "O", __reduce__);
1965 goto finally;
1966 }
Tim Peters84e87f32001-03-17 04:50:51 +00001967
Guido van Rossum60456fd1997-04-09 17:36:32 +00001968 callable = PyTuple_GET_ITEM(t, 0);
Guido van Rossum142eeb81997-08-13 03:14:41 +00001969
Guido van Rossum60456fd1997-04-09 17:36:32 +00001970 arg_tup = PyTuple_GET_ITEM(t, 1);
1971
1972 if (size > 2) {
1973 state = PyTuple_GET_ITEM(t, 2);
1974 }
1975
Guido van Rossum053b8df1998-11-25 16:18:00 +00001976 UNLESS (PyTuple_Check(arg_tup) || arg_tup==Py_None) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001977 cPickle_ErrFormat(PicklingError, "Second element of tuple "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001978 "returned by %s must be a tuple", "O", __reduce__);
1979 goto finally;
1980 }
1981
1982 res = save_reduce(self, callable, arg_tup, state, args);
1983 goto finally;
1984 }
1985
Guido van Rossumc03158b1999-06-09 15:23:31 +00001986 PyErr_SetObject(UnpickleableError, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001987
1988finally:
1989 Py_XDECREF(py_ob_id);
1990 Py_XDECREF(__reduce__);
1991 Py_XDECREF(t);
Tim Peters84e87f32001-03-17 04:50:51 +00001992
Guido van Rossum60456fd1997-04-09 17:36:32 +00001993 return res;
1994}
1995
1996
1997static int
1998dump(Picklerobject *self, PyObject *args) {
1999 static char stop = STOP;
2000
2001 if (save(self, args, 0) < 0)
2002 return -1;
2003
2004 if ((*self->write_func)(self, &stop, 1) < 0)
2005 return -1;
2006
2007 if ((*self->write_func)(self, NULL, 0) < 0)
2008 return -1;
2009
2010 return 0;
2011}
2012
2013static PyObject *
Guido van Rossum053b8df1998-11-25 16:18:00 +00002014Pickle_clear_memo(Picklerobject *self, PyObject *args) {
Guido van Rossum43713e52000-02-29 13:59:29 +00002015 if (args && ! PyArg_ParseTuple(args,":clear_memo")) return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002016 if (self->memo) PyDict_Clear(self->memo);
2017 Py_INCREF(Py_None);
2018 return Py_None;
2019}
2020
2021static PyObject *
2022Pickle_getvalue(Picklerobject *self, PyObject *args) {
2023 int l, i, rsize, ssize, clear=1, lm;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00002024 long ik;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002025 PyObject *k, *r;
2026 char *s, *p, *have_get;
2027 Pdata *data;
2028
Guido van Rossum43713e52000-02-29 13:59:29 +00002029 if (args && ! PyArg_ParseTuple(args,"|i:getvalue",&clear)) return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002030
2031 /* Check to make sure we are based on a list */
2032 if (! Pdata_Check(self->file)) {
2033 PyErr_SetString(PicklingError,
2034 "Attempt to getvalue a non-list-based pickler");
2035 return NULL;
2036 }
2037
2038 /* flush write buffer */
2039 if (write_other(self, NULL, 0) < 0) return NULL;
2040
2041 data=(Pdata*)self->file;
2042 l=data->length;
2043
2044 /* set up an array to hold get/put status */
2045 if ((lm=PyDict_Size(self->memo)) < 0) return NULL;
2046 lm++;
Tim Peters84e87f32001-03-17 04:50:51 +00002047 if (! (have_get=malloc((lm)*sizeof(char)))) return PyErr_NoMemory();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002048 memset(have_get,0,lm);
2049
2050 /* Scan for gets. */
2051 for (rsize=0, i=l; --i >= 0; ) {
2052 k=data->data[i];
Tim Peters84e87f32001-03-17 04:50:51 +00002053
Guido van Rossum053b8df1998-11-25 16:18:00 +00002054 if (PyString_Check(k)) {
2055 rsize += PyString_GET_SIZE(k);
2056 }
2057
2058 else if (PyInt_Check(k)) { /* put */
2059 ik=PyInt_AS_LONG((PyIntObject*)k);
2060 if (ik >= lm || ik==0) {
2061 PyErr_SetString(PicklingError,
2062 "Invalid get data");
2063 return NULL;
Tim Peters84e87f32001-03-17 04:50:51 +00002064 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002065 if (have_get[ik]) { /* with matching get */
2066 if (ik < 256) rsize += 2;
2067 else rsize+=5;
2068 }
2069 }
2070
2071 else if (! (PyTuple_Check(k) &&
2072 PyTuple_GET_SIZE(k) == 2 &&
2073 PyInt_Check((k=PyTuple_GET_ITEM(k,0))))
2074 ) {
2075 PyErr_SetString(PicklingError,
2076 "Unexpected data in internal list");
2077 return NULL;
2078 }
2079
2080 else { /* put */
2081 ik=PyInt_AS_LONG((PyIntObject*)k);
2082 if (ik >= lm || ik==0) {
2083 PyErr_SetString(PicklingError,
2084 "Invalid get data");
2085 return NULL;
Tim Peters84e87f32001-03-17 04:50:51 +00002086 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002087 have_get[ik]=1;
2088 if (ik < 256) rsize += 2;
2089 else rsize+=5;
2090 }
2091
2092 }
2093
2094 /* Now generate the result */
2095 UNLESS (r=PyString_FromStringAndSize(NULL,rsize)) goto err;
2096 s=PyString_AS_STRING((PyStringObject*)r);
2097
2098 for (i=0; i<l; i++) {
2099 k=data->data[i];
2100
2101 if (PyString_Check(k)) {
2102 ssize=PyString_GET_SIZE(k);
2103 if (ssize) {
2104 p=PyString_AS_STRING((PyStringObject*)k);
2105 while (--ssize >= 0) *s++=*p++;
2106 }
2107 }
2108
2109 else if (PyTuple_Check(k)) { /* get */
2110 ik=PyInt_AS_LONG((PyIntObject*)PyTuple_GET_ITEM(k,0));
2111 if (ik < 256) {
2112 *s++ = BINGET;
2113 *s++ = (int)(ik & 0xff);
2114 }
2115 else {
2116 *s++ = LONG_BINGET;
2117 *s++ = (int)(ik & 0xff);
2118 *s++ = (int)((ik >> 8) & 0xff);
2119 *s++ = (int)((ik >> 16) & 0xff);
2120 *s++ = (int)((ik >> 24) & 0xff);
2121 }
2122 }
2123
2124 else { /* put */
2125 ik=PyInt_AS_LONG((PyIntObject*)k);
2126
2127 if (have_get[ik]) { /* with matching get */
2128 if (ik < 256) {
2129 *s++ = BINPUT;
2130 *s++ = (int)(ik & 0xff);
2131 }
2132 else {
2133 *s++ = LONG_BINPUT;
2134 *s++ = (int)(ik & 0xff);
2135 *s++ = (int)((ik >> 8) & 0xff);
2136 *s++ = (int)((ik >> 16) & 0xff);
2137 *s++ = (int)((ik >> 24) & 0xff);
2138 }
2139 }
2140 }
2141
2142 }
2143
2144 if (clear) {
2145 PyDict_Clear(self->memo);
2146 Pdata_clear(data,0);
2147 }
Tim Peters84e87f32001-03-17 04:50:51 +00002148
Guido van Rossum053b8df1998-11-25 16:18:00 +00002149 free(have_get);
2150 return r;
2151err:
2152 free(have_get);
2153 return NULL;
2154}
2155
2156static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002157Pickler_dump(Picklerobject *self, PyObject *args) {
2158 PyObject *ob;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002159 int get=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002160
Guido van Rossum43713e52000-02-29 13:59:29 +00002161 UNLESS (PyArg_ParseTuple(args, "O|i:dump", &ob, &get))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002162 return NULL;
2163
2164 if (dump(self, ob) < 0)
2165 return NULL;
2166
Guido van Rossum053b8df1998-11-25 16:18:00 +00002167 if (get) return Pickle_getvalue(self, NULL);
2168
2169 Py_INCREF(self);
2170 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002171}
2172
2173
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002174static struct PyMethodDef Pickler_methods[] = {
Guido van Rossum142eeb81997-08-13 03:14:41 +00002175 {"dump", (PyCFunction)Pickler_dump, 1,
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002176 "dump(object) --"
2177 "Write an object in pickle format to the object's pickle stream\n"
2178 },
Guido van Rossum142eeb81997-08-13 03:14:41 +00002179 {"clear_memo", (PyCFunction)Pickle_clear_memo, 1,
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002180 "clear_memo() -- Clear the picklers memo"},
Guido van Rossum053b8df1998-11-25 16:18:00 +00002181 {"getvalue", (PyCFunction)Pickle_getvalue, 1,
2182 "getvalue() -- Finish picking a list-based pickle"},
Guido van Rossum60456fd1997-04-09 17:36:32 +00002183 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002184};
2185
2186
2187static Picklerobject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002188newPicklerobject(PyObject *file, int bin) {
2189 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002190
Guido van Rossumb18618d2000-05-03 23:44:39 +00002191 UNLESS (self = PyObject_New(Picklerobject, &Picklertype))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002192 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002193
2194 self->fp = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002195 self->write = NULL;
2196 self->memo = NULL;
2197 self->arg = NULL;
2198 self->pers_func = NULL;
2199 self->inst_pers_func = NULL;
2200 self->write_buf = NULL;
2201 self->bin = bin;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002202 self->fast = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002203 self->buf_size = 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002204 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002205
Guido van Rossum053b8df1998-11-25 16:18:00 +00002206 if (file)
2207 Py_INCREF(file);
2208 else
Guido van Rossum50f385c1998-12-04 18:48:44 +00002209 file=Pdata_New();
Tim Peters84e87f32001-03-17 04:50:51 +00002210
2211 UNLESS (self->file = file)
Guido van Rossum83addc72000-04-21 20:49:36 +00002212 goto err;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002213
Tim Peters84e87f32001-03-17 04:50:51 +00002214 UNLESS (self->memo = PyDict_New())
Guido van Rossum83addc72000-04-21 20:49:36 +00002215 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002216
Guido van Rossum60456fd1997-04-09 17:36:32 +00002217 if (PyFile_Check(file)) {
2218 self->fp = PyFile_AsFile(file);
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002219 if (self->fp == NULL) {
Guido van Rossum83addc72000-04-21 20:49:36 +00002220 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
2221 goto err;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002222 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002223 self->write_func = write_file;
2224 }
2225 else if (PycStringIO_OutputCheck(file)) {
2226 self->write_func = write_cStringIO;
2227 }
Guido van Rossum142eeb81997-08-13 03:14:41 +00002228 else if (file == Py_None) {
2229 self->write_func = write_none;
2230 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002231 else {
2232 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002233
Guido van Rossum053b8df1998-11-25 16:18:00 +00002234 if (! Pdata_Check(file)) {
2235 UNLESS (self->write = PyObject_GetAttr(file, write_str)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002236 PyErr_Clear();
2237 PyErr_SetString(PyExc_TypeError, "argument must have 'write' "
Guido van Rossum053b8df1998-11-25 16:18:00 +00002238 "attribute");
2239 goto err;
2240 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002241 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002242
Tim Peters84e87f32001-03-17 04:50:51 +00002243 UNLESS (self->write_buf =
2244 (char *)malloc(WRITE_BUF_SIZE * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002245 PyErr_NoMemory();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002246 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002247 }
2248 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002249
Guido van Rossum053b8df1998-11-25 16:18:00 +00002250 if (PyEval_GetRestricted()) {
2251 /* Restricted execution, get private tables */
2252 PyObject *m;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002253
Guido van Rossum053b8df1998-11-25 16:18:00 +00002254 UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
2255 self->dispatch_table=PyObject_GetAttr(m, dispatch_table_str);
2256 Py_DECREF(m);
2257 UNLESS (self->dispatch_table) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002258 }
2259 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002260 self->dispatch_table=dispatch_table;
2261 Py_INCREF(dispatch_table);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002262 }
2263
Guido van Rossum60456fd1997-04-09 17:36:32 +00002264 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002265
2266err:
2267 Py_DECREF((PyObject *)self);
2268 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002269}
2270
2271
2272static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002273get_Pickler(PyObject *self, PyObject *args) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002274 PyObject *file=NULL;
2275 int bin;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002276
Guido van Rossum053b8df1998-11-25 16:18:00 +00002277 bin=1;
Guido van Rossum43713e52000-02-29 13:59:29 +00002278 if (! PyArg_ParseTuple(args, "|i:Pickler", &bin)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002279 PyErr_Clear();
2280 bin=0;
Guido van Rossum43713e52000-02-29 13:59:29 +00002281 if (! PyArg_ParseTuple(args, "O|i:Pickler", &file, &bin))
Guido van Rossum053b8df1998-11-25 16:18:00 +00002282 return NULL;
2283 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002284 return (PyObject *)newPicklerobject(file, bin);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002285}
2286
2287
2288static void
Guido van Rossum60456fd1997-04-09 17:36:32 +00002289Pickler_dealloc(Picklerobject *self) {
2290 Py_XDECREF(self->write);
2291 Py_XDECREF(self->memo);
2292 Py_XDECREF(self->arg);
2293 Py_XDECREF(self->file);
2294 Py_XDECREF(self->pers_func);
2295 Py_XDECREF(self->inst_pers_func);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002296 Py_XDECREF(self->dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002297
Tim Peters84e87f32001-03-17 04:50:51 +00002298 if (self->write_buf) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002299 free(self->write_buf);
2300 }
2301
Guido van Rossumb18618d2000-05-03 23:44:39 +00002302 PyObject_Del(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002303}
2304
2305
2306static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002307Pickler_getattr(Picklerobject *self, char *name) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002308
2309 switch (*name) {
2310 case 'p':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002311 if (strcmp(name, "persistent_id") == 0) {
2312 if (!self->pers_func) {
2313 PyErr_SetString(PyExc_AttributeError, name);
2314 return NULL;
2315 }
2316
2317 Py_INCREF(self->pers_func);
2318 return self->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002319 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002320 break;
2321 case 'm':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002322 if (strcmp(name, "memo") == 0) {
2323 if (!self->memo) {
2324 PyErr_SetString(PyExc_AttributeError, name);
2325 return NULL;
2326 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002327
Guido van Rossum60456fd1997-04-09 17:36:32 +00002328 Py_INCREF(self->memo);
2329 return self->memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002330 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002331 break;
2332 case 'P':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002333 if (strcmp(name, "PicklingError") == 0) {
2334 Py_INCREF(PicklingError);
2335 return PicklingError;
2336 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002337 break;
2338 case 'b':
2339 if (strcmp(name, "binary")==0)
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002340 return PyInt_FromLong(self->bin);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002341 break;
2342 case 'f':
2343 if (strcmp(name, "fast")==0)
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002344 return PyInt_FromLong(self->fast);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002345 break;
2346 case 'g':
2347 if (strcmp(name, "getvalue")==0 && ! Pdata_Check(self->file)) {
2348 PyErr_SetString(PyExc_AttributeError, name);
2349 return NULL;
2350 }
2351 break;
2352 }
2353 return Py_FindMethod(Pickler_methods, (PyObject *)self, name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002354}
2355
2356
Tim Peters84e87f32001-03-17 04:50:51 +00002357int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002358Pickler_setattr(Picklerobject *self, char *name, PyObject *value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002359
Guido van Rossum053b8df1998-11-25 16:18:00 +00002360 if (! value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002361 PyErr_SetString(PyExc_TypeError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00002362 "attribute deletion is not supported");
2363 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002364 }
Tim Peters84e87f32001-03-17 04:50:51 +00002365
Guido van Rossum60456fd1997-04-09 17:36:32 +00002366 if (strcmp(name, "persistent_id") == 0) {
2367 Py_XDECREF(self->pers_func);
2368 self->pers_func = value;
2369 Py_INCREF(value);
2370 return 0;
2371 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002372
Guido van Rossum60456fd1997-04-09 17:36:32 +00002373 if (strcmp(name, "inst_persistent_id") == 0) {
2374 Py_XDECREF(self->inst_pers_func);
2375 self->inst_pers_func = value;
2376 Py_INCREF(value);
2377 return 0;
2378 }
2379
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002380 if (strcmp(name, "memo") == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002381 if (! PyDict_Check(value)) {
2382 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
2383 return -1;
2384 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002385 Py_XDECREF(self->memo);
2386 self->memo = value;
2387 Py_INCREF(value);
2388 return 0;
2389 }
2390
Guido van Rossum053b8df1998-11-25 16:18:00 +00002391 if (strcmp(name, "binary")==0) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002392 self->bin=PyObject_IsTrue(value);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002393 return 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002394 }
2395
Guido van Rossum053b8df1998-11-25 16:18:00 +00002396 if (strcmp(name, "fast")==0) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002397 self->fast=PyObject_IsTrue(value);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002398 return 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002399 }
2400
Guido van Rossum60456fd1997-04-09 17:36:32 +00002401 PyErr_SetString(PyExc_AttributeError, name);
2402 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002403}
2404
2405
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002406static char Picklertype__doc__[] =
2407"Objects that know how to pickle objects\n"
2408;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002409
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002410static PyTypeObject Picklertype = {
2411 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00002412 0, /*ob_size*/
2413 "Pickler", /*tp_name*/
2414 sizeof(Picklerobject), /*tp_basicsize*/
2415 0, /*tp_itemsize*/
2416 /* methods */
2417 (destructor)Pickler_dealloc, /*tp_dealloc*/
2418 (printfunc)0, /*tp_print*/
2419 (getattrfunc)Pickler_getattr, /*tp_getattr*/
2420 (setattrfunc)Pickler_setattr, /*tp_setattr*/
2421 (cmpfunc)0, /*tp_compare*/
2422 (reprfunc)0, /*tp_repr*/
2423 0, /*tp_as_number*/
2424 0, /*tp_as_sequence*/
2425 0, /*tp_as_mapping*/
2426 (hashfunc)0, /*tp_hash*/
2427 (ternaryfunc)0, /*tp_call*/
2428 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002429
Guido van Rossum60456fd1997-04-09 17:36:32 +00002430 /* Space for future expansion */
2431 0L,0L,0L,0L,
2432 Picklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002433};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002434
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002435static PyObject *
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002436find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc) {
Guido van Rossume2d81cd1998-08-08 19:40:10 +00002437 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002438
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002439 if (fc) {
2440 if (fc==Py_None) {
Tim Peters84e87f32001-03-17 04:50:51 +00002441 PyErr_SetString(UnpicklingError,
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002442 "Global and instance pickles are not supported.");
2443 return NULL;
2444 }
2445 return PyObject_CallFunction(fc, "OO", py_module_name, py_global_name);
2446 }
2447
Jeremy Hyltond1055231998-08-11 19:52:51 +00002448 module = PySys_GetObject("modules");
2449 if (module == NULL)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002450 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00002451
2452 module = PyDict_GetItem(module, py_module_name);
2453 if (module == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002454 module = PyImport_Import(py_module_name);
2455 if (!module)
2456 return NULL;
2457 global = PyObject_GetAttr(module, py_global_name);
2458 Py_DECREF(module);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002459 }
Jeremy Hyltond1055231998-08-11 19:52:51 +00002460 else
Guido van Rossum053b8df1998-11-25 16:18:00 +00002461 global = PyObject_GetAttr(module, py_global_name);
Jeremy Hyltond1055231998-08-11 19:52:51 +00002462 if (global == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002463 char buf[256 + 37];
2464 sprintf(buf, "Failed to import class %.128s from module %.128s",
2465 PyString_AS_STRING((PyStringObject*)py_global_name),
Tim Peters84e87f32001-03-17 04:50:51 +00002466 PyString_AS_STRING((PyStringObject*)py_module_name));
Guido van Rossum053b8df1998-11-25 16:18:00 +00002467 PyErr_SetString(PyExc_SystemError, buf);
2468 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00002469 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002470 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002471}
2472
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002473static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002474marker(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002475 if (self->num_marks < 1) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002476 PyErr_SetString(UnpicklingError, "could not find MARK");
2477 return -1;
2478 }
2479
2480 return self->marks[--self->num_marks];
2481}
2482
Tim Peters84e87f32001-03-17 04:50:51 +00002483
Guido van Rossum60456fd1997-04-09 17:36:32 +00002484static int
2485load_none(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002486 PDATA_APPEND(self->stack, Py_None, -1);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002487 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002488}
2489
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002490static int
Thomas Wouters58d05102000-07-24 14:43:35 +00002491bad_readline(void) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002492 PyErr_SetString(UnpicklingError, "pickle data was truncated");
2493 return -1;
2494}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002495
2496static int
2497load_int(Unpicklerobject *self) {
2498 PyObject *py_int = 0;
2499 char *endptr, *s;
2500 int len, res = -1;
2501 long l;
2502
2503 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002504 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002505 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002506
2507 errno = 0;
2508 l = strtol(s, &endptr, 0);
2509
2510 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
2511 /* Hm, maybe we've got something long. Let's try reading
Guido van Rossum053b8df1998-11-25 16:18:00 +00002512 it as a Python long object. */
Guido van Rossum60456fd1997-04-09 17:36:32 +00002513 errno=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002514 UNLESS (py_int=PyLong_FromString(s,&endptr,0)) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002515
Guido van Rossum053b8df1998-11-25 16:18:00 +00002516 if ((*endptr != '\n') || (endptr[1] != '\0')) {
2517 PyErr_SetString(PyExc_ValueError,
2518 "could not convert string to int");
2519 goto finally;
2520 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002521 }
2522 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002523 UNLESS (py_int = PyInt_FromLong(l)) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002524 }
2525
Guido van Rossum053b8df1998-11-25 16:18:00 +00002526 free(s);
2527 PDATA_PUSH(self->stack, py_int, -1);
2528 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002529
2530finally:
2531 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002532
2533 return res;
2534}
2535
2536
Tim Peters84e87f32001-03-17 04:50:51 +00002537static long
Guido van Rossum60456fd1997-04-09 17:36:32 +00002538calc_binint(char *s, int x) {
2539 unsigned char c;
2540 int i;
2541 long l;
2542
2543 for (i = 0, l = 0L; i < x; i++) {
2544 c = (unsigned char)s[i];
2545 l |= (long)c << (i * 8);
2546 }
Tim Petersbfa18f72001-04-10 01:54:42 +00002547#if SIZEOF_LONG > 4
2548 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
2549 * is signed, so on a box with longs bigger than 4 bytes we need
2550 * to extend a BININT's sign bit to the full width.
2551 */
2552 if (x == 4 && l & (1L << 31))
2553 l |= (~0L) << 32;
2554#endif
Guido van Rossum60456fd1997-04-09 17:36:32 +00002555 return l;
2556}
2557
2558
2559static int
2560load_binintx(Unpicklerobject *self, char *s, int x) {
2561 PyObject *py_int = 0;
2562 long l;
2563
2564 l = calc_binint(s, x);
2565
Guido van Rossum053b8df1998-11-25 16:18:00 +00002566 UNLESS (py_int = PyInt_FromLong(l))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002567 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002568
Guido van Rossum053b8df1998-11-25 16:18:00 +00002569 PDATA_PUSH(self->stack, py_int, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002570 return 0;
2571}
2572
2573
2574static int
2575load_binint(Unpicklerobject *self) {
2576 char *s;
2577
2578 if ((*self->read_func)(self, &s, 4) < 0)
2579 return -1;
2580
2581 return load_binintx(self, s, 4);
2582}
2583
2584
2585static int
2586load_binint1(Unpicklerobject *self) {
2587 char *s;
2588
2589 if ((*self->read_func)(self, &s, 1) < 0)
2590 return -1;
2591
2592 return load_binintx(self, s, 1);
2593}
2594
2595
2596static int
2597load_binint2(Unpicklerobject *self) {
2598 char *s;
2599
2600 if ((*self->read_func)(self, &s, 2) < 0)
2601 return -1;
2602
2603 return load_binintx(self, s, 2);
2604}
Tim Peters84e87f32001-03-17 04:50:51 +00002605
Guido van Rossum60456fd1997-04-09 17:36:32 +00002606static int
2607load_long(Unpicklerobject *self) {
2608 PyObject *l = 0;
2609 char *end, *s;
2610 int len, res = -1;
2611
Guido van Rossum60456fd1997-04-09 17:36:32 +00002612 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002613 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002614 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002615
Guido van Rossum053b8df1998-11-25 16:18:00 +00002616 UNLESS (l = PyLong_FromString(s, &end, 0))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002617 goto finally;
2618
Guido van Rossum053b8df1998-11-25 16:18:00 +00002619 free(s);
2620 PDATA_PUSH(self->stack, l, -1);
2621 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002622
2623finally:
2624 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002625
2626 return res;
2627}
2628
Tim Peters84e87f32001-03-17 04:50:51 +00002629
Guido van Rossum60456fd1997-04-09 17:36:32 +00002630static int
2631load_float(Unpicklerobject *self) {
2632 PyObject *py_float = 0;
2633 char *endptr, *s;
2634 int len, res = -1;
2635 double d;
2636
2637 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002638 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002639 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002640
2641 errno = 0;
2642 d = strtod(s, &endptr);
2643
2644 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
Tim Peters84e87f32001-03-17 04:50:51 +00002645 PyErr_SetString(PyExc_ValueError,
Guido van Rossum60456fd1997-04-09 17:36:32 +00002646 "could not convert string to float");
2647 goto finally;
2648 }
2649
Guido van Rossum053b8df1998-11-25 16:18:00 +00002650 UNLESS (py_float = PyFloat_FromDouble(d))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002651 goto finally;
2652
Guido van Rossum053b8df1998-11-25 16:18:00 +00002653 free(s);
2654 PDATA_PUSH(self->stack, py_float, -1);
2655 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002656
2657finally:
2658 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002659
2660 return res;
2661}
2662
Guido van Rossum60456fd1997-04-09 17:36:32 +00002663static int
2664load_binfloat(Unpicklerobject *self) {
2665 PyObject *py_float = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002666 int s, e;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002667 long fhi, flo;
2668 double x;
2669 char *p;
2670
2671 if ((*self->read_func)(self, &p, 8) < 0)
2672 return -1;
2673
2674 /* First byte */
2675 s = (*p>>7) & 1;
2676 e = (*p & 0x7F) << 4;
2677 p++;
2678
2679 /* Second byte */
2680 e |= (*p>>4) & 0xF;
2681 fhi = (*p & 0xF) << 24;
2682 p++;
2683
2684 /* Third byte */
2685 fhi |= (*p & 0xFF) << 16;
2686 p++;
2687
2688 /* Fourth byte */
2689 fhi |= (*p & 0xFF) << 8;
2690 p++;
2691
2692 /* Fifth byte */
2693 fhi |= *p & 0xFF;
2694 p++;
2695
2696 /* Sixth byte */
2697 flo = (*p & 0xFF) << 16;
2698 p++;
2699
2700 /* Seventh byte */
2701 flo |= (*p & 0xFF) << 8;
2702 p++;
2703
2704 /* Eighth byte */
2705 flo |= *p & 0xFF;
2706
2707 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
2708 x /= 268435456.0; /* 2**28 */
2709
2710 /* XXX This sadly ignores Inf/NaN */
2711 if (e == 0)
2712 e = -1022;
2713 else {
2714 x += 1.0;
2715 e -= 1023;
2716 }
2717 x = ldexp(x, e);
2718
2719 if (s)
2720 x = -x;
2721
Guido van Rossum053b8df1998-11-25 16:18:00 +00002722 UNLESS (py_float = PyFloat_FromDouble(x)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002723
Guido van Rossum053b8df1998-11-25 16:18:00 +00002724 PDATA_PUSH(self->stack, py_float, -1);
2725 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002726}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002727
2728static int
2729load_string(Unpicklerobject *self) {
2730 PyObject *str = 0;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002731 int len, res = -1, nslash;
2732 char *s, q, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002733
2734 static PyObject *eval_dict = 0;
2735
2736 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002737 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002738 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002739
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002740 /* Check for unquoted quotes (evil strings) */
2741 q=*s;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002742 if (q != '"' && q != '\'') goto insecure;
2743 for (p=s+1, nslash=0; *p; p++) {
2744 if (*p==q && nslash%2==0) break;
2745 if (*p=='\\') nslash++;
2746 else nslash=0;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002747 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002748 if (*p==q)
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002749 {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002750 for (p++; *p; p++) if (*p > ' ') goto insecure;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002751 }
2752 else goto insecure;
2753 /********************************************/
2754
Guido van Rossum053b8df1998-11-25 16:18:00 +00002755 UNLESS (eval_dict)
2756 UNLESS (eval_dict = Py_BuildValue("{s{}}", "__builtins__"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002757 goto finally;
2758
Guido van Rossum053b8df1998-11-25 16:18:00 +00002759 UNLESS (str = PyRun_String(s, Py_eval_input, eval_dict, eval_dict))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002760 goto finally;
2761
Guido van Rossum053b8df1998-11-25 16:18:00 +00002762 free(s);
2763 PDATA_PUSH(self->stack, str, -1);
2764 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002765
2766finally:
2767 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002768
2769 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00002770
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002771insecure:
2772 free(s);
2773 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
2774 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00002775}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002776
2777
2778static int
2779load_binstring(Unpicklerobject *self) {
2780 PyObject *py_string = 0;
2781 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002782 char *s;
2783
Guido van Rossum053b8df1998-11-25 16:18:00 +00002784 if ((*self->read_func)(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002785
2786 l = calc_binint(s, 4);
2787
2788 if ((*self->read_func)(self, &s, l) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002789 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002790
Guido van Rossum053b8df1998-11-25 16:18:00 +00002791 UNLESS (py_string = PyString_FromStringAndSize(s, l))
2792 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002793
Guido van Rossum053b8df1998-11-25 16:18:00 +00002794 PDATA_PUSH(self->stack, py_string, -1);
2795 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002796}
2797
2798
2799static int
2800load_short_binstring(Unpicklerobject *self) {
2801 PyObject *py_string = 0;
Tim Peters84e87f32001-03-17 04:50:51 +00002802 unsigned char l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002803 char *s;
2804
2805 if ((*self->read_func)(self, &s, 1) < 0)
2806 return -1;
2807
2808 l = (unsigned char)s[0];
2809
Guido van Rossum053b8df1998-11-25 16:18:00 +00002810 if ((*self->read_func)(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002811
Guido van Rossum053b8df1998-11-25 16:18:00 +00002812 UNLESS (py_string = PyString_FromStringAndSize(s, l)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002813
Guido van Rossum053b8df1998-11-25 16:18:00 +00002814 PDATA_PUSH(self->stack, py_string, -1);
2815 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00002816}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002817
2818
2819static int
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002820load_unicode(Unpicklerobject *self) {
2821 PyObject *str = 0;
2822 int len, res = -1;
2823 char *s;
2824
2825 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00002826 if (len < 1) return bad_readline();
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002827
2828 UNLESS (str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL))
2829 goto finally;
2830
2831 PDATA_PUSH(self->stack, str, -1);
2832 return 0;
2833
2834finally:
2835 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00002836}
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002837
2838
2839static int
2840load_binunicode(Unpicklerobject *self) {
2841 PyObject *unicode;
2842 long l;
2843 char *s;
2844
2845 if ((*self->read_func)(self, &s, 4) < 0) return -1;
2846
2847 l = calc_binint(s, 4);
2848
2849 if ((*self->read_func)(self, &s, l) < 0)
2850 return -1;
2851
2852 UNLESS (unicode = PyUnicode_DecodeUTF8(s, l, NULL))
2853 return -1;
2854
2855 PDATA_PUSH(self->stack, unicode, -1);
2856 return 0;
2857}
2858
2859
2860static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002861load_tuple(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002862 PyObject *tup;
2863 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002864
Guido van Rossum053b8df1998-11-25 16:18:00 +00002865 if ((i = marker(self)) < 0) return -1;
2866 UNLESS (tup=Pdata_popTuple(self->stack, i)) return -1;
2867 PDATA_PUSH(self->stack, tup, -1);
2868 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002869}
2870
2871static int
2872load_empty_tuple(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002873 PyObject *tup;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002874
Guido van Rossum053b8df1998-11-25 16:18:00 +00002875 UNLESS (tup=PyTuple_New(0)) return -1;
2876 PDATA_PUSH(self->stack, tup, -1);
2877 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002878}
2879
2880static int
2881load_empty_list(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002882 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002883
Guido van Rossum053b8df1998-11-25 16:18:00 +00002884 UNLESS (list=PyList_New(0)) return -1;
2885 PDATA_PUSH(self->stack, list, -1);
2886 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002887}
2888
2889static int
2890load_empty_dict(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002891 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002892
Guido van Rossum053b8df1998-11-25 16:18:00 +00002893 UNLESS (dict=PyDict_New()) return -1;
2894 PDATA_PUSH(self->stack, dict, -1);
2895 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002896}
2897
2898
2899static int
2900load_list(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002901 PyObject *list = 0;
2902 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002903
Guido van Rossum053b8df1998-11-25 16:18:00 +00002904 if ((i = marker(self)) < 0) return -1;
2905 UNLESS (list=Pdata_popList(self->stack, i)) return -1;
2906 PDATA_PUSH(self->stack, list, -1);
2907 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002908}
2909
2910static int
2911load_dict(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002912 PyObject *dict, *key, *value;
2913 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002914
Guido van Rossum053b8df1998-11-25 16:18:00 +00002915 if ((i = marker(self)) < 0) return -1;
2916 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002917
Guido van Rossum053b8df1998-11-25 16:18:00 +00002918 UNLESS (dict = PyDict_New()) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002919
Guido van Rossum053b8df1998-11-25 16:18:00 +00002920 for (k = i+1; k < j; k += 2) {
2921 key =self->stack->data[k-1];
2922 value=self->stack->data[k ];
2923 if (PyDict_SetItem(dict, key, value) < 0) {
2924 Py_DECREF(dict);
2925 return -1;
2926 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002927 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002928 Pdata_clear(self->stack, i);
2929 PDATA_PUSH(self->stack, dict, -1);
2930 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002931}
2932
2933static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002934Instance_New(PyObject *cls, PyObject *args) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002935 int has_key;
2936 PyObject *safe=0, *r=0;
2937
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002938 if (PyClass_Check(cls)) {
2939 int l;
Tim Peters84e87f32001-03-17 04:50:51 +00002940
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002941 if ((l=PyObject_Size(args)) < 0) goto err;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002942 UNLESS (l) {
2943 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002944
Guido van Rossum053b8df1998-11-25 16:18:00 +00002945 UNLESS (__getinitargs__=PyObject_GetAttr(cls, __getinitargs___str)) {
2946 /* We have a class with no __getinitargs__, so bypass usual
2947 construction */
Fred Drake2c773552001-03-22 17:52:17 +00002948 PyObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002949
Guido van Rossum053b8df1998-11-25 16:18:00 +00002950 PyErr_Clear();
Fred Drake2c773552001-03-22 17:52:17 +00002951 UNLESS (inst=PyInstance_NewRaw(cls, NULL))
Guido van Rossum053b8df1998-11-25 16:18:00 +00002952 goto err;
Fred Drake2c773552001-03-22 17:52:17 +00002953 return inst;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002954 }
2955 Py_DECREF(__getinitargs__);
2956 }
Tim Peters84e87f32001-03-17 04:50:51 +00002957
Guido van Rossum053b8df1998-11-25 16:18:00 +00002958 if ((r=PyInstance_New(cls, args, NULL))) return r;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002959 else goto err;
2960 }
Tim Peters84e87f32001-03-17 04:50:51 +00002961
2962
Guido van Rossum60456fd1997-04-09 17:36:32 +00002963 if ((has_key = cPickle_PyMapping_HasKey(safe_constructors, cls)) < 0)
2964 goto err;
Tim Peters84e87f32001-03-17 04:50:51 +00002965
Guido van Rossum60456fd1997-04-09 17:36:32 +00002966 if (!has_key)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002967 if (!(safe = PyObject_GetAttr(cls, __safe_for_unpickling___str)) ||
Guido van Rossum60456fd1997-04-09 17:36:32 +00002968 !PyObject_IsTrue(safe)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002969 cPickle_ErrFormat(UnpicklingError,
2970 "%s is not safe for unpickling", "O", cls);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002971 Py_XDECREF(safe);
2972 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002973 }
2974
Guido van Rossum053b8df1998-11-25 16:18:00 +00002975 if (args==Py_None) {
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002976 /* Special case, call cls.__basicnew__() */
2977 PyObject *basicnew;
Tim Peters84e87f32001-03-17 04:50:51 +00002978
Guido van Rossum053b8df1998-11-25 16:18:00 +00002979 UNLESS (basicnew=PyObject_GetAttr(cls, __basicnew___str)) return NULL;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002980 r=PyObject_CallObject(basicnew, NULL);
2981 Py_DECREF(basicnew);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002982 if (r) return r;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002983 }
2984
Guido van Rossum053b8df1998-11-25 16:18:00 +00002985 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00002986
Guido van Rossum60456fd1997-04-09 17:36:32 +00002987err:
2988 {
2989 PyObject *tp, *v, *tb;
2990
2991 PyErr_Fetch(&tp, &v, &tb);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002992 if ((r=Py_BuildValue("OOO",v,cls,args))) {
2993 Py_XDECREF(v);
2994 v=r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002995 }
2996 PyErr_Restore(tp,v,tb);
2997 }
2998 return NULL;
2999}
Tim Peters84e87f32001-03-17 04:50:51 +00003000
Guido van Rossum60456fd1997-04-09 17:36:32 +00003001
3002static int
3003load_obj(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003004 PyObject *class, *tup, *obj=0;
3005 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003006
Guido van Rossum053b8df1998-11-25 16:18:00 +00003007 if ((i = marker(self)) < 0) return -1;
3008 UNLESS (tup=Pdata_popTuple(self->stack, i+1)) return -1;
3009 PDATA_POP(self->stack, class);
3010 if (class) {
3011 obj = Instance_New(class, tup);
3012 Py_DECREF(class);
3013 }
3014 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003015
Guido van Rossum053b8df1998-11-25 16:18:00 +00003016 if (! obj) return -1;
3017 PDATA_PUSH(self->stack, obj, -1);
3018 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003019}
3020
3021
3022static int
3023load_inst(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003024 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003025 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003026 char *s;
3027
Guido van Rossum053b8df1998-11-25 16:18:00 +00003028 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003029
Guido van Rossum053b8df1998-11-25 16:18:00 +00003030 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003031 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00003032 UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003033
Guido van Rossum053b8df1998-11-25 16:18:00 +00003034 if ((len = (*self->readline_func)(self, &s)) >= 0) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003035 if (len < 2) return bad_readline();
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003036 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003037 class = find_class(module_name, class_name, self->find_class);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003038 Py_DECREF(class_name);
3039 }
3040 }
3041 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003042
Guido van Rossum053b8df1998-11-25 16:18:00 +00003043 if (! class) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003044
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003045 if ((tup=Pdata_popTuple(self->stack, i))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003046 obj = Instance_New(class, tup);
3047 Py_DECREF(tup);
3048 }
3049 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003050
Guido van Rossum053b8df1998-11-25 16:18:00 +00003051 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003052
Guido van Rossum053b8df1998-11-25 16:18:00 +00003053 PDATA_PUSH(self->stack, obj, -1);
3054 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003055}
3056
3057
3058static int
3059load_global(Unpicklerobject *self) {
3060 PyObject *class = 0, *module_name = 0, *class_name = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003061 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003062 char *s;
3063
Guido van Rossum053b8df1998-11-25 16:18:00 +00003064 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003065 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00003066 UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003067
Guido van Rossum053b8df1998-11-25 16:18:00 +00003068 if ((len = (*self->readline_func)(self, &s)) >= 0) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003069 if (len < 2) return bad_readline();
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003070 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003071 class = find_class(module_name, class_name, self->find_class);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003072 Py_DECREF(class_name);
3073 }
3074 }
3075 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003076
Guido van Rossum053b8df1998-11-25 16:18:00 +00003077 if (! class) return -1;
3078 PDATA_PUSH(self->stack, class, -1);
3079 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003080}
3081
3082
3083static int
3084load_persid(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003085 PyObject *pid = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003086 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003087 char *s;
3088
3089 if (self->pers_func) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003090 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003091 if (len < 2) return bad_readline();
Tim Peters84e87f32001-03-17 04:50:51 +00003092
Guido van Rossum053b8df1998-11-25 16:18:00 +00003093 UNLESS (pid = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003094
Guido van Rossum053b8df1998-11-25 16:18:00 +00003095 if (PyList_Check(self->pers_func)) {
3096 if (PyList_Append(self->pers_func, pid) < 0) {
3097 Py_DECREF(pid);
3098 return -1;
3099 }
3100 }
3101 else {
3102 ARG_TUP(self, pid);
3103 if (self->arg) {
3104 pid = PyObject_CallObject(self->pers_func, self->arg);
3105 FREE_ARG_TUP(self);
3106 }
3107 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003108
Guido van Rossum053b8df1998-11-25 16:18:00 +00003109 if (! pid) return -1;
3110
3111 PDATA_PUSH(self->stack, pid, -1);
3112 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003113 }
3114 else {
3115 PyErr_SetString(UnpicklingError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00003116 "A load persistent id instruction was encountered,\n"
3117 "but no persistent_load function was specified.");
3118 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003119 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003120}
3121
Guido van Rossum60456fd1997-04-09 17:36:32 +00003122static int
3123load_binpersid(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003124 PyObject *pid = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003125
3126 if (self->pers_func) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003127 PDATA_POP(self->stack, pid);
3128 if (! pid) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003129
Guido van Rossum053b8df1998-11-25 16:18:00 +00003130 if (PyList_Check(self->pers_func)) {
3131 if (PyList_Append(self->pers_func, pid) < 0) {
3132 Py_DECREF(pid);
3133 return -1;
3134 }
3135 }
3136 else {
3137 ARG_TUP(self, pid);
3138 if (self->arg) {
3139 pid = PyObject_CallObject(self->pers_func, self->arg);
3140 FREE_ARG_TUP(self);
3141 }
3142 if (! pid) return -1;
3143 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003144
Guido van Rossum053b8df1998-11-25 16:18:00 +00003145 PDATA_PUSH(self->stack, pid, -1);
3146 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003147 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003148 else {
3149 PyErr_SetString(UnpicklingError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00003150 "A load persistent id instruction was encountered,\n"
3151 "but no persistent_load function was specified.");
3152 return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003153 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003154}
3155
3156
3157static int
3158load_pop(Unpicklerobject *self) {
3159 int len;
3160
Guido van Rossum053b8df1998-11-25 16:18:00 +00003161 UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003162
Tim Peters84e87f32001-03-17 04:50:51 +00003163 /* Note that we split the (pickle.py) stack into two stacks,
Guido van Rossumea2b7152000-05-09 18:14:50 +00003164 an object stack and a mark stack. We have to be clever and
3165 pop the right one. We do this by looking at the top of the
3166 mark stack.
3167 */
3168
Tim Peters84e87f32001-03-17 04:50:51 +00003169 if ((self->num_marks > 0) &&
Guido van Rossum60456fd1997-04-09 17:36:32 +00003170 (self->marks[self->num_marks - 1] == len))
3171 self->num_marks--;
Tim Peters84e87f32001-03-17 04:50:51 +00003172 else {
Guido van Rossumea2b7152000-05-09 18:14:50 +00003173 len--;
3174 Py_DECREF(self->stack->data[len]);
3175 self->stack->length=len;
3176 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003177
3178 return 0;
3179}
3180
3181
3182static int
3183load_pop_mark(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003184 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003185
3186 if ((i = marker(self)) < 0)
3187 return -1;
3188
Guido van Rossum053b8df1998-11-25 16:18:00 +00003189 Pdata_clear(self->stack, i);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003190
3191 return 0;
3192}
3193
3194
3195static int
3196load_dup(Unpicklerobject *self) {
3197 PyObject *last;
3198 int len;
3199
Guido van Rossum053b8df1998-11-25 16:18:00 +00003200 if ((len = self->stack->length) <= 0) return stackUnderflow();
3201 last=self->stack->data[len-1];
3202 Py_INCREF(last);
3203 PDATA_PUSH(self->stack, last, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003204 return 0;
3205}
3206
3207
3208static int
3209load_get(Unpicklerobject *self) {
3210 PyObject *py_str = 0, *value = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003211 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003212 char *s;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003213 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003214
Guido van Rossum053b8df1998-11-25 16:18:00 +00003215 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003216 if (len < 2) return bad_readline();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003217
Guido van Rossum053b8df1998-11-25 16:18:00 +00003218 UNLESS (py_str = PyString_FromStringAndSize(s, len - 1)) return -1;
3219
3220 value = PyDict_GetItem(self->memo, py_str);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003221 if (! value) {
3222 PyErr_SetObject(BadPickleGet, py_str);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003223 rc = -1;
3224 } else {
3225 PDATA_APPEND(self->stack, value, -1);
3226 rc = 0;
3227 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003228
Guido van Rossum2f80d961999-07-13 15:18:58 +00003229 Py_DECREF(py_str);
3230 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003231}
3232
3233
3234static int
3235load_binget(Unpicklerobject *self) {
3236 PyObject *py_key = 0, *value = 0;
3237 unsigned char key;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003238 char *s;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003239 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003240
Guido van Rossum053b8df1998-11-25 16:18:00 +00003241 if ((*self->read_func)(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003242
3243 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00003244 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003245
Guido van Rossum053b8df1998-11-25 16:18:00 +00003246 value = PyDict_GetItem(self->memo, py_key);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003247 if (! value) {
3248 PyErr_SetObject(BadPickleGet, py_key);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003249 rc = -1;
3250 } else {
3251 PDATA_APPEND(self->stack, value, -1);
3252 rc = 0;
3253 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003254
Guido van Rossum2f80d961999-07-13 15:18:58 +00003255 Py_DECREF(py_key);
3256 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003257}
3258
3259
3260static int
3261load_long_binget(Unpicklerobject *self) {
3262 PyObject *py_key = 0, *value = 0;
Thomas Wouters3b6448f2000-07-22 23:56:07 +00003263 unsigned char c;
3264 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003265 long key;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003266 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003267
Guido van Rossum053b8df1998-11-25 16:18:00 +00003268 if ((*self->read_func)(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003269
3270 c = (unsigned char)s[0];
3271 key = (long)c;
3272 c = (unsigned char)s[1];
3273 key |= (long)c << 8;
3274 c = (unsigned char)s[2];
3275 key |= (long)c << 16;
3276 c = (unsigned char)s[3];
3277 key |= (long)c << 24;
3278
Guido van Rossum053b8df1998-11-25 16:18:00 +00003279 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003280
Guido van Rossum053b8df1998-11-25 16:18:00 +00003281 value = PyDict_GetItem(self->memo, py_key);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003282 if (! value) {
3283 PyErr_SetObject(BadPickleGet, py_key);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003284 rc = -1;
3285 } else {
3286 PDATA_APPEND(self->stack, value, -1);
3287 rc = 0;
3288 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003289
Guido van Rossum2f80d961999-07-13 15:18:58 +00003290 Py_DECREF(py_key);
3291 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003292}
3293
3294
3295static int
3296load_put(Unpicklerobject *self) {
3297 PyObject *py_str = 0, *value = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003298 int len, l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003299 char *s;
3300
Guido van Rossum053b8df1998-11-25 16:18:00 +00003301 if ((l = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumd1f66dc1999-02-08 22:38:25 +00003302 if (l < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00003303 UNLESS (len=self->stack->length) return stackUnderflow();
3304 UNLESS (py_str = PyString_FromStringAndSize(s, l - 1)) return -1;
3305 value=self->stack->data[len-1];
3306 l=PyDict_SetItem(self->memo, py_str, value);
3307 Py_DECREF(py_str);
3308 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003309}
3310
3311
3312static int
3313load_binput(Unpicklerobject *self) {
3314 PyObject *py_key = 0, *value = 0;
Thomas Wouters3b6448f2000-07-22 23:56:07 +00003315 unsigned char key;
3316 char *s;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003317 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003318
Guido van Rossum053b8df1998-11-25 16:18:00 +00003319 if ((*self->read_func)(self, &s, 1) < 0) return -1;
3320 UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003321
3322 key = (unsigned char)s[0];
3323
Guido van Rossum053b8df1998-11-25 16:18:00 +00003324 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3325 value=self->stack->data[len-1];
3326 len=PyDict_SetItem(self->memo, py_key, value);
3327 Py_DECREF(py_key);
3328 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003329}
3330
3331
3332static int
3333load_long_binput(Unpicklerobject *self) {
3334 PyObject *py_key = 0, *value = 0;
3335 long key;
Thomas Wouters3b6448f2000-07-22 23:56:07 +00003336 unsigned char c;
3337 char *s;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003338 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003339
Guido van Rossum053b8df1998-11-25 16:18:00 +00003340 if ((*self->read_func)(self, &s, 4) < 0) return -1;
3341 UNLESS (len=self->stack->length) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003342
3343 c = (unsigned char)s[0];
3344 key = (long)c;
3345 c = (unsigned char)s[1];
3346 key |= (long)c << 8;
3347 c = (unsigned char)s[2];
3348 key |= (long)c << 16;
3349 c = (unsigned char)s[3];
3350 key |= (long)c << 24;
3351
Guido van Rossum053b8df1998-11-25 16:18:00 +00003352 UNLESS (py_key = PyInt_FromLong(key)) return -1;
3353 value=self->stack->data[len-1];
3354 len=PyDict_SetItem(self->memo, py_key, value);
3355 Py_DECREF(py_key);
3356 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003357}
3358
3359
Tim Peters84e87f32001-03-17 04:50:51 +00003360static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00003361do_append(Unpicklerobject *self, int x) {
3362 PyObject *value = 0, *list = 0, *append_method = 0;
3363 int len, i;
3364
Guido van Rossum053b8df1998-11-25 16:18:00 +00003365 UNLESS ((len=self->stack->length) >= x && x > 0) return stackUnderflow();
3366 if (len==x) return 0; /* nothing to do */
Guido van Rossum60456fd1997-04-09 17:36:32 +00003367
Guido van Rossum053b8df1998-11-25 16:18:00 +00003368 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003369
3370 if (PyList_Check(list)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003371 PyObject *slice;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003372 int list_len;
Tim Peters84e87f32001-03-17 04:50:51 +00003373
Guido van Rossum053b8df1998-11-25 16:18:00 +00003374 slice=Pdata_popList(self->stack, x);
3375 list_len = PyList_GET_SIZE(list);
3376 i=PyList_SetSlice(list, list_len, list_len, slice);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003377 Py_DECREF(slice);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003378 return i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003379 }
3380 else {
3381
Guido van Rossum053b8df1998-11-25 16:18:00 +00003382 UNLESS (append_method = PyObject_GetAttr(list, append_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00003383 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003384
Guido van Rossum60456fd1997-04-09 17:36:32 +00003385 for (i = x; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003386 PyObject *junk;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003387
Guido van Rossum053b8df1998-11-25 16:18:00 +00003388 value=self->stack->data[i];
3389 junk=0;
3390 ARG_TUP(self, value);
3391 if (self->arg) {
3392 junk = PyObject_CallObject(append_method, self->arg);
3393 FREE_ARG_TUP(self);
3394 }
3395 if (! junk) {
3396 Pdata_clear(self->stack, i+1);
3397 self->stack->length=x;
3398 Py_DECREF(append_method);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003399 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003400 }
3401 Py_DECREF(junk);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003402 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003403 self->stack->length=x;
3404 Py_DECREF(append_method);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003405 }
3406
Guido van Rossum60456fd1997-04-09 17:36:32 +00003407 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003408}
3409
Tim Peters84e87f32001-03-17 04:50:51 +00003410
Guido van Rossum60456fd1997-04-09 17:36:32 +00003411static int
3412load_append(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003413 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003414}
3415
3416
3417static int
3418load_appends(Unpicklerobject *self) {
3419 return do_append(self, marker(self));
3420}
3421
3422
3423static int
3424do_setitems(Unpicklerobject *self, int x) {
3425 PyObject *value = 0, *key = 0, *dict = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003426 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003427
Guido van Rossum053b8df1998-11-25 16:18:00 +00003428 UNLESS ((len=self->stack->length) >= x
3429 && x > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003430
Guido van Rossum053b8df1998-11-25 16:18:00 +00003431 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003432
Guido van Rossum053b8df1998-11-25 16:18:00 +00003433 for (i = x+1; i < len; i += 2) {
3434 key =self->stack->data[i-1];
3435 value=self->stack->data[i ];
3436 if (PyObject_SetItem(dict, key, value) < 0) {
3437 r=-1;
3438 break;
3439 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003440 }
3441
Guido van Rossum053b8df1998-11-25 16:18:00 +00003442 Pdata_clear(self->stack, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003443
Guido van Rossum053b8df1998-11-25 16:18:00 +00003444 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003445}
3446
3447
3448static int
3449load_setitem(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003450 return do_setitems(self, self->stack->length - 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003451}
3452
Guido van Rossum60456fd1997-04-09 17:36:32 +00003453static int
3454load_setitems(Unpicklerobject *self) {
3455 return do_setitems(self, marker(self));
3456}
3457
3458
3459static int
3460load_build(Unpicklerobject *self) {
Tim Peters84e87f32001-03-17 04:50:51 +00003461 PyObject *value = 0, *inst = 0, *instdict = 0, *d_key = 0, *d_value = 0,
Guido van Rossum60456fd1997-04-09 17:36:32 +00003462 *junk = 0, *__setstate__ = 0;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003463 int i, r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003464
Guido van Rossum053b8df1998-11-25 16:18:00 +00003465 if (self->stack->length < 2) return stackUnderflow();
3466 PDATA_POP(self->stack, value);
3467 if (! value) return -1;
3468 inst=self->stack->data[self->stack->length-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003469
Guido van Rossum053b8df1998-11-25 16:18:00 +00003470 if ((__setstate__ = PyObject_GetAttr(inst, __setstate___str))) {
3471 ARG_TUP(self, value);
3472 if (self->arg) {
3473 junk = PyObject_CallObject(__setstate__, self->arg);
3474 FREE_ARG_TUP(self);
3475 }
3476 Py_DECREF(__setstate__);
3477 if (! junk) return -1;
3478 Py_DECREF(junk);
3479 return 0;
3480 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003481
Guido van Rossum053b8df1998-11-25 16:18:00 +00003482 PyErr_Clear();
3483 if ((instdict = PyObject_GetAttr(inst, __dict___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003484 i = 0;
3485 while (PyDict_Next(value, &i, &d_key, &d_value)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003486 if (PyObject_SetItem(instdict, d_key, d_value) < 0) {
3487 r=-1;
3488 break;
3489 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003490 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003491 Py_DECREF(instdict);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003492 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003493 else r=-1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003494
Guido van Rossum053b8df1998-11-25 16:18:00 +00003495 Py_XDECREF(value);
Tim Peters84e87f32001-03-17 04:50:51 +00003496
Guido van Rossum053b8df1998-11-25 16:18:00 +00003497 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003498}
3499
3500
3501static int
3502load_mark(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003503 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003504
Guido van Rossumea2b7152000-05-09 18:14:50 +00003505 /* Note that we split the (pickle.py) stack into two stacks, an
3506 object stack and a mark stack. Here we push a mark onto the
Tim Peters84e87f32001-03-17 04:50:51 +00003507 mark stack.
Guido van Rossumea2b7152000-05-09 18:14:50 +00003508 */
3509
Guido van Rossum053b8df1998-11-25 16:18:00 +00003510 if ((self->num_marks + 1) >= self->marks_size) {
3511 s=self->marks_size+20;
3512 if (s <= self->num_marks) s=self->num_marks + 1;
Guido van Rossum761fcd01999-04-12 22:51:20 +00003513 if (self->marks == NULL)
Guido van Rossumaa8d1671999-01-25 21:43:51 +00003514 self->marks=(int *)malloc(s * sizeof(int));
3515 else
3516 self->marks=(int *)realloc(self->marks, s * sizeof(int));
Guido van Rossum053b8df1998-11-25 16:18:00 +00003517 if (! self->marks) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003518 PyErr_NoMemory();
3519 return -1;
3520 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003521 self->marks_size = s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003522 }
3523
Guido van Rossum053b8df1998-11-25 16:18:00 +00003524 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003525
3526 return 0;
3527}
3528
3529static int
3530load_reduce(Unpicklerobject *self) {
3531 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003532
Guido van Rossum053b8df1998-11-25 16:18:00 +00003533 PDATA_POP(self->stack, arg_tup);
3534 if (! arg_tup) return -1;
3535 PDATA_POP(self->stack, callable);
3536 if (callable) {
3537 ob = Instance_New(callable, arg_tup);
3538 Py_DECREF(callable);
3539 }
3540 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003541
Guido van Rossum053b8df1998-11-25 16:18:00 +00003542 if (! ob) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003543
Guido van Rossum053b8df1998-11-25 16:18:00 +00003544 PDATA_PUSH(self->stack, ob, -1);
3545 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003546}
Tim Peters84e87f32001-03-17 04:50:51 +00003547
Guido van Rossum60456fd1997-04-09 17:36:32 +00003548static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003549load(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003550 PyObject *err = 0, *val = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003551 char *s;
3552
Guido van Rossum60456fd1997-04-09 17:36:32 +00003553 self->num_marks = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003554 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003555
3556 while (1) {
3557 if ((*self->read_func)(self, &s, 1) < 0)
3558 break;
3559
3560 switch (s[0]) {
3561 case NONE:
3562 if (load_none(self) < 0)
3563 break;
3564 continue;
3565
3566 case BININT:
3567 if (load_binint(self) < 0)
3568 break;
3569 continue;
3570
3571 case BININT1:
3572 if (load_binint1(self) < 0)
3573 break;
3574 continue;
3575
3576 case BININT2:
3577 if (load_binint2(self) < 0)
3578 break;
3579 continue;
3580
3581 case INT:
3582 if (load_int(self) < 0)
3583 break;
3584 continue;
3585
3586 case LONG:
3587 if (load_long(self) < 0)
3588 break;
3589 continue;
3590
3591 case FLOAT:
3592 if (load_float(self) < 0)
3593 break;
3594 continue;
3595
Guido van Rossum60456fd1997-04-09 17:36:32 +00003596 case BINFLOAT:
3597 if (load_binfloat(self) < 0)
3598 break;
3599 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003600
3601 case BINSTRING:
3602 if (load_binstring(self) < 0)
3603 break;
3604 continue;
3605
3606 case SHORT_BINSTRING:
3607 if (load_short_binstring(self) < 0)
3608 break;
3609 continue;
3610
3611 case STRING:
3612 if (load_string(self) < 0)
3613 break;
3614 continue;
3615
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003616 case UNICODE:
3617 if (load_unicode(self) < 0)
3618 break;
3619 continue;
3620
3621 case BINUNICODE:
3622 if (load_binunicode(self) < 0)
3623 break;
3624 continue;
3625
Guido van Rossum60456fd1997-04-09 17:36:32 +00003626 case EMPTY_TUPLE:
3627 if (load_empty_tuple(self) < 0)
3628 break;
3629 continue;
3630
3631 case TUPLE:
3632 if (load_tuple(self) < 0)
3633 break;
3634 continue;
3635
3636 case EMPTY_LIST:
3637 if (load_empty_list(self) < 0)
3638 break;
3639 continue;
3640
3641 case LIST:
3642 if (load_list(self) < 0)
3643 break;
3644 continue;
3645
3646 case EMPTY_DICT:
3647 if (load_empty_dict(self) < 0)
3648 break;
3649 continue;
3650
3651 case DICT:
3652 if (load_dict(self) < 0)
3653 break;
3654 continue;
3655
3656 case OBJ:
3657 if (load_obj(self) < 0)
3658 break;
3659 continue;
3660
3661 case INST:
3662 if (load_inst(self) < 0)
3663 break;
3664 continue;
3665
3666 case GLOBAL:
3667 if (load_global(self) < 0)
3668 break;
3669 continue;
3670
3671 case APPEND:
3672 if (load_append(self) < 0)
3673 break;
3674 continue;
3675
3676 case APPENDS:
3677 if (load_appends(self) < 0)
3678 break;
3679 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003680
Guido van Rossum60456fd1997-04-09 17:36:32 +00003681 case BUILD:
3682 if (load_build(self) < 0)
3683 break;
3684 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003685
Guido van Rossum60456fd1997-04-09 17:36:32 +00003686 case DUP:
3687 if (load_dup(self) < 0)
3688 break;
3689 continue;
3690
3691 case BINGET:
3692 if (load_binget(self) < 0)
3693 break;
3694 continue;
3695
3696 case LONG_BINGET:
3697 if (load_long_binget(self) < 0)
3698 break;
3699 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003700
Guido van Rossum60456fd1997-04-09 17:36:32 +00003701 case GET:
3702 if (load_get(self) < 0)
3703 break;
3704 continue;
3705
3706 case MARK:
3707 if (load_mark(self) < 0)
3708 break;
3709 continue;
3710
3711 case BINPUT:
3712 if (load_binput(self) < 0)
3713 break;
3714 continue;
3715
3716 case LONG_BINPUT:
3717 if (load_long_binput(self) < 0)
3718 break;
3719 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003720
Guido van Rossum60456fd1997-04-09 17:36:32 +00003721 case PUT:
3722 if (load_put(self) < 0)
3723 break;
3724 continue;
3725
3726 case POP:
3727 if (load_pop(self) < 0)
3728 break;
3729 continue;
3730
3731 case POP_MARK:
3732 if (load_pop_mark(self) < 0)
3733 break;
3734 continue;
3735
3736 case SETITEM:
3737 if (load_setitem(self) < 0)
3738 break;
3739 continue;
3740
3741 case SETITEMS:
3742 if (load_setitems(self) < 0)
3743 break;
3744 continue;
3745
3746 case STOP:
3747 break;
3748
3749 case PERSID:
3750 if (load_persid(self) < 0)
3751 break;
3752 continue;
3753
3754 case BINPERSID:
3755 if (load_binpersid(self) < 0)
3756 break;
3757 continue;
3758
3759 case REDUCE:
3760 if (load_reduce(self) < 0)
3761 break;
3762 continue;
3763
Tim Peters84e87f32001-03-17 04:50:51 +00003764 default:
3765 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossum60456fd1997-04-09 17:36:32 +00003766 "c", s[0]);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003767 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003768 }
3769
3770 break;
3771 }
3772
Guido van Rossum053b8df1998-11-25 16:18:00 +00003773 if ((err = PyErr_Occurred())) {
3774 if (err == PyExc_EOFError) {
3775 PyErr_SetNone(PyExc_EOFError);
Tim Peters84e87f32001-03-17 04:50:51 +00003776 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003777 return NULL;
3778 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003779
Tim Peters84e87f32001-03-17 04:50:51 +00003780 PDATA_POP(self->stack, val);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003781 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003782}
Tim Peters84e87f32001-03-17 04:50:51 +00003783
Guido van Rossum60456fd1997-04-09 17:36:32 +00003784
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003785/* No-load functions to support noload, which is used to
3786 find persistent references. */
3787
3788static int
3789noload_obj(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003790 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003791
3792 if ((i = marker(self)) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003793 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003794}
3795
3796
3797static int
3798noload_inst(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003799 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003800 char *s;
3801
3802 if ((i = marker(self)) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003803 Pdata_clear(self->stack, i);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003804 if ((*self->readline_func)(self, &s) < 0) return -1;
3805 if ((*self->readline_func)(self, &s) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003806 PDATA_APPEND(self->stack, Py_None,-1);
3807 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003808}
3809
3810static int
3811noload_global(Unpicklerobject *self) {
3812 char *s;
3813
3814 if ((*self->readline_func)(self, &s) < 0) return -1;
3815 if ((*self->readline_func)(self, &s) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003816 PDATA_APPEND(self->stack, Py_None,-1);
3817 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003818}
3819
3820static int
3821noload_reduce(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003822
Guido van Rossum053b8df1998-11-25 16:18:00 +00003823 if (self->stack->length < 2) return stackUnderflow();
3824 Pdata_clear(self->stack, self->stack->length-2);
3825 PDATA_APPEND(self->stack, Py_None,-1);
3826 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003827}
3828
3829static int
3830noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003831
Guido van Rossum053b8df1998-11-25 16:18:00 +00003832 if (self->stack->length < 1) return stackUnderflow();
3833 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00003834 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003835}
3836
3837
3838static PyObject *
3839noload(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003840 PyObject *err = 0, *val = 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003841 char *s;
3842
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003843 self->num_marks = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003844 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003845
3846 while (1) {
3847 if ((*self->read_func)(self, &s, 1) < 0)
3848 break;
3849
3850 switch (s[0]) {
3851 case NONE:
3852 if (load_none(self) < 0)
3853 break;
3854 continue;
3855
3856 case BININT:
3857 if (load_binint(self) < 0)
3858 break;
3859 continue;
3860
3861 case BININT1:
3862 if (load_binint1(self) < 0)
3863 break;
3864 continue;
3865
3866 case BININT2:
3867 if (load_binint2(self) < 0)
3868 break;
3869 continue;
3870
3871 case INT:
3872 if (load_int(self) < 0)
3873 break;
3874 continue;
3875
3876 case LONG:
3877 if (load_long(self) < 0)
3878 break;
3879 continue;
3880
3881 case FLOAT:
3882 if (load_float(self) < 0)
3883 break;
3884 continue;
3885
3886 case BINFLOAT:
3887 if (load_binfloat(self) < 0)
3888 break;
3889 continue;
3890
3891 case BINSTRING:
3892 if (load_binstring(self) < 0)
3893 break;
3894 continue;
3895
3896 case SHORT_BINSTRING:
3897 if (load_short_binstring(self) < 0)
3898 break;
3899 continue;
3900
3901 case STRING:
3902 if (load_string(self) < 0)
3903 break;
3904 continue;
3905
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003906 case UNICODE:
3907 if (load_unicode(self) < 0)
3908 break;
3909 continue;
3910
3911 case BINUNICODE:
3912 if (load_binunicode(self) < 0)
3913 break;
3914 continue;
3915
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003916 case EMPTY_TUPLE:
3917 if (load_empty_tuple(self) < 0)
3918 break;
3919 continue;
3920
3921 case TUPLE:
3922 if (load_tuple(self) < 0)
3923 break;
3924 continue;
3925
3926 case EMPTY_LIST:
3927 if (load_empty_list(self) < 0)
3928 break;
3929 continue;
3930
3931 case LIST:
3932 if (load_list(self) < 0)
3933 break;
3934 continue;
3935
3936 case EMPTY_DICT:
3937 if (load_empty_dict(self) < 0)
3938 break;
3939 continue;
3940
3941 case DICT:
3942 if (load_dict(self) < 0)
3943 break;
3944 continue;
3945
3946 case OBJ:
3947 if (noload_obj(self) < 0)
3948 break;
3949 continue;
3950
3951 case INST:
3952 if (noload_inst(self) < 0)
3953 break;
3954 continue;
3955
3956 case GLOBAL:
3957 if (noload_global(self) < 0)
3958 break;
3959 continue;
3960
3961 case APPEND:
3962 if (load_append(self) < 0)
3963 break;
3964 continue;
3965
3966 case APPENDS:
3967 if (load_appends(self) < 0)
3968 break;
3969 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003970
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003971 case BUILD:
3972 if (noload_build(self) < 0)
3973 break;
3974 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003975
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003976 case DUP:
3977 if (load_dup(self) < 0)
3978 break;
3979 continue;
3980
3981 case BINGET:
3982 if (load_binget(self) < 0)
3983 break;
3984 continue;
3985
3986 case LONG_BINGET:
3987 if (load_long_binget(self) < 0)
3988 break;
3989 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00003990
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003991 case GET:
3992 if (load_get(self) < 0)
3993 break;
3994 continue;
3995
3996 case MARK:
3997 if (load_mark(self) < 0)
3998 break;
3999 continue;
4000
4001 case BINPUT:
4002 if (load_binput(self) < 0)
4003 break;
4004 continue;
4005
4006 case LONG_BINPUT:
4007 if (load_long_binput(self) < 0)
4008 break;
4009 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004010
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004011 case PUT:
4012 if (load_put(self) < 0)
4013 break;
4014 continue;
4015
4016 case POP:
4017 if (load_pop(self) < 0)
4018 break;
4019 continue;
4020
4021 case POP_MARK:
4022 if (load_pop_mark(self) < 0)
4023 break;
4024 continue;
4025
4026 case SETITEM:
4027 if (load_setitem(self) < 0)
4028 break;
4029 continue;
4030
4031 case SETITEMS:
4032 if (load_setitems(self) < 0)
4033 break;
4034 continue;
4035
4036 case STOP:
4037 break;
4038
4039 case PERSID:
4040 if (load_persid(self) < 0)
4041 break;
4042 continue;
4043
4044 case BINPERSID:
4045 if (load_binpersid(self) < 0)
4046 break;
4047 continue;
4048
4049 case REDUCE:
4050 if (noload_reduce(self) < 0)
4051 break;
4052 continue;
4053
Tim Peters84e87f32001-03-17 04:50:51 +00004054 default:
4055 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004056 "c", s[0]);
Guido van Rossum053b8df1998-11-25 16:18:00 +00004057 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004058 }
4059
4060 break;
4061 }
4062
Guido van Rossum053b8df1998-11-25 16:18:00 +00004063 if ((err = PyErr_Occurred())) {
4064 if (err == PyExc_EOFError) {
4065 PyErr_SetNone(PyExc_EOFError);
Tim Peters84e87f32001-03-17 04:50:51 +00004066 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00004067 return NULL;
4068 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004069
Tim Peters84e87f32001-03-17 04:50:51 +00004070 PDATA_POP(self->stack, val);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004071 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004072}
Tim Peters84e87f32001-03-17 04:50:51 +00004073
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004074
Guido van Rossum60456fd1997-04-09 17:36:32 +00004075static PyObject *
4076Unpickler_load(Unpicklerobject *self, PyObject *args) {
Tim Peters84e87f32001-03-17 04:50:51 +00004077 UNLESS (PyArg_ParseTuple(args, ":load"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004078 return NULL;
4079
4080 return load(self);
4081}
4082
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004083static PyObject *
4084Unpickler_noload(Unpicklerobject *self, PyObject *args) {
Tim Peters84e87f32001-03-17 04:50:51 +00004085 UNLESS (PyArg_ParseTuple(args, ":noload"))
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004086 return NULL;
4087
4088 return noload(self);
4089}
4090
Guido van Rossum60456fd1997-04-09 17:36:32 +00004091
4092static struct PyMethodDef Unpickler_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004093 {"load", (PyCFunction)Unpickler_load, 1,
4094 "load() -- Load a pickle"
4095 },
4096 {"noload", (PyCFunction)Unpickler_noload, 1,
4097 "noload() -- not load a pickle, but go through most of the motions\n"
4098 "\n"
4099 "This function can be used to read past a pickle without instantiating\n"
4100 "any objects or importing any modules. It can also be used to find all\n"
4101 "persistent references without instantiating any objects or importing\n"
4102 "any modules.\n"
4103 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00004104 {NULL, NULL} /* sentinel */
4105};
4106
4107
4108static Unpicklerobject *
4109newUnpicklerobject(PyObject *f) {
4110 Unpicklerobject *self;
4111
Guido van Rossumb18618d2000-05-03 23:44:39 +00004112 UNLESS (self = PyObject_New(Unpicklerobject, &Unpicklertype))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004113 return NULL;
4114
4115 self->file = NULL;
4116 self->arg = NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00004117 self->stack = (Pdata*)Pdata_New();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004118 self->pers_func = NULL;
4119 self->last_string = NULL;
4120 self->marks = NULL;
4121 self->num_marks = 0;
4122 self->marks_size = 0;
4123 self->buf_size = 0;
4124 self->read = NULL;
Guido van Rossum8a6dba31998-03-06 01:39:39 +00004125 self->readline = NULL;
Guido van Rossum21ef0881998-12-11 03:20:00 +00004126 self->safe_constructors = NULL;
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004127 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004128
Tim Peters84e87f32001-03-17 04:50:51 +00004129 UNLESS (self->memo = PyDict_New())
Guido van Rossum83addc72000-04-21 20:49:36 +00004130 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004131
4132 Py_INCREF(f);
4133 self->file = f;
4134
4135 /* Set read, readline based on type of f */
4136 if (PyFile_Check(f)) {
4137 self->fp = PyFile_AsFile(f);
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00004138 if (self->fp == NULL) {
Guido van Rossum83addc72000-04-21 20:49:36 +00004139 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
4140 goto err;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00004141 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004142 self->read_func = read_file;
4143 self->readline_func = readline_file;
4144 }
4145 else if (PycStringIO_InputCheck(f)) {
4146 self->fp = NULL;
4147 self->read_func = read_cStringIO;
4148 self->readline_func = readline_cStringIO;
4149 }
4150 else {
4151
4152 self->fp = NULL;
4153 self->read_func = read_other;
4154 self->readline_func = readline_other;
4155
Guido van Rossum053b8df1998-11-25 16:18:00 +00004156 UNLESS ((self->readline = PyObject_GetAttr(f, readline_str)) &&
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004157 (self->read = PyObject_GetAttr(f, read_str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004158 PyErr_Clear();
4159 PyErr_SetString( PyExc_TypeError, "argument must have 'read' and "
4160 "'readline' attributes" );
Guido van Rossum053b8df1998-11-25 16:18:00 +00004161 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004162 }
4163 }
4164
Guido van Rossum053b8df1998-11-25 16:18:00 +00004165 if (PyEval_GetRestricted()) {
4166 /* Restricted execution, get private tables */
4167 PyObject *m;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004168
Guido van Rossum053b8df1998-11-25 16:18:00 +00004169 UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
4170 self->safe_constructors=PyObject_GetAttr(m, safe_constructors_str);
4171 Py_DECREF(m);
4172 UNLESS (self->safe_constructors) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004173 }
4174 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00004175 self->safe_constructors=safe_constructors;
4176 Py_INCREF(safe_constructors);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004177 }
4178
Guido van Rossum60456fd1997-04-09 17:36:32 +00004179 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004180
4181err:
4182 Py_DECREF((PyObject *)self);
4183 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004184}
4185
4186
4187static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004188get_Unpickler(PyObject *self, PyObject *args) {
4189 PyObject *file;
Tim Peters84e87f32001-03-17 04:50:51 +00004190
Guido van Rossum43713e52000-02-29 13:59:29 +00004191 UNLESS (PyArg_ParseTuple(args, "O:Unpickler", &file))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004192 return NULL;
4193 return (PyObject *)newUnpicklerobject(file);
4194}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004195
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004196
Guido van Rossum60456fd1997-04-09 17:36:32 +00004197static void
4198Unpickler_dealloc(Unpicklerobject *self) {
4199 Py_XDECREF(self->readline);
4200 Py_XDECREF(self->read);
4201 Py_XDECREF(self->file);
4202 Py_XDECREF(self->memo);
4203 Py_XDECREF(self->stack);
4204 Py_XDECREF(self->pers_func);
4205 Py_XDECREF(self->arg);
4206 Py_XDECREF(self->last_string);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004207 Py_XDECREF(self->safe_constructors);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004208
Guido van Rossum60456fd1997-04-09 17:36:32 +00004209 if (self->marks) {
4210 free(self->marks);
4211 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004212
Guido van Rossum60456fd1997-04-09 17:36:32 +00004213 if (self->buf_size) {
4214 free(self->buf);
4215 }
Tim Peters84e87f32001-03-17 04:50:51 +00004216
Guido van Rossumb18618d2000-05-03 23:44:39 +00004217 PyObject_Del(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004218}
4219
4220
4221static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004222Unpickler_getattr(Unpicklerobject *self, char *name) {
4223 if (!strcmp(name, "persistent_load")) {
4224 if (!self->pers_func) {
4225 PyErr_SetString(PyExc_AttributeError, name);
4226 return NULL;
4227 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004228
Guido van Rossum60456fd1997-04-09 17:36:32 +00004229 Py_INCREF(self->pers_func);
4230 return self->pers_func;
4231 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004232
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004233 if (!strcmp(name, "find_global")) {
4234 if (!self->find_class) {
4235 PyErr_SetString(PyExc_AttributeError, name);
4236 return NULL;
4237 }
4238
4239 Py_INCREF(self->find_class);
4240 return self->find_class;
4241 }
4242
Guido van Rossum60456fd1997-04-09 17:36:32 +00004243 if (!strcmp(name, "memo")) {
4244 if (!self->memo) {
4245 PyErr_SetString(PyExc_AttributeError, name);
4246 return NULL;
4247 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004248
Guido van Rossum60456fd1997-04-09 17:36:32 +00004249 Py_INCREF(self->memo);
4250 return self->memo;
4251 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004252
Guido van Rossum60456fd1997-04-09 17:36:32 +00004253 if (!strcmp(name, "UnpicklingError")) {
4254 Py_INCREF(UnpicklingError);
4255 return UnpicklingError;
4256 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004257
Guido van Rossum60456fd1997-04-09 17:36:32 +00004258 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
4259}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004260
Guido van Rossum60456fd1997-04-09 17:36:32 +00004261
4262static int
4263Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004264
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004265 if (!strcmp(name, "persistent_load")) {
4266 Py_XDECREF(self->pers_func);
4267 self->pers_func = value;
4268 Py_XINCREF(value);
4269 return 0;
4270 }
4271
4272 if (!strcmp(name, "find_global")) {
4273 Py_XDECREF(self->find_class);
4274 self->find_class = value;
4275 Py_XINCREF(value);
4276 return 0;
4277 }
4278
Guido van Rossum053b8df1998-11-25 16:18:00 +00004279 if (! value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004280 PyErr_SetString(PyExc_TypeError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00004281 "attribute deletion is not supported");
4282 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004283 }
4284
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004285 if (strcmp(name, "memo") == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00004286 if (! PyDict_Check(value)) {
4287 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
4288 return -1;
4289 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004290 Py_XDECREF(self->memo);
4291 self->memo = value;
4292 Py_INCREF(value);
4293 return 0;
4294 }
4295
Guido van Rossum60456fd1997-04-09 17:36:32 +00004296 PyErr_SetString(PyExc_AttributeError, name);
4297 return -1;
4298}
4299
4300
4301static PyObject *
4302cpm_dump(PyObject *self, PyObject *args) {
4303 PyObject *ob, *file, *res = NULL;
4304 Picklerobject *pickler = 0;
4305 int bin = 0;
4306
Guido van Rossum053b8df1998-11-25 16:18:00 +00004307 UNLESS (PyArg_ParseTuple(args, "OO|i", &ob, &file, &bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004308 goto finally;
4309
Guido van Rossum053b8df1998-11-25 16:18:00 +00004310 UNLESS (pickler = newPicklerobject(file, bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004311 goto finally;
4312
4313 if (dump(pickler, ob) < 0)
4314 goto finally;
4315
4316 Py_INCREF(Py_None);
4317 res = Py_None;
4318
4319finally:
4320 Py_XDECREF(pickler);
4321
4322 return res;
4323}
4324
4325
4326static PyObject *
4327cpm_dumps(PyObject *self, PyObject *args) {
4328 PyObject *ob, *file = 0, *res = NULL;
4329 Picklerobject *pickler = 0;
4330 int bin = 0;
4331
Guido van Rossum43713e52000-02-29 13:59:29 +00004332 UNLESS (PyArg_ParseTuple(args, "O|i:dumps", &ob, &bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004333 goto finally;
4334
Guido van Rossum053b8df1998-11-25 16:18:00 +00004335 UNLESS (file = PycStringIO->NewOutput(128))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004336 goto finally;
4337
Guido van Rossum053b8df1998-11-25 16:18:00 +00004338 UNLESS (pickler = newPicklerobject(file, bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004339 goto finally;
4340
4341 if (dump(pickler, ob) < 0)
4342 goto finally;
4343
4344 res = PycStringIO->cgetvalue(file);
4345
4346finally:
4347 Py_XDECREF(pickler);
4348 Py_XDECREF(file);
4349
4350 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00004351}
4352
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004353
4354static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004355cpm_load(PyObject *self, PyObject *args) {
4356 Unpicklerobject *unpickler = 0;
4357 PyObject *ob, *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004358
Guido van Rossum43713e52000-02-29 13:59:29 +00004359 UNLESS (PyArg_ParseTuple(args, "O:load", &ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004360 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004361
Guido van Rossum053b8df1998-11-25 16:18:00 +00004362 UNLESS (unpickler = newUnpicklerobject(ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004363 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004364
Guido van Rossum60456fd1997-04-09 17:36:32 +00004365 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004366
Guido van Rossum60456fd1997-04-09 17:36:32 +00004367finally:
4368 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004369
Guido van Rossum60456fd1997-04-09 17:36:32 +00004370 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004371}
4372
4373
4374static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004375cpm_loads(PyObject *self, PyObject *args) {
4376 PyObject *ob, *file = 0, *res = NULL;
4377 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004378
Guido van Rossum43713e52000-02-29 13:59:29 +00004379 UNLESS (PyArg_ParseTuple(args, "S:loads", &ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004380 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004381
Guido van Rossum053b8df1998-11-25 16:18:00 +00004382 UNLESS (file = PycStringIO->NewInput(ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004383 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00004384
Guido van Rossum053b8df1998-11-25 16:18:00 +00004385 UNLESS (unpickler = newUnpicklerobject(file))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004386 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004387
Guido van Rossum60456fd1997-04-09 17:36:32 +00004388 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004389
Guido van Rossum60456fd1997-04-09 17:36:32 +00004390finally:
4391 Py_XDECREF(file);
4392 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004393
Guido van Rossum60456fd1997-04-09 17:36:32 +00004394 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004395}
4396
4397
Tim Peters84e87f32001-03-17 04:50:51 +00004398static char Unpicklertype__doc__[] =
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004399"Objects that know how to unpickle";
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004400
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004401static PyTypeObject Unpicklertype = {
4402 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004403 0, /*ob_size*/
4404 "Unpickler", /*tp_name*/
4405 sizeof(Unpicklerobject), /*tp_basicsize*/
4406 0, /*tp_itemsize*/
4407 /* methods */
4408 (destructor)Unpickler_dealloc, /*tp_dealloc*/
4409 (printfunc)0, /*tp_print*/
4410 (getattrfunc)Unpickler_getattr, /*tp_getattr*/
4411 (setattrfunc)Unpickler_setattr, /*tp_setattr*/
4412 (cmpfunc)0, /*tp_compare*/
4413 (reprfunc)0, /*tp_repr*/
4414 0, /*tp_as_number*/
4415 0, /*tp_as_sequence*/
4416 0, /*tp_as_mapping*/
4417 (hashfunc)0, /*tp_hash*/
4418 (ternaryfunc)0, /*tp_call*/
4419 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004420
Guido van Rossum60456fd1997-04-09 17:36:32 +00004421 /* Space for future expansion */
4422 0L,0L,0L,0L,
4423 Unpicklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004424};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004425
Guido van Rossum60456fd1997-04-09 17:36:32 +00004426static struct PyMethodDef cPickle_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004427 {"dump", (PyCFunction)cpm_dump, 1,
4428 "dump(object, file, [binary]) --"
4429 "Write an object in pickle format to the given file\n"
4430 "\n"
4431 "If the optional argument, binary, is provided and is true, then the\n"
4432 "pickle will be written in binary format, which is more space and\n"
4433 "computationally efficient. \n"
4434 },
4435 {"dumps", (PyCFunction)cpm_dumps, 1,
4436 "dumps(object, [binary]) --"
4437 "Return a string containing an object in pickle format\n"
4438 "\n"
4439 "If the optional argument, binary, is provided and is true, then the\n"
4440 "pickle will be written in binary format, which is more space and\n"
4441 "computationally efficient. \n"
4442 },
4443 {"load", (PyCFunction)cpm_load, 1,
4444 "load(file) -- Load a pickle from the given file"},
4445 {"loads", (PyCFunction)cpm_loads, 1,
4446 "loads(string) -- Load a pickle from the given string"},
4447 {"Pickler", (PyCFunction)get_Pickler, 1,
4448 "Pickler(file, [binary]) -- Create a pickler\n"
4449 "\n"
4450 "If the optional argument, binary, is provided and is true, then\n"
4451 "pickles will be written in binary format, which is more space and\n"
4452 "computationally efficient. \n"
4453 },
4454 {"Unpickler", (PyCFunction)get_Unpickler, 1,
4455 "Unpickler(file) -- Create an unpickler"},
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004456 { NULL, NULL }
4457};
4458
Guido van Rossum60456fd1997-04-09 17:36:32 +00004459static int
Guido van Rossumebba4202000-09-07 14:35:37 +00004460init_stuff(PyObject *module_dict) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004461 PyObject *string, *copy_reg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004462
4463#define INIT_STR(S) UNLESS(S ## _str=PyString_FromString(#S)) return -1;
4464
4465 INIT_STR(__class__);
4466 INIT_STR(__getinitargs__);
4467 INIT_STR(__dict__);
4468 INIT_STR(__getstate__);
4469 INIT_STR(__setstate__);
4470 INIT_STR(__name__);
Guido van Rossum142eeb81997-08-13 03:14:41 +00004471 INIT_STR(__main__);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004472 INIT_STR(__reduce__);
4473 INIT_STR(write);
4474 INIT_STR(__safe_for_unpickling__);
4475 INIT_STR(append);
4476 INIT_STR(read);
4477 INIT_STR(readline);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004478 INIT_STR(copy_reg);
4479 INIT_STR(dispatch_table);
4480 INIT_STR(safe_constructors);
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004481 INIT_STR(__basicnew__);
Guido van Rossum053b8df1998-11-25 16:18:00 +00004482 UNLESS (empty_str=PyString_FromString("")) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004483
Guido van Rossum053b8df1998-11-25 16:18:00 +00004484 UNLESS (copy_reg = PyImport_ImportModule("copy_reg"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004485 return -1;
4486
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004487 /* These next few are special because we want to use different
4488 ones in restricted mode. */
4489
Guido van Rossum053b8df1998-11-25 16:18:00 +00004490 UNLESS (dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004491 return -1;
4492
Guido van Rossum053b8df1998-11-25 16:18:00 +00004493 UNLESS (safe_constructors = PyObject_GetAttr(copy_reg,
4494 safe_constructors_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004495 return -1;
4496
4497 Py_DECREF(copy_reg);
4498
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004499 /* Down to here ********************************** */
4500
Guido van Rossum053b8df1998-11-25 16:18:00 +00004501 UNLESS (string = PyImport_ImportModule("string"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004502 return -1;
4503
Guido van Rossum053b8df1998-11-25 16:18:00 +00004504 UNLESS (atol_func = PyObject_GetAttrString(string, "atol"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004505 return -1;
4506
4507 Py_DECREF(string);
4508
Guido van Rossum053b8df1998-11-25 16:18:00 +00004509 UNLESS (empty_tuple = PyTuple_New(0))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004510 return -1;
4511
Guido van Rossumc03158b1999-06-09 15:23:31 +00004512 /* Ugh */
4513 UNLESS (t=PyImport_ImportModule("__builtin__")) return -1;
4514 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
4515 return -1;
4516
4517 UNLESS (t=PyDict_New()) return -1;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004518 UNLESS (r=PyRun_String(
4519 "def __init__(self, *args): self.args=args\n\n"
4520 "def __str__(self):\n"
4521 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
4522 Py_file_input,
4523 module_dict, t) ) return -1;
4524 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00004525
4526 UNLESS (PickleError = PyErr_NewException("cPickle.PickleError", NULL, t))
4527 return -1;
4528
4529 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00004530
Tim Peters84e87f32001-03-17 04:50:51 +00004531
4532 UNLESS (PicklingError = PyErr_NewException("cPickle.PicklingError",
Guido van Rossumc03158b1999-06-09 15:23:31 +00004533 PickleError, NULL))
4534 return -1;
4535
4536 UNLESS (t=PyDict_New()) return -1;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004537 UNLESS (r=PyRun_String(
4538 "def __init__(self, *args): self.args=args\n\n"
4539 "def __str__(self):\n"
4540 " a=self.args\n"
4541 " a=a and type(a[0]) or '(what)'\n"
4542 " return 'Cannot pickle %s objects' % a\n"
4543 , Py_file_input,
4544 module_dict, t) ) return -1;
4545 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00004546
4547 UNLESS (UnpickleableError = PyErr_NewException(
4548 "cPickle.UnpickleableError", PicklingError, t))
4549 return -1;
4550
4551 Py_DECREF(t);
4552
Tim Peters84e87f32001-03-17 04:50:51 +00004553 UNLESS (UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
Guido van Rossumc03158b1999-06-09 15:23:31 +00004554 PickleError, NULL))
4555 return -1;
4556
Tim Peters84e87f32001-03-17 04:50:51 +00004557 if (PyDict_SetItemString(module_dict, "PickleError",
Guido van Rossumc03158b1999-06-09 15:23:31 +00004558 PickleError) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004559 return -1;
4560
Tim Peters84e87f32001-03-17 04:50:51 +00004561 if (PyDict_SetItemString(module_dict, "PicklingError",
Guido van Rossum60456fd1997-04-09 17:36:32 +00004562 PicklingError) < 0)
4563 return -1;
4564
Guido van Rossum60456fd1997-04-09 17:36:32 +00004565 if (PyDict_SetItemString(module_dict, "UnpicklingError",
4566 UnpicklingError) < 0)
4567 return -1;
4568
Guido van Rossumc03158b1999-06-09 15:23:31 +00004569 if (PyDict_SetItemString(module_dict, "UnpickleableError",
4570 UnpickleableError) < 0)
4571 return -1;
4572
Guido van Rossum053b8df1998-11-25 16:18:00 +00004573 UNLESS (BadPickleGet = PyString_FromString("cPickle.BadPickleGet"))
4574 return -1;
4575
4576 if (PyDict_SetItemString(module_dict, "BadPickleGet",
4577 BadPickleGet) < 0)
4578 return -1;
4579
Guido van Rossum60456fd1997-04-09 17:36:32 +00004580 PycString_IMPORT;
Tim Peters84e87f32001-03-17 04:50:51 +00004581
Guido van Rossum60456fd1997-04-09 17:36:32 +00004582 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004583}
4584
Guido van Rossumf9ffb031999-02-04 14:54:04 +00004585#ifndef DL_EXPORT /* declarations for DLL import/export */
4586#define DL_EXPORT(RTYPE) RTYPE
4587#endif
Guido van Rossum50f385c1998-12-04 18:48:44 +00004588DL_EXPORT(void)
Thomas Wouters58d05102000-07-24 14:43:35 +00004589initcPickle(void) {
Guido van Rossumebba4202000-09-07 14:35:37 +00004590 PyObject *m, *d, *di, *v, *k;
4591 int i;
Guido van Rossum2f80d961999-07-13 15:18:58 +00004592 char *rev="1.71";
Guido van Rossum60456fd1997-04-09 17:36:32 +00004593 PyObject *format_version;
4594 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004595
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004596 Picklertype.ob_type = &PyType_Type;
4597 Unpicklertype.ob_type = &PyType_Type;
Guido van Rossum053b8df1998-11-25 16:18:00 +00004598 PdataType.ob_type = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004599
Tim Peters84e87f32001-03-17 04:50:51 +00004600 /* Initialize some pieces. We need to do this before module creation,
4601 so we're forced to use a temporary dictionary. :(
Guido van Rossumebba4202000-09-07 14:35:37 +00004602 */
4603 di=PyDict_New();
4604 if (!di) return;
4605 if (init_stuff(di) < 0) return;
4606
Guido van Rossum60456fd1997-04-09 17:36:32 +00004607 /* Create the module and add the functions */
4608 m = Py_InitModule4("cPickle", cPickle_methods,
4609 cPickle_module_documentation,
4610 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004611
Guido van Rossum60456fd1997-04-09 17:36:32 +00004612 /* Add some symbolic constants to the module */
4613 d = PyModule_GetDict(m);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004614 PyDict_SetItemString(d,"__version__", v = PyString_FromString(rev));
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004615 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00004616
Guido van Rossumebba4202000-09-07 14:35:37 +00004617 /* Copy data from di. Waaa. */
4618 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
4619 if (PyObject_SetItem(d, k, v) < 0) {
4620 Py_DECREF(di);
4621 return;
4622 }
4623 }
4624 Py_DECREF(di);
4625
Guido van Rossum60456fd1997-04-09 17:36:32 +00004626 format_version = PyString_FromString("1.3");
4627 compatible_formats = Py_BuildValue("[sss]", "1.0", "1.1", "1.2");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004628
Guido van Rossum60456fd1997-04-09 17:36:32 +00004629 PyDict_SetItemString(d, "format_version", format_version);
4630 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004631 Py_XDECREF(format_version);
4632 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004633}