blob: 18df599b9e9325e215aa90ebc0eb6ba7e9ebfa06 [file] [log] [blame]
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001#include "Python.h"
2#include "cStringIO.h"
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003#include "structmember.h"
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005PyDoc_STRVAR(cPickle_module_documentation,
Tim Peters64c04d12003-02-01 06:27:59 +00006"C implementation and optimization of the Python pickle module.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007
Guido van Rossum142eeb81997-08-13 03:14:41 +00008#ifndef Py_eval_input
9#include <graminit.h>
10#define Py_eval_input eval_input
Guido van Rossumc6ef2041997-08-21 02:30:45 +000011#endif /* Py_eval_input */
Guido van Rossum142eeb81997-08-13 03:14:41 +000012
Guido van Rossum60456fd1997-04-09 17:36:32 +000013#define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL))
Guido van Rossum2f4caa41997-01-06 22:59:08 +000014
Guido van Rossum60456fd1997-04-09 17:36:32 +000015#define WRITE_BUF_SIZE 256
16
Tim Peters5bd2a792003-02-01 16:45:06 +000017/* Bump this when new opcodes are added to the pickle protocol. */
Tim Peters8587b3c2003-02-13 15:44:41 +000018#define HIGHEST_PROTOCOL 2
Tim Peters5bd2a792003-02-01 16:45:06 +000019
Tim Peters797ec242003-02-01 06:22:36 +000020/*
21 * Pickle opcodes. These must be kept in synch with pickle.py. Extensive
22 * docs are in pickletools.py.
23 */
Guido van Rossum60456fd1997-04-09 17:36:32 +000024#define MARK '('
25#define STOP '.'
26#define POP '0'
27#define POP_MARK '1'
28#define DUP '2'
29#define FLOAT 'F'
Guido van Rossum60456fd1997-04-09 17:36:32 +000030#define BINFLOAT 'G'
Guido van Rossum60456fd1997-04-09 17:36:32 +000031#define INT 'I'
32#define BININT 'J'
33#define BININT1 'K'
34#define LONG 'L'
35#define BININT2 'M'
36#define NONE 'N'
37#define PERSID 'P'
38#define BINPERSID 'Q'
39#define REDUCE 'R'
40#define STRING 'S'
41#define BINSTRING 'T'
Guido van Rossum2f4caa41997-01-06 22:59:08 +000042#define SHORT_BINSTRING 'U'
Guido van Rossum5fccb7c2000-03-10 23:11:40 +000043#define UNICODE 'V'
44#define BINUNICODE 'X'
Guido van Rossum60456fd1997-04-09 17:36:32 +000045#define APPEND 'a'
46#define BUILD 'b'
47#define GLOBAL 'c'
48#define DICT 'd'
49#define EMPTY_DICT '}'
50#define APPENDS 'e'
51#define GET 'g'
52#define BINGET 'h'
53#define INST 'i'
54#define LONG_BINGET 'j'
55#define LIST 'l'
56#define EMPTY_LIST ']'
57#define OBJ 'o'
58#define PUT 'p'
59#define BINPUT 'q'
60#define LONG_BINPUT 'r'
61#define SETITEM 's'
62#define TUPLE 't'
63#define EMPTY_TUPLE ')'
64#define SETITEMS 'u'
Tim Peters797ec242003-02-01 06:22:36 +000065
66/* Protocol 2. */
67#define PROTO '\x80' /* identify pickle protocol */
68#define NEWOBJ '\x81' /* build object by applying cls.__new__ to argtuple */
69#define EXT1 '\x82' /* push object from extension registry; 1-byte index */
70#define EXT2 '\x83' /* ditto, but 2-byte index */
71#define EXT4 '\x84' /* ditto, but 4-byte index */
72#define TUPLE1 '\x85' /* build 1-tuple from stack top */
73#define TUPLE2 '\x86' /* build 2-tuple from two topmost stack items */
74#define TUPLE3 '\x87' /* build 3-tuple from three topmost stack items */
75#define NEWTRUE '\x88' /* push True */
76#define NEWFALSE '\x89' /* push False */
77#define LONG1 '\x8a' /* push long from < 256 bytes */
78#define LONG4 '\x8b' /* push really big long */
79
80/* There aren't opcodes -- they're ways to pickle bools before protocol 2,
81 * so that unpicklers written before bools were introduced unpickle them
82 * as ints, but unpicklers after can recognize that bools were intended.
83 * Note that protocol 2 added direct ways to pickle bools.
84 */
Jack Jansen3a967022002-06-26 20:40:42 +000085#undef TRUE
Guido van Rossume2763392002-04-05 19:30:08 +000086#define TRUE "I01\n"
Jack Jansen3a967022002-06-26 20:40:42 +000087#undef FALSE
Guido van Rossume2763392002-04-05 19:30:08 +000088#define FALSE "I00\n"
Guido van Rossum77f6a652002-04-03 22:41:51 +000089
Tim Peters1092d642003-02-11 21:06:20 +000090/* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
Tim Peters42f08ac2003-02-11 22:43:24 +000091 * batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
92 * break if this gets out of synch with pickle.py, but it's unclear that
93 * would help anything either.
Tim Peters1092d642003-02-11 21:06:20 +000094 */
95#define BATCHSIZE 1000
96
Guido van Rossum60456fd1997-04-09 17:36:32 +000097static char MARKv = MARK;
Guido van Rossum2f4caa41997-01-06 22:59:08 +000098
Guido van Rossumc03158b1999-06-09 15:23:31 +000099static PyObject *PickleError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000100static PyObject *PicklingError;
Guido van Rossumc03158b1999-06-09 15:23:31 +0000101static PyObject *UnpickleableError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000102static PyObject *UnpicklingError;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000103static PyObject *BadPickleGet;
104
Tim Peters5b7da392003-02-04 00:21:07 +0000105/* As the name says, an empty tuple. */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000106static PyObject *empty_tuple;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000107
Tim Peters5b7da392003-02-04 00:21:07 +0000108/* copy_reg.dispatch_table, {type_object: pickling_function} */
109static PyObject *dispatch_table;
110
111/* For EXT[124] opcodes. */
Guido van Rossumd4b920c2003-02-04 01:54:49 +0000112/* copy_reg._extension_registry, {(module_name, function_name): code} */
Tim Peters5b7da392003-02-04 00:21:07 +0000113static PyObject *extension_registry;
Guido van Rossumd4b920c2003-02-04 01:54:49 +0000114/* copy_reg._inverted_registry, {code: (module_name, function_name)} */
Tim Peters5b7da392003-02-04 00:21:07 +0000115static PyObject *inverted_registry;
Guido van Rossumd4b920c2003-02-04 01:54:49 +0000116/* copy_reg._extension_cache, {code: object} */
Tim Peters5b7da392003-02-04 00:21:07 +0000117static PyObject *extension_cache;
118
Tim Peters731098b2003-02-04 20:56:09 +0000119/* For looking up name pairs in copy_reg._extension_registry. */
120static PyObject *two_tuple;
121
Guido van Rossum60456fd1997-04-09 17:36:32 +0000122static PyObject *__class___str, *__getinitargs___str, *__dict___str,
123 *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
Guido van Rossumb289b872003-02-19 01:45:13 +0000124 *__reduce_ex___str,
Tim Peters1f1b2d22003-02-01 02:16:37 +0000125 *write_str, *append_str,
Neal Norwitzb183a252006-04-10 01:03:32 +0000126 *read_str, *readline_str, *__main___str,
Tim Peters1f1b2d22003-02-01 02:16:37 +0000127 *copy_reg_str, *dispatch_table_str;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000128
Guido van Rossum053b8df1998-11-25 16:18:00 +0000129/*************************************************************************
130 Internal Data type for pickle data. */
131
132typedef struct {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000133 PyObject_HEAD
Tim Peters1d63c9f2003-02-02 20:29:39 +0000134 int length; /* number of initial slots in data currently used */
135 int size; /* number of slots in data allocated */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000136 PyObject **data;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000137} Pdata;
138
Tim Peters84e87f32001-03-17 04:50:51 +0000139static void
Tim Peterscba30e22003-02-01 06:24:36 +0000140Pdata_dealloc(Pdata *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000141{
142 int i;
143 PyObject **p;
Tim Peterscba30e22003-02-01 06:24:36 +0000144
Tim Peters1d63c9f2003-02-02 20:29:39 +0000145 for (i = self->length, p = self->data; --i >= 0; p++) {
146 Py_DECREF(*p);
147 }
148 if (self->data)
149 free(self->data);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000150 PyObject_Del(self);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000151}
152
153static PyTypeObject PdataType = {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000154 PyObject_HEAD_INIT(NULL) 0, "cPickle.Pdata", sizeof(Pdata), 0,
155 (destructor)Pdata_dealloc,
156 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
Guido van Rossum053b8df1998-11-25 16:18:00 +0000157};
158
159#define Pdata_Check(O) ((O)->ob_type == &PdataType)
160
161static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000162Pdata_New(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000163{
164 Pdata *self;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000165
Tim Peters1d63c9f2003-02-02 20:29:39 +0000166 if (!(self = PyObject_New(Pdata, &PdataType)))
167 return NULL;
168 self->size = 8;
169 self->length = 0;
170 self->data = malloc(self->size * sizeof(PyObject*));
171 if (self->data)
172 return (PyObject*)self;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000173 Py_DECREF(self);
174 return PyErr_NoMemory();
Guido van Rossum053b8df1998-11-25 16:18:00 +0000175}
176
Tim Peters84e87f32001-03-17 04:50:51 +0000177static int
Tim Peterscba30e22003-02-01 06:24:36 +0000178stackUnderflow(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000179{
180 PyErr_SetString(UnpicklingError, "unpickling stack underflow");
181 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000182}
183
Tim Peters1d63c9f2003-02-02 20:29:39 +0000184/* Retain only the initial clearto items. If clearto >= the current
185 * number of items, this is a (non-erroneous) NOP.
186 */
Guido van Rossum053b8df1998-11-25 16:18:00 +0000187static int
Tim Peterscba30e22003-02-01 06:24:36 +0000188Pdata_clear(Pdata *self, int clearto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000189{
190 int i;
191 PyObject **p;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000192
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000193 if (clearto < 0) return stackUnderflow();
194 if (clearto >= self->length) return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000195
Tim Peters1d63c9f2003-02-02 20:29:39 +0000196 for (i = self->length, p = self->data + clearto;
197 --i >= clearto;
198 p++) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000199 Py_DECREF(*p);
Tim Peters1d63c9f2003-02-02 20:29:39 +0000200 }
201 self->length = clearto;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000202
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000203 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000204}
205
Tim Peters84e87f32001-03-17 04:50:51 +0000206static int
Tim Peterscba30e22003-02-01 06:24:36 +0000207Pdata_grow(Pdata *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000208{
Tim Peters1d63c9f2003-02-02 20:29:39 +0000209 int bigger;
210 size_t nbytes;
211
Tim Peters1d63c9f2003-02-02 20:29:39 +0000212 bigger = self->size << 1;
Tim Peterse0a39072003-02-03 15:45:56 +0000213 if (bigger <= 0) /* was 0, or new value overflows */
Tim Peters1d63c9f2003-02-02 20:29:39 +0000214 goto nomemory;
215 if ((int)(size_t)bigger != bigger)
216 goto nomemory;
217 nbytes = (size_t)bigger * sizeof(PyObject *);
218 if (nbytes / sizeof(PyObject *) != (size_t)bigger)
219 goto nomemory;
220 self->data = realloc(self->data, nbytes);
221 if (self->data == NULL)
222 goto nomemory;
223 self->size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000224 return 0;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000225
226 nomemory:
227 self->size = 0;
228 PyErr_NoMemory();
229 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000230}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000231
Tim Peterse0a39072003-02-03 15:45:56 +0000232/* D is a Pdata*. Pop the topmost element and store it into V, which
233 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
Tim Peters1d63c9f2003-02-02 20:29:39 +0000234 * is raised and V is set to NULL. D and V may be evaluated several times.
235 */
236#define PDATA_POP(D, V) { \
Tim Peterse0a39072003-02-03 15:45:56 +0000237 if ((D)->length) \
238 (V) = (D)->data[--((D)->length)]; \
239 else { \
240 PyErr_SetString(UnpicklingError, "bad pickle data"); \
241 (V) = NULL; \
242 } \
Guido van Rossum053b8df1998-11-25 16:18:00 +0000243}
244
Tim Peterse0a39072003-02-03 15:45:56 +0000245/* PDATA_PUSH and PDATA_APPEND both push rvalue PyObject* O on to Pdata*
246 * D. If the Pdata stack can't be grown to hold the new value, both
247 * raise MemoryError and execute "return ER". The difference is in ownership
248 * of O after: _PUSH transfers ownership of O from the caller to the stack
249 * (no incref of O is done, and in case of error O is decrefed), while
250 * _APPEND pushes a new reference.
251 */
252
253/* Push O on stack D, giving ownership of O to the stack. */
254#define PDATA_PUSH(D, O, ER) { \
255 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
256 Pdata_grow((Pdata*)(D)) < 0) { \
257 Py_DECREF(O); \
258 return ER; \
259 } \
260 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
261}
262
263/* Push O on stack D, pushing a new reference. */
264#define PDATA_APPEND(D, O, ER) { \
265 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
266 Pdata_grow((Pdata*)(D)) < 0) \
267 return ER; \
268 Py_INCREF(O); \
269 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
270}
271
272
Guido van Rossum053b8df1998-11-25 16:18:00 +0000273static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000274Pdata_popTuple(Pdata *self, int start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000275{
276 PyObject *r;
277 int i, j, l;
Tim Peterscba30e22003-02-01 06:24:36 +0000278
Tim Peters1d63c9f2003-02-02 20:29:39 +0000279 l = self->length-start;
280 r = PyTuple_New(l);
281 if (r == NULL)
282 return NULL;
283 for (i = start, j = 0 ; j < l; i++, j++)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000284 PyTuple_SET_ITEM(r, j, self->data[i]);
Tim Peterscba30e22003-02-01 06:24:36 +0000285
Tim Peters1d63c9f2003-02-02 20:29:39 +0000286 self->length = start;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000287 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000288}
289
290static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000291Pdata_popList(Pdata *self, int start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000292{
293 PyObject *r;
294 int i, j, l;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000295
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000296 l=self->length-start;
297 if (!( r=PyList_New(l))) return NULL;
298 for (i=start, j=0 ; j < l; i++, j++)
299 PyList_SET_ITEM(r, j, self->data[i]);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000300
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000301 self->length=start;
302 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000303}
304
Guido van Rossum053b8df1998-11-25 16:18:00 +0000305/*************************************************************************/
306
307#define ARG_TUP(self, o) { \
308 if (self->arg || (self->arg=PyTuple_New(1))) { \
309 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
310 PyTuple_SET_ITEM(self->arg,0,o); \
311 } \
312 else { \
313 Py_DECREF(o); \
314 } \
315}
316
317#define FREE_ARG_TUP(self) { \
318 if (self->arg->ob_refcnt > 1) { \
319 Py_DECREF(self->arg); \
320 self->arg=NULL; \
321 } \
322 }
323
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000324typedef struct Picklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000325 PyObject_HEAD
326 FILE *fp;
327 PyObject *write;
328 PyObject *file;
329 PyObject *memo;
330 PyObject *arg;
331 PyObject *pers_func;
332 PyObject *inst_pers_func;
Tim Peters797ec242003-02-01 06:22:36 +0000333
334 /* pickle protocol number, >= 0 */
335 int proto;
336
337 /* bool, true if proto > 0 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000338 int bin;
Tim Peters797ec242003-02-01 06:22:36 +0000339
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000340 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
Martin v. Löwis5a395302002-08-04 08:20:23 +0000341 int nesting;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000342 int (*write_func)(struct Picklerobject *, const char *, Py_ssize_t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000343 char *write_buf;
344 int buf_size;
345 PyObject *dispatch_table;
346 int fast_container; /* count nested container dumps */
347 PyObject *fast_memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000348} Picklerobject;
349
Barry Warsaw52acb492001-12-21 20:04:22 +0000350#ifndef PY_CPICKLE_FAST_LIMIT
351#define PY_CPICKLE_FAST_LIMIT 50
352#endif
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000353
Jeremy Hylton938ace62002-07-17 16:30:39 +0000354static PyTypeObject Picklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000355
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000356typedef struct Unpicklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000357 PyObject_HEAD
358 FILE *fp;
359 PyObject *file;
360 PyObject *readline;
361 PyObject *read;
362 PyObject *memo;
363 PyObject *arg;
364 Pdata *stack;
365 PyObject *mark;
366 PyObject *pers_func;
367 PyObject *last_string;
368 int *marks;
369 int num_marks;
370 int marks_size;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000371 Py_ssize_t (*read_func)(struct Unpicklerobject *, char **, Py_ssize_t);
372 Py_ssize_t (*readline_func)(struct Unpicklerobject *, char **);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000373 int buf_size;
374 char *buf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000375 PyObject *find_class;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000376} Unpicklerobject;
Tim Peters84e87f32001-03-17 04:50:51 +0000377
Jeremy Hylton938ace62002-07-17 16:30:39 +0000378static PyTypeObject Unpicklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000379
Thomas Wouters4789b3a2000-07-24 11:36:47 +0000380/* Forward decls that need the above structs */
381static int save(Picklerobject *, PyObject *, int);
382static int put2(Picklerobject *, PyObject *);
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{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000388 va_list va;
389 PyObject *args=0, *retval=0;
390 va_start(va, format);
Tim Peterscba30e22003-02-01 06:24:36 +0000391
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000392 if (format) args = Py_VaBuildValue(format, va);
393 va_end(va);
394 if (format && ! args) return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000395 if (stringformat && !(retval=PyString_FromString(stringformat)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000396 return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000397
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000398 if (retval) {
399 if (args) {
400 PyObject *v;
401 v=PyString_Format(retval, args);
402 Py_DECREF(retval);
403 Py_DECREF(args);
404 if (! v) return NULL;
405 retval=v;
406 }
407 }
408 else
409 if (args) retval=args;
410 else {
411 PyErr_SetObject(ErrType,Py_None);
412 return NULL;
413 }
414 PyErr_SetObject(ErrType,retval);
415 Py_DECREF(retval);
416 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000417}
418
Tim Peters84e87f32001-03-17 04:50:51 +0000419static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000420write_file(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000421{
422 size_t nbyteswritten;
Tim Peters84e87f32001-03-17 04:50:51 +0000423
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000424 if (s == NULL) {
425 return 0;
426 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000427
Martin v. Löwis18e16552006-02-15 17:27:45 +0000428 if (n > INT_MAX) {
429 /* String too large */
430 return -1;
431 }
432
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000433 Py_BEGIN_ALLOW_THREADS
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000434 nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000435 Py_END_ALLOW_THREADS
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000436 if (nbyteswritten != (size_t)n) {
437 PyErr_SetFromErrno(PyExc_IOError);
438 return -1;
439 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000440
Martin v. Löwis18e16552006-02-15 17:27:45 +0000441 return (int)n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000442}
443
Tim Peters84e87f32001-03-17 04:50:51 +0000444static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000445write_cStringIO(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000446{
447 if (s == NULL) {
448 return 0;
449 }
Tim Peterscba30e22003-02-01 06:24:36 +0000450
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000451 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
452 return -1;
453 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000454
Martin v. Löwis18e16552006-02-15 17:27:45 +0000455 return (int)n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000456}
457
Tim Peters84e87f32001-03-17 04:50:51 +0000458static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000459write_none(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000460{
461 if (s == NULL) return 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000462 if (n > INT_MAX) return -1;
463 return (int)n;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000464}
465
Tim Peters84e87f32001-03-17 04:50:51 +0000466static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000467write_other(Picklerobject *self, const char *s, Py_ssize_t _n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000468{
469 PyObject *py_str = 0, *junk = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000470 int n;
Tim Peterscba30e22003-02-01 06:24:36 +0000471
Martin v. Löwis18e16552006-02-15 17:27:45 +0000472 if (_n > INT_MAX)
473 return -1;
474 n = (int)_n;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000475 if (s == NULL) {
476 if (!( self->buf_size )) return 0;
Tim Peterscba30e22003-02-01 06:24:36 +0000477 py_str = PyString_FromStringAndSize(self->write_buf,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000478 self->buf_size);
Tim Peterscba30e22003-02-01 06:24:36 +0000479 if (!py_str)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000480 return -1;
481 }
482 else {
483 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
484 if (write_other(self, NULL, 0) < 0)
485 return -1;
486 }
Tim Peterscba30e22003-02-01 06:24:36 +0000487
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000488 if (n > WRITE_BUF_SIZE) {
489 if (!( py_str =
Tim Peterscba30e22003-02-01 06:24:36 +0000490 PyString_FromStringAndSize(s, n)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000491 return -1;
492 }
493 else {
494 memcpy(self->write_buf + self->buf_size, s, n);
495 self->buf_size += n;
496 return n;
497 }
498 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000499
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000500 if (self->write) {
501 /* object with write method */
502 ARG_TUP(self, py_str);
503 if (self->arg) {
504 junk = PyObject_Call(self->write, self->arg, NULL);
505 FREE_ARG_TUP(self);
506 }
507 if (junk) Py_DECREF(junk);
508 else return -1;
509 }
510 else
511 PDATA_PUSH(self->file, py_str, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000512
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000513 self->buf_size = 0;
514 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000515}
516
517
Martin v. Löwis18e16552006-02-15 17:27:45 +0000518static Py_ssize_t
519read_file(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000520{
521 size_t nbytesread;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000522
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000523 if (self->buf_size == 0) {
524 int size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000525
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000526 size = ((n < 32) ? 32 : n);
Tim Petersee1a53c2003-02-02 02:57:53 +0000527 if (!( self->buf = (char *)malloc(size))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000528 PyErr_NoMemory();
529 return -1;
530 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000531
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000532 self->buf_size = size;
533 }
534 else if (n > self->buf_size) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000535 self->buf = (char *)realloc(self->buf, n);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000536 if (!self->buf) {
537 PyErr_NoMemory();
538 return -1;
539 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000540 self->buf_size = n;
541 }
Tim Peters84e87f32001-03-17 04:50:51 +0000542
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000543 Py_BEGIN_ALLOW_THREADS
544 nbytesread = fread(self->buf, sizeof(char), n, self->fp);
545 Py_END_ALLOW_THREADS
546 if (nbytesread != (size_t)n) {
547 if (feof(self->fp)) {
548 PyErr_SetNone(PyExc_EOFError);
549 return -1;
550 }
Tim Peterscba30e22003-02-01 06:24:36 +0000551
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000552 PyErr_SetFromErrno(PyExc_IOError);
553 return -1;
554 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000555
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000556 *s = self->buf;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000557
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000558 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000559}
560
561
Martin v. Löwis18e16552006-02-15 17:27:45 +0000562static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000563readline_file(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000564{
565 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000566
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000567 if (self->buf_size == 0) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000568 if (!( self->buf = (char *)malloc(40))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000569 PyErr_NoMemory();
570 return -1;
571 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000572 self->buf_size = 40;
573 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000574
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000575 i = 0;
576 while (1) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000577 int bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000578 for (; i < (self->buf_size - 1); i++) {
Tim Peterscba30e22003-02-01 06:24:36 +0000579 if (feof(self->fp) ||
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000580 (self->buf[i] = getc(self->fp)) == '\n') {
581 self->buf[i + 1] = '\0';
582 *s = self->buf;
583 return i + 1;
584 }
585 }
Tim Petersee1a53c2003-02-02 02:57:53 +0000586 bigger = self->buf_size << 1;
587 if (bigger <= 0) { /* overflow */
588 PyErr_NoMemory();
589 return -1;
590 }
591 self->buf = (char *)realloc(self->buf, bigger);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000592 if (!self->buf) {
593 PyErr_NoMemory();
594 return -1;
595 }
Tim Petersee1a53c2003-02-02 02:57:53 +0000596 self->buf_size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000597 }
Tim Peters84e87f32001-03-17 04:50:51 +0000598}
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000599
600
Martin v. Löwis18e16552006-02-15 17:27:45 +0000601static Py_ssize_t
602read_cStringIO(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000603{
604 char *ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000605
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000606 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
607 PyErr_SetNone(PyExc_EOFError);
608 return -1;
609 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000610
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000611 *s = ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000612
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000613 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000614}
615
616
Martin v. Löwis18e16552006-02-15 17:27:45 +0000617static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000618readline_cStringIO(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000619{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000620 Py_ssize_t n;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000621 char *ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000622
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000623 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
624 return -1;
625 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000626
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000627 *s = ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000628
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000629 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000630}
631
632
Martin v. Löwis18e16552006-02-15 17:27:45 +0000633static Py_ssize_t
634read_other(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000635{
636 PyObject *bytes, *str=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000637
Martin v. Löwis18e16552006-02-15 17:27:45 +0000638 if (!( bytes = PyInt_FromSsize_t(n))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000639
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000640 ARG_TUP(self, bytes);
641 if (self->arg) {
642 str = PyObject_Call(self->read, self->arg, NULL);
643 FREE_ARG_TUP(self);
644 }
645 if (! str) return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000646
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000647 Py_XDECREF(self->last_string);
648 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000649
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000650 if (! (*s = PyString_AsString(str))) return -1;
651 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000652}
653
654
Martin v. Löwis18e16552006-02-15 17:27:45 +0000655static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000656readline_other(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000657{
658 PyObject *str;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000659 Py_ssize_t str_size;
Tim Peterscba30e22003-02-01 06:24:36 +0000660
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000661 if (!( str = PyObject_CallObject(self->readline, empty_tuple))) {
662 return -1;
663 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000664
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000665 if ((str_size = PyString_Size(str)) < 0)
666 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000667
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000668 Py_XDECREF(self->last_string);
669 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000670
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000671 if (! (*s = PyString_AsString(str)))
672 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000673
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000674 return str_size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000675}
676
Tim Petersee1a53c2003-02-02 02:57:53 +0000677/* Copy the first n bytes from s into newly malloc'ed memory, plus a
678 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
679 * The caller is responsible for free()'ing the return value.
680 */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000681static char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000682pystrndup(const char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000683{
Tim Petersee1a53c2003-02-02 02:57:53 +0000684 char *r = (char *)malloc(n+1);
685 if (r == NULL)
686 return (char*)PyErr_NoMemory();
687 memcpy(r, s, n);
688 r[n] = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000689 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000690}
691
692
693static int
Tim Peterscba30e22003-02-01 06:24:36 +0000694get(Picklerobject *self, PyObject *id)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000695{
696 PyObject *value, *mv;
697 long c_value;
698 char s[30];
699 size_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000700
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000701 if (!( mv = PyDict_GetItem(self->memo, id))) {
702 PyErr_SetObject(PyExc_KeyError, id);
703 return -1;
704 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000705
Tim Peterscba30e22003-02-01 06:24:36 +0000706 if (!( value = PyTuple_GetItem(mv, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000707 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000708
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000709 if (!( PyInt_Check(value))) {
710 PyErr_SetString(PicklingError, "no int where int expected in memo");
711 return -1;
712 }
713 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000714
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000715 if (!self->bin) {
716 s[0] = GET;
717 PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value);
718 len = strlen(s);
719 }
720 else if (Pdata_Check(self->file)) {
721 if (write_other(self, NULL, 0) < 0) return -1;
722 PDATA_APPEND(self->file, mv, -1);
723 return 0;
724 }
725 else {
726 if (c_value < 256) {
727 s[0] = BINGET;
728 s[1] = (int)(c_value & 0xff);
729 len = 2;
730 }
731 else {
732 s[0] = LONG_BINGET;
733 s[1] = (int)(c_value & 0xff);
734 s[2] = (int)((c_value >> 8) & 0xff);
735 s[3] = (int)((c_value >> 16) & 0xff);
736 s[4] = (int)((c_value >> 24) & 0xff);
737 len = 5;
738 }
739 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000740
Tim Peters0bc93f52003-02-02 18:29:33 +0000741 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000742 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000743
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000744 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000745}
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000746
Guido van Rossum60456fd1997-04-09 17:36:32 +0000747
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000748static int
Tim Peterscba30e22003-02-01 06:24:36 +0000749put(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000750{
Tim Peterscba30e22003-02-01 06:24:36 +0000751 if (ob->ob_refcnt < 2 || self->fast)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000752 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000753
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000754 return put2(self, ob);
755}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000756
Guido van Rossum053b8df1998-11-25 16:18:00 +0000757
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000758static int
Tim Peterscba30e22003-02-01 06:24:36 +0000759put2(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000760{
761 char c_str[30];
762 int p;
763 size_t len;
764 int res = -1;
765 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000766
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000767 if (self->fast)
768 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000769
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000770 if ((p = PyDict_Size(self->memo)) < 0)
771 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000772
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000773 /* Make sure memo keys are positive! */
Tim Peters70b02d72003-02-02 17:26:40 +0000774 /* XXX Why?
775 * XXX And does "positive" really mean non-negative?
776 * XXX pickle.py starts with PUT index 0, not 1. This makes for
777 * XXX gratuitous differences between the pickling modules.
778 */
Tim Peterscba30e22003-02-01 06:24:36 +0000779 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000780
Tim Peterscba30e22003-02-01 06:24:36 +0000781 if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000782 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000783
Tim Peterscba30e22003-02-01 06:24:36 +0000784 if (!( memo_len = PyInt_FromLong(p)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000785 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000786
Tim Peterscba30e22003-02-01 06:24:36 +0000787 if (!( t = PyTuple_New(2)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000788 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000789
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000790 PyTuple_SET_ITEM(t, 0, memo_len);
791 Py_INCREF(memo_len);
792 PyTuple_SET_ITEM(t, 1, ob);
793 Py_INCREF(ob);
794
795 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
796 goto finally;
797
798 if (!self->bin) {
799 c_str[0] = PUT;
800 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p);
801 len = strlen(c_str);
802 }
803 else if (Pdata_Check(self->file)) {
804 if (write_other(self, NULL, 0) < 0) return -1;
805 PDATA_APPEND(self->file, memo_len, -1);
806 res=0; /* Job well done ;) */
807 goto finally;
808 }
809 else {
810 if (p >= 256) {
811 c_str[0] = LONG_BINPUT;
812 c_str[1] = (int)(p & 0xff);
813 c_str[2] = (int)((p >> 8) & 0xff);
814 c_str[3] = (int)((p >> 16) & 0xff);
815 c_str[4] = (int)((p >> 24) & 0xff);
816 len = 5;
817 }
818 else {
819 c_str[0] = BINPUT;
820 c_str[1] = p;
821 len = 2;
822 }
823 }
824
Tim Peters0bc93f52003-02-02 18:29:33 +0000825 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000826 goto finally;
827
828 res = 0;
829
830 finally:
831 Py_XDECREF(py_ob_id);
832 Py_XDECREF(memo_len);
833 Py_XDECREF(t);
834
835 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000836}
837
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000838static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000839whichmodule(PyObject *global, PyObject *global_name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000840{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000841 Py_ssize_t i, j;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000842 PyObject *module = 0, *modules_dict = 0,
843 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000844
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000845 module = PyObject_GetAttrString(global, "__module__");
Jeremy Hyltonf00368f2003-06-16 20:19:49 +0000846 if (module)
847 return module;
848 if (PyErr_ExceptionMatches(PyExc_AttributeError))
849 PyErr_Clear();
850 else
851 return NULL;
Guido van Rossum45188231997-09-28 05:38:51 +0000852
Tim Peterscba30e22003-02-01 06:24:36 +0000853 if (!( modules_dict = PySys_GetObject("modules")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000854 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000855
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000856 i = 0;
857 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000858
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000859 if (PyObject_Compare(name, __main___str)==0) continue;
Tim Peters84e87f32001-03-17 04:50:51 +0000860
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000861 global_name_attr = PyObject_GetAttr(module, global_name);
862 if (!global_name_attr) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +0000863 if (PyErr_ExceptionMatches(PyExc_AttributeError))
864 PyErr_Clear();
865 else
866 return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000867 continue;
868 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000869
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000870 if (global_name_attr != global) {
871 Py_DECREF(global_name_attr);
872 continue;
873 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000874
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000875 Py_DECREF(global_name_attr);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000876
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000877 break;
878 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000879
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000880 /* The following implements the rule in pickle.py added in 1.5
881 that used __main__ if no module is found. I don't actually
882 like this rule. jlf
883 */
884 if (!j) {
885 j=1;
886 name=__main___str;
887 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000888
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000889 Py_INCREF(name);
890 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000891}
892
893
Guido van Rossum60456fd1997-04-09 17:36:32 +0000894static int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000895fast_save_enter(Picklerobject *self, PyObject *obj)
896{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000897 /* if fast_container < 0, we're doing an error exit. */
898 if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
899 PyObject *key = NULL;
900 if (self->fast_memo == NULL) {
901 self->fast_memo = PyDict_New();
902 if (self->fast_memo == NULL) {
903 self->fast_container = -1;
904 return 0;
905 }
906 }
907 key = PyLong_FromVoidPtr(obj);
908 if (key == NULL)
909 return 0;
910 if (PyDict_GetItem(self->fast_memo, key)) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000911 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000912 PyErr_Format(PyExc_ValueError,
Tim Peters92c8bb32003-02-13 23:00:26 +0000913 "fast mode: can't pickle cyclic objects "
914 "including object type %s at %p",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000915 obj->ob_type->tp_name, obj);
916 self->fast_container = -1;
917 return 0;
918 }
919 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000920 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000921 self->fast_container = -1;
922 return 0;
923 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000924 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000925 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000926 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000927}
928
Tim Peterscba30e22003-02-01 06:24:36 +0000929int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000930fast_save_leave(Picklerobject *self, PyObject *obj)
931{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000932 if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
933 PyObject *key = PyLong_FromVoidPtr(obj);
934 if (key == NULL)
935 return 0;
936 if (PyDict_DelItem(self->fast_memo, key) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000937 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000938 return 0;
939 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000940 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000941 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000942 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000943}
944
945static int
Tim Peterscba30e22003-02-01 06:24:36 +0000946save_none(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000947{
948 static char none = NONE;
Tim Peters0bc93f52003-02-02 18:29:33 +0000949 if (self->write_func(self, &none, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000950 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000951
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000952 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000953}
954
Guido van Rossum77f6a652002-04-03 22:41:51 +0000955static int
Tim Peterscba30e22003-02-01 06:24:36 +0000956save_bool(Picklerobject *self, PyObject *args)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000957{
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000958 static const char *buf[2] = {FALSE, TRUE};
Guido van Rossume2763392002-04-05 19:30:08 +0000959 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
Guido van Rossum77f6a652002-04-03 22:41:51 +0000960 long l = PyInt_AS_LONG((PyIntObject *)args);
961
Tim Peters3c67d792003-02-02 17:59:11 +0000962 if (self->proto >= 2) {
963 char opcode = l ? NEWTRUE : NEWFALSE;
964 if (self->write_func(self, &opcode, 1) < 0)
965 return -1;
966 }
967 else if (self->write_func(self, buf[l], len[l]) < 0)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000968 return -1;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000969 return 0;
970}
Tim Peters84e87f32001-03-17 04:50:51 +0000971
Guido van Rossum60456fd1997-04-09 17:36:32 +0000972static int
Tim Peterscba30e22003-02-01 06:24:36 +0000973save_int(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000974{
975 char c_str[32];
976 long l = PyInt_AS_LONG((PyIntObject *)args);
977 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000978
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000979 if (!self->bin
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000980#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000981 || l > 0x7fffffffL
982 || l < -0x80000000L
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000983#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000984 ) {
985 /* Text-mode pickle, or long too big to fit in the 4-byte
986 * signed BININT format: store as a string.
987 */
988 c_str[0] = INT;
989 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
Tim Peters0bc93f52003-02-02 18:29:33 +0000990 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000991 return -1;
992 }
993 else {
994 /* Binary pickle and l fits in a signed 4-byte int. */
995 c_str[1] = (int)( l & 0xff);
996 c_str[2] = (int)((l >> 8) & 0xff);
997 c_str[3] = (int)((l >> 16) & 0xff);
998 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000999
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001000 if ((c_str[4] == 0) && (c_str[3] == 0)) {
1001 if (c_str[2] == 0) {
1002 c_str[0] = BININT1;
1003 len = 2;
1004 }
1005 else {
1006 c_str[0] = BININT2;
1007 len = 3;
1008 }
1009 }
1010 else {
1011 c_str[0] = BININT;
1012 len = 5;
1013 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001014
Tim Peters0bc93f52003-02-02 18:29:33 +00001015 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001016 return -1;
1017 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001018
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001019 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001020}
1021
1022
1023static int
Tim Peterscba30e22003-02-01 06:24:36 +00001024save_long(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001025{
Tim Petersee1a53c2003-02-02 02:57:53 +00001026 int size;
1027 int res = -1;
1028 PyObject *repr = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001029
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001030 static char l = LONG;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001031
Tim Petersee1a53c2003-02-02 02:57:53 +00001032 if (self->proto >= 2) {
1033 /* Linear-time pickling. */
1034 size_t nbits;
1035 size_t nbytes;
1036 unsigned char *pdata;
1037 char c_str[5];
1038 int i;
1039 int sign = _PyLong_Sign(args);
1040
1041 if (sign == 0) {
1042 /* It's 0 -- an empty bytestring. */
1043 c_str[0] = LONG1;
1044 c_str[1] = 0;
1045 i = self->write_func(self, c_str, 2);
1046 if (i < 0) goto finally;
1047 res = 0;
1048 goto finally;
1049 }
1050 nbits = _PyLong_NumBits(args);
1051 if (nbits == (size_t)-1 && PyErr_Occurred())
1052 goto finally;
1053 /* How many bytes do we need? There are nbits >> 3 full
1054 * bytes of data, and nbits & 7 leftover bits. If there
1055 * are any leftover bits, then we clearly need another
1056 * byte. Wnat's not so obvious is that we *probably*
1057 * need another byte even if there aren't any leftovers:
1058 * the most-significant bit of the most-significant byte
1059 * acts like a sign bit, and it's usually got a sense
1060 * opposite of the one we need. The exception is longs
1061 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1062 * its own 256's-complement, so has the right sign bit
1063 * even without the extra byte. That's a pain to check
1064 * for in advance, though, so we always grab an extra
1065 * byte at the start, and cut it back later if possible.
1066 */
1067 nbytes = (nbits >> 3) + 1;
1068 if ((int)nbytes < 0 || (size_t)(int)nbytes != nbytes) {
1069 PyErr_SetString(PyExc_OverflowError, "long too large "
1070 "to pickle");
1071 goto finally;
1072 }
1073 repr = PyString_FromStringAndSize(NULL, (int)nbytes);
1074 if (repr == NULL) goto finally;
1075 pdata = (unsigned char *)PyString_AS_STRING(repr);
1076 i = _PyLong_AsByteArray((PyLongObject *)args,
1077 pdata, nbytes,
1078 1 /* little endian */, 1 /* signed */);
1079 if (i < 0) goto finally;
1080 /* If the long is negative, this may be a byte more than
1081 * needed. This is so iff the MSB is all redundant sign
1082 * bits.
1083 */
1084 if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
1085 (pdata[nbytes - 2] & 0x80) != 0)
1086 --nbytes;
1087
1088 if (nbytes < 256) {
1089 c_str[0] = LONG1;
1090 c_str[1] = (char)nbytes;
1091 size = 2;
1092 }
1093 else {
1094 c_str[0] = LONG4;
1095 size = (int)nbytes;
1096 for (i = 1; i < 5; i++) {
1097 c_str[i] = (char)(size & 0xff);
1098 size >>= 8;
1099 }
1100 size = 5;
1101 }
1102 i = self->write_func(self, c_str, size);
1103 if (i < 0) goto finally;
1104 i = self->write_func(self, (char *)pdata, (int)nbytes);
1105 if (i < 0) goto finally;
1106 res = 0;
1107 goto finally;
1108 }
1109
1110 /* proto < 2: write the repr and newline. This is quadratic-time
1111 * (in the number of digits), in both directions.
1112 */
Tim Peterscba30e22003-02-01 06:24:36 +00001113 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001114 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001115
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001116 if ((size = PyString_Size(repr)) < 0)
1117 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001118
Tim Peters0bc93f52003-02-02 18:29:33 +00001119 if (self->write_func(self, &l, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001120 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001121
Tim Peters0bc93f52003-02-02 18:29:33 +00001122 if (self->write_func(self,
1123 PyString_AS_STRING((PyStringObject *)repr),
1124 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001125 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001126
Tim Peters0bc93f52003-02-02 18:29:33 +00001127 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001128 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001129
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001130 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001131
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001132 finally:
1133 Py_XDECREF(repr);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001134 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001135}
1136
1137
1138static int
Tim Peterscba30e22003-02-01 06:24:36 +00001139save_float(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001140{
1141 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001142
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001143 if (self->bin) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001144 char str[9];
Tim Peters9905b942003-03-20 20:53:32 +00001145 str[0] = BINFLOAT;
1146 if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001147 return -1;
Tim Peters0bc93f52003-02-02 18:29:33 +00001148 if (self->write_func(self, str, 9) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001149 return -1;
1150 }
1151 else {
1152 char c_str[250];
1153 c_str[0] = FLOAT;
1154 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%.17g\n", x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001155
Tim Peters0bc93f52003-02-02 18:29:33 +00001156 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001157 return -1;
1158 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001159
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001160 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001161}
1162
1163
1164static int
Tim Peterscba30e22003-02-01 06:24:36 +00001165save_string(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001166{
1167 int size, len;
1168 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001169
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001170 if ((size = PyString_Size(args)) < 0)
1171 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001172
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001173 if (!self->bin) {
1174 char *repr_str;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001175
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001176 static char string = STRING;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001177
Tim Peterscba30e22003-02-01 06:24:36 +00001178 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001179 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001180
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001181 if ((len = PyString_Size(repr)) < 0)
1182 goto err;
1183 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001184
Tim Peters0bc93f52003-02-02 18:29:33 +00001185 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001186 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001187
Tim Peters0bc93f52003-02-02 18:29:33 +00001188 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001189 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001190
Tim Peters0bc93f52003-02-02 18:29:33 +00001191 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001192 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001193
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001194 Py_XDECREF(repr);
1195 }
1196 else {
1197 int i;
1198 char c_str[5];
Guido van Rossum60456fd1997-04-09 17:36:32 +00001199
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001200 if ((size = PyString_Size(args)) < 0)
1201 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001202
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001203 if (size < 256) {
1204 c_str[0] = SHORT_BINSTRING;
1205 c_str[1] = size;
1206 len = 2;
1207 }
1208 else {
1209 c_str[0] = BINSTRING;
1210 for (i = 1; i < 5; i++)
1211 c_str[i] = (int)(size >> ((i - 1) * 8));
1212 len = 5;
1213 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001214
Tim Peters0bc93f52003-02-02 18:29:33 +00001215 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001216 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001217
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001218 if (size > 128 && Pdata_Check(self->file)) {
1219 if (write_other(self, NULL, 0) < 0) return -1;
1220 PDATA_APPEND(self->file, args, -1);
1221 }
1222 else {
Tim Peters0bc93f52003-02-02 18:29:33 +00001223 if (self->write_func(self,
1224 PyString_AS_STRING(
1225 (PyStringObject *)args),
1226 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001227 return -1;
1228 }
1229 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001230
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001231 if (doput)
1232 if (put(self, args) < 0)
1233 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001234
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001235 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001236
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001237 err:
1238 Py_XDECREF(repr);
1239 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001240}
1241
1242
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001243#ifdef Py_USING_UNICODE
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001244/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1245 backslash and newline characters to \uXXXX escapes. */
1246static PyObject *
1247modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, int size)
1248{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001249 PyObject *repr;
1250 char *p;
1251 char *q;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001252
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001253 static const char *hexdigit = "0123456789ABCDEF";
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001254
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001255 repr = PyString_FromStringAndSize(NULL, 6 * size);
1256 if (repr == NULL)
1257 return NULL;
1258 if (size == 0)
1259 return repr;
1260
1261 p = q = PyString_AS_STRING(repr);
1262 while (size-- > 0) {
1263 Py_UNICODE ch = *s++;
1264 /* Map 16-bit characters to '\uxxxx' */
1265 if (ch >= 256 || ch == '\\' || ch == '\n') {
1266 *p++ = '\\';
1267 *p++ = 'u';
1268 *p++ = hexdigit[(ch >> 12) & 0xf];
1269 *p++ = hexdigit[(ch >> 8) & 0xf];
1270 *p++ = hexdigit[(ch >> 4) & 0xf];
1271 *p++ = hexdigit[ch & 15];
1272 }
1273 /* Copy everything else as-is */
1274 else
1275 *p++ = (char) ch;
1276 }
1277 *p = '\0';
Tim Peters5de98422002-04-27 18:44:32 +00001278 _PyString_Resize(&repr, p - q);
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001279 return repr;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001280}
1281
1282
Guido van Rossum60456fd1997-04-09 17:36:32 +00001283static int
Tim Peterscba30e22003-02-01 06:24:36 +00001284save_unicode(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001285{
1286 int size, len;
1287 PyObject *repr=0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001288
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001289 if (!PyUnicode_Check(args))
1290 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001291
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001292 if (!self->bin) {
1293 char *repr_str;
1294 static char string = UNICODE;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001295
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001296 repr = modified_EncodeRawUnicodeEscape(
1297 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
Tim Peterscba30e22003-02-01 06:24:36 +00001298 if (!repr)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001299 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001300
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001301 if ((len = PyString_Size(repr)) < 0)
1302 goto err;
1303 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001304
Tim Peters0bc93f52003-02-02 18:29:33 +00001305 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001306 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001307
Tim Peters0bc93f52003-02-02 18:29:33 +00001308 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001309 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001310
Tim Peters0bc93f52003-02-02 18:29:33 +00001311 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001312 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001313
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001314 Py_XDECREF(repr);
1315 }
1316 else {
1317 int i;
1318 char c_str[5];
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001319
Tim Peterscba30e22003-02-01 06:24:36 +00001320 if (!( repr = PyUnicode_AsUTF8String(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001321 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001322
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001323 if ((size = PyString_Size(repr)) < 0)
1324 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001325
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001326 c_str[0] = BINUNICODE;
1327 for (i = 1; i < 5; i++)
1328 c_str[i] = (int)(size >> ((i - 1) * 8));
1329 len = 5;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001330
Tim Peters0bc93f52003-02-02 18:29:33 +00001331 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001332 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001333
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001334 if (size > 128 && Pdata_Check(self->file)) {
1335 if (write_other(self, NULL, 0) < 0)
1336 goto err;
1337 PDATA_APPEND(self->file, repr, -1);
1338 }
1339 else {
Tim Peters0bc93f52003-02-02 18:29:33 +00001340 if (self->write_func(self, PyString_AS_STRING(repr),
1341 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001342 goto err;
1343 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001344
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001345 Py_DECREF(repr);
1346 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001347
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001348 if (doput)
1349 if (put(self, args) < 0)
1350 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001351
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001352 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001353
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001354 err:
1355 Py_XDECREF(repr);
1356 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001357}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001358#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001359
Tim Peters1d63c9f2003-02-02 20:29:39 +00001360/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1361static int
Tim Peters67920142003-02-05 03:46:17 +00001362store_tuple_elements(Picklerobject *self, PyObject *t, int len)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001363{
1364 int i;
1365 int res = -1; /* guilty until proved innocent */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001366
Tim Peters1d63c9f2003-02-02 20:29:39 +00001367 assert(PyTuple_Size(t) == len);
1368
1369 for (i = 0; i < len; i++) {
1370 PyObject *element = PyTuple_GET_ITEM(t, i);
1371
1372 if (element == NULL)
1373 goto finally;
1374 if (save(self, element, 0) < 0)
1375 goto finally;
1376 }
1377 res = 0;
1378
1379 finally:
1380 return res;
1381}
1382
1383/* Tuples are ubiquitous in the pickle protocols, so many techniques are
1384 * used across protocols to minimize the space needed to pickle them.
Tim Peters67920142003-02-05 03:46:17 +00001385 * Tuples are also the only builtin immutable type that can be recursive
Tim Peters1d63c9f2003-02-02 20:29:39 +00001386 * (a tuple can be reached from itself), and that requires some subtle
1387 * magic so that it works in all cases. IOW, this is a long routine.
1388 */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001389static int
Tim Peterscba30e22003-02-01 06:24:36 +00001390save_tuple(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001391{
Tim Peters1d63c9f2003-02-02 20:29:39 +00001392 PyObject *py_tuple_id = NULL;
1393 int len, i;
1394 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001395
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001396 static char tuple = TUPLE;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001397 static char pop = POP;
1398 static char pop_mark = POP_MARK;
1399 static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
Guido van Rossum60456fd1997-04-09 17:36:32 +00001400
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001401 if ((len = PyTuple_Size(args)) < 0)
1402 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001403
Tim Peters1d63c9f2003-02-02 20:29:39 +00001404 if (len == 0) {
1405 char c_str[2];
Tim Peters84e87f32001-03-17 04:50:51 +00001406
Tim Peters1d63c9f2003-02-02 20:29:39 +00001407 if (self->proto) {
1408 c_str[0] = EMPTY_TUPLE;
1409 len = 1;
1410 }
1411 else {
1412 c_str[0] = MARK;
1413 c_str[1] = TUPLE;
1414 len = 2;
1415 }
1416 if (self->write_func(self, c_str, len) >= 0)
1417 res = 0;
1418 /* Don't memoize an empty tuple. */
1419 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001420 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001421
Tim Peters1d63c9f2003-02-02 20:29:39 +00001422 /* A non-empty tuple. */
1423
1424 /* id(tuple) isn't in the memo now. If it shows up there after
1425 * saving the tuple elements, the tuple must be recursive, in
1426 * which case we'll pop everything we put on the stack, and fetch
1427 * its value from the memo.
1428 */
1429 py_tuple_id = PyLong_FromVoidPtr(args);
1430 if (py_tuple_id == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001431 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001432
Tim Peters1d63c9f2003-02-02 20:29:39 +00001433 if (len <= 3 && self->proto >= 2) {
1434 /* Use TUPLE{1,2,3} opcodes. */
Tim Peters67920142003-02-05 03:46:17 +00001435 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001436 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001437 if (PyDict_GetItem(self->memo, py_tuple_id)) {
Tim Peters1d63c9f2003-02-02 20:29:39 +00001438 /* pop the len elements */
1439 for (i = 0; i < len; ++i)
1440 if (self->write_func(self, &pop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001441 goto finally;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001442 /* fetch from memo */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001443 if (get(self, py_tuple_id) < 0)
1444 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001445 res = 0;
1446 goto finally;
1447 }
Tim Peters1d63c9f2003-02-02 20:29:39 +00001448 /* Not recursive. */
1449 if (self->write_func(self, len2opcode + len, 1) < 0)
1450 goto finally;
1451 goto memoize;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001452 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001453
Tim Peters1d63c9f2003-02-02 20:29:39 +00001454 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1455 * Generate MARK elt1 elt2 ... TUPLE
1456 */
1457 if (self->write_func(self, &MARKv, 1) < 0)
1458 goto finally;
1459
Tim Peters67920142003-02-05 03:46:17 +00001460 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001461 goto finally;
1462
1463 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1464 /* pop the stack stuff we pushed */
1465 if (self->bin) {
1466 if (self->write_func(self, &pop_mark, 1) < 0)
1467 goto finally;
1468 }
1469 else {
1470 /* Note that we pop one more than len, to remove
1471 * the MARK too.
1472 */
1473 for (i = 0; i <= len; i++)
1474 if (self->write_func(self, &pop, 1) < 0)
1475 goto finally;
1476 }
1477 /* fetch from memo */
1478 if (get(self, py_tuple_id) >= 0)
1479 res = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001480 goto finally;
1481 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001482
Tim Peters1d63c9f2003-02-02 20:29:39 +00001483 /* Not recursive. */
1484 if (self->write_func(self, &tuple, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001485 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001486
Tim Peters1d63c9f2003-02-02 20:29:39 +00001487 memoize:
1488 if (put(self, args) >= 0)
1489 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001490
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001491 finally:
1492 Py_XDECREF(py_tuple_id);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001493 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001494}
1495
Tim Peters1092d642003-02-11 21:06:20 +00001496/* iter is an iterator giving items, and we batch up chunks of
1497 * MARK item item ... item APPENDS
1498 * opcode sequences. Calling code should have arranged to first create an
1499 * empty list, or list-like object, for the APPENDS to operate on.
1500 * Returns 0 on success, <0 on error.
1501 */
1502static int
1503batch_list(Picklerobject *self, PyObject *iter)
1504{
1505 PyObject *obj;
1506 PyObject *slice[BATCHSIZE];
1507 int i, n;
1508
1509 static char append = APPEND;
1510 static char appends = APPENDS;
1511
1512 assert(iter != NULL);
1513
1514 if (self->proto == 0) {
1515 /* APPENDS isn't available; do one at a time. */
1516 for (;;) {
1517 obj = PyIter_Next(iter);
1518 if (obj == NULL) {
1519 if (PyErr_Occurred())
1520 return -1;
1521 break;
1522 }
1523 i = save(self, obj, 0);
1524 Py_DECREF(obj);
1525 if (i < 0)
1526 return -1;
1527 if (self->write_func(self, &append, 1) < 0)
1528 return -1;
Tim Peters1092d642003-02-11 21:06:20 +00001529 }
1530 return 0;
1531 }
1532
1533 /* proto > 0: write in batches of BATCHSIZE. */
1534 do {
1535 /* Get next group of (no more than) BATCHSIZE elements. */
1536 for (n = 0; n < BATCHSIZE; ++n) {
1537 obj = PyIter_Next(iter);
1538 if (obj == NULL) {
1539 if (PyErr_Occurred())
1540 goto BatchFailed;
1541 break;
1542 }
1543 slice[n] = obj;
1544 }
1545
1546 if (n > 1) {
1547 /* Pump out MARK, slice[0:n], APPENDS. */
1548 if (self->write_func(self, &MARKv, 1) < 0)
1549 goto BatchFailed;
1550 for (i = 0; i < n; ++i) {
1551 if (save(self, slice[i], 0) < 0)
1552 goto BatchFailed;
1553 }
1554 if (self->write_func(self, &appends, 1) < 0)
1555 goto BatchFailed;
1556 }
1557 else if (n == 1) {
1558 if (save(self, slice[0], 0) < 0)
1559 goto BatchFailed;
1560 if (self->write_func(self, &append, 1) < 0)
1561 goto BatchFailed;
1562 }
1563
1564 for (i = 0; i < n; ++i) {
1565 Py_DECREF(slice[i]);
1566 }
Tim Peters90975f12003-02-12 05:28:58 +00001567 } while (n == BATCHSIZE);
Tim Peters1092d642003-02-11 21:06:20 +00001568 return 0;
1569
1570BatchFailed:
1571 while (--n >= 0) {
1572 Py_DECREF(slice[n]);
1573 }
1574 return -1;
1575}
1576
Guido van Rossum60456fd1997-04-09 17:36:32 +00001577static int
Tim Peterscba30e22003-02-01 06:24:36 +00001578save_list(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001579{
Tim Peters1092d642003-02-11 21:06:20 +00001580 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001581 char s[3];
Tim Peters1092d642003-02-11 21:06:20 +00001582 int len;
1583 PyObject *iter;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001584
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001585 if (self->fast && !fast_save_enter(self, args))
1586 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001587
Tim Peters1092d642003-02-11 21:06:20 +00001588 /* Create an empty list. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001589 if (self->bin) {
1590 s[0] = EMPTY_LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001591 len = 1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001592 }
1593 else {
1594 s[0] = MARK;
1595 s[1] = LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001596 len = 2;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001597 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001598
Tim Peters1092d642003-02-11 21:06:20 +00001599 if (self->write_func(self, s, len) < 0)
1600 goto finally;
1601
1602 /* Get list length, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001603 if ((len = PyList_Size(args)) < 0)
1604 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001605
Tim Peters1092d642003-02-11 21:06:20 +00001606 /* Memoize. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001607 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001608 if (put(self, args) >= 0)
1609 res = 0;
1610 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001611 }
Tim Peters90975f12003-02-12 05:28:58 +00001612 if (put2(self, args) < 0)
1613 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001614
Tim Peters1092d642003-02-11 21:06:20 +00001615 /* Materialize the list elements. */
1616 iter = PyObject_GetIter(args);
1617 if (iter == NULL)
1618 goto finally;
1619 res = batch_list(self, iter);
1620 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001621
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001622 finally:
1623 if (self->fast && !fast_save_leave(self, args))
1624 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001625
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001626 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001627}
1628
1629
Tim Peters42f08ac2003-02-11 22:43:24 +00001630/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1631 * MARK key value ... key value SETITEMS
1632 * opcode sequences. Calling code should have arranged to first create an
1633 * empty dict, or dict-like object, for the SETITEMS to operate on.
1634 * Returns 0 on success, <0 on error.
1635 *
1636 * This is very much like batch_list(). The difference between saving
1637 * elements directly, and picking apart two-tuples, is so long-winded at
1638 * the C level, though, that attempts to combine these routines were too
1639 * ugly to bear.
1640 */
1641static int
1642batch_dict(Picklerobject *self, PyObject *iter)
1643{
1644 PyObject *p;
1645 PyObject *slice[BATCHSIZE];
1646 int i, n;
1647
1648 static char setitem = SETITEM;
1649 static char setitems = SETITEMS;
1650
1651 assert(iter != NULL);
1652
1653 if (self->proto == 0) {
1654 /* SETITEMS isn't available; do one at a time. */
1655 for (;;) {
1656 p = PyIter_Next(iter);
1657 if (p == NULL) {
1658 if (PyErr_Occurred())
1659 return -1;
1660 break;
1661 }
1662 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1663 PyErr_SetString(PyExc_TypeError, "dict items "
1664 "iterator must return 2-tuples");
1665 return -1;
1666 }
1667 i = save(self, PyTuple_GET_ITEM(p, 0), 0);
1668 if (i >= 0)
1669 i = save(self, PyTuple_GET_ITEM(p, 1), 0);
1670 Py_DECREF(p);
1671 if (i < 0)
1672 return -1;
1673 if (self->write_func(self, &setitem, 1) < 0)
1674 return -1;
Tim Peters42f08ac2003-02-11 22:43:24 +00001675 }
1676 return 0;
1677 }
1678
1679 /* proto > 0: write in batches of BATCHSIZE. */
1680 do {
1681 /* Get next group of (no more than) BATCHSIZE elements. */
1682 for (n = 0; n < BATCHSIZE; ++n) {
1683 p = PyIter_Next(iter);
1684 if (p == NULL) {
1685 if (PyErr_Occurred())
1686 goto BatchFailed;
1687 break;
1688 }
1689 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1690 PyErr_SetString(PyExc_TypeError, "dict items "
1691 "iterator must return 2-tuples");
1692 goto BatchFailed;
1693 }
1694 slice[n] = p;
1695 }
1696
1697 if (n > 1) {
1698 /* Pump out MARK, slice[0:n], SETITEMS. */
1699 if (self->write_func(self, &MARKv, 1) < 0)
1700 goto BatchFailed;
1701 for (i = 0; i < n; ++i) {
1702 p = slice[i];
1703 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1704 goto BatchFailed;
1705 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1706 goto BatchFailed;
1707 }
1708 if (self->write_func(self, &setitems, 1) < 0)
1709 goto BatchFailed;
1710 }
1711 else if (n == 1) {
1712 p = slice[0];
1713 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1714 goto BatchFailed;
1715 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1716 goto BatchFailed;
1717 if (self->write_func(self, &setitem, 1) < 0)
1718 goto BatchFailed;
1719 }
1720
1721 for (i = 0; i < n; ++i) {
1722 Py_DECREF(slice[i]);
1723 }
Tim Peters90975f12003-02-12 05:28:58 +00001724 } while (n == BATCHSIZE);
Tim Peters42f08ac2003-02-11 22:43:24 +00001725 return 0;
1726
1727BatchFailed:
1728 while (--n >= 0) {
1729 Py_DECREF(slice[n]);
1730 }
1731 return -1;
1732}
1733
Guido van Rossum60456fd1997-04-09 17:36:32 +00001734static int
Tim Peterscba30e22003-02-01 06:24:36 +00001735save_dict(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001736{
Tim Peters42f08ac2003-02-11 22:43:24 +00001737 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001738 char s[3];
Tim Peters42f08ac2003-02-11 22:43:24 +00001739 int len;
1740 PyObject *iter;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001741
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001742 if (self->fast && !fast_save_enter(self, args))
1743 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001744
Tim Peters42f08ac2003-02-11 22:43:24 +00001745 /* Create an empty dict. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001746 if (self->bin) {
1747 s[0] = EMPTY_DICT;
1748 len = 1;
1749 }
1750 else {
1751 s[0] = MARK;
1752 s[1] = DICT;
1753 len = 2;
1754 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001755
Tim Peters0bc93f52003-02-02 18:29:33 +00001756 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001757 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001758
Tim Peters42f08ac2003-02-11 22:43:24 +00001759 /* Get dict size, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001760 if ((len = PyDict_Size(args)) < 0)
1761 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001762
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001763 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001764 if (put(self, args) >= 0)
1765 res = 0;
1766 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001767 }
Tim Peters90975f12003-02-12 05:28:58 +00001768 if (put2(self, args) < 0)
1769 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001770
Tim Peters42f08ac2003-02-11 22:43:24 +00001771 /* Materialize the dict items. */
1772 iter = PyObject_CallMethod(args, "iteritems", "()");
1773 if (iter == NULL)
1774 goto finally;
1775 res = batch_dict(self, iter);
1776 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001777
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001778 finally:
1779 if (self->fast && !fast_save_leave(self, args))
1780 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001781
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001782 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001783}
1784
1785
Tim Peters84e87f32001-03-17 04:50:51 +00001786static int
Tim Peterscba30e22003-02-01 06:24:36 +00001787save_inst(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001788{
1789 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1790 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1791 char *module_str, *name_str;
1792 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001793
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001794 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001795
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001796 if (self->fast && !fast_save_enter(self, args))
1797 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001798
Tim Peters0bc93f52003-02-02 18:29:33 +00001799 if (self->write_func(self, &MARKv, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001800 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001801
Tim Peterscba30e22003-02-01 06:24:36 +00001802 if (!( class = PyObject_GetAttr(args, __class___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001803 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001804
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001805 if (self->bin) {
1806 if (save(self, class, 0) < 0)
1807 goto finally;
1808 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001809
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001810 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
1811 PyObject *element = 0;
1812 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001813
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001814 if (!( class_args =
Tim Peterscba30e22003-02-01 06:24:36 +00001815 PyObject_Call(getinitargs_func, empty_tuple, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001816 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001817
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001818 if ((len = PyObject_Size(class_args)) < 0)
1819 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001820
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001821 for (i = 0; i < len; i++) {
Tim Peterscba30e22003-02-01 06:24:36 +00001822 if (!( element = PySequence_GetItem(class_args, i)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001823 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001824
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001825 if (save(self, element, 0) < 0) {
1826 Py_DECREF(element);
1827 goto finally;
1828 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001829
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001830 Py_DECREF(element);
1831 }
1832 }
1833 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00001834 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1835 PyErr_Clear();
1836 else
1837 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001838 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001839
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001840 if (!self->bin) {
1841 if (!( name = ((PyClassObject *)class)->cl_name )) {
1842 PyErr_SetString(PicklingError, "class has no name");
1843 goto finally;
1844 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001845
Tim Peterscba30e22003-02-01 06:24:36 +00001846 if (!( module = whichmodule(class, name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001847 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001848
Tim Peters84e87f32001-03-17 04:50:51 +00001849
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001850 if ((module_size = PyString_Size(module)) < 0 ||
1851 (name_size = PyString_Size(name)) < 0)
1852 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001853
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001854 module_str = PyString_AS_STRING((PyStringObject *)module);
1855 name_str = PyString_AS_STRING((PyStringObject *)name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001856
Tim Peters0bc93f52003-02-02 18:29:33 +00001857 if (self->write_func(self, &inst, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001858 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001859
Tim Peters0bc93f52003-02-02 18:29:33 +00001860 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001861 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001862
Tim Peters0bc93f52003-02-02 18:29:33 +00001863 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001864 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001865
Tim Peters0bc93f52003-02-02 18:29:33 +00001866 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001867 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001868
Tim Peters0bc93f52003-02-02 18:29:33 +00001869 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001870 goto finally;
1871 }
Tim Peters0bc93f52003-02-02 18:29:33 +00001872 else if (self->write_func(self, &obj, 1) < 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001873 goto finally;
1874 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001875
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001876 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
1877 state = PyObject_Call(getstate_func, empty_tuple, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00001878 if (!state)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001879 goto finally;
1880 }
1881 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00001882 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1883 PyErr_Clear();
1884 else
1885 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001886
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001887 if (!( state = PyObject_GetAttr(args, __dict___str))) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00001888 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1889 PyErr_Clear();
1890 else
1891 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001892 res = 0;
1893 goto finally;
1894 }
1895 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001896
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001897 if (!PyDict_Check(state)) {
1898 if (put2(self, args) < 0)
1899 goto finally;
1900 }
1901 else {
1902 if (put(self, args) < 0)
1903 goto finally;
1904 }
Tim Peters84e87f32001-03-17 04:50:51 +00001905
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001906 if (save(self, state, 0) < 0)
1907 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001908
Tim Peters0bc93f52003-02-02 18:29:33 +00001909 if (self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001910 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001911
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001912 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001913
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001914 finally:
1915 if (self->fast && !fast_save_leave(self, args))
1916 res = -1;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001917
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001918 Py_XDECREF(module);
1919 Py_XDECREF(class);
1920 Py_XDECREF(state);
1921 Py_XDECREF(getinitargs_func);
1922 Py_XDECREF(getstate_func);
1923 Py_XDECREF(class_args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001924
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001925 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001926}
1927
1928
Guido van Rossum60456fd1997-04-09 17:36:32 +00001929static int
Tim Peterscba30e22003-02-01 06:24:36 +00001930save_global(Picklerobject *self, PyObject *args, PyObject *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001931{
Jeremy Hylton9ee91f12002-07-11 22:02:33 +00001932 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001933 char *name_str, *module_str;
1934 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001935
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001936 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001937
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001938 if (name) {
1939 global_name = name;
1940 Py_INCREF(global_name);
1941 }
1942 else {
Tim Peterscba30e22003-02-01 06:24:36 +00001943 if (!( global_name = PyObject_GetAttr(args, __name___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001944 goto finally;
1945 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001946
Tim Peterscba30e22003-02-01 06:24:36 +00001947 if (!( module = whichmodule(args, global_name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001948 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001949
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001950 if ((module_size = PyString_Size(module)) < 0 ||
1951 (name_size = PyString_Size(global_name)) < 0)
1952 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001953
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001954 module_str = PyString_AS_STRING((PyStringObject *)module);
1955 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001956
Guido van Rossum75bfd052002-12-24 18:10:07 +00001957 /* XXX This can be doing a relative import. Clearly it shouldn't,
1958 but I don't know how to stop it. :-( */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001959 mod = PyImport_ImportModule(module_str);
1960 if (mod == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001961 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00001962 "Can't pickle %s: import of module %s "
1963 "failed",
1964 "OS", args, module);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001965 goto finally;
1966 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001967 klass = PyObject_GetAttrString(mod, name_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001968 if (klass == NULL) {
1969 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00001970 "Can't pickle %s: attribute lookup %s.%s "
1971 "failed",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001972 "OSS", args, module, global_name);
1973 goto finally;
1974 }
1975 if (klass != args) {
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001976 Py_DECREF(klass);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001977 cPickle_ErrFormat(PicklingError,
Tim Peters731098b2003-02-04 20:56:09 +00001978 "Can't pickle %s: it's not the same object "
1979 "as %s.%s",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001980 "OSS", args, module, global_name);
1981 goto finally;
1982 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001983 Py_DECREF(klass);
Guido van Rossuma92d16a2001-08-18 21:22:07 +00001984
Tim Peters731098b2003-02-04 20:56:09 +00001985 if (self->proto >= 2) {
1986 /* See whether this is in the extension registry, and if
1987 * so generate an EXT opcode.
1988 */
1989 PyObject *py_code; /* extension code as Python object */
Tim Peters3e667d52003-02-04 21:47:44 +00001990 long code; /* extension code as C value */
Tim Peters731098b2003-02-04 20:56:09 +00001991 char c_str[5];
1992 int n;
1993
1994 PyTuple_SET_ITEM(two_tuple, 0, module);
1995 PyTuple_SET_ITEM(two_tuple, 1, global_name);
1996 py_code = PyDict_GetItem(extension_registry, two_tuple);
1997 if (py_code == NULL)
1998 goto gen_global; /* not registered */
1999
2000 /* Verify py_code has the right type and value. */
2001 if (!PyInt_Check(py_code)) {
2002 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
Tim Peters6288e232003-02-05 03:53:10 +00002003 "extension code %s isn't an integer",
Tim Peters731098b2003-02-04 20:56:09 +00002004 "OO", args, py_code);
2005 goto finally;
2006 }
2007 code = PyInt_AS_LONG(py_code);
2008 if (code <= 0 || code > 0x7fffffffL) {
2009 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2010 "extension code %ld is out of range",
2011 "Ol", args, code);
2012 goto finally;
2013 }
2014
2015 /* Generate an EXT opcode. */
2016 if (code <= 0xff) {
2017 c_str[0] = EXT1;
2018 c_str[1] = (char)code;
2019 n = 2;
2020 }
2021 else if (code <= 0xffff) {
2022 c_str[0] = EXT2;
2023 c_str[1] = (char)(code & 0xff);
2024 c_str[2] = (char)((code >> 8) & 0xff);
2025 n = 3;
2026 }
2027 else {
2028 c_str[0] = EXT4;
2029 c_str[1] = (char)(code & 0xff);
2030 c_str[2] = (char)((code >> 8) & 0xff);
2031 c_str[3] = (char)((code >> 16) & 0xff);
2032 c_str[4] = (char)((code >> 24) & 0xff);
2033 n = 5;
2034 }
2035
2036 if (self->write_func(self, c_str, n) >= 0)
2037 res = 0;
2038 goto finally; /* and don't memoize */
2039 }
2040
2041 gen_global:
Tim Peters0bc93f52003-02-02 18:29:33 +00002042 if (self->write_func(self, &global, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002043 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002044
Tim Peters0bc93f52003-02-02 18:29:33 +00002045 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002046 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002047
Tim Peters0bc93f52003-02-02 18:29:33 +00002048 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002049 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002050
Tim Peters0bc93f52003-02-02 18:29:33 +00002051 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002052 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002053
Tim Peters0bc93f52003-02-02 18:29:33 +00002054 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002055 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002056
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002057 if (put(self, args) < 0)
2058 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002059
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002060 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002061
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002062 finally:
2063 Py_XDECREF(module);
2064 Py_XDECREF(global_name);
2065 Py_XDECREF(mod);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002066
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002067 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002068}
2069
Guido van Rossum60456fd1997-04-09 17:36:32 +00002070static int
Tim Peterscba30e22003-02-01 06:24:36 +00002071save_pers(Picklerobject *self, PyObject *args, PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002072{
2073 PyObject *pid = 0;
2074 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002075
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002076 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002077
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002078 Py_INCREF(args);
2079 ARG_TUP(self, args);
2080 if (self->arg) {
2081 pid = PyObject_Call(f, self->arg, NULL);
2082 FREE_ARG_TUP(self);
2083 }
2084 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002085
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002086 if (pid != Py_None) {
2087 if (!self->bin) {
2088 if (!PyString_Check(pid)) {
2089 PyErr_SetString(PicklingError,
2090 "persistent id must be string");
2091 goto finally;
2092 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002093
Tim Peters0bc93f52003-02-02 18:29:33 +00002094 if (self->write_func(self, &persid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002095 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002096
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002097 if ((size = PyString_Size(pid)) < 0)
2098 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002099
Tim Peters0bc93f52003-02-02 18:29:33 +00002100 if (self->write_func(self,
2101 PyString_AS_STRING(
2102 (PyStringObject *)pid),
2103 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002104 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002105
Tim Peters0bc93f52003-02-02 18:29:33 +00002106 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002107 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002108
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002109 res = 1;
2110 goto finally;
2111 }
2112 else if (save(self, pid, 1) >= 0) {
Tim Peters0bc93f52003-02-02 18:29:33 +00002113 if (self->write_func(self, &binpersid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002114 res = -1;
2115 else
2116 res = 1;
2117 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002118
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002119 goto finally;
2120 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002121
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002122 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002123
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002124 finally:
2125 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002126
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002127 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002128}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002129
Tim Peters71fcda52003-02-14 23:05:28 +00002130/* We're saving ob, and args is the 2-thru-5 tuple returned by the
2131 * appropriate __reduce__ method for ob.
2132 */
Tim Peters84e87f32001-03-17 04:50:51 +00002133static int
Tim Peters71fcda52003-02-14 23:05:28 +00002134save_reduce(Picklerobject *self, PyObject *args, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002135{
Tim Peters71fcda52003-02-14 23:05:28 +00002136 PyObject *callable;
2137 PyObject *argtup;
2138 PyObject *state = NULL;
2139 PyObject *listitems = NULL;
2140 PyObject *dictitems = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002141
Tim Peters71fcda52003-02-14 23:05:28 +00002142 int use_newobj = self->proto >= 2;
2143
2144 static char reduce = REDUCE;
2145 static char build = BUILD;
2146 static char newobj = NEWOBJ;
2147
2148 if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2149 &callable,
2150 &argtup,
2151 &state,
2152 &listitems,
2153 &dictitems))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002154 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002155
Raymond Hettingera6b45cc2004-12-07 07:05:57 +00002156 if (!PyTuple_Check(argtup)) {
2157 PyErr_SetString(PicklingError,
2158 "args from reduce() should be a tuple");
2159 return -1;
2160 }
2161
Tim Peters71fcda52003-02-14 23:05:28 +00002162 if (state == Py_None)
2163 state = NULL;
2164 if (listitems == Py_None)
2165 listitems = NULL;
2166 if (dictitems == Py_None)
2167 dictitems = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002168
Tim Peters71fcda52003-02-14 23:05:28 +00002169 /* Protocol 2 special case: if callable's name is __newobj__, use
2170 * NEWOBJ. This consumes a lot of code.
2171 */
2172 if (use_newobj) {
2173 PyObject *temp = PyObject_GetAttr(callable, __name___str);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002174
Tim Peters71fcda52003-02-14 23:05:28 +00002175 if (temp == NULL) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002176 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2177 PyErr_Clear();
2178 else
2179 return -1;
Tim Peters71fcda52003-02-14 23:05:28 +00002180 use_newobj = 0;
2181 }
2182 else {
2183 use_newobj = PyString_Check(temp) &&
2184 strcmp(PyString_AS_STRING(temp),
2185 "__newobj__") == 0;
2186 Py_DECREF(temp);
2187 }
2188 }
2189 if (use_newobj) {
2190 PyObject *cls;
2191 PyObject *newargtup;
2192 int n, i;
2193
2194 /* Sanity checks. */
2195 n = PyTuple_Size(argtup);
2196 if (n < 1) {
2197 PyErr_SetString(PicklingError, "__newobj__ arglist "
2198 "is empty");
2199 return -1;
2200 }
2201
2202 cls = PyTuple_GET_ITEM(argtup, 0);
2203 if (! PyObject_HasAttrString(cls, "__new__")) {
2204 PyErr_SetString(PicklingError, "args[0] from "
2205 "__newobj__ args has no __new__");
2206 return -1;
2207 }
2208
2209 /* XXX How could ob be NULL? */
2210 if (ob != NULL) {
2211 PyObject *ob_dot_class;
2212
2213 ob_dot_class = PyObject_GetAttr(ob, __class___str);
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002214 if (ob_dot_class == NULL) {
2215 if (PyErr_ExceptionMatches(
2216 PyExc_AttributeError))
2217 PyErr_Clear();
2218 else
2219 return -1;
2220 }
Tim Peters71fcda52003-02-14 23:05:28 +00002221 i = ob_dot_class != cls; /* true iff a problem */
2222 Py_XDECREF(ob_dot_class);
2223 if (i) {
2224 PyErr_SetString(PicklingError, "args[0] from "
2225 "__newobj__ args has the wrong class");
2226 return -1;
2227 }
2228 }
2229
2230 /* Save the class and its __new__ arguments. */
2231 if (save(self, cls, 0) < 0)
2232 return -1;
2233
2234 newargtup = PyTuple_New(n-1); /* argtup[1:] */
2235 if (newargtup == NULL)
2236 return -1;
2237 for (i = 1; i < n; ++i) {
2238 PyObject *temp = PyTuple_GET_ITEM(argtup, i);
2239 Py_INCREF(temp);
2240 PyTuple_SET_ITEM(newargtup, i-1, temp);
2241 }
2242 i = save(self, newargtup, 0) < 0;
2243 Py_DECREF(newargtup);
2244 if (i < 0)
2245 return -1;
2246
2247 /* Add NEWOBJ opcode. */
2248 if (self->write_func(self, &newobj, 1) < 0)
2249 return -1;
2250 }
2251 else {
2252 /* Not using NEWOBJ. */
2253 if (save(self, callable, 0) < 0 ||
2254 save(self, argtup, 0) < 0 ||
2255 self->write_func(self, &reduce, 1) < 0)
2256 return -1;
2257 }
2258
2259 /* Memoize. */
2260 /* XXX How can ob be NULL? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002261 if (ob != NULL) {
2262 if (state && !PyDict_Check(state)) {
2263 if (put2(self, ob) < 0)
2264 return -1;
2265 }
Tim Peters71fcda52003-02-14 23:05:28 +00002266 else if (put(self, ob) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002267 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002268 }
Tim Peters84e87f32001-03-17 04:50:51 +00002269
Guido van Rossum60456fd1997-04-09 17:36:32 +00002270
Tim Peters71fcda52003-02-14 23:05:28 +00002271 if (listitems && batch_list(self, listitems) < 0)
2272 return -1;
2273
2274 if (dictitems && batch_dict(self, dictitems) < 0)
2275 return -1;
2276
2277 if (state) {
2278 if (save(self, state, 0) < 0 ||
2279 self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002280 return -1;
2281 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002282
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002283 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002284}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002285
Guido van Rossum60456fd1997-04-09 17:36:32 +00002286static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00002287save(Picklerobject *self, PyObject *args, int pers_save)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002288{
2289 PyTypeObject *type;
Tim Peters71fcda52003-02-14 23:05:28 +00002290 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
2291 PyObject *arg_tup;
2292 int res = -1;
2293 int tmp, size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002294
Martin v. Löwis5a395302002-08-04 08:20:23 +00002295 if (self->nesting++ > Py_GetRecursionLimit()){
2296 PyErr_SetString(PyExc_RuntimeError,
2297 "maximum recursion depth exceeded");
2298 goto finally;
2299 }
2300
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002301 if (!pers_save && self->pers_func) {
2302 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2303 res = tmp;
2304 goto finally;
2305 }
2306 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002307
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002308 if (args == Py_None) {
2309 res = save_none(self, args);
2310 goto finally;
2311 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002312
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002313 type = args->ob_type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002314
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002315 switch (type->tp_name[0]) {
Guido van Rossum77f6a652002-04-03 22:41:51 +00002316 case 'b':
2317 if (args == Py_False || args == Py_True) {
2318 res = save_bool(self, args);
2319 goto finally;
2320 }
2321 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002322 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002323 if (type == &PyInt_Type) {
2324 res = save_int(self, args);
2325 goto finally;
2326 }
2327 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002328
Guido van Rossum60456fd1997-04-09 17:36:32 +00002329 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002330 if (type == &PyLong_Type) {
2331 res = save_long(self, args);
2332 goto finally;
2333 }
2334 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002335
Guido van Rossum60456fd1997-04-09 17:36:32 +00002336 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002337 if (type == &PyFloat_Type) {
2338 res = save_float(self, args);
2339 goto finally;
2340 }
2341 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002342
Guido van Rossum60456fd1997-04-09 17:36:32 +00002343 case 't':
Tim Peters1d63c9f2003-02-02 20:29:39 +00002344 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2345 res = save_tuple(self, args);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002346 goto finally;
2347 }
2348 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002349
Guido van Rossum60456fd1997-04-09 17:36:32 +00002350 case 's':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002351 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
2352 res = save_string(self, args, 0);
2353 goto finally;
2354 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002355
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002356#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002357 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002358 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
2359 res = save_unicode(self, args, 0);
2360 goto finally;
2361 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002362#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002363 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002364
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002365 if (args->ob_refcnt > 1) {
Tim Peterscba30e22003-02-01 06:24:36 +00002366 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002367 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002368
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002369 if (PyDict_GetItem(self->memo, py_ob_id)) {
2370 if (get(self, py_ob_id) < 0)
2371 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002372
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002373 res = 0;
2374 goto finally;
2375 }
2376 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002377
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002378 switch (type->tp_name[0]) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002379 case 's':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002380 if (type == &PyString_Type) {
2381 res = save_string(self, args, 1);
2382 goto finally;
2383 }
2384 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002385
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002386#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002387 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002388 if (type == &PyUnicode_Type) {
2389 res = save_unicode(self, args, 1);
2390 goto finally;
2391 }
2392 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002393#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002394
Guido van Rossum60456fd1997-04-09 17:36:32 +00002395 case 't':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002396 if (type == &PyTuple_Type) {
2397 res = save_tuple(self, args);
2398 goto finally;
2399 }
2400 if (type == &PyType_Type) {
2401 res = save_global(self, args, NULL);
2402 goto finally;
2403 }
2404 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002405
Guido van Rossum60456fd1997-04-09 17:36:32 +00002406 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002407 if (type == &PyList_Type) {
2408 res = save_list(self, args);
2409 goto finally;
2410 }
2411 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002412
2413 case 'd':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002414 if (type == &PyDict_Type) {
2415 res = save_dict(self, args);
2416 goto finally;
2417 }
2418 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002419
2420 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002421 if (type == &PyInstance_Type) {
2422 res = save_inst(self, args);
2423 goto finally;
2424 }
2425 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002426
2427 case 'c':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002428 if (type == &PyClass_Type) {
2429 res = save_global(self, args, NULL);
2430 goto finally;
2431 }
2432 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002433
2434 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002435 if (type == &PyFunction_Type) {
2436 res = save_global(self, args, NULL);
Christian Tismer2460c622004-02-26 16:21:45 +00002437 if (res && PyErr_ExceptionMatches(PickleError)) {
2438 /* fall back to reduce */
2439 PyErr_Clear();
2440 break;
2441 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002442 goto finally;
2443 }
2444 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002445
2446 case 'b':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002447 if (type == &PyCFunction_Type) {
2448 res = save_global(self, args, NULL);
2449 goto finally;
2450 }
2451 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002452
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002453 if (!pers_save && self->inst_pers_func) {
2454 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2455 res = tmp;
2456 goto finally;
2457 }
2458 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002459
Jeremy Hylton39c61162002-07-16 19:47:43 +00002460 if (PyType_IsSubtype(type, &PyType_Type)) {
2461 res = save_global(self, args, NULL);
2462 goto finally;
2463 }
2464
Guido van Rossumb289b872003-02-19 01:45:13 +00002465 /* Get a reduction callable, and call it. This may come from
2466 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
2467 * or the object's __reduce__ method.
Tim Peters71fcda52003-02-14 23:05:28 +00002468 */
Tim Peters5aa3da62003-02-13 21:03:57 +00002469 __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2470 if (__reduce__ != NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002471 Py_INCREF(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00002472 Py_INCREF(args);
2473 ARG_TUP(self, args);
2474 if (self->arg) {
2475 t = PyObject_Call(__reduce__, self->arg, NULL);
2476 FREE_ARG_TUP(self);
2477 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002478 }
2479 else {
Guido van Rossumb289b872003-02-19 01:45:13 +00002480 /* Check for a __reduce_ex__ method. */
2481 __reduce__ = PyObject_GetAttr(args, __reduce_ex___str);
2482 if (__reduce__ != NULL) {
2483 t = PyInt_FromLong(self->proto);
2484 if (t != NULL) {
2485 ARG_TUP(self, t);
2486 t = NULL;
2487 if (self->arg) {
2488 t = PyObject_Call(__reduce__,
2489 self->arg, NULL);
2490 FREE_ARG_TUP(self);
2491 }
2492 }
2493 }
2494 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002495 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2496 PyErr_Clear();
2497 else
2498 goto finally;
Guido van Rossumb289b872003-02-19 01:45:13 +00002499 /* Check for a __reduce__ method. */
2500 __reduce__ = PyObject_GetAttr(args, __reduce___str);
2501 if (__reduce__ != NULL) {
2502 t = PyObject_Call(__reduce__,
2503 empty_tuple, NULL);
2504 }
2505 else {
2506 PyErr_SetObject(UnpickleableError, args);
2507 goto finally;
2508 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002509 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002510 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002511
Tim Peters71fcda52003-02-14 23:05:28 +00002512 if (t == NULL)
2513 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002514
Tim Peters71fcda52003-02-14 23:05:28 +00002515 if (PyString_Check(t)) {
2516 res = save_global(self, args, t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002517 goto finally;
2518 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002519
Tim Peters71fcda52003-02-14 23:05:28 +00002520 if (! PyTuple_Check(t)) {
2521 cPickle_ErrFormat(PicklingError, "Value returned by "
2522 "%s must be string or tuple",
2523 "O", __reduce__);
2524 goto finally;
2525 }
2526
2527 size = PyTuple_Size(t);
2528 if (size < 2 || size > 5) {
2529 cPickle_ErrFormat(PicklingError, "tuple returned by "
2530 "%s must contain 2 through 5 elements",
2531 "O", __reduce__);
2532 goto finally;
2533 }
2534
2535 arg_tup = PyTuple_GET_ITEM(t, 1);
2536 if (!(PyTuple_Check(arg_tup) || arg_tup == Py_None)) {
2537 cPickle_ErrFormat(PicklingError, "Second element of "
2538 "tuple returned by %s must be a tuple",
2539 "O", __reduce__);
2540 goto finally;
2541 }
2542
2543 res = save_reduce(self, t, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002544
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002545 finally:
Martin v. Löwis5a395302002-08-04 08:20:23 +00002546 self->nesting--;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002547 Py_XDECREF(py_ob_id);
2548 Py_XDECREF(__reduce__);
2549 Py_XDECREF(t);
Tim Peters84e87f32001-03-17 04:50:51 +00002550
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002551 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002552}
2553
2554
2555static int
Tim Peterscba30e22003-02-01 06:24:36 +00002556dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002557{
2558 static char stop = STOP;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002559
Tim Peters4190fb82003-02-02 16:09:05 +00002560 if (self->proto >= 2) {
2561 char bytes[2];
2562
2563 bytes[0] = PROTO;
Tim Peters87482ea2003-02-02 16:16:30 +00002564 assert(self->proto >= 0 && self->proto < 256);
2565 bytes[1] = (char)self->proto;
Tim Peters4190fb82003-02-02 16:09:05 +00002566 if (self->write_func(self, bytes, 2) < 0)
2567 return -1;
2568 }
2569
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002570 if (save(self, args, 0) < 0)
2571 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002572
Tim Peters4190fb82003-02-02 16:09:05 +00002573 if (self->write_func(self, &stop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002574 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002575
Tim Peters4190fb82003-02-02 16:09:05 +00002576 if (self->write_func(self, NULL, 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002577 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002578
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002579 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002580}
2581
2582static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002583Pickle_clear_memo(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002584{
Tim Peterscba30e22003-02-01 06:24:36 +00002585 if (self->memo)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002586 PyDict_Clear(self->memo);
2587 Py_INCREF(Py_None);
2588 return Py_None;
2589}
2590
2591static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002592Pickle_getvalue(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002593{
2594 int l, i, rsize, ssize, clear=1, lm;
2595 long ik;
2596 PyObject *k, *r;
2597 char *s, *p, *have_get;
2598 Pdata *data;
2599
2600 /* Can be called by Python code or C code */
Tim Peterscba30e22003-02-01 06:24:36 +00002601 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002602 return NULL;
2603
2604 /* Check to make sure we are based on a list */
2605 if (! Pdata_Check(self->file)) {
2606 PyErr_SetString(PicklingError,
2607 "Attempt to getvalue() a non-list-based pickler");
2608 return NULL;
2609 }
2610
2611 /* flush write buffer */
2612 if (write_other(self, NULL, 0) < 0) return NULL;
2613
2614 data=(Pdata*)self->file;
2615 l=data->length;
2616
2617 /* set up an array to hold get/put status */
Tim Petersac5687a2003-02-02 18:08:34 +00002618 lm = PyDict_Size(self->memo);
2619 if (lm < 0) return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002620 lm++;
Tim Petersac5687a2003-02-02 18:08:34 +00002621 have_get = malloc(lm);
2622 if (have_get == NULL) return PyErr_NoMemory();
2623 memset(have_get, 0, lm);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002624
2625 /* Scan for gets. */
Tim Petersac5687a2003-02-02 18:08:34 +00002626 for (rsize = 0, i = l; --i >= 0; ) {
2627 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002628
Tim Petersac5687a2003-02-02 18:08:34 +00002629 if (PyString_Check(k))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002630 rsize += PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002631
2632 else if (PyInt_Check(k)) { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002633 ik = PyInt_AS_LONG((PyIntObject*)k);
2634 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002635 PyErr_SetString(PicklingError,
2636 "Invalid get data");
2637 return NULL;
2638 }
Tim Petersac5687a2003-02-02 18:08:34 +00002639 if (have_get[ik]) /* with matching get */
2640 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002641 }
2642
2643 else if (! (PyTuple_Check(k) &&
2644 PyTuple_GET_SIZE(k) == 2 &&
Tim Petersac5687a2003-02-02 18:08:34 +00002645 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002646 ) {
2647 PyErr_SetString(PicklingError,
2648 "Unexpected data in internal list");
2649 return NULL;
2650 }
2651
2652 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002653 ik = PyInt_AS_LONG((PyIntObject *)k);
2654 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002655 PyErr_SetString(PicklingError,
2656 "Invalid get data");
2657 return NULL;
2658 }
Tim Petersac5687a2003-02-02 18:08:34 +00002659 have_get[ik] = 1;
2660 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002661 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002662 }
2663
2664 /* Now generate the result */
Tim Petersac5687a2003-02-02 18:08:34 +00002665 r = PyString_FromStringAndSize(NULL, rsize);
2666 if (r == NULL) goto err;
2667 s = PyString_AS_STRING((PyStringObject *)r);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002668
Tim Petersac5687a2003-02-02 18:08:34 +00002669 for (i = 0; i < l; i++) {
2670 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002671
2672 if (PyString_Check(k)) {
Tim Petersac5687a2003-02-02 18:08:34 +00002673 ssize = PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002674 if (ssize) {
Tim Petersac5687a2003-02-02 18:08:34 +00002675 p=PyString_AS_STRING((PyStringObject *)k);
2676 while (--ssize >= 0)
2677 *s++ = *p++;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002678 }
2679 }
2680
2681 else if (PyTuple_Check(k)) { /* get */
Tim Petersac5687a2003-02-02 18:08:34 +00002682 ik = PyInt_AS_LONG((PyIntObject *)
2683 PyTuple_GET_ITEM(k, 0));
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002684 if (ik < 256) {
2685 *s++ = BINGET;
2686 *s++ = (int)(ik & 0xff);
2687 }
2688 else {
2689 *s++ = LONG_BINGET;
2690 *s++ = (int)(ik & 0xff);
2691 *s++ = (int)((ik >> 8) & 0xff);
2692 *s++ = (int)((ik >> 16) & 0xff);
2693 *s++ = (int)((ik >> 24) & 0xff);
2694 }
2695 }
2696
2697 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002698 ik = PyInt_AS_LONG((PyIntObject*)k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002699
2700 if (have_get[ik]) { /* with matching get */
2701 if (ik < 256) {
2702 *s++ = BINPUT;
2703 *s++ = (int)(ik & 0xff);
2704 }
2705 else {
2706 *s++ = LONG_BINPUT;
2707 *s++ = (int)(ik & 0xff);
2708 *s++ = (int)((ik >> 8) & 0xff);
2709 *s++ = (int)((ik >> 16) & 0xff);
2710 *s++ = (int)((ik >> 24) & 0xff);
2711 }
2712 }
2713 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002714 }
2715
2716 if (clear) {
2717 PyDict_Clear(self->memo);
Tim Petersac5687a2003-02-02 18:08:34 +00002718 Pdata_clear(data, 0);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002719 }
2720
2721 free(have_get);
2722 return r;
2723 err:
2724 free(have_get);
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002725 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002726}
2727
2728static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002729Pickler_dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002730{
2731 PyObject *ob;
2732 int get=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002733
Tim Peterscba30e22003-02-01 06:24:36 +00002734 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002735 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002736
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002737 if (dump(self, ob) < 0)
2738 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002739
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002740 if (get) return Pickle_getvalue(self, NULL);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002741
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002742 /* XXX Why does dump() return self? */
2743 Py_INCREF(self);
2744 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002745}
2746
2747
Tim Peterscba30e22003-02-01 06:24:36 +00002748static struct PyMethodDef Pickler_methods[] =
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002749{
Neal Norwitzb0493252002-03-31 14:44:22 +00002750 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002751 PyDoc_STR("dump(object) -- "
2752 "Write an object in pickle format to the object's pickle stream")},
Fred Drake0ebacc82002-05-01 20:36:39 +00002753 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002754 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
Neal Norwitzb0493252002-03-31 14:44:22 +00002755 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002756 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
Guido van Rossum60456fd1997-04-09 17:36:32 +00002757 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002758};
2759
2760
2761static Picklerobject *
Tim Peters5bd2a792003-02-01 16:45:06 +00002762newPicklerobject(PyObject *file, int proto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002763{
2764 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002765
Tim Peters5bd2a792003-02-01 16:45:06 +00002766 if (proto < 0)
Tim Peters8587b3c2003-02-13 15:44:41 +00002767 proto = HIGHEST_PROTOCOL;
2768 if (proto > HIGHEST_PROTOCOL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002769 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
2770 "the highest available protocol is %d",
Tim Peters8587b3c2003-02-13 15:44:41 +00002771 proto, HIGHEST_PROTOCOL);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002772 return NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002773 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002774
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002775 self = PyObject_GC_New(Picklerobject, &Picklertype);
Tim Peters5bd2a792003-02-01 16:45:06 +00002776 if (self == NULL)
2777 return NULL;
2778 self->proto = proto;
2779 self->bin = proto > 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002780 self->fp = NULL;
2781 self->write = NULL;
2782 self->memo = NULL;
2783 self->arg = NULL;
2784 self->pers_func = NULL;
2785 self->inst_pers_func = NULL;
2786 self->write_buf = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002787 self->fast = 0;
Martin v. Löwis5a395302002-08-04 08:20:23 +00002788 self->nesting = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002789 self->fast_container = 0;
2790 self->fast_memo = NULL;
2791 self->buf_size = 0;
2792 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002793
Tim Peters5bd2a792003-02-01 16:45:06 +00002794 self->file = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002795 if (file)
2796 Py_INCREF(file);
Tim Peters5bd2a792003-02-01 16:45:06 +00002797 else {
2798 file = Pdata_New();
2799 if (file == NULL)
2800 goto err;
2801 }
2802 self->file = file;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002803
Tim Peterscba30e22003-02-01 06:24:36 +00002804 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002805 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002806
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002807 if (PyFile_Check(file)) {
2808 self->fp = PyFile_AsFile(file);
2809 if (self->fp == NULL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002810 PyErr_SetString(PyExc_ValueError,
2811 "I/O operation on closed file");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002812 goto err;
2813 }
2814 self->write_func = write_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002815 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002816 else if (PycStringIO_OutputCheck(file)) {
2817 self->write_func = write_cStringIO;
2818 }
2819 else if (file == Py_None) {
2820 self->write_func = write_none;
2821 }
2822 else {
2823 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002824
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002825 if (! Pdata_Check(file)) {
2826 self->write = PyObject_GetAttr(file, write_str);
2827 if (!self->write) {
2828 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00002829 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002830 "argument must have 'write' "
2831 "attribute");
2832 goto err;
2833 }
2834 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002835
Tim Peters5bd2a792003-02-01 16:45:06 +00002836 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
2837 if (self->write_buf == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002838 PyErr_NoMemory();
2839 goto err;
2840 }
2841 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002842
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002843 if (PyEval_GetRestricted()) {
2844 /* Restricted execution, get private tables */
Tim Peters5b7da392003-02-04 00:21:07 +00002845 PyObject *m = PyImport_Import(copy_reg_str);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002846
Tim Peters5b7da392003-02-04 00:21:07 +00002847 if (m == NULL)
2848 goto err;
2849 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002850 Py_DECREF(m);
Tim Peters5b7da392003-02-04 00:21:07 +00002851 if (self->dispatch_table == NULL)
2852 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002853 }
2854 else {
Tim Peters5b7da392003-02-04 00:21:07 +00002855 self->dispatch_table = dispatch_table;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002856 Py_INCREF(dispatch_table);
2857 }
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002858 PyObject_GC_Track(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002859
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002860 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002861
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002862 err:
Tim Peters5bd2a792003-02-01 16:45:06 +00002863 Py_DECREF(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002864 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002865}
2866
2867
2868static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00002869get_Pickler(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002870{
Martin v. Löwis15e62742006-02-27 16:46:16 +00002871 static char *kwlist[] = {"file", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002872 PyObject *file = NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002873 int proto = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002874
Tim Peters92c8bb32003-02-13 23:00:26 +00002875 /* XXX
Martin v. Löwis544f1192004-07-27 05:22:33 +00002876 * The documented signature is Pickler(file, protocol=0), but this
Tim Peters92c8bb32003-02-13 23:00:26 +00002877 * accepts Pickler() and Pickler(integer) too. The meaning then
2878 * is clear as mud, undocumented, and not supported by pickle.py.
2879 * I'm told Zope uses this, but I haven't traced into this code
2880 * far enough to figure out what it means.
Tim Peters5bd2a792003-02-01 16:45:06 +00002881 */
2882 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002883 PyErr_Clear();
Tim Peters5bd2a792003-02-01 16:45:06 +00002884 proto = 0;
Martin v. Löwis544f1192004-07-27 05:22:33 +00002885 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler",
2886 kwlist, &file, &proto))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002887 return NULL;
2888 }
Tim Peters5bd2a792003-02-01 16:45:06 +00002889 return (PyObject *)newPicklerobject(file, proto);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002890}
2891
2892
2893static void
Tim Peterscba30e22003-02-01 06:24:36 +00002894Pickler_dealloc(Picklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002895{
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002896 PyObject_GC_UnTrack(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002897 Py_XDECREF(self->write);
2898 Py_XDECREF(self->memo);
2899 Py_XDECREF(self->fast_memo);
2900 Py_XDECREF(self->arg);
2901 Py_XDECREF(self->file);
2902 Py_XDECREF(self->pers_func);
2903 Py_XDECREF(self->inst_pers_func);
2904 Py_XDECREF(self->dispatch_table);
Tim Peters5bd2a792003-02-01 16:45:06 +00002905 PyMem_Free(self->write_buf);
Tim Peters3cfe7542003-05-21 21:29:48 +00002906 self->ob_type->tp_free((PyObject *)self);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002907}
2908
2909static int
2910Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)
2911{
Thomas Woutersc6e55062006-04-15 21:47:09 +00002912 Py_VISIT(self->write);
2913 Py_VISIT(self->memo);
2914 Py_VISIT(self->fast_memo);
2915 Py_VISIT(self->arg);
2916 Py_VISIT(self->file);
2917 Py_VISIT(self->pers_func);
2918 Py_VISIT(self->inst_pers_func);
2919 Py_VISIT(self->dispatch_table);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002920 return 0;
2921}
2922
2923static int
2924Pickler_clear(Picklerobject *self)
2925{
Thomas Woutersedf17d82006-04-15 17:28:34 +00002926 Py_CLEAR(self->write);
2927 Py_CLEAR(self->memo);
2928 Py_CLEAR(self->fast_memo);
2929 Py_CLEAR(self->arg);
2930 Py_CLEAR(self->file);
2931 Py_CLEAR(self->pers_func);
2932 Py_CLEAR(self->inst_pers_func);
2933 Py_CLEAR(self->dispatch_table);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002934 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002935}
2936
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002937static PyObject *
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002938Pickler_get_pers_func(Picklerobject *p)
2939{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002940 if (p->pers_func == NULL)
2941 PyErr_SetString(PyExc_AttributeError, "persistent_id");
2942 else
2943 Py_INCREF(p->pers_func);
2944 return p->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002945}
2946
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002947static int
2948Pickler_set_pers_func(Picklerobject *p, PyObject *v)
2949{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002950 if (v == NULL) {
2951 PyErr_SetString(PyExc_TypeError,
2952 "attribute deletion is not supported");
2953 return -1;
2954 }
2955 Py_XDECREF(p->pers_func);
2956 Py_INCREF(v);
2957 p->pers_func = v;
2958 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002959}
2960
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002961static int
2962Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
2963{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002964 if (v == NULL) {
2965 PyErr_SetString(PyExc_TypeError,
2966 "attribute deletion is not supported");
2967 return -1;
2968 }
2969 Py_XDECREF(p->inst_pers_func);
2970 Py_INCREF(v);
2971 p->inst_pers_func = v;
2972 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002973}
2974
2975static PyObject *
2976Pickler_get_memo(Picklerobject *p)
2977{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002978 if (p->memo == NULL)
2979 PyErr_SetString(PyExc_AttributeError, "memo");
2980 else
2981 Py_INCREF(p->memo);
2982 return p->memo;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002983}
2984
2985static int
2986Pickler_set_memo(Picklerobject *p, PyObject *v)
2987{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002988 if (v == NULL) {
2989 PyErr_SetString(PyExc_TypeError,
2990 "attribute deletion is not supported");
2991 return -1;
2992 }
2993 if (!PyDict_Check(v)) {
2994 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
2995 return -1;
2996 }
2997 Py_XDECREF(p->memo);
2998 Py_INCREF(v);
2999 p->memo = v;
3000 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003001}
3002
3003static PyObject *
3004Pickler_get_error(Picklerobject *p)
3005{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003006 /* why is this an attribute on the Pickler? */
3007 Py_INCREF(PicklingError);
3008 return PicklingError;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003009}
3010
3011static PyMemberDef Pickler_members[] = {
3012 {"binary", T_INT, offsetof(Picklerobject, bin)},
3013 {"fast", T_INT, offsetof(Picklerobject, fast)},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003014 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003015};
3016
3017static PyGetSetDef Pickler_getsets[] = {
Tim Peterscba30e22003-02-01 06:24:36 +00003018 {"persistent_id", (getter)Pickler_get_pers_func,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003019 (setter)Pickler_set_pers_func},
3020 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
3021 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003022 {"PicklingError", (getter)Pickler_get_error, NULL},
3023 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003024};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003025
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003026PyDoc_STRVAR(Picklertype__doc__,
3027"Objects that know how to pickle objects\n");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003028
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003029static PyTypeObject Picklertype = {
3030 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00003031 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +00003032 "cPickle.Pickler", /*tp_name*/
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003033 sizeof(Picklerobject), /*tp_basicsize*/
3034 0,
3035 (destructor)Pickler_dealloc, /* tp_dealloc */
3036 0, /* tp_print */
3037 0, /* tp_getattr */
3038 0, /* tp_setattr */
3039 0, /* tp_compare */
3040 0, /* tp_repr */
3041 0, /* tp_as_number */
3042 0, /* tp_as_sequence */
3043 0, /* tp_as_mapping */
3044 0, /* tp_hash */
3045 0, /* tp_call */
3046 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00003047 PyObject_GenericGetAttr, /* tp_getattro */
3048 PyObject_GenericSetAttr, /* tp_setattro */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003049 0, /* tp_as_buffer */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003050 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003051 Picklertype__doc__, /* tp_doc */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003052 (traverseproc)Pickler_traverse, /* tp_traverse */
3053 (inquiry)Pickler_clear, /* tp_clear */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003054 0, /* tp_richcompare */
3055 0, /* tp_weaklistoffset */
3056 0, /* tp_iter */
3057 0, /* tp_iternext */
3058 Pickler_methods, /* tp_methods */
3059 Pickler_members, /* tp_members */
3060 Pickler_getsets, /* tp_getset */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003061};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003062
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003063static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003064find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003065{
3066 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003067
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003068 if (fc) {
3069 if (fc==Py_None) {
Tim Peters92c8bb32003-02-13 23:00:26 +00003070 PyErr_SetString(UnpicklingError, "Global and instance "
3071 "pickles are not supported.");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003072 return NULL;
3073 }
Tim Peterscba30e22003-02-01 06:24:36 +00003074 return PyObject_CallFunction(fc, "OO", py_module_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003075 py_global_name);
3076 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003077
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003078 module = PySys_GetObject("modules");
3079 if (module == NULL)
3080 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00003081
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003082 module = PyDict_GetItem(module, py_module_name);
3083 if (module == NULL) {
3084 module = PyImport_Import(py_module_name);
3085 if (!module)
3086 return NULL;
3087 global = PyObject_GetAttr(module, py_global_name);
3088 Py_DECREF(module);
3089 }
3090 else
3091 global = PyObject_GetAttr(module, py_global_name);
3092 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003093}
3094
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003095static int
Tim Peterscba30e22003-02-01 06:24:36 +00003096marker(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003097{
3098 if (self->num_marks < 1) {
3099 PyErr_SetString(UnpicklingError, "could not find MARK");
3100 return -1;
3101 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003102
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003103 return self->marks[--self->num_marks];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003104}
3105
Tim Peters84e87f32001-03-17 04:50:51 +00003106
Guido van Rossum60456fd1997-04-09 17:36:32 +00003107static int
Tim Peterscba30e22003-02-01 06:24:36 +00003108load_none(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003109{
3110 PDATA_APPEND(self->stack, Py_None, -1);
3111 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003112}
3113
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003114static int
Tim Peterscba30e22003-02-01 06:24:36 +00003115bad_readline(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003116{
3117 PyErr_SetString(UnpicklingError, "pickle data was truncated");
3118 return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003119}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003120
3121static int
Tim Peterscba30e22003-02-01 06:24:36 +00003122load_int(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003123{
3124 PyObject *py_int = 0;
3125 char *endptr, *s;
3126 int len, res = -1;
3127 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003128
Tim Peters0bc93f52003-02-02 18:29:33 +00003129 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003130 if (len < 2) return bad_readline();
3131 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003132
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003133 errno = 0;
3134 l = strtol(s, &endptr, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003135
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003136 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
3137 /* Hm, maybe we've got something long. Let's try reading
3138 it as a Python long object. */
3139 errno = 0;
3140 py_int = PyLong_FromString(s, NULL, 0);
3141 if (py_int == NULL) {
3142 PyErr_SetString(PyExc_ValueError,
3143 "could not convert string to int");
3144 goto finally;
3145 }
3146 }
3147 else {
Guido van Rossume2763392002-04-05 19:30:08 +00003148 if (len == 3 && (l == 0 || l == 1)) {
3149 if (!( py_int = PyBool_FromLong(l))) goto finally;
3150 }
3151 else {
3152 if (!( py_int = PyInt_FromLong(l))) goto finally;
3153 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003154 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003155
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003156 free(s);
3157 PDATA_PUSH(self->stack, py_int, -1);
3158 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003159
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003160 finally:
3161 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003162
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003163 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003164}
3165
Tim Peters3c67d792003-02-02 17:59:11 +00003166static int
3167load_bool(Unpicklerobject *self, PyObject *boolean)
3168{
3169 assert(boolean == Py_True || boolean == Py_False);
Tim Peterse0a39072003-02-03 15:45:56 +00003170 PDATA_APPEND(self->stack, boolean, -1);
Tim Peters3c67d792003-02-02 17:59:11 +00003171 return 0;
3172}
3173
Tim Petersee1a53c2003-02-02 02:57:53 +00003174/* s contains x bytes of a little-endian integer. Return its value as a
3175 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3176 * int, but when x is 4 it's a signed one. This is an historical source
3177 * of x-platform bugs.
3178 */
Tim Peters84e87f32001-03-17 04:50:51 +00003179static long
Tim Petersee1a53c2003-02-02 02:57:53 +00003180calc_binint(char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003181{
3182 unsigned char c;
3183 int i;
3184 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003185
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003186 for (i = 0, l = 0L; i < x; i++) {
3187 c = (unsigned char)s[i];
3188 l |= (long)c << (i * 8);
3189 }
Tim Petersbfa18f72001-04-10 01:54:42 +00003190#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003191 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3192 * is signed, so on a box with longs bigger than 4 bytes we need
3193 * to extend a BININT's sign bit to the full width.
3194 */
3195 if (x == 4 && l & (1L << 31))
3196 l |= (~0L) << 32;
Tim Petersbfa18f72001-04-10 01:54:42 +00003197#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003198 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003199}
3200
3201
3202static int
Tim Peterscba30e22003-02-01 06:24:36 +00003203load_binintx(Unpicklerobject *self, char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003204{
3205 PyObject *py_int = 0;
3206 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003207
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003208 l = calc_binint(s, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003209
Tim Peterscba30e22003-02-01 06:24:36 +00003210 if (!( py_int = PyInt_FromLong(l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003211 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003212
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003213 PDATA_PUSH(self->stack, py_int, -1);
3214 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003215}
3216
3217
3218static int
Tim Peterscba30e22003-02-01 06:24:36 +00003219load_binint(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003220{
3221 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003222
Tim Peters0bc93f52003-02-02 18:29:33 +00003223 if (self->read_func(self, &s, 4) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003224 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003225
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003226 return load_binintx(self, s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003227}
3228
3229
3230static int
Tim Peterscba30e22003-02-01 06:24:36 +00003231load_binint1(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003232{
3233 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003234
Tim Peters0bc93f52003-02-02 18:29:33 +00003235 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003236 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003237
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003238 return load_binintx(self, s, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003239}
3240
3241
3242static int
Tim Peterscba30e22003-02-01 06:24:36 +00003243load_binint2(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003244{
3245 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003246
Tim Peters0bc93f52003-02-02 18:29:33 +00003247 if (self->read_func(self, &s, 2) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003248 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003249
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003250 return load_binintx(self, s, 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003251}
Tim Peters84e87f32001-03-17 04:50:51 +00003252
Guido van Rossum60456fd1997-04-09 17:36:32 +00003253static int
Tim Peterscba30e22003-02-01 06:24:36 +00003254load_long(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003255{
3256 PyObject *l = 0;
3257 char *end, *s;
3258 int len, res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003259
Tim Peters0bc93f52003-02-02 18:29:33 +00003260 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003261 if (len < 2) return bad_readline();
3262 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003263
Tim Peterscba30e22003-02-01 06:24:36 +00003264 if (!( l = PyLong_FromString(s, &end, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003265 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003266
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003267 free(s);
3268 PDATA_PUSH(self->stack, l, -1);
3269 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003270
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003271 finally:
3272 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003273
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003274 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003275}
3276
Tim Petersee1a53c2003-02-02 02:57:53 +00003277/* 'size' bytes contain the # of bytes of little-endian 256's-complement
3278 * data following.
3279 */
3280static int
3281load_counted_long(Unpicklerobject *self, int size)
3282{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003283 Py_ssize_t i;
Tim Petersee1a53c2003-02-02 02:57:53 +00003284 char *nbytes;
3285 unsigned char *pdata;
3286 PyObject *along;
3287
3288 assert(size == 1 || size == 4);
3289 i = self->read_func(self, &nbytes, size);
3290 if (i < 0) return -1;
3291
3292 size = calc_binint(nbytes, size);
3293 if (size < 0) {
3294 /* Corrupt or hostile pickle -- we never write one like
3295 * this.
3296 */
Tim Peters0c7c48e2003-02-03 22:07:24 +00003297 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
Tim Petersee1a53c2003-02-02 02:57:53 +00003298 "byte count");
3299 return -1;
3300 }
3301
3302 if (size == 0)
3303 along = PyLong_FromLong(0L);
3304 else {
3305 /* Read the raw little-endian bytes & convert. */
Neal Norwitz529baf22003-02-02 17:08:33 +00003306 i = self->read_func(self, (char **)&pdata, size);
Tim Petersee1a53c2003-02-02 02:57:53 +00003307 if (i < 0) return -1;
3308 along = _PyLong_FromByteArray(pdata, (size_t)size,
3309 1 /* little endian */, 1 /* signed */);
3310 }
3311 if (along == NULL)
3312 return -1;
3313 PDATA_PUSH(self->stack, along, -1);
3314 return 0;
3315}
Tim Peters84e87f32001-03-17 04:50:51 +00003316
Guido van Rossum60456fd1997-04-09 17:36:32 +00003317static int
Tim Peterscba30e22003-02-01 06:24:36 +00003318load_float(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003319{
3320 PyObject *py_float = 0;
3321 char *endptr, *s;
3322 int len, res = -1;
3323 double d;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003324
Tim Peters0bc93f52003-02-02 18:29:33 +00003325 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003326 if (len < 2) return bad_readline();
3327 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003328
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003329 errno = 0;
Martin v. Löwis737ea822004-06-08 18:52:54 +00003330 d = PyOS_ascii_strtod(s, &endptr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003331
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003332 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
3333 PyErr_SetString(PyExc_ValueError,
3334 "could not convert string to float");
3335 goto finally;
3336 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003337
Tim Peterscba30e22003-02-01 06:24:36 +00003338 if (!( py_float = PyFloat_FromDouble(d)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003339 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003340
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003341 free(s);
3342 PDATA_PUSH(self->stack, py_float, -1);
3343 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003344
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003345 finally:
3346 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003347
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003348 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003349}
3350
Guido van Rossum60456fd1997-04-09 17:36:32 +00003351static int
Tim Peterscba30e22003-02-01 06:24:36 +00003352load_binfloat(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003353{
Tim Peters9905b942003-03-20 20:53:32 +00003354 PyObject *py_float;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003355 double x;
3356 char *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003357
Tim Peters0bc93f52003-02-02 18:29:33 +00003358 if (self->read_func(self, &p, 8) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003359 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003360
Tim Peters9905b942003-03-20 20:53:32 +00003361 x = _PyFloat_Unpack8((unsigned char *)p, 0);
3362 if (x == -1.0 && PyErr_Occurred())
3363 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003364
Tim Peters9905b942003-03-20 20:53:32 +00003365 py_float = PyFloat_FromDouble(x);
3366 if (py_float == NULL)
3367 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003368
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003369 PDATA_PUSH(self->stack, py_float, -1);
3370 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003371}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003372
3373static int
Tim Peterscba30e22003-02-01 06:24:36 +00003374load_string(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003375{
3376 PyObject *str = 0;
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003377 int len, res = -1;
3378 char *s, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003379
Tim Peters0bc93f52003-02-02 18:29:33 +00003380 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003381 if (len < 2) return bad_readline();
3382 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003383
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003384
3385 /* Strip outermost quotes */
3386 while (s[len-1] <= ' ')
3387 len--;
3388 if(s[0]=='"' && s[len-1]=='"'){
3389 s[len-1] = '\0';
3390 p = s + 1 ;
3391 len -= 2;
3392 } else if(s[0]=='\'' && s[len-1]=='\''){
3393 s[len-1] = '\0';
3394 p = s + 1 ;
3395 len -= 2;
3396 } else
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003397 goto insecure;
3398 /********************************************/
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003399
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003400 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
3401 if (str) {
3402 PDATA_PUSH(self->stack, str, -1);
3403 res = 0;
3404 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003405 free(s);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003406 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003407
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003408 insecure:
3409 free(s);
3410 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3411 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003412}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003413
3414
3415static int
Tim Peterscba30e22003-02-01 06:24:36 +00003416load_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003417{
3418 PyObject *py_string = 0;
3419 long l;
3420 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003421
Tim Peters0bc93f52003-02-02 18:29:33 +00003422 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003423
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003424 l = calc_binint(s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003425
Tim Peters0bc93f52003-02-02 18:29:33 +00003426 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003427 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003428
Tim Peterscba30e22003-02-01 06:24:36 +00003429 if (!( py_string = PyString_FromStringAndSize(s, l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003430 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003431
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003432 PDATA_PUSH(self->stack, py_string, -1);
3433 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003434}
3435
3436
3437static int
Tim Peterscba30e22003-02-01 06:24:36 +00003438load_short_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003439{
3440 PyObject *py_string = 0;
3441 unsigned char l;
3442 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003443
Tim Peters0bc93f52003-02-02 18:29:33 +00003444 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003445 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003446
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003447 l = (unsigned char)s[0];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003448
Tim Peters0bc93f52003-02-02 18:29:33 +00003449 if (self->read_func(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003450
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003451 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003452
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003453 PDATA_PUSH(self->stack, py_string, -1);
3454 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00003455}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003456
3457
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003458#ifdef Py_USING_UNICODE
Guido van Rossum60456fd1997-04-09 17:36:32 +00003459static int
Tim Peterscba30e22003-02-01 06:24:36 +00003460load_unicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003461{
3462 PyObject *str = 0;
3463 int len, res = -1;
3464 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003465
Tim Peters0bc93f52003-02-02 18:29:33 +00003466 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003467 if (len < 1) return bad_readline();
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003468
Tim Peterscba30e22003-02-01 06:24:36 +00003469 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003470 goto finally;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003471
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003472 PDATA_PUSH(self->stack, str, -1);
3473 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003474
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003475 finally:
3476 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003477}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003478#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003479
3480
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003481#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003482static int
Tim Peterscba30e22003-02-01 06:24:36 +00003483load_binunicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003484{
3485 PyObject *unicode;
3486 long l;
3487 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003488
Tim Peters0bc93f52003-02-02 18:29:33 +00003489 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003490
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003491 l = calc_binint(s, 4);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003492
Tim Peters0bc93f52003-02-02 18:29:33 +00003493 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003494 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003495
Tim Peterscba30e22003-02-01 06:24:36 +00003496 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003497 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003498
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003499 PDATA_PUSH(self->stack, unicode, -1);
3500 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003501}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003502#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003503
3504
3505static int
Tim Peterscba30e22003-02-01 06:24:36 +00003506load_tuple(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003507{
3508 PyObject *tup;
3509 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003510
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003511 if ((i = marker(self)) < 0) return -1;
3512 if (!( tup=Pdata_popTuple(self->stack, i))) return -1;
3513 PDATA_PUSH(self->stack, tup, -1);
3514 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003515}
3516
3517static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00003518load_counted_tuple(Unpicklerobject *self, int len)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003519{
Tim Peters1d63c9f2003-02-02 20:29:39 +00003520 PyObject *tup = PyTuple_New(len);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003521
Tim Peters1d63c9f2003-02-02 20:29:39 +00003522 if (tup == NULL)
3523 return -1;
3524
3525 while (--len >= 0) {
3526 PyObject *element;
3527
3528 PDATA_POP(self->stack, element);
3529 if (element == NULL)
3530 return -1;
3531 PyTuple_SET_ITEM(tup, len, element);
3532 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003533 PDATA_PUSH(self->stack, tup, -1);
3534 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003535}
3536
3537static int
Tim Peterscba30e22003-02-01 06:24:36 +00003538load_empty_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003539{
3540 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003541
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003542 if (!( list=PyList_New(0))) return -1;
3543 PDATA_PUSH(self->stack, list, -1);
3544 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003545}
3546
3547static int
Tim Peterscba30e22003-02-01 06:24:36 +00003548load_empty_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003549{
3550 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003551
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003552 if (!( dict=PyDict_New())) return -1;
3553 PDATA_PUSH(self->stack, dict, -1);
3554 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003555}
3556
3557
3558static int
Tim Peterscba30e22003-02-01 06:24:36 +00003559load_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003560{
3561 PyObject *list = 0;
3562 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003563
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003564 if ((i = marker(self)) < 0) return -1;
3565 if (!( list=Pdata_popList(self->stack, i))) return -1;
3566 PDATA_PUSH(self->stack, list, -1);
3567 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003568}
3569
3570static int
Tim Peterscba30e22003-02-01 06:24:36 +00003571load_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003572{
3573 PyObject *dict, *key, *value;
3574 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003575
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003576 if ((i = marker(self)) < 0) return -1;
3577 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003578
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003579 if (!( dict = PyDict_New())) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003580
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003581 for (k = i+1; k < j; k += 2) {
3582 key =self->stack->data[k-1];
3583 value=self->stack->data[k ];
3584 if (PyDict_SetItem(dict, key, value) < 0) {
3585 Py_DECREF(dict);
3586 return -1;
3587 }
3588 }
3589 Pdata_clear(self->stack, i);
3590 PDATA_PUSH(self->stack, dict, -1);
3591 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003592}
3593
3594static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003595Instance_New(PyObject *cls, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003596{
Tim Peters1f1b2d22003-02-01 02:16:37 +00003597 PyObject *r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003598
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003599 if (PyClass_Check(cls)) {
3600 int l;
Tim Peters84e87f32001-03-17 04:50:51 +00003601
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003602 if ((l=PyObject_Size(args)) < 0) goto err;
3603 if (!( l )) {
3604 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003605
Tim Peterscba30e22003-02-01 06:24:36 +00003606 __getinitargs__ = PyObject_GetAttr(cls,
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00003607 __getinitargs___str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003608 if (!__getinitargs__) {
Tim Peterscba30e22003-02-01 06:24:36 +00003609 /* We have a class with no __getinitargs__,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003610 so bypass usual construction */
3611 PyObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003612
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003613 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00003614 if (!( inst=PyInstance_NewRaw(cls, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003615 goto err;
3616 return inst;
3617 }
3618 Py_DECREF(__getinitargs__);
3619 }
Tim Peters84e87f32001-03-17 04:50:51 +00003620
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003621 if ((r=PyInstance_New(cls, args, NULL))) return r;
3622 else goto err;
3623 }
Tim Peters84e87f32001-03-17 04:50:51 +00003624
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003625 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00003626
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003627 err:
3628 {
3629 PyObject *tp, *v, *tb;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003630
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003631 PyErr_Fetch(&tp, &v, &tb);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003632 if ((r=PyTuple_Pack(3,v,cls,args))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003633 Py_XDECREF(v);
3634 v=r;
3635 }
3636 PyErr_Restore(tp,v,tb);
3637 }
3638 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003639}
Tim Peters84e87f32001-03-17 04:50:51 +00003640
Guido van Rossum60456fd1997-04-09 17:36:32 +00003641
3642static int
Tim Peterscba30e22003-02-01 06:24:36 +00003643load_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003644{
3645 PyObject *class, *tup, *obj=0;
3646 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003647
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003648 if ((i = marker(self)) < 0) return -1;
3649 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3650 PDATA_POP(self->stack, class);
3651 if (class) {
3652 obj = Instance_New(class, tup);
3653 Py_DECREF(class);
3654 }
3655 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003656
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003657 if (! obj) return -1;
3658 PDATA_PUSH(self->stack, obj, -1);
3659 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003660}
3661
3662
3663static int
Tim Peterscba30e22003-02-01 06:24:36 +00003664load_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003665{
3666 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
3667 int i, len;
3668 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003669
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003670 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003671
Tim Peters0bc93f52003-02-02 18:29:33 +00003672 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003673 if (len < 2) return bad_readline();
3674 module_name = PyString_FromStringAndSize(s, len - 1);
3675 if (!module_name) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003676
Tim Peters0bc93f52003-02-02 18:29:33 +00003677 if ((len = self->readline_func(self, &s)) >= 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003678 if (len < 2) return bad_readline();
3679 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003680 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003681 self->find_class);
3682 Py_DECREF(class_name);
3683 }
3684 }
3685 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003686
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003687 if (! class) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003688
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003689 if ((tup=Pdata_popTuple(self->stack, i))) {
3690 obj = Instance_New(class, tup);
3691 Py_DECREF(tup);
3692 }
3693 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003694
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003695 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003696
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003697 PDATA_PUSH(self->stack, obj, -1);
3698 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003699}
3700
Tim Peterseab7db32003-02-13 18:24:14 +00003701static int
3702load_newobj(Unpicklerobject *self)
3703{
3704 PyObject *args = NULL;
3705 PyObject *clsraw = NULL;
3706 PyTypeObject *cls; /* clsraw cast to its true type */
3707 PyObject *obj;
3708
3709 /* Stack is ... cls argtuple, and we want to call
3710 * cls.__new__(cls, *argtuple).
3711 */
3712 PDATA_POP(self->stack, args);
3713 if (args == NULL) goto Fail;
3714 if (! PyTuple_Check(args)) {
3715 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
3716 "tuple.");
3717 goto Fail;
3718 }
3719
3720 PDATA_POP(self->stack, clsraw);
3721 cls = (PyTypeObject *)clsraw;
3722 if (cls == NULL) goto Fail;
3723 if (! PyType_Check(cls)) {
3724 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3725 "isn't a type object");
3726 goto Fail;
3727 }
3728 if (cls->tp_new == NULL) {
3729 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3730 "has NULL tp_new");
3731 goto Fail;
3732 }
3733
3734 /* Call __new__. */
3735 obj = cls->tp_new(cls, args, NULL);
3736 if (obj == NULL) goto Fail;
3737
3738 Py_DECREF(args);
3739 Py_DECREF(clsraw);
3740 PDATA_PUSH(self->stack, obj, -1);
3741 return 0;
3742
3743 Fail:
3744 Py_XDECREF(args);
3745 Py_XDECREF(clsraw);
3746 return -1;
3747}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003748
3749static int
Tim Peterscba30e22003-02-01 06:24:36 +00003750load_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003751{
3752 PyObject *class = 0, *module_name = 0, *class_name = 0;
3753 int len;
3754 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003755
Tim Peters0bc93f52003-02-02 18:29:33 +00003756 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003757 if (len < 2) return bad_readline();
3758 module_name = PyString_FromStringAndSize(s, len - 1);
3759 if (!module_name) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003760
Tim Peters0bc93f52003-02-02 18:29:33 +00003761 if ((len = self->readline_func(self, &s)) >= 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +00003762 if (len < 2) {
3763 Py_DECREF(module_name);
3764 return bad_readline();
3765 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003766 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003767 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003768 self->find_class);
3769 Py_DECREF(class_name);
3770 }
3771 }
3772 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003773
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003774 if (! class) return -1;
3775 PDATA_PUSH(self->stack, class, -1);
3776 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003777}
3778
3779
3780static int
Tim Peterscba30e22003-02-01 06:24:36 +00003781load_persid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003782{
3783 PyObject *pid = 0;
3784 int len;
3785 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003786
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003787 if (self->pers_func) {
Tim Peters0bc93f52003-02-02 18:29:33 +00003788 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003789 if (len < 2) return bad_readline();
3790
3791 pid = PyString_FromStringAndSize(s, len - 1);
3792 if (!pid) return -1;
3793
3794 if (PyList_Check(self->pers_func)) {
3795 if (PyList_Append(self->pers_func, pid) < 0) {
3796 Py_DECREF(pid);
3797 return -1;
3798 }
3799 }
3800 else {
3801 ARG_TUP(self, pid);
3802 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00003803 pid = PyObject_Call(self->pers_func, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003804 NULL);
3805 FREE_ARG_TUP(self);
3806 }
3807 }
3808
3809 if (! pid) return -1;
3810
3811 PDATA_PUSH(self->stack, pid, -1);
3812 return 0;
3813 }
3814 else {
3815 PyErr_SetString(UnpicklingError,
3816 "A load persistent id instruction was encountered,\n"
3817 "but no persistent_load function was specified.");
3818 return -1;
3819 }
3820}
3821
3822static int
Tim Peterscba30e22003-02-01 06:24:36 +00003823load_binpersid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003824{
3825 PyObject *pid = 0;
3826
3827 if (self->pers_func) {
3828 PDATA_POP(self->stack, pid);
3829 if (! pid) return -1;
3830
3831 if (PyList_Check(self->pers_func)) {
3832 if (PyList_Append(self->pers_func, pid) < 0) {
3833 Py_DECREF(pid);
3834 return -1;
3835 }
3836 }
3837 else {
3838 ARG_TUP(self, pid);
3839 if (self->arg) {
3840 pid = PyObject_Call(self->pers_func, self->arg,
3841 NULL);
3842 FREE_ARG_TUP(self);
3843 }
3844 if (! pid) return -1;
3845 }
3846
3847 PDATA_PUSH(self->stack, pid, -1);
3848 return 0;
3849 }
3850 else {
3851 PyErr_SetString(UnpicklingError,
3852 "A load persistent id instruction was encountered,\n"
3853 "but no persistent_load function was specified.");
3854 return -1;
3855 }
3856}
3857
3858
3859static int
Tim Peterscba30e22003-02-01 06:24:36 +00003860load_pop(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003861{
3862 int len;
3863
3864 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
3865
3866 /* Note that we split the (pickle.py) stack into two stacks,
3867 an object stack and a mark stack. We have to be clever and
3868 pop the right one. We do this by looking at the top of the
3869 mark stack.
3870 */
3871
3872 if ((self->num_marks > 0) &&
3873 (self->marks[self->num_marks - 1] == len))
3874 self->num_marks--;
3875 else {
3876 len--;
3877 Py_DECREF(self->stack->data[len]);
3878 self->stack->length=len;
3879 }
3880
3881 return 0;
3882}
3883
3884
3885static int
Tim Peterscba30e22003-02-01 06:24:36 +00003886load_pop_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003887{
3888 int i;
3889
3890 if ((i = marker(self)) < 0)
3891 return -1;
3892
3893 Pdata_clear(self->stack, i);
3894
3895 return 0;
3896}
3897
3898
3899static int
Tim Peterscba30e22003-02-01 06:24:36 +00003900load_dup(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003901{
3902 PyObject *last;
3903 int len;
3904
3905 if ((len = self->stack->length) <= 0) return stackUnderflow();
3906 last=self->stack->data[len-1];
3907 Py_INCREF(last);
3908 PDATA_PUSH(self->stack, last, -1);
3909 return 0;
3910}
3911
3912
3913static int
Tim Peterscba30e22003-02-01 06:24:36 +00003914load_get(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003915{
3916 PyObject *py_str = 0, *value = 0;
3917 int len;
3918 char *s;
3919 int rc;
3920
Tim Peters0bc93f52003-02-02 18:29:33 +00003921 if ((len = self->readline_func(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003922 if (len < 2) return bad_readline();
Tim Peters84e87f32001-03-17 04:50:51 +00003923
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003924 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003925
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003926 value = PyDict_GetItem(self->memo, py_str);
3927 if (! value) {
3928 PyErr_SetObject(BadPickleGet, py_str);
3929 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00003930 }
3931 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003932 PDATA_APPEND(self->stack, value, -1);
3933 rc = 0;
3934 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003935
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003936 Py_DECREF(py_str);
3937 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003938}
3939
3940
3941static int
Tim Peterscba30e22003-02-01 06:24:36 +00003942load_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003943{
3944 PyObject *py_key = 0, *value = 0;
3945 unsigned char key;
3946 char *s;
3947 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003948
Tim Peters0bc93f52003-02-02 18:29:33 +00003949 if (self->read_func(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003950
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003951 key = (unsigned char)s[0];
3952 if (!( py_key = PyInt_FromLong((long)key))) return -1;
Guido van Rossumea2b7152000-05-09 18:14:50 +00003953
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003954 value = PyDict_GetItem(self->memo, py_key);
3955 if (! value) {
3956 PyErr_SetObject(BadPickleGet, py_key);
3957 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00003958 }
3959 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003960 PDATA_APPEND(self->stack, value, -1);
3961 rc = 0;
3962 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003963
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003964 Py_DECREF(py_key);
3965 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003966}
3967
3968
3969static int
Tim Peterscba30e22003-02-01 06:24:36 +00003970load_long_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003971{
3972 PyObject *py_key = 0, *value = 0;
3973 unsigned char c;
3974 char *s;
3975 long key;
3976 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003977
Tim Peters0bc93f52003-02-02 18:29:33 +00003978 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003979
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003980 c = (unsigned char)s[0];
3981 key = (long)c;
3982 c = (unsigned char)s[1];
3983 key |= (long)c << 8;
3984 c = (unsigned char)s[2];
3985 key |= (long)c << 16;
3986 c = (unsigned char)s[3];
3987 key |= (long)c << 24;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003988
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003989 if (!( py_key = PyInt_FromLong((long)key))) return -1;
3990
3991 value = PyDict_GetItem(self->memo, py_key);
3992 if (! value) {
3993 PyErr_SetObject(BadPickleGet, py_key);
3994 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00003995 }
3996 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003997 PDATA_APPEND(self->stack, value, -1);
3998 rc = 0;
3999 }
4000
4001 Py_DECREF(py_key);
4002 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004003}
4004
Tim Peters2d629652003-02-04 05:06:17 +00004005/* Push an object from the extension registry (EXT[124]). nbytes is
4006 * the number of bytes following the opcode, holding the index (code) value.
4007 */
4008static int
4009load_extension(Unpicklerobject *self, int nbytes)
4010{
4011 char *codebytes; /* the nbytes bytes after the opcode */
4012 long code; /* calc_binint returns long */
4013 PyObject *py_code; /* code as a Python int */
4014 PyObject *obj; /* the object to push */
4015 PyObject *pair; /* (module_name, class_name) */
4016 PyObject *module_name, *class_name;
4017
4018 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4019 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4020 code = calc_binint(codebytes, nbytes);
4021 if (code <= 0) { /* note that 0 is forbidden */
4022 /* Corrupt or hostile pickle. */
4023 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
4024 return -1;
4025 }
4026
4027 /* Look for the code in the cache. */
4028 py_code = PyInt_FromLong(code);
4029 if (py_code == NULL) return -1;
4030 obj = PyDict_GetItem(extension_cache, py_code);
4031 if (obj != NULL) {
4032 /* Bingo. */
4033 Py_DECREF(py_code);
4034 PDATA_APPEND(self->stack, obj, -1);
4035 return 0;
4036 }
4037
4038 /* Look up the (module_name, class_name) pair. */
4039 pair = PyDict_GetItem(inverted_registry, py_code);
4040 if (pair == NULL) {
4041 Py_DECREF(py_code);
4042 PyErr_Format(PyExc_ValueError, "unregistered extension "
4043 "code %ld", code);
4044 return -1;
4045 }
4046 /* Since the extension registry is manipulable via Python code,
Tim Petersfa05ce32003-02-04 05:20:32 +00004047 * confirm that pair is really a 2-tuple of strings.
Tim Peters2d629652003-02-04 05:06:17 +00004048 */
4049 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
4050 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
4051 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
4052 Py_DECREF(py_code);
4053 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
4054 "isn't a 2-tuple of strings", code);
4055 return -1;
4056 }
4057 /* Load the object. */
4058 obj = find_class(module_name, class_name, self->find_class);
4059 if (obj == NULL) {
4060 Py_DECREF(py_code);
4061 return -1;
4062 }
4063 /* Cache code -> obj. */
4064 code = PyDict_SetItem(extension_cache, py_code, obj);
4065 Py_DECREF(py_code);
4066 if (code < 0) {
4067 Py_DECREF(obj);
4068 return -1;
4069 }
4070 PDATA_PUSH(self->stack, obj, -1);
4071 return 0;
4072}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004073
4074static int
Tim Peterscba30e22003-02-01 06:24:36 +00004075load_put(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004076{
4077 PyObject *py_str = 0, *value = 0;
4078 int len, l;
4079 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004080
Tim Peters0bc93f52003-02-02 18:29:33 +00004081 if ((l = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004082 if (l < 2) return bad_readline();
4083 if (!( len=self->stack->length )) return stackUnderflow();
4084 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
4085 value=self->stack->data[len-1];
4086 l=PyDict_SetItem(self->memo, py_str, value);
4087 Py_DECREF(py_str);
4088 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004089}
4090
4091
4092static int
Tim Peterscba30e22003-02-01 06:24:36 +00004093load_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004094{
4095 PyObject *py_key = 0, *value = 0;
4096 unsigned char key;
4097 char *s;
4098 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004099
Tim Peters0bc93f52003-02-02 18:29:33 +00004100 if (self->read_func(self, &s, 1) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004101 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004102
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004103 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00004104
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004105 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4106 value=self->stack->data[len-1];
4107 len=PyDict_SetItem(self->memo, py_key, value);
4108 Py_DECREF(py_key);
4109 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004110}
4111
4112
4113static int
Tim Peterscba30e22003-02-01 06:24:36 +00004114load_long_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004115{
4116 PyObject *py_key = 0, *value = 0;
4117 long key;
4118 unsigned char c;
4119 char *s;
4120 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004121
Tim Peters0bc93f52003-02-02 18:29:33 +00004122 if (self->read_func(self, &s, 4) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004123 if (!( len=self->stack->length )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004124
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004125 c = (unsigned char)s[0];
4126 key = (long)c;
4127 c = (unsigned char)s[1];
4128 key |= (long)c << 8;
4129 c = (unsigned char)s[2];
4130 key |= (long)c << 16;
4131 c = (unsigned char)s[3];
4132 key |= (long)c << 24;
Tim Peters84e87f32001-03-17 04:50:51 +00004133
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004134 if (!( py_key = PyInt_FromLong(key))) return -1;
4135 value=self->stack->data[len-1];
4136 len=PyDict_SetItem(self->memo, py_key, value);
4137 Py_DECREF(py_key);
4138 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004139}
4140
4141
4142static int
Tim Peterscba30e22003-02-01 06:24:36 +00004143do_append(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004144{
4145 PyObject *value = 0, *list = 0, *append_method = 0;
4146 int len, i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004147
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004148 len=self->stack->length;
4149 if (!( len >= x && x > 0 )) return stackUnderflow();
4150 /* nothing to do */
Tim Peterscba30e22003-02-01 06:24:36 +00004151 if (len==x) return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004152
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004153 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004154
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004155 if (PyList_Check(list)) {
4156 PyObject *slice;
4157 int list_len;
Tim Peters84e87f32001-03-17 04:50:51 +00004158
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004159 slice=Pdata_popList(self->stack, x);
4160 list_len = PyList_GET_SIZE(list);
4161 i=PyList_SetSlice(list, list_len, list_len, slice);
4162 Py_DECREF(slice);
4163 return i;
4164 }
4165 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004166
Tim Peterscba30e22003-02-01 06:24:36 +00004167 if (!( append_method = PyObject_GetAttr(list, append_str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004168 return -1;
4169
4170 for (i = x; i < len; i++) {
4171 PyObject *junk;
4172
4173 value=self->stack->data[i];
4174 junk=0;
4175 ARG_TUP(self, value);
4176 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00004177 junk = PyObject_Call(append_method, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004178 NULL);
4179 FREE_ARG_TUP(self);
4180 }
4181 if (! junk) {
4182 Pdata_clear(self->stack, i+1);
4183 self->stack->length=x;
4184 Py_DECREF(append_method);
4185 return -1;
4186 }
4187 Py_DECREF(junk);
4188 }
4189 self->stack->length=x;
4190 Py_DECREF(append_method);
4191 }
4192
4193 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004194}
4195
4196
4197static int
Tim Peterscba30e22003-02-01 06:24:36 +00004198load_append(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004199{
4200 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004201}
4202
4203
4204static int
Tim Peterscba30e22003-02-01 06:24:36 +00004205load_appends(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004206{
4207 return do_append(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004208}
4209
4210
4211static int
Tim Peterscba30e22003-02-01 06:24:36 +00004212do_setitems(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004213{
4214 PyObject *value = 0, *key = 0, *dict = 0;
4215 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004216
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004217 if (!( (len=self->stack->length) >= x
4218 && x > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004219
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004220 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004221
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004222 for (i = x+1; i < len; i += 2) {
4223 key =self->stack->data[i-1];
4224 value=self->stack->data[i ];
4225 if (PyObject_SetItem(dict, key, value) < 0) {
4226 r=-1;
4227 break;
4228 }
4229 }
4230
4231 Pdata_clear(self->stack, x);
4232
4233 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004234}
4235
4236
Tim Peters84e87f32001-03-17 04:50:51 +00004237static int
Tim Peterscba30e22003-02-01 06:24:36 +00004238load_setitem(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004239{
4240 return do_setitems(self, self->stack->length - 2);
4241}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004242
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004243static int
Tim Peterscba30e22003-02-01 06:24:36 +00004244load_setitems(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004245{
4246 return do_setitems(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004247}
4248
Tim Peters84e87f32001-03-17 04:50:51 +00004249
Guido van Rossum60456fd1997-04-09 17:36:32 +00004250static int
Tim Peterscba30e22003-02-01 06:24:36 +00004251load_build(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004252{
Tim Peters080c88b2003-02-15 03:01:11 +00004253 PyObject *state, *inst, *slotstate;
4254 PyObject *__setstate__;
4255 PyObject *d_key, *d_value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004256 Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00004257 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004258
Tim Peters080c88b2003-02-15 03:01:11 +00004259 /* Stack is ... instance, state. We want to leave instance at
4260 * the stack top, possibly mutated via instance.__setstate__(state).
4261 */
4262 if (self->stack->length < 2)
4263 return stackUnderflow();
4264 PDATA_POP(self->stack, state);
4265 if (state == NULL)
4266 return -1;
4267 inst = self->stack->data[self->stack->length - 1];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004268
Tim Peters080c88b2003-02-15 03:01:11 +00004269 __setstate__ = PyObject_GetAttr(inst, __setstate___str);
4270 if (__setstate__ != NULL) {
4271 PyObject *junk = NULL;
4272
4273 /* The explicit __setstate__ is responsible for everything. */
4274 ARG_TUP(self, state);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004275 if (self->arg) {
4276 junk = PyObject_Call(__setstate__, self->arg, NULL);
4277 FREE_ARG_TUP(self);
4278 }
4279 Py_DECREF(__setstate__);
Tim Peters080c88b2003-02-15 03:01:11 +00004280 if (junk == NULL)
4281 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004282 Py_DECREF(junk);
4283 return 0;
4284 }
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00004285 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4286 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004287 PyErr_Clear();
Tim Peters080c88b2003-02-15 03:01:11 +00004288
4289 /* A default __setstate__. First see whether state embeds a
4290 * slot state dict too (a proto 2 addition).
4291 */
4292 if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
4293 PyObject *temp = state;
4294 state = PyTuple_GET_ITEM(temp, 0);
4295 slotstate = PyTuple_GET_ITEM(temp, 1);
4296 Py_INCREF(state);
4297 Py_INCREF(slotstate);
4298 Py_DECREF(temp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004299 }
Tim Peters080c88b2003-02-15 03:01:11 +00004300 else
4301 slotstate = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004302
Tim Peters080c88b2003-02-15 03:01:11 +00004303 /* Set inst.__dict__ from the state dict (if any). */
4304 if (state != Py_None) {
4305 PyObject *dict;
4306 if (! PyDict_Check(state)) {
4307 PyErr_SetString(UnpicklingError, "state is not a "
4308 "dictionary");
4309 goto finally;
4310 }
4311 dict = PyObject_GetAttr(inst, __dict___str);
4312 if (dict == NULL)
4313 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004314
Tim Peters080c88b2003-02-15 03:01:11 +00004315 i = 0;
4316 while (PyDict_Next(state, &i, &d_key, &d_value)) {
4317 if (PyObject_SetItem(dict, d_key, d_value) < 0)
4318 goto finally;
4319 }
4320 Py_DECREF(dict);
4321 }
4322
4323 /* Also set instance attributes from the slotstate dict (if any). */
4324 if (slotstate != NULL) {
4325 if (! PyDict_Check(slotstate)) {
4326 PyErr_SetString(UnpicklingError, "slot state is not "
4327 "a dictionary");
4328 goto finally;
4329 }
4330 i = 0;
4331 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
4332 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
4333 goto finally;
4334 }
4335 }
4336 res = 0;
4337
4338 finally:
4339 Py_DECREF(state);
4340 Py_XDECREF(slotstate);
4341 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004342}
4343
4344
4345static int
Tim Peterscba30e22003-02-01 06:24:36 +00004346load_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004347{
4348 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004349
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004350 /* Note that we split the (pickle.py) stack into two stacks, an
4351 object stack and a mark stack. Here we push a mark onto the
4352 mark stack.
4353 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00004354
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004355 if ((self->num_marks + 1) >= self->marks_size) {
4356 s=self->marks_size+20;
4357 if (s <= self->num_marks) s=self->num_marks + 1;
4358 if (self->marks == NULL)
4359 self->marks=(int *)malloc(s * sizeof(int));
4360 else
Tim Peterscba30e22003-02-01 06:24:36 +00004361 self->marks=(int *)realloc(self->marks,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004362 s * sizeof(int));
4363 if (! self->marks) {
4364 PyErr_NoMemory();
4365 return -1;
4366 }
4367 self->marks_size = s;
4368 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004369
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004370 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004371
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004372 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004373}
4374
Guido van Rossum60456fd1997-04-09 17:36:32 +00004375static int
Tim Peterscba30e22003-02-01 06:24:36 +00004376load_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004377{
4378 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004379
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004380 PDATA_POP(self->stack, arg_tup);
4381 if (! arg_tup) return -1;
4382 PDATA_POP(self->stack, callable);
4383 if (callable) {
4384 ob = Instance_New(callable, arg_tup);
4385 Py_DECREF(callable);
4386 }
4387 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004388
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004389 if (! ob) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004390
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004391 PDATA_PUSH(self->stack, ob, -1);
4392 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004393}
Tim Peters84e87f32001-03-17 04:50:51 +00004394
Tim Peters4190fb82003-02-02 16:09:05 +00004395/* Just raises an error if we don't know the protocol specified. PROTO
4396 * is the first opcode for protocols >= 2.
4397 */
4398static int
4399load_proto(Unpicklerobject *self)
4400{
4401 int i;
4402 char *protobyte;
4403
4404 i = self->read_func(self, &protobyte, 1);
4405 if (i < 0)
4406 return -1;
4407
4408 i = calc_binint(protobyte, 1);
4409 /* No point checking for < 0, since calc_binint returns an unsigned
4410 * int when chewing on 1 byte.
4411 */
4412 assert(i >= 0);
Tim Peters8587b3c2003-02-13 15:44:41 +00004413 if (i <= HIGHEST_PROTOCOL)
Tim Peters4190fb82003-02-02 16:09:05 +00004414 return 0;
4415
4416 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4417 return -1;
4418}
4419
Guido van Rossum60456fd1997-04-09 17:36:32 +00004420static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004421load(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004422{
4423 PyObject *err = 0, *val = 0;
4424 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004425
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004426 self->num_marks = 0;
4427 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004428
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004429 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004430 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004431 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004432
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004433 switch (s[0]) {
4434 case NONE:
4435 if (load_none(self) < 0)
4436 break;
4437 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004438
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004439 case BININT:
4440 if (load_binint(self) < 0)
4441 break;
4442 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004443
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004444 case BININT1:
4445 if (load_binint1(self) < 0)
4446 break;
4447 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004448
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004449 case BININT2:
4450 if (load_binint2(self) < 0)
4451 break;
4452 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004453
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004454 case INT:
4455 if (load_int(self) < 0)
4456 break;
4457 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004458
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004459 case LONG:
4460 if (load_long(self) < 0)
4461 break;
4462 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004463
Tim Petersee1a53c2003-02-02 02:57:53 +00004464 case LONG1:
4465 if (load_counted_long(self, 1) < 0)
4466 break;
4467 continue;
4468
4469 case LONG4:
4470 if (load_counted_long(self, 4) < 0)
4471 break;
4472 continue;
4473
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004474 case FLOAT:
4475 if (load_float(self) < 0)
4476 break;
4477 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004478
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004479 case BINFLOAT:
4480 if (load_binfloat(self) < 0)
4481 break;
4482 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004483
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004484 case BINSTRING:
4485 if (load_binstring(self) < 0)
4486 break;
4487 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004488
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004489 case SHORT_BINSTRING:
4490 if (load_short_binstring(self) < 0)
4491 break;
4492 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004493
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004494 case STRING:
4495 if (load_string(self) < 0)
4496 break;
4497 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004498
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004499#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004500 case UNICODE:
4501 if (load_unicode(self) < 0)
4502 break;
4503 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004504
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004505 case BINUNICODE:
4506 if (load_binunicode(self) < 0)
4507 break;
4508 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004509#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004510
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004511 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004512 if (load_counted_tuple(self, 0) < 0)
4513 break;
4514 continue;
4515
4516 case TUPLE1:
4517 if (load_counted_tuple(self, 1) < 0)
4518 break;
4519 continue;
4520
4521 case TUPLE2:
4522 if (load_counted_tuple(self, 2) < 0)
4523 break;
4524 continue;
4525
4526 case TUPLE3:
4527 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004528 break;
4529 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004530
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004531 case TUPLE:
4532 if (load_tuple(self) < 0)
4533 break;
4534 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004535
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004536 case EMPTY_LIST:
4537 if (load_empty_list(self) < 0)
4538 break;
4539 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004540
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004541 case LIST:
4542 if (load_list(self) < 0)
4543 break;
4544 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004545
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004546 case EMPTY_DICT:
4547 if (load_empty_dict(self) < 0)
4548 break;
4549 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004550
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004551 case DICT:
4552 if (load_dict(self) < 0)
4553 break;
4554 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004555
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004556 case OBJ:
4557 if (load_obj(self) < 0)
4558 break;
4559 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004560
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004561 case INST:
4562 if (load_inst(self) < 0)
4563 break;
4564 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004565
Tim Peterseab7db32003-02-13 18:24:14 +00004566 case NEWOBJ:
4567 if (load_newobj(self) < 0)
4568 break;
4569 continue;
4570
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004571 case GLOBAL:
4572 if (load_global(self) < 0)
4573 break;
4574 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004575
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004576 case APPEND:
4577 if (load_append(self) < 0)
4578 break;
4579 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004580
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004581 case APPENDS:
4582 if (load_appends(self) < 0)
4583 break;
4584 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004585
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004586 case BUILD:
4587 if (load_build(self) < 0)
4588 break;
4589 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004590
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004591 case DUP:
4592 if (load_dup(self) < 0)
4593 break;
4594 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004595
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004596 case BINGET:
4597 if (load_binget(self) < 0)
4598 break;
4599 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004600
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004601 case LONG_BINGET:
4602 if (load_long_binget(self) < 0)
4603 break;
4604 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004605
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004606 case GET:
4607 if (load_get(self) < 0)
4608 break;
4609 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004610
Tim Peters2d629652003-02-04 05:06:17 +00004611 case EXT1:
4612 if (load_extension(self, 1) < 0)
4613 break;
4614 continue;
4615
4616 case EXT2:
4617 if (load_extension(self, 2) < 0)
4618 break;
4619 continue;
4620
4621 case EXT4:
4622 if (load_extension(self, 4) < 0)
4623 break;
4624 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004625 case MARK:
4626 if (load_mark(self) < 0)
4627 break;
4628 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004629
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004630 case BINPUT:
4631 if (load_binput(self) < 0)
4632 break;
4633 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004634
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004635 case LONG_BINPUT:
4636 if (load_long_binput(self) < 0)
4637 break;
4638 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004639
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004640 case PUT:
4641 if (load_put(self) < 0)
4642 break;
4643 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004644
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004645 case POP:
4646 if (load_pop(self) < 0)
4647 break;
4648 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004649
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004650 case POP_MARK:
4651 if (load_pop_mark(self) < 0)
4652 break;
4653 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004654
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004655 case SETITEM:
4656 if (load_setitem(self) < 0)
4657 break;
4658 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004659
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004660 case SETITEMS:
4661 if (load_setitems(self) < 0)
4662 break;
4663 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004664
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004665 case STOP:
4666 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004667
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004668 case PERSID:
4669 if (load_persid(self) < 0)
4670 break;
4671 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004672
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004673 case BINPERSID:
4674 if (load_binpersid(self) < 0)
4675 break;
4676 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004677
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004678 case REDUCE:
4679 if (load_reduce(self) < 0)
4680 break;
4681 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004682
Tim Peters4190fb82003-02-02 16:09:05 +00004683 case PROTO:
4684 if (load_proto(self) < 0)
4685 break;
4686 continue;
4687
Tim Peters3c67d792003-02-02 17:59:11 +00004688 case NEWTRUE:
4689 if (load_bool(self, Py_True) < 0)
4690 break;
4691 continue;
4692
4693 case NEWFALSE:
4694 if (load_bool(self, Py_False) < 0)
4695 break;
4696 continue;
4697
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004698 case '\0':
4699 /* end of file */
4700 PyErr_SetNone(PyExc_EOFError);
4701 break;
Tim Peterscba30e22003-02-01 06:24:36 +00004702
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004703 default:
Tim Peterscba30e22003-02-01 06:24:36 +00004704 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004705 "invalid load key, '%s'.",
4706 "c", s[0]);
4707 return NULL;
4708 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004709
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004710 break;
4711 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004712
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004713 if ((err = PyErr_Occurred())) {
4714 if (err == PyExc_EOFError) {
4715 PyErr_SetNone(PyExc_EOFError);
4716 }
4717 return NULL;
4718 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004719
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004720 PDATA_POP(self->stack, val);
4721 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004722}
Tim Peters84e87f32001-03-17 04:50:51 +00004723
Guido van Rossum60456fd1997-04-09 17:36:32 +00004724
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004725/* No-load functions to support noload, which is used to
4726 find persistent references. */
4727
4728static int
Tim Peterscba30e22003-02-01 06:24:36 +00004729noload_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004730{
4731 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004732
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004733 if ((i = marker(self)) < 0) return -1;
4734 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004735}
4736
4737
4738static int
Tim Peterscba30e22003-02-01 06:24:36 +00004739noload_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004740{
4741 int i;
4742 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004743
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004744 if ((i = marker(self)) < 0) return -1;
4745 Pdata_clear(self->stack, i);
Tim Peters0bc93f52003-02-02 18:29:33 +00004746 if (self->readline_func(self, &s) < 0) return -1;
4747 if (self->readline_func(self, &s) < 0) return -1;
Tim Peterseab7db32003-02-13 18:24:14 +00004748 PDATA_APPEND(self->stack, Py_None, -1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004749 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004750}
4751
4752static int
Tim Peterseab7db32003-02-13 18:24:14 +00004753noload_newobj(Unpicklerobject *self)
4754{
4755 PyObject *obj;
4756
4757 PDATA_POP(self->stack, obj); /* pop argtuple */
4758 if (obj == NULL) return -1;
4759 Py_DECREF(obj);
4760
4761 PDATA_POP(self->stack, obj); /* pop cls */
4762 if (obj == NULL) return -1;
4763 Py_DECREF(obj);
4764
4765 PDATA_APPEND(self->stack, Py_None, -1);
4766 return 0;
4767}
4768
4769static int
Tim Peterscba30e22003-02-01 06:24:36 +00004770noload_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004771{
4772 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004773
Tim Peters0bc93f52003-02-02 18:29:33 +00004774 if (self->readline_func(self, &s) < 0) return -1;
4775 if (self->readline_func(self, &s) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004776 PDATA_APPEND(self->stack, Py_None,-1);
4777 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004778}
4779
4780static int
Tim Peterscba30e22003-02-01 06:24:36 +00004781noload_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004782{
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004783
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004784 if (self->stack->length < 2) return stackUnderflow();
4785 Pdata_clear(self->stack, self->stack->length-2);
4786 PDATA_APPEND(self->stack, Py_None,-1);
4787 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004788}
4789
4790static int
4791noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004792
Guido van Rossum053b8df1998-11-25 16:18:00 +00004793 if (self->stack->length < 1) return stackUnderflow();
4794 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00004795 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004796}
4797
Tim Peters2d629652003-02-04 05:06:17 +00004798static int
4799noload_extension(Unpicklerobject *self, int nbytes)
4800{
4801 char *codebytes;
4802
4803 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4804 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4805 PDATA_APPEND(self->stack, Py_None, -1);
4806 return 0;
4807}
4808
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004809
4810static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004811noload(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004812{
4813 PyObject *err = 0, *val = 0;
4814 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004815
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004816 self->num_marks = 0;
4817 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004818
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004819 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004820 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004821 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004822
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004823 switch (s[0]) {
4824 case NONE:
4825 if (load_none(self) < 0)
4826 break;
4827 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004828
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004829 case BININT:
4830 if (load_binint(self) < 0)
4831 break;
4832 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004833
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004834 case BININT1:
4835 if (load_binint1(self) < 0)
4836 break;
4837 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004838
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004839 case BININT2:
4840 if (load_binint2(self) < 0)
4841 break;
4842 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004843
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004844 case INT:
4845 if (load_int(self) < 0)
4846 break;
4847 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004848
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004849 case LONG:
4850 if (load_long(self) < 0)
4851 break;
4852 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004853
Tim Peters4190fb82003-02-02 16:09:05 +00004854 case LONG1:
4855 if (load_counted_long(self, 1) < 0)
4856 break;
4857 continue;
4858
4859 case LONG4:
4860 if (load_counted_long(self, 4) < 0)
4861 break;
4862 continue;
4863
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004864 case FLOAT:
4865 if (load_float(self) < 0)
4866 break;
4867 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004868
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004869 case BINFLOAT:
4870 if (load_binfloat(self) < 0)
4871 break;
4872 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004873
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004874 case BINSTRING:
4875 if (load_binstring(self) < 0)
4876 break;
4877 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004878
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004879 case SHORT_BINSTRING:
4880 if (load_short_binstring(self) < 0)
4881 break;
4882 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004883
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004884 case STRING:
4885 if (load_string(self) < 0)
4886 break;
4887 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004888
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004889#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004890 case UNICODE:
4891 if (load_unicode(self) < 0)
4892 break;
4893 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004894
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004895 case BINUNICODE:
4896 if (load_binunicode(self) < 0)
4897 break;
4898 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004899#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004900
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004901 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004902 if (load_counted_tuple(self, 0) < 0)
4903 break;
4904 continue;
4905
4906 case TUPLE1:
4907 if (load_counted_tuple(self, 1) < 0)
4908 break;
4909 continue;
4910
4911 case TUPLE2:
4912 if (load_counted_tuple(self, 2) < 0)
4913 break;
4914 continue;
4915
4916 case TUPLE3:
4917 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004918 break;
4919 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004920
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004921 case TUPLE:
4922 if (load_tuple(self) < 0)
4923 break;
4924 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004925
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004926 case EMPTY_LIST:
4927 if (load_empty_list(self) < 0)
4928 break;
4929 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004930
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004931 case LIST:
4932 if (load_list(self) < 0)
4933 break;
4934 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004935
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004936 case EMPTY_DICT:
4937 if (load_empty_dict(self) < 0)
4938 break;
4939 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004940
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004941 case DICT:
4942 if (load_dict(self) < 0)
4943 break;
4944 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004945
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004946 case OBJ:
4947 if (noload_obj(self) < 0)
4948 break;
4949 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004950
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004951 case INST:
4952 if (noload_inst(self) < 0)
4953 break;
4954 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004955
Tim Peterseab7db32003-02-13 18:24:14 +00004956 case NEWOBJ:
4957 if (noload_newobj(self) < 0)
4958 break;
4959 continue;
4960
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004961 case GLOBAL:
4962 if (noload_global(self) < 0)
4963 break;
4964 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004965
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004966 case APPEND:
4967 if (load_append(self) < 0)
4968 break;
4969 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004970
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004971 case APPENDS:
4972 if (load_appends(self) < 0)
4973 break;
4974 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004975
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004976 case BUILD:
4977 if (noload_build(self) < 0)
4978 break;
4979 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004980
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004981 case DUP:
4982 if (load_dup(self) < 0)
4983 break;
4984 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004985
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004986 case BINGET:
4987 if (load_binget(self) < 0)
4988 break;
4989 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004990
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004991 case LONG_BINGET:
4992 if (load_long_binget(self) < 0)
4993 break;
4994 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004995
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004996 case GET:
4997 if (load_get(self) < 0)
4998 break;
4999 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005000
Tim Peters2d629652003-02-04 05:06:17 +00005001 case EXT1:
5002 if (noload_extension(self, 1) < 0)
5003 break;
5004 continue;
5005
5006 case EXT2:
5007 if (noload_extension(self, 2) < 0)
5008 break;
5009 continue;
5010
5011 case EXT4:
5012 if (noload_extension(self, 4) < 0)
5013 break;
5014 continue;
5015
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005016 case MARK:
5017 if (load_mark(self) < 0)
5018 break;
5019 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005020
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005021 case BINPUT:
5022 if (load_binput(self) < 0)
5023 break;
5024 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005025
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005026 case LONG_BINPUT:
5027 if (load_long_binput(self) < 0)
5028 break;
5029 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005030
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005031 case PUT:
5032 if (load_put(self) < 0)
5033 break;
5034 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005035
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005036 case POP:
5037 if (load_pop(self) < 0)
5038 break;
5039 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005040
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005041 case POP_MARK:
5042 if (load_pop_mark(self) < 0)
5043 break;
5044 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005045
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005046 case SETITEM:
5047 if (load_setitem(self) < 0)
5048 break;
5049 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005050
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005051 case SETITEMS:
5052 if (load_setitems(self) < 0)
5053 break;
5054 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005055
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005056 case STOP:
5057 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005058
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005059 case PERSID:
5060 if (load_persid(self) < 0)
5061 break;
5062 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005063
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005064 case BINPERSID:
5065 if (load_binpersid(self) < 0)
5066 break;
5067 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005068
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005069 case REDUCE:
5070 if (noload_reduce(self) < 0)
5071 break;
5072 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005073
Tim Peters4190fb82003-02-02 16:09:05 +00005074 case PROTO:
5075 if (load_proto(self) < 0)
5076 break;
5077 continue;
5078
Tim Peters3c67d792003-02-02 17:59:11 +00005079 case NEWTRUE:
5080 if (load_bool(self, Py_True) < 0)
5081 break;
5082 continue;
5083
5084 case NEWFALSE:
5085 if (load_bool(self, Py_False) < 0)
5086 break;
5087 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005088 default:
Tim Peterscba30e22003-02-01 06:24:36 +00005089 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005090 "invalid load key, '%s'.",
5091 "c", s[0]);
5092 return NULL;
5093 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005094
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005095 break;
5096 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005097
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005098 if ((err = PyErr_Occurred())) {
5099 if (err == PyExc_EOFError) {
5100 PyErr_SetNone(PyExc_EOFError);
5101 }
5102 return NULL;
5103 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005104
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005105 PDATA_POP(self->stack, val);
5106 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005107}
Tim Peters84e87f32001-03-17 04:50:51 +00005108
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005109
Guido van Rossum60456fd1997-04-09 17:36:32 +00005110static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005111Unpickler_load(Unpicklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005112{
Tim Peterscba30e22003-02-01 06:24:36 +00005113 if (!( PyArg_ParseTuple(args, ":load")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005114 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005115
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005116 return load(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005117}
5118
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005119static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005120Unpickler_noload(Unpicklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005121{
Tim Peterscba30e22003-02-01 06:24:36 +00005122 if (!( PyArg_ParseTuple(args, ":noload")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005123 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005124
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005125 return noload(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005126}
5127
Guido van Rossum60456fd1997-04-09 17:36:32 +00005128
5129static struct PyMethodDef Unpickler_methods[] = {
Neal Norwitzb0493252002-03-31 14:44:22 +00005130 {"load", (PyCFunction)Unpickler_load, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005131 PyDoc_STR("load() -- Load a pickle")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005132 },
Neal Norwitzb0493252002-03-31 14:44:22 +00005133 {"noload", (PyCFunction)Unpickler_noload, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005134 PyDoc_STR(
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005135 "noload() -- not load a pickle, but go through most of the motions\n"
5136 "\n"
5137 "This function can be used to read past a pickle without instantiating\n"
5138 "any objects or importing any modules. It can also be used to find all\n"
5139 "persistent references without instantiating any objects or importing\n"
Neal Norwitz200788c2002-08-13 22:20:41 +00005140 "any modules.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005141 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00005142 {NULL, NULL} /* sentinel */
5143};
5144
5145
5146static Unpicklerobject *
Tim Peterscba30e22003-02-01 06:24:36 +00005147newUnpicklerobject(PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005148{
5149 Unpicklerobject *self;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005150
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005151 if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005152 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005153
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005154 self->file = NULL;
5155 self->arg = NULL;
5156 self->stack = (Pdata*)Pdata_New();
5157 self->pers_func = NULL;
5158 self->last_string = NULL;
5159 self->marks = NULL;
5160 self->num_marks = 0;
5161 self->marks_size = 0;
5162 self->buf_size = 0;
5163 self->read = NULL;
5164 self->readline = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005165 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005166
Tim Peterscba30e22003-02-01 06:24:36 +00005167 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005168 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005169
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005170 Py_INCREF(f);
5171 self->file = f;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005172
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005173 /* Set read, readline based on type of f */
5174 if (PyFile_Check(f)) {
5175 self->fp = PyFile_AsFile(f);
5176 if (self->fp == NULL) {
Tim Peterscba30e22003-02-01 06:24:36 +00005177 PyErr_SetString(PyExc_ValueError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005178 "I/O operation on closed file");
5179 goto err;
5180 }
5181 self->read_func = read_file;
5182 self->readline_func = readline_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00005183 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005184 else if (PycStringIO_InputCheck(f)) {
5185 self->fp = NULL;
5186 self->read_func = read_cStringIO;
5187 self->readline_func = readline_cStringIO;
5188 }
5189 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00005190
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005191 self->fp = NULL;
5192 self->read_func = read_other;
5193 self->readline_func = readline_other;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005194
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005195 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5196 (self->read = PyObject_GetAttr(f, read_str)))) {
5197 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00005198 PyErr_SetString( PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005199 "argument must have 'read' and "
5200 "'readline' attributes" );
5201 goto err;
5202 }
5203 }
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005204 PyObject_GC_Track(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005205
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005206 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005207
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005208 err:
5209 Py_DECREF((PyObject *)self);
5210 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005211}
5212
5213
5214static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005215get_Unpickler(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005216{
5217 PyObject *file;
Tim Peters84e87f32001-03-17 04:50:51 +00005218
Tim Peterscba30e22003-02-01 06:24:36 +00005219 if (!( PyArg_ParseTuple(args, "O:Unpickler", &file)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005220 return NULL;
5221 return (PyObject *)newUnpicklerobject(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005222}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005223
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005224
Guido van Rossum60456fd1997-04-09 17:36:32 +00005225static void
Tim Peterscba30e22003-02-01 06:24:36 +00005226Unpickler_dealloc(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005227{
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005228 PyObject_GC_UnTrack((PyObject *)self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005229 Py_XDECREF(self->readline);
5230 Py_XDECREF(self->read);
5231 Py_XDECREF(self->file);
5232 Py_XDECREF(self->memo);
5233 Py_XDECREF(self->stack);
5234 Py_XDECREF(self->pers_func);
5235 Py_XDECREF(self->arg);
5236 Py_XDECREF(self->last_string);
Jeremy Hyltonfff093f2003-07-11 19:42:49 +00005237 Py_XDECREF(self->find_class);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005238
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005239 if (self->marks) {
5240 free(self->marks);
5241 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005242
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005243 if (self->buf_size) {
5244 free(self->buf);
5245 }
Tim Peters84e87f32001-03-17 04:50:51 +00005246
Tim Peters3cfe7542003-05-21 21:29:48 +00005247 self->ob_type->tp_free((PyObject *)self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005248}
5249
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005250static int
5251Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg)
5252{
Thomas Woutersc6e55062006-04-15 21:47:09 +00005253 Py_VISIT(self->readline);
5254 Py_VISIT(self->read);
5255 Py_VISIT(self->file);
5256 Py_VISIT(self->memo);
5257 Py_VISIT(self->stack);
5258 Py_VISIT(self->pers_func);
5259 Py_VISIT(self->arg);
5260 Py_VISIT(self->last_string);
5261 Py_VISIT(self->find_class);
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005262 return 0;
5263}
5264
5265static int
5266Unpickler_clear(Unpicklerobject *self)
5267{
Thomas Woutersedf17d82006-04-15 17:28:34 +00005268 Py_CLEAR(self->readline);
5269 Py_CLEAR(self->read);
5270 Py_CLEAR(self->file);
5271 Py_CLEAR(self->memo);
5272 Py_CLEAR(self->stack);
5273 Py_CLEAR(self->pers_func);
5274 Py_CLEAR(self->arg);
5275 Py_CLEAR(self->last_string);
5276 Py_CLEAR(self->find_class);
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005277 return 0;
5278}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005279
5280static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005281Unpickler_getattr(Unpicklerobject *self, char *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005282{
5283 if (!strcmp(name, "persistent_load")) {
5284 if (!self->pers_func) {
5285 PyErr_SetString(PyExc_AttributeError, name);
5286 return NULL;
5287 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005288
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005289 Py_INCREF(self->pers_func);
5290 return self->pers_func;
5291 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005292
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005293 if (!strcmp(name, "find_global")) {
5294 if (!self->find_class) {
5295 PyErr_SetString(PyExc_AttributeError, name);
5296 return NULL;
5297 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005298
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005299 Py_INCREF(self->find_class);
5300 return self->find_class;
5301 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005302
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005303 if (!strcmp(name, "memo")) {
5304 if (!self->memo) {
5305 PyErr_SetString(PyExc_AttributeError, name);
5306 return NULL;
5307 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005308
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005309 Py_INCREF(self->memo);
5310 return self->memo;
5311 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005312
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005313 if (!strcmp(name, "UnpicklingError")) {
5314 Py_INCREF(UnpicklingError);
5315 return UnpicklingError;
5316 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005317
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005318 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005319}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005320
Guido van Rossum60456fd1997-04-09 17:36:32 +00005321
5322static int
Tim Peterscba30e22003-02-01 06:24:36 +00005323Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005324{
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005325
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005326 if (!strcmp(name, "persistent_load")) {
5327 Py_XDECREF(self->pers_func);
5328 self->pers_func = value;
5329 Py_XINCREF(value);
5330 return 0;
5331 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005332
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005333 if (!strcmp(name, "find_global")) {
5334 Py_XDECREF(self->find_class);
5335 self->find_class = value;
5336 Py_XINCREF(value);
5337 return 0;
5338 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005339
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005340 if (! value) {
5341 PyErr_SetString(PyExc_TypeError,
5342 "attribute deletion is not supported");
5343 return -1;
5344 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005345
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005346 if (strcmp(name, "memo") == 0) {
5347 if (!PyDict_Check(value)) {
Tim Peterscba30e22003-02-01 06:24:36 +00005348 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005349 "memo must be a dictionary");
5350 return -1;
5351 }
5352 Py_XDECREF(self->memo);
5353 self->memo = value;
5354 Py_INCREF(value);
5355 return 0;
5356 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005357
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005358 PyErr_SetString(PyExc_AttributeError, name);
5359 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005360}
5361
Tim Peters5bd2a792003-02-01 16:45:06 +00005362/* ---------------------------------------------------------------------------
5363 * Module-level functions.
5364 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005365
Martin v. Löwis544f1192004-07-27 05:22:33 +00005366/* dump(obj, file, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005367static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005368cpm_dump(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005369{
Martin v. Löwis15e62742006-02-27 16:46:16 +00005370 static char *kwlist[] = {"obj", "file", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005371 PyObject *ob, *file, *res = NULL;
5372 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005373 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005374
Martin v. Löwis544f1192004-07-27 05:22:33 +00005375 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist,
5376 &ob, &file, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005377 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005378
Tim Peters5bd2a792003-02-01 16:45:06 +00005379 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005380 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005381
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005382 if (dump(pickler, ob) < 0)
5383 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005384
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005385 Py_INCREF(Py_None);
5386 res = Py_None;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005387
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005388 finally:
5389 Py_XDECREF(pickler);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005390
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005391 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005392}
5393
5394
Martin v. Löwis544f1192004-07-27 05:22:33 +00005395/* dumps(obj, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005396static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005397cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005398{
Martin v. Löwis15e62742006-02-27 16:46:16 +00005399 static char *kwlist[] = {"obj", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005400 PyObject *ob, *file = 0, *res = NULL;
5401 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005402 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005403
Martin v. Löwis544f1192004-07-27 05:22:33 +00005404 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist,
5405 &ob, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005406 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005407
Tim Peterscba30e22003-02-01 06:24:36 +00005408 if (!( file = PycStringIO->NewOutput(128)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005409 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005410
Tim Peters5bd2a792003-02-01 16:45:06 +00005411 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005412 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005413
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005414 if (dump(pickler, ob) < 0)
5415 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005416
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005417 res = PycStringIO->cgetvalue(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005418
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005419 finally:
5420 Py_XDECREF(pickler);
5421 Py_XDECREF(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005422
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005423 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00005424}
5425
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005426
Tim Peters5bd2a792003-02-01 16:45:06 +00005427/* load(fileobj). */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005428static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005429cpm_load(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005430{
5431 Unpicklerobject *unpickler = 0;
5432 PyObject *ob, *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005433
Tim Peterscba30e22003-02-01 06:24:36 +00005434 if (!( PyArg_ParseTuple(args, "O:load", &ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005435 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005436
Tim Peterscba30e22003-02-01 06:24:36 +00005437 if (!( unpickler = newUnpicklerobject(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005438 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005439
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005440 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005441
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005442 finally:
5443 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005444
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005445 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005446}
5447
5448
Tim Peters5bd2a792003-02-01 16:45:06 +00005449/* loads(string) */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005450static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005451cpm_loads(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005452{
5453 PyObject *ob, *file = 0, *res = NULL;
5454 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005455
Tim Peterscba30e22003-02-01 06:24:36 +00005456 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005457 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005458
Tim Peterscba30e22003-02-01 06:24:36 +00005459 if (!( file = PycStringIO->NewInput(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005460 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00005461
Tim Peterscba30e22003-02-01 06:24:36 +00005462 if (!( unpickler = newUnpicklerobject(file)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005463 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005464
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005465 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005466
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005467 finally:
5468 Py_XDECREF(file);
5469 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005470
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005471 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005472}
5473
5474
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005475PyDoc_STRVAR(Unpicklertype__doc__,
5476"Objects that know how to unpickle");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005477
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005478static PyTypeObject Unpicklertype = {
5479 PyObject_HEAD_INIT(NULL)
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005480 0, /*ob_size*/
5481 "cPickle.Unpickler", /*tp_name*/
5482 sizeof(Unpicklerobject), /*tp_basicsize*/
5483 0,
5484 (destructor)Unpickler_dealloc, /* tp_dealloc */
5485 0, /* tp_print */
5486 (getattrfunc)Unpickler_getattr, /* tp_getattr */
5487 (setattrfunc)Unpickler_setattr, /* tp_setattr */
5488 0, /* tp_compare */
5489 0, /* tp_repr */
5490 0, /* tp_as_number */
5491 0, /* tp_as_sequence */
5492 0, /* tp_as_mapping */
5493 0, /* tp_hash */
5494 0, /* tp_call */
5495 0, /* tp_str */
5496 0, /* tp_getattro */
5497 0, /* tp_setattro */
5498 0, /* tp_as_buffer */
5499 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
5500 Unpicklertype__doc__, /* tp_doc */
5501 (traverseproc)Unpickler_traverse, /* tp_traverse */
5502 (inquiry)Unpickler_clear, /* tp_clear */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005503};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005504
Guido van Rossum60456fd1997-04-09 17:36:32 +00005505static struct PyMethodDef cPickle_methods[] = {
Martin v. Löwis544f1192004-07-27 05:22:33 +00005506 {"dump", (PyCFunction)cpm_dump, METH_VARARGS | METH_KEYWORDS,
5507 PyDoc_STR("dump(obj, file, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005508 "Write an object in pickle format to the given file.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005509 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005510 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005511 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005512
Martin v. Löwis544f1192004-07-27 05:22:33 +00005513 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS | METH_KEYWORDS,
5514 PyDoc_STR("dumps(obj, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005515 "Return a string containing an object in pickle format.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005516 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005517 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005518 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005519
Neal Norwitzb0493252002-03-31 14:44:22 +00005520 {"load", (PyCFunction)cpm_load, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005521 PyDoc_STR("load(file) -- Load a pickle from the given file")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005522
Neal Norwitzb0493252002-03-31 14:44:22 +00005523 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005524 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005525
Martin v. Löwis544f1192004-07-27 05:22:33 +00005526 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS | METH_KEYWORDS,
5527 PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005528 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005529 "This takes a file-like object for writing a pickle data stream.\n"
5530 "The optional proto argument tells the pickler to use the given\n"
5531 "protocol; supported protocols are 0, 1, 2. The default\n"
5532 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5533 "only protocol that can be written to a file opened in text\n"
5534 "mode and read back successfully. When using a protocol higher\n"
5535 "than 0, make sure the file is opened in binary mode, both when\n"
5536 "pickling and unpickling.)\n"
5537 "\n"
5538 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5539 "more efficient than protocol 1.\n"
5540 "\n"
5541 "Specifying a negative protocol version selects the highest\n"
5542 "protocol version supported. The higher the protocol used, the\n"
5543 "more recent the version of Python needed to read the pickle\n"
5544 "produced.\n"
5545 "\n"
5546 "The file parameter must have a write() method that accepts a single\n"
5547 "string argument. It can thus be an open file object, a StringIO\n"
5548 "object, or any other custom object that meets this interface.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005549 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005550
Neal Norwitzb0493252002-03-31 14:44:22 +00005551 {"Unpickler", (PyCFunction)get_Unpickler, METH_VARARGS,
Tim Peters5bd2a792003-02-01 16:45:06 +00005552 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5553
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005554 { NULL, NULL }
5555};
5556
Guido van Rossum60456fd1997-04-09 17:36:32 +00005557static int
Tim Peterscba30e22003-02-01 06:24:36 +00005558init_stuff(PyObject *module_dict)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005559{
5560 PyObject *copy_reg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005561
Martin v. Löwis43c9d8a2002-04-01 12:34:33 +00005562#define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005563
Tim Peters3cfe7542003-05-21 21:29:48 +00005564 if (PyType_Ready(&Unpicklertype) < 0)
5565 return -1;
5566 if (PyType_Ready(&Picklertype) < 0)
5567 return -1;
5568
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005569 INIT_STR(__class__);
5570 INIT_STR(__getinitargs__);
5571 INIT_STR(__dict__);
5572 INIT_STR(__getstate__);
5573 INIT_STR(__setstate__);
5574 INIT_STR(__name__);
5575 INIT_STR(__main__);
5576 INIT_STR(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00005577 INIT_STR(__reduce_ex__);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005578 INIT_STR(write);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005579 INIT_STR(append);
5580 INIT_STR(read);
5581 INIT_STR(readline);
5582 INIT_STR(copy_reg);
5583 INIT_STR(dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005584
Tim Peterscba30e22003-02-01 06:24:36 +00005585 if (!( copy_reg = PyImport_ImportModule("copy_reg")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005586 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005587
Tim Peters1f1b2d22003-02-01 02:16:37 +00005588 /* This is special because we want to use a different
5589 one in restricted mode. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005590 dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str);
Tim Peters5b7da392003-02-04 00:21:07 +00005591 if (!dispatch_table) return -1;
5592
5593 extension_registry = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005594 "_extension_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005595 if (!extension_registry) return -1;
5596
5597 inverted_registry = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005598 "_inverted_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005599 if (!inverted_registry) return -1;
5600
5601 extension_cache = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005602 "_extension_cache");
Tim Peters5b7da392003-02-04 00:21:07 +00005603 if (!extension_cache) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005604
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005605 Py_DECREF(copy_reg);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005606
Tim Peters731098b2003-02-04 20:56:09 +00005607 if (!(empty_tuple = PyTuple_New(0)))
5608 return -1;
5609
5610 two_tuple = PyTuple_New(2);
5611 if (two_tuple == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005612 return -1;
Tim Peters3e667d52003-02-04 21:47:44 +00005613 /* We use this temp container with no regard to refcounts, or to
5614 * keeping containees alive. Exempt from GC, because we don't
5615 * want anything looking at two_tuple() by magic.
5616 */
5617 PyObject_GC_UnTrack(two_tuple);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005618
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005619 /* Ugh */
5620 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5621 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5622 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005623
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005624 if (!( t=PyDict_New())) return -1;
5625 if (!( r=PyRun_String(
5626 "def __init__(self, *args): self.args=args\n\n"
5627 "def __str__(self):\n"
5628 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5629 Py_file_input,
5630 module_dict, t) )) return -1;
5631 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005632
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005633 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
Tim Peterscba30e22003-02-01 06:24:36 +00005634 if (!PickleError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005635 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005636
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005637 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005638
Tim Peterscba30e22003-02-01 06:24:36 +00005639 PicklingError = PyErr_NewException("cPickle.PicklingError",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005640 PickleError, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00005641 if (!PicklingError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005642 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005643
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005644 if (!( t=PyDict_New())) return -1;
5645 if (!( r=PyRun_String(
5646 "def __init__(self, *args): self.args=args\n\n"
5647 "def __str__(self):\n"
5648 " a=self.args\n"
5649 " a=a and type(a[0]) or '(what)'\n"
5650 " return 'Cannot pickle %s objects' % a\n"
5651 , Py_file_input,
5652 module_dict, t) )) return -1;
5653 Py_DECREF(r);
Tim Peters84e87f32001-03-17 04:50:51 +00005654
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005655 if (!( UnpickleableError = PyErr_NewException(
Tim Peterscba30e22003-02-01 06:24:36 +00005656 "cPickle.UnpickleableError", PicklingError, t)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005657 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005658
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005659 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005660
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005661 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
Tim Peterscba30e22003-02-01 06:24:36 +00005662 PickleError, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005663 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005664
Martin v. Löwis658009a2002-09-16 17:26:24 +00005665 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5666 UnpicklingError, NULL)))
5667 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +00005668
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005669 if (PyDict_SetItemString(module_dict, "PickleError",
5670 PickleError) < 0)
5671 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005672
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005673 if (PyDict_SetItemString(module_dict, "PicklingError",
5674 PicklingError) < 0)
5675 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005676
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005677 if (PyDict_SetItemString(module_dict, "UnpicklingError",
5678 UnpicklingError) < 0)
5679 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005680
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005681 if (PyDict_SetItemString(module_dict, "UnpickleableError",
5682 UnpickleableError) < 0)
5683 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005684
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005685 if (PyDict_SetItemString(module_dict, "BadPickleGet",
5686 BadPickleGet) < 0)
5687 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005688
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005689 PycString_IMPORT;
Guido van Rossum053b8df1998-11-25 16:18:00 +00005690
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005691 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005692}
5693
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005694#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5695#define PyMODINIT_FUNC void
Guido van Rossumf9ffb031999-02-04 14:54:04 +00005696#endif
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005697PyMODINIT_FUNC
Tim Peterscba30e22003-02-01 06:24:36 +00005698initcPickle(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005699{
5700 PyObject *m, *d, *di, *v, *k;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005701 Py_ssize_t i;
Tim Peters5b7da392003-02-04 00:21:07 +00005702 char *rev = "1.71"; /* XXX when does this change? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005703 PyObject *format_version;
5704 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005705
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005706 Picklertype.ob_type = &PyType_Type;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005707 Unpicklertype.ob_type = &PyType_Type;
5708 PdataType.ob_type = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005709
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005710 /* Initialize some pieces. We need to do this before module creation,
Tim Peters5b7da392003-02-04 00:21:07 +00005711 * so we're forced to use a temporary dictionary. :(
5712 */
5713 di = PyDict_New();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005714 if (!di) return;
5715 if (init_stuff(di) < 0) return;
Guido van Rossumebba4202000-09-07 14:35:37 +00005716
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005717 /* Create the module and add the functions */
5718 m = Py_InitModule4("cPickle", cPickle_methods,
5719 cPickle_module_documentation,
5720 (PyObject*)NULL,PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00005721 if (m == NULL)
5722 return;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005723
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005724 /* Add some symbolic constants to the module */
5725 d = PyModule_GetDict(m);
Tim Peters5b7da392003-02-04 00:21:07 +00005726 v = PyString_FromString(rev);
5727 PyDict_SetItemString(d, "__version__", v);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005728 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00005729
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005730 /* Copy data from di. Waaa. */
5731 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
5732 if (PyObject_SetItem(d, k, v) < 0) {
5733 Py_DECREF(di);
5734 return;
5735 }
5736 }
5737 Py_DECREF(di);
Guido van Rossumebba4202000-09-07 14:35:37 +00005738
Tim Peters8587b3c2003-02-13 15:44:41 +00005739 i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
5740 if (i < 0)
5741 return;
5742
Tim Peters5b7da392003-02-04 00:21:07 +00005743 /* These are purely informational; no code uses them. */
5744 /* File format version we write. */
5745 format_version = PyString_FromString("2.0");
5746 /* Format versions we can read. */
5747 compatible_formats = Py_BuildValue("[sssss]",
5748 "1.0", /* Original protocol 0 */
5749 "1.1", /* Protocol 0 + INST */
5750 "1.2", /* Original protocol 1 */
5751 "1.3", /* Protocol 1 + BINFLOAT */
Tim Peters0dd23aa2003-02-04 00:30:46 +00005752 "2.0"); /* Original protocol 2 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005753 PyDict_SetItemString(d, "format_version", format_version);
5754 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
5755 Py_XDECREF(format_version);
5756 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005757}