blob: f777286a6a7bca234cc869a13ec14601b0ba6f70 [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öwis18e16552006-02-15 17:27:45 +0000342 int (*write_func)(struct Picklerobject *, const char *, Py_ssize_t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000343 char *write_buf;
344 int buf_size;
345 PyObject *dispatch_table;
346 int fast_container; /* count nested container dumps */
347 PyObject *fast_memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000348} Picklerobject;
349
Barry Warsaw52acb492001-12-21 20:04:22 +0000350#ifndef PY_CPICKLE_FAST_LIMIT
351#define PY_CPICKLE_FAST_LIMIT 50
352#endif
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000353
Jeremy Hylton938ace62002-07-17 16:30:39 +0000354static PyTypeObject Picklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000355
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000356typedef struct Unpicklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000357 PyObject_HEAD
358 FILE *fp;
359 PyObject *file;
360 PyObject *readline;
361 PyObject *read;
362 PyObject *memo;
363 PyObject *arg;
364 Pdata *stack;
365 PyObject *mark;
366 PyObject *pers_func;
367 PyObject *last_string;
368 int *marks;
369 int num_marks;
370 int marks_size;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000371 Py_ssize_t (*read_func)(struct Unpicklerobject *, char **, Py_ssize_t);
372 Py_ssize_t (*readline_func)(struct Unpicklerobject *, char **);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000373 int buf_size;
374 char *buf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000375 PyObject *find_class;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000376} Unpicklerobject;
Tim Peters84e87f32001-03-17 04:50:51 +0000377
Jeremy Hylton938ace62002-07-17 16:30:39 +0000378static PyTypeObject Unpicklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000379
Thomas Wouters4789b3a2000-07-24 11:36:47 +0000380/* Forward decls that need the above structs */
381static int save(Picklerobject *, PyObject *, int);
382static int put2(Picklerobject *, PyObject *);
383
Guido van Rossumd385d591997-04-09 17:47:47 +0000384static
Guido van Rossum60456fd1997-04-09 17:36:32 +0000385PyObject *
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000386cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
387{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000388 va_list va;
389 PyObject *args=0, *retval=0;
390 va_start(va, format);
Tim Peterscba30e22003-02-01 06:24:36 +0000391
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000392 if (format) args = Py_VaBuildValue(format, va);
393 va_end(va);
394 if (format && ! args) return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000395 if (stringformat && !(retval=PyString_FromString(stringformat)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000396 return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000397
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000398 if (retval) {
399 if (args) {
400 PyObject *v;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000401 v=PyString_Format(retval, args);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000402 Py_DECREF(retval);
403 Py_DECREF(args);
404 if (! v) return NULL;
405 retval=v;
406 }
407 }
408 else
409 if (args) retval=args;
410 else {
411 PyErr_SetObject(ErrType,Py_None);
412 return NULL;
413 }
414 PyErr_SetObject(ErrType,retval);
415 Py_DECREF(retval);
416 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000417}
418
Tim Peters84e87f32001-03-17 04:50:51 +0000419static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000420write_file(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000421{
422 size_t nbyteswritten;
Tim Peters84e87f32001-03-17 04:50:51 +0000423
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000424 if (s == NULL) {
425 return 0;
426 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000427
Martin v. Löwis18e16552006-02-15 17:27:45 +0000428 if (n > INT_MAX) {
429 /* String too large */
430 return -1;
431 }
432
Gregory P. Smithc20adf82008-04-07 06:33:21 +0000433 PyFile_IncUseCount((PyFileObject *)self->file);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000434 Py_BEGIN_ALLOW_THREADS
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000435 nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000436 Py_END_ALLOW_THREADS
Gregory P. Smithc20adf82008-04-07 06:33:21 +0000437 PyFile_DecUseCount((PyFileObject *)self->file);
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000438 if (nbyteswritten != (size_t)n) {
439 PyErr_SetFromErrno(PyExc_IOError);
440 return -1;
441 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000442
Martin v. Löwis18e16552006-02-15 17:27:45 +0000443 return (int)n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000444}
445
Tim Peters84e87f32001-03-17 04:50:51 +0000446static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000447write_cStringIO(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000448{
449 if (s == NULL) {
450 return 0;
451 }
Tim Peterscba30e22003-02-01 06:24:36 +0000452
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000453 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
454 return -1;
455 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000456
Martin v. Löwis18e16552006-02-15 17:27:45 +0000457 return (int)n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000458}
459
Tim Peters84e87f32001-03-17 04:50:51 +0000460static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000461write_none(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000462{
463 if (s == NULL) return 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000464 if (n > INT_MAX) return -1;
465 return (int)n;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000466}
467
Tim Peters84e87f32001-03-17 04:50:51 +0000468static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000469write_other(Picklerobject *self, const char *s, Py_ssize_t _n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000470{
471 PyObject *py_str = 0, *junk = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000472 int n;
Tim Peterscba30e22003-02-01 06:24:36 +0000473
Martin v. Löwis18e16552006-02-15 17:27:45 +0000474 if (_n > INT_MAX)
475 return -1;
476 n = (int)_n;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000477 if (s == NULL) {
478 if (!( self->buf_size )) return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000479 py_str = PyString_FromStringAndSize(self->write_buf,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000480 self->buf_size);
Tim Peterscba30e22003-02-01 06:24:36 +0000481 if (!py_str)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000482 return -1;
483 }
484 else {
485 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
486 if (write_other(self, NULL, 0) < 0)
487 return -1;
488 }
Tim Peterscba30e22003-02-01 06:24:36 +0000489
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000490 if (n > WRITE_BUF_SIZE) {
491 if (!( py_str =
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000492 PyString_FromStringAndSize(s, n)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000493 return -1;
494 }
495 else {
496 memcpy(self->write_buf + self->buf_size, s, n);
497 self->buf_size += n;
498 return n;
499 }
500 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000501
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000502 if (self->write) {
503 /* object with write method */
504 ARG_TUP(self, py_str);
505 if (self->arg) {
506 junk = PyObject_Call(self->write, self->arg, NULL);
507 FREE_ARG_TUP(self);
508 }
509 if (junk) Py_DECREF(junk);
510 else return -1;
511 }
512 else
513 PDATA_PUSH(self->file, py_str, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000514
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000515 self->buf_size = 0;
516 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000517}
518
519
Martin v. Löwis18e16552006-02-15 17:27:45 +0000520static Py_ssize_t
521read_file(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000522{
523 size_t nbytesread;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000524
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000525 if (self->buf_size == 0) {
526 int size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000527
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000528 size = ((n < 32) ? 32 : n);
Tim Petersee1a53c2003-02-02 02:57:53 +0000529 if (!( self->buf = (char *)malloc(size))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000530 PyErr_NoMemory();
531 return -1;
532 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000533
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000534 self->buf_size = size;
535 }
536 else if (n > self->buf_size) {
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000537 char *newbuf = (char *)realloc(self->buf, n);
538 if (!newbuf) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000539 PyErr_NoMemory();
540 return -1;
541 }
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000542 self->buf = newbuf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000543 self->buf_size = n;
544 }
Tim Peters84e87f32001-03-17 04:50:51 +0000545
Gregory P. Smithc20adf82008-04-07 06:33:21 +0000546 PyFile_IncUseCount((PyFileObject *)self->file);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000547 Py_BEGIN_ALLOW_THREADS
548 nbytesread = fread(self->buf, sizeof(char), n, self->fp);
549 Py_END_ALLOW_THREADS
Gregory P. Smithc20adf82008-04-07 06:33:21 +0000550 PyFile_DecUseCount((PyFileObject *)self->file);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000551 if (nbytesread != (size_t)n) {
552 if (feof(self->fp)) {
553 PyErr_SetNone(PyExc_EOFError);
554 return -1;
555 }
Tim Peterscba30e22003-02-01 06:24:36 +0000556
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000557 PyErr_SetFromErrno(PyExc_IOError);
558 return -1;
559 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000560
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000561 *s = self->buf;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000562
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000563 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000564}
565
566
Martin v. Löwis18e16552006-02-15 17:27:45 +0000567static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000568readline_file(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000569{
570 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000571
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000572 if (self->buf_size == 0) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000573 if (!( self->buf = (char *)malloc(40))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000574 PyErr_NoMemory();
575 return -1;
576 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000577 self->buf_size = 40;
578 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000579
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000580 i = 0;
581 while (1) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000582 int bigger;
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000583 char *newbuf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000584 for (; i < (self->buf_size - 1); i++) {
Tim Peterscba30e22003-02-01 06:24:36 +0000585 if (feof(self->fp) ||
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000586 (self->buf[i] = getc(self->fp)) == '\n') {
587 self->buf[i + 1] = '\0';
588 *s = self->buf;
589 return i + 1;
590 }
591 }
Tim Petersee1a53c2003-02-02 02:57:53 +0000592 bigger = self->buf_size << 1;
593 if (bigger <= 0) { /* overflow */
594 PyErr_NoMemory();
595 return -1;
596 }
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000597 newbuf = (char *)realloc(self->buf, bigger);
598 if (!newbuf) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000599 PyErr_NoMemory();
600 return -1;
601 }
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000602 self->buf = newbuf;
Tim Petersee1a53c2003-02-02 02:57:53 +0000603 self->buf_size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000604 }
Tim Peters84e87f32001-03-17 04:50:51 +0000605}
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000606
607
Martin v. Löwis18e16552006-02-15 17:27:45 +0000608static Py_ssize_t
609read_cStringIO(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000610{
611 char *ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000612
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000613 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
614 PyErr_SetNone(PyExc_EOFError);
615 return -1;
616 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000617
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000618 *s = ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000619
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000620 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000621}
622
623
Martin v. Löwis18e16552006-02-15 17:27:45 +0000624static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000625readline_cStringIO(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000626{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000627 Py_ssize_t n;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000628 char *ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000629
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000630 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
631 return -1;
632 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000633
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000634 *s = ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000635
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000636 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000637}
638
639
Martin v. Löwis18e16552006-02-15 17:27:45 +0000640static Py_ssize_t
641read_other(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000642{
643 PyObject *bytes, *str=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000644
Martin v. Löwis18e16552006-02-15 17:27:45 +0000645 if (!( bytes = PyInt_FromSsize_t(n))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000646
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000647 ARG_TUP(self, bytes);
648 if (self->arg) {
649 str = PyObject_Call(self->read, self->arg, NULL);
650 FREE_ARG_TUP(self);
651 }
652 if (! str) return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000653
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000654 Py_XDECREF(self->last_string);
655 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000656
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000657 if (! (*s = PyString_AsString(str))) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000658 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000659}
660
661
Martin v. Löwis18e16552006-02-15 17:27:45 +0000662static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000663readline_other(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000664{
665 PyObject *str;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000666 Py_ssize_t str_size;
Tim Peterscba30e22003-02-01 06:24:36 +0000667
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000668 if (!( str = PyObject_CallObject(self->readline, empty_tuple))) {
669 return -1;
670 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000671
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000672 if ((str_size = PyString_Size(str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000673 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000674
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000675 Py_XDECREF(self->last_string);
676 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000677
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000678 if (! (*s = PyString_AsString(str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000679 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000680
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000681 return str_size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000682}
683
Tim Petersee1a53c2003-02-02 02:57:53 +0000684/* Copy the first n bytes from s into newly malloc'ed memory, plus a
685 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
686 * The caller is responsible for free()'ing the return value.
687 */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000688static char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000689pystrndup(const char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000690{
Tim Petersee1a53c2003-02-02 02:57:53 +0000691 char *r = (char *)malloc(n+1);
692 if (r == NULL)
693 return (char*)PyErr_NoMemory();
694 memcpy(r, s, n);
695 r[n] = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000696 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000697}
698
699
700static int
Tim Peterscba30e22003-02-01 06:24:36 +0000701get(Picklerobject *self, PyObject *id)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000702{
703 PyObject *value, *mv;
704 long c_value;
705 char s[30];
706 size_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000707
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000708 if (!( mv = PyDict_GetItem(self->memo, id))) {
709 PyErr_SetObject(PyExc_KeyError, id);
710 return -1;
711 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000712
Tim Peterscba30e22003-02-01 06:24:36 +0000713 if (!( value = PyTuple_GetItem(mv, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000714 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000715
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000716 if (!( PyInt_Check(value))) {
717 PyErr_SetString(PicklingError, "no int where int expected in memo");
718 return -1;
719 }
720 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000721
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000722 if (!self->bin) {
723 s[0] = GET;
724 PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value);
725 len = strlen(s);
726 }
727 else if (Pdata_Check(self->file)) {
728 if (write_other(self, NULL, 0) < 0) return -1;
729 PDATA_APPEND(self->file, mv, -1);
730 return 0;
731 }
732 else {
733 if (c_value < 256) {
734 s[0] = BINGET;
735 s[1] = (int)(c_value & 0xff);
736 len = 2;
737 }
738 else {
739 s[0] = LONG_BINGET;
740 s[1] = (int)(c_value & 0xff);
741 s[2] = (int)((c_value >> 8) & 0xff);
742 s[3] = (int)((c_value >> 16) & 0xff);
743 s[4] = (int)((c_value >> 24) & 0xff);
744 len = 5;
745 }
746 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000747
Tim Peters0bc93f52003-02-02 18:29:33 +0000748 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000749 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000750
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000751 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000752}
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000753
Guido van Rossum60456fd1997-04-09 17:36:32 +0000754
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000755static int
Tim Peterscba30e22003-02-01 06:24:36 +0000756put(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000757{
Christian Heimese93237d2007-12-19 02:37:44 +0000758 if (Py_REFCNT(ob) < 2 || self->fast)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000759 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000760
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000761 return put2(self, ob);
762}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000763
Guido van Rossum053b8df1998-11-25 16:18:00 +0000764
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000765static int
Tim Peterscba30e22003-02-01 06:24:36 +0000766put2(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000767{
768 char c_str[30];
769 int p;
770 size_t len;
771 int res = -1;
772 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000773
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000774 if (self->fast)
775 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000776
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000777 if ((p = PyDict_Size(self->memo)) < 0)
778 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000779
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000780 /* Make sure memo keys are positive! */
Tim Peters70b02d72003-02-02 17:26:40 +0000781 /* XXX Why?
782 * XXX And does "positive" really mean non-negative?
783 * XXX pickle.py starts with PUT index 0, not 1. This makes for
784 * XXX gratuitous differences between the pickling modules.
785 */
Tim Peterscba30e22003-02-01 06:24:36 +0000786 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000787
Tim Peterscba30e22003-02-01 06:24:36 +0000788 if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000789 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000790
Tim Peterscba30e22003-02-01 06:24:36 +0000791 if (!( memo_len = PyInt_FromLong(p)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000792 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000793
Tim Peterscba30e22003-02-01 06:24:36 +0000794 if (!( t = PyTuple_New(2)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000795 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000796
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000797 PyTuple_SET_ITEM(t, 0, memo_len);
798 Py_INCREF(memo_len);
799 PyTuple_SET_ITEM(t, 1, ob);
800 Py_INCREF(ob);
801
802 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
803 goto finally;
804
805 if (!self->bin) {
806 c_str[0] = PUT;
807 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p);
808 len = strlen(c_str);
809 }
810 else if (Pdata_Check(self->file)) {
811 if (write_other(self, NULL, 0) < 0) return -1;
812 PDATA_APPEND(self->file, memo_len, -1);
813 res=0; /* Job well done ;) */
814 goto finally;
815 }
816 else {
817 if (p >= 256) {
818 c_str[0] = LONG_BINPUT;
819 c_str[1] = (int)(p & 0xff);
820 c_str[2] = (int)((p >> 8) & 0xff);
821 c_str[3] = (int)((p >> 16) & 0xff);
822 c_str[4] = (int)((p >> 24) & 0xff);
823 len = 5;
824 }
825 else {
826 c_str[0] = BINPUT;
827 c_str[1] = p;
828 len = 2;
829 }
830 }
831
Tim Peters0bc93f52003-02-02 18:29:33 +0000832 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000833 goto finally;
834
835 res = 0;
836
837 finally:
838 Py_XDECREF(py_ob_id);
839 Py_XDECREF(memo_len);
840 Py_XDECREF(t);
841
842 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000843}
844
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000845static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000846whichmodule(PyObject *global, PyObject *global_name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000847{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000848 Py_ssize_t i, j;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000849 PyObject *module = 0, *modules_dict = 0,
850 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000851
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000852 module = PyObject_GetAttrString(global, "__module__");
Jeremy Hyltonf00368f2003-06-16 20:19:49 +0000853 if (module)
854 return module;
855 if (PyErr_ExceptionMatches(PyExc_AttributeError))
856 PyErr_Clear();
857 else
858 return NULL;
Guido van Rossum45188231997-09-28 05:38:51 +0000859
Tim Peterscba30e22003-02-01 06:24:36 +0000860 if (!( modules_dict = PySys_GetObject("modules")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000861 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000862
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000863 i = 0;
864 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000865
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000866 if (PyObject_Compare(name, __main___str)==0) continue;
Tim Peters84e87f32001-03-17 04:50:51 +0000867
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000868 global_name_attr = PyObject_GetAttr(module, global_name);
869 if (!global_name_attr) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +0000870 if (PyErr_ExceptionMatches(PyExc_AttributeError))
871 PyErr_Clear();
872 else
873 return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000874 continue;
875 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000876
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000877 if (global_name_attr != global) {
878 Py_DECREF(global_name_attr);
879 continue;
880 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000881
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000882 Py_DECREF(global_name_attr);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000883
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000884 break;
885 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000886
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000887 /* The following implements the rule in pickle.py added in 1.5
888 that used __main__ if no module is found. I don't actually
889 like this rule. jlf
890 */
891 if (!j) {
892 j=1;
893 name=__main___str;
894 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000895
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000896 Py_INCREF(name);
897 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000898}
899
900
Guido van Rossum60456fd1997-04-09 17:36:32 +0000901static int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000902fast_save_enter(Picklerobject *self, PyObject *obj)
903{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000904 /* if fast_container < 0, we're doing an error exit. */
905 if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
906 PyObject *key = NULL;
907 if (self->fast_memo == NULL) {
908 self->fast_memo = PyDict_New();
909 if (self->fast_memo == NULL) {
910 self->fast_container = -1;
911 return 0;
912 }
913 }
914 key = PyLong_FromVoidPtr(obj);
915 if (key == NULL)
916 return 0;
917 if (PyDict_GetItem(self->fast_memo, key)) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000918 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000919 PyErr_Format(PyExc_ValueError,
Tim Peters92c8bb32003-02-13 23:00:26 +0000920 "fast mode: can't pickle cyclic objects "
921 "including object type %s at %p",
Christian Heimese93237d2007-12-19 02:37:44 +0000922 Py_TYPE(obj)->tp_name, obj);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000923 self->fast_container = -1;
924 return 0;
925 }
926 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000927 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000928 self->fast_container = -1;
929 return 0;
930 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000931 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000932 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000933 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000934}
935
Tim Peterscba30e22003-02-01 06:24:36 +0000936int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000937fast_save_leave(Picklerobject *self, PyObject *obj)
938{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000939 if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
940 PyObject *key = PyLong_FromVoidPtr(obj);
941 if (key == NULL)
942 return 0;
943 if (PyDict_DelItem(self->fast_memo, key) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000944 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000945 return 0;
946 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000947 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000948 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000949 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000950}
951
952static int
Tim Peterscba30e22003-02-01 06:24:36 +0000953save_none(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000954{
955 static char none = NONE;
Tim Peters0bc93f52003-02-02 18:29:33 +0000956 if (self->write_func(self, &none, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000957 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000958
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000959 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000960}
961
Guido van Rossum77f6a652002-04-03 22:41:51 +0000962static int
Tim Peterscba30e22003-02-01 06:24:36 +0000963save_bool(Picklerobject *self, PyObject *args)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000964{
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000965 static const char *buf[2] = {FALSE, TRUE};
Guido van Rossume2763392002-04-05 19:30:08 +0000966 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
Guido van Rossum77f6a652002-04-03 22:41:51 +0000967 long l = PyInt_AS_LONG((PyIntObject *)args);
968
Tim Peters3c67d792003-02-02 17:59:11 +0000969 if (self->proto >= 2) {
970 char opcode = l ? NEWTRUE : NEWFALSE;
971 if (self->write_func(self, &opcode, 1) < 0)
972 return -1;
973 }
974 else if (self->write_func(self, buf[l], len[l]) < 0)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000975 return -1;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000976 return 0;
977}
Tim Peters84e87f32001-03-17 04:50:51 +0000978
Guido van Rossum60456fd1997-04-09 17:36:32 +0000979static int
Tim Peterscba30e22003-02-01 06:24:36 +0000980save_int(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000981{
982 char c_str[32];
983 long l = PyInt_AS_LONG((PyIntObject *)args);
984 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000985
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000986 if (!self->bin
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000987#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000988 || l > 0x7fffffffL
989 || l < -0x80000000L
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000990#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000991 ) {
992 /* Text-mode pickle, or long too big to fit in the 4-byte
993 * signed BININT format: store as a string.
994 */
995 c_str[0] = INT;
996 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
Tim Peters0bc93f52003-02-02 18:29:33 +0000997 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000998 return -1;
999 }
1000 else {
1001 /* Binary pickle and l fits in a signed 4-byte int. */
1002 c_str[1] = (int)( l & 0xff);
1003 c_str[2] = (int)((l >> 8) & 0xff);
1004 c_str[3] = (int)((l >> 16) & 0xff);
1005 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001006
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001007 if ((c_str[4] == 0) && (c_str[3] == 0)) {
1008 if (c_str[2] == 0) {
1009 c_str[0] = BININT1;
1010 len = 2;
1011 }
1012 else {
1013 c_str[0] = BININT2;
1014 len = 3;
1015 }
1016 }
1017 else {
1018 c_str[0] = BININT;
1019 len = 5;
1020 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001021
Tim Peters0bc93f52003-02-02 18:29:33 +00001022 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001023 return -1;
1024 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001025
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001026 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001027}
1028
1029
1030static int
Tim Peterscba30e22003-02-01 06:24:36 +00001031save_long(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001032{
Armin Rigo7ccbca92006-10-04 12:17:45 +00001033 Py_ssize_t size;
Tim Petersee1a53c2003-02-02 02:57:53 +00001034 int res = -1;
1035 PyObject *repr = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001036
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001037 static char l = LONG;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001038
Tim Petersee1a53c2003-02-02 02:57:53 +00001039 if (self->proto >= 2) {
1040 /* Linear-time pickling. */
1041 size_t nbits;
1042 size_t nbytes;
1043 unsigned char *pdata;
1044 char c_str[5];
1045 int i;
1046 int sign = _PyLong_Sign(args);
1047
1048 if (sign == 0) {
1049 /* It's 0 -- an empty bytestring. */
1050 c_str[0] = LONG1;
1051 c_str[1] = 0;
1052 i = self->write_func(self, c_str, 2);
1053 if (i < 0) goto finally;
1054 res = 0;
1055 goto finally;
1056 }
1057 nbits = _PyLong_NumBits(args);
1058 if (nbits == (size_t)-1 && PyErr_Occurred())
1059 goto finally;
1060 /* How many bytes do we need? There are nbits >> 3 full
1061 * bytes of data, and nbits & 7 leftover bits. If there
1062 * are any leftover bits, then we clearly need another
1063 * byte. Wnat's not so obvious is that we *probably*
1064 * need another byte even if there aren't any leftovers:
1065 * the most-significant bit of the most-significant byte
1066 * acts like a sign bit, and it's usually got a sense
1067 * opposite of the one we need. The exception is longs
1068 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1069 * its own 256's-complement, so has the right sign bit
1070 * even without the extra byte. That's a pain to check
1071 * for in advance, though, so we always grab an extra
1072 * byte at the start, and cut it back later if possible.
1073 */
1074 nbytes = (nbits >> 3) + 1;
Armin Rigo7ccbca92006-10-04 12:17:45 +00001075 if (nbytes > INT_MAX) {
Tim Petersee1a53c2003-02-02 02:57:53 +00001076 PyErr_SetString(PyExc_OverflowError, "long too large "
1077 "to pickle");
1078 goto finally;
1079 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001080 repr = PyString_FromStringAndSize(NULL, (int)nbytes);
Tim Petersee1a53c2003-02-02 02:57:53 +00001081 if (repr == NULL) goto finally;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001082 pdata = (unsigned char *)PyString_AS_STRING(repr);
Tim Petersee1a53c2003-02-02 02:57:53 +00001083 i = _PyLong_AsByteArray((PyLongObject *)args,
1084 pdata, nbytes,
1085 1 /* little endian */, 1 /* signed */);
1086 if (i < 0) goto finally;
1087 /* If the long is negative, this may be a byte more than
1088 * needed. This is so iff the MSB is all redundant sign
1089 * bits.
1090 */
1091 if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
1092 (pdata[nbytes - 2] & 0x80) != 0)
1093 --nbytes;
1094
1095 if (nbytes < 256) {
1096 c_str[0] = LONG1;
1097 c_str[1] = (char)nbytes;
1098 size = 2;
1099 }
1100 else {
1101 c_str[0] = LONG4;
1102 size = (int)nbytes;
1103 for (i = 1; i < 5; i++) {
1104 c_str[i] = (char)(size & 0xff);
1105 size >>= 8;
1106 }
1107 size = 5;
1108 }
1109 i = self->write_func(self, c_str, size);
1110 if (i < 0) goto finally;
1111 i = self->write_func(self, (char *)pdata, (int)nbytes);
1112 if (i < 0) goto finally;
1113 res = 0;
1114 goto finally;
1115 }
1116
1117 /* proto < 2: write the repr and newline. This is quadratic-time
1118 * (in the number of digits), in both directions.
1119 */
Tim Peterscba30e22003-02-01 06:24:36 +00001120 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001121 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001122
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001123 if ((size = PyString_Size(repr)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001124 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001125
Tim Peters0bc93f52003-02-02 18:29:33 +00001126 if (self->write_func(self, &l, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001127 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001128
Tim Peters0bc93f52003-02-02 18:29:33 +00001129 if (self->write_func(self,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001130 PyString_AS_STRING((PyStringObject *)repr),
Tim Peters0bc93f52003-02-02 18:29:33 +00001131 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001132 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001133
Tim Peters0bc93f52003-02-02 18:29:33 +00001134 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001135 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001136
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001137 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001138
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001139 finally:
1140 Py_XDECREF(repr);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001141 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001142}
1143
1144
1145static int
Tim Peterscba30e22003-02-01 06:24:36 +00001146save_float(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001147{
1148 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001149
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001150 if (self->bin) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001151 char str[9];
Tim Peters9905b942003-03-20 20:53:32 +00001152 str[0] = BINFLOAT;
1153 if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001154 return -1;
Tim Peters0bc93f52003-02-02 18:29:33 +00001155 if (self->write_func(self, str, 9) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001156 return -1;
1157 }
1158 else {
1159 char c_str[250];
1160 c_str[0] = FLOAT;
Georg Brandlde9b6242006-04-30 11:13:56 +00001161 PyOS_ascii_formatd(c_str + 1, sizeof(c_str) - 2, "%.17g", x);
1162 /* Extend the formatted string with a newline character */
1163 strcat(c_str, "\n");
Guido van Rossum60456fd1997-04-09 17:36:32 +00001164
Tim Peters0bc93f52003-02-02 18:29:33 +00001165 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001166 return -1;
1167 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001168
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001169 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001170}
1171
1172
1173static int
Tim Peterscba30e22003-02-01 06:24:36 +00001174save_string(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001175{
1176 int size, len;
1177 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001178
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001179 if ((size = PyString_Size(args)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001180 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001181
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001182 if (!self->bin) {
1183 char *repr_str;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001184
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001185 static char string = STRING;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001186
Tim Peterscba30e22003-02-01 06:24:36 +00001187 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001188 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001189
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001190 if ((len = PyString_Size(repr)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001191 goto err;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001192 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001193
Tim Peters0bc93f52003-02-02 18:29:33 +00001194 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001195 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001196
Tim Peters0bc93f52003-02-02 18:29:33 +00001197 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001198 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001199
Tim Peters0bc93f52003-02-02 18:29:33 +00001200 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001201 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001202
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001203 Py_XDECREF(repr);
1204 }
1205 else {
1206 int i;
1207 char c_str[5];
Guido van Rossum60456fd1997-04-09 17:36:32 +00001208
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001209 if ((size = PyString_Size(args)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001210 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001211
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001212 if (size < 256) {
1213 c_str[0] = SHORT_BINSTRING;
1214 c_str[1] = size;
1215 len = 2;
1216 }
Armin Rigo7ccbca92006-10-04 12:17:45 +00001217 else if (size <= INT_MAX) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001218 c_str[0] = BINSTRING;
1219 for (i = 1; i < 5; i++)
1220 c_str[i] = (int)(size >> ((i - 1) * 8));
1221 len = 5;
1222 }
Armin Rigo7ccbca92006-10-04 12:17:45 +00001223 else
1224 return -1; /* string too large */
Guido van Rossum60456fd1997-04-09 17:36:32 +00001225
Tim Peters0bc93f52003-02-02 18:29:33 +00001226 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001227 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001228
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001229 if (size > 128 && Pdata_Check(self->file)) {
1230 if (write_other(self, NULL, 0) < 0) return -1;
1231 PDATA_APPEND(self->file, args, -1);
1232 }
1233 else {
Tim Peters0bc93f52003-02-02 18:29:33 +00001234 if (self->write_func(self,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001235 PyString_AS_STRING(
1236 (PyStringObject *)args),
Tim Peters0bc93f52003-02-02 18:29:33 +00001237 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001238 return -1;
1239 }
1240 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001241
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001242 if (doput)
1243 if (put(self, args) < 0)
1244 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001245
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001246 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001247
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001248 err:
1249 Py_XDECREF(repr);
1250 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001251}
1252
1253
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001254#ifdef Py_USING_UNICODE
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001255/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1256 backslash and newline characters to \uXXXX escapes. */
1257static PyObject *
1258modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, int size)
1259{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001260 PyObject *repr;
1261 char *p;
1262 char *q;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001263
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001264 static const char *hexdigit = "0123456789ABCDEF";
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001265
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001266 repr = PyString_FromStringAndSize(NULL, 6 * size);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001267 if (repr == NULL)
1268 return NULL;
1269 if (size == 0)
1270 return repr;
1271
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001272 p = q = PyString_AS_STRING(repr);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001273 while (size-- > 0) {
1274 Py_UNICODE ch = *s++;
1275 /* Map 16-bit characters to '\uxxxx' */
1276 if (ch >= 256 || ch == '\\' || ch == '\n') {
1277 *p++ = '\\';
1278 *p++ = 'u';
1279 *p++ = hexdigit[(ch >> 12) & 0xf];
1280 *p++ = hexdigit[(ch >> 8) & 0xf];
1281 *p++ = hexdigit[(ch >> 4) & 0xf];
1282 *p++ = hexdigit[ch & 15];
1283 }
1284 /* Copy everything else as-is */
1285 else
1286 *p++ = (char) ch;
1287 }
1288 *p = '\0';
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001289 _PyString_Resize(&repr, p - q);
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001290 return repr;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001291}
1292
1293
Guido van Rossum60456fd1997-04-09 17:36:32 +00001294static int
Tim Peterscba30e22003-02-01 06:24:36 +00001295save_unicode(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001296{
Armin Rigo7ccbca92006-10-04 12:17:45 +00001297 Py_ssize_t size, len;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001298 PyObject *repr=0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001299
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001300 if (!PyUnicode_Check(args))
1301 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001302
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001303 if (!self->bin) {
1304 char *repr_str;
1305 static char string = UNICODE;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001306
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001307 repr = modified_EncodeRawUnicodeEscape(
1308 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
Tim Peterscba30e22003-02-01 06:24:36 +00001309 if (!repr)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001310 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001311
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001312 if ((len = PyString_Size(repr)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001313 goto err;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001314 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001315
Tim Peters0bc93f52003-02-02 18:29:33 +00001316 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001317 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001318
Tim Peters0bc93f52003-02-02 18:29:33 +00001319 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001320 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001321
Tim Peters0bc93f52003-02-02 18:29:33 +00001322 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001323 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001324
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001325 Py_XDECREF(repr);
1326 }
1327 else {
1328 int i;
1329 char c_str[5];
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001330
Tim Peterscba30e22003-02-01 06:24:36 +00001331 if (!( repr = PyUnicode_AsUTF8String(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001332 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001333
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001334 if ((size = PyString_Size(repr)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001335 goto err;
Armin Rigo7ccbca92006-10-04 12:17:45 +00001336 if (size > INT_MAX)
1337 return -1; /* string too large */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001338
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001339 c_str[0] = BINUNICODE;
1340 for (i = 1; i < 5; i++)
1341 c_str[i] = (int)(size >> ((i - 1) * 8));
1342 len = 5;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001343
Tim Peters0bc93f52003-02-02 18:29:33 +00001344 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001345 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001346
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001347 if (size > 128 && Pdata_Check(self->file)) {
1348 if (write_other(self, NULL, 0) < 0)
1349 goto err;
1350 PDATA_APPEND(self->file, repr, -1);
1351 }
1352 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001353 if (self->write_func(self, PyString_AS_STRING(repr),
Tim Peters0bc93f52003-02-02 18:29:33 +00001354 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001355 goto err;
1356 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001357
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001358 Py_DECREF(repr);
1359 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001360
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001361 if (doput)
1362 if (put(self, args) < 0)
1363 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001364
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001365 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001366
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001367 err:
1368 Py_XDECREF(repr);
1369 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001370}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001371#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001372
Tim Peters1d63c9f2003-02-02 20:29:39 +00001373/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1374static int
Tim Peters67920142003-02-05 03:46:17 +00001375store_tuple_elements(Picklerobject *self, PyObject *t, int len)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001376{
1377 int i;
1378 int res = -1; /* guilty until proved innocent */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001379
Tim Peters1d63c9f2003-02-02 20:29:39 +00001380 assert(PyTuple_Size(t) == len);
1381
1382 for (i = 0; i < len; i++) {
1383 PyObject *element = PyTuple_GET_ITEM(t, i);
1384
1385 if (element == NULL)
1386 goto finally;
1387 if (save(self, element, 0) < 0)
1388 goto finally;
1389 }
1390 res = 0;
1391
1392 finally:
1393 return res;
1394}
1395
1396/* Tuples are ubiquitous in the pickle protocols, so many techniques are
1397 * used across protocols to minimize the space needed to pickle them.
Tim Peters67920142003-02-05 03:46:17 +00001398 * Tuples are also the only builtin immutable type that can be recursive
Tim Peters1d63c9f2003-02-02 20:29:39 +00001399 * (a tuple can be reached from itself), and that requires some subtle
1400 * magic so that it works in all cases. IOW, this is a long routine.
1401 */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001402static int
Tim Peterscba30e22003-02-01 06:24:36 +00001403save_tuple(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001404{
Tim Peters1d63c9f2003-02-02 20:29:39 +00001405 PyObject *py_tuple_id = NULL;
1406 int len, i;
1407 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001408
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001409 static char tuple = TUPLE;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001410 static char pop = POP;
1411 static char pop_mark = POP_MARK;
1412 static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
Guido van Rossum60456fd1997-04-09 17:36:32 +00001413
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001414 if ((len = PyTuple_Size(args)) < 0)
1415 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001416
Tim Peters1d63c9f2003-02-02 20:29:39 +00001417 if (len == 0) {
1418 char c_str[2];
Tim Peters84e87f32001-03-17 04:50:51 +00001419
Tim Peters1d63c9f2003-02-02 20:29:39 +00001420 if (self->proto) {
1421 c_str[0] = EMPTY_TUPLE;
1422 len = 1;
1423 }
1424 else {
1425 c_str[0] = MARK;
1426 c_str[1] = TUPLE;
1427 len = 2;
1428 }
1429 if (self->write_func(self, c_str, len) >= 0)
1430 res = 0;
1431 /* Don't memoize an empty tuple. */
1432 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001433 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001434
Tim Peters1d63c9f2003-02-02 20:29:39 +00001435 /* A non-empty tuple. */
1436
1437 /* id(tuple) isn't in the memo now. If it shows up there after
1438 * saving the tuple elements, the tuple must be recursive, in
1439 * which case we'll pop everything we put on the stack, and fetch
1440 * its value from the memo.
1441 */
1442 py_tuple_id = PyLong_FromVoidPtr(args);
1443 if (py_tuple_id == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001444 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001445
Tim Peters1d63c9f2003-02-02 20:29:39 +00001446 if (len <= 3 && self->proto >= 2) {
1447 /* Use TUPLE{1,2,3} opcodes. */
Tim Peters67920142003-02-05 03:46:17 +00001448 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001449 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001450 if (PyDict_GetItem(self->memo, py_tuple_id)) {
Tim Peters1d63c9f2003-02-02 20:29:39 +00001451 /* pop the len elements */
1452 for (i = 0; i < len; ++i)
1453 if (self->write_func(self, &pop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001454 goto finally;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001455 /* fetch from memo */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001456 if (get(self, py_tuple_id) < 0)
1457 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001458 res = 0;
1459 goto finally;
1460 }
Tim Peters1d63c9f2003-02-02 20:29:39 +00001461 /* Not recursive. */
1462 if (self->write_func(self, len2opcode + len, 1) < 0)
1463 goto finally;
1464 goto memoize;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001465 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001466
Tim Peters1d63c9f2003-02-02 20:29:39 +00001467 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1468 * Generate MARK elt1 elt2 ... TUPLE
1469 */
1470 if (self->write_func(self, &MARKv, 1) < 0)
1471 goto finally;
1472
Tim Peters67920142003-02-05 03:46:17 +00001473 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001474 goto finally;
1475
1476 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1477 /* pop the stack stuff we pushed */
1478 if (self->bin) {
1479 if (self->write_func(self, &pop_mark, 1) < 0)
1480 goto finally;
1481 }
1482 else {
1483 /* Note that we pop one more than len, to remove
1484 * the MARK too.
1485 */
1486 for (i = 0; i <= len; i++)
1487 if (self->write_func(self, &pop, 1) < 0)
1488 goto finally;
1489 }
1490 /* fetch from memo */
1491 if (get(self, py_tuple_id) >= 0)
1492 res = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001493 goto finally;
1494 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001495
Tim Peters1d63c9f2003-02-02 20:29:39 +00001496 /* Not recursive. */
1497 if (self->write_func(self, &tuple, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001498 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001499
Tim Peters1d63c9f2003-02-02 20:29:39 +00001500 memoize:
1501 if (put(self, args) >= 0)
1502 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001503
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001504 finally:
1505 Py_XDECREF(py_tuple_id);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001506 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001507}
1508
Tim Peters1092d642003-02-11 21:06:20 +00001509/* iter is an iterator giving items, and we batch up chunks of
1510 * MARK item item ... item APPENDS
1511 * opcode sequences. Calling code should have arranged to first create an
1512 * empty list, or list-like object, for the APPENDS to operate on.
1513 * Returns 0 on success, <0 on error.
1514 */
1515static int
1516batch_list(Picklerobject *self, PyObject *iter)
1517{
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001518 PyObject *obj = NULL;
1519 PyObject *firstitem = NULL;
Tim Peters1092d642003-02-11 21:06:20 +00001520 int i, n;
1521
1522 static char append = APPEND;
1523 static char appends = APPENDS;
1524
1525 assert(iter != NULL);
1526
1527 if (self->proto == 0) {
1528 /* APPENDS isn't available; do one at a time. */
1529 for (;;) {
1530 obj = PyIter_Next(iter);
1531 if (obj == NULL) {
1532 if (PyErr_Occurred())
1533 return -1;
1534 break;
1535 }
1536 i = save(self, obj, 0);
1537 Py_DECREF(obj);
1538 if (i < 0)
1539 return -1;
1540 if (self->write_func(self, &append, 1) < 0)
1541 return -1;
Tim Peters1092d642003-02-11 21:06:20 +00001542 }
1543 return 0;
1544 }
1545
1546 /* proto > 0: write in batches of BATCHSIZE. */
1547 do {
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001548 /* Get first item */
1549 firstitem = PyIter_Next(iter);
1550 if (firstitem == NULL) {
1551 if (PyErr_Occurred())
1552 goto BatchFailed;
1553
1554 /* nothing more to add */
1555 break;
1556 }
1557
1558 /* Try to get a second item */
1559 obj = PyIter_Next(iter);
1560 if (obj == NULL) {
1561 if (PyErr_Occurred())
1562 goto BatchFailed;
1563
1564 /* Only one item to write */
1565 if (save(self, firstitem, 0) < 0)
1566 goto BatchFailed;
1567 if (self->write_func(self, &append, 1) < 0)
1568 goto BatchFailed;
1569 Py_CLEAR(firstitem);
1570 break;
1571 }
1572
1573 /* More than one item to write */
1574
1575 /* Pump out MARK, items, APPENDS. */
1576 if (self->write_func(self, &MARKv, 1) < 0)
1577 goto BatchFailed;
1578
1579 if (save(self, firstitem, 0) < 0)
1580 goto BatchFailed;
1581 Py_CLEAR(firstitem);
1582 n = 1;
1583
1584 /* Fetch and save up to BATCHSIZE items */
1585 while (obj) {
1586 if (save(self, obj, 0) < 0)
1587 goto BatchFailed;
1588 Py_CLEAR(obj);
1589 n += 1;
1590
1591 if (n == BATCHSIZE)
1592 break;
1593
Tim Peters1092d642003-02-11 21:06:20 +00001594 obj = PyIter_Next(iter);
1595 if (obj == NULL) {
1596 if (PyErr_Occurred())
1597 goto BatchFailed;
1598 break;
1599 }
Tim Peters1092d642003-02-11 21:06:20 +00001600 }
1601
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001602 if (self->write_func(self, &appends, 1) < 0)
1603 goto BatchFailed;
Tim Peters1092d642003-02-11 21:06:20 +00001604
Tim Peters90975f12003-02-12 05:28:58 +00001605 } while (n == BATCHSIZE);
Tim Peters1092d642003-02-11 21:06:20 +00001606 return 0;
1607
1608BatchFailed:
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001609 Py_XDECREF(firstitem);
1610 Py_XDECREF(obj);
Tim Peters1092d642003-02-11 21:06:20 +00001611 return -1;
1612}
1613
Guido van Rossum60456fd1997-04-09 17:36:32 +00001614static int
Tim Peterscba30e22003-02-01 06:24:36 +00001615save_list(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001616{
Tim Peters1092d642003-02-11 21:06:20 +00001617 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001618 char s[3];
Tim Peters1092d642003-02-11 21:06:20 +00001619 int len;
1620 PyObject *iter;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001621
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001622 if (self->fast && !fast_save_enter(self, args))
1623 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001624
Tim Peters1092d642003-02-11 21:06:20 +00001625 /* Create an empty list. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001626 if (self->bin) {
1627 s[0] = EMPTY_LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001628 len = 1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001629 }
1630 else {
1631 s[0] = MARK;
1632 s[1] = LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001633 len = 2;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001634 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001635
Tim Peters1092d642003-02-11 21:06:20 +00001636 if (self->write_func(self, s, len) < 0)
1637 goto finally;
1638
1639 /* Get list length, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001640 if ((len = PyList_Size(args)) < 0)
1641 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001642
Tim Peters1092d642003-02-11 21:06:20 +00001643 /* Memoize. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001644 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001645 if (put(self, args) >= 0)
1646 res = 0;
1647 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001648 }
Tim Peters90975f12003-02-12 05:28:58 +00001649 if (put2(self, args) < 0)
1650 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001651
Tim Peters1092d642003-02-11 21:06:20 +00001652 /* Materialize the list elements. */
1653 iter = PyObject_GetIter(args);
1654 if (iter == NULL)
1655 goto finally;
Facundo Batista763d3092008-06-30 01:10:55 +00001656
1657 if (Py_EnterRecursiveCall(" while pickling an object") == 0)
1658 {
1659 res = batch_list(self, iter);
1660 Py_LeaveRecursiveCall();
1661 }
Tim Peters1092d642003-02-11 21:06:20 +00001662 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001663
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001664 finally:
1665 if (self->fast && !fast_save_leave(self, args))
1666 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001667
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001668 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001669}
1670
1671
Tim Peters42f08ac2003-02-11 22:43:24 +00001672/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1673 * MARK key value ... key value SETITEMS
1674 * opcode sequences. Calling code should have arranged to first create an
1675 * empty dict, or dict-like object, for the SETITEMS to operate on.
1676 * Returns 0 on success, <0 on error.
1677 *
1678 * This is very much like batch_list(). The difference between saving
1679 * elements directly, and picking apart two-tuples, is so long-winded at
1680 * the C level, though, that attempts to combine these routines were too
1681 * ugly to bear.
1682 */
1683static int
1684batch_dict(Picklerobject *self, PyObject *iter)
1685{
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001686 PyObject *p = NULL;
1687 PyObject *firstitem = NULL;
Tim Peters42f08ac2003-02-11 22:43:24 +00001688 int i, n;
1689
1690 static char setitem = SETITEM;
1691 static char setitems = SETITEMS;
1692
1693 assert(iter != NULL);
1694
1695 if (self->proto == 0) {
1696 /* SETITEMS isn't available; do one at a time. */
1697 for (;;) {
1698 p = PyIter_Next(iter);
1699 if (p == NULL) {
1700 if (PyErr_Occurred())
1701 return -1;
1702 break;
1703 }
1704 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1705 PyErr_SetString(PyExc_TypeError, "dict items "
1706 "iterator must return 2-tuples");
1707 return -1;
1708 }
1709 i = save(self, PyTuple_GET_ITEM(p, 0), 0);
1710 if (i >= 0)
1711 i = save(self, PyTuple_GET_ITEM(p, 1), 0);
1712 Py_DECREF(p);
1713 if (i < 0)
1714 return -1;
1715 if (self->write_func(self, &setitem, 1) < 0)
1716 return -1;
Tim Peters42f08ac2003-02-11 22:43:24 +00001717 }
1718 return 0;
1719 }
1720
1721 /* proto > 0: write in batches of BATCHSIZE. */
1722 do {
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001723 /* Get first item */
1724 firstitem = PyIter_Next(iter);
1725 if (firstitem == NULL) {
1726 if (PyErr_Occurred())
1727 goto BatchFailed;
1728
1729 /* nothing more to add */
1730 break;
1731 }
1732 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
1733 PyErr_SetString(PyExc_TypeError, "dict items "
1734 "iterator must return 2-tuples");
1735 goto BatchFailed;
1736 }
1737
1738 /* Try to get a second item */
1739 p = PyIter_Next(iter);
1740 if (p == NULL) {
1741 if (PyErr_Occurred())
1742 goto BatchFailed;
1743
1744 /* Only one item to write */
1745 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
1746 goto BatchFailed;
1747 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
1748 goto BatchFailed;
1749 if (self->write_func(self, &setitem, 1) < 0)
1750 goto BatchFailed;
1751 Py_CLEAR(firstitem);
1752 break;
1753 }
1754
1755 /* More than one item to write */
1756
1757 /* Pump out MARK, items, SETITEMS. */
1758 if (self->write_func(self, &MARKv, 1) < 0)
1759 goto BatchFailed;
1760
1761 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
1762 goto BatchFailed;
1763 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
1764 goto BatchFailed;
1765 Py_CLEAR(firstitem);
1766 n = 1;
1767
1768 /* Fetch and save up to BATCHSIZE items */
1769 while (p) {
1770 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1771 PyErr_SetString(PyExc_TypeError, "dict items "
1772 "iterator must return 2-tuples");
1773 goto BatchFailed;
1774 }
1775 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1776 goto BatchFailed;
1777 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1778 goto BatchFailed;
1779 Py_CLEAR(p);
1780 n += 1;
1781
1782 if (n == BATCHSIZE)
1783 break;
1784
Tim Peters42f08ac2003-02-11 22:43:24 +00001785 p = PyIter_Next(iter);
1786 if (p == NULL) {
1787 if (PyErr_Occurred())
1788 goto BatchFailed;
1789 break;
1790 }
Tim Peters42f08ac2003-02-11 22:43:24 +00001791 }
1792
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001793 if (self->write_func(self, &setitems, 1) < 0)
1794 goto BatchFailed;
Tim Peters42f08ac2003-02-11 22:43:24 +00001795
Tim Peters90975f12003-02-12 05:28:58 +00001796 } while (n == BATCHSIZE);
Tim Peters42f08ac2003-02-11 22:43:24 +00001797 return 0;
1798
1799BatchFailed:
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001800 Py_XDECREF(firstitem);
1801 Py_XDECREF(p);
Tim Peters42f08ac2003-02-11 22:43:24 +00001802 return -1;
1803}
1804
Guido van Rossum60456fd1997-04-09 17:36:32 +00001805static int
Tim Peterscba30e22003-02-01 06:24:36 +00001806save_dict(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001807{
Tim Peters42f08ac2003-02-11 22:43:24 +00001808 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001809 char s[3];
Tim Peters42f08ac2003-02-11 22:43:24 +00001810 int len;
1811 PyObject *iter;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001812
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001813 if (self->fast && !fast_save_enter(self, args))
1814 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001815
Tim Peters42f08ac2003-02-11 22:43:24 +00001816 /* Create an empty dict. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001817 if (self->bin) {
1818 s[0] = EMPTY_DICT;
1819 len = 1;
1820 }
1821 else {
1822 s[0] = MARK;
1823 s[1] = DICT;
1824 len = 2;
1825 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001826
Tim Peters0bc93f52003-02-02 18:29:33 +00001827 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001828 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001829
Tim Peters42f08ac2003-02-11 22:43:24 +00001830 /* Get dict size, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001831 if ((len = PyDict_Size(args)) < 0)
1832 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001833
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001834 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001835 if (put(self, args) >= 0)
1836 res = 0;
1837 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001838 }
Tim Peters90975f12003-02-12 05:28:58 +00001839 if (put2(self, args) < 0)
1840 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001841
Tim Peters42f08ac2003-02-11 22:43:24 +00001842 /* Materialize the dict items. */
1843 iter = PyObject_CallMethod(args, "iteritems", "()");
1844 if (iter == NULL)
1845 goto finally;
Facundo Batista763d3092008-06-30 01:10:55 +00001846 if (Py_EnterRecursiveCall(" while pickling an object") == 0)
1847 {
1848 res = batch_dict(self, iter);
1849 Py_LeaveRecursiveCall();
1850 }
Tim Peters42f08ac2003-02-11 22:43:24 +00001851 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001852
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001853 finally:
1854 if (self->fast && !fast_save_leave(self, args))
1855 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001856
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001857 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001858}
1859
1860
Tim Peters84e87f32001-03-17 04:50:51 +00001861static int
Tim Peterscba30e22003-02-01 06:24:36 +00001862save_inst(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001863{
1864 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1865 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1866 char *module_str, *name_str;
1867 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001868
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001869 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001870
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001871 if (self->fast && !fast_save_enter(self, args))
1872 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001873
Tim Peters0bc93f52003-02-02 18:29:33 +00001874 if (self->write_func(self, &MARKv, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001875 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001876
Tim Peterscba30e22003-02-01 06:24:36 +00001877 if (!( class = PyObject_GetAttr(args, __class___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001878 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001879
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001880 if (self->bin) {
1881 if (save(self, class, 0) < 0)
1882 goto finally;
1883 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001884
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001885 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
1886 PyObject *element = 0;
1887 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001888
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001889 if (!( class_args =
Tim Peterscba30e22003-02-01 06:24:36 +00001890 PyObject_Call(getinitargs_func, empty_tuple, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001891 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001892
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001893 if ((len = PyObject_Size(class_args)) < 0)
1894 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001895
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001896 for (i = 0; i < len; i++) {
Tim Peterscba30e22003-02-01 06:24:36 +00001897 if (!( element = PySequence_GetItem(class_args, i)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001898 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001899
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001900 if (save(self, element, 0) < 0) {
1901 Py_DECREF(element);
1902 goto finally;
1903 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001904
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001905 Py_DECREF(element);
1906 }
1907 }
1908 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00001909 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1910 PyErr_Clear();
1911 else
1912 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001913 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001914
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001915 if (!self->bin) {
1916 if (!( name = ((PyClassObject *)class)->cl_name )) {
1917 PyErr_SetString(PicklingError, "class has no name");
1918 goto finally;
1919 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001920
Tim Peterscba30e22003-02-01 06:24:36 +00001921 if (!( module = whichmodule(class, name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001922 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001923
Tim Peters84e87f32001-03-17 04:50:51 +00001924
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001925 if ((module_size = PyString_Size(module)) < 0 ||
1926 (name_size = PyString_Size(name)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001927 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001928
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001929 module_str = PyString_AS_STRING((PyStringObject *)module);
1930 name_str = PyString_AS_STRING((PyStringObject *)name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001931
Tim Peters0bc93f52003-02-02 18:29:33 +00001932 if (self->write_func(self, &inst, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001933 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001934
Tim Peters0bc93f52003-02-02 18:29:33 +00001935 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001936 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001937
Tim Peters0bc93f52003-02-02 18:29:33 +00001938 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001939 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001940
Tim Peters0bc93f52003-02-02 18:29:33 +00001941 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001942 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001943
Tim Peters0bc93f52003-02-02 18:29:33 +00001944 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001945 goto finally;
1946 }
Tim Peters0bc93f52003-02-02 18:29:33 +00001947 else if (self->write_func(self, &obj, 1) < 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001948 goto finally;
1949 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001950
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001951 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
1952 state = PyObject_Call(getstate_func, empty_tuple, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00001953 if (!state)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001954 goto finally;
1955 }
1956 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00001957 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1958 PyErr_Clear();
1959 else
1960 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001961
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001962 if (!( state = PyObject_GetAttr(args, __dict___str))) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00001963 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1964 PyErr_Clear();
1965 else
1966 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001967 res = 0;
1968 goto finally;
1969 }
1970 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001971
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001972 if (!PyDict_Check(state)) {
1973 if (put2(self, args) < 0)
1974 goto finally;
1975 }
1976 else {
1977 if (put(self, args) < 0)
1978 goto finally;
1979 }
Tim Peters84e87f32001-03-17 04:50:51 +00001980
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001981 if (save(self, state, 0) < 0)
1982 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001983
Tim Peters0bc93f52003-02-02 18:29:33 +00001984 if (self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001985 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001986
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001987 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001988
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001989 finally:
1990 if (self->fast && !fast_save_leave(self, args))
1991 res = -1;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001992
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001993 Py_XDECREF(module);
1994 Py_XDECREF(class);
1995 Py_XDECREF(state);
1996 Py_XDECREF(getinitargs_func);
1997 Py_XDECREF(getstate_func);
1998 Py_XDECREF(class_args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001999
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002000 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002001}
2002
2003
Guido van Rossum60456fd1997-04-09 17:36:32 +00002004static int
Tim Peterscba30e22003-02-01 06:24:36 +00002005save_global(Picklerobject *self, PyObject *args, PyObject *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002006{
Jeremy Hylton9ee91f12002-07-11 22:02:33 +00002007 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002008 char *name_str, *module_str;
2009 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002010
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002011 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002012
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002013 if (name) {
2014 global_name = name;
2015 Py_INCREF(global_name);
2016 }
2017 else {
Tim Peterscba30e22003-02-01 06:24:36 +00002018 if (!( global_name = PyObject_GetAttr(args, __name___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002019 goto finally;
2020 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002021
Tim Peterscba30e22003-02-01 06:24:36 +00002022 if (!( module = whichmodule(args, global_name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002023 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002024
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002025 if ((module_size = PyString_Size(module)) < 0 ||
2026 (name_size = PyString_Size(global_name)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002027 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002028
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002029 module_str = PyString_AS_STRING((PyStringObject *)module);
2030 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002031
Guido van Rossum75bfd052002-12-24 18:10:07 +00002032 /* XXX This can be doing a relative import. Clearly it shouldn't,
2033 but I don't know how to stop it. :-( */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002034 mod = PyImport_ImportModule(module_str);
2035 if (mod == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002036 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00002037 "Can't pickle %s: import of module %s "
2038 "failed",
2039 "OS", args, module);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002040 goto finally;
2041 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00002042 klass = PyObject_GetAttrString(mod, name_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002043 if (klass == NULL) {
2044 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00002045 "Can't pickle %s: attribute lookup %s.%s "
2046 "failed",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002047 "OSS", args, module, global_name);
2048 goto finally;
2049 }
2050 if (klass != args) {
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00002051 Py_DECREF(klass);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002052 cPickle_ErrFormat(PicklingError,
Tim Peters731098b2003-02-04 20:56:09 +00002053 "Can't pickle %s: it's not the same object "
2054 "as %s.%s",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002055 "OSS", args, module, global_name);
2056 goto finally;
2057 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00002058 Py_DECREF(klass);
Guido van Rossuma92d16a2001-08-18 21:22:07 +00002059
Tim Peters731098b2003-02-04 20:56:09 +00002060 if (self->proto >= 2) {
2061 /* See whether this is in the extension registry, and if
2062 * so generate an EXT opcode.
2063 */
2064 PyObject *py_code; /* extension code as Python object */
Tim Peters3e667d52003-02-04 21:47:44 +00002065 long code; /* extension code as C value */
Tim Peters731098b2003-02-04 20:56:09 +00002066 char c_str[5];
2067 int n;
2068
2069 PyTuple_SET_ITEM(two_tuple, 0, module);
2070 PyTuple_SET_ITEM(two_tuple, 1, global_name);
2071 py_code = PyDict_GetItem(extension_registry, two_tuple);
2072 if (py_code == NULL)
2073 goto gen_global; /* not registered */
2074
2075 /* Verify py_code has the right type and value. */
2076 if (!PyInt_Check(py_code)) {
2077 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
Tim Peters6288e232003-02-05 03:53:10 +00002078 "extension code %s isn't an integer",
Tim Peters731098b2003-02-04 20:56:09 +00002079 "OO", args, py_code);
2080 goto finally;
2081 }
2082 code = PyInt_AS_LONG(py_code);
2083 if (code <= 0 || code > 0x7fffffffL) {
2084 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2085 "extension code %ld is out of range",
2086 "Ol", args, code);
2087 goto finally;
2088 }
2089
2090 /* Generate an EXT opcode. */
2091 if (code <= 0xff) {
2092 c_str[0] = EXT1;
2093 c_str[1] = (char)code;
2094 n = 2;
2095 }
2096 else if (code <= 0xffff) {
2097 c_str[0] = EXT2;
2098 c_str[1] = (char)(code & 0xff);
2099 c_str[2] = (char)((code >> 8) & 0xff);
2100 n = 3;
2101 }
2102 else {
2103 c_str[0] = EXT4;
2104 c_str[1] = (char)(code & 0xff);
2105 c_str[2] = (char)((code >> 8) & 0xff);
2106 c_str[3] = (char)((code >> 16) & 0xff);
2107 c_str[4] = (char)((code >> 24) & 0xff);
2108 n = 5;
2109 }
2110
2111 if (self->write_func(self, c_str, n) >= 0)
2112 res = 0;
2113 goto finally; /* and don't memoize */
2114 }
2115
2116 gen_global:
Tim Peters0bc93f52003-02-02 18:29:33 +00002117 if (self->write_func(self, &global, 1) < 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, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002121 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002122
Tim Peters0bc93f52003-02-02 18:29:33 +00002123 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002124 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002125
Tim Peters0bc93f52003-02-02 18:29:33 +00002126 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002127 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002128
Tim Peters0bc93f52003-02-02 18:29:33 +00002129 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002130 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002131
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002132 if (put(self, args) < 0)
2133 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002134
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002135 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002136
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002137 finally:
2138 Py_XDECREF(module);
2139 Py_XDECREF(global_name);
2140 Py_XDECREF(mod);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002141
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002142 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002143}
2144
Guido van Rossum60456fd1997-04-09 17:36:32 +00002145static int
Tim Peterscba30e22003-02-01 06:24:36 +00002146save_pers(Picklerobject *self, PyObject *args, PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002147{
2148 PyObject *pid = 0;
2149 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002150
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002151 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002152
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002153 Py_INCREF(args);
2154 ARG_TUP(self, args);
2155 if (self->arg) {
2156 pid = PyObject_Call(f, self->arg, NULL);
2157 FREE_ARG_TUP(self);
2158 }
2159 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002160
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002161 if (pid != Py_None) {
2162 if (!self->bin) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002163 if (!PyString_Check(pid)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002164 PyErr_SetString(PicklingError,
2165 "persistent id must be string");
2166 goto finally;
2167 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002168
Tim Peters0bc93f52003-02-02 18:29:33 +00002169 if (self->write_func(self, &persid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002170 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002171
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002172 if ((size = PyString_Size(pid)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002173 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002174
Tim Peters0bc93f52003-02-02 18:29:33 +00002175 if (self->write_func(self,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002176 PyString_AS_STRING(
2177 (PyStringObject *)pid),
Tim Peters0bc93f52003-02-02 18:29:33 +00002178 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002179 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002180
Tim Peters0bc93f52003-02-02 18:29:33 +00002181 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002182 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002183
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002184 res = 1;
2185 goto finally;
2186 }
2187 else if (save(self, pid, 1) >= 0) {
Tim Peters0bc93f52003-02-02 18:29:33 +00002188 if (self->write_func(self, &binpersid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002189 res = -1;
2190 else
2191 res = 1;
2192 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002193
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002194 goto finally;
2195 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002196
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002197 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002198
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002199 finally:
2200 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002201
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002202 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002203}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002204
Tim Peters71fcda52003-02-14 23:05:28 +00002205/* We're saving ob, and args is the 2-thru-5 tuple returned by the
2206 * appropriate __reduce__ method for ob.
2207 */
Tim Peters84e87f32001-03-17 04:50:51 +00002208static int
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002209save_reduce(Picklerobject *self, PyObject *args, PyObject *fn, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002210{
Tim Peters71fcda52003-02-14 23:05:28 +00002211 PyObject *callable;
2212 PyObject *argtup;
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002213 PyObject *state = NULL;
2214 PyObject *listitems = Py_None;
2215 PyObject *dictitems = Py_None;
2216 Py_ssize_t size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002217
Tim Peters71fcda52003-02-14 23:05:28 +00002218 int use_newobj = self->proto >= 2;
2219
2220 static char reduce = REDUCE;
2221 static char build = BUILD;
2222 static char newobj = NEWOBJ;
2223
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002224 size = PyTuple_Size(args);
2225 if (size < 2 || size > 5) {
2226 cPickle_ErrFormat(PicklingError, "tuple returned by "
2227 "%s must contain 2 through 5 elements",
2228 "O", fn);
2229 return -1;
2230 }
2231
Tim Peters71fcda52003-02-14 23:05:28 +00002232 if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2233 &callable,
2234 &argtup,
2235 &state,
2236 &listitems,
2237 &dictitems))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002238 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002239
Raymond Hettingera6b45cc2004-12-07 07:05:57 +00002240 if (!PyTuple_Check(argtup)) {
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002241 cPickle_ErrFormat(PicklingError, "Second element of "
2242 "tuple returned by %s must be a tuple",
2243 "O", fn);
Raymond Hettingera6b45cc2004-12-07 07:05:57 +00002244 return -1;
2245 }
2246
Tim Peters71fcda52003-02-14 23:05:28 +00002247 if (state == Py_None)
2248 state = NULL;
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002249
Tim Peters71fcda52003-02-14 23:05:28 +00002250 if (listitems == Py_None)
2251 listitems = NULL;
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002252 else if (!PyIter_Check(listitems)) {
2253 cPickle_ErrFormat(PicklingError, "Fourth element of "
2254 "tuple returned by %s must be an iterator, not %s",
2255 "Os", fn, Py_TYPE(listitems)->tp_name);
2256 return -1;
2257 }
2258
Tim Peters71fcda52003-02-14 23:05:28 +00002259 if (dictitems == Py_None)
2260 dictitems = NULL;
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002261 else if (!PyIter_Check(dictitems)) {
2262 cPickle_ErrFormat(PicklingError, "Fifth element of "
2263 "tuple returned by %s must be an iterator, not %s",
2264 "Os", fn, Py_TYPE(dictitems)->tp_name);
2265 return -1;
2266 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002267
Tim Peters71fcda52003-02-14 23:05:28 +00002268 /* Protocol 2 special case: if callable's name is __newobj__, use
2269 * NEWOBJ. This consumes a lot of code.
2270 */
2271 if (use_newobj) {
2272 PyObject *temp = PyObject_GetAttr(callable, __name___str);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002273
Tim Peters71fcda52003-02-14 23:05:28 +00002274 if (temp == NULL) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002275 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2276 PyErr_Clear();
2277 else
2278 return -1;
Tim Peters71fcda52003-02-14 23:05:28 +00002279 use_newobj = 0;
2280 }
2281 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002282 use_newobj = PyString_Check(temp) &&
2283 strcmp(PyString_AS_STRING(temp),
Tim Peters71fcda52003-02-14 23:05:28 +00002284 "__newobj__") == 0;
2285 Py_DECREF(temp);
2286 }
2287 }
2288 if (use_newobj) {
2289 PyObject *cls;
2290 PyObject *newargtup;
2291 int n, i;
2292
2293 /* Sanity checks. */
2294 n = PyTuple_Size(argtup);
2295 if (n < 1) {
2296 PyErr_SetString(PicklingError, "__newobj__ arglist "
2297 "is empty");
2298 return -1;
2299 }
2300
2301 cls = PyTuple_GET_ITEM(argtup, 0);
2302 if (! PyObject_HasAttrString(cls, "__new__")) {
2303 PyErr_SetString(PicklingError, "args[0] from "
2304 "__newobj__ args has no __new__");
2305 return -1;
2306 }
2307
2308 /* XXX How could ob be NULL? */
2309 if (ob != NULL) {
2310 PyObject *ob_dot_class;
2311
2312 ob_dot_class = PyObject_GetAttr(ob, __class___str);
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002313 if (ob_dot_class == NULL) {
2314 if (PyErr_ExceptionMatches(
2315 PyExc_AttributeError))
2316 PyErr_Clear();
2317 else
2318 return -1;
2319 }
Tim Peters71fcda52003-02-14 23:05:28 +00002320 i = ob_dot_class != cls; /* true iff a problem */
2321 Py_XDECREF(ob_dot_class);
2322 if (i) {
2323 PyErr_SetString(PicklingError, "args[0] from "
2324 "__newobj__ args has the wrong class");
2325 return -1;
2326 }
2327 }
2328
2329 /* Save the class and its __new__ arguments. */
2330 if (save(self, cls, 0) < 0)
2331 return -1;
2332
2333 newargtup = PyTuple_New(n-1); /* argtup[1:] */
2334 if (newargtup == NULL)
2335 return -1;
2336 for (i = 1; i < n; ++i) {
2337 PyObject *temp = PyTuple_GET_ITEM(argtup, i);
2338 Py_INCREF(temp);
2339 PyTuple_SET_ITEM(newargtup, i-1, temp);
2340 }
Neal Norwitz5a29dd32007-10-05 05:01:38 +00002341 i = save(self, newargtup, 0);
Tim Peters71fcda52003-02-14 23:05:28 +00002342 Py_DECREF(newargtup);
2343 if (i < 0)
2344 return -1;
2345
2346 /* Add NEWOBJ opcode. */
2347 if (self->write_func(self, &newobj, 1) < 0)
2348 return -1;
2349 }
2350 else {
2351 /* Not using NEWOBJ. */
2352 if (save(self, callable, 0) < 0 ||
2353 save(self, argtup, 0) < 0 ||
2354 self->write_func(self, &reduce, 1) < 0)
2355 return -1;
2356 }
2357
2358 /* Memoize. */
2359 /* XXX How can ob be NULL? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002360 if (ob != NULL) {
2361 if (state && !PyDict_Check(state)) {
2362 if (put2(self, ob) < 0)
2363 return -1;
2364 }
Tim Peters71fcda52003-02-14 23:05:28 +00002365 else if (put(self, ob) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002366 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002367 }
Tim Peters84e87f32001-03-17 04:50:51 +00002368
Guido van Rossum60456fd1997-04-09 17:36:32 +00002369
Tim Peters71fcda52003-02-14 23:05:28 +00002370 if (listitems && batch_list(self, listitems) < 0)
2371 return -1;
2372
2373 if (dictitems && batch_dict(self, dictitems) < 0)
2374 return -1;
2375
2376 if (state) {
2377 if (save(self, state, 0) < 0 ||
2378 self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002379 return -1;
2380 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002381
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002382 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002383}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002384
Guido van Rossum60456fd1997-04-09 17:36:32 +00002385static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00002386save(Picklerobject *self, PyObject *args, int pers_save)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002387{
2388 PyTypeObject *type;
Tim Peters71fcda52003-02-14 23:05:28 +00002389 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
Tim Peters71fcda52003-02-14 23:05:28 +00002390 int res = -1;
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002391 int tmp;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002392
Facundo Batista763d3092008-06-30 01:10:55 +00002393 if (Py_EnterRecursiveCall(" while pickling an object"))
2394 return -1;
Martin v. Löwis5a395302002-08-04 08:20:23 +00002395
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002396 if (!pers_save && self->pers_func) {
2397 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2398 res = tmp;
2399 goto finally;
2400 }
2401 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002402
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002403 if (args == Py_None) {
2404 res = save_none(self, args);
2405 goto finally;
2406 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002407
Christian Heimese93237d2007-12-19 02:37:44 +00002408 type = Py_TYPE(args);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002409
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002410 switch (type->tp_name[0]) {
Guido van Rossum77f6a652002-04-03 22:41:51 +00002411 case 'b':
2412 if (args == Py_False || args == Py_True) {
2413 res = save_bool(self, args);
2414 goto finally;
2415 }
2416 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002417 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002418 if (type == &PyInt_Type) {
2419 res = save_int(self, args);
2420 goto finally;
2421 }
2422 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002423
Guido van Rossum60456fd1997-04-09 17:36:32 +00002424 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002425 if (type == &PyLong_Type) {
2426 res = save_long(self, args);
2427 goto finally;
2428 }
2429 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002430
Guido van Rossum60456fd1997-04-09 17:36:32 +00002431 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002432 if (type == &PyFloat_Type) {
2433 res = save_float(self, args);
2434 goto finally;
2435 }
2436 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002437
Guido van Rossum60456fd1997-04-09 17:36:32 +00002438 case 't':
Tim Peters1d63c9f2003-02-02 20:29:39 +00002439 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2440 res = save_tuple(self, args);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002441 goto finally;
2442 }
2443 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002444
Guido van Rossum60456fd1997-04-09 17:36:32 +00002445 case 's':
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002446 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002447 res = save_string(self, args, 0);
2448 goto finally;
2449 }
Facundo Batista14618862008-06-22 15:27:10 +00002450 break;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002451
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002452#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002453 case 'u':
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002454 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002455 res = save_unicode(self, args, 0);
2456 goto finally;
2457 }
Facundo Batista14618862008-06-22 15:27:10 +00002458 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002459#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002460 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002461
Christian Heimese93237d2007-12-19 02:37:44 +00002462 if (Py_REFCNT(args) > 1) {
Tim Peterscba30e22003-02-01 06:24:36 +00002463 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002464 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002465
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002466 if (PyDict_GetItem(self->memo, py_ob_id)) {
2467 if (get(self, py_ob_id) < 0)
2468 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002469
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002470 res = 0;
2471 goto finally;
2472 }
2473 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002474
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002475 switch (type->tp_name[0]) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002476 case 's':
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002477 if (type == &PyString_Type) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002478 res = save_string(self, args, 1);
2479 goto finally;
2480 }
2481 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002482
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002483#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002484 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002485 if (type == &PyUnicode_Type) {
2486 res = save_unicode(self, args, 1);
2487 goto finally;
2488 }
2489 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002490#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002491
Guido van Rossum60456fd1997-04-09 17:36:32 +00002492 case 't':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002493 if (type == &PyTuple_Type) {
2494 res = save_tuple(self, args);
2495 goto finally;
2496 }
2497 if (type == &PyType_Type) {
2498 res = save_global(self, args, NULL);
2499 goto finally;
2500 }
2501 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002502
Guido van Rossum60456fd1997-04-09 17:36:32 +00002503 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002504 if (type == &PyList_Type) {
2505 res = save_list(self, args);
2506 goto finally;
2507 }
2508 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002509
2510 case 'd':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002511 if (type == &PyDict_Type) {
2512 res = save_dict(self, args);
2513 goto finally;
2514 }
2515 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002516
2517 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002518 if (type == &PyInstance_Type) {
2519 res = save_inst(self, args);
2520 goto finally;
2521 }
2522 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002523
2524 case 'c':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002525 if (type == &PyClass_Type) {
2526 res = save_global(self, args, NULL);
2527 goto finally;
2528 }
2529 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002530
2531 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002532 if (type == &PyFunction_Type) {
2533 res = save_global(self, args, NULL);
Christian Tismer2460c622004-02-26 16:21:45 +00002534 if (res && PyErr_ExceptionMatches(PickleError)) {
2535 /* fall back to reduce */
2536 PyErr_Clear();
2537 break;
2538 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002539 goto finally;
2540 }
2541 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002542
2543 case 'b':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002544 if (type == &PyCFunction_Type) {
2545 res = save_global(self, args, NULL);
2546 goto finally;
2547 }
2548 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002549
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002550 if (!pers_save && self->inst_pers_func) {
2551 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2552 res = tmp;
2553 goto finally;
2554 }
2555 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002556
Jeremy Hylton39c61162002-07-16 19:47:43 +00002557 if (PyType_IsSubtype(type, &PyType_Type)) {
2558 res = save_global(self, args, NULL);
2559 goto finally;
2560 }
2561
Guido van Rossumb289b872003-02-19 01:45:13 +00002562 /* Get a reduction callable, and call it. This may come from
Georg Brandldffbf5f2008-05-20 07:49:57 +00002563 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
Guido van Rossumb289b872003-02-19 01:45:13 +00002564 * or the object's __reduce__ method.
Tim Peters71fcda52003-02-14 23:05:28 +00002565 */
Tim Peters5aa3da62003-02-13 21:03:57 +00002566 __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2567 if (__reduce__ != NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002568 Py_INCREF(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00002569 Py_INCREF(args);
2570 ARG_TUP(self, args);
2571 if (self->arg) {
2572 t = PyObject_Call(__reduce__, self->arg, NULL);
2573 FREE_ARG_TUP(self);
2574 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002575 }
2576 else {
Guido van Rossumb289b872003-02-19 01:45:13 +00002577 /* Check for a __reduce_ex__ method. */
2578 __reduce__ = PyObject_GetAttr(args, __reduce_ex___str);
2579 if (__reduce__ != NULL) {
2580 t = PyInt_FromLong(self->proto);
2581 if (t != NULL) {
2582 ARG_TUP(self, t);
2583 t = NULL;
2584 if (self->arg) {
2585 t = PyObject_Call(__reduce__,
2586 self->arg, NULL);
2587 FREE_ARG_TUP(self);
2588 }
2589 }
2590 }
2591 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002592 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2593 PyErr_Clear();
2594 else
2595 goto finally;
Guido van Rossumb289b872003-02-19 01:45:13 +00002596 /* Check for a __reduce__ method. */
2597 __reduce__ = PyObject_GetAttr(args, __reduce___str);
2598 if (__reduce__ != NULL) {
2599 t = PyObject_Call(__reduce__,
2600 empty_tuple, NULL);
2601 }
2602 else {
2603 PyErr_SetObject(UnpickleableError, args);
2604 goto finally;
2605 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002606 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002607 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002608
Tim Peters71fcda52003-02-14 23:05:28 +00002609 if (t == NULL)
2610 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002611
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002612 if (PyString_Check(t)) {
Tim Peters71fcda52003-02-14 23:05:28 +00002613 res = save_global(self, args, t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002614 goto finally;
2615 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002616
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002617 if (!PyTuple_Check(t)) {
Tim Peters71fcda52003-02-14 23:05:28 +00002618 cPickle_ErrFormat(PicklingError, "Value returned by "
2619 "%s must be string or tuple",
2620 "O", __reduce__);
2621 goto finally;
2622 }
2623
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002624 res = save_reduce(self, t, __reduce__, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002625
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002626 finally:
Facundo Batista763d3092008-06-30 01:10:55 +00002627 Py_LeaveRecursiveCall();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002628 Py_XDECREF(py_ob_id);
2629 Py_XDECREF(__reduce__);
2630 Py_XDECREF(t);
Tim Peters84e87f32001-03-17 04:50:51 +00002631
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002632 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002633}
2634
2635
2636static int
Tim Peterscba30e22003-02-01 06:24:36 +00002637dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002638{
2639 static char stop = STOP;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002640
Tim Peters4190fb82003-02-02 16:09:05 +00002641 if (self->proto >= 2) {
2642 char bytes[2];
2643
2644 bytes[0] = PROTO;
Tim Peters87482ea2003-02-02 16:16:30 +00002645 assert(self->proto >= 0 && self->proto < 256);
2646 bytes[1] = (char)self->proto;
Tim Peters4190fb82003-02-02 16:09:05 +00002647 if (self->write_func(self, bytes, 2) < 0)
2648 return -1;
2649 }
2650
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002651 if (save(self, args, 0) < 0)
2652 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002653
Tim Peters4190fb82003-02-02 16:09:05 +00002654 if (self->write_func(self, &stop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002655 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002656
Tim Peters4190fb82003-02-02 16:09:05 +00002657 if (self->write_func(self, NULL, 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002658 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002659
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002660 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002661}
2662
2663static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002664Pickle_clear_memo(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002665{
Tim Peterscba30e22003-02-01 06:24:36 +00002666 if (self->memo)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002667 PyDict_Clear(self->memo);
2668 Py_INCREF(Py_None);
2669 return Py_None;
2670}
2671
2672static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002673Pickle_getvalue(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002674{
2675 int l, i, rsize, ssize, clear=1, lm;
2676 long ik;
2677 PyObject *k, *r;
2678 char *s, *p, *have_get;
2679 Pdata *data;
2680
2681 /* Can be called by Python code or C code */
Tim Peterscba30e22003-02-01 06:24:36 +00002682 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002683 return NULL;
2684
2685 /* Check to make sure we are based on a list */
2686 if (! Pdata_Check(self->file)) {
2687 PyErr_SetString(PicklingError,
2688 "Attempt to getvalue() a non-list-based pickler");
2689 return NULL;
2690 }
2691
2692 /* flush write buffer */
2693 if (write_other(self, NULL, 0) < 0) return NULL;
2694
2695 data=(Pdata*)self->file;
2696 l=data->length;
2697
2698 /* set up an array to hold get/put status */
Tim Petersac5687a2003-02-02 18:08:34 +00002699 lm = PyDict_Size(self->memo);
2700 if (lm < 0) return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002701 lm++;
Tim Petersac5687a2003-02-02 18:08:34 +00002702 have_get = malloc(lm);
2703 if (have_get == NULL) return PyErr_NoMemory();
2704 memset(have_get, 0, lm);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002705
2706 /* Scan for gets. */
Tim Petersac5687a2003-02-02 18:08:34 +00002707 for (rsize = 0, i = l; --i >= 0; ) {
2708 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002709
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002710 if (PyString_Check(k))
2711 rsize += PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002712
2713 else if (PyInt_Check(k)) { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002714 ik = PyInt_AS_LONG((PyIntObject*)k);
2715 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002716 PyErr_SetString(PicklingError,
2717 "Invalid get data");
Neal Norwitz98a96002006-07-23 07:57:11 +00002718 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002719 }
Tim Petersac5687a2003-02-02 18:08:34 +00002720 if (have_get[ik]) /* with matching get */
2721 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002722 }
2723
2724 else if (! (PyTuple_Check(k) &&
2725 PyTuple_GET_SIZE(k) == 2 &&
Tim Petersac5687a2003-02-02 18:08:34 +00002726 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002727 ) {
2728 PyErr_SetString(PicklingError,
2729 "Unexpected data in internal list");
Neal Norwitz98a96002006-07-23 07:57:11 +00002730 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002731 }
2732
2733 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002734 ik = PyInt_AS_LONG((PyIntObject *)k);
2735 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002736 PyErr_SetString(PicklingError,
2737 "Invalid get data");
2738 return NULL;
2739 }
Tim Petersac5687a2003-02-02 18:08:34 +00002740 have_get[ik] = 1;
2741 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002742 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002743 }
2744
2745 /* Now generate the result */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002746 r = PyString_FromStringAndSize(NULL, rsize);
Tim Petersac5687a2003-02-02 18:08:34 +00002747 if (r == NULL) goto err;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002748 s = PyString_AS_STRING((PyStringObject *)r);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002749
Tim Petersac5687a2003-02-02 18:08:34 +00002750 for (i = 0; i < l; i++) {
2751 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002752
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002753 if (PyString_Check(k)) {
2754 ssize = PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002755 if (ssize) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002756 p=PyString_AS_STRING((PyStringObject *)k);
Tim Petersac5687a2003-02-02 18:08:34 +00002757 while (--ssize >= 0)
2758 *s++ = *p++;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002759 }
2760 }
2761
2762 else if (PyTuple_Check(k)) { /* get */
Tim Petersac5687a2003-02-02 18:08:34 +00002763 ik = PyInt_AS_LONG((PyIntObject *)
2764 PyTuple_GET_ITEM(k, 0));
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002765 if (ik < 256) {
2766 *s++ = BINGET;
2767 *s++ = (int)(ik & 0xff);
2768 }
2769 else {
2770 *s++ = LONG_BINGET;
2771 *s++ = (int)(ik & 0xff);
2772 *s++ = (int)((ik >> 8) & 0xff);
2773 *s++ = (int)((ik >> 16) & 0xff);
2774 *s++ = (int)((ik >> 24) & 0xff);
2775 }
2776 }
2777
2778 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002779 ik = PyInt_AS_LONG((PyIntObject*)k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002780
2781 if (have_get[ik]) { /* with matching get */
2782 if (ik < 256) {
2783 *s++ = BINPUT;
2784 *s++ = (int)(ik & 0xff);
2785 }
2786 else {
2787 *s++ = LONG_BINPUT;
2788 *s++ = (int)(ik & 0xff);
2789 *s++ = (int)((ik >> 8) & 0xff);
2790 *s++ = (int)((ik >> 16) & 0xff);
2791 *s++ = (int)((ik >> 24) & 0xff);
2792 }
2793 }
2794 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002795 }
2796
2797 if (clear) {
2798 PyDict_Clear(self->memo);
Tim Petersac5687a2003-02-02 18:08:34 +00002799 Pdata_clear(data, 0);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002800 }
2801
2802 free(have_get);
2803 return r;
2804 err:
2805 free(have_get);
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002806 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002807}
2808
2809static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002810Pickler_dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002811{
2812 PyObject *ob;
2813 int get=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002814
Tim Peterscba30e22003-02-01 06:24:36 +00002815 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002816 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002817
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002818 if (dump(self, ob) < 0)
2819 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002820
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002821 if (get) return Pickle_getvalue(self, NULL);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002822
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002823 /* XXX Why does dump() return self? */
2824 Py_INCREF(self);
2825 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002826}
2827
2828
Tim Peterscba30e22003-02-01 06:24:36 +00002829static struct PyMethodDef Pickler_methods[] =
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002830{
Neal Norwitzb0493252002-03-31 14:44:22 +00002831 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002832 PyDoc_STR("dump(object) -- "
2833 "Write an object in pickle format to the object's pickle stream")},
Fred Drake0ebacc82002-05-01 20:36:39 +00002834 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002835 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
Neal Norwitzb0493252002-03-31 14:44:22 +00002836 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002837 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
Guido van Rossum60456fd1997-04-09 17:36:32 +00002838 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002839};
2840
2841
2842static Picklerobject *
Tim Peters5bd2a792003-02-01 16:45:06 +00002843newPicklerobject(PyObject *file, int proto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002844{
2845 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002846
Tim Peters5bd2a792003-02-01 16:45:06 +00002847 if (proto < 0)
Tim Peters8587b3c2003-02-13 15:44:41 +00002848 proto = HIGHEST_PROTOCOL;
2849 if (proto > HIGHEST_PROTOCOL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002850 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
2851 "the highest available protocol is %d",
Tim Peters8587b3c2003-02-13 15:44:41 +00002852 proto, HIGHEST_PROTOCOL);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002853 return NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002854 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002855
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002856 self = PyObject_GC_New(Picklerobject, &Picklertype);
Tim Peters5bd2a792003-02-01 16:45:06 +00002857 if (self == NULL)
2858 return NULL;
2859 self->proto = proto;
2860 self->bin = proto > 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002861 self->fp = NULL;
2862 self->write = NULL;
2863 self->memo = NULL;
2864 self->arg = NULL;
2865 self->pers_func = NULL;
2866 self->inst_pers_func = NULL;
2867 self->write_buf = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002868 self->fast = 0;
2869 self->fast_container = 0;
2870 self->fast_memo = NULL;
2871 self->buf_size = 0;
2872 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002873
Tim Peters5bd2a792003-02-01 16:45:06 +00002874 self->file = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002875 if (file)
2876 Py_INCREF(file);
Tim Peters5bd2a792003-02-01 16:45:06 +00002877 else {
2878 file = Pdata_New();
2879 if (file == NULL)
2880 goto err;
2881 }
2882 self->file = file;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002883
Tim Peterscba30e22003-02-01 06:24:36 +00002884 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002885 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002886
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002887 if (PyFile_Check(file)) {
2888 self->fp = PyFile_AsFile(file);
2889 if (self->fp == NULL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002890 PyErr_SetString(PyExc_ValueError,
2891 "I/O operation on closed file");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002892 goto err;
2893 }
2894 self->write_func = write_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002895 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002896 else if (PycStringIO_OutputCheck(file)) {
2897 self->write_func = write_cStringIO;
2898 }
2899 else if (file == Py_None) {
2900 self->write_func = write_none;
2901 }
2902 else {
2903 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002904
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002905 if (! Pdata_Check(file)) {
2906 self->write = PyObject_GetAttr(file, write_str);
2907 if (!self->write) {
2908 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00002909 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002910 "argument must have 'write' "
2911 "attribute");
2912 goto err;
2913 }
2914 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002915
Tim Peters5bd2a792003-02-01 16:45:06 +00002916 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
2917 if (self->write_buf == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002918 PyErr_NoMemory();
2919 goto err;
2920 }
2921 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002922
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002923 if (PyEval_GetRestricted()) {
2924 /* Restricted execution, get private tables */
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00002925 PyObject *m = PyImport_Import(copyreg_str);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002926
Tim Peters5b7da392003-02-04 00:21:07 +00002927 if (m == NULL)
2928 goto err;
2929 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002930 Py_DECREF(m);
Tim Peters5b7da392003-02-04 00:21:07 +00002931 if (self->dispatch_table == NULL)
2932 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002933 }
2934 else {
Tim Peters5b7da392003-02-04 00:21:07 +00002935 self->dispatch_table = dispatch_table;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002936 Py_INCREF(dispatch_table);
2937 }
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002938 PyObject_GC_Track(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002939
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002940 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002941
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002942 err:
Tim Peters5bd2a792003-02-01 16:45:06 +00002943 Py_DECREF(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002944 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002945}
2946
2947
2948static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00002949get_Pickler(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002950{
Martin v. Löwis15e62742006-02-27 16:46:16 +00002951 static char *kwlist[] = {"file", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002952 PyObject *file = NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002953 int proto = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002954
Tim Peters92c8bb32003-02-13 23:00:26 +00002955 /* XXX
Martin v. Löwis544f1192004-07-27 05:22:33 +00002956 * The documented signature is Pickler(file, protocol=0), but this
Tim Peters92c8bb32003-02-13 23:00:26 +00002957 * accepts Pickler() and Pickler(integer) too. The meaning then
2958 * is clear as mud, undocumented, and not supported by pickle.py.
2959 * I'm told Zope uses this, but I haven't traced into this code
2960 * far enough to figure out what it means.
Tim Peters5bd2a792003-02-01 16:45:06 +00002961 */
2962 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002963 PyErr_Clear();
Tim Peters5bd2a792003-02-01 16:45:06 +00002964 proto = 0;
Martin v. Löwis544f1192004-07-27 05:22:33 +00002965 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler",
2966 kwlist, &file, &proto))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002967 return NULL;
2968 }
Tim Peters5bd2a792003-02-01 16:45:06 +00002969 return (PyObject *)newPicklerobject(file, proto);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002970}
2971
2972
2973static void
Tim Peterscba30e22003-02-01 06:24:36 +00002974Pickler_dealloc(Picklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002975{
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002976 PyObject_GC_UnTrack(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002977 Py_XDECREF(self->write);
2978 Py_XDECREF(self->memo);
2979 Py_XDECREF(self->fast_memo);
2980 Py_XDECREF(self->arg);
2981 Py_XDECREF(self->file);
2982 Py_XDECREF(self->pers_func);
2983 Py_XDECREF(self->inst_pers_func);
2984 Py_XDECREF(self->dispatch_table);
Tim Peters5bd2a792003-02-01 16:45:06 +00002985 PyMem_Free(self->write_buf);
Christian Heimese93237d2007-12-19 02:37:44 +00002986 Py_TYPE(self)->tp_free((PyObject *)self);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002987}
2988
2989static int
2990Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)
2991{
Thomas Woutersc6e55062006-04-15 21:47:09 +00002992 Py_VISIT(self->write);
2993 Py_VISIT(self->memo);
2994 Py_VISIT(self->fast_memo);
2995 Py_VISIT(self->arg);
2996 Py_VISIT(self->file);
2997 Py_VISIT(self->pers_func);
2998 Py_VISIT(self->inst_pers_func);
2999 Py_VISIT(self->dispatch_table);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003000 return 0;
3001}
3002
3003static int
3004Pickler_clear(Picklerobject *self)
3005{
Thomas Woutersedf17d82006-04-15 17:28:34 +00003006 Py_CLEAR(self->write);
3007 Py_CLEAR(self->memo);
3008 Py_CLEAR(self->fast_memo);
3009 Py_CLEAR(self->arg);
3010 Py_CLEAR(self->file);
3011 Py_CLEAR(self->pers_func);
3012 Py_CLEAR(self->inst_pers_func);
3013 Py_CLEAR(self->dispatch_table);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003014 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003015}
3016
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003017static PyObject *
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003018Pickler_get_pers_func(Picklerobject *p)
3019{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003020 if (p->pers_func == NULL)
3021 PyErr_SetString(PyExc_AttributeError, "persistent_id");
3022 else
3023 Py_INCREF(p->pers_func);
3024 return p->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003025}
3026
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003027static int
3028Pickler_set_pers_func(Picklerobject *p, PyObject *v)
3029{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003030 if (v == NULL) {
3031 PyErr_SetString(PyExc_TypeError,
3032 "attribute deletion is not supported");
3033 return -1;
3034 }
3035 Py_XDECREF(p->pers_func);
3036 Py_INCREF(v);
3037 p->pers_func = v;
3038 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003039}
3040
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003041static int
3042Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
3043{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003044 if (v == NULL) {
3045 PyErr_SetString(PyExc_TypeError,
3046 "attribute deletion is not supported");
3047 return -1;
3048 }
3049 Py_XDECREF(p->inst_pers_func);
3050 Py_INCREF(v);
3051 p->inst_pers_func = v;
3052 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003053}
3054
3055static PyObject *
3056Pickler_get_memo(Picklerobject *p)
3057{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003058 if (p->memo == NULL)
3059 PyErr_SetString(PyExc_AttributeError, "memo");
3060 else
3061 Py_INCREF(p->memo);
3062 return p->memo;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003063}
3064
3065static int
3066Pickler_set_memo(Picklerobject *p, PyObject *v)
3067{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003068 if (v == NULL) {
3069 PyErr_SetString(PyExc_TypeError,
3070 "attribute deletion is not supported");
3071 return -1;
3072 }
3073 if (!PyDict_Check(v)) {
3074 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
3075 return -1;
3076 }
3077 Py_XDECREF(p->memo);
3078 Py_INCREF(v);
3079 p->memo = v;
3080 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003081}
3082
3083static PyObject *
3084Pickler_get_error(Picklerobject *p)
3085{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003086 /* why is this an attribute on the Pickler? */
3087 Py_INCREF(PicklingError);
3088 return PicklingError;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003089}
3090
3091static PyMemberDef Pickler_members[] = {
3092 {"binary", T_INT, offsetof(Picklerobject, bin)},
3093 {"fast", T_INT, offsetof(Picklerobject, fast)},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003094 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003095};
3096
3097static PyGetSetDef Pickler_getsets[] = {
Tim Peterscba30e22003-02-01 06:24:36 +00003098 {"persistent_id", (getter)Pickler_get_pers_func,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003099 (setter)Pickler_set_pers_func},
3100 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
3101 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003102 {"PicklingError", (getter)Pickler_get_error, NULL},
3103 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003104};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003105
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003106PyDoc_STRVAR(Picklertype__doc__,
3107"Objects that know how to pickle objects\n");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003108
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003109static PyTypeObject Picklertype = {
Martin v. Löwis68192102007-07-21 06:55:02 +00003110 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00003111 "cPickle.Pickler", /*tp_name*/
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003112 sizeof(Picklerobject), /*tp_basicsize*/
3113 0,
3114 (destructor)Pickler_dealloc, /* tp_dealloc */
3115 0, /* tp_print */
3116 0, /* tp_getattr */
3117 0, /* tp_setattr */
3118 0, /* tp_compare */
3119 0, /* tp_repr */
3120 0, /* tp_as_number */
3121 0, /* tp_as_sequence */
3122 0, /* tp_as_mapping */
3123 0, /* tp_hash */
3124 0, /* tp_call */
3125 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00003126 PyObject_GenericGetAttr, /* tp_getattro */
3127 PyObject_GenericSetAttr, /* tp_setattro */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003128 0, /* tp_as_buffer */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003129 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003130 Picklertype__doc__, /* tp_doc */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003131 (traverseproc)Pickler_traverse, /* tp_traverse */
3132 (inquiry)Pickler_clear, /* tp_clear */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003133 0, /* tp_richcompare */
3134 0, /* tp_weaklistoffset */
3135 0, /* tp_iter */
3136 0, /* tp_iternext */
3137 Pickler_methods, /* tp_methods */
3138 Pickler_members, /* tp_members */
3139 Pickler_getsets, /* tp_getset */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003140};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003141
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003142static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003143find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003144{
3145 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003146
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003147 if (fc) {
3148 if (fc==Py_None) {
Tim Peters92c8bb32003-02-13 23:00:26 +00003149 PyErr_SetString(UnpicklingError, "Global and instance "
3150 "pickles are not supported.");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003151 return NULL;
3152 }
Georg Brandl684fd0c2006-05-25 19:15:31 +00003153 return PyObject_CallFunctionObjArgs(fc, py_module_name,
3154 py_global_name, NULL);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003155 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003156
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003157 module = PySys_GetObject("modules");
3158 if (module == NULL)
3159 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00003160
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003161 module = PyDict_GetItem(module, py_module_name);
3162 if (module == NULL) {
3163 module = PyImport_Import(py_module_name);
3164 if (!module)
3165 return NULL;
3166 global = PyObject_GetAttr(module, py_global_name);
3167 Py_DECREF(module);
3168 }
3169 else
3170 global = PyObject_GetAttr(module, py_global_name);
3171 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003172}
3173
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003174static int
Tim Peterscba30e22003-02-01 06:24:36 +00003175marker(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003176{
3177 if (self->num_marks < 1) {
3178 PyErr_SetString(UnpicklingError, "could not find MARK");
3179 return -1;
3180 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003181
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003182 return self->marks[--self->num_marks];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003183}
3184
Tim Peters84e87f32001-03-17 04:50:51 +00003185
Guido van Rossum60456fd1997-04-09 17:36:32 +00003186static int
Tim Peterscba30e22003-02-01 06:24:36 +00003187load_none(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003188{
3189 PDATA_APPEND(self->stack, Py_None, -1);
3190 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003191}
3192
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003193static int
Tim Peterscba30e22003-02-01 06:24:36 +00003194bad_readline(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003195{
3196 PyErr_SetString(UnpicklingError, "pickle data was truncated");
3197 return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003198}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003199
3200static int
Tim Peterscba30e22003-02-01 06:24:36 +00003201load_int(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003202{
3203 PyObject *py_int = 0;
3204 char *endptr, *s;
3205 int len, res = -1;
3206 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003207
Tim Peters0bc93f52003-02-02 18:29:33 +00003208 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003209 if (len < 2) return bad_readline();
3210 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003211
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003212 errno = 0;
3213 l = strtol(s, &endptr, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003214
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003215 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
3216 /* Hm, maybe we've got something long. Let's try reading
3217 it as a Python long object. */
3218 errno = 0;
3219 py_int = PyLong_FromString(s, NULL, 0);
3220 if (py_int == NULL) {
3221 PyErr_SetString(PyExc_ValueError,
3222 "could not convert string to int");
3223 goto finally;
3224 }
3225 }
3226 else {
Guido van Rossume2763392002-04-05 19:30:08 +00003227 if (len == 3 && (l == 0 || l == 1)) {
3228 if (!( py_int = PyBool_FromLong(l))) goto finally;
3229 }
3230 else {
3231 if (!( py_int = PyInt_FromLong(l))) goto finally;
3232 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003233 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003234
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003235 free(s);
3236 PDATA_PUSH(self->stack, py_int, -1);
3237 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003238
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003239 finally:
3240 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003241
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003242 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003243}
3244
Tim Peters3c67d792003-02-02 17:59:11 +00003245static int
3246load_bool(Unpicklerobject *self, PyObject *boolean)
3247{
3248 assert(boolean == Py_True || boolean == Py_False);
Tim Peterse0a39072003-02-03 15:45:56 +00003249 PDATA_APPEND(self->stack, boolean, -1);
Tim Peters3c67d792003-02-02 17:59:11 +00003250 return 0;
3251}
3252
Tim Petersee1a53c2003-02-02 02:57:53 +00003253/* s contains x bytes of a little-endian integer. Return its value as a
3254 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3255 * int, but when x is 4 it's a signed one. This is an historical source
3256 * of x-platform bugs.
3257 */
Tim Peters84e87f32001-03-17 04:50:51 +00003258static long
Tim Petersee1a53c2003-02-02 02:57:53 +00003259calc_binint(char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003260{
3261 unsigned char c;
3262 int i;
3263 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003264
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003265 for (i = 0, l = 0L; i < x; i++) {
3266 c = (unsigned char)s[i];
3267 l |= (long)c << (i * 8);
3268 }
Tim Petersbfa18f72001-04-10 01:54:42 +00003269#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003270 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3271 * is signed, so on a box with longs bigger than 4 bytes we need
3272 * to extend a BININT's sign bit to the full width.
3273 */
3274 if (x == 4 && l & (1L << 31))
3275 l |= (~0L) << 32;
Tim Petersbfa18f72001-04-10 01:54:42 +00003276#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003277 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003278}
3279
3280
3281static int
Tim Peterscba30e22003-02-01 06:24:36 +00003282load_binintx(Unpicklerobject *self, char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003283{
3284 PyObject *py_int = 0;
3285 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003286
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003287 l = calc_binint(s, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003288
Tim Peterscba30e22003-02-01 06:24:36 +00003289 if (!( py_int = PyInt_FromLong(l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003290 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003291
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003292 PDATA_PUSH(self->stack, py_int, -1);
3293 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003294}
3295
3296
3297static int
Tim Peterscba30e22003-02-01 06:24:36 +00003298load_binint(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003299{
3300 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003301
Tim Peters0bc93f52003-02-02 18:29:33 +00003302 if (self->read_func(self, &s, 4) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003303 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003304
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003305 return load_binintx(self, s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003306}
3307
3308
3309static int
Tim Peterscba30e22003-02-01 06:24:36 +00003310load_binint1(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003311{
3312 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003313
Tim Peters0bc93f52003-02-02 18:29:33 +00003314 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003315 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003316
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003317 return load_binintx(self, s, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003318}
3319
3320
3321static int
Tim Peterscba30e22003-02-01 06:24:36 +00003322load_binint2(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003323{
3324 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003325
Tim Peters0bc93f52003-02-02 18:29:33 +00003326 if (self->read_func(self, &s, 2) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003327 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003328
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003329 return load_binintx(self, s, 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003330}
Tim Peters84e87f32001-03-17 04:50:51 +00003331
Guido van Rossum60456fd1997-04-09 17:36:32 +00003332static int
Tim Peterscba30e22003-02-01 06:24:36 +00003333load_long(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003334{
3335 PyObject *l = 0;
3336 char *end, *s;
3337 int len, res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003338
Tim Peters0bc93f52003-02-02 18:29:33 +00003339 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003340 if (len < 2) return bad_readline();
3341 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003342
Tim Peterscba30e22003-02-01 06:24:36 +00003343 if (!( l = PyLong_FromString(s, &end, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003344 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003345
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003346 free(s);
3347 PDATA_PUSH(self->stack, l, -1);
3348 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003349
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003350 finally:
3351 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003352
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003353 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003354}
3355
Tim Petersee1a53c2003-02-02 02:57:53 +00003356/* 'size' bytes contain the # of bytes of little-endian 256's-complement
3357 * data following.
3358 */
3359static int
3360load_counted_long(Unpicklerobject *self, int size)
3361{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003362 Py_ssize_t i;
Tim Petersee1a53c2003-02-02 02:57:53 +00003363 char *nbytes;
3364 unsigned char *pdata;
3365 PyObject *along;
3366
3367 assert(size == 1 || size == 4);
3368 i = self->read_func(self, &nbytes, size);
3369 if (i < 0) return -1;
3370
3371 size = calc_binint(nbytes, size);
3372 if (size < 0) {
3373 /* Corrupt or hostile pickle -- we never write one like
3374 * this.
3375 */
Tim Peters0c7c48e2003-02-03 22:07:24 +00003376 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
Tim Petersee1a53c2003-02-02 02:57:53 +00003377 "byte count");
3378 return -1;
3379 }
3380
3381 if (size == 0)
3382 along = PyLong_FromLong(0L);
3383 else {
3384 /* Read the raw little-endian bytes & convert. */
Neal Norwitz529baf22003-02-02 17:08:33 +00003385 i = self->read_func(self, (char **)&pdata, size);
Tim Petersee1a53c2003-02-02 02:57:53 +00003386 if (i < 0) return -1;
3387 along = _PyLong_FromByteArray(pdata, (size_t)size,
3388 1 /* little endian */, 1 /* signed */);
3389 }
3390 if (along == NULL)
3391 return -1;
3392 PDATA_PUSH(self->stack, along, -1);
3393 return 0;
3394}
Tim Peters84e87f32001-03-17 04:50:51 +00003395
Guido van Rossum60456fd1997-04-09 17:36:32 +00003396static int
Tim Peterscba30e22003-02-01 06:24:36 +00003397load_float(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003398{
3399 PyObject *py_float = 0;
3400 char *endptr, *s;
3401 int len, res = -1;
3402 double d;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003403
Tim Peters0bc93f52003-02-02 18:29:33 +00003404 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003405 if (len < 2) return bad_readline();
3406 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003407
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003408 errno = 0;
Martin v. Löwis737ea822004-06-08 18:52:54 +00003409 d = PyOS_ascii_strtod(s, &endptr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003410
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003411 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
3412 PyErr_SetString(PyExc_ValueError,
3413 "could not convert string to float");
3414 goto finally;
3415 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003416
Tim Peterscba30e22003-02-01 06:24:36 +00003417 if (!( py_float = PyFloat_FromDouble(d)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003418 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003419
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003420 free(s);
3421 PDATA_PUSH(self->stack, py_float, -1);
3422 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003423
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003424 finally:
3425 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003426
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003427 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003428}
3429
Guido van Rossum60456fd1997-04-09 17:36:32 +00003430static int
Tim Peterscba30e22003-02-01 06:24:36 +00003431load_binfloat(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003432{
Tim Peters9905b942003-03-20 20:53:32 +00003433 PyObject *py_float;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003434 double x;
3435 char *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003436
Tim Peters0bc93f52003-02-02 18:29:33 +00003437 if (self->read_func(self, &p, 8) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003438 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003439
Tim Peters9905b942003-03-20 20:53:32 +00003440 x = _PyFloat_Unpack8((unsigned char *)p, 0);
3441 if (x == -1.0 && PyErr_Occurred())
3442 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003443
Tim Peters9905b942003-03-20 20:53:32 +00003444 py_float = PyFloat_FromDouble(x);
3445 if (py_float == NULL)
3446 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003447
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003448 PDATA_PUSH(self->stack, py_float, -1);
3449 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003450}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003451
3452static int
Tim Peterscba30e22003-02-01 06:24:36 +00003453load_string(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003454{
3455 PyObject *str = 0;
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003456 int len, res = -1;
3457 char *s, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003458
Tim Peters0bc93f52003-02-02 18:29:33 +00003459 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003460 if (len < 2) return bad_readline();
3461 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003462
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003463
3464 /* Strip outermost quotes */
3465 while (s[len-1] <= ' ')
3466 len--;
3467 if(s[0]=='"' && s[len-1]=='"'){
3468 s[len-1] = '\0';
3469 p = s + 1 ;
3470 len -= 2;
3471 } else if(s[0]=='\'' && s[len-1]=='\''){
3472 s[len-1] = '\0';
3473 p = s + 1 ;
3474 len -= 2;
3475 } else
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003476 goto insecure;
3477 /********************************************/
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003478
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003479 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
Neal Norwitz99dfe3c2006-08-02 04:27:11 +00003480 free(s);
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003481 if (str) {
3482 PDATA_PUSH(self->stack, str, -1);
3483 res = 0;
3484 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003485 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003486
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003487 insecure:
3488 free(s);
3489 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3490 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003491}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003492
3493
3494static int
Tim Peterscba30e22003-02-01 06:24:36 +00003495load_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003496{
3497 PyObject *py_string = 0;
3498 long l;
3499 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003500
Tim Peters0bc93f52003-02-02 18:29:33 +00003501 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003502
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003503 l = calc_binint(s, 4);
Gregory P. Smith9d534572008-06-11 07:41:16 +00003504 if (l < 0) {
3505 /* Corrupt or hostile pickle -- we never write one like
3506 * this.
3507 */
3508 PyErr_SetString(UnpicklingError,
3509 "BINSTRING pickle has negative byte count");
3510 return -1;
3511 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003512
Tim Peters0bc93f52003-02-02 18:29:33 +00003513 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003514 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003515
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003516 if (!( py_string = PyString_FromStringAndSize(s, l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003517 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003518
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003519 PDATA_PUSH(self->stack, py_string, -1);
3520 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003521}
3522
3523
3524static int
Tim Peterscba30e22003-02-01 06:24:36 +00003525load_short_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003526{
3527 PyObject *py_string = 0;
3528 unsigned char l;
3529 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003530
Tim Peters0bc93f52003-02-02 18:29:33 +00003531 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003532 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003533
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003534 l = (unsigned char)s[0];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003535
Tim Peters0bc93f52003-02-02 18:29:33 +00003536 if (self->read_func(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003537
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003538 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003539
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003540 PDATA_PUSH(self->stack, py_string, -1);
3541 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00003542}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003543
3544
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003545#ifdef Py_USING_UNICODE
Guido van Rossum60456fd1997-04-09 17:36:32 +00003546static int
Tim Peterscba30e22003-02-01 06:24:36 +00003547load_unicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003548{
3549 PyObject *str = 0;
3550 int len, res = -1;
3551 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003552
Tim Peters0bc93f52003-02-02 18:29:33 +00003553 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003554 if (len < 1) return bad_readline();
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003555
Tim Peterscba30e22003-02-01 06:24:36 +00003556 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003557 goto finally;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003558
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003559 PDATA_PUSH(self->stack, str, -1);
3560 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003561
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003562 finally:
3563 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003564}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003565#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003566
3567
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003568#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003569static int
Tim Peterscba30e22003-02-01 06:24:36 +00003570load_binunicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003571{
3572 PyObject *unicode;
3573 long l;
3574 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003575
Tim Peters0bc93f52003-02-02 18:29:33 +00003576 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003577
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003578 l = calc_binint(s, 4);
Gregory P. Smith9d534572008-06-11 07:41:16 +00003579 if (l < 0) {
3580 /* Corrupt or hostile pickle -- we never write one like
3581 * this.
3582 */
3583 PyErr_SetString(UnpicklingError,
3584 "BINUNICODE pickle has negative byte count");
3585 return -1;
3586 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003587
Tim Peters0bc93f52003-02-02 18:29:33 +00003588 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003589 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003590
Tim Peterscba30e22003-02-01 06:24:36 +00003591 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003592 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003593
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003594 PDATA_PUSH(self->stack, unicode, -1);
3595 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003596}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003597#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003598
3599
3600static int
Tim Peterscba30e22003-02-01 06:24:36 +00003601load_tuple(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003602{
3603 PyObject *tup;
3604 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003605
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003606 if ((i = marker(self)) < 0) return -1;
3607 if (!( tup=Pdata_popTuple(self->stack, i))) return -1;
3608 PDATA_PUSH(self->stack, tup, -1);
3609 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003610}
3611
3612static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00003613load_counted_tuple(Unpicklerobject *self, int len)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003614{
Tim Peters1d63c9f2003-02-02 20:29:39 +00003615 PyObject *tup = PyTuple_New(len);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003616
Tim Peters1d63c9f2003-02-02 20:29:39 +00003617 if (tup == NULL)
3618 return -1;
3619
3620 while (--len >= 0) {
3621 PyObject *element;
3622
3623 PDATA_POP(self->stack, element);
3624 if (element == NULL)
3625 return -1;
3626 PyTuple_SET_ITEM(tup, len, element);
3627 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003628 PDATA_PUSH(self->stack, tup, -1);
3629 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003630}
3631
3632static int
Tim Peterscba30e22003-02-01 06:24:36 +00003633load_empty_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003634{
3635 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003636
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003637 if (!( list=PyList_New(0))) return -1;
3638 PDATA_PUSH(self->stack, list, -1);
3639 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003640}
3641
3642static int
Tim Peterscba30e22003-02-01 06:24:36 +00003643load_empty_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003644{
3645 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003646
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003647 if (!( dict=PyDict_New())) return -1;
3648 PDATA_PUSH(self->stack, dict, -1);
3649 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003650}
3651
3652
3653static int
Tim Peterscba30e22003-02-01 06:24:36 +00003654load_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003655{
3656 PyObject *list = 0;
3657 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003658
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003659 if ((i = marker(self)) < 0) return -1;
3660 if (!( list=Pdata_popList(self->stack, i))) return -1;
3661 PDATA_PUSH(self->stack, list, -1);
3662 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003663}
3664
3665static int
Tim Peterscba30e22003-02-01 06:24:36 +00003666load_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003667{
3668 PyObject *dict, *key, *value;
3669 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003670
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003671 if ((i = marker(self)) < 0) return -1;
3672 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003673
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003674 if (!( dict = PyDict_New())) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003675
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003676 for (k = i+1; k < j; k += 2) {
3677 key =self->stack->data[k-1];
3678 value=self->stack->data[k ];
3679 if (PyDict_SetItem(dict, key, value) < 0) {
3680 Py_DECREF(dict);
3681 return -1;
3682 }
3683 }
3684 Pdata_clear(self->stack, i);
3685 PDATA_PUSH(self->stack, dict, -1);
3686 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003687}
3688
3689static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003690Instance_New(PyObject *cls, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003691{
Tim Peters1f1b2d22003-02-01 02:16:37 +00003692 PyObject *r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003693
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003694 if (PyClass_Check(cls)) {
3695 int l;
Tim Peters84e87f32001-03-17 04:50:51 +00003696
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003697 if ((l=PyObject_Size(args)) < 0) goto err;
3698 if (!( l )) {
3699 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003700
Tim Peterscba30e22003-02-01 06:24:36 +00003701 __getinitargs__ = PyObject_GetAttr(cls,
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00003702 __getinitargs___str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003703 if (!__getinitargs__) {
Tim Peterscba30e22003-02-01 06:24:36 +00003704 /* We have a class with no __getinitargs__,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003705 so bypass usual construction */
3706 PyObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003707
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003708 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00003709 if (!( inst=PyInstance_NewRaw(cls, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003710 goto err;
3711 return inst;
3712 }
3713 Py_DECREF(__getinitargs__);
3714 }
Tim Peters84e87f32001-03-17 04:50:51 +00003715
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003716 if ((r=PyInstance_New(cls, args, NULL))) return r;
3717 else goto err;
3718 }
Tim Peters84e87f32001-03-17 04:50:51 +00003719
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003720 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00003721
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003722 err:
3723 {
Neal Norwitz0f8b31a2006-06-28 06:28:31 +00003724 PyObject *tp, *v, *tb, *tmp_value;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003725
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003726 PyErr_Fetch(&tp, &v, &tb);
Neal Norwitz0f8b31a2006-06-28 06:28:31 +00003727 tmp_value = v;
3728 /* NULL occurs when there was a KeyboardInterrupt */
3729 if (tmp_value == NULL)
3730 tmp_value = Py_None;
3731 if ((r = PyTuple_Pack(3, tmp_value, cls, args))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003732 Py_XDECREF(v);
3733 v=r;
3734 }
3735 PyErr_Restore(tp,v,tb);
3736 }
3737 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003738}
Tim Peters84e87f32001-03-17 04:50:51 +00003739
Guido van Rossum60456fd1997-04-09 17:36:32 +00003740
3741static int
Tim Peterscba30e22003-02-01 06:24:36 +00003742load_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003743{
3744 PyObject *class, *tup, *obj=0;
3745 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003746
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003747 if ((i = marker(self)) < 0) return -1;
3748 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3749 PDATA_POP(self->stack, class);
3750 if (class) {
3751 obj = Instance_New(class, tup);
3752 Py_DECREF(class);
3753 }
3754 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003755
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003756 if (! obj) return -1;
3757 PDATA_PUSH(self->stack, obj, -1);
3758 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003759}
3760
3761
3762static int
Tim Peterscba30e22003-02-01 06:24:36 +00003763load_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003764{
3765 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
3766 int i, len;
3767 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003768
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003769 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003770
Tim Peters0bc93f52003-02-02 18:29:33 +00003771 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003772 if (len < 2) return bad_readline();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003773 module_name = PyString_FromStringAndSize(s, len - 1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003774 if (!module_name) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003775
Tim Peters0bc93f52003-02-02 18:29:33 +00003776 if ((len = self->readline_func(self, &s)) >= 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003777 if (len < 2) return bad_readline();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003778 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003779 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003780 self->find_class);
3781 Py_DECREF(class_name);
3782 }
3783 }
3784 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003785
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003786 if (! class) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003787
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003788 if ((tup=Pdata_popTuple(self->stack, i))) {
3789 obj = Instance_New(class, tup);
3790 Py_DECREF(tup);
3791 }
3792 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003793
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003794 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003795
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003796 PDATA_PUSH(self->stack, obj, -1);
3797 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003798}
3799
Tim Peterseab7db32003-02-13 18:24:14 +00003800static int
3801load_newobj(Unpicklerobject *self)
3802{
3803 PyObject *args = NULL;
3804 PyObject *clsraw = NULL;
3805 PyTypeObject *cls; /* clsraw cast to its true type */
3806 PyObject *obj;
3807
3808 /* Stack is ... cls argtuple, and we want to call
3809 * cls.__new__(cls, *argtuple).
3810 */
3811 PDATA_POP(self->stack, args);
3812 if (args == NULL) goto Fail;
3813 if (! PyTuple_Check(args)) {
3814 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
3815 "tuple.");
3816 goto Fail;
3817 }
3818
3819 PDATA_POP(self->stack, clsraw);
3820 cls = (PyTypeObject *)clsraw;
3821 if (cls == NULL) goto Fail;
3822 if (! PyType_Check(cls)) {
3823 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3824 "isn't a type object");
3825 goto Fail;
3826 }
3827 if (cls->tp_new == NULL) {
3828 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3829 "has NULL tp_new");
3830 goto Fail;
3831 }
3832
3833 /* Call __new__. */
3834 obj = cls->tp_new(cls, args, NULL);
3835 if (obj == NULL) goto Fail;
3836
3837 Py_DECREF(args);
3838 Py_DECREF(clsraw);
3839 PDATA_PUSH(self->stack, obj, -1);
3840 return 0;
3841
3842 Fail:
3843 Py_XDECREF(args);
3844 Py_XDECREF(clsraw);
3845 return -1;
3846}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003847
3848static int
Tim Peterscba30e22003-02-01 06:24:36 +00003849load_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003850{
3851 PyObject *class = 0, *module_name = 0, *class_name = 0;
3852 int len;
3853 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003854
Tim Peters0bc93f52003-02-02 18:29:33 +00003855 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003856 if (len < 2) return bad_readline();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003857 module_name = PyString_FromStringAndSize(s, len - 1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003858 if (!module_name) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003859
Tim Peters0bc93f52003-02-02 18:29:33 +00003860 if ((len = self->readline_func(self, &s)) >= 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +00003861 if (len < 2) {
3862 Py_DECREF(module_name);
3863 return bad_readline();
3864 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003865 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003866 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003867 self->find_class);
3868 Py_DECREF(class_name);
3869 }
3870 }
3871 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003872
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003873 if (! class) return -1;
3874 PDATA_PUSH(self->stack, class, -1);
3875 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003876}
3877
3878
3879static int
Tim Peterscba30e22003-02-01 06:24:36 +00003880load_persid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003881{
3882 PyObject *pid = 0;
3883 int len;
3884 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003885
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003886 if (self->pers_func) {
Tim Peters0bc93f52003-02-02 18:29:33 +00003887 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003888 if (len < 2) return bad_readline();
3889
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003890 pid = PyString_FromStringAndSize(s, len - 1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003891 if (!pid) return -1;
3892
3893 if (PyList_Check(self->pers_func)) {
3894 if (PyList_Append(self->pers_func, pid) < 0) {
3895 Py_DECREF(pid);
3896 return -1;
3897 }
3898 }
3899 else {
3900 ARG_TUP(self, pid);
3901 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00003902 pid = PyObject_Call(self->pers_func, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003903 NULL);
3904 FREE_ARG_TUP(self);
3905 }
3906 }
3907
3908 if (! pid) return -1;
3909
3910 PDATA_PUSH(self->stack, pid, -1);
3911 return 0;
3912 }
3913 else {
3914 PyErr_SetString(UnpicklingError,
3915 "A load persistent id instruction was encountered,\n"
3916 "but no persistent_load function was specified.");
3917 return -1;
3918 }
3919}
3920
3921static int
Tim Peterscba30e22003-02-01 06:24:36 +00003922load_binpersid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003923{
3924 PyObject *pid = 0;
3925
3926 if (self->pers_func) {
3927 PDATA_POP(self->stack, pid);
3928 if (! pid) return -1;
3929
3930 if (PyList_Check(self->pers_func)) {
3931 if (PyList_Append(self->pers_func, pid) < 0) {
3932 Py_DECREF(pid);
3933 return -1;
3934 }
3935 }
3936 else {
3937 ARG_TUP(self, pid);
3938 if (self->arg) {
3939 pid = PyObject_Call(self->pers_func, self->arg,
3940 NULL);
3941 FREE_ARG_TUP(self);
3942 }
3943 if (! pid) return -1;
3944 }
3945
3946 PDATA_PUSH(self->stack, pid, -1);
3947 return 0;
3948 }
3949 else {
3950 PyErr_SetString(UnpicklingError,
3951 "A load persistent id instruction was encountered,\n"
3952 "but no persistent_load function was specified.");
3953 return -1;
3954 }
3955}
3956
3957
3958static int
Tim Peterscba30e22003-02-01 06:24:36 +00003959load_pop(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003960{
3961 int len;
3962
3963 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
3964
3965 /* Note that we split the (pickle.py) stack into two stacks,
3966 an object stack and a mark stack. We have to be clever and
3967 pop the right one. We do this by looking at the top of the
3968 mark stack.
3969 */
3970
3971 if ((self->num_marks > 0) &&
3972 (self->marks[self->num_marks - 1] == len))
3973 self->num_marks--;
3974 else {
3975 len--;
3976 Py_DECREF(self->stack->data[len]);
3977 self->stack->length=len;
3978 }
3979
3980 return 0;
3981}
3982
3983
3984static int
Tim Peterscba30e22003-02-01 06:24:36 +00003985load_pop_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003986{
3987 int i;
3988
3989 if ((i = marker(self)) < 0)
3990 return -1;
3991
3992 Pdata_clear(self->stack, i);
3993
3994 return 0;
3995}
3996
3997
3998static int
Tim Peterscba30e22003-02-01 06:24:36 +00003999load_dup(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004000{
4001 PyObject *last;
4002 int len;
4003
4004 if ((len = self->stack->length) <= 0) return stackUnderflow();
4005 last=self->stack->data[len-1];
4006 Py_INCREF(last);
4007 PDATA_PUSH(self->stack, last, -1);
4008 return 0;
4009}
4010
4011
4012static int
Tim Peterscba30e22003-02-01 06:24:36 +00004013load_get(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004014{
4015 PyObject *py_str = 0, *value = 0;
4016 int len;
4017 char *s;
4018 int rc;
4019
Tim Peters0bc93f52003-02-02 18:29:33 +00004020 if ((len = self->readline_func(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00004021 if (len < 2) return bad_readline();
Tim Peters84e87f32001-03-17 04:50:51 +00004022
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004023 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004024
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004025 value = PyDict_GetItem(self->memo, py_str);
4026 if (! value) {
4027 PyErr_SetObject(BadPickleGet, py_str);
4028 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00004029 }
4030 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004031 PDATA_APPEND(self->stack, value, -1);
4032 rc = 0;
4033 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004034
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004035 Py_DECREF(py_str);
4036 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004037}
4038
4039
4040static int
Tim Peterscba30e22003-02-01 06:24:36 +00004041load_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004042{
4043 PyObject *py_key = 0, *value = 0;
4044 unsigned char key;
4045 char *s;
4046 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004047
Tim Peters0bc93f52003-02-02 18:29:33 +00004048 if (self->read_func(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004049
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004050 key = (unsigned char)s[0];
4051 if (!( py_key = PyInt_FromLong((long)key))) return -1;
Guido van Rossumea2b7152000-05-09 18:14:50 +00004052
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004053 value = PyDict_GetItem(self->memo, py_key);
4054 if (! value) {
4055 PyErr_SetObject(BadPickleGet, py_key);
4056 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00004057 }
4058 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004059 PDATA_APPEND(self->stack, value, -1);
4060 rc = 0;
4061 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004062
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004063 Py_DECREF(py_key);
4064 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004065}
4066
4067
4068static int
Tim Peterscba30e22003-02-01 06:24:36 +00004069load_long_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004070{
4071 PyObject *py_key = 0, *value = 0;
4072 unsigned char c;
4073 char *s;
4074 long key;
4075 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004076
Tim Peters0bc93f52003-02-02 18:29:33 +00004077 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004078
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004079 c = (unsigned char)s[0];
4080 key = (long)c;
4081 c = (unsigned char)s[1];
4082 key |= (long)c << 8;
4083 c = (unsigned char)s[2];
4084 key |= (long)c << 16;
4085 c = (unsigned char)s[3];
4086 key |= (long)c << 24;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004087
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004088 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4089
4090 value = PyDict_GetItem(self->memo, py_key);
4091 if (! value) {
4092 PyErr_SetObject(BadPickleGet, py_key);
4093 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00004094 }
4095 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004096 PDATA_APPEND(self->stack, value, -1);
4097 rc = 0;
4098 }
4099
4100 Py_DECREF(py_key);
4101 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004102}
4103
Tim Peters2d629652003-02-04 05:06:17 +00004104/* Push an object from the extension registry (EXT[124]). nbytes is
4105 * the number of bytes following the opcode, holding the index (code) value.
4106 */
4107static int
4108load_extension(Unpicklerobject *self, int nbytes)
4109{
4110 char *codebytes; /* the nbytes bytes after the opcode */
4111 long code; /* calc_binint returns long */
4112 PyObject *py_code; /* code as a Python int */
4113 PyObject *obj; /* the object to push */
4114 PyObject *pair; /* (module_name, class_name) */
4115 PyObject *module_name, *class_name;
4116
4117 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4118 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4119 code = calc_binint(codebytes, nbytes);
4120 if (code <= 0) { /* note that 0 is forbidden */
4121 /* Corrupt or hostile pickle. */
4122 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
4123 return -1;
4124 }
4125
4126 /* Look for the code in the cache. */
4127 py_code = PyInt_FromLong(code);
4128 if (py_code == NULL) return -1;
4129 obj = PyDict_GetItem(extension_cache, py_code);
4130 if (obj != NULL) {
4131 /* Bingo. */
4132 Py_DECREF(py_code);
4133 PDATA_APPEND(self->stack, obj, -1);
4134 return 0;
4135 }
4136
4137 /* Look up the (module_name, class_name) pair. */
4138 pair = PyDict_GetItem(inverted_registry, py_code);
4139 if (pair == NULL) {
4140 Py_DECREF(py_code);
4141 PyErr_Format(PyExc_ValueError, "unregistered extension "
4142 "code %ld", code);
4143 return -1;
4144 }
4145 /* Since the extension registry is manipulable via Python code,
Tim Petersfa05ce32003-02-04 05:20:32 +00004146 * confirm that pair is really a 2-tuple of strings.
Tim Peters2d629652003-02-04 05:06:17 +00004147 */
4148 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004149 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
4150 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
Tim Peters2d629652003-02-04 05:06:17 +00004151 Py_DECREF(py_code);
4152 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
4153 "isn't a 2-tuple of strings", code);
4154 return -1;
4155 }
4156 /* Load the object. */
4157 obj = find_class(module_name, class_name, self->find_class);
4158 if (obj == NULL) {
4159 Py_DECREF(py_code);
4160 return -1;
4161 }
4162 /* Cache code -> obj. */
4163 code = PyDict_SetItem(extension_cache, py_code, obj);
4164 Py_DECREF(py_code);
4165 if (code < 0) {
4166 Py_DECREF(obj);
4167 return -1;
4168 }
4169 PDATA_PUSH(self->stack, obj, -1);
4170 return 0;
4171}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004172
4173static int
Tim Peterscba30e22003-02-01 06:24:36 +00004174load_put(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004175{
4176 PyObject *py_str = 0, *value = 0;
4177 int len, l;
4178 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004179
Tim Peters0bc93f52003-02-02 18:29:33 +00004180 if ((l = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004181 if (l < 2) return bad_readline();
4182 if (!( len=self->stack->length )) return stackUnderflow();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004183 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004184 value=self->stack->data[len-1];
4185 l=PyDict_SetItem(self->memo, py_str, value);
4186 Py_DECREF(py_str);
4187 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004188}
4189
4190
4191static int
Tim Peterscba30e22003-02-01 06:24:36 +00004192load_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004193{
4194 PyObject *py_key = 0, *value = 0;
4195 unsigned char key;
4196 char *s;
4197 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004198
Tim Peters0bc93f52003-02-02 18:29:33 +00004199 if (self->read_func(self, &s, 1) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004200 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004201
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004202 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00004203
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004204 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4205 value=self->stack->data[len-1];
4206 len=PyDict_SetItem(self->memo, py_key, value);
4207 Py_DECREF(py_key);
4208 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004209}
4210
4211
4212static int
Tim Peterscba30e22003-02-01 06:24:36 +00004213load_long_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004214{
4215 PyObject *py_key = 0, *value = 0;
4216 long key;
4217 unsigned char c;
4218 char *s;
4219 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004220
Tim Peters0bc93f52003-02-02 18:29:33 +00004221 if (self->read_func(self, &s, 4) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004222 if (!( len=self->stack->length )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004223
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004224 c = (unsigned char)s[0];
4225 key = (long)c;
4226 c = (unsigned char)s[1];
4227 key |= (long)c << 8;
4228 c = (unsigned char)s[2];
4229 key |= (long)c << 16;
4230 c = (unsigned char)s[3];
4231 key |= (long)c << 24;
Tim Peters84e87f32001-03-17 04:50:51 +00004232
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004233 if (!( py_key = PyInt_FromLong(key))) return -1;
4234 value=self->stack->data[len-1];
4235 len=PyDict_SetItem(self->memo, py_key, value);
4236 Py_DECREF(py_key);
4237 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004238}
4239
4240
4241static int
Tim Peterscba30e22003-02-01 06:24:36 +00004242do_append(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004243{
4244 PyObject *value = 0, *list = 0, *append_method = 0;
4245 int len, i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004246
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004247 len=self->stack->length;
4248 if (!( len >= x && x > 0 )) return stackUnderflow();
4249 /* nothing to do */
Tim Peterscba30e22003-02-01 06:24:36 +00004250 if (len==x) return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004251
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004252 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004253
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004254 if (PyList_Check(list)) {
4255 PyObject *slice;
4256 int list_len;
Tim Peters84e87f32001-03-17 04:50:51 +00004257
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004258 slice=Pdata_popList(self->stack, x);
Neal Norwitzb59d08c2006-07-22 16:20:49 +00004259 if (! slice) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004260 list_len = PyList_GET_SIZE(list);
4261 i=PyList_SetSlice(list, list_len, list_len, slice);
4262 Py_DECREF(slice);
4263 return i;
4264 }
4265 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004266
Tim Peterscba30e22003-02-01 06:24:36 +00004267 if (!( append_method = PyObject_GetAttr(list, append_str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004268 return -1;
4269
4270 for (i = x; i < len; i++) {
4271 PyObject *junk;
4272
4273 value=self->stack->data[i];
4274 junk=0;
4275 ARG_TUP(self, value);
4276 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00004277 junk = PyObject_Call(append_method, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004278 NULL);
4279 FREE_ARG_TUP(self);
4280 }
4281 if (! junk) {
4282 Pdata_clear(self->stack, i+1);
4283 self->stack->length=x;
4284 Py_DECREF(append_method);
4285 return -1;
4286 }
4287 Py_DECREF(junk);
4288 }
4289 self->stack->length=x;
4290 Py_DECREF(append_method);
4291 }
4292
4293 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004294}
4295
4296
4297static int
Tim Peterscba30e22003-02-01 06:24:36 +00004298load_append(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004299{
4300 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004301}
4302
4303
4304static int
Tim Peterscba30e22003-02-01 06:24:36 +00004305load_appends(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004306{
4307 return do_append(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004308}
4309
4310
4311static int
Tim Peterscba30e22003-02-01 06:24:36 +00004312do_setitems(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004313{
4314 PyObject *value = 0, *key = 0, *dict = 0;
4315 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004316
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004317 if (!( (len=self->stack->length) >= x
4318 && x > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004319
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004320 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004321
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004322 for (i = x+1; i < len; i += 2) {
4323 key =self->stack->data[i-1];
4324 value=self->stack->data[i ];
4325 if (PyObject_SetItem(dict, key, value) < 0) {
4326 r=-1;
4327 break;
4328 }
4329 }
4330
4331 Pdata_clear(self->stack, x);
4332
4333 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004334}
4335
4336
Tim Peters84e87f32001-03-17 04:50:51 +00004337static int
Tim Peterscba30e22003-02-01 06:24:36 +00004338load_setitem(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004339{
4340 return do_setitems(self, self->stack->length - 2);
4341}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004342
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004343static int
Tim Peterscba30e22003-02-01 06:24:36 +00004344load_setitems(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004345{
4346 return do_setitems(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004347}
4348
Tim Peters84e87f32001-03-17 04:50:51 +00004349
Guido van Rossum60456fd1997-04-09 17:36:32 +00004350static int
Tim Peterscba30e22003-02-01 06:24:36 +00004351load_build(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004352{
Tim Peters080c88b2003-02-15 03:01:11 +00004353 PyObject *state, *inst, *slotstate;
4354 PyObject *__setstate__;
4355 PyObject *d_key, *d_value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004356 Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00004357 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004358
Tim Peters080c88b2003-02-15 03:01:11 +00004359 /* Stack is ... instance, state. We want to leave instance at
4360 * the stack top, possibly mutated via instance.__setstate__(state).
4361 */
4362 if (self->stack->length < 2)
4363 return stackUnderflow();
4364 PDATA_POP(self->stack, state);
4365 if (state == NULL)
4366 return -1;
4367 inst = self->stack->data[self->stack->length - 1];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004368
Tim Peters080c88b2003-02-15 03:01:11 +00004369 __setstate__ = PyObject_GetAttr(inst, __setstate___str);
4370 if (__setstate__ != NULL) {
4371 PyObject *junk = NULL;
4372
4373 /* The explicit __setstate__ is responsible for everything. */
4374 ARG_TUP(self, state);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004375 if (self->arg) {
4376 junk = PyObject_Call(__setstate__, self->arg, NULL);
4377 FREE_ARG_TUP(self);
4378 }
4379 Py_DECREF(__setstate__);
Tim Peters080c88b2003-02-15 03:01:11 +00004380 if (junk == NULL)
4381 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004382 Py_DECREF(junk);
4383 return 0;
4384 }
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00004385 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4386 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004387 PyErr_Clear();
Tim Peters080c88b2003-02-15 03:01:11 +00004388
4389 /* A default __setstate__. First see whether state embeds a
4390 * slot state dict too (a proto 2 addition).
4391 */
4392 if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
4393 PyObject *temp = state;
4394 state = PyTuple_GET_ITEM(temp, 0);
4395 slotstate = PyTuple_GET_ITEM(temp, 1);
4396 Py_INCREF(state);
4397 Py_INCREF(slotstate);
4398 Py_DECREF(temp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004399 }
Tim Peters080c88b2003-02-15 03:01:11 +00004400 else
4401 slotstate = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004402
Tim Peters080c88b2003-02-15 03:01:11 +00004403 /* Set inst.__dict__ from the state dict (if any). */
4404 if (state != Py_None) {
4405 PyObject *dict;
4406 if (! PyDict_Check(state)) {
4407 PyErr_SetString(UnpicklingError, "state is not a "
4408 "dictionary");
4409 goto finally;
4410 }
4411 dict = PyObject_GetAttr(inst, __dict___str);
4412 if (dict == NULL)
4413 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004414
Tim Peters080c88b2003-02-15 03:01:11 +00004415 i = 0;
4416 while (PyDict_Next(state, &i, &d_key, &d_value)) {
4417 if (PyObject_SetItem(dict, d_key, d_value) < 0)
4418 goto finally;
4419 }
4420 Py_DECREF(dict);
4421 }
4422
4423 /* Also set instance attributes from the slotstate dict (if any). */
4424 if (slotstate != NULL) {
4425 if (! PyDict_Check(slotstate)) {
4426 PyErr_SetString(UnpicklingError, "slot state is not "
4427 "a dictionary");
4428 goto finally;
4429 }
4430 i = 0;
4431 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
4432 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
4433 goto finally;
4434 }
4435 }
4436 res = 0;
4437
4438 finally:
4439 Py_DECREF(state);
4440 Py_XDECREF(slotstate);
4441 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004442}
4443
4444
4445static int
Tim Peterscba30e22003-02-01 06:24:36 +00004446load_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004447{
4448 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004449
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004450 /* Note that we split the (pickle.py) stack into two stacks, an
4451 object stack and a mark stack. Here we push a mark onto the
4452 mark stack.
4453 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00004454
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004455 if ((self->num_marks + 1) >= self->marks_size) {
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004456 int *marks;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004457 s=self->marks_size+20;
4458 if (s <= self->num_marks) s=self->num_marks + 1;
4459 if (self->marks == NULL)
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004460 marks=(int *)malloc(s * sizeof(int));
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004461 else
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004462 marks=(int *)realloc(self->marks,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004463 s * sizeof(int));
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004464 if (!marks) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004465 PyErr_NoMemory();
4466 return -1;
4467 }
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004468 self->marks = marks;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004469 self->marks_size = s;
4470 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004471
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004472 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004473
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004474 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004475}
4476
Guido van Rossum60456fd1997-04-09 17:36:32 +00004477static int
Tim Peterscba30e22003-02-01 06:24:36 +00004478load_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004479{
4480 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004481
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004482 PDATA_POP(self->stack, arg_tup);
4483 if (! arg_tup) return -1;
4484 PDATA_POP(self->stack, callable);
4485 if (callable) {
4486 ob = Instance_New(callable, arg_tup);
4487 Py_DECREF(callable);
4488 }
4489 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004490
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004491 if (! ob) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004492
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004493 PDATA_PUSH(self->stack, ob, -1);
4494 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004495}
Tim Peters84e87f32001-03-17 04:50:51 +00004496
Tim Peters4190fb82003-02-02 16:09:05 +00004497/* Just raises an error if we don't know the protocol specified. PROTO
4498 * is the first opcode for protocols >= 2.
4499 */
4500static int
4501load_proto(Unpicklerobject *self)
4502{
4503 int i;
4504 char *protobyte;
4505
4506 i = self->read_func(self, &protobyte, 1);
4507 if (i < 0)
4508 return -1;
4509
4510 i = calc_binint(protobyte, 1);
4511 /* No point checking for < 0, since calc_binint returns an unsigned
4512 * int when chewing on 1 byte.
4513 */
4514 assert(i >= 0);
Tim Peters8587b3c2003-02-13 15:44:41 +00004515 if (i <= HIGHEST_PROTOCOL)
Tim Peters4190fb82003-02-02 16:09:05 +00004516 return 0;
4517
4518 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4519 return -1;
4520}
4521
Guido van Rossum60456fd1997-04-09 17:36:32 +00004522static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004523load(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004524{
4525 PyObject *err = 0, *val = 0;
4526 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004527
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004528 self->num_marks = 0;
4529 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004530
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004531 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004532 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004533 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004534
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004535 switch (s[0]) {
4536 case NONE:
4537 if (load_none(self) < 0)
4538 break;
4539 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004540
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004541 case BININT:
4542 if (load_binint(self) < 0)
4543 break;
4544 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004545
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004546 case BININT1:
4547 if (load_binint1(self) < 0)
4548 break;
4549 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004550
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004551 case BININT2:
4552 if (load_binint2(self) < 0)
4553 break;
4554 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004555
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004556 case INT:
4557 if (load_int(self) < 0)
4558 break;
4559 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004560
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004561 case LONG:
4562 if (load_long(self) < 0)
4563 break;
4564 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004565
Tim Petersee1a53c2003-02-02 02:57:53 +00004566 case LONG1:
4567 if (load_counted_long(self, 1) < 0)
4568 break;
4569 continue;
4570
4571 case LONG4:
4572 if (load_counted_long(self, 4) < 0)
4573 break;
4574 continue;
4575
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004576 case FLOAT:
4577 if (load_float(self) < 0)
4578 break;
4579 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004580
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004581 case BINFLOAT:
4582 if (load_binfloat(self) < 0)
4583 break;
4584 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004585
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004586 case BINSTRING:
4587 if (load_binstring(self) < 0)
4588 break;
4589 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004590
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004591 case SHORT_BINSTRING:
4592 if (load_short_binstring(self) < 0)
4593 break;
4594 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004595
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004596 case STRING:
4597 if (load_string(self) < 0)
4598 break;
4599 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004600
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004601#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004602 case UNICODE:
4603 if (load_unicode(self) < 0)
4604 break;
4605 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004606
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004607 case BINUNICODE:
4608 if (load_binunicode(self) < 0)
4609 break;
4610 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004611#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004612
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004613 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004614 if (load_counted_tuple(self, 0) < 0)
4615 break;
4616 continue;
4617
4618 case TUPLE1:
4619 if (load_counted_tuple(self, 1) < 0)
4620 break;
4621 continue;
4622
4623 case TUPLE2:
4624 if (load_counted_tuple(self, 2) < 0)
4625 break;
4626 continue;
4627
4628 case TUPLE3:
4629 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004630 break;
4631 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004632
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004633 case TUPLE:
4634 if (load_tuple(self) < 0)
4635 break;
4636 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004637
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004638 case EMPTY_LIST:
4639 if (load_empty_list(self) < 0)
4640 break;
4641 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004642
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004643 case LIST:
4644 if (load_list(self) < 0)
4645 break;
4646 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004647
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004648 case EMPTY_DICT:
4649 if (load_empty_dict(self) < 0)
4650 break;
4651 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004652
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004653 case DICT:
4654 if (load_dict(self) < 0)
4655 break;
4656 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004657
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004658 case OBJ:
4659 if (load_obj(self) < 0)
4660 break;
4661 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004662
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004663 case INST:
4664 if (load_inst(self) < 0)
4665 break;
4666 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004667
Tim Peterseab7db32003-02-13 18:24:14 +00004668 case NEWOBJ:
4669 if (load_newobj(self) < 0)
4670 break;
4671 continue;
4672
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004673 case GLOBAL:
4674 if (load_global(self) < 0)
4675 break;
4676 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004677
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004678 case APPEND:
4679 if (load_append(self) < 0)
4680 break;
4681 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004682
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004683 case APPENDS:
4684 if (load_appends(self) < 0)
4685 break;
4686 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004687
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004688 case BUILD:
4689 if (load_build(self) < 0)
4690 break;
4691 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004692
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004693 case DUP:
4694 if (load_dup(self) < 0)
4695 break;
4696 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004697
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004698 case BINGET:
4699 if (load_binget(self) < 0)
4700 break;
4701 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004702
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004703 case LONG_BINGET:
4704 if (load_long_binget(self) < 0)
4705 break;
4706 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004707
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004708 case GET:
4709 if (load_get(self) < 0)
4710 break;
4711 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004712
Tim Peters2d629652003-02-04 05:06:17 +00004713 case EXT1:
4714 if (load_extension(self, 1) < 0)
4715 break;
4716 continue;
4717
4718 case EXT2:
4719 if (load_extension(self, 2) < 0)
4720 break;
4721 continue;
4722
4723 case EXT4:
4724 if (load_extension(self, 4) < 0)
4725 break;
4726 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004727 case MARK:
4728 if (load_mark(self) < 0)
4729 break;
4730 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004731
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004732 case BINPUT:
4733 if (load_binput(self) < 0)
4734 break;
4735 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004736
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004737 case LONG_BINPUT:
4738 if (load_long_binput(self) < 0)
4739 break;
4740 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004741
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004742 case PUT:
4743 if (load_put(self) < 0)
4744 break;
4745 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004746
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004747 case POP:
4748 if (load_pop(self) < 0)
4749 break;
4750 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004751
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004752 case POP_MARK:
4753 if (load_pop_mark(self) < 0)
4754 break;
4755 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004756
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004757 case SETITEM:
4758 if (load_setitem(self) < 0)
4759 break;
4760 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004761
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004762 case SETITEMS:
4763 if (load_setitems(self) < 0)
4764 break;
4765 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004766
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004767 case STOP:
4768 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004769
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004770 case PERSID:
4771 if (load_persid(self) < 0)
4772 break;
4773 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004774
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004775 case BINPERSID:
4776 if (load_binpersid(self) < 0)
4777 break;
4778 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004779
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004780 case REDUCE:
4781 if (load_reduce(self) < 0)
4782 break;
4783 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004784
Tim Peters4190fb82003-02-02 16:09:05 +00004785 case PROTO:
4786 if (load_proto(self) < 0)
4787 break;
4788 continue;
4789
Tim Peters3c67d792003-02-02 17:59:11 +00004790 case NEWTRUE:
4791 if (load_bool(self, Py_True) < 0)
4792 break;
4793 continue;
4794
4795 case NEWFALSE:
4796 if (load_bool(self, Py_False) < 0)
4797 break;
4798 continue;
4799
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004800 case '\0':
4801 /* end of file */
4802 PyErr_SetNone(PyExc_EOFError);
4803 break;
Tim Peterscba30e22003-02-01 06:24:36 +00004804
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004805 default:
Tim Peterscba30e22003-02-01 06:24:36 +00004806 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004807 "invalid load key, '%s'.",
4808 "c", s[0]);
4809 return NULL;
4810 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004811
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004812 break;
4813 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004814
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004815 if ((err = PyErr_Occurred())) {
4816 if (err == PyExc_EOFError) {
4817 PyErr_SetNone(PyExc_EOFError);
4818 }
4819 return NULL;
4820 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004821
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004822 PDATA_POP(self->stack, val);
4823 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004824}
Tim Peters84e87f32001-03-17 04:50:51 +00004825
Guido van Rossum60456fd1997-04-09 17:36:32 +00004826
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004827/* No-load functions to support noload, which is used to
4828 find persistent references. */
4829
4830static int
Tim Peterscba30e22003-02-01 06:24:36 +00004831noload_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004832{
4833 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004834
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004835 if ((i = marker(self)) < 0) return -1;
4836 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004837}
4838
4839
4840static int
Tim Peterscba30e22003-02-01 06:24:36 +00004841noload_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004842{
4843 int i;
4844 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004845
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004846 if ((i = marker(self)) < 0) return -1;
4847 Pdata_clear(self->stack, i);
Tim Peters0bc93f52003-02-02 18:29:33 +00004848 if (self->readline_func(self, &s) < 0) return -1;
4849 if (self->readline_func(self, &s) < 0) return -1;
Tim Peterseab7db32003-02-13 18:24:14 +00004850 PDATA_APPEND(self->stack, Py_None, -1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004851 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004852}
4853
4854static int
Tim Peterseab7db32003-02-13 18:24:14 +00004855noload_newobj(Unpicklerobject *self)
4856{
4857 PyObject *obj;
4858
4859 PDATA_POP(self->stack, obj); /* pop argtuple */
4860 if (obj == NULL) return -1;
4861 Py_DECREF(obj);
4862
4863 PDATA_POP(self->stack, obj); /* pop cls */
4864 if (obj == NULL) return -1;
4865 Py_DECREF(obj);
4866
4867 PDATA_APPEND(self->stack, Py_None, -1);
4868 return 0;
4869}
4870
4871static int
Tim Peterscba30e22003-02-01 06:24:36 +00004872noload_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004873{
4874 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004875
Tim Peters0bc93f52003-02-02 18:29:33 +00004876 if (self->readline_func(self, &s) < 0) return -1;
4877 if (self->readline_func(self, &s) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004878 PDATA_APPEND(self->stack, Py_None,-1);
4879 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004880}
4881
4882static int
Tim Peterscba30e22003-02-01 06:24:36 +00004883noload_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004884{
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004885
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004886 if (self->stack->length < 2) return stackUnderflow();
4887 Pdata_clear(self->stack, self->stack->length-2);
4888 PDATA_APPEND(self->stack, Py_None,-1);
4889 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004890}
4891
4892static int
4893noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004894
Guido van Rossum053b8df1998-11-25 16:18:00 +00004895 if (self->stack->length < 1) return stackUnderflow();
4896 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00004897 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004898}
4899
Tim Peters2d629652003-02-04 05:06:17 +00004900static int
4901noload_extension(Unpicklerobject *self, int nbytes)
4902{
4903 char *codebytes;
4904
4905 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4906 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4907 PDATA_APPEND(self->stack, Py_None, -1);
4908 return 0;
4909}
4910
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004911
4912static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004913noload(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004914{
4915 PyObject *err = 0, *val = 0;
4916 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004917
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004918 self->num_marks = 0;
4919 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004920
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004921 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004922 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004923 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004924
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004925 switch (s[0]) {
4926 case NONE:
4927 if (load_none(self) < 0)
4928 break;
4929 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004930
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004931 case BININT:
4932 if (load_binint(self) < 0)
4933 break;
4934 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004935
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004936 case BININT1:
4937 if (load_binint1(self) < 0)
4938 break;
4939 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004940
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004941 case BININT2:
4942 if (load_binint2(self) < 0)
4943 break;
4944 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004945
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004946 case INT:
4947 if (load_int(self) < 0)
4948 break;
4949 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004950
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004951 case LONG:
4952 if (load_long(self) < 0)
4953 break;
4954 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004955
Tim Peters4190fb82003-02-02 16:09:05 +00004956 case LONG1:
4957 if (load_counted_long(self, 1) < 0)
4958 break;
4959 continue;
4960
4961 case LONG4:
4962 if (load_counted_long(self, 4) < 0)
4963 break;
4964 continue;
4965
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004966 case FLOAT:
4967 if (load_float(self) < 0)
4968 break;
4969 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004970
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004971 case BINFLOAT:
4972 if (load_binfloat(self) < 0)
4973 break;
4974 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004975
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004976 case BINSTRING:
4977 if (load_binstring(self) < 0)
4978 break;
4979 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004980
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004981 case SHORT_BINSTRING:
4982 if (load_short_binstring(self) < 0)
4983 break;
4984 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004985
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004986 case STRING:
4987 if (load_string(self) < 0)
4988 break;
4989 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004990
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004991#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004992 case UNICODE:
4993 if (load_unicode(self) < 0)
4994 break;
4995 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004996
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004997 case BINUNICODE:
4998 if (load_binunicode(self) < 0)
4999 break;
5000 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00005001#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00005002
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005003 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00005004 if (load_counted_tuple(self, 0) < 0)
5005 break;
5006 continue;
5007
5008 case TUPLE1:
5009 if (load_counted_tuple(self, 1) < 0)
5010 break;
5011 continue;
5012
5013 case TUPLE2:
5014 if (load_counted_tuple(self, 2) < 0)
5015 break;
5016 continue;
5017
5018 case TUPLE3:
5019 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005020 break;
5021 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005022
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005023 case TUPLE:
5024 if (load_tuple(self) < 0)
5025 break;
5026 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005027
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005028 case EMPTY_LIST:
5029 if (load_empty_list(self) < 0)
5030 break;
5031 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005032
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005033 case LIST:
5034 if (load_list(self) < 0)
5035 break;
5036 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005037
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005038 case EMPTY_DICT:
5039 if (load_empty_dict(self) < 0)
5040 break;
5041 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005042
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005043 case DICT:
5044 if (load_dict(self) < 0)
5045 break;
5046 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005047
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005048 case OBJ:
5049 if (noload_obj(self) < 0)
5050 break;
5051 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005052
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005053 case INST:
5054 if (noload_inst(self) < 0)
5055 break;
5056 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005057
Tim Peterseab7db32003-02-13 18:24:14 +00005058 case NEWOBJ:
5059 if (noload_newobj(self) < 0)
5060 break;
5061 continue;
5062
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005063 case GLOBAL:
5064 if (noload_global(self) < 0)
5065 break;
5066 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005067
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005068 case APPEND:
5069 if (load_append(self) < 0)
5070 break;
5071 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005072
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005073 case APPENDS:
5074 if (load_appends(self) < 0)
5075 break;
5076 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005077
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005078 case BUILD:
5079 if (noload_build(self) < 0)
5080 break;
5081 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005082
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005083 case DUP:
5084 if (load_dup(self) < 0)
5085 break;
5086 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005087
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005088 case BINGET:
5089 if (load_binget(self) < 0)
5090 break;
5091 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005092
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005093 case LONG_BINGET:
5094 if (load_long_binget(self) < 0)
5095 break;
5096 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005097
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005098 case GET:
5099 if (load_get(self) < 0)
5100 break;
5101 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005102
Tim Peters2d629652003-02-04 05:06:17 +00005103 case EXT1:
5104 if (noload_extension(self, 1) < 0)
5105 break;
5106 continue;
5107
5108 case EXT2:
5109 if (noload_extension(self, 2) < 0)
5110 break;
5111 continue;
5112
5113 case EXT4:
5114 if (noload_extension(self, 4) < 0)
5115 break;
5116 continue;
5117
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005118 case MARK:
5119 if (load_mark(self) < 0)
5120 break;
5121 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005122
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005123 case BINPUT:
5124 if (load_binput(self) < 0)
5125 break;
5126 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005127
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005128 case LONG_BINPUT:
5129 if (load_long_binput(self) < 0)
5130 break;
5131 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005132
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005133 case PUT:
5134 if (load_put(self) < 0)
5135 break;
5136 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005137
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005138 case POP:
5139 if (load_pop(self) < 0)
5140 break;
5141 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005142
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005143 case POP_MARK:
5144 if (load_pop_mark(self) < 0)
5145 break;
5146 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005147
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005148 case SETITEM:
5149 if (load_setitem(self) < 0)
5150 break;
5151 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005152
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005153 case SETITEMS:
5154 if (load_setitems(self) < 0)
5155 break;
5156 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005157
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005158 case STOP:
5159 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005160
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005161 case PERSID:
5162 if (load_persid(self) < 0)
5163 break;
5164 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005165
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005166 case BINPERSID:
5167 if (load_binpersid(self) < 0)
5168 break;
5169 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005170
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005171 case REDUCE:
5172 if (noload_reduce(self) < 0)
5173 break;
5174 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005175
Tim Peters4190fb82003-02-02 16:09:05 +00005176 case PROTO:
5177 if (load_proto(self) < 0)
5178 break;
5179 continue;
5180
Tim Peters3c67d792003-02-02 17:59:11 +00005181 case NEWTRUE:
5182 if (load_bool(self, Py_True) < 0)
5183 break;
5184 continue;
5185
5186 case NEWFALSE:
5187 if (load_bool(self, Py_False) < 0)
5188 break;
5189 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005190 default:
Tim Peterscba30e22003-02-01 06:24:36 +00005191 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005192 "invalid load key, '%s'.",
5193 "c", s[0]);
5194 return NULL;
5195 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005196
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005197 break;
5198 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005199
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005200 if ((err = PyErr_Occurred())) {
5201 if (err == PyExc_EOFError) {
5202 PyErr_SetNone(PyExc_EOFError);
5203 }
5204 return NULL;
5205 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005206
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005207 PDATA_POP(self->stack, val);
5208 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005209}
Tim Peters84e87f32001-03-17 04:50:51 +00005210
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005211
Guido van Rossum60456fd1997-04-09 17:36:32 +00005212static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005213Unpickler_load(Unpicklerobject *self, PyObject *unused)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005214{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005215 return load(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005216}
5217
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005218static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005219Unpickler_noload(Unpicklerobject *self, PyObject *unused)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005220{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005221 return noload(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005222}
5223
Guido van Rossum60456fd1997-04-09 17:36:32 +00005224
5225static struct PyMethodDef Unpickler_methods[] = {
Georg Brandl96a8c392006-05-29 21:04:52 +00005226 {"load", (PyCFunction)Unpickler_load, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005227 PyDoc_STR("load() -- Load a pickle")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005228 },
Georg Brandl96a8c392006-05-29 21:04:52 +00005229 {"noload", (PyCFunction)Unpickler_noload, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005230 PyDoc_STR(
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005231 "noload() -- not load a pickle, but go through most of the motions\n"
5232 "\n"
5233 "This function can be used to read past a pickle without instantiating\n"
5234 "any objects or importing any modules. It can also be used to find all\n"
5235 "persistent references without instantiating any objects or importing\n"
Neal Norwitz200788c2002-08-13 22:20:41 +00005236 "any modules.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005237 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00005238 {NULL, NULL} /* sentinel */
5239};
5240
5241
5242static Unpicklerobject *
Tim Peterscba30e22003-02-01 06:24:36 +00005243newUnpicklerobject(PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005244{
5245 Unpicklerobject *self;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005246
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005247 if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005248 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005249
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005250 self->file = NULL;
5251 self->arg = NULL;
5252 self->stack = (Pdata*)Pdata_New();
5253 self->pers_func = NULL;
5254 self->last_string = NULL;
5255 self->marks = NULL;
5256 self->num_marks = 0;
5257 self->marks_size = 0;
5258 self->buf_size = 0;
5259 self->read = NULL;
5260 self->readline = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005261 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005262
Tim Peterscba30e22003-02-01 06:24:36 +00005263 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005264 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005265
Neal Norwitzb59d08c2006-07-22 16:20:49 +00005266 if (!self->stack)
5267 goto err;
5268
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005269 Py_INCREF(f);
5270 self->file = f;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005271
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005272 /* Set read, readline based on type of f */
5273 if (PyFile_Check(f)) {
5274 self->fp = PyFile_AsFile(f);
5275 if (self->fp == NULL) {
Tim Peterscba30e22003-02-01 06:24:36 +00005276 PyErr_SetString(PyExc_ValueError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005277 "I/O operation on closed file");
5278 goto err;
5279 }
5280 self->read_func = read_file;
5281 self->readline_func = readline_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00005282 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005283 else if (PycStringIO_InputCheck(f)) {
5284 self->fp = NULL;
5285 self->read_func = read_cStringIO;
5286 self->readline_func = readline_cStringIO;
5287 }
5288 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00005289
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005290 self->fp = NULL;
5291 self->read_func = read_other;
5292 self->readline_func = readline_other;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005293
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005294 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5295 (self->read = PyObject_GetAttr(f, read_str)))) {
5296 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00005297 PyErr_SetString( PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005298 "argument must have 'read' and "
5299 "'readline' attributes" );
5300 goto err;
5301 }
5302 }
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005303 PyObject_GC_Track(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005304
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005305 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005306
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005307 err:
5308 Py_DECREF((PyObject *)self);
5309 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005310}
5311
5312
5313static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005314get_Unpickler(PyObject *self, PyObject *file)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005315{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005316 return (PyObject *)newUnpicklerobject(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005317}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005318
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005319
Guido van Rossum60456fd1997-04-09 17:36:32 +00005320static void
Tim Peterscba30e22003-02-01 06:24:36 +00005321Unpickler_dealloc(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005322{
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005323 PyObject_GC_UnTrack((PyObject *)self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005324 Py_XDECREF(self->readline);
5325 Py_XDECREF(self->read);
5326 Py_XDECREF(self->file);
5327 Py_XDECREF(self->memo);
5328 Py_XDECREF(self->stack);
5329 Py_XDECREF(self->pers_func);
5330 Py_XDECREF(self->arg);
5331 Py_XDECREF(self->last_string);
Jeremy Hyltonfff093f2003-07-11 19:42:49 +00005332 Py_XDECREF(self->find_class);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005333
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005334 if (self->marks) {
5335 free(self->marks);
5336 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005337
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005338 if (self->buf_size) {
5339 free(self->buf);
5340 }
Tim Peters84e87f32001-03-17 04:50:51 +00005341
Christian Heimese93237d2007-12-19 02:37:44 +00005342 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005343}
5344
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005345static int
5346Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg)
5347{
Thomas Woutersc6e55062006-04-15 21:47:09 +00005348 Py_VISIT(self->readline);
5349 Py_VISIT(self->read);
5350 Py_VISIT(self->file);
5351 Py_VISIT(self->memo);
5352 Py_VISIT(self->stack);
5353 Py_VISIT(self->pers_func);
5354 Py_VISIT(self->arg);
5355 Py_VISIT(self->last_string);
5356 Py_VISIT(self->find_class);
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005357 return 0;
5358}
5359
5360static int
5361Unpickler_clear(Unpicklerobject *self)
5362{
Thomas Woutersedf17d82006-04-15 17:28:34 +00005363 Py_CLEAR(self->readline);
5364 Py_CLEAR(self->read);
5365 Py_CLEAR(self->file);
5366 Py_CLEAR(self->memo);
5367 Py_CLEAR(self->stack);
5368 Py_CLEAR(self->pers_func);
5369 Py_CLEAR(self->arg);
5370 Py_CLEAR(self->last_string);
5371 Py_CLEAR(self->find_class);
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005372 return 0;
5373}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005374
5375static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005376Unpickler_getattr(Unpicklerobject *self, char *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005377{
5378 if (!strcmp(name, "persistent_load")) {
5379 if (!self->pers_func) {
5380 PyErr_SetString(PyExc_AttributeError, name);
5381 return NULL;
5382 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005383
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005384 Py_INCREF(self->pers_func);
5385 return self->pers_func;
5386 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005387
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005388 if (!strcmp(name, "find_global")) {
5389 if (!self->find_class) {
5390 PyErr_SetString(PyExc_AttributeError, name);
5391 return NULL;
5392 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005393
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005394 Py_INCREF(self->find_class);
5395 return self->find_class;
5396 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005397
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005398 if (!strcmp(name, "memo")) {
5399 if (!self->memo) {
5400 PyErr_SetString(PyExc_AttributeError, name);
5401 return NULL;
5402 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005403
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005404 Py_INCREF(self->memo);
5405 return self->memo;
5406 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005407
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005408 if (!strcmp(name, "UnpicklingError")) {
5409 Py_INCREF(UnpicklingError);
5410 return UnpicklingError;
5411 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005412
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005413 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005414}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005415
Guido van Rossum60456fd1997-04-09 17:36:32 +00005416
5417static int
Tim Peterscba30e22003-02-01 06:24:36 +00005418Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005419{
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005420
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005421 if (!strcmp(name, "persistent_load")) {
5422 Py_XDECREF(self->pers_func);
5423 self->pers_func = value;
5424 Py_XINCREF(value);
5425 return 0;
5426 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005427
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005428 if (!strcmp(name, "find_global")) {
5429 Py_XDECREF(self->find_class);
5430 self->find_class = value;
5431 Py_XINCREF(value);
5432 return 0;
5433 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005434
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005435 if (! value) {
5436 PyErr_SetString(PyExc_TypeError,
5437 "attribute deletion is not supported");
5438 return -1;
5439 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005440
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005441 if (strcmp(name, "memo") == 0) {
5442 if (!PyDict_Check(value)) {
Tim Peterscba30e22003-02-01 06:24:36 +00005443 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005444 "memo must be a dictionary");
5445 return -1;
5446 }
5447 Py_XDECREF(self->memo);
5448 self->memo = value;
5449 Py_INCREF(value);
5450 return 0;
5451 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005452
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005453 PyErr_SetString(PyExc_AttributeError, name);
5454 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005455}
5456
Tim Peters5bd2a792003-02-01 16:45:06 +00005457/* ---------------------------------------------------------------------------
5458 * Module-level functions.
5459 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005460
Martin v. Löwis544f1192004-07-27 05:22:33 +00005461/* dump(obj, file, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005462static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005463cpm_dump(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005464{
Martin v. Löwis15e62742006-02-27 16:46:16 +00005465 static char *kwlist[] = {"obj", "file", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005466 PyObject *ob, *file, *res = NULL;
5467 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005468 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005469
Martin v. Löwis544f1192004-07-27 05:22:33 +00005470 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist,
5471 &ob, &file, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005472 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005473
Tim Peters5bd2a792003-02-01 16:45:06 +00005474 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005475 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005476
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005477 if (dump(pickler, ob) < 0)
5478 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005479
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005480 Py_INCREF(Py_None);
5481 res = Py_None;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005482
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005483 finally:
5484 Py_XDECREF(pickler);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005485
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005486 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005487}
5488
5489
Martin v. Löwis544f1192004-07-27 05:22:33 +00005490/* dumps(obj, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005491static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005492cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005493{
Martin v. Löwis15e62742006-02-27 16:46:16 +00005494 static char *kwlist[] = {"obj", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005495 PyObject *ob, *file = 0, *res = NULL;
5496 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005497 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005498
Martin v. Löwis544f1192004-07-27 05:22:33 +00005499 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist,
5500 &ob, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005501 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005502
Tim Peterscba30e22003-02-01 06:24:36 +00005503 if (!( file = PycStringIO->NewOutput(128)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005504 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005505
Tim Peters5bd2a792003-02-01 16:45:06 +00005506 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005507 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005508
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005509 if (dump(pickler, ob) < 0)
5510 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005511
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005512 res = PycStringIO->cgetvalue(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005513
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005514 finally:
5515 Py_XDECREF(pickler);
5516 Py_XDECREF(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005517
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005518 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00005519}
5520
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005521
Tim Peters5bd2a792003-02-01 16:45:06 +00005522/* load(fileobj). */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005523static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005524cpm_load(PyObject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005525{
5526 Unpicklerobject *unpickler = 0;
Georg Brandl96a8c392006-05-29 21:04:52 +00005527 PyObject *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005528
Tim Peterscba30e22003-02-01 06:24:36 +00005529 if (!( unpickler = newUnpicklerobject(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005530 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005531
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005532 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005533
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005534 finally:
5535 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005536
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005537 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005538}
5539
5540
Tim Peters5bd2a792003-02-01 16:45:06 +00005541/* loads(string) */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005542static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005543cpm_loads(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005544{
5545 PyObject *ob, *file = 0, *res = NULL;
5546 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005547
Tim Peterscba30e22003-02-01 06:24:36 +00005548 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005549 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005550
Tim Peterscba30e22003-02-01 06:24:36 +00005551 if (!( file = PycStringIO->NewInput(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005552 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00005553
Tim Peterscba30e22003-02-01 06:24:36 +00005554 if (!( unpickler = newUnpicklerobject(file)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005555 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005556
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005557 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005558
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005559 finally:
5560 Py_XDECREF(file);
5561 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005562
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005563 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005564}
5565
5566
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005567PyDoc_STRVAR(Unpicklertype__doc__,
5568"Objects that know how to unpickle");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005569
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005570static PyTypeObject Unpicklertype = {
Martin v. Löwis68192102007-07-21 06:55:02 +00005571 PyVarObject_HEAD_INIT(NULL, 0)
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005572 "cPickle.Unpickler", /*tp_name*/
5573 sizeof(Unpicklerobject), /*tp_basicsize*/
5574 0,
5575 (destructor)Unpickler_dealloc, /* tp_dealloc */
5576 0, /* tp_print */
5577 (getattrfunc)Unpickler_getattr, /* tp_getattr */
5578 (setattrfunc)Unpickler_setattr, /* tp_setattr */
5579 0, /* tp_compare */
5580 0, /* tp_repr */
5581 0, /* tp_as_number */
5582 0, /* tp_as_sequence */
5583 0, /* tp_as_mapping */
5584 0, /* tp_hash */
5585 0, /* tp_call */
5586 0, /* tp_str */
5587 0, /* tp_getattro */
5588 0, /* tp_setattro */
5589 0, /* tp_as_buffer */
5590 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
5591 Unpicklertype__doc__, /* tp_doc */
5592 (traverseproc)Unpickler_traverse, /* tp_traverse */
5593 (inquiry)Unpickler_clear, /* tp_clear */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005594};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005595
Guido van Rossum60456fd1997-04-09 17:36:32 +00005596static struct PyMethodDef cPickle_methods[] = {
Martin v. Löwis544f1192004-07-27 05:22:33 +00005597 {"dump", (PyCFunction)cpm_dump, METH_VARARGS | METH_KEYWORDS,
5598 PyDoc_STR("dump(obj, file, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005599 "Write an object in pickle format to the given file.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005600 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005601 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005602 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005603
Martin v. Löwis544f1192004-07-27 05:22:33 +00005604 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS | METH_KEYWORDS,
5605 PyDoc_STR("dumps(obj, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005606 "Return a string containing an object in pickle format.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005607 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005608 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005609 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005610
Georg Brandl96a8c392006-05-29 21:04:52 +00005611 {"load", (PyCFunction)cpm_load, METH_O,
Neal Norwitz200788c2002-08-13 22:20:41 +00005612 PyDoc_STR("load(file) -- Load a pickle from the given file")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005613
Neal Norwitzb0493252002-03-31 14:44:22 +00005614 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005615 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005616
Martin v. Löwis544f1192004-07-27 05:22:33 +00005617 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS | METH_KEYWORDS,
5618 PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005619 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005620 "This takes a file-like object for writing a pickle data stream.\n"
5621 "The optional proto argument tells the pickler to use the given\n"
5622 "protocol; supported protocols are 0, 1, 2. The default\n"
5623 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5624 "only protocol that can be written to a file opened in text\n"
5625 "mode and read back successfully. When using a protocol higher\n"
5626 "than 0, make sure the file is opened in binary mode, both when\n"
5627 "pickling and unpickling.)\n"
5628 "\n"
5629 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5630 "more efficient than protocol 1.\n"
5631 "\n"
5632 "Specifying a negative protocol version selects the highest\n"
5633 "protocol version supported. The higher the protocol used, the\n"
5634 "more recent the version of Python needed to read the pickle\n"
5635 "produced.\n"
5636 "\n"
5637 "The file parameter must have a write() method that accepts a single\n"
5638 "string argument. It can thus be an open file object, a StringIO\n"
5639 "object, or any other custom object that meets this interface.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005640 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005641
Georg Brandl96a8c392006-05-29 21:04:52 +00005642 {"Unpickler", (PyCFunction)get_Unpickler, METH_O,
Tim Peters5bd2a792003-02-01 16:45:06 +00005643 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5644
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005645 { NULL, NULL }
5646};
5647
Guido van Rossum60456fd1997-04-09 17:36:32 +00005648static int
Tim Peterscba30e22003-02-01 06:24:36 +00005649init_stuff(PyObject *module_dict)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005650{
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005651 PyObject *copyreg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005652
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005653#define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005654
Tim Peters3cfe7542003-05-21 21:29:48 +00005655 if (PyType_Ready(&Unpicklertype) < 0)
5656 return -1;
5657 if (PyType_Ready(&Picklertype) < 0)
5658 return -1;
5659
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005660 INIT_STR(__class__);
5661 INIT_STR(__getinitargs__);
5662 INIT_STR(__dict__);
5663 INIT_STR(__getstate__);
5664 INIT_STR(__setstate__);
5665 INIT_STR(__name__);
5666 INIT_STR(__main__);
5667 INIT_STR(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00005668 INIT_STR(__reduce_ex__);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005669 INIT_STR(write);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005670 INIT_STR(append);
5671 INIT_STR(read);
5672 INIT_STR(readline);
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005673 INIT_STR(copyreg);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005674 INIT_STR(dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005675
Georg Brandldffbf5f2008-05-20 07:49:57 +00005676 if (!( copyreg = PyImport_ImportModule("copy_reg")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005677 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005678
Tim Peters1f1b2d22003-02-01 02:16:37 +00005679 /* This is special because we want to use a different
5680 one in restricted mode. */
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005681 dispatch_table = PyObject_GetAttr(copyreg, dispatch_table_str);
Tim Peters5b7da392003-02-04 00:21:07 +00005682 if (!dispatch_table) return -1;
5683
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005684 extension_registry = PyObject_GetAttrString(copyreg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005685 "_extension_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005686 if (!extension_registry) return -1;
5687
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005688 inverted_registry = PyObject_GetAttrString(copyreg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005689 "_inverted_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005690 if (!inverted_registry) return -1;
5691
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005692 extension_cache = PyObject_GetAttrString(copyreg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005693 "_extension_cache");
Tim Peters5b7da392003-02-04 00:21:07 +00005694 if (!extension_cache) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005695
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005696 Py_DECREF(copyreg);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005697
Tim Peters731098b2003-02-04 20:56:09 +00005698 if (!(empty_tuple = PyTuple_New(0)))
5699 return -1;
5700
5701 two_tuple = PyTuple_New(2);
5702 if (two_tuple == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005703 return -1;
Tim Peters3e667d52003-02-04 21:47:44 +00005704 /* We use this temp container with no regard to refcounts, or to
5705 * keeping containees alive. Exempt from GC, because we don't
5706 * want anything looking at two_tuple() by magic.
5707 */
5708 PyObject_GC_UnTrack(two_tuple);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005709
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005710 /* Ugh */
5711 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5712 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5713 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005714
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005715 if (!( t=PyDict_New())) return -1;
5716 if (!( r=PyRun_String(
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005717 "def __str__(self):\n"
5718 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5719 Py_file_input,
5720 module_dict, t) )) return -1;
5721 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005722
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005723 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
Tim Peterscba30e22003-02-01 06:24:36 +00005724 if (!PickleError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005725 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005726
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005727 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005728
Tim Peterscba30e22003-02-01 06:24:36 +00005729 PicklingError = PyErr_NewException("cPickle.PicklingError",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005730 PickleError, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00005731 if (!PicklingError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005732 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005733
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005734 if (!( t=PyDict_New())) return -1;
5735 if (!( r=PyRun_String(
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005736 "def __str__(self):\n"
5737 " a=self.args\n"
5738 " a=a and type(a[0]) or '(what)'\n"
5739 " return 'Cannot pickle %s objects' % a\n"
5740 , Py_file_input,
5741 module_dict, t) )) return -1;
5742 Py_DECREF(r);
Tim Peters84e87f32001-03-17 04:50:51 +00005743
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005744 if (!( UnpickleableError = PyErr_NewException(
Tim Peterscba30e22003-02-01 06:24:36 +00005745 "cPickle.UnpickleableError", PicklingError, t)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005746 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005747
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005748 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005749
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005750 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
Tim Peterscba30e22003-02-01 06:24:36 +00005751 PickleError, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005752 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005753
Martin v. Löwis658009a2002-09-16 17:26:24 +00005754 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5755 UnpicklingError, NULL)))
5756 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +00005757
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005758 if (PyDict_SetItemString(module_dict, "PickleError",
5759 PickleError) < 0)
5760 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005761
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005762 if (PyDict_SetItemString(module_dict, "PicklingError",
5763 PicklingError) < 0)
5764 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005765
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005766 if (PyDict_SetItemString(module_dict, "UnpicklingError",
5767 UnpicklingError) < 0)
5768 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005769
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005770 if (PyDict_SetItemString(module_dict, "UnpickleableError",
5771 UnpickleableError) < 0)
5772 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005773
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005774 if (PyDict_SetItemString(module_dict, "BadPickleGet",
5775 BadPickleGet) < 0)
5776 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005777
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005778 PycString_IMPORT;
Guido van Rossum053b8df1998-11-25 16:18:00 +00005779
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005780 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005781}
5782
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005783#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5784#define PyMODINIT_FUNC void
Guido van Rossumf9ffb031999-02-04 14:54:04 +00005785#endif
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005786PyMODINIT_FUNC
Tim Peterscba30e22003-02-01 06:24:36 +00005787initcPickle(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005788{
5789 PyObject *m, *d, *di, *v, *k;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005790 Py_ssize_t i;
Tim Peters5b7da392003-02-04 00:21:07 +00005791 char *rev = "1.71"; /* XXX when does this change? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005792 PyObject *format_version;
5793 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005794
Alexandre Vassalotti3c4971c2008-05-16 19:14:31 +00005795 /* XXX: Should mention that the pickle module will include the C
5796 XXX: optimized implementation automatically. */
5797 if (PyErr_WarnPy3k("the cPickle module has been removed in "
5798 "Python 3.0", 2) < 0)
5799 return;
5800
Christian Heimese93237d2007-12-19 02:37:44 +00005801 Py_TYPE(&Picklertype) = &PyType_Type;
5802 Py_TYPE(&Unpicklertype) = &PyType_Type;
5803 Py_TYPE(&PdataType) = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005804
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005805 /* Initialize some pieces. We need to do this before module creation,
Tim Peters5b7da392003-02-04 00:21:07 +00005806 * so we're forced to use a temporary dictionary. :(
5807 */
5808 di = PyDict_New();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005809 if (!di) return;
5810 if (init_stuff(di) < 0) return;
Guido van Rossumebba4202000-09-07 14:35:37 +00005811
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005812 /* Create the module and add the functions */
5813 m = Py_InitModule4("cPickle", cPickle_methods,
5814 cPickle_module_documentation,
5815 (PyObject*)NULL,PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00005816 if (m == NULL)
5817 return;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005818
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005819 /* Add some symbolic constants to the module */
5820 d = PyModule_GetDict(m);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005821 v = PyString_FromString(rev);
Tim Peters5b7da392003-02-04 00:21:07 +00005822 PyDict_SetItemString(d, "__version__", v);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005823 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00005824
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005825 /* Copy data from di. Waaa. */
5826 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
5827 if (PyObject_SetItem(d, k, v) < 0) {
5828 Py_DECREF(di);
5829 return;
5830 }
5831 }
5832 Py_DECREF(di);
Guido van Rossumebba4202000-09-07 14:35:37 +00005833
Tim Peters8587b3c2003-02-13 15:44:41 +00005834 i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
5835 if (i < 0)
5836 return;
5837
Tim Peters5b7da392003-02-04 00:21:07 +00005838 /* These are purely informational; no code uses them. */
5839 /* File format version we write. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005840 format_version = PyString_FromString("2.0");
Tim Peters5b7da392003-02-04 00:21:07 +00005841 /* Format versions we can read. */
5842 compatible_formats = Py_BuildValue("[sssss]",
5843 "1.0", /* Original protocol 0 */
5844 "1.1", /* Protocol 0 + INST */
5845 "1.2", /* Original protocol 1 */
5846 "1.3", /* Protocol 1 + BINFLOAT */
Tim Peters0dd23aa2003-02-04 00:30:46 +00005847 "2.0"); /* Original protocol 2 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005848 PyDict_SetItemString(d, "format_version", format_version);
5849 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
5850 Py_XDECREF(format_version);
5851 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005852}