blob: 51fc2268310747f40ef8667ebf9fd958de4dc7af [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
Georg Brandldffbf5f2008-05-20 07:49:57 +0000108/* copy_reg.dispatch_table, {type_object: pickling_function} */
Tim Peters5b7da392003-02-04 00:21:07 +0000109static PyObject *dispatch_table;
110
111/* For EXT[124] opcodes. */
Georg Brandldffbf5f2008-05-20 07:49:57 +0000112/* copy_reg._extension_registry, {(module_name, function_name): code} */
Tim Peters5b7da392003-02-04 00:21:07 +0000113static PyObject *extension_registry;
Georg Brandldffbf5f2008-05-20 07:49:57 +0000114/* copy_reg._inverted_registry, {code: (module_name, function_name)} */
Tim Peters5b7da392003-02-04 00:21:07 +0000115static PyObject *inverted_registry;
Georg Brandldffbf5f2008-05-20 07:49:57 +0000116/* copy_reg._extension_cache, {code: object} */
Tim Peters5b7da392003-02-04 00:21:07 +0000117static PyObject *extension_cache;
118
Georg Brandldffbf5f2008-05-20 07:49:57 +0000119/* For looking up name pairs in copy_reg._extension_registry. */
Tim Peters731098b2003-02-04 20:56:09 +0000120static 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,
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +0000127 *copyreg_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öwis68192102007-07-21 06:55:02 +0000154 PyVarObject_HEAD_INIT(NULL, 0) "cPickle.Pdata", sizeof(Pdata), 0,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000155 (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
Christian Heimese93237d2007-12-19 02:37:44 +0000159#define Pdata_Check(O) (Py_TYPE(O) == &PdataType)
Guido van Rossum053b8df1998-11-25 16:18:00 +0000160
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++) {
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000199 Py_CLEAR(*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;
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000211 PyObject **tmp;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000212
Tim Peters1d63c9f2003-02-02 20:29:39 +0000213 bigger = self->size << 1;
Tim Peterse0a39072003-02-03 15:45:56 +0000214 if (bigger <= 0) /* was 0, or new value overflows */
Tim Peters1d63c9f2003-02-02 20:29:39 +0000215 goto nomemory;
216 if ((int)(size_t)bigger != bigger)
217 goto nomemory;
218 nbytes = (size_t)bigger * sizeof(PyObject *);
219 if (nbytes / sizeof(PyObject *) != (size_t)bigger)
220 goto nomemory;
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000221 tmp = realloc(self->data, nbytes);
222 if (tmp == NULL)
Tim Peters1d63c9f2003-02-02 20:29:39 +0000223 goto nomemory;
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000224 self->data = tmp;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000225 self->size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000226 return 0;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000227
228 nomemory:
Tim Peters1d63c9f2003-02-02 20:29:39 +0000229 PyErr_NoMemory();
230 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000231}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000232
Tim Peterse0a39072003-02-03 15:45:56 +0000233/* D is a Pdata*. Pop the topmost element and store it into V, which
234 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
Tim Peters1d63c9f2003-02-02 20:29:39 +0000235 * is raised and V is set to NULL. D and V may be evaluated several times.
236 */
237#define PDATA_POP(D, V) { \
Tim Peterse0a39072003-02-03 15:45:56 +0000238 if ((D)->length) \
239 (V) = (D)->data[--((D)->length)]; \
240 else { \
241 PyErr_SetString(UnpicklingError, "bad pickle data"); \
242 (V) = NULL; \
243 } \
Guido van Rossum053b8df1998-11-25 16:18:00 +0000244}
245
Tim Peterse0a39072003-02-03 15:45:56 +0000246/* PDATA_PUSH and PDATA_APPEND both push rvalue PyObject* O on to Pdata*
247 * D. If the Pdata stack can't be grown to hold the new value, both
248 * raise MemoryError and execute "return ER". The difference is in ownership
249 * of O after: _PUSH transfers ownership of O from the caller to the stack
250 * (no incref of O is done, and in case of error O is decrefed), while
251 * _APPEND pushes a new reference.
252 */
253
254/* Push O on stack D, giving ownership of O to the stack. */
255#define PDATA_PUSH(D, O, ER) { \
256 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
257 Pdata_grow((Pdata*)(D)) < 0) { \
258 Py_DECREF(O); \
259 return ER; \
260 } \
261 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
262}
263
264/* Push O on stack D, pushing a new reference. */
265#define PDATA_APPEND(D, O, ER) { \
266 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
267 Pdata_grow((Pdata*)(D)) < 0) \
268 return ER; \
269 Py_INCREF(O); \
270 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
271}
272
273
Guido van Rossum053b8df1998-11-25 16:18:00 +0000274static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000275Pdata_popTuple(Pdata *self, int start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000276{
277 PyObject *r;
278 int i, j, l;
Tim Peterscba30e22003-02-01 06:24:36 +0000279
Tim Peters1d63c9f2003-02-02 20:29:39 +0000280 l = self->length-start;
281 r = PyTuple_New(l);
282 if (r == NULL)
283 return NULL;
284 for (i = start, j = 0 ; j < l; i++, j++)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000285 PyTuple_SET_ITEM(r, j, self->data[i]);
Tim Peterscba30e22003-02-01 06:24:36 +0000286
Tim Peters1d63c9f2003-02-02 20:29:39 +0000287 self->length = start;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000288 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000289}
290
291static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000292Pdata_popList(Pdata *self, int start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000293{
294 PyObject *r;
295 int i, j, l;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000296
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000297 l=self->length-start;
298 if (!( r=PyList_New(l))) return NULL;
299 for (i=start, j=0 ; j < l; i++, j++)
300 PyList_SET_ITEM(r, j, self->data[i]);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000301
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000302 self->length=start;
303 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000304}
305
Guido van Rossum053b8df1998-11-25 16:18:00 +0000306/*************************************************************************/
307
308#define ARG_TUP(self, o) { \
309 if (self->arg || (self->arg=PyTuple_New(1))) { \
310 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
311 PyTuple_SET_ITEM(self->arg,0,o); \
312 } \
313 else { \
314 Py_DECREF(o); \
315 } \
316}
317
318#define FREE_ARG_TUP(self) { \
Christian Heimese93237d2007-12-19 02:37:44 +0000319 if (Py_REFCNT(self->arg) > 1) { \
Guido van Rossum053b8df1998-11-25 16:18:00 +0000320 Py_DECREF(self->arg); \
321 self->arg=NULL; \
322 } \
323 }
324
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000325typedef struct Picklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000326 PyObject_HEAD
327 FILE *fp;
328 PyObject *write;
329 PyObject *file;
330 PyObject *memo;
331 PyObject *arg;
332 PyObject *pers_func;
333 PyObject *inst_pers_func;
Tim Peters797ec242003-02-01 06:22:36 +0000334
335 /* pickle protocol number, >= 0 */
336 int proto;
337
338 /* bool, true if proto > 0 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000339 int bin;
Tim Peters797ec242003-02-01 06:22:36 +0000340
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000341 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
Martin v. Löwis5a395302002-08-04 08:20:23 +0000342 int nesting;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000343 int (*write_func)(struct Picklerobject *, const char *, Py_ssize_t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000344 char *write_buf;
345 int buf_size;
346 PyObject *dispatch_table;
347 int fast_container; /* count nested container dumps */
348 PyObject *fast_memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000349} Picklerobject;
350
Barry Warsaw52acb492001-12-21 20:04:22 +0000351#ifndef PY_CPICKLE_FAST_LIMIT
352#define PY_CPICKLE_FAST_LIMIT 50
353#endif
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000354
Jeremy Hylton938ace62002-07-17 16:30:39 +0000355static PyTypeObject Picklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000356
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000357typedef struct Unpicklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000358 PyObject_HEAD
359 FILE *fp;
360 PyObject *file;
361 PyObject *readline;
362 PyObject *read;
363 PyObject *memo;
364 PyObject *arg;
365 Pdata *stack;
366 PyObject *mark;
367 PyObject *pers_func;
368 PyObject *last_string;
369 int *marks;
370 int num_marks;
371 int marks_size;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000372 Py_ssize_t (*read_func)(struct Unpicklerobject *, char **, Py_ssize_t);
373 Py_ssize_t (*readline_func)(struct Unpicklerobject *, char **);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000374 int buf_size;
375 char *buf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000376 PyObject *find_class;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000377} Unpicklerobject;
Tim Peters84e87f32001-03-17 04:50:51 +0000378
Jeremy Hylton938ace62002-07-17 16:30:39 +0000379static PyTypeObject Unpicklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000380
Thomas Wouters4789b3a2000-07-24 11:36:47 +0000381/* Forward decls that need the above structs */
382static int save(Picklerobject *, PyObject *, int);
383static int put2(Picklerobject *, PyObject *);
384
Guido van Rossumd385d591997-04-09 17:47:47 +0000385static
Guido van Rossum60456fd1997-04-09 17:36:32 +0000386PyObject *
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000387cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
388{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000389 va_list va;
390 PyObject *args=0, *retval=0;
391 va_start(va, format);
Tim Peterscba30e22003-02-01 06:24:36 +0000392
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000393 if (format) args = Py_VaBuildValue(format, va);
394 va_end(va);
395 if (format && ! args) return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000396 if (stringformat && !(retval=PyString_FromString(stringformat)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000397 return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000398
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000399 if (retval) {
400 if (args) {
401 PyObject *v;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000402 v=PyString_Format(retval, args);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000403 Py_DECREF(retval);
404 Py_DECREF(args);
405 if (! v) return NULL;
406 retval=v;
407 }
408 }
409 else
410 if (args) retval=args;
411 else {
412 PyErr_SetObject(ErrType,Py_None);
413 return NULL;
414 }
415 PyErr_SetObject(ErrType,retval);
416 Py_DECREF(retval);
417 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000418}
419
Tim Peters84e87f32001-03-17 04:50:51 +0000420static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000421write_file(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000422{
423 size_t nbyteswritten;
Tim Peters84e87f32001-03-17 04:50:51 +0000424
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000425 if (s == NULL) {
426 return 0;
427 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000428
Martin v. Löwis18e16552006-02-15 17:27:45 +0000429 if (n > INT_MAX) {
430 /* String too large */
431 return -1;
432 }
433
Gregory P. Smithc20adf82008-04-07 06:33:21 +0000434 PyFile_IncUseCount((PyFileObject *)self->file);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000435 Py_BEGIN_ALLOW_THREADS
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000436 nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000437 Py_END_ALLOW_THREADS
Gregory P. Smithc20adf82008-04-07 06:33:21 +0000438 PyFile_DecUseCount((PyFileObject *)self->file);
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000439 if (nbyteswritten != (size_t)n) {
440 PyErr_SetFromErrno(PyExc_IOError);
441 return -1;
442 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000443
Martin v. Löwis18e16552006-02-15 17:27:45 +0000444 return (int)n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000445}
446
Tim Peters84e87f32001-03-17 04:50:51 +0000447static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000448write_cStringIO(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000449{
450 if (s == NULL) {
451 return 0;
452 }
Tim Peterscba30e22003-02-01 06:24:36 +0000453
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000454 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
455 return -1;
456 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000457
Martin v. Löwis18e16552006-02-15 17:27:45 +0000458 return (int)n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000459}
460
Tim Peters84e87f32001-03-17 04:50:51 +0000461static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000462write_none(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000463{
464 if (s == NULL) return 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000465 if (n > INT_MAX) return -1;
466 return (int)n;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000467}
468
Tim Peters84e87f32001-03-17 04:50:51 +0000469static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000470write_other(Picklerobject *self, const char *s, Py_ssize_t _n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000471{
472 PyObject *py_str = 0, *junk = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000473 int n;
Tim Peterscba30e22003-02-01 06:24:36 +0000474
Martin v. Löwis18e16552006-02-15 17:27:45 +0000475 if (_n > INT_MAX)
476 return -1;
477 n = (int)_n;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000478 if (s == NULL) {
479 if (!( self->buf_size )) return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000480 py_str = PyString_FromStringAndSize(self->write_buf,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000481 self->buf_size);
Tim Peterscba30e22003-02-01 06:24:36 +0000482 if (!py_str)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000483 return -1;
484 }
485 else {
486 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
487 if (write_other(self, NULL, 0) < 0)
488 return -1;
489 }
Tim Peterscba30e22003-02-01 06:24:36 +0000490
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000491 if (n > WRITE_BUF_SIZE) {
492 if (!( py_str =
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000493 PyString_FromStringAndSize(s, n)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000494 return -1;
495 }
496 else {
497 memcpy(self->write_buf + self->buf_size, s, n);
498 self->buf_size += n;
499 return n;
500 }
501 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000502
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000503 if (self->write) {
504 /* object with write method */
505 ARG_TUP(self, py_str);
506 if (self->arg) {
507 junk = PyObject_Call(self->write, self->arg, NULL);
508 FREE_ARG_TUP(self);
509 }
510 if (junk) Py_DECREF(junk);
511 else return -1;
512 }
513 else
514 PDATA_PUSH(self->file, py_str, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000515
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000516 self->buf_size = 0;
517 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000518}
519
520
Martin v. Löwis18e16552006-02-15 17:27:45 +0000521static Py_ssize_t
522read_file(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000523{
524 size_t nbytesread;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000525
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000526 if (self->buf_size == 0) {
527 int size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000528
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000529 size = ((n < 32) ? 32 : n);
Tim Petersee1a53c2003-02-02 02:57:53 +0000530 if (!( self->buf = (char *)malloc(size))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000531 PyErr_NoMemory();
532 return -1;
533 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000534
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000535 self->buf_size = size;
536 }
537 else if (n > self->buf_size) {
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000538 char *newbuf = (char *)realloc(self->buf, n);
539 if (!newbuf) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000540 PyErr_NoMemory();
541 return -1;
542 }
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000543 self->buf = newbuf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000544 self->buf_size = n;
545 }
Tim Peters84e87f32001-03-17 04:50:51 +0000546
Gregory P. Smithc20adf82008-04-07 06:33:21 +0000547 PyFile_IncUseCount((PyFileObject *)self->file);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000548 Py_BEGIN_ALLOW_THREADS
549 nbytesread = fread(self->buf, sizeof(char), n, self->fp);
550 Py_END_ALLOW_THREADS
Gregory P. Smithc20adf82008-04-07 06:33:21 +0000551 PyFile_DecUseCount((PyFileObject *)self->file);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000552 if (nbytesread != (size_t)n) {
553 if (feof(self->fp)) {
554 PyErr_SetNone(PyExc_EOFError);
555 return -1;
556 }
Tim Peterscba30e22003-02-01 06:24:36 +0000557
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000558 PyErr_SetFromErrno(PyExc_IOError);
559 return -1;
560 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000561
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000562 *s = self->buf;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000563
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000564 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000565}
566
567
Martin v. Löwis18e16552006-02-15 17:27:45 +0000568static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000569readline_file(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000570{
571 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000572
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000573 if (self->buf_size == 0) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000574 if (!( self->buf = (char *)malloc(40))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000575 PyErr_NoMemory();
576 return -1;
577 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000578 self->buf_size = 40;
579 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000580
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000581 i = 0;
582 while (1) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000583 int bigger;
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000584 char *newbuf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000585 for (; i < (self->buf_size - 1); i++) {
Tim Peterscba30e22003-02-01 06:24:36 +0000586 if (feof(self->fp) ||
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000587 (self->buf[i] = getc(self->fp)) == '\n') {
588 self->buf[i + 1] = '\0';
589 *s = self->buf;
590 return i + 1;
591 }
592 }
Tim Petersee1a53c2003-02-02 02:57:53 +0000593 bigger = self->buf_size << 1;
594 if (bigger <= 0) { /* overflow */
595 PyErr_NoMemory();
596 return -1;
597 }
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000598 newbuf = (char *)realloc(self->buf, bigger);
599 if (!newbuf) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000600 PyErr_NoMemory();
601 return -1;
602 }
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000603 self->buf = newbuf;
Tim Petersee1a53c2003-02-02 02:57:53 +0000604 self->buf_size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000605 }
Tim Peters84e87f32001-03-17 04:50:51 +0000606}
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000607
608
Martin v. Löwis18e16552006-02-15 17:27:45 +0000609static Py_ssize_t
610read_cStringIO(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000611{
612 char *ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000613
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000614 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
615 PyErr_SetNone(PyExc_EOFError);
616 return -1;
617 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000618
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000619 *s = ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000620
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000621 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000622}
623
624
Martin v. Löwis18e16552006-02-15 17:27:45 +0000625static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000626readline_cStringIO(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000627{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000628 Py_ssize_t n;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000629 char *ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000630
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000631 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
632 return -1;
633 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000634
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000635 *s = ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000636
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000637 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000638}
639
640
Martin v. Löwis18e16552006-02-15 17:27:45 +0000641static Py_ssize_t
642read_other(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000643{
644 PyObject *bytes, *str=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000645
Martin v. Löwis18e16552006-02-15 17:27:45 +0000646 if (!( bytes = PyInt_FromSsize_t(n))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000647
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000648 ARG_TUP(self, bytes);
649 if (self->arg) {
650 str = PyObject_Call(self->read, self->arg, NULL);
651 FREE_ARG_TUP(self);
652 }
653 if (! str) return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000654
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000655 Py_XDECREF(self->last_string);
656 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000657
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000658 if (! (*s = PyString_AsString(str))) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000659 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000660}
661
662
Martin v. Löwis18e16552006-02-15 17:27:45 +0000663static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000664readline_other(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000665{
666 PyObject *str;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000667 Py_ssize_t str_size;
Tim Peterscba30e22003-02-01 06:24:36 +0000668
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000669 if (!( str = PyObject_CallObject(self->readline, empty_tuple))) {
670 return -1;
671 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000672
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000673 if ((str_size = PyString_Size(str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000674 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000675
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000676 Py_XDECREF(self->last_string);
677 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000678
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000679 if (! (*s = PyString_AsString(str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000680 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000681
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000682 return str_size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000683}
684
Tim Petersee1a53c2003-02-02 02:57:53 +0000685/* Copy the first n bytes from s into newly malloc'ed memory, plus a
686 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
687 * The caller is responsible for free()'ing the return value.
688 */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000689static char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000690pystrndup(const char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000691{
Tim Petersee1a53c2003-02-02 02:57:53 +0000692 char *r = (char *)malloc(n+1);
693 if (r == NULL)
694 return (char*)PyErr_NoMemory();
695 memcpy(r, s, n);
696 r[n] = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000697 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000698}
699
700
701static int
Tim Peterscba30e22003-02-01 06:24:36 +0000702get(Picklerobject *self, PyObject *id)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000703{
704 PyObject *value, *mv;
705 long c_value;
706 char s[30];
707 size_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000708
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000709 if (!( mv = PyDict_GetItem(self->memo, id))) {
710 PyErr_SetObject(PyExc_KeyError, id);
711 return -1;
712 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000713
Tim Peterscba30e22003-02-01 06:24:36 +0000714 if (!( value = PyTuple_GetItem(mv, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000715 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000716
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000717 if (!( PyInt_Check(value))) {
718 PyErr_SetString(PicklingError, "no int where int expected in memo");
719 return -1;
720 }
721 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000722
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000723 if (!self->bin) {
724 s[0] = GET;
725 PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value);
726 len = strlen(s);
727 }
728 else if (Pdata_Check(self->file)) {
729 if (write_other(self, NULL, 0) < 0) return -1;
730 PDATA_APPEND(self->file, mv, -1);
731 return 0;
732 }
733 else {
734 if (c_value < 256) {
735 s[0] = BINGET;
736 s[1] = (int)(c_value & 0xff);
737 len = 2;
738 }
739 else {
740 s[0] = LONG_BINGET;
741 s[1] = (int)(c_value & 0xff);
742 s[2] = (int)((c_value >> 8) & 0xff);
743 s[3] = (int)((c_value >> 16) & 0xff);
744 s[4] = (int)((c_value >> 24) & 0xff);
745 len = 5;
746 }
747 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000748
Tim Peters0bc93f52003-02-02 18:29:33 +0000749 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000750 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000751
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000752 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000753}
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000754
Guido van Rossum60456fd1997-04-09 17:36:32 +0000755
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000756static int
Tim Peterscba30e22003-02-01 06:24:36 +0000757put(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000758{
Christian Heimese93237d2007-12-19 02:37:44 +0000759 if (Py_REFCNT(ob) < 2 || self->fast)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000760 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000761
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000762 return put2(self, ob);
763}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000764
Guido van Rossum053b8df1998-11-25 16:18:00 +0000765
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000766static int
Tim Peterscba30e22003-02-01 06:24:36 +0000767put2(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000768{
769 char c_str[30];
770 int p;
771 size_t len;
772 int res = -1;
773 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000774
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000775 if (self->fast)
776 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000777
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000778 if ((p = PyDict_Size(self->memo)) < 0)
779 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000780
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000781 /* Make sure memo keys are positive! */
Tim Peters70b02d72003-02-02 17:26:40 +0000782 /* XXX Why?
783 * XXX And does "positive" really mean non-negative?
784 * XXX pickle.py starts with PUT index 0, not 1. This makes for
785 * XXX gratuitous differences between the pickling modules.
786 */
Tim Peterscba30e22003-02-01 06:24:36 +0000787 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000788
Tim Peterscba30e22003-02-01 06:24:36 +0000789 if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000790 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000791
Tim Peterscba30e22003-02-01 06:24:36 +0000792 if (!( memo_len = PyInt_FromLong(p)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000793 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000794
Tim Peterscba30e22003-02-01 06:24:36 +0000795 if (!( t = PyTuple_New(2)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000796 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000797
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000798 PyTuple_SET_ITEM(t, 0, memo_len);
799 Py_INCREF(memo_len);
800 PyTuple_SET_ITEM(t, 1, ob);
801 Py_INCREF(ob);
802
803 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
804 goto finally;
805
806 if (!self->bin) {
807 c_str[0] = PUT;
808 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p);
809 len = strlen(c_str);
810 }
811 else if (Pdata_Check(self->file)) {
812 if (write_other(self, NULL, 0) < 0) return -1;
813 PDATA_APPEND(self->file, memo_len, -1);
814 res=0; /* Job well done ;) */
815 goto finally;
816 }
817 else {
818 if (p >= 256) {
819 c_str[0] = LONG_BINPUT;
820 c_str[1] = (int)(p & 0xff);
821 c_str[2] = (int)((p >> 8) & 0xff);
822 c_str[3] = (int)((p >> 16) & 0xff);
823 c_str[4] = (int)((p >> 24) & 0xff);
824 len = 5;
825 }
826 else {
827 c_str[0] = BINPUT;
828 c_str[1] = p;
829 len = 2;
830 }
831 }
832
Tim Peters0bc93f52003-02-02 18:29:33 +0000833 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000834 goto finally;
835
836 res = 0;
837
838 finally:
839 Py_XDECREF(py_ob_id);
840 Py_XDECREF(memo_len);
841 Py_XDECREF(t);
842
843 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000844}
845
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000846static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000847whichmodule(PyObject *global, PyObject *global_name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000848{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000849 Py_ssize_t i, j;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000850 PyObject *module = 0, *modules_dict = 0,
851 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000852
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000853 module = PyObject_GetAttrString(global, "__module__");
Jeremy Hyltonf00368f2003-06-16 20:19:49 +0000854 if (module)
855 return module;
856 if (PyErr_ExceptionMatches(PyExc_AttributeError))
857 PyErr_Clear();
858 else
859 return NULL;
Guido van Rossum45188231997-09-28 05:38:51 +0000860
Tim Peterscba30e22003-02-01 06:24:36 +0000861 if (!( modules_dict = PySys_GetObject("modules")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000862 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000863
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000864 i = 0;
865 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000866
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000867 if (PyObject_Compare(name, __main___str)==0) continue;
Tim Peters84e87f32001-03-17 04:50:51 +0000868
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000869 global_name_attr = PyObject_GetAttr(module, global_name);
870 if (!global_name_attr) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +0000871 if (PyErr_ExceptionMatches(PyExc_AttributeError))
872 PyErr_Clear();
873 else
874 return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000875 continue;
876 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000877
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000878 if (global_name_attr != global) {
879 Py_DECREF(global_name_attr);
880 continue;
881 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000882
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000883 Py_DECREF(global_name_attr);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000884
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000885 break;
886 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000887
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000888 /* The following implements the rule in pickle.py added in 1.5
889 that used __main__ if no module is found. I don't actually
890 like this rule. jlf
891 */
892 if (!j) {
893 j=1;
894 name=__main___str;
895 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000896
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000897 Py_INCREF(name);
898 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000899}
900
901
Guido van Rossum60456fd1997-04-09 17:36:32 +0000902static int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000903fast_save_enter(Picklerobject *self, PyObject *obj)
904{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000905 /* if fast_container < 0, we're doing an error exit. */
906 if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
907 PyObject *key = NULL;
908 if (self->fast_memo == NULL) {
909 self->fast_memo = PyDict_New();
910 if (self->fast_memo == NULL) {
911 self->fast_container = -1;
912 return 0;
913 }
914 }
915 key = PyLong_FromVoidPtr(obj);
916 if (key == NULL)
917 return 0;
918 if (PyDict_GetItem(self->fast_memo, key)) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000919 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000920 PyErr_Format(PyExc_ValueError,
Tim Peters92c8bb32003-02-13 23:00:26 +0000921 "fast mode: can't pickle cyclic objects "
922 "including object type %s at %p",
Christian Heimese93237d2007-12-19 02:37:44 +0000923 Py_TYPE(obj)->tp_name, obj);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000924 self->fast_container = -1;
925 return 0;
926 }
927 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000928 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000929 self->fast_container = -1;
930 return 0;
931 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000932 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000933 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000934 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000935}
936
Tim Peterscba30e22003-02-01 06:24:36 +0000937int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000938fast_save_leave(Picklerobject *self, PyObject *obj)
939{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000940 if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
941 PyObject *key = PyLong_FromVoidPtr(obj);
942 if (key == NULL)
943 return 0;
944 if (PyDict_DelItem(self->fast_memo, key) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000945 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000946 return 0;
947 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000948 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000949 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000950 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000951}
952
953static int
Tim Peterscba30e22003-02-01 06:24:36 +0000954save_none(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000955{
956 static char none = NONE;
Tim Peters0bc93f52003-02-02 18:29:33 +0000957 if (self->write_func(self, &none, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000958 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000959
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000960 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000961}
962
Guido van Rossum77f6a652002-04-03 22:41:51 +0000963static int
Tim Peterscba30e22003-02-01 06:24:36 +0000964save_bool(Picklerobject *self, PyObject *args)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000965{
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000966 static const char *buf[2] = {FALSE, TRUE};
Guido van Rossume2763392002-04-05 19:30:08 +0000967 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
Guido van Rossum77f6a652002-04-03 22:41:51 +0000968 long l = PyInt_AS_LONG((PyIntObject *)args);
969
Tim Peters3c67d792003-02-02 17:59:11 +0000970 if (self->proto >= 2) {
971 char opcode = l ? NEWTRUE : NEWFALSE;
972 if (self->write_func(self, &opcode, 1) < 0)
973 return -1;
974 }
975 else if (self->write_func(self, buf[l], len[l]) < 0)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000976 return -1;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000977 return 0;
978}
Tim Peters84e87f32001-03-17 04:50:51 +0000979
Guido van Rossum60456fd1997-04-09 17:36:32 +0000980static int
Tim Peterscba30e22003-02-01 06:24:36 +0000981save_int(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000982{
983 char c_str[32];
984 long l = PyInt_AS_LONG((PyIntObject *)args);
985 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000986
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000987 if (!self->bin
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000988#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000989 || l > 0x7fffffffL
990 || l < -0x80000000L
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000991#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000992 ) {
993 /* Text-mode pickle, or long too big to fit in the 4-byte
994 * signed BININT format: store as a string.
995 */
996 c_str[0] = INT;
997 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
Tim Peters0bc93f52003-02-02 18:29:33 +0000998 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000999 return -1;
1000 }
1001 else {
1002 /* Binary pickle and l fits in a signed 4-byte int. */
1003 c_str[1] = (int)( l & 0xff);
1004 c_str[2] = (int)((l >> 8) & 0xff);
1005 c_str[3] = (int)((l >> 16) & 0xff);
1006 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001007
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001008 if ((c_str[4] == 0) && (c_str[3] == 0)) {
1009 if (c_str[2] == 0) {
1010 c_str[0] = BININT1;
1011 len = 2;
1012 }
1013 else {
1014 c_str[0] = BININT2;
1015 len = 3;
1016 }
1017 }
1018 else {
1019 c_str[0] = BININT;
1020 len = 5;
1021 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001022
Tim Peters0bc93f52003-02-02 18:29:33 +00001023 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001024 return -1;
1025 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001026
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001027 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001028}
1029
1030
1031static int
Tim Peterscba30e22003-02-01 06:24:36 +00001032save_long(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001033{
Armin Rigo7ccbca92006-10-04 12:17:45 +00001034 Py_ssize_t size;
Tim Petersee1a53c2003-02-02 02:57:53 +00001035 int res = -1;
1036 PyObject *repr = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001037
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001038 static char l = LONG;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001039
Tim Petersee1a53c2003-02-02 02:57:53 +00001040 if (self->proto >= 2) {
1041 /* Linear-time pickling. */
1042 size_t nbits;
1043 size_t nbytes;
1044 unsigned char *pdata;
1045 char c_str[5];
1046 int i;
1047 int sign = _PyLong_Sign(args);
1048
1049 if (sign == 0) {
1050 /* It's 0 -- an empty bytestring. */
1051 c_str[0] = LONG1;
1052 c_str[1] = 0;
1053 i = self->write_func(self, c_str, 2);
1054 if (i < 0) goto finally;
1055 res = 0;
1056 goto finally;
1057 }
1058 nbits = _PyLong_NumBits(args);
1059 if (nbits == (size_t)-1 && PyErr_Occurred())
1060 goto finally;
1061 /* How many bytes do we need? There are nbits >> 3 full
1062 * bytes of data, and nbits & 7 leftover bits. If there
1063 * are any leftover bits, then we clearly need another
1064 * byte. Wnat's not so obvious is that we *probably*
1065 * need another byte even if there aren't any leftovers:
1066 * the most-significant bit of the most-significant byte
1067 * acts like a sign bit, and it's usually got a sense
1068 * opposite of the one we need. The exception is longs
1069 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1070 * its own 256's-complement, so has the right sign bit
1071 * even without the extra byte. That's a pain to check
1072 * for in advance, though, so we always grab an extra
1073 * byte at the start, and cut it back later if possible.
1074 */
1075 nbytes = (nbits >> 3) + 1;
Armin Rigo7ccbca92006-10-04 12:17:45 +00001076 if (nbytes > INT_MAX) {
Tim Petersee1a53c2003-02-02 02:57:53 +00001077 PyErr_SetString(PyExc_OverflowError, "long too large "
1078 "to pickle");
1079 goto finally;
1080 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001081 repr = PyString_FromStringAndSize(NULL, (int)nbytes);
Tim Petersee1a53c2003-02-02 02:57:53 +00001082 if (repr == NULL) goto finally;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001083 pdata = (unsigned char *)PyString_AS_STRING(repr);
Tim Petersee1a53c2003-02-02 02:57:53 +00001084 i = _PyLong_AsByteArray((PyLongObject *)args,
1085 pdata, nbytes,
1086 1 /* little endian */, 1 /* signed */);
1087 if (i < 0) goto finally;
1088 /* If the long is negative, this may be a byte more than
1089 * needed. This is so iff the MSB is all redundant sign
1090 * bits.
1091 */
1092 if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
1093 (pdata[nbytes - 2] & 0x80) != 0)
1094 --nbytes;
1095
1096 if (nbytes < 256) {
1097 c_str[0] = LONG1;
1098 c_str[1] = (char)nbytes;
1099 size = 2;
1100 }
1101 else {
1102 c_str[0] = LONG4;
1103 size = (int)nbytes;
1104 for (i = 1; i < 5; i++) {
1105 c_str[i] = (char)(size & 0xff);
1106 size >>= 8;
1107 }
1108 size = 5;
1109 }
1110 i = self->write_func(self, c_str, size);
1111 if (i < 0) goto finally;
1112 i = self->write_func(self, (char *)pdata, (int)nbytes);
1113 if (i < 0) goto finally;
1114 res = 0;
1115 goto finally;
1116 }
1117
1118 /* proto < 2: write the repr and newline. This is quadratic-time
1119 * (in the number of digits), in both directions.
1120 */
Tim Peterscba30e22003-02-01 06:24:36 +00001121 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001122 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001123
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001124 if ((size = PyString_Size(repr)) < 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, &l, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001128 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001129
Tim Peters0bc93f52003-02-02 18:29:33 +00001130 if (self->write_func(self,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001131 PyString_AS_STRING((PyStringObject *)repr),
Tim Peters0bc93f52003-02-02 18:29:33 +00001132 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001133 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001134
Tim Peters0bc93f52003-02-02 18:29:33 +00001135 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001136 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001137
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001138 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001139
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001140 finally:
1141 Py_XDECREF(repr);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001142 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001143}
1144
1145
1146static int
Tim Peterscba30e22003-02-01 06:24:36 +00001147save_float(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001148{
1149 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001150
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001151 if (self->bin) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001152 char str[9];
Tim Peters9905b942003-03-20 20:53:32 +00001153 str[0] = BINFLOAT;
1154 if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001155 return -1;
Tim Peters0bc93f52003-02-02 18:29:33 +00001156 if (self->write_func(self, str, 9) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001157 return -1;
1158 }
1159 else {
1160 char c_str[250];
1161 c_str[0] = FLOAT;
Georg Brandlde9b6242006-04-30 11:13:56 +00001162 PyOS_ascii_formatd(c_str + 1, sizeof(c_str) - 2, "%.17g", x);
1163 /* Extend the formatted string with a newline character */
1164 strcat(c_str, "\n");
Guido van Rossum60456fd1997-04-09 17:36:32 +00001165
Tim Peters0bc93f52003-02-02 18:29:33 +00001166 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001167 return -1;
1168 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001169
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001170 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001171}
1172
1173
1174static int
Tim Peterscba30e22003-02-01 06:24:36 +00001175save_string(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001176{
1177 int size, len;
1178 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001179
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001180 if ((size = PyString_Size(args)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001181 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001182
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001183 if (!self->bin) {
1184 char *repr_str;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001185
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001186 static char string = STRING;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001187
Tim Peterscba30e22003-02-01 06:24:36 +00001188 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001189 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001190
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001191 if ((len = PyString_Size(repr)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001192 goto err;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001193 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001194
Tim Peters0bc93f52003-02-02 18:29:33 +00001195 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001196 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001197
Tim Peters0bc93f52003-02-02 18:29:33 +00001198 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001199 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001200
Tim Peters0bc93f52003-02-02 18:29:33 +00001201 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001202 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001203
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001204 Py_XDECREF(repr);
1205 }
1206 else {
1207 int i;
1208 char c_str[5];
Guido van Rossum60456fd1997-04-09 17:36:32 +00001209
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001210 if ((size = PyString_Size(args)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001211 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001212
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001213 if (size < 256) {
1214 c_str[0] = SHORT_BINSTRING;
1215 c_str[1] = size;
1216 len = 2;
1217 }
Armin Rigo7ccbca92006-10-04 12:17:45 +00001218 else if (size <= INT_MAX) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001219 c_str[0] = BINSTRING;
1220 for (i = 1; i < 5; i++)
1221 c_str[i] = (int)(size >> ((i - 1) * 8));
1222 len = 5;
1223 }
Armin Rigo7ccbca92006-10-04 12:17:45 +00001224 else
1225 return -1; /* string too large */
Guido van Rossum60456fd1997-04-09 17:36:32 +00001226
Tim Peters0bc93f52003-02-02 18:29:33 +00001227 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001228 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001229
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001230 if (size > 128 && Pdata_Check(self->file)) {
1231 if (write_other(self, NULL, 0) < 0) return -1;
1232 PDATA_APPEND(self->file, args, -1);
1233 }
1234 else {
Tim Peters0bc93f52003-02-02 18:29:33 +00001235 if (self->write_func(self,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001236 PyString_AS_STRING(
1237 (PyStringObject *)args),
Tim Peters0bc93f52003-02-02 18:29:33 +00001238 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001239 return -1;
1240 }
1241 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001242
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001243 if (doput)
1244 if (put(self, args) < 0)
1245 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001246
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001247 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001248
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001249 err:
1250 Py_XDECREF(repr);
1251 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001252}
1253
1254
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001255#ifdef Py_USING_UNICODE
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001256/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1257 backslash and newline characters to \uXXXX escapes. */
1258static PyObject *
1259modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, int size)
1260{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001261 PyObject *repr;
1262 char *p;
1263 char *q;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001264
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001265 static const char *hexdigit = "0123456789ABCDEF";
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001266
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001267 repr = PyString_FromStringAndSize(NULL, 6 * size);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001268 if (repr == NULL)
1269 return NULL;
1270 if (size == 0)
1271 return repr;
1272
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001273 p = q = PyString_AS_STRING(repr);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001274 while (size-- > 0) {
1275 Py_UNICODE ch = *s++;
1276 /* Map 16-bit characters to '\uxxxx' */
1277 if (ch >= 256 || ch == '\\' || ch == '\n') {
1278 *p++ = '\\';
1279 *p++ = 'u';
1280 *p++ = hexdigit[(ch >> 12) & 0xf];
1281 *p++ = hexdigit[(ch >> 8) & 0xf];
1282 *p++ = hexdigit[(ch >> 4) & 0xf];
1283 *p++ = hexdigit[ch & 15];
1284 }
1285 /* Copy everything else as-is */
1286 else
1287 *p++ = (char) ch;
1288 }
1289 *p = '\0';
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001290 _PyString_Resize(&repr, p - q);
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001291 return repr;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001292}
1293
1294
Guido van Rossum60456fd1997-04-09 17:36:32 +00001295static int
Tim Peterscba30e22003-02-01 06:24:36 +00001296save_unicode(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001297{
Armin Rigo7ccbca92006-10-04 12:17:45 +00001298 Py_ssize_t size, len;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001299 PyObject *repr=0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001300
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001301 if (!PyUnicode_Check(args))
1302 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001303
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001304 if (!self->bin) {
1305 char *repr_str;
1306 static char string = UNICODE;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001307
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001308 repr = modified_EncodeRawUnicodeEscape(
1309 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
Tim Peterscba30e22003-02-01 06:24:36 +00001310 if (!repr)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001311 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001312
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001313 if ((len = PyString_Size(repr)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001314 goto err;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001315 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001316
Tim Peters0bc93f52003-02-02 18:29:33 +00001317 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001318 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001319
Tim Peters0bc93f52003-02-02 18:29:33 +00001320 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001321 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001322
Tim Peters0bc93f52003-02-02 18:29:33 +00001323 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001324 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001325
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001326 Py_XDECREF(repr);
1327 }
1328 else {
1329 int i;
1330 char c_str[5];
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001331
Tim Peterscba30e22003-02-01 06:24:36 +00001332 if (!( repr = PyUnicode_AsUTF8String(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001333 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001334
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001335 if ((size = PyString_Size(repr)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001336 goto err;
Armin Rigo7ccbca92006-10-04 12:17:45 +00001337 if (size > INT_MAX)
1338 return -1; /* string too large */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001339
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001340 c_str[0] = BINUNICODE;
1341 for (i = 1; i < 5; i++)
1342 c_str[i] = (int)(size >> ((i - 1) * 8));
1343 len = 5;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001344
Tim Peters0bc93f52003-02-02 18:29:33 +00001345 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001346 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001347
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001348 if (size > 128 && Pdata_Check(self->file)) {
1349 if (write_other(self, NULL, 0) < 0)
1350 goto err;
1351 PDATA_APPEND(self->file, repr, -1);
1352 }
1353 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001354 if (self->write_func(self, PyString_AS_STRING(repr),
Tim Peters0bc93f52003-02-02 18:29:33 +00001355 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001356 goto err;
1357 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001358
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001359 Py_DECREF(repr);
1360 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001361
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001362 if (doput)
1363 if (put(self, args) < 0)
1364 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001365
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001366 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001367
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001368 err:
1369 Py_XDECREF(repr);
1370 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001371}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001372#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001373
Tim Peters1d63c9f2003-02-02 20:29:39 +00001374/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1375static int
Tim Peters67920142003-02-05 03:46:17 +00001376store_tuple_elements(Picklerobject *self, PyObject *t, int len)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001377{
1378 int i;
1379 int res = -1; /* guilty until proved innocent */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001380
Tim Peters1d63c9f2003-02-02 20:29:39 +00001381 assert(PyTuple_Size(t) == len);
1382
1383 for (i = 0; i < len; i++) {
1384 PyObject *element = PyTuple_GET_ITEM(t, i);
1385
1386 if (element == NULL)
1387 goto finally;
1388 if (save(self, element, 0) < 0)
1389 goto finally;
1390 }
1391 res = 0;
1392
1393 finally:
1394 return res;
1395}
1396
1397/* Tuples are ubiquitous in the pickle protocols, so many techniques are
1398 * used across protocols to minimize the space needed to pickle them.
Tim Peters67920142003-02-05 03:46:17 +00001399 * Tuples are also the only builtin immutable type that can be recursive
Tim Peters1d63c9f2003-02-02 20:29:39 +00001400 * (a tuple can be reached from itself), and that requires some subtle
1401 * magic so that it works in all cases. IOW, this is a long routine.
1402 */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001403static int
Tim Peterscba30e22003-02-01 06:24:36 +00001404save_tuple(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001405{
Tim Peters1d63c9f2003-02-02 20:29:39 +00001406 PyObject *py_tuple_id = NULL;
1407 int len, i;
1408 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001409
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001410 static char tuple = TUPLE;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001411 static char pop = POP;
1412 static char pop_mark = POP_MARK;
1413 static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
Guido van Rossum60456fd1997-04-09 17:36:32 +00001414
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001415 if ((len = PyTuple_Size(args)) < 0)
1416 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001417
Tim Peters1d63c9f2003-02-02 20:29:39 +00001418 if (len == 0) {
1419 char c_str[2];
Tim Peters84e87f32001-03-17 04:50:51 +00001420
Tim Peters1d63c9f2003-02-02 20:29:39 +00001421 if (self->proto) {
1422 c_str[0] = EMPTY_TUPLE;
1423 len = 1;
1424 }
1425 else {
1426 c_str[0] = MARK;
1427 c_str[1] = TUPLE;
1428 len = 2;
1429 }
1430 if (self->write_func(self, c_str, len) >= 0)
1431 res = 0;
1432 /* Don't memoize an empty tuple. */
1433 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001434 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001435
Tim Peters1d63c9f2003-02-02 20:29:39 +00001436 /* A non-empty tuple. */
1437
1438 /* id(tuple) isn't in the memo now. If it shows up there after
1439 * saving the tuple elements, the tuple must be recursive, in
1440 * which case we'll pop everything we put on the stack, and fetch
1441 * its value from the memo.
1442 */
1443 py_tuple_id = PyLong_FromVoidPtr(args);
1444 if (py_tuple_id == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001445 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001446
Tim Peters1d63c9f2003-02-02 20:29:39 +00001447 if (len <= 3 && self->proto >= 2) {
1448 /* Use TUPLE{1,2,3} opcodes. */
Tim Peters67920142003-02-05 03:46:17 +00001449 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001450 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001451 if (PyDict_GetItem(self->memo, py_tuple_id)) {
Tim Peters1d63c9f2003-02-02 20:29:39 +00001452 /* pop the len elements */
1453 for (i = 0; i < len; ++i)
1454 if (self->write_func(self, &pop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001455 goto finally;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001456 /* fetch from memo */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001457 if (get(self, py_tuple_id) < 0)
1458 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001459 res = 0;
1460 goto finally;
1461 }
Tim Peters1d63c9f2003-02-02 20:29:39 +00001462 /* Not recursive. */
1463 if (self->write_func(self, len2opcode + len, 1) < 0)
1464 goto finally;
1465 goto memoize;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001466 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001467
Tim Peters1d63c9f2003-02-02 20:29:39 +00001468 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1469 * Generate MARK elt1 elt2 ... TUPLE
1470 */
1471 if (self->write_func(self, &MARKv, 1) < 0)
1472 goto finally;
1473
Tim Peters67920142003-02-05 03:46:17 +00001474 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001475 goto finally;
1476
1477 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1478 /* pop the stack stuff we pushed */
1479 if (self->bin) {
1480 if (self->write_func(self, &pop_mark, 1) < 0)
1481 goto finally;
1482 }
1483 else {
1484 /* Note that we pop one more than len, to remove
1485 * the MARK too.
1486 */
1487 for (i = 0; i <= len; i++)
1488 if (self->write_func(self, &pop, 1) < 0)
1489 goto finally;
1490 }
1491 /* fetch from memo */
1492 if (get(self, py_tuple_id) >= 0)
1493 res = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001494 goto finally;
1495 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001496
Tim Peters1d63c9f2003-02-02 20:29:39 +00001497 /* Not recursive. */
1498 if (self->write_func(self, &tuple, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001499 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001500
Tim Peters1d63c9f2003-02-02 20:29:39 +00001501 memoize:
1502 if (put(self, args) >= 0)
1503 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001504
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001505 finally:
1506 Py_XDECREF(py_tuple_id);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001507 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001508}
1509
Tim Peters1092d642003-02-11 21:06:20 +00001510/* iter is an iterator giving items, and we batch up chunks of
1511 * MARK item item ... item APPENDS
1512 * opcode sequences. Calling code should have arranged to first create an
1513 * empty list, or list-like object, for the APPENDS to operate on.
1514 * Returns 0 on success, <0 on error.
1515 */
1516static int
1517batch_list(Picklerobject *self, PyObject *iter)
1518{
1519 PyObject *obj;
1520 PyObject *slice[BATCHSIZE];
1521 int i, n;
1522
1523 static char append = APPEND;
1524 static char appends = APPENDS;
1525
1526 assert(iter != NULL);
1527
1528 if (self->proto == 0) {
1529 /* APPENDS isn't available; do one at a time. */
1530 for (;;) {
1531 obj = PyIter_Next(iter);
1532 if (obj == NULL) {
1533 if (PyErr_Occurred())
1534 return -1;
1535 break;
1536 }
1537 i = save(self, obj, 0);
1538 Py_DECREF(obj);
1539 if (i < 0)
1540 return -1;
1541 if (self->write_func(self, &append, 1) < 0)
1542 return -1;
Tim Peters1092d642003-02-11 21:06:20 +00001543 }
1544 return 0;
1545 }
1546
1547 /* proto > 0: write in batches of BATCHSIZE. */
1548 do {
1549 /* Get next group of (no more than) BATCHSIZE elements. */
1550 for (n = 0; n < BATCHSIZE; ++n) {
1551 obj = PyIter_Next(iter);
1552 if (obj == NULL) {
1553 if (PyErr_Occurred())
1554 goto BatchFailed;
1555 break;
1556 }
1557 slice[n] = obj;
1558 }
1559
1560 if (n > 1) {
1561 /* Pump out MARK, slice[0:n], APPENDS. */
1562 if (self->write_func(self, &MARKv, 1) < 0)
1563 goto BatchFailed;
1564 for (i = 0; i < n; ++i) {
1565 if (save(self, slice[i], 0) < 0)
1566 goto BatchFailed;
1567 }
1568 if (self->write_func(self, &appends, 1) < 0)
1569 goto BatchFailed;
1570 }
1571 else if (n == 1) {
1572 if (save(self, slice[0], 0) < 0)
1573 goto BatchFailed;
1574 if (self->write_func(self, &append, 1) < 0)
1575 goto BatchFailed;
1576 }
1577
1578 for (i = 0; i < n; ++i) {
1579 Py_DECREF(slice[i]);
1580 }
Tim Peters90975f12003-02-12 05:28:58 +00001581 } while (n == BATCHSIZE);
Tim Peters1092d642003-02-11 21:06:20 +00001582 return 0;
1583
1584BatchFailed:
1585 while (--n >= 0) {
1586 Py_DECREF(slice[n]);
1587 }
1588 return -1;
1589}
1590
Guido van Rossum60456fd1997-04-09 17:36:32 +00001591static int
Tim Peterscba30e22003-02-01 06:24:36 +00001592save_list(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001593{
Tim Peters1092d642003-02-11 21:06:20 +00001594 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001595 char s[3];
Tim Peters1092d642003-02-11 21:06:20 +00001596 int len;
1597 PyObject *iter;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001598
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001599 if (self->fast && !fast_save_enter(self, args))
1600 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001601
Tim Peters1092d642003-02-11 21:06:20 +00001602 /* Create an empty list. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001603 if (self->bin) {
1604 s[0] = EMPTY_LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001605 len = 1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001606 }
1607 else {
1608 s[0] = MARK;
1609 s[1] = LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001610 len = 2;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001611 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001612
Tim Peters1092d642003-02-11 21:06:20 +00001613 if (self->write_func(self, s, len) < 0)
1614 goto finally;
1615
1616 /* Get list length, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001617 if ((len = PyList_Size(args)) < 0)
1618 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001619
Tim Peters1092d642003-02-11 21:06:20 +00001620 /* Memoize. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001621 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001622 if (put(self, args) >= 0)
1623 res = 0;
1624 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001625 }
Tim Peters90975f12003-02-12 05:28:58 +00001626 if (put2(self, args) < 0)
1627 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001628
Tim Peters1092d642003-02-11 21:06:20 +00001629 /* Materialize the list elements. */
1630 iter = PyObject_GetIter(args);
1631 if (iter == NULL)
1632 goto finally;
1633 res = batch_list(self, iter);
1634 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001635
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001636 finally:
1637 if (self->fast && !fast_save_leave(self, args))
1638 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001639
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001640 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001641}
1642
1643
Tim Peters42f08ac2003-02-11 22:43:24 +00001644/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1645 * MARK key value ... key value SETITEMS
1646 * opcode sequences. Calling code should have arranged to first create an
1647 * empty dict, or dict-like object, for the SETITEMS to operate on.
1648 * Returns 0 on success, <0 on error.
1649 *
1650 * This is very much like batch_list(). The difference between saving
1651 * elements directly, and picking apart two-tuples, is so long-winded at
1652 * the C level, though, that attempts to combine these routines were too
1653 * ugly to bear.
1654 */
1655static int
1656batch_dict(Picklerobject *self, PyObject *iter)
1657{
1658 PyObject *p;
1659 PyObject *slice[BATCHSIZE];
1660 int i, n;
1661
1662 static char setitem = SETITEM;
1663 static char setitems = SETITEMS;
1664
1665 assert(iter != NULL);
1666
1667 if (self->proto == 0) {
1668 /* SETITEMS isn't available; do one at a time. */
1669 for (;;) {
1670 p = PyIter_Next(iter);
1671 if (p == NULL) {
1672 if (PyErr_Occurred())
1673 return -1;
1674 break;
1675 }
1676 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1677 PyErr_SetString(PyExc_TypeError, "dict items "
1678 "iterator must return 2-tuples");
1679 return -1;
1680 }
1681 i = save(self, PyTuple_GET_ITEM(p, 0), 0);
1682 if (i >= 0)
1683 i = save(self, PyTuple_GET_ITEM(p, 1), 0);
1684 Py_DECREF(p);
1685 if (i < 0)
1686 return -1;
1687 if (self->write_func(self, &setitem, 1) < 0)
1688 return -1;
Tim Peters42f08ac2003-02-11 22:43:24 +00001689 }
1690 return 0;
1691 }
1692
1693 /* proto > 0: write in batches of BATCHSIZE. */
1694 do {
1695 /* Get next group of (no more than) BATCHSIZE elements. */
1696 for (n = 0; n < BATCHSIZE; ++n) {
1697 p = PyIter_Next(iter);
1698 if (p == NULL) {
1699 if (PyErr_Occurred())
1700 goto BatchFailed;
1701 break;
1702 }
1703 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1704 PyErr_SetString(PyExc_TypeError, "dict items "
1705 "iterator must return 2-tuples");
1706 goto BatchFailed;
1707 }
1708 slice[n] = p;
1709 }
1710
1711 if (n > 1) {
1712 /* Pump out MARK, slice[0:n], SETITEMS. */
1713 if (self->write_func(self, &MARKv, 1) < 0)
1714 goto BatchFailed;
1715 for (i = 0; i < n; ++i) {
1716 p = slice[i];
1717 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1718 goto BatchFailed;
1719 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1720 goto BatchFailed;
1721 }
1722 if (self->write_func(self, &setitems, 1) < 0)
1723 goto BatchFailed;
1724 }
1725 else if (n == 1) {
1726 p = slice[0];
1727 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1728 goto BatchFailed;
1729 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1730 goto BatchFailed;
1731 if (self->write_func(self, &setitem, 1) < 0)
1732 goto BatchFailed;
1733 }
1734
1735 for (i = 0; i < n; ++i) {
1736 Py_DECREF(slice[i]);
1737 }
Tim Peters90975f12003-02-12 05:28:58 +00001738 } while (n == BATCHSIZE);
Tim Peters42f08ac2003-02-11 22:43:24 +00001739 return 0;
1740
1741BatchFailed:
1742 while (--n >= 0) {
1743 Py_DECREF(slice[n]);
1744 }
1745 return -1;
1746}
1747
Guido van Rossum60456fd1997-04-09 17:36:32 +00001748static int
Tim Peterscba30e22003-02-01 06:24:36 +00001749save_dict(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001750{
Tim Peters42f08ac2003-02-11 22:43:24 +00001751 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001752 char s[3];
Tim Peters42f08ac2003-02-11 22:43:24 +00001753 int len;
1754 PyObject *iter;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001755
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001756 if (self->fast && !fast_save_enter(self, args))
1757 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001758
Tim Peters42f08ac2003-02-11 22:43:24 +00001759 /* Create an empty dict. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001760 if (self->bin) {
1761 s[0] = EMPTY_DICT;
1762 len = 1;
1763 }
1764 else {
1765 s[0] = MARK;
1766 s[1] = DICT;
1767 len = 2;
1768 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001769
Tim Peters0bc93f52003-02-02 18:29:33 +00001770 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001771 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001772
Tim Peters42f08ac2003-02-11 22:43:24 +00001773 /* Get dict size, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001774 if ((len = PyDict_Size(args)) < 0)
1775 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001776
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001777 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001778 if (put(self, args) >= 0)
1779 res = 0;
1780 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001781 }
Tim Peters90975f12003-02-12 05:28:58 +00001782 if (put2(self, args) < 0)
1783 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001784
Tim Peters42f08ac2003-02-11 22:43:24 +00001785 /* Materialize the dict items. */
1786 iter = PyObject_CallMethod(args, "iteritems", "()");
1787 if (iter == NULL)
1788 goto finally;
1789 res = batch_dict(self, iter);
1790 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001791
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001792 finally:
1793 if (self->fast && !fast_save_leave(self, args))
1794 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001795
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001796 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001797}
1798
1799
Tim Peters84e87f32001-03-17 04:50:51 +00001800static int
Tim Peterscba30e22003-02-01 06:24:36 +00001801save_inst(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001802{
1803 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1804 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1805 char *module_str, *name_str;
1806 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001807
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001808 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001809
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001810 if (self->fast && !fast_save_enter(self, args))
1811 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001812
Tim Peters0bc93f52003-02-02 18:29:33 +00001813 if (self->write_func(self, &MARKv, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001814 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001815
Tim Peterscba30e22003-02-01 06:24:36 +00001816 if (!( class = PyObject_GetAttr(args, __class___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001817 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001818
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001819 if (self->bin) {
1820 if (save(self, class, 0) < 0)
1821 goto finally;
1822 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001823
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001824 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
1825 PyObject *element = 0;
1826 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001827
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001828 if (!( class_args =
Tim Peterscba30e22003-02-01 06:24:36 +00001829 PyObject_Call(getinitargs_func, empty_tuple, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001830 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001831
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001832 if ((len = PyObject_Size(class_args)) < 0)
1833 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001834
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001835 for (i = 0; i < len; i++) {
Tim Peterscba30e22003-02-01 06:24:36 +00001836 if (!( element = PySequence_GetItem(class_args, i)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001837 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001838
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001839 if (save(self, element, 0) < 0) {
1840 Py_DECREF(element);
1841 goto finally;
1842 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001843
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001844 Py_DECREF(element);
1845 }
1846 }
1847 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00001848 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1849 PyErr_Clear();
1850 else
1851 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001852 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001853
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001854 if (!self->bin) {
1855 if (!( name = ((PyClassObject *)class)->cl_name )) {
1856 PyErr_SetString(PicklingError, "class has no name");
1857 goto finally;
1858 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001859
Tim Peterscba30e22003-02-01 06:24:36 +00001860 if (!( module = whichmodule(class, name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001861 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001862
Tim Peters84e87f32001-03-17 04:50:51 +00001863
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001864 if ((module_size = PyString_Size(module)) < 0 ||
1865 (name_size = PyString_Size(name)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001866 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001867
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001868 module_str = PyString_AS_STRING((PyStringObject *)module);
1869 name_str = PyString_AS_STRING((PyStringObject *)name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001870
Tim Peters0bc93f52003-02-02 18:29:33 +00001871 if (self->write_func(self, &inst, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001872 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001873
Tim Peters0bc93f52003-02-02 18:29:33 +00001874 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001875 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001876
Tim Peters0bc93f52003-02-02 18:29:33 +00001877 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001878 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001879
Tim Peters0bc93f52003-02-02 18:29:33 +00001880 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001881 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001882
Tim Peters0bc93f52003-02-02 18:29:33 +00001883 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001884 goto finally;
1885 }
Tim Peters0bc93f52003-02-02 18:29:33 +00001886 else if (self->write_func(self, &obj, 1) < 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001887 goto finally;
1888 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001889
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001890 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
1891 state = PyObject_Call(getstate_func, empty_tuple, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00001892 if (!state)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001893 goto finally;
1894 }
1895 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00001896 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1897 PyErr_Clear();
1898 else
1899 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001900
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001901 if (!( state = PyObject_GetAttr(args, __dict___str))) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00001902 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1903 PyErr_Clear();
1904 else
1905 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001906 res = 0;
1907 goto finally;
1908 }
1909 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001910
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001911 if (!PyDict_Check(state)) {
1912 if (put2(self, args) < 0)
1913 goto finally;
1914 }
1915 else {
1916 if (put(self, args) < 0)
1917 goto finally;
1918 }
Tim Peters84e87f32001-03-17 04:50:51 +00001919
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001920 if (save(self, state, 0) < 0)
1921 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001922
Tim Peters0bc93f52003-02-02 18:29:33 +00001923 if (self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001924 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001925
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001926 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001927
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001928 finally:
1929 if (self->fast && !fast_save_leave(self, args))
1930 res = -1;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001931
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001932 Py_XDECREF(module);
1933 Py_XDECREF(class);
1934 Py_XDECREF(state);
1935 Py_XDECREF(getinitargs_func);
1936 Py_XDECREF(getstate_func);
1937 Py_XDECREF(class_args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001938
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001939 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001940}
1941
1942
Guido van Rossum60456fd1997-04-09 17:36:32 +00001943static int
Tim Peterscba30e22003-02-01 06:24:36 +00001944save_global(Picklerobject *self, PyObject *args, PyObject *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001945{
Jeremy Hylton9ee91f12002-07-11 22:02:33 +00001946 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001947 char *name_str, *module_str;
1948 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001949
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001950 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001951
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001952 if (name) {
1953 global_name = name;
1954 Py_INCREF(global_name);
1955 }
1956 else {
Tim Peterscba30e22003-02-01 06:24:36 +00001957 if (!( global_name = PyObject_GetAttr(args, __name___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001958 goto finally;
1959 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001960
Tim Peterscba30e22003-02-01 06:24:36 +00001961 if (!( module = whichmodule(args, global_name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001962 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001963
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001964 if ((module_size = PyString_Size(module)) < 0 ||
1965 (name_size = PyString_Size(global_name)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001966 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001967
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001968 module_str = PyString_AS_STRING((PyStringObject *)module);
1969 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001970
Guido van Rossum75bfd052002-12-24 18:10:07 +00001971 /* XXX This can be doing a relative import. Clearly it shouldn't,
1972 but I don't know how to stop it. :-( */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001973 mod = PyImport_ImportModule(module_str);
1974 if (mod == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001975 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00001976 "Can't pickle %s: import of module %s "
1977 "failed",
1978 "OS", args, module);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001979 goto finally;
1980 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001981 klass = PyObject_GetAttrString(mod, name_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001982 if (klass == NULL) {
1983 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00001984 "Can't pickle %s: attribute lookup %s.%s "
1985 "failed",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001986 "OSS", args, module, global_name);
1987 goto finally;
1988 }
1989 if (klass != args) {
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001990 Py_DECREF(klass);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001991 cPickle_ErrFormat(PicklingError,
Tim Peters731098b2003-02-04 20:56:09 +00001992 "Can't pickle %s: it's not the same object "
1993 "as %s.%s",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001994 "OSS", args, module, global_name);
1995 goto finally;
1996 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001997 Py_DECREF(klass);
Guido van Rossuma92d16a2001-08-18 21:22:07 +00001998
Tim Peters731098b2003-02-04 20:56:09 +00001999 if (self->proto >= 2) {
2000 /* See whether this is in the extension registry, and if
2001 * so generate an EXT opcode.
2002 */
2003 PyObject *py_code; /* extension code as Python object */
Tim Peters3e667d52003-02-04 21:47:44 +00002004 long code; /* extension code as C value */
Tim Peters731098b2003-02-04 20:56:09 +00002005 char c_str[5];
2006 int n;
2007
2008 PyTuple_SET_ITEM(two_tuple, 0, module);
2009 PyTuple_SET_ITEM(two_tuple, 1, global_name);
2010 py_code = PyDict_GetItem(extension_registry, two_tuple);
2011 if (py_code == NULL)
2012 goto gen_global; /* not registered */
2013
2014 /* Verify py_code has the right type and value. */
2015 if (!PyInt_Check(py_code)) {
2016 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
Tim Peters6288e232003-02-05 03:53:10 +00002017 "extension code %s isn't an integer",
Tim Peters731098b2003-02-04 20:56:09 +00002018 "OO", args, py_code);
2019 goto finally;
2020 }
2021 code = PyInt_AS_LONG(py_code);
2022 if (code <= 0 || code > 0x7fffffffL) {
2023 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2024 "extension code %ld is out of range",
2025 "Ol", args, code);
2026 goto finally;
2027 }
2028
2029 /* Generate an EXT opcode. */
2030 if (code <= 0xff) {
2031 c_str[0] = EXT1;
2032 c_str[1] = (char)code;
2033 n = 2;
2034 }
2035 else if (code <= 0xffff) {
2036 c_str[0] = EXT2;
2037 c_str[1] = (char)(code & 0xff);
2038 c_str[2] = (char)((code >> 8) & 0xff);
2039 n = 3;
2040 }
2041 else {
2042 c_str[0] = EXT4;
2043 c_str[1] = (char)(code & 0xff);
2044 c_str[2] = (char)((code >> 8) & 0xff);
2045 c_str[3] = (char)((code >> 16) & 0xff);
2046 c_str[4] = (char)((code >> 24) & 0xff);
2047 n = 5;
2048 }
2049
2050 if (self->write_func(self, c_str, n) >= 0)
2051 res = 0;
2052 goto finally; /* and don't memoize */
2053 }
2054
2055 gen_global:
Tim Peters0bc93f52003-02-02 18:29:33 +00002056 if (self->write_func(self, &global, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002057 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002058
Tim Peters0bc93f52003-02-02 18:29:33 +00002059 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002060 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002061
Tim Peters0bc93f52003-02-02 18:29:33 +00002062 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002063 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002064
Tim Peters0bc93f52003-02-02 18:29:33 +00002065 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002066 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002067
Tim Peters0bc93f52003-02-02 18:29:33 +00002068 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002069 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002070
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002071 if (put(self, args) < 0)
2072 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002073
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002074 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002075
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002076 finally:
2077 Py_XDECREF(module);
2078 Py_XDECREF(global_name);
2079 Py_XDECREF(mod);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002080
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002081 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002082}
2083
Guido van Rossum60456fd1997-04-09 17:36:32 +00002084static int
Tim Peterscba30e22003-02-01 06:24:36 +00002085save_pers(Picklerobject *self, PyObject *args, PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002086{
2087 PyObject *pid = 0;
2088 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002089
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002090 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002091
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002092 Py_INCREF(args);
2093 ARG_TUP(self, args);
2094 if (self->arg) {
2095 pid = PyObject_Call(f, self->arg, NULL);
2096 FREE_ARG_TUP(self);
2097 }
2098 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002099
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002100 if (pid != Py_None) {
2101 if (!self->bin) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002102 if (!PyString_Check(pid)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002103 PyErr_SetString(PicklingError,
2104 "persistent id must be string");
2105 goto finally;
2106 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002107
Tim Peters0bc93f52003-02-02 18:29:33 +00002108 if (self->write_func(self, &persid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002109 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002110
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002111 if ((size = PyString_Size(pid)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002112 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002113
Tim Peters0bc93f52003-02-02 18:29:33 +00002114 if (self->write_func(self,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002115 PyString_AS_STRING(
2116 (PyStringObject *)pid),
Tim Peters0bc93f52003-02-02 18:29:33 +00002117 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002118 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002119
Tim Peters0bc93f52003-02-02 18:29:33 +00002120 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002121 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002122
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002123 res = 1;
2124 goto finally;
2125 }
2126 else if (save(self, pid, 1) >= 0) {
Tim Peters0bc93f52003-02-02 18:29:33 +00002127 if (self->write_func(self, &binpersid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002128 res = -1;
2129 else
2130 res = 1;
2131 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002132
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002133 goto finally;
2134 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002135
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002136 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002137
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002138 finally:
2139 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002140
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002141 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002142}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002143
Tim Peters71fcda52003-02-14 23:05:28 +00002144/* We're saving ob, and args is the 2-thru-5 tuple returned by the
2145 * appropriate __reduce__ method for ob.
2146 */
Tim Peters84e87f32001-03-17 04:50:51 +00002147static int
Tim Peters71fcda52003-02-14 23:05:28 +00002148save_reduce(Picklerobject *self, PyObject *args, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002149{
Tim Peters71fcda52003-02-14 23:05:28 +00002150 PyObject *callable;
2151 PyObject *argtup;
2152 PyObject *state = NULL;
2153 PyObject *listitems = NULL;
2154 PyObject *dictitems = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002155
Tim Peters71fcda52003-02-14 23:05:28 +00002156 int use_newobj = self->proto >= 2;
2157
2158 static char reduce = REDUCE;
2159 static char build = BUILD;
2160 static char newobj = NEWOBJ;
2161
2162 if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2163 &callable,
2164 &argtup,
2165 &state,
2166 &listitems,
2167 &dictitems))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002168 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002169
Raymond Hettingera6b45cc2004-12-07 07:05:57 +00002170 if (!PyTuple_Check(argtup)) {
2171 PyErr_SetString(PicklingError,
2172 "args from reduce() should be a tuple");
2173 return -1;
2174 }
2175
Tim Peters71fcda52003-02-14 23:05:28 +00002176 if (state == Py_None)
2177 state = NULL;
2178 if (listitems == Py_None)
2179 listitems = NULL;
2180 if (dictitems == Py_None)
2181 dictitems = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002182
Tim Peters71fcda52003-02-14 23:05:28 +00002183 /* Protocol 2 special case: if callable's name is __newobj__, use
2184 * NEWOBJ. This consumes a lot of code.
2185 */
2186 if (use_newobj) {
2187 PyObject *temp = PyObject_GetAttr(callable, __name___str);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002188
Tim Peters71fcda52003-02-14 23:05:28 +00002189 if (temp == NULL) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002190 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2191 PyErr_Clear();
2192 else
2193 return -1;
Tim Peters71fcda52003-02-14 23:05:28 +00002194 use_newobj = 0;
2195 }
2196 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002197 use_newobj = PyString_Check(temp) &&
2198 strcmp(PyString_AS_STRING(temp),
Tim Peters71fcda52003-02-14 23:05:28 +00002199 "__newobj__") == 0;
2200 Py_DECREF(temp);
2201 }
2202 }
2203 if (use_newobj) {
2204 PyObject *cls;
2205 PyObject *newargtup;
2206 int n, i;
2207
2208 /* Sanity checks. */
2209 n = PyTuple_Size(argtup);
2210 if (n < 1) {
2211 PyErr_SetString(PicklingError, "__newobj__ arglist "
2212 "is empty");
2213 return -1;
2214 }
2215
2216 cls = PyTuple_GET_ITEM(argtup, 0);
2217 if (! PyObject_HasAttrString(cls, "__new__")) {
2218 PyErr_SetString(PicklingError, "args[0] from "
2219 "__newobj__ args has no __new__");
2220 return -1;
2221 }
2222
2223 /* XXX How could ob be NULL? */
2224 if (ob != NULL) {
2225 PyObject *ob_dot_class;
2226
2227 ob_dot_class = PyObject_GetAttr(ob, __class___str);
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002228 if (ob_dot_class == NULL) {
2229 if (PyErr_ExceptionMatches(
2230 PyExc_AttributeError))
2231 PyErr_Clear();
2232 else
2233 return -1;
2234 }
Tim Peters71fcda52003-02-14 23:05:28 +00002235 i = ob_dot_class != cls; /* true iff a problem */
2236 Py_XDECREF(ob_dot_class);
2237 if (i) {
2238 PyErr_SetString(PicklingError, "args[0] from "
2239 "__newobj__ args has the wrong class");
2240 return -1;
2241 }
2242 }
2243
2244 /* Save the class and its __new__ arguments. */
2245 if (save(self, cls, 0) < 0)
2246 return -1;
2247
2248 newargtup = PyTuple_New(n-1); /* argtup[1:] */
2249 if (newargtup == NULL)
2250 return -1;
2251 for (i = 1; i < n; ++i) {
2252 PyObject *temp = PyTuple_GET_ITEM(argtup, i);
2253 Py_INCREF(temp);
2254 PyTuple_SET_ITEM(newargtup, i-1, temp);
2255 }
Neal Norwitz5a29dd32007-10-05 05:01:38 +00002256 i = save(self, newargtup, 0);
Tim Peters71fcda52003-02-14 23:05:28 +00002257 Py_DECREF(newargtup);
2258 if (i < 0)
2259 return -1;
2260
2261 /* Add NEWOBJ opcode. */
2262 if (self->write_func(self, &newobj, 1) < 0)
2263 return -1;
2264 }
2265 else {
2266 /* Not using NEWOBJ. */
2267 if (save(self, callable, 0) < 0 ||
2268 save(self, argtup, 0) < 0 ||
2269 self->write_func(self, &reduce, 1) < 0)
2270 return -1;
2271 }
2272
2273 /* Memoize. */
2274 /* XXX How can ob be NULL? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002275 if (ob != NULL) {
2276 if (state && !PyDict_Check(state)) {
2277 if (put2(self, ob) < 0)
2278 return -1;
2279 }
Tim Peters71fcda52003-02-14 23:05:28 +00002280 else if (put(self, ob) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002281 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002282 }
Tim Peters84e87f32001-03-17 04:50:51 +00002283
Guido van Rossum60456fd1997-04-09 17:36:32 +00002284
Tim Peters71fcda52003-02-14 23:05:28 +00002285 if (listitems && batch_list(self, listitems) < 0)
2286 return -1;
2287
2288 if (dictitems && batch_dict(self, dictitems) < 0)
2289 return -1;
2290
2291 if (state) {
2292 if (save(self, state, 0) < 0 ||
2293 self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002294 return -1;
2295 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002296
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002297 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002298}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002299
Guido van Rossum60456fd1997-04-09 17:36:32 +00002300static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00002301save(Picklerobject *self, PyObject *args, int pers_save)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002302{
2303 PyTypeObject *type;
Tim Peters71fcda52003-02-14 23:05:28 +00002304 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
2305 PyObject *arg_tup;
2306 int res = -1;
2307 int tmp, size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002308
Martin v. Löwis5a395302002-08-04 08:20:23 +00002309 if (self->nesting++ > Py_GetRecursionLimit()){
2310 PyErr_SetString(PyExc_RuntimeError,
2311 "maximum recursion depth exceeded");
2312 goto finally;
2313 }
2314
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002315 if (!pers_save && self->pers_func) {
2316 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2317 res = tmp;
2318 goto finally;
2319 }
2320 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002321
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002322 if (args == Py_None) {
2323 res = save_none(self, args);
2324 goto finally;
2325 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002326
Christian Heimese93237d2007-12-19 02:37:44 +00002327 type = Py_TYPE(args);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002328
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002329 switch (type->tp_name[0]) {
Guido van Rossum77f6a652002-04-03 22:41:51 +00002330 case 'b':
2331 if (args == Py_False || args == Py_True) {
2332 res = save_bool(self, args);
2333 goto finally;
2334 }
2335 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002336 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002337 if (type == &PyInt_Type) {
2338 res = save_int(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 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002344 if (type == &PyLong_Type) {
2345 res = save_long(self, args);
2346 goto finally;
2347 }
2348 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002349
Guido van Rossum60456fd1997-04-09 17:36:32 +00002350 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002351 if (type == &PyFloat_Type) {
2352 res = save_float(self, args);
2353 goto finally;
2354 }
2355 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002356
Guido van Rossum60456fd1997-04-09 17:36:32 +00002357 case 't':
Tim Peters1d63c9f2003-02-02 20:29:39 +00002358 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2359 res = save_tuple(self, args);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002360 goto finally;
2361 }
2362 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002363
Guido van Rossum60456fd1997-04-09 17:36:32 +00002364 case 's':
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002365 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002366 res = save_string(self, args, 0);
2367 goto finally;
2368 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002369
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002370#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002371 case 'u':
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002372 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002373 res = save_unicode(self, args, 0);
2374 goto finally;
2375 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002376#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002377 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002378
Christian Heimese93237d2007-12-19 02:37:44 +00002379 if (Py_REFCNT(args) > 1) {
Tim Peterscba30e22003-02-01 06:24:36 +00002380 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002381 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002382
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002383 if (PyDict_GetItem(self->memo, py_ob_id)) {
2384 if (get(self, py_ob_id) < 0)
2385 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002386
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002387 res = 0;
2388 goto finally;
2389 }
2390 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002391
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002392 switch (type->tp_name[0]) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002393 case 's':
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002394 if (type == &PyString_Type) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002395 res = save_string(self, args, 1);
2396 goto finally;
2397 }
2398 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002399
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002400#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002401 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002402 if (type == &PyUnicode_Type) {
2403 res = save_unicode(self, args, 1);
2404 goto finally;
2405 }
2406 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002407#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002408
Guido van Rossum60456fd1997-04-09 17:36:32 +00002409 case 't':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002410 if (type == &PyTuple_Type) {
2411 res = save_tuple(self, args);
2412 goto finally;
2413 }
2414 if (type == &PyType_Type) {
2415 res = save_global(self, args, NULL);
2416 goto finally;
2417 }
2418 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002419
Guido van Rossum60456fd1997-04-09 17:36:32 +00002420 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002421 if (type == &PyList_Type) {
2422 res = save_list(self, args);
2423 goto finally;
2424 }
2425 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002426
2427 case 'd':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002428 if (type == &PyDict_Type) {
2429 res = save_dict(self, args);
2430 goto finally;
2431 }
2432 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002433
2434 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002435 if (type == &PyInstance_Type) {
2436 res = save_inst(self, args);
2437 goto finally;
2438 }
2439 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002440
2441 case 'c':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002442 if (type == &PyClass_Type) {
2443 res = save_global(self, args, NULL);
2444 goto finally;
2445 }
2446 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002447
2448 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002449 if (type == &PyFunction_Type) {
2450 res = save_global(self, args, NULL);
Christian Tismer2460c622004-02-26 16:21:45 +00002451 if (res && PyErr_ExceptionMatches(PickleError)) {
2452 /* fall back to reduce */
2453 PyErr_Clear();
2454 break;
2455 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002456 goto finally;
2457 }
2458 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002459
2460 case 'b':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002461 if (type == &PyCFunction_Type) {
2462 res = save_global(self, args, NULL);
2463 goto finally;
2464 }
2465 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002466
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002467 if (!pers_save && self->inst_pers_func) {
2468 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2469 res = tmp;
2470 goto finally;
2471 }
2472 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002473
Jeremy Hylton39c61162002-07-16 19:47:43 +00002474 if (PyType_IsSubtype(type, &PyType_Type)) {
2475 res = save_global(self, args, NULL);
2476 goto finally;
2477 }
2478
Guido van Rossumb289b872003-02-19 01:45:13 +00002479 /* Get a reduction callable, and call it. This may come from
Georg Brandldffbf5f2008-05-20 07:49:57 +00002480 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
Guido van Rossumb289b872003-02-19 01:45:13 +00002481 * or the object's __reduce__ method.
Tim Peters71fcda52003-02-14 23:05:28 +00002482 */
Tim Peters5aa3da62003-02-13 21:03:57 +00002483 __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2484 if (__reduce__ != NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002485 Py_INCREF(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00002486 Py_INCREF(args);
2487 ARG_TUP(self, args);
2488 if (self->arg) {
2489 t = PyObject_Call(__reduce__, self->arg, NULL);
2490 FREE_ARG_TUP(self);
2491 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002492 }
2493 else {
Guido van Rossumb289b872003-02-19 01:45:13 +00002494 /* Check for a __reduce_ex__ method. */
2495 __reduce__ = PyObject_GetAttr(args, __reduce_ex___str);
2496 if (__reduce__ != NULL) {
2497 t = PyInt_FromLong(self->proto);
2498 if (t != NULL) {
2499 ARG_TUP(self, t);
2500 t = NULL;
2501 if (self->arg) {
2502 t = PyObject_Call(__reduce__,
2503 self->arg, NULL);
2504 FREE_ARG_TUP(self);
2505 }
2506 }
2507 }
2508 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002509 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2510 PyErr_Clear();
2511 else
2512 goto finally;
Guido van Rossumb289b872003-02-19 01:45:13 +00002513 /* Check for a __reduce__ method. */
2514 __reduce__ = PyObject_GetAttr(args, __reduce___str);
2515 if (__reduce__ != NULL) {
2516 t = PyObject_Call(__reduce__,
2517 empty_tuple, NULL);
2518 }
2519 else {
2520 PyErr_SetObject(UnpickleableError, args);
2521 goto finally;
2522 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002523 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002524 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002525
Tim Peters71fcda52003-02-14 23:05:28 +00002526 if (t == NULL)
2527 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002528
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002529 if (PyString_Check(t)) {
Tim Peters71fcda52003-02-14 23:05:28 +00002530 res = save_global(self, args, t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002531 goto finally;
2532 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002533
Tim Peters71fcda52003-02-14 23:05:28 +00002534 if (! PyTuple_Check(t)) {
2535 cPickle_ErrFormat(PicklingError, "Value returned by "
2536 "%s must be string or tuple",
2537 "O", __reduce__);
2538 goto finally;
2539 }
2540
2541 size = PyTuple_Size(t);
2542 if (size < 2 || size > 5) {
2543 cPickle_ErrFormat(PicklingError, "tuple returned by "
2544 "%s must contain 2 through 5 elements",
2545 "O", __reduce__);
2546 goto finally;
2547 }
2548
2549 arg_tup = PyTuple_GET_ITEM(t, 1);
2550 if (!(PyTuple_Check(arg_tup) || arg_tup == Py_None)) {
2551 cPickle_ErrFormat(PicklingError, "Second element of "
2552 "tuple returned by %s must be a tuple",
2553 "O", __reduce__);
2554 goto finally;
2555 }
2556
2557 res = save_reduce(self, t, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002558
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002559 finally:
Martin v. Löwis5a395302002-08-04 08:20:23 +00002560 self->nesting--;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002561 Py_XDECREF(py_ob_id);
2562 Py_XDECREF(__reduce__);
2563 Py_XDECREF(t);
Tim Peters84e87f32001-03-17 04:50:51 +00002564
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002565 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002566}
2567
2568
2569static int
Tim Peterscba30e22003-02-01 06:24:36 +00002570dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002571{
2572 static char stop = STOP;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002573
Tim Peters4190fb82003-02-02 16:09:05 +00002574 if (self->proto >= 2) {
2575 char bytes[2];
2576
2577 bytes[0] = PROTO;
Tim Peters87482ea2003-02-02 16:16:30 +00002578 assert(self->proto >= 0 && self->proto < 256);
2579 bytes[1] = (char)self->proto;
Tim Peters4190fb82003-02-02 16:09:05 +00002580 if (self->write_func(self, bytes, 2) < 0)
2581 return -1;
2582 }
2583
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002584 if (save(self, args, 0) < 0)
2585 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002586
Tim Peters4190fb82003-02-02 16:09:05 +00002587 if (self->write_func(self, &stop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002588 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002589
Tim Peters4190fb82003-02-02 16:09:05 +00002590 if (self->write_func(self, NULL, 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002591 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002592
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002593 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002594}
2595
2596static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002597Pickle_clear_memo(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002598{
Tim Peterscba30e22003-02-01 06:24:36 +00002599 if (self->memo)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002600 PyDict_Clear(self->memo);
2601 Py_INCREF(Py_None);
2602 return Py_None;
2603}
2604
2605static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002606Pickle_getvalue(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002607{
2608 int l, i, rsize, ssize, clear=1, lm;
2609 long ik;
2610 PyObject *k, *r;
2611 char *s, *p, *have_get;
2612 Pdata *data;
2613
2614 /* Can be called by Python code or C code */
Tim Peterscba30e22003-02-01 06:24:36 +00002615 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002616 return NULL;
2617
2618 /* Check to make sure we are based on a list */
2619 if (! Pdata_Check(self->file)) {
2620 PyErr_SetString(PicklingError,
2621 "Attempt to getvalue() a non-list-based pickler");
2622 return NULL;
2623 }
2624
2625 /* flush write buffer */
2626 if (write_other(self, NULL, 0) < 0) return NULL;
2627
2628 data=(Pdata*)self->file;
2629 l=data->length;
2630
2631 /* set up an array to hold get/put status */
Tim Petersac5687a2003-02-02 18:08:34 +00002632 lm = PyDict_Size(self->memo);
2633 if (lm < 0) return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002634 lm++;
Tim Petersac5687a2003-02-02 18:08:34 +00002635 have_get = malloc(lm);
2636 if (have_get == NULL) return PyErr_NoMemory();
2637 memset(have_get, 0, lm);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002638
2639 /* Scan for gets. */
Tim Petersac5687a2003-02-02 18:08:34 +00002640 for (rsize = 0, i = l; --i >= 0; ) {
2641 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002642
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002643 if (PyString_Check(k))
2644 rsize += PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002645
2646 else if (PyInt_Check(k)) { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002647 ik = PyInt_AS_LONG((PyIntObject*)k);
2648 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002649 PyErr_SetString(PicklingError,
2650 "Invalid get data");
Neal Norwitz98a96002006-07-23 07:57:11 +00002651 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002652 }
Tim Petersac5687a2003-02-02 18:08:34 +00002653 if (have_get[ik]) /* with matching get */
2654 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002655 }
2656
2657 else if (! (PyTuple_Check(k) &&
2658 PyTuple_GET_SIZE(k) == 2 &&
Tim Petersac5687a2003-02-02 18:08:34 +00002659 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002660 ) {
2661 PyErr_SetString(PicklingError,
2662 "Unexpected data in internal list");
Neal Norwitz98a96002006-07-23 07:57:11 +00002663 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002664 }
2665
2666 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002667 ik = PyInt_AS_LONG((PyIntObject *)k);
2668 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002669 PyErr_SetString(PicklingError,
2670 "Invalid get data");
2671 return NULL;
2672 }
Tim Petersac5687a2003-02-02 18:08:34 +00002673 have_get[ik] = 1;
2674 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002675 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002676 }
2677
2678 /* Now generate the result */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002679 r = PyString_FromStringAndSize(NULL, rsize);
Tim Petersac5687a2003-02-02 18:08:34 +00002680 if (r == NULL) goto err;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002681 s = PyString_AS_STRING((PyStringObject *)r);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002682
Tim Petersac5687a2003-02-02 18:08:34 +00002683 for (i = 0; i < l; i++) {
2684 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002685
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002686 if (PyString_Check(k)) {
2687 ssize = PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002688 if (ssize) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002689 p=PyString_AS_STRING((PyStringObject *)k);
Tim Petersac5687a2003-02-02 18:08:34 +00002690 while (--ssize >= 0)
2691 *s++ = *p++;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002692 }
2693 }
2694
2695 else if (PyTuple_Check(k)) { /* get */
Tim Petersac5687a2003-02-02 18:08:34 +00002696 ik = PyInt_AS_LONG((PyIntObject *)
2697 PyTuple_GET_ITEM(k, 0));
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002698 if (ik < 256) {
2699 *s++ = BINGET;
2700 *s++ = (int)(ik & 0xff);
2701 }
2702 else {
2703 *s++ = LONG_BINGET;
2704 *s++ = (int)(ik & 0xff);
2705 *s++ = (int)((ik >> 8) & 0xff);
2706 *s++ = (int)((ik >> 16) & 0xff);
2707 *s++ = (int)((ik >> 24) & 0xff);
2708 }
2709 }
2710
2711 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002712 ik = PyInt_AS_LONG((PyIntObject*)k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002713
2714 if (have_get[ik]) { /* with matching get */
2715 if (ik < 256) {
2716 *s++ = BINPUT;
2717 *s++ = (int)(ik & 0xff);
2718 }
2719 else {
2720 *s++ = LONG_BINPUT;
2721 *s++ = (int)(ik & 0xff);
2722 *s++ = (int)((ik >> 8) & 0xff);
2723 *s++ = (int)((ik >> 16) & 0xff);
2724 *s++ = (int)((ik >> 24) & 0xff);
2725 }
2726 }
2727 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002728 }
2729
2730 if (clear) {
2731 PyDict_Clear(self->memo);
Tim Petersac5687a2003-02-02 18:08:34 +00002732 Pdata_clear(data, 0);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002733 }
2734
2735 free(have_get);
2736 return r;
2737 err:
2738 free(have_get);
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002739 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002740}
2741
2742static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002743Pickler_dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002744{
2745 PyObject *ob;
2746 int get=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002747
Tim Peterscba30e22003-02-01 06:24:36 +00002748 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002749 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002750
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002751 if (dump(self, ob) < 0)
2752 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002753
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002754 if (get) return Pickle_getvalue(self, NULL);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002755
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002756 /* XXX Why does dump() return self? */
2757 Py_INCREF(self);
2758 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002759}
2760
2761
Tim Peterscba30e22003-02-01 06:24:36 +00002762static struct PyMethodDef Pickler_methods[] =
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002763{
Neal Norwitzb0493252002-03-31 14:44:22 +00002764 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002765 PyDoc_STR("dump(object) -- "
2766 "Write an object in pickle format to the object's pickle stream")},
Fred Drake0ebacc82002-05-01 20:36:39 +00002767 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002768 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
Neal Norwitzb0493252002-03-31 14:44:22 +00002769 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002770 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
Guido van Rossum60456fd1997-04-09 17:36:32 +00002771 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002772};
2773
2774
2775static Picklerobject *
Tim Peters5bd2a792003-02-01 16:45:06 +00002776newPicklerobject(PyObject *file, int proto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002777{
2778 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002779
Tim Peters5bd2a792003-02-01 16:45:06 +00002780 if (proto < 0)
Tim Peters8587b3c2003-02-13 15:44:41 +00002781 proto = HIGHEST_PROTOCOL;
2782 if (proto > HIGHEST_PROTOCOL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002783 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
2784 "the highest available protocol is %d",
Tim Peters8587b3c2003-02-13 15:44:41 +00002785 proto, HIGHEST_PROTOCOL);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002786 return NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002787 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002788
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002789 self = PyObject_GC_New(Picklerobject, &Picklertype);
Tim Peters5bd2a792003-02-01 16:45:06 +00002790 if (self == NULL)
2791 return NULL;
2792 self->proto = proto;
2793 self->bin = proto > 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002794 self->fp = NULL;
2795 self->write = NULL;
2796 self->memo = NULL;
2797 self->arg = NULL;
2798 self->pers_func = NULL;
2799 self->inst_pers_func = NULL;
2800 self->write_buf = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002801 self->fast = 0;
Martin v. Löwis5a395302002-08-04 08:20:23 +00002802 self->nesting = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002803 self->fast_container = 0;
2804 self->fast_memo = NULL;
2805 self->buf_size = 0;
2806 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002807
Tim Peters5bd2a792003-02-01 16:45:06 +00002808 self->file = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002809 if (file)
2810 Py_INCREF(file);
Tim Peters5bd2a792003-02-01 16:45:06 +00002811 else {
2812 file = Pdata_New();
2813 if (file == NULL)
2814 goto err;
2815 }
2816 self->file = file;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002817
Tim Peterscba30e22003-02-01 06:24:36 +00002818 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002819 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002820
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002821 if (PyFile_Check(file)) {
2822 self->fp = PyFile_AsFile(file);
2823 if (self->fp == NULL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002824 PyErr_SetString(PyExc_ValueError,
2825 "I/O operation on closed file");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002826 goto err;
2827 }
2828 self->write_func = write_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002829 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002830 else if (PycStringIO_OutputCheck(file)) {
2831 self->write_func = write_cStringIO;
2832 }
2833 else if (file == Py_None) {
2834 self->write_func = write_none;
2835 }
2836 else {
2837 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002838
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002839 if (! Pdata_Check(file)) {
2840 self->write = PyObject_GetAttr(file, write_str);
2841 if (!self->write) {
2842 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00002843 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002844 "argument must have 'write' "
2845 "attribute");
2846 goto err;
2847 }
2848 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002849
Tim Peters5bd2a792003-02-01 16:45:06 +00002850 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
2851 if (self->write_buf == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002852 PyErr_NoMemory();
2853 goto err;
2854 }
2855 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002856
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002857 if (PyEval_GetRestricted()) {
2858 /* Restricted execution, get private tables */
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00002859 PyObject *m = PyImport_Import(copyreg_str);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002860
Tim Peters5b7da392003-02-04 00:21:07 +00002861 if (m == NULL)
2862 goto err;
2863 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002864 Py_DECREF(m);
Tim Peters5b7da392003-02-04 00:21:07 +00002865 if (self->dispatch_table == NULL)
2866 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002867 }
2868 else {
Tim Peters5b7da392003-02-04 00:21:07 +00002869 self->dispatch_table = dispatch_table;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002870 Py_INCREF(dispatch_table);
2871 }
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002872 PyObject_GC_Track(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002873
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002874 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002875
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002876 err:
Tim Peters5bd2a792003-02-01 16:45:06 +00002877 Py_DECREF(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002878 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002879}
2880
2881
2882static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00002883get_Pickler(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002884{
Martin v. Löwis15e62742006-02-27 16:46:16 +00002885 static char *kwlist[] = {"file", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002886 PyObject *file = NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002887 int proto = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002888
Tim Peters92c8bb32003-02-13 23:00:26 +00002889 /* XXX
Martin v. Löwis544f1192004-07-27 05:22:33 +00002890 * The documented signature is Pickler(file, protocol=0), but this
Tim Peters92c8bb32003-02-13 23:00:26 +00002891 * accepts Pickler() and Pickler(integer) too. The meaning then
2892 * is clear as mud, undocumented, and not supported by pickle.py.
2893 * I'm told Zope uses this, but I haven't traced into this code
2894 * far enough to figure out what it means.
Tim Peters5bd2a792003-02-01 16:45:06 +00002895 */
2896 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002897 PyErr_Clear();
Tim Peters5bd2a792003-02-01 16:45:06 +00002898 proto = 0;
Martin v. Löwis544f1192004-07-27 05:22:33 +00002899 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler",
2900 kwlist, &file, &proto))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002901 return NULL;
2902 }
Tim Peters5bd2a792003-02-01 16:45:06 +00002903 return (PyObject *)newPicklerobject(file, proto);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002904}
2905
2906
2907static void
Tim Peterscba30e22003-02-01 06:24:36 +00002908Pickler_dealloc(Picklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002909{
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002910 PyObject_GC_UnTrack(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002911 Py_XDECREF(self->write);
2912 Py_XDECREF(self->memo);
2913 Py_XDECREF(self->fast_memo);
2914 Py_XDECREF(self->arg);
2915 Py_XDECREF(self->file);
2916 Py_XDECREF(self->pers_func);
2917 Py_XDECREF(self->inst_pers_func);
2918 Py_XDECREF(self->dispatch_table);
Tim Peters5bd2a792003-02-01 16:45:06 +00002919 PyMem_Free(self->write_buf);
Christian Heimese93237d2007-12-19 02:37:44 +00002920 Py_TYPE(self)->tp_free((PyObject *)self);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002921}
2922
2923static int
2924Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)
2925{
Thomas Woutersc6e55062006-04-15 21:47:09 +00002926 Py_VISIT(self->write);
2927 Py_VISIT(self->memo);
2928 Py_VISIT(self->fast_memo);
2929 Py_VISIT(self->arg);
2930 Py_VISIT(self->file);
2931 Py_VISIT(self->pers_func);
2932 Py_VISIT(self->inst_pers_func);
2933 Py_VISIT(self->dispatch_table);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002934 return 0;
2935}
2936
2937static int
2938Pickler_clear(Picklerobject *self)
2939{
Thomas Woutersedf17d82006-04-15 17:28:34 +00002940 Py_CLEAR(self->write);
2941 Py_CLEAR(self->memo);
2942 Py_CLEAR(self->fast_memo);
2943 Py_CLEAR(self->arg);
2944 Py_CLEAR(self->file);
2945 Py_CLEAR(self->pers_func);
2946 Py_CLEAR(self->inst_pers_func);
2947 Py_CLEAR(self->dispatch_table);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002948 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002949}
2950
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002951static PyObject *
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002952Pickler_get_pers_func(Picklerobject *p)
2953{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002954 if (p->pers_func == NULL)
2955 PyErr_SetString(PyExc_AttributeError, "persistent_id");
2956 else
2957 Py_INCREF(p->pers_func);
2958 return p->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002959}
2960
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002961static int
2962Pickler_set_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->pers_func);
2970 Py_INCREF(v);
2971 p->pers_func = v;
2972 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002973}
2974
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002975static int
2976Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
2977{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002978 if (v == NULL) {
2979 PyErr_SetString(PyExc_TypeError,
2980 "attribute deletion is not supported");
2981 return -1;
2982 }
2983 Py_XDECREF(p->inst_pers_func);
2984 Py_INCREF(v);
2985 p->inst_pers_func = v;
2986 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002987}
2988
2989static PyObject *
2990Pickler_get_memo(Picklerobject *p)
2991{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002992 if (p->memo == NULL)
2993 PyErr_SetString(PyExc_AttributeError, "memo");
2994 else
2995 Py_INCREF(p->memo);
2996 return p->memo;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002997}
2998
2999static int
3000Pickler_set_memo(Picklerobject *p, PyObject *v)
3001{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003002 if (v == NULL) {
3003 PyErr_SetString(PyExc_TypeError,
3004 "attribute deletion is not supported");
3005 return -1;
3006 }
3007 if (!PyDict_Check(v)) {
3008 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
3009 return -1;
3010 }
3011 Py_XDECREF(p->memo);
3012 Py_INCREF(v);
3013 p->memo = v;
3014 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003015}
3016
3017static PyObject *
3018Pickler_get_error(Picklerobject *p)
3019{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003020 /* why is this an attribute on the Pickler? */
3021 Py_INCREF(PicklingError);
3022 return PicklingError;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003023}
3024
3025static PyMemberDef Pickler_members[] = {
3026 {"binary", T_INT, offsetof(Picklerobject, bin)},
3027 {"fast", T_INT, offsetof(Picklerobject, fast)},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003028 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003029};
3030
3031static PyGetSetDef Pickler_getsets[] = {
Tim Peterscba30e22003-02-01 06:24:36 +00003032 {"persistent_id", (getter)Pickler_get_pers_func,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003033 (setter)Pickler_set_pers_func},
3034 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
3035 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003036 {"PicklingError", (getter)Pickler_get_error, NULL},
3037 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003038};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003039
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003040PyDoc_STRVAR(Picklertype__doc__,
3041"Objects that know how to pickle objects\n");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003042
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003043static PyTypeObject Picklertype = {
Martin v. Löwis68192102007-07-21 06:55:02 +00003044 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00003045 "cPickle.Pickler", /*tp_name*/
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003046 sizeof(Picklerobject), /*tp_basicsize*/
3047 0,
3048 (destructor)Pickler_dealloc, /* tp_dealloc */
3049 0, /* tp_print */
3050 0, /* tp_getattr */
3051 0, /* tp_setattr */
3052 0, /* tp_compare */
3053 0, /* tp_repr */
3054 0, /* tp_as_number */
3055 0, /* tp_as_sequence */
3056 0, /* tp_as_mapping */
3057 0, /* tp_hash */
3058 0, /* tp_call */
3059 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00003060 PyObject_GenericGetAttr, /* tp_getattro */
3061 PyObject_GenericSetAttr, /* tp_setattro */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003062 0, /* tp_as_buffer */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003063 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003064 Picklertype__doc__, /* tp_doc */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003065 (traverseproc)Pickler_traverse, /* tp_traverse */
3066 (inquiry)Pickler_clear, /* tp_clear */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003067 0, /* tp_richcompare */
3068 0, /* tp_weaklistoffset */
3069 0, /* tp_iter */
3070 0, /* tp_iternext */
3071 Pickler_methods, /* tp_methods */
3072 Pickler_members, /* tp_members */
3073 Pickler_getsets, /* tp_getset */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003074};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003075
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003076static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003077find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003078{
3079 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003080
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003081 if (fc) {
3082 if (fc==Py_None) {
Tim Peters92c8bb32003-02-13 23:00:26 +00003083 PyErr_SetString(UnpicklingError, "Global and instance "
3084 "pickles are not supported.");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003085 return NULL;
3086 }
Georg Brandl684fd0c2006-05-25 19:15:31 +00003087 return PyObject_CallFunctionObjArgs(fc, py_module_name,
3088 py_global_name, NULL);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003089 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003090
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003091 module = PySys_GetObject("modules");
3092 if (module == NULL)
3093 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00003094
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003095 module = PyDict_GetItem(module, py_module_name);
3096 if (module == NULL) {
3097 module = PyImport_Import(py_module_name);
3098 if (!module)
3099 return NULL;
3100 global = PyObject_GetAttr(module, py_global_name);
3101 Py_DECREF(module);
3102 }
3103 else
3104 global = PyObject_GetAttr(module, py_global_name);
3105 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003106}
3107
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003108static int
Tim Peterscba30e22003-02-01 06:24:36 +00003109marker(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003110{
3111 if (self->num_marks < 1) {
3112 PyErr_SetString(UnpicklingError, "could not find MARK");
3113 return -1;
3114 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003115
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003116 return self->marks[--self->num_marks];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003117}
3118
Tim Peters84e87f32001-03-17 04:50:51 +00003119
Guido van Rossum60456fd1997-04-09 17:36:32 +00003120static int
Tim Peterscba30e22003-02-01 06:24:36 +00003121load_none(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003122{
3123 PDATA_APPEND(self->stack, Py_None, -1);
3124 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003125}
3126
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003127static int
Tim Peterscba30e22003-02-01 06:24:36 +00003128bad_readline(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003129{
3130 PyErr_SetString(UnpicklingError, "pickle data was truncated");
3131 return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003132}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003133
3134static int
Tim Peterscba30e22003-02-01 06:24:36 +00003135load_int(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003136{
3137 PyObject *py_int = 0;
3138 char *endptr, *s;
3139 int len, res = -1;
3140 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003141
Tim Peters0bc93f52003-02-02 18:29:33 +00003142 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003143 if (len < 2) return bad_readline();
3144 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003145
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003146 errno = 0;
3147 l = strtol(s, &endptr, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003148
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003149 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
3150 /* Hm, maybe we've got something long. Let's try reading
3151 it as a Python long object. */
3152 errno = 0;
3153 py_int = PyLong_FromString(s, NULL, 0);
3154 if (py_int == NULL) {
3155 PyErr_SetString(PyExc_ValueError,
3156 "could not convert string to int");
3157 goto finally;
3158 }
3159 }
3160 else {
Guido van Rossume2763392002-04-05 19:30:08 +00003161 if (len == 3 && (l == 0 || l == 1)) {
3162 if (!( py_int = PyBool_FromLong(l))) goto finally;
3163 }
3164 else {
3165 if (!( py_int = PyInt_FromLong(l))) goto finally;
3166 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003167 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003168
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003169 free(s);
3170 PDATA_PUSH(self->stack, py_int, -1);
3171 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003172
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003173 finally:
3174 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003175
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003176 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003177}
3178
Tim Peters3c67d792003-02-02 17:59:11 +00003179static int
3180load_bool(Unpicklerobject *self, PyObject *boolean)
3181{
3182 assert(boolean == Py_True || boolean == Py_False);
Tim Peterse0a39072003-02-03 15:45:56 +00003183 PDATA_APPEND(self->stack, boolean, -1);
Tim Peters3c67d792003-02-02 17:59:11 +00003184 return 0;
3185}
3186
Tim Petersee1a53c2003-02-02 02:57:53 +00003187/* s contains x bytes of a little-endian integer. Return its value as a
3188 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3189 * int, but when x is 4 it's a signed one. This is an historical source
3190 * of x-platform bugs.
3191 */
Tim Peters84e87f32001-03-17 04:50:51 +00003192static long
Tim Petersee1a53c2003-02-02 02:57:53 +00003193calc_binint(char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003194{
3195 unsigned char c;
3196 int i;
3197 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003198
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003199 for (i = 0, l = 0L; i < x; i++) {
3200 c = (unsigned char)s[i];
3201 l |= (long)c << (i * 8);
3202 }
Tim Petersbfa18f72001-04-10 01:54:42 +00003203#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003204 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3205 * is signed, so on a box with longs bigger than 4 bytes we need
3206 * to extend a BININT's sign bit to the full width.
3207 */
3208 if (x == 4 && l & (1L << 31))
3209 l |= (~0L) << 32;
Tim Petersbfa18f72001-04-10 01:54:42 +00003210#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003211 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003212}
3213
3214
3215static int
Tim Peterscba30e22003-02-01 06:24:36 +00003216load_binintx(Unpicklerobject *self, char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003217{
3218 PyObject *py_int = 0;
3219 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003220
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003221 l = calc_binint(s, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003222
Tim Peterscba30e22003-02-01 06:24:36 +00003223 if (!( py_int = PyInt_FromLong(l)))
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 PDATA_PUSH(self->stack, py_int, -1);
3227 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003228}
3229
3230
3231static int
Tim Peterscba30e22003-02-01 06:24:36 +00003232load_binint(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003233{
3234 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003235
Tim Peters0bc93f52003-02-02 18:29:33 +00003236 if (self->read_func(self, &s, 4) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003237 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003238
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003239 return load_binintx(self, s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003240}
3241
3242
3243static int
Tim Peterscba30e22003-02-01 06:24:36 +00003244load_binint1(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003245{
3246 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003247
Tim Peters0bc93f52003-02-02 18:29:33 +00003248 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003249 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003250
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003251 return load_binintx(self, s, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003252}
3253
3254
3255static int
Tim Peterscba30e22003-02-01 06:24:36 +00003256load_binint2(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003257{
3258 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003259
Tim Peters0bc93f52003-02-02 18:29:33 +00003260 if (self->read_func(self, &s, 2) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003261 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003262
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003263 return load_binintx(self, s, 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003264}
Tim Peters84e87f32001-03-17 04:50:51 +00003265
Guido van Rossum60456fd1997-04-09 17:36:32 +00003266static int
Tim Peterscba30e22003-02-01 06:24:36 +00003267load_long(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003268{
3269 PyObject *l = 0;
3270 char *end, *s;
3271 int len, res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003272
Tim Peters0bc93f52003-02-02 18:29:33 +00003273 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003274 if (len < 2) return bad_readline();
3275 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003276
Tim Peterscba30e22003-02-01 06:24:36 +00003277 if (!( l = PyLong_FromString(s, &end, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003278 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003279
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003280 free(s);
3281 PDATA_PUSH(self->stack, l, -1);
3282 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003283
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003284 finally:
3285 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003286
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003287 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003288}
3289
Tim Petersee1a53c2003-02-02 02:57:53 +00003290/* 'size' bytes contain the # of bytes of little-endian 256's-complement
3291 * data following.
3292 */
3293static int
3294load_counted_long(Unpicklerobject *self, int size)
3295{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003296 Py_ssize_t i;
Tim Petersee1a53c2003-02-02 02:57:53 +00003297 char *nbytes;
3298 unsigned char *pdata;
3299 PyObject *along;
3300
3301 assert(size == 1 || size == 4);
3302 i = self->read_func(self, &nbytes, size);
3303 if (i < 0) return -1;
3304
3305 size = calc_binint(nbytes, size);
3306 if (size < 0) {
3307 /* Corrupt or hostile pickle -- we never write one like
3308 * this.
3309 */
Tim Peters0c7c48e2003-02-03 22:07:24 +00003310 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
Tim Petersee1a53c2003-02-02 02:57:53 +00003311 "byte count");
3312 return -1;
3313 }
3314
3315 if (size == 0)
3316 along = PyLong_FromLong(0L);
3317 else {
3318 /* Read the raw little-endian bytes & convert. */
Neal Norwitz529baf22003-02-02 17:08:33 +00003319 i = self->read_func(self, (char **)&pdata, size);
Tim Petersee1a53c2003-02-02 02:57:53 +00003320 if (i < 0) return -1;
3321 along = _PyLong_FromByteArray(pdata, (size_t)size,
3322 1 /* little endian */, 1 /* signed */);
3323 }
3324 if (along == NULL)
3325 return -1;
3326 PDATA_PUSH(self->stack, along, -1);
3327 return 0;
3328}
Tim Peters84e87f32001-03-17 04:50:51 +00003329
Guido van Rossum60456fd1997-04-09 17:36:32 +00003330static int
Tim Peterscba30e22003-02-01 06:24:36 +00003331load_float(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003332{
3333 PyObject *py_float = 0;
3334 char *endptr, *s;
3335 int len, res = -1;
3336 double d;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003337
Tim Peters0bc93f52003-02-02 18:29:33 +00003338 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003339 if (len < 2) return bad_readline();
3340 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003341
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003342 errno = 0;
Martin v. Löwis737ea822004-06-08 18:52:54 +00003343 d = PyOS_ascii_strtod(s, &endptr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003344
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003345 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
3346 PyErr_SetString(PyExc_ValueError,
3347 "could not convert string to float");
3348 goto finally;
3349 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003350
Tim Peterscba30e22003-02-01 06:24:36 +00003351 if (!( py_float = PyFloat_FromDouble(d)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003352 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003353
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003354 free(s);
3355 PDATA_PUSH(self->stack, py_float, -1);
3356 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003357
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003358 finally:
3359 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003360
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003361 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003362}
3363
Guido van Rossum60456fd1997-04-09 17:36:32 +00003364static int
Tim Peterscba30e22003-02-01 06:24:36 +00003365load_binfloat(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003366{
Tim Peters9905b942003-03-20 20:53:32 +00003367 PyObject *py_float;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003368 double x;
3369 char *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003370
Tim Peters0bc93f52003-02-02 18:29:33 +00003371 if (self->read_func(self, &p, 8) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003372 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003373
Tim Peters9905b942003-03-20 20:53:32 +00003374 x = _PyFloat_Unpack8((unsigned char *)p, 0);
3375 if (x == -1.0 && PyErr_Occurred())
3376 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003377
Tim Peters9905b942003-03-20 20:53:32 +00003378 py_float = PyFloat_FromDouble(x);
3379 if (py_float == NULL)
3380 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003381
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003382 PDATA_PUSH(self->stack, py_float, -1);
3383 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003384}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003385
3386static int
Tim Peterscba30e22003-02-01 06:24:36 +00003387load_string(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003388{
3389 PyObject *str = 0;
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003390 int len, res = -1;
3391 char *s, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003392
Tim Peters0bc93f52003-02-02 18:29:33 +00003393 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003394 if (len < 2) return bad_readline();
3395 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003396
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003397
3398 /* Strip outermost quotes */
3399 while (s[len-1] <= ' ')
3400 len--;
3401 if(s[0]=='"' && s[len-1]=='"'){
3402 s[len-1] = '\0';
3403 p = s + 1 ;
3404 len -= 2;
3405 } else if(s[0]=='\'' && s[len-1]=='\''){
3406 s[len-1] = '\0';
3407 p = s + 1 ;
3408 len -= 2;
3409 } else
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003410 goto insecure;
3411 /********************************************/
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003412
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003413 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
Neal Norwitz99dfe3c2006-08-02 04:27:11 +00003414 free(s);
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003415 if (str) {
3416 PDATA_PUSH(self->stack, str, -1);
3417 res = 0;
3418 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003419 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003420
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003421 insecure:
3422 free(s);
3423 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3424 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003425}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003426
3427
3428static int
Tim Peterscba30e22003-02-01 06:24:36 +00003429load_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003430{
3431 PyObject *py_string = 0;
3432 long l;
3433 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003434
Tim Peters0bc93f52003-02-02 18:29:33 +00003435 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003436
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003437 l = calc_binint(s, 4);
Gregory P. Smith9d534572008-06-11 07:41:16 +00003438 if (l < 0) {
3439 /* Corrupt or hostile pickle -- we never write one like
3440 * this.
3441 */
3442 PyErr_SetString(UnpicklingError,
3443 "BINSTRING pickle has negative byte count");
3444 return -1;
3445 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003446
Tim Peters0bc93f52003-02-02 18:29:33 +00003447 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003448 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003449
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003450 if (!( py_string = PyString_FromStringAndSize(s, l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003451 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;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003455}
3456
3457
3458static int
Tim Peterscba30e22003-02-01 06:24:36 +00003459load_short_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003460{
3461 PyObject *py_string = 0;
3462 unsigned char l;
3463 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003464
Tim Peters0bc93f52003-02-02 18:29:33 +00003465 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003466 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003467
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003468 l = (unsigned char)s[0];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003469
Tim Peters0bc93f52003-02-02 18:29:33 +00003470 if (self->read_func(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003471
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003472 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003473
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003474 PDATA_PUSH(self->stack, py_string, -1);
3475 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00003476}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003477
3478
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003479#ifdef Py_USING_UNICODE
Guido van Rossum60456fd1997-04-09 17:36:32 +00003480static int
Tim Peterscba30e22003-02-01 06:24:36 +00003481load_unicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003482{
3483 PyObject *str = 0;
3484 int len, res = -1;
3485 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003486
Tim Peters0bc93f52003-02-02 18:29:33 +00003487 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003488 if (len < 1) return bad_readline();
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003489
Tim Peterscba30e22003-02-01 06:24:36 +00003490 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003491 goto finally;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003492
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003493 PDATA_PUSH(self->stack, str, -1);
3494 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003495
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003496 finally:
3497 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003498}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003499#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003500
3501
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003502#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003503static int
Tim Peterscba30e22003-02-01 06:24:36 +00003504load_binunicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003505{
3506 PyObject *unicode;
3507 long l;
3508 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003509
Tim Peters0bc93f52003-02-02 18:29:33 +00003510 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003511
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003512 l = calc_binint(s, 4);
Gregory P. Smith9d534572008-06-11 07:41:16 +00003513 if (l < 0) {
3514 /* Corrupt or hostile pickle -- we never write one like
3515 * this.
3516 */
3517 PyErr_SetString(UnpicklingError,
3518 "BINUNICODE pickle has negative byte count");
3519 return -1;
3520 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003521
Tim Peters0bc93f52003-02-02 18:29:33 +00003522 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003523 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003524
Tim Peterscba30e22003-02-01 06:24:36 +00003525 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003526 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003527
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003528 PDATA_PUSH(self->stack, unicode, -1);
3529 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003530}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003531#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003532
3533
3534static int
Tim Peterscba30e22003-02-01 06:24:36 +00003535load_tuple(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003536{
3537 PyObject *tup;
3538 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003539
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003540 if ((i = marker(self)) < 0) return -1;
3541 if (!( tup=Pdata_popTuple(self->stack, i))) return -1;
3542 PDATA_PUSH(self->stack, tup, -1);
3543 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003544}
3545
3546static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00003547load_counted_tuple(Unpicklerobject *self, int len)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003548{
Tim Peters1d63c9f2003-02-02 20:29:39 +00003549 PyObject *tup = PyTuple_New(len);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003550
Tim Peters1d63c9f2003-02-02 20:29:39 +00003551 if (tup == NULL)
3552 return -1;
3553
3554 while (--len >= 0) {
3555 PyObject *element;
3556
3557 PDATA_POP(self->stack, element);
3558 if (element == NULL)
3559 return -1;
3560 PyTuple_SET_ITEM(tup, len, element);
3561 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003562 PDATA_PUSH(self->stack, tup, -1);
3563 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003564}
3565
3566static int
Tim Peterscba30e22003-02-01 06:24:36 +00003567load_empty_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003568{
3569 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003570
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003571 if (!( list=PyList_New(0))) return -1;
3572 PDATA_PUSH(self->stack, list, -1);
3573 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003574}
3575
3576static int
Tim Peterscba30e22003-02-01 06:24:36 +00003577load_empty_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003578{
3579 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003580
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003581 if (!( dict=PyDict_New())) return -1;
3582 PDATA_PUSH(self->stack, dict, -1);
3583 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003584}
3585
3586
3587static int
Tim Peterscba30e22003-02-01 06:24:36 +00003588load_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003589{
3590 PyObject *list = 0;
3591 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003592
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003593 if ((i = marker(self)) < 0) return -1;
3594 if (!( list=Pdata_popList(self->stack, i))) return -1;
3595 PDATA_PUSH(self->stack, list, -1);
3596 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003597}
3598
3599static int
Tim Peterscba30e22003-02-01 06:24:36 +00003600load_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003601{
3602 PyObject *dict, *key, *value;
3603 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003604
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003605 if ((i = marker(self)) < 0) return -1;
3606 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003607
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003608 if (!( dict = PyDict_New())) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003609
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003610 for (k = i+1; k < j; k += 2) {
3611 key =self->stack->data[k-1];
3612 value=self->stack->data[k ];
3613 if (PyDict_SetItem(dict, key, value) < 0) {
3614 Py_DECREF(dict);
3615 return -1;
3616 }
3617 }
3618 Pdata_clear(self->stack, i);
3619 PDATA_PUSH(self->stack, dict, -1);
3620 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003621}
3622
3623static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003624Instance_New(PyObject *cls, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003625{
Tim Peters1f1b2d22003-02-01 02:16:37 +00003626 PyObject *r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003627
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003628 if (PyClass_Check(cls)) {
3629 int l;
Tim Peters84e87f32001-03-17 04:50:51 +00003630
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003631 if ((l=PyObject_Size(args)) < 0) goto err;
3632 if (!( l )) {
3633 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003634
Tim Peterscba30e22003-02-01 06:24:36 +00003635 __getinitargs__ = PyObject_GetAttr(cls,
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00003636 __getinitargs___str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003637 if (!__getinitargs__) {
Tim Peterscba30e22003-02-01 06:24:36 +00003638 /* We have a class with no __getinitargs__,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003639 so bypass usual construction */
3640 PyObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003641
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003642 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00003643 if (!( inst=PyInstance_NewRaw(cls, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003644 goto err;
3645 return inst;
3646 }
3647 Py_DECREF(__getinitargs__);
3648 }
Tim Peters84e87f32001-03-17 04:50:51 +00003649
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003650 if ((r=PyInstance_New(cls, args, NULL))) return r;
3651 else goto err;
3652 }
Tim Peters84e87f32001-03-17 04:50:51 +00003653
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003654 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00003655
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003656 err:
3657 {
Neal Norwitz0f8b31a2006-06-28 06:28:31 +00003658 PyObject *tp, *v, *tb, *tmp_value;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003659
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003660 PyErr_Fetch(&tp, &v, &tb);
Neal Norwitz0f8b31a2006-06-28 06:28:31 +00003661 tmp_value = v;
3662 /* NULL occurs when there was a KeyboardInterrupt */
3663 if (tmp_value == NULL)
3664 tmp_value = Py_None;
3665 if ((r = PyTuple_Pack(3, tmp_value, cls, args))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003666 Py_XDECREF(v);
3667 v=r;
3668 }
3669 PyErr_Restore(tp,v,tb);
3670 }
3671 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003672}
Tim Peters84e87f32001-03-17 04:50:51 +00003673
Guido van Rossum60456fd1997-04-09 17:36:32 +00003674
3675static int
Tim Peterscba30e22003-02-01 06:24:36 +00003676load_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003677{
3678 PyObject *class, *tup, *obj=0;
3679 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003680
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003681 if ((i = marker(self)) < 0) return -1;
3682 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3683 PDATA_POP(self->stack, class);
3684 if (class) {
3685 obj = Instance_New(class, tup);
3686 Py_DECREF(class);
3687 }
3688 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003689
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003690 if (! obj) return -1;
3691 PDATA_PUSH(self->stack, obj, -1);
3692 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003693}
3694
3695
3696static int
Tim Peterscba30e22003-02-01 06:24:36 +00003697load_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003698{
3699 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
3700 int i, len;
3701 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003702
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003703 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003704
Tim Peters0bc93f52003-02-02 18:29:33 +00003705 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003706 if (len < 2) return bad_readline();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003707 module_name = PyString_FromStringAndSize(s, len - 1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003708 if (!module_name) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003709
Tim Peters0bc93f52003-02-02 18:29:33 +00003710 if ((len = self->readline_func(self, &s)) >= 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003711 if (len < 2) return bad_readline();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003712 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003713 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003714 self->find_class);
3715 Py_DECREF(class_name);
3716 }
3717 }
3718 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003719
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003720 if (! class) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003721
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003722 if ((tup=Pdata_popTuple(self->stack, i))) {
3723 obj = Instance_New(class, tup);
3724 Py_DECREF(tup);
3725 }
3726 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003727
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003728 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003729
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003730 PDATA_PUSH(self->stack, obj, -1);
3731 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003732}
3733
Tim Peterseab7db32003-02-13 18:24:14 +00003734static int
3735load_newobj(Unpicklerobject *self)
3736{
3737 PyObject *args = NULL;
3738 PyObject *clsraw = NULL;
3739 PyTypeObject *cls; /* clsraw cast to its true type */
3740 PyObject *obj;
3741
3742 /* Stack is ... cls argtuple, and we want to call
3743 * cls.__new__(cls, *argtuple).
3744 */
3745 PDATA_POP(self->stack, args);
3746 if (args == NULL) goto Fail;
3747 if (! PyTuple_Check(args)) {
3748 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
3749 "tuple.");
3750 goto Fail;
3751 }
3752
3753 PDATA_POP(self->stack, clsraw);
3754 cls = (PyTypeObject *)clsraw;
3755 if (cls == NULL) goto Fail;
3756 if (! PyType_Check(cls)) {
3757 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3758 "isn't a type object");
3759 goto Fail;
3760 }
3761 if (cls->tp_new == NULL) {
3762 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3763 "has NULL tp_new");
3764 goto Fail;
3765 }
3766
3767 /* Call __new__. */
3768 obj = cls->tp_new(cls, args, NULL);
3769 if (obj == NULL) goto Fail;
3770
3771 Py_DECREF(args);
3772 Py_DECREF(clsraw);
3773 PDATA_PUSH(self->stack, obj, -1);
3774 return 0;
3775
3776 Fail:
3777 Py_XDECREF(args);
3778 Py_XDECREF(clsraw);
3779 return -1;
3780}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003781
3782static int
Tim Peterscba30e22003-02-01 06:24:36 +00003783load_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003784{
3785 PyObject *class = 0, *module_name = 0, *class_name = 0;
3786 int len;
3787 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003788
Tim Peters0bc93f52003-02-02 18:29:33 +00003789 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003790 if (len < 2) return bad_readline();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003791 module_name = PyString_FromStringAndSize(s, len - 1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003792 if (!module_name) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003793
Tim Peters0bc93f52003-02-02 18:29:33 +00003794 if ((len = self->readline_func(self, &s)) >= 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +00003795 if (len < 2) {
3796 Py_DECREF(module_name);
3797 return bad_readline();
3798 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003799 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003800 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003801 self->find_class);
3802 Py_DECREF(class_name);
3803 }
3804 }
3805 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003806
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003807 if (! class) return -1;
3808 PDATA_PUSH(self->stack, class, -1);
3809 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003810}
3811
3812
3813static int
Tim Peterscba30e22003-02-01 06:24:36 +00003814load_persid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003815{
3816 PyObject *pid = 0;
3817 int len;
3818 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003819
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003820 if (self->pers_func) {
Tim Peters0bc93f52003-02-02 18:29:33 +00003821 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003822 if (len < 2) return bad_readline();
3823
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003824 pid = PyString_FromStringAndSize(s, len - 1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003825 if (!pid) return -1;
3826
3827 if (PyList_Check(self->pers_func)) {
3828 if (PyList_Append(self->pers_func, pid) < 0) {
3829 Py_DECREF(pid);
3830 return -1;
3831 }
3832 }
3833 else {
3834 ARG_TUP(self, pid);
3835 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00003836 pid = PyObject_Call(self->pers_func, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003837 NULL);
3838 FREE_ARG_TUP(self);
3839 }
3840 }
3841
3842 if (! pid) return -1;
3843
3844 PDATA_PUSH(self->stack, pid, -1);
3845 return 0;
3846 }
3847 else {
3848 PyErr_SetString(UnpicklingError,
3849 "A load persistent id instruction was encountered,\n"
3850 "but no persistent_load function was specified.");
3851 return -1;
3852 }
3853}
3854
3855static int
Tim Peterscba30e22003-02-01 06:24:36 +00003856load_binpersid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003857{
3858 PyObject *pid = 0;
3859
3860 if (self->pers_func) {
3861 PDATA_POP(self->stack, pid);
3862 if (! pid) return -1;
3863
3864 if (PyList_Check(self->pers_func)) {
3865 if (PyList_Append(self->pers_func, pid) < 0) {
3866 Py_DECREF(pid);
3867 return -1;
3868 }
3869 }
3870 else {
3871 ARG_TUP(self, pid);
3872 if (self->arg) {
3873 pid = PyObject_Call(self->pers_func, self->arg,
3874 NULL);
3875 FREE_ARG_TUP(self);
3876 }
3877 if (! pid) return -1;
3878 }
3879
3880 PDATA_PUSH(self->stack, pid, -1);
3881 return 0;
3882 }
3883 else {
3884 PyErr_SetString(UnpicklingError,
3885 "A load persistent id instruction was encountered,\n"
3886 "but no persistent_load function was specified.");
3887 return -1;
3888 }
3889}
3890
3891
3892static int
Tim Peterscba30e22003-02-01 06:24:36 +00003893load_pop(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003894{
3895 int len;
3896
3897 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
3898
3899 /* Note that we split the (pickle.py) stack into two stacks,
3900 an object stack and a mark stack. We have to be clever and
3901 pop the right one. We do this by looking at the top of the
3902 mark stack.
3903 */
3904
3905 if ((self->num_marks > 0) &&
3906 (self->marks[self->num_marks - 1] == len))
3907 self->num_marks--;
3908 else {
3909 len--;
3910 Py_DECREF(self->stack->data[len]);
3911 self->stack->length=len;
3912 }
3913
3914 return 0;
3915}
3916
3917
3918static int
Tim Peterscba30e22003-02-01 06:24:36 +00003919load_pop_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003920{
3921 int i;
3922
3923 if ((i = marker(self)) < 0)
3924 return -1;
3925
3926 Pdata_clear(self->stack, i);
3927
3928 return 0;
3929}
3930
3931
3932static int
Tim Peterscba30e22003-02-01 06:24:36 +00003933load_dup(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003934{
3935 PyObject *last;
3936 int len;
3937
3938 if ((len = self->stack->length) <= 0) return stackUnderflow();
3939 last=self->stack->data[len-1];
3940 Py_INCREF(last);
3941 PDATA_PUSH(self->stack, last, -1);
3942 return 0;
3943}
3944
3945
3946static int
Tim Peterscba30e22003-02-01 06:24:36 +00003947load_get(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003948{
3949 PyObject *py_str = 0, *value = 0;
3950 int len;
3951 char *s;
3952 int rc;
3953
Tim Peters0bc93f52003-02-02 18:29:33 +00003954 if ((len = self->readline_func(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003955 if (len < 2) return bad_readline();
Tim Peters84e87f32001-03-17 04:50:51 +00003956
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003957 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003958
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003959 value = PyDict_GetItem(self->memo, py_str);
3960 if (! value) {
3961 PyErr_SetObject(BadPickleGet, py_str);
3962 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00003963 }
3964 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003965 PDATA_APPEND(self->stack, value, -1);
3966 rc = 0;
3967 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003968
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003969 Py_DECREF(py_str);
3970 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003971}
3972
3973
3974static int
Tim Peterscba30e22003-02-01 06:24:36 +00003975load_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003976{
3977 PyObject *py_key = 0, *value = 0;
3978 unsigned char key;
3979 char *s;
3980 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003981
Tim Peters0bc93f52003-02-02 18:29:33 +00003982 if (self->read_func(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003983
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003984 key = (unsigned char)s[0];
3985 if (!( py_key = PyInt_FromLong((long)key))) return -1;
Guido van Rossumea2b7152000-05-09 18:14:50 +00003986
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003987 value = PyDict_GetItem(self->memo, py_key);
3988 if (! value) {
3989 PyErr_SetObject(BadPickleGet, py_key);
3990 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00003991 }
3992 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003993 PDATA_APPEND(self->stack, value, -1);
3994 rc = 0;
3995 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003996
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003997 Py_DECREF(py_key);
3998 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003999}
4000
4001
4002static int
Tim Peterscba30e22003-02-01 06:24:36 +00004003load_long_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004004{
4005 PyObject *py_key = 0, *value = 0;
4006 unsigned char c;
4007 char *s;
4008 long key;
4009 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004010
Tim Peters0bc93f52003-02-02 18:29:33 +00004011 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004012
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004013 c = (unsigned char)s[0];
4014 key = (long)c;
4015 c = (unsigned char)s[1];
4016 key |= (long)c << 8;
4017 c = (unsigned char)s[2];
4018 key |= (long)c << 16;
4019 c = (unsigned char)s[3];
4020 key |= (long)c << 24;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004021
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004022 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4023
4024 value = PyDict_GetItem(self->memo, py_key);
4025 if (! value) {
4026 PyErr_SetObject(BadPickleGet, py_key);
4027 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00004028 }
4029 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004030 PDATA_APPEND(self->stack, value, -1);
4031 rc = 0;
4032 }
4033
4034 Py_DECREF(py_key);
4035 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004036}
4037
Tim Peters2d629652003-02-04 05:06:17 +00004038/* Push an object from the extension registry (EXT[124]). nbytes is
4039 * the number of bytes following the opcode, holding the index (code) value.
4040 */
4041static int
4042load_extension(Unpicklerobject *self, int nbytes)
4043{
4044 char *codebytes; /* the nbytes bytes after the opcode */
4045 long code; /* calc_binint returns long */
4046 PyObject *py_code; /* code as a Python int */
4047 PyObject *obj; /* the object to push */
4048 PyObject *pair; /* (module_name, class_name) */
4049 PyObject *module_name, *class_name;
4050
4051 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4052 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4053 code = calc_binint(codebytes, nbytes);
4054 if (code <= 0) { /* note that 0 is forbidden */
4055 /* Corrupt or hostile pickle. */
4056 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
4057 return -1;
4058 }
4059
4060 /* Look for the code in the cache. */
4061 py_code = PyInt_FromLong(code);
4062 if (py_code == NULL) return -1;
4063 obj = PyDict_GetItem(extension_cache, py_code);
4064 if (obj != NULL) {
4065 /* Bingo. */
4066 Py_DECREF(py_code);
4067 PDATA_APPEND(self->stack, obj, -1);
4068 return 0;
4069 }
4070
4071 /* Look up the (module_name, class_name) pair. */
4072 pair = PyDict_GetItem(inverted_registry, py_code);
4073 if (pair == NULL) {
4074 Py_DECREF(py_code);
4075 PyErr_Format(PyExc_ValueError, "unregistered extension "
4076 "code %ld", code);
4077 return -1;
4078 }
4079 /* Since the extension registry is manipulable via Python code,
Tim Petersfa05ce32003-02-04 05:20:32 +00004080 * confirm that pair is really a 2-tuple of strings.
Tim Peters2d629652003-02-04 05:06:17 +00004081 */
4082 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004083 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
4084 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
Tim Peters2d629652003-02-04 05:06:17 +00004085 Py_DECREF(py_code);
4086 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
4087 "isn't a 2-tuple of strings", code);
4088 return -1;
4089 }
4090 /* Load the object. */
4091 obj = find_class(module_name, class_name, self->find_class);
4092 if (obj == NULL) {
4093 Py_DECREF(py_code);
4094 return -1;
4095 }
4096 /* Cache code -> obj. */
4097 code = PyDict_SetItem(extension_cache, py_code, obj);
4098 Py_DECREF(py_code);
4099 if (code < 0) {
4100 Py_DECREF(obj);
4101 return -1;
4102 }
4103 PDATA_PUSH(self->stack, obj, -1);
4104 return 0;
4105}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004106
4107static int
Tim Peterscba30e22003-02-01 06:24:36 +00004108load_put(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004109{
4110 PyObject *py_str = 0, *value = 0;
4111 int len, l;
4112 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004113
Tim Peters0bc93f52003-02-02 18:29:33 +00004114 if ((l = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004115 if (l < 2) return bad_readline();
4116 if (!( len=self->stack->length )) return stackUnderflow();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004117 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004118 value=self->stack->data[len-1];
4119 l=PyDict_SetItem(self->memo, py_str, value);
4120 Py_DECREF(py_str);
4121 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004122}
4123
4124
4125static int
Tim Peterscba30e22003-02-01 06:24:36 +00004126load_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004127{
4128 PyObject *py_key = 0, *value = 0;
4129 unsigned char key;
4130 char *s;
4131 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004132
Tim Peters0bc93f52003-02-02 18:29:33 +00004133 if (self->read_func(self, &s, 1) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004134 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004135
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004136 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00004137
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004138 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4139 value=self->stack->data[len-1];
4140 len=PyDict_SetItem(self->memo, py_key, value);
4141 Py_DECREF(py_key);
4142 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004143}
4144
4145
4146static int
Tim Peterscba30e22003-02-01 06:24:36 +00004147load_long_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004148{
4149 PyObject *py_key = 0, *value = 0;
4150 long key;
4151 unsigned char c;
4152 char *s;
4153 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004154
Tim Peters0bc93f52003-02-02 18:29:33 +00004155 if (self->read_func(self, &s, 4) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004156 if (!( len=self->stack->length )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004157
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004158 c = (unsigned char)s[0];
4159 key = (long)c;
4160 c = (unsigned char)s[1];
4161 key |= (long)c << 8;
4162 c = (unsigned char)s[2];
4163 key |= (long)c << 16;
4164 c = (unsigned char)s[3];
4165 key |= (long)c << 24;
Tim Peters84e87f32001-03-17 04:50:51 +00004166
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004167 if (!( py_key = PyInt_FromLong(key))) return -1;
4168 value=self->stack->data[len-1];
4169 len=PyDict_SetItem(self->memo, py_key, value);
4170 Py_DECREF(py_key);
4171 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004172}
4173
4174
4175static int
Tim Peterscba30e22003-02-01 06:24:36 +00004176do_append(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004177{
4178 PyObject *value = 0, *list = 0, *append_method = 0;
4179 int len, i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004180
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004181 len=self->stack->length;
4182 if (!( len >= x && x > 0 )) return stackUnderflow();
4183 /* nothing to do */
Tim Peterscba30e22003-02-01 06:24:36 +00004184 if (len==x) return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004185
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004186 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004187
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004188 if (PyList_Check(list)) {
4189 PyObject *slice;
4190 int list_len;
Tim Peters84e87f32001-03-17 04:50:51 +00004191
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004192 slice=Pdata_popList(self->stack, x);
Neal Norwitzb59d08c2006-07-22 16:20:49 +00004193 if (! slice) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004194 list_len = PyList_GET_SIZE(list);
4195 i=PyList_SetSlice(list, list_len, list_len, slice);
4196 Py_DECREF(slice);
4197 return i;
4198 }
4199 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004200
Tim Peterscba30e22003-02-01 06:24:36 +00004201 if (!( append_method = PyObject_GetAttr(list, append_str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004202 return -1;
4203
4204 for (i = x; i < len; i++) {
4205 PyObject *junk;
4206
4207 value=self->stack->data[i];
4208 junk=0;
4209 ARG_TUP(self, value);
4210 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00004211 junk = PyObject_Call(append_method, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004212 NULL);
4213 FREE_ARG_TUP(self);
4214 }
4215 if (! junk) {
4216 Pdata_clear(self->stack, i+1);
4217 self->stack->length=x;
4218 Py_DECREF(append_method);
4219 return -1;
4220 }
4221 Py_DECREF(junk);
4222 }
4223 self->stack->length=x;
4224 Py_DECREF(append_method);
4225 }
4226
4227 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004228}
4229
4230
4231static int
Tim Peterscba30e22003-02-01 06:24:36 +00004232load_append(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004233{
4234 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004235}
4236
4237
4238static int
Tim Peterscba30e22003-02-01 06:24:36 +00004239load_appends(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004240{
4241 return do_append(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004242}
4243
4244
4245static int
Tim Peterscba30e22003-02-01 06:24:36 +00004246do_setitems(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004247{
4248 PyObject *value = 0, *key = 0, *dict = 0;
4249 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004250
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004251 if (!( (len=self->stack->length) >= x
4252 && x > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004253
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004254 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004255
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004256 for (i = x+1; i < len; i += 2) {
4257 key =self->stack->data[i-1];
4258 value=self->stack->data[i ];
4259 if (PyObject_SetItem(dict, key, value) < 0) {
4260 r=-1;
4261 break;
4262 }
4263 }
4264
4265 Pdata_clear(self->stack, x);
4266
4267 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004268}
4269
4270
Tim Peters84e87f32001-03-17 04:50:51 +00004271static int
Tim Peterscba30e22003-02-01 06:24:36 +00004272load_setitem(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004273{
4274 return do_setitems(self, self->stack->length - 2);
4275}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004276
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004277static int
Tim Peterscba30e22003-02-01 06:24:36 +00004278load_setitems(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004279{
4280 return do_setitems(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004281}
4282
Tim Peters84e87f32001-03-17 04:50:51 +00004283
Guido van Rossum60456fd1997-04-09 17:36:32 +00004284static int
Tim Peterscba30e22003-02-01 06:24:36 +00004285load_build(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004286{
Tim Peters080c88b2003-02-15 03:01:11 +00004287 PyObject *state, *inst, *slotstate;
4288 PyObject *__setstate__;
4289 PyObject *d_key, *d_value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004290 Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00004291 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004292
Tim Peters080c88b2003-02-15 03:01:11 +00004293 /* Stack is ... instance, state. We want to leave instance at
4294 * the stack top, possibly mutated via instance.__setstate__(state).
4295 */
4296 if (self->stack->length < 2)
4297 return stackUnderflow();
4298 PDATA_POP(self->stack, state);
4299 if (state == NULL)
4300 return -1;
4301 inst = self->stack->data[self->stack->length - 1];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004302
Tim Peters080c88b2003-02-15 03:01:11 +00004303 __setstate__ = PyObject_GetAttr(inst, __setstate___str);
4304 if (__setstate__ != NULL) {
4305 PyObject *junk = NULL;
4306
4307 /* The explicit __setstate__ is responsible for everything. */
4308 ARG_TUP(self, state);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004309 if (self->arg) {
4310 junk = PyObject_Call(__setstate__, self->arg, NULL);
4311 FREE_ARG_TUP(self);
4312 }
4313 Py_DECREF(__setstate__);
Tim Peters080c88b2003-02-15 03:01:11 +00004314 if (junk == NULL)
4315 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004316 Py_DECREF(junk);
4317 return 0;
4318 }
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00004319 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4320 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004321 PyErr_Clear();
Tim Peters080c88b2003-02-15 03:01:11 +00004322
4323 /* A default __setstate__. First see whether state embeds a
4324 * slot state dict too (a proto 2 addition).
4325 */
4326 if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
4327 PyObject *temp = state;
4328 state = PyTuple_GET_ITEM(temp, 0);
4329 slotstate = PyTuple_GET_ITEM(temp, 1);
4330 Py_INCREF(state);
4331 Py_INCREF(slotstate);
4332 Py_DECREF(temp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004333 }
Tim Peters080c88b2003-02-15 03:01:11 +00004334 else
4335 slotstate = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004336
Tim Peters080c88b2003-02-15 03:01:11 +00004337 /* Set inst.__dict__ from the state dict (if any). */
4338 if (state != Py_None) {
4339 PyObject *dict;
4340 if (! PyDict_Check(state)) {
4341 PyErr_SetString(UnpicklingError, "state is not a "
4342 "dictionary");
4343 goto finally;
4344 }
4345 dict = PyObject_GetAttr(inst, __dict___str);
4346 if (dict == NULL)
4347 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004348
Tim Peters080c88b2003-02-15 03:01:11 +00004349 i = 0;
4350 while (PyDict_Next(state, &i, &d_key, &d_value)) {
4351 if (PyObject_SetItem(dict, d_key, d_value) < 0)
4352 goto finally;
4353 }
4354 Py_DECREF(dict);
4355 }
4356
4357 /* Also set instance attributes from the slotstate dict (if any). */
4358 if (slotstate != NULL) {
4359 if (! PyDict_Check(slotstate)) {
4360 PyErr_SetString(UnpicklingError, "slot state is not "
4361 "a dictionary");
4362 goto finally;
4363 }
4364 i = 0;
4365 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
4366 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
4367 goto finally;
4368 }
4369 }
4370 res = 0;
4371
4372 finally:
4373 Py_DECREF(state);
4374 Py_XDECREF(slotstate);
4375 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004376}
4377
4378
4379static int
Tim Peterscba30e22003-02-01 06:24:36 +00004380load_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004381{
4382 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004383
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004384 /* Note that we split the (pickle.py) stack into two stacks, an
4385 object stack and a mark stack. Here we push a mark onto the
4386 mark stack.
4387 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00004388
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004389 if ((self->num_marks + 1) >= self->marks_size) {
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004390 int *marks;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004391 s=self->marks_size+20;
4392 if (s <= self->num_marks) s=self->num_marks + 1;
4393 if (self->marks == NULL)
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004394 marks=(int *)malloc(s * sizeof(int));
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004395 else
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004396 marks=(int *)realloc(self->marks,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004397 s * sizeof(int));
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004398 if (!marks) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004399 PyErr_NoMemory();
4400 return -1;
4401 }
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004402 self->marks = marks;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004403 self->marks_size = s;
4404 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004405
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004406 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004407
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004408 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004409}
4410
Guido van Rossum60456fd1997-04-09 17:36:32 +00004411static int
Tim Peterscba30e22003-02-01 06:24:36 +00004412load_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004413{
4414 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004415
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004416 PDATA_POP(self->stack, arg_tup);
4417 if (! arg_tup) return -1;
4418 PDATA_POP(self->stack, callable);
4419 if (callable) {
4420 ob = Instance_New(callable, arg_tup);
4421 Py_DECREF(callable);
4422 }
4423 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004424
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004425 if (! ob) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004426
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004427 PDATA_PUSH(self->stack, ob, -1);
4428 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004429}
Tim Peters84e87f32001-03-17 04:50:51 +00004430
Tim Peters4190fb82003-02-02 16:09:05 +00004431/* Just raises an error if we don't know the protocol specified. PROTO
4432 * is the first opcode for protocols >= 2.
4433 */
4434static int
4435load_proto(Unpicklerobject *self)
4436{
4437 int i;
4438 char *protobyte;
4439
4440 i = self->read_func(self, &protobyte, 1);
4441 if (i < 0)
4442 return -1;
4443
4444 i = calc_binint(protobyte, 1);
4445 /* No point checking for < 0, since calc_binint returns an unsigned
4446 * int when chewing on 1 byte.
4447 */
4448 assert(i >= 0);
Tim Peters8587b3c2003-02-13 15:44:41 +00004449 if (i <= HIGHEST_PROTOCOL)
Tim Peters4190fb82003-02-02 16:09:05 +00004450 return 0;
4451
4452 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4453 return -1;
4454}
4455
Guido van Rossum60456fd1997-04-09 17:36:32 +00004456static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004457load(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004458{
4459 PyObject *err = 0, *val = 0;
4460 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004461
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004462 self->num_marks = 0;
4463 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004464
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004465 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004466 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004467 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004468
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004469 switch (s[0]) {
4470 case NONE:
4471 if (load_none(self) < 0)
4472 break;
4473 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004474
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004475 case BININT:
4476 if (load_binint(self) < 0)
4477 break;
4478 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004479
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004480 case BININT1:
4481 if (load_binint1(self) < 0)
4482 break;
4483 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004484
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004485 case BININT2:
4486 if (load_binint2(self) < 0)
4487 break;
4488 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004489
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004490 case INT:
4491 if (load_int(self) < 0)
4492 break;
4493 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004494
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004495 case LONG:
4496 if (load_long(self) < 0)
4497 break;
4498 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004499
Tim Petersee1a53c2003-02-02 02:57:53 +00004500 case LONG1:
4501 if (load_counted_long(self, 1) < 0)
4502 break;
4503 continue;
4504
4505 case LONG4:
4506 if (load_counted_long(self, 4) < 0)
4507 break;
4508 continue;
4509
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004510 case FLOAT:
4511 if (load_float(self) < 0)
4512 break;
4513 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004514
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004515 case BINFLOAT:
4516 if (load_binfloat(self) < 0)
4517 break;
4518 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004519
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004520 case BINSTRING:
4521 if (load_binstring(self) < 0)
4522 break;
4523 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004524
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004525 case SHORT_BINSTRING:
4526 if (load_short_binstring(self) < 0)
4527 break;
4528 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004529
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004530 case STRING:
4531 if (load_string(self) < 0)
4532 break;
4533 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004534
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004535#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004536 case UNICODE:
4537 if (load_unicode(self) < 0)
4538 break;
4539 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004540
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004541 case BINUNICODE:
4542 if (load_binunicode(self) < 0)
4543 break;
4544 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004545#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004546
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004547 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004548 if (load_counted_tuple(self, 0) < 0)
4549 break;
4550 continue;
4551
4552 case TUPLE1:
4553 if (load_counted_tuple(self, 1) < 0)
4554 break;
4555 continue;
4556
4557 case TUPLE2:
4558 if (load_counted_tuple(self, 2) < 0)
4559 break;
4560 continue;
4561
4562 case TUPLE3:
4563 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004564 break;
4565 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004566
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004567 case TUPLE:
4568 if (load_tuple(self) < 0)
4569 break;
4570 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004571
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004572 case EMPTY_LIST:
4573 if (load_empty_list(self) < 0)
4574 break;
4575 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004576
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004577 case LIST:
4578 if (load_list(self) < 0)
4579 break;
4580 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004581
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004582 case EMPTY_DICT:
4583 if (load_empty_dict(self) < 0)
4584 break;
4585 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004586
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004587 case DICT:
4588 if (load_dict(self) < 0)
4589 break;
4590 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004591
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004592 case OBJ:
4593 if (load_obj(self) < 0)
4594 break;
4595 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004596
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004597 case INST:
4598 if (load_inst(self) < 0)
4599 break;
4600 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004601
Tim Peterseab7db32003-02-13 18:24:14 +00004602 case NEWOBJ:
4603 if (load_newobj(self) < 0)
4604 break;
4605 continue;
4606
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004607 case GLOBAL:
4608 if (load_global(self) < 0)
4609 break;
4610 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004611
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004612 case APPEND:
4613 if (load_append(self) < 0)
4614 break;
4615 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004616
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004617 case APPENDS:
4618 if (load_appends(self) < 0)
4619 break;
4620 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004621
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004622 case BUILD:
4623 if (load_build(self) < 0)
4624 break;
4625 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004626
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004627 case DUP:
4628 if (load_dup(self) < 0)
4629 break;
4630 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004631
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004632 case BINGET:
4633 if (load_binget(self) < 0)
4634 break;
4635 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004636
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004637 case LONG_BINGET:
4638 if (load_long_binget(self) < 0)
4639 break;
4640 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004641
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004642 case GET:
4643 if (load_get(self) < 0)
4644 break;
4645 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004646
Tim Peters2d629652003-02-04 05:06:17 +00004647 case EXT1:
4648 if (load_extension(self, 1) < 0)
4649 break;
4650 continue;
4651
4652 case EXT2:
4653 if (load_extension(self, 2) < 0)
4654 break;
4655 continue;
4656
4657 case EXT4:
4658 if (load_extension(self, 4) < 0)
4659 break;
4660 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004661 case MARK:
4662 if (load_mark(self) < 0)
4663 break;
4664 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004665
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004666 case BINPUT:
4667 if (load_binput(self) < 0)
4668 break;
4669 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004670
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004671 case LONG_BINPUT:
4672 if (load_long_binput(self) < 0)
4673 break;
4674 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004675
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004676 case PUT:
4677 if (load_put(self) < 0)
4678 break;
4679 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004680
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004681 case POP:
4682 if (load_pop(self) < 0)
4683 break;
4684 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004685
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004686 case POP_MARK:
4687 if (load_pop_mark(self) < 0)
4688 break;
4689 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004690
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004691 case SETITEM:
4692 if (load_setitem(self) < 0)
4693 break;
4694 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004695
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004696 case SETITEMS:
4697 if (load_setitems(self) < 0)
4698 break;
4699 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004700
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004701 case STOP:
4702 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004703
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004704 case PERSID:
4705 if (load_persid(self) < 0)
4706 break;
4707 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004708
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004709 case BINPERSID:
4710 if (load_binpersid(self) < 0)
4711 break;
4712 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004713
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004714 case REDUCE:
4715 if (load_reduce(self) < 0)
4716 break;
4717 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004718
Tim Peters4190fb82003-02-02 16:09:05 +00004719 case PROTO:
4720 if (load_proto(self) < 0)
4721 break;
4722 continue;
4723
Tim Peters3c67d792003-02-02 17:59:11 +00004724 case NEWTRUE:
4725 if (load_bool(self, Py_True) < 0)
4726 break;
4727 continue;
4728
4729 case NEWFALSE:
4730 if (load_bool(self, Py_False) < 0)
4731 break;
4732 continue;
4733
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004734 case '\0':
4735 /* end of file */
4736 PyErr_SetNone(PyExc_EOFError);
4737 break;
Tim Peterscba30e22003-02-01 06:24:36 +00004738
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004739 default:
Tim Peterscba30e22003-02-01 06:24:36 +00004740 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004741 "invalid load key, '%s'.",
4742 "c", s[0]);
4743 return NULL;
4744 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004745
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004746 break;
4747 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004748
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004749 if ((err = PyErr_Occurred())) {
4750 if (err == PyExc_EOFError) {
4751 PyErr_SetNone(PyExc_EOFError);
4752 }
4753 return NULL;
4754 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004755
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004756 PDATA_POP(self->stack, val);
4757 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004758}
Tim Peters84e87f32001-03-17 04:50:51 +00004759
Guido van Rossum60456fd1997-04-09 17:36:32 +00004760
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004761/* No-load functions to support noload, which is used to
4762 find persistent references. */
4763
4764static int
Tim Peterscba30e22003-02-01 06:24:36 +00004765noload_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004766{
4767 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004768
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004769 if ((i = marker(self)) < 0) return -1;
4770 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004771}
4772
4773
4774static int
Tim Peterscba30e22003-02-01 06:24:36 +00004775noload_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004776{
4777 int i;
4778 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004779
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004780 if ((i = marker(self)) < 0) return -1;
4781 Pdata_clear(self->stack, i);
Tim Peters0bc93f52003-02-02 18:29:33 +00004782 if (self->readline_func(self, &s) < 0) return -1;
4783 if (self->readline_func(self, &s) < 0) return -1;
Tim Peterseab7db32003-02-13 18:24:14 +00004784 PDATA_APPEND(self->stack, Py_None, -1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004785 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004786}
4787
4788static int
Tim Peterseab7db32003-02-13 18:24:14 +00004789noload_newobj(Unpicklerobject *self)
4790{
4791 PyObject *obj;
4792
4793 PDATA_POP(self->stack, obj); /* pop argtuple */
4794 if (obj == NULL) return -1;
4795 Py_DECREF(obj);
4796
4797 PDATA_POP(self->stack, obj); /* pop cls */
4798 if (obj == NULL) return -1;
4799 Py_DECREF(obj);
4800
4801 PDATA_APPEND(self->stack, Py_None, -1);
4802 return 0;
4803}
4804
4805static int
Tim Peterscba30e22003-02-01 06:24:36 +00004806noload_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004807{
4808 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004809
Tim Peters0bc93f52003-02-02 18:29:33 +00004810 if (self->readline_func(self, &s) < 0) return -1;
4811 if (self->readline_func(self, &s) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004812 PDATA_APPEND(self->stack, Py_None,-1);
4813 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004814}
4815
4816static int
Tim Peterscba30e22003-02-01 06:24:36 +00004817noload_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004818{
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004819
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004820 if (self->stack->length < 2) return stackUnderflow();
4821 Pdata_clear(self->stack, self->stack->length-2);
4822 PDATA_APPEND(self->stack, Py_None,-1);
4823 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004824}
4825
4826static int
4827noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004828
Guido van Rossum053b8df1998-11-25 16:18:00 +00004829 if (self->stack->length < 1) return stackUnderflow();
4830 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00004831 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004832}
4833
Tim Peters2d629652003-02-04 05:06:17 +00004834static int
4835noload_extension(Unpicklerobject *self, int nbytes)
4836{
4837 char *codebytes;
4838
4839 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4840 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4841 PDATA_APPEND(self->stack, Py_None, -1);
4842 return 0;
4843}
4844
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004845
4846static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004847noload(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004848{
4849 PyObject *err = 0, *val = 0;
4850 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004851
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004852 self->num_marks = 0;
4853 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004854
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004855 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004856 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004857 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004858
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004859 switch (s[0]) {
4860 case NONE:
4861 if (load_none(self) < 0)
4862 break;
4863 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004864
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004865 case BININT:
4866 if (load_binint(self) < 0)
4867 break;
4868 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004869
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004870 case BININT1:
4871 if (load_binint1(self) < 0)
4872 break;
4873 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004874
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004875 case BININT2:
4876 if (load_binint2(self) < 0)
4877 break;
4878 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004879
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004880 case INT:
4881 if (load_int(self) < 0)
4882 break;
4883 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004884
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004885 case LONG:
4886 if (load_long(self) < 0)
4887 break;
4888 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004889
Tim Peters4190fb82003-02-02 16:09:05 +00004890 case LONG1:
4891 if (load_counted_long(self, 1) < 0)
4892 break;
4893 continue;
4894
4895 case LONG4:
4896 if (load_counted_long(self, 4) < 0)
4897 break;
4898 continue;
4899
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004900 case FLOAT:
4901 if (load_float(self) < 0)
4902 break;
4903 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004904
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004905 case BINFLOAT:
4906 if (load_binfloat(self) < 0)
4907 break;
4908 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004909
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004910 case BINSTRING:
4911 if (load_binstring(self) < 0)
4912 break;
4913 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004914
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004915 case SHORT_BINSTRING:
4916 if (load_short_binstring(self) < 0)
4917 break;
4918 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004919
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004920 case STRING:
4921 if (load_string(self) < 0)
4922 break;
4923 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004924
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004925#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004926 case UNICODE:
4927 if (load_unicode(self) < 0)
4928 break;
4929 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004930
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004931 case BINUNICODE:
4932 if (load_binunicode(self) < 0)
4933 break;
4934 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004935#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004936
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004937 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004938 if (load_counted_tuple(self, 0) < 0)
4939 break;
4940 continue;
4941
4942 case TUPLE1:
4943 if (load_counted_tuple(self, 1) < 0)
4944 break;
4945 continue;
4946
4947 case TUPLE2:
4948 if (load_counted_tuple(self, 2) < 0)
4949 break;
4950 continue;
4951
4952 case TUPLE3:
4953 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004954 break;
4955 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004956
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004957 case TUPLE:
4958 if (load_tuple(self) < 0)
4959 break;
4960 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004961
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004962 case EMPTY_LIST:
4963 if (load_empty_list(self) < 0)
4964 break;
4965 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004966
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004967 case LIST:
4968 if (load_list(self) < 0)
4969 break;
4970 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004971
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004972 case EMPTY_DICT:
4973 if (load_empty_dict(self) < 0)
4974 break;
4975 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004976
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004977 case DICT:
4978 if (load_dict(self) < 0)
4979 break;
4980 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004981
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004982 case OBJ:
4983 if (noload_obj(self) < 0)
4984 break;
4985 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004986
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004987 case INST:
4988 if (noload_inst(self) < 0)
4989 break;
4990 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004991
Tim Peterseab7db32003-02-13 18:24:14 +00004992 case NEWOBJ:
4993 if (noload_newobj(self) < 0)
4994 break;
4995 continue;
4996
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004997 case GLOBAL:
4998 if (noload_global(self) < 0)
4999 break;
5000 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005001
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005002 case APPEND:
5003 if (load_append(self) < 0)
5004 break;
5005 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005006
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005007 case APPENDS:
5008 if (load_appends(self) < 0)
5009 break;
5010 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005011
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005012 case BUILD:
5013 if (noload_build(self) < 0)
5014 break;
5015 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005016
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005017 case DUP:
5018 if (load_dup(self) < 0)
5019 break;
5020 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005021
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005022 case BINGET:
5023 if (load_binget(self) < 0)
5024 break;
5025 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005026
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005027 case LONG_BINGET:
5028 if (load_long_binget(self) < 0)
5029 break;
5030 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005031
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005032 case GET:
5033 if (load_get(self) < 0)
5034 break;
5035 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005036
Tim Peters2d629652003-02-04 05:06:17 +00005037 case EXT1:
5038 if (noload_extension(self, 1) < 0)
5039 break;
5040 continue;
5041
5042 case EXT2:
5043 if (noload_extension(self, 2) < 0)
5044 break;
5045 continue;
5046
5047 case EXT4:
5048 if (noload_extension(self, 4) < 0)
5049 break;
5050 continue;
5051
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005052 case MARK:
5053 if (load_mark(self) < 0)
5054 break;
5055 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005056
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005057 case BINPUT:
5058 if (load_binput(self) < 0)
5059 break;
5060 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005061
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005062 case LONG_BINPUT:
5063 if (load_long_binput(self) < 0)
5064 break;
5065 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005066
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005067 case PUT:
5068 if (load_put(self) < 0)
5069 break;
5070 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005071
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005072 case POP:
5073 if (load_pop(self) < 0)
5074 break;
5075 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005076
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005077 case POP_MARK:
5078 if (load_pop_mark(self) < 0)
5079 break;
5080 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005081
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005082 case SETITEM:
5083 if (load_setitem(self) < 0)
5084 break;
5085 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005086
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005087 case SETITEMS:
5088 if (load_setitems(self) < 0)
5089 break;
5090 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005091
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005092 case STOP:
5093 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005094
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005095 case PERSID:
5096 if (load_persid(self) < 0)
5097 break;
5098 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005099
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005100 case BINPERSID:
5101 if (load_binpersid(self) < 0)
5102 break;
5103 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005104
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005105 case REDUCE:
5106 if (noload_reduce(self) < 0)
5107 break;
5108 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005109
Tim Peters4190fb82003-02-02 16:09:05 +00005110 case PROTO:
5111 if (load_proto(self) < 0)
5112 break;
5113 continue;
5114
Tim Peters3c67d792003-02-02 17:59:11 +00005115 case NEWTRUE:
5116 if (load_bool(self, Py_True) < 0)
5117 break;
5118 continue;
5119
5120 case NEWFALSE:
5121 if (load_bool(self, Py_False) < 0)
5122 break;
5123 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005124 default:
Tim Peterscba30e22003-02-01 06:24:36 +00005125 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005126 "invalid load key, '%s'.",
5127 "c", s[0]);
5128 return NULL;
5129 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005130
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005131 break;
5132 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005133
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005134 if ((err = PyErr_Occurred())) {
5135 if (err == PyExc_EOFError) {
5136 PyErr_SetNone(PyExc_EOFError);
5137 }
5138 return NULL;
5139 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005140
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005141 PDATA_POP(self->stack, val);
5142 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005143}
Tim Peters84e87f32001-03-17 04:50:51 +00005144
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005145
Guido van Rossum60456fd1997-04-09 17:36:32 +00005146static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005147Unpickler_load(Unpicklerobject *self, PyObject *unused)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005148{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005149 return load(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005150}
5151
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005152static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005153Unpickler_noload(Unpicklerobject *self, PyObject *unused)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005154{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005155 return noload(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005156}
5157
Guido van Rossum60456fd1997-04-09 17:36:32 +00005158
5159static struct PyMethodDef Unpickler_methods[] = {
Georg Brandl96a8c392006-05-29 21:04:52 +00005160 {"load", (PyCFunction)Unpickler_load, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005161 PyDoc_STR("load() -- Load a pickle")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005162 },
Georg Brandl96a8c392006-05-29 21:04:52 +00005163 {"noload", (PyCFunction)Unpickler_noload, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005164 PyDoc_STR(
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005165 "noload() -- not load a pickle, but go through most of the motions\n"
5166 "\n"
5167 "This function can be used to read past a pickle without instantiating\n"
5168 "any objects or importing any modules. It can also be used to find all\n"
5169 "persistent references without instantiating any objects or importing\n"
Neal Norwitz200788c2002-08-13 22:20:41 +00005170 "any modules.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005171 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00005172 {NULL, NULL} /* sentinel */
5173};
5174
5175
5176static Unpicklerobject *
Tim Peterscba30e22003-02-01 06:24:36 +00005177newUnpicklerobject(PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005178{
5179 Unpicklerobject *self;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005180
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005181 if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005182 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005183
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005184 self->file = NULL;
5185 self->arg = NULL;
5186 self->stack = (Pdata*)Pdata_New();
5187 self->pers_func = NULL;
5188 self->last_string = NULL;
5189 self->marks = NULL;
5190 self->num_marks = 0;
5191 self->marks_size = 0;
5192 self->buf_size = 0;
5193 self->read = NULL;
5194 self->readline = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005195 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005196
Tim Peterscba30e22003-02-01 06:24:36 +00005197 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005198 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005199
Neal Norwitzb59d08c2006-07-22 16:20:49 +00005200 if (!self->stack)
5201 goto err;
5202
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005203 Py_INCREF(f);
5204 self->file = f;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005205
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005206 /* Set read, readline based on type of f */
5207 if (PyFile_Check(f)) {
5208 self->fp = PyFile_AsFile(f);
5209 if (self->fp == NULL) {
Tim Peterscba30e22003-02-01 06:24:36 +00005210 PyErr_SetString(PyExc_ValueError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005211 "I/O operation on closed file");
5212 goto err;
5213 }
5214 self->read_func = read_file;
5215 self->readline_func = readline_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00005216 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005217 else if (PycStringIO_InputCheck(f)) {
5218 self->fp = NULL;
5219 self->read_func = read_cStringIO;
5220 self->readline_func = readline_cStringIO;
5221 }
5222 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00005223
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005224 self->fp = NULL;
5225 self->read_func = read_other;
5226 self->readline_func = readline_other;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005227
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005228 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5229 (self->read = PyObject_GetAttr(f, read_str)))) {
5230 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00005231 PyErr_SetString( PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005232 "argument must have 'read' and "
5233 "'readline' attributes" );
5234 goto err;
5235 }
5236 }
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005237 PyObject_GC_Track(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005238
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005239 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005240
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005241 err:
5242 Py_DECREF((PyObject *)self);
5243 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005244}
5245
5246
5247static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005248get_Unpickler(PyObject *self, PyObject *file)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005249{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005250 return (PyObject *)newUnpicklerobject(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005251}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005252
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005253
Guido van Rossum60456fd1997-04-09 17:36:32 +00005254static void
Tim Peterscba30e22003-02-01 06:24:36 +00005255Unpickler_dealloc(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005256{
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005257 PyObject_GC_UnTrack((PyObject *)self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005258 Py_XDECREF(self->readline);
5259 Py_XDECREF(self->read);
5260 Py_XDECREF(self->file);
5261 Py_XDECREF(self->memo);
5262 Py_XDECREF(self->stack);
5263 Py_XDECREF(self->pers_func);
5264 Py_XDECREF(self->arg);
5265 Py_XDECREF(self->last_string);
Jeremy Hyltonfff093f2003-07-11 19:42:49 +00005266 Py_XDECREF(self->find_class);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005267
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005268 if (self->marks) {
5269 free(self->marks);
5270 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005271
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005272 if (self->buf_size) {
5273 free(self->buf);
5274 }
Tim Peters84e87f32001-03-17 04:50:51 +00005275
Christian Heimese93237d2007-12-19 02:37:44 +00005276 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005277}
5278
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005279static int
5280Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg)
5281{
Thomas Woutersc6e55062006-04-15 21:47:09 +00005282 Py_VISIT(self->readline);
5283 Py_VISIT(self->read);
5284 Py_VISIT(self->file);
5285 Py_VISIT(self->memo);
5286 Py_VISIT(self->stack);
5287 Py_VISIT(self->pers_func);
5288 Py_VISIT(self->arg);
5289 Py_VISIT(self->last_string);
5290 Py_VISIT(self->find_class);
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005291 return 0;
5292}
5293
5294static int
5295Unpickler_clear(Unpicklerobject *self)
5296{
Thomas Woutersedf17d82006-04-15 17:28:34 +00005297 Py_CLEAR(self->readline);
5298 Py_CLEAR(self->read);
5299 Py_CLEAR(self->file);
5300 Py_CLEAR(self->memo);
5301 Py_CLEAR(self->stack);
5302 Py_CLEAR(self->pers_func);
5303 Py_CLEAR(self->arg);
5304 Py_CLEAR(self->last_string);
5305 Py_CLEAR(self->find_class);
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005306 return 0;
5307}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005308
5309static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005310Unpickler_getattr(Unpicklerobject *self, char *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005311{
5312 if (!strcmp(name, "persistent_load")) {
5313 if (!self->pers_func) {
5314 PyErr_SetString(PyExc_AttributeError, name);
5315 return NULL;
5316 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005317
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005318 Py_INCREF(self->pers_func);
5319 return self->pers_func;
5320 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005321
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005322 if (!strcmp(name, "find_global")) {
5323 if (!self->find_class) {
5324 PyErr_SetString(PyExc_AttributeError, name);
5325 return NULL;
5326 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005327
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005328 Py_INCREF(self->find_class);
5329 return self->find_class;
5330 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005331
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005332 if (!strcmp(name, "memo")) {
5333 if (!self->memo) {
5334 PyErr_SetString(PyExc_AttributeError, name);
5335 return NULL;
5336 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005337
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005338 Py_INCREF(self->memo);
5339 return self->memo;
5340 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005341
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005342 if (!strcmp(name, "UnpicklingError")) {
5343 Py_INCREF(UnpicklingError);
5344 return UnpicklingError;
5345 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005346
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005347 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005348}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005349
Guido van Rossum60456fd1997-04-09 17:36:32 +00005350
5351static int
Tim Peterscba30e22003-02-01 06:24:36 +00005352Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005353{
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005354
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005355 if (!strcmp(name, "persistent_load")) {
5356 Py_XDECREF(self->pers_func);
5357 self->pers_func = value;
5358 Py_XINCREF(value);
5359 return 0;
5360 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005361
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005362 if (!strcmp(name, "find_global")) {
5363 Py_XDECREF(self->find_class);
5364 self->find_class = value;
5365 Py_XINCREF(value);
5366 return 0;
5367 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005368
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005369 if (! value) {
5370 PyErr_SetString(PyExc_TypeError,
5371 "attribute deletion is not supported");
5372 return -1;
5373 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005374
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005375 if (strcmp(name, "memo") == 0) {
5376 if (!PyDict_Check(value)) {
Tim Peterscba30e22003-02-01 06:24:36 +00005377 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005378 "memo must be a dictionary");
5379 return -1;
5380 }
5381 Py_XDECREF(self->memo);
5382 self->memo = value;
5383 Py_INCREF(value);
5384 return 0;
5385 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005386
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005387 PyErr_SetString(PyExc_AttributeError, name);
5388 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005389}
5390
Tim Peters5bd2a792003-02-01 16:45:06 +00005391/* ---------------------------------------------------------------------------
5392 * Module-level functions.
5393 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005394
Martin v. Löwis544f1192004-07-27 05:22:33 +00005395/* dump(obj, file, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005396static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005397cpm_dump(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", "file", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005400 PyObject *ob, *file, *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, "OO|i", kwlist,
5405 &ob, &file, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005406 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005407
Tim Peters5bd2a792003-02-01 16:45:06 +00005408 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005409 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005410
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005411 if (dump(pickler, ob) < 0)
5412 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005413
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005414 Py_INCREF(Py_None);
5415 res = Py_None;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005416
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005417 finally:
5418 Py_XDECREF(pickler);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005419
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005420 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005421}
5422
5423
Martin v. Löwis544f1192004-07-27 05:22:33 +00005424/* dumps(obj, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005425static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005426cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005427{
Martin v. Löwis15e62742006-02-27 16:46:16 +00005428 static char *kwlist[] = {"obj", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005429 PyObject *ob, *file = 0, *res = NULL;
5430 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005431 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005432
Martin v. Löwis544f1192004-07-27 05:22:33 +00005433 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist,
5434 &ob, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005435 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005436
Tim Peterscba30e22003-02-01 06:24:36 +00005437 if (!( file = PycStringIO->NewOutput(128)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005438 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005439
Tim Peters5bd2a792003-02-01 16:45:06 +00005440 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005441 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005442
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005443 if (dump(pickler, ob) < 0)
5444 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005445
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005446 res = PycStringIO->cgetvalue(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005447
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005448 finally:
5449 Py_XDECREF(pickler);
5450 Py_XDECREF(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005451
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005452 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00005453}
5454
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005455
Tim Peters5bd2a792003-02-01 16:45:06 +00005456/* load(fileobj). */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005457static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005458cpm_load(PyObject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005459{
5460 Unpicklerobject *unpickler = 0;
Georg Brandl96a8c392006-05-29 21:04:52 +00005461 PyObject *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005462
Tim Peterscba30e22003-02-01 06:24:36 +00005463 if (!( unpickler = newUnpicklerobject(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005464 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005465
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005466 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005467
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005468 finally:
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
Tim Peters5bd2a792003-02-01 16:45:06 +00005475/* loads(string) */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005476static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005477cpm_loads(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005478{
5479 PyObject *ob, *file = 0, *res = NULL;
5480 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005481
Tim Peterscba30e22003-02-01 06:24:36 +00005482 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005483 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005484
Tim Peterscba30e22003-02-01 06:24:36 +00005485 if (!( file = PycStringIO->NewInput(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005486 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00005487
Tim Peterscba30e22003-02-01 06:24:36 +00005488 if (!( unpickler = newUnpicklerobject(file)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005489 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005490
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005491 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005492
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005493 finally:
5494 Py_XDECREF(file);
5495 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005496
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005497 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005498}
5499
5500
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005501PyDoc_STRVAR(Unpicklertype__doc__,
5502"Objects that know how to unpickle");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005503
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005504static PyTypeObject Unpicklertype = {
Martin v. Löwis68192102007-07-21 06:55:02 +00005505 PyVarObject_HEAD_INIT(NULL, 0)
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005506 "cPickle.Unpickler", /*tp_name*/
5507 sizeof(Unpicklerobject), /*tp_basicsize*/
5508 0,
5509 (destructor)Unpickler_dealloc, /* tp_dealloc */
5510 0, /* tp_print */
5511 (getattrfunc)Unpickler_getattr, /* tp_getattr */
5512 (setattrfunc)Unpickler_setattr, /* tp_setattr */
5513 0, /* tp_compare */
5514 0, /* tp_repr */
5515 0, /* tp_as_number */
5516 0, /* tp_as_sequence */
5517 0, /* tp_as_mapping */
5518 0, /* tp_hash */
5519 0, /* tp_call */
5520 0, /* tp_str */
5521 0, /* tp_getattro */
5522 0, /* tp_setattro */
5523 0, /* tp_as_buffer */
5524 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
5525 Unpicklertype__doc__, /* tp_doc */
5526 (traverseproc)Unpickler_traverse, /* tp_traverse */
5527 (inquiry)Unpickler_clear, /* tp_clear */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005528};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005529
Guido van Rossum60456fd1997-04-09 17:36:32 +00005530static struct PyMethodDef cPickle_methods[] = {
Martin v. Löwis544f1192004-07-27 05:22:33 +00005531 {"dump", (PyCFunction)cpm_dump, METH_VARARGS | METH_KEYWORDS,
5532 PyDoc_STR("dump(obj, file, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005533 "Write an object in pickle format to the given file.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005534 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005535 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005536 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005537
Martin v. Löwis544f1192004-07-27 05:22:33 +00005538 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS | METH_KEYWORDS,
5539 PyDoc_STR("dumps(obj, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005540 "Return a string containing an object in pickle format.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005541 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005542 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005543 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005544
Georg Brandl96a8c392006-05-29 21:04:52 +00005545 {"load", (PyCFunction)cpm_load, METH_O,
Neal Norwitz200788c2002-08-13 22:20:41 +00005546 PyDoc_STR("load(file) -- Load a pickle from the given file")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005547
Neal Norwitzb0493252002-03-31 14:44:22 +00005548 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005549 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005550
Martin v. Löwis544f1192004-07-27 05:22:33 +00005551 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS | METH_KEYWORDS,
5552 PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005553 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005554 "This takes a file-like object for writing a pickle data stream.\n"
5555 "The optional proto argument tells the pickler to use the given\n"
5556 "protocol; supported protocols are 0, 1, 2. The default\n"
5557 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5558 "only protocol that can be written to a file opened in text\n"
5559 "mode and read back successfully. When using a protocol higher\n"
5560 "than 0, make sure the file is opened in binary mode, both when\n"
5561 "pickling and unpickling.)\n"
5562 "\n"
5563 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5564 "more efficient than protocol 1.\n"
5565 "\n"
5566 "Specifying a negative protocol version selects the highest\n"
5567 "protocol version supported. The higher the protocol used, the\n"
5568 "more recent the version of Python needed to read the pickle\n"
5569 "produced.\n"
5570 "\n"
5571 "The file parameter must have a write() method that accepts a single\n"
5572 "string argument. It can thus be an open file object, a StringIO\n"
5573 "object, or any other custom object that meets this interface.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005574 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005575
Georg Brandl96a8c392006-05-29 21:04:52 +00005576 {"Unpickler", (PyCFunction)get_Unpickler, METH_O,
Tim Peters5bd2a792003-02-01 16:45:06 +00005577 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5578
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005579 { NULL, NULL }
5580};
5581
Guido van Rossum60456fd1997-04-09 17:36:32 +00005582static int
Tim Peterscba30e22003-02-01 06:24:36 +00005583init_stuff(PyObject *module_dict)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005584{
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005585 PyObject *copyreg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005586
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005587#define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005588
Tim Peters3cfe7542003-05-21 21:29:48 +00005589 if (PyType_Ready(&Unpicklertype) < 0)
5590 return -1;
5591 if (PyType_Ready(&Picklertype) < 0)
5592 return -1;
5593
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005594 INIT_STR(__class__);
5595 INIT_STR(__getinitargs__);
5596 INIT_STR(__dict__);
5597 INIT_STR(__getstate__);
5598 INIT_STR(__setstate__);
5599 INIT_STR(__name__);
5600 INIT_STR(__main__);
5601 INIT_STR(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00005602 INIT_STR(__reduce_ex__);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005603 INIT_STR(write);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005604 INIT_STR(append);
5605 INIT_STR(read);
5606 INIT_STR(readline);
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005607 INIT_STR(copyreg);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005608 INIT_STR(dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005609
Georg Brandldffbf5f2008-05-20 07:49:57 +00005610 if (!( copyreg = PyImport_ImportModule("copy_reg")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005611 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005612
Tim Peters1f1b2d22003-02-01 02:16:37 +00005613 /* This is special because we want to use a different
5614 one in restricted mode. */
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005615 dispatch_table = PyObject_GetAttr(copyreg, dispatch_table_str);
Tim Peters5b7da392003-02-04 00:21:07 +00005616 if (!dispatch_table) return -1;
5617
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005618 extension_registry = PyObject_GetAttrString(copyreg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005619 "_extension_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005620 if (!extension_registry) return -1;
5621
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005622 inverted_registry = PyObject_GetAttrString(copyreg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005623 "_inverted_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005624 if (!inverted_registry) return -1;
5625
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005626 extension_cache = PyObject_GetAttrString(copyreg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005627 "_extension_cache");
Tim Peters5b7da392003-02-04 00:21:07 +00005628 if (!extension_cache) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005629
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005630 Py_DECREF(copyreg);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005631
Tim Peters731098b2003-02-04 20:56:09 +00005632 if (!(empty_tuple = PyTuple_New(0)))
5633 return -1;
5634
5635 two_tuple = PyTuple_New(2);
5636 if (two_tuple == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005637 return -1;
Tim Peters3e667d52003-02-04 21:47:44 +00005638 /* We use this temp container with no regard to refcounts, or to
5639 * keeping containees alive. Exempt from GC, because we don't
5640 * want anything looking at two_tuple() by magic.
5641 */
5642 PyObject_GC_UnTrack(two_tuple);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005643
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005644 /* Ugh */
5645 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5646 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5647 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005648
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005649 if (!( t=PyDict_New())) return -1;
5650 if (!( r=PyRun_String(
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005651 "def __str__(self):\n"
5652 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5653 Py_file_input,
5654 module_dict, t) )) return -1;
5655 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005656
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005657 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
Tim Peterscba30e22003-02-01 06:24:36 +00005658 if (!PickleError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005659 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005660
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005661 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005662
Tim Peterscba30e22003-02-01 06:24:36 +00005663 PicklingError = PyErr_NewException("cPickle.PicklingError",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005664 PickleError, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00005665 if (!PicklingError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005666 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005667
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005668 if (!( t=PyDict_New())) return -1;
5669 if (!( r=PyRun_String(
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005670 "def __str__(self):\n"
5671 " a=self.args\n"
5672 " a=a and type(a[0]) or '(what)'\n"
5673 " return 'Cannot pickle %s objects' % a\n"
5674 , Py_file_input,
5675 module_dict, t) )) return -1;
5676 Py_DECREF(r);
Tim Peters84e87f32001-03-17 04:50:51 +00005677
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005678 if (!( UnpickleableError = PyErr_NewException(
Tim Peterscba30e22003-02-01 06:24:36 +00005679 "cPickle.UnpickleableError", PicklingError, t)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005680 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005681
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005682 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005683
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005684 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
Tim Peterscba30e22003-02-01 06:24:36 +00005685 PickleError, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005686 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005687
Martin v. Löwis658009a2002-09-16 17:26:24 +00005688 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5689 UnpicklingError, NULL)))
5690 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +00005691
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005692 if (PyDict_SetItemString(module_dict, "PickleError",
5693 PickleError) < 0)
5694 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005695
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005696 if (PyDict_SetItemString(module_dict, "PicklingError",
5697 PicklingError) < 0)
5698 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005699
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005700 if (PyDict_SetItemString(module_dict, "UnpicklingError",
5701 UnpicklingError) < 0)
5702 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005703
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005704 if (PyDict_SetItemString(module_dict, "UnpickleableError",
5705 UnpickleableError) < 0)
5706 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005707
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005708 if (PyDict_SetItemString(module_dict, "BadPickleGet",
5709 BadPickleGet) < 0)
5710 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005711
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005712 PycString_IMPORT;
Guido van Rossum053b8df1998-11-25 16:18:00 +00005713
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005714 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005715}
5716
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005717#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5718#define PyMODINIT_FUNC void
Guido van Rossumf9ffb031999-02-04 14:54:04 +00005719#endif
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005720PyMODINIT_FUNC
Tim Peterscba30e22003-02-01 06:24:36 +00005721initcPickle(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005722{
5723 PyObject *m, *d, *di, *v, *k;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005724 Py_ssize_t i;
Tim Peters5b7da392003-02-04 00:21:07 +00005725 char *rev = "1.71"; /* XXX when does this change? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005726 PyObject *format_version;
5727 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005728
Alexandre Vassalotti3c4971c2008-05-16 19:14:31 +00005729 /* XXX: Should mention that the pickle module will include the C
5730 XXX: optimized implementation automatically. */
5731 if (PyErr_WarnPy3k("the cPickle module has been removed in "
5732 "Python 3.0", 2) < 0)
5733 return;
5734
Christian Heimese93237d2007-12-19 02:37:44 +00005735 Py_TYPE(&Picklertype) = &PyType_Type;
5736 Py_TYPE(&Unpicklertype) = &PyType_Type;
5737 Py_TYPE(&PdataType) = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005738
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005739 /* Initialize some pieces. We need to do this before module creation,
Tim Peters5b7da392003-02-04 00:21:07 +00005740 * so we're forced to use a temporary dictionary. :(
5741 */
5742 di = PyDict_New();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005743 if (!di) return;
5744 if (init_stuff(di) < 0) return;
Guido van Rossumebba4202000-09-07 14:35:37 +00005745
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005746 /* Create the module and add the functions */
5747 m = Py_InitModule4("cPickle", cPickle_methods,
5748 cPickle_module_documentation,
5749 (PyObject*)NULL,PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00005750 if (m == NULL)
5751 return;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005752
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005753 /* Add some symbolic constants to the module */
5754 d = PyModule_GetDict(m);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005755 v = PyString_FromString(rev);
Tim Peters5b7da392003-02-04 00:21:07 +00005756 PyDict_SetItemString(d, "__version__", v);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005757 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00005758
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005759 /* Copy data from di. Waaa. */
5760 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
5761 if (PyObject_SetItem(d, k, v) < 0) {
5762 Py_DECREF(di);
5763 return;
5764 }
5765 }
5766 Py_DECREF(di);
Guido van Rossumebba4202000-09-07 14:35:37 +00005767
Tim Peters8587b3c2003-02-13 15:44:41 +00005768 i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
5769 if (i < 0)
5770 return;
5771
Tim Peters5b7da392003-02-04 00:21:07 +00005772 /* These are purely informational; no code uses them. */
5773 /* File format version we write. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005774 format_version = PyString_FromString("2.0");
Tim Peters5b7da392003-02-04 00:21:07 +00005775 /* Format versions we can read. */
5776 compatible_formats = Py_BuildValue("[sssss]",
5777 "1.0", /* Original protocol 0 */
5778 "1.1", /* Protocol 0 + INST */
5779 "1.2", /* Original protocol 1 */
5780 "1.3", /* Protocol 1 + BINFLOAT */
Tim Peters0dd23aa2003-02-04 00:30:46 +00005781 "2.0"); /* Original protocol 2 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005782 PyDict_SetItemString(d, "format_version", format_version);
5783 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
5784 Py_XDECREF(format_version);
5785 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005786}