blob: 4e53ae6722714dd075b6ae82d13d660880814e87 [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/*
Georg Brandl734373c2009-01-03 21:55:17 +000021 * Note: The UNICODE macro controls the TCHAR meaning of the win32 API. Since
22 * all headers have already been included here, we can safely redefine it.
23 */
24#ifdef UNICODE
25# undef UNICODE
26#endif
27
28/*
Tim Peters797ec242003-02-01 06:22:36 +000029 * Pickle opcodes. These must be kept in synch with pickle.py. Extensive
30 * docs are in pickletools.py.
31 */
Guido van Rossum60456fd1997-04-09 17:36:32 +000032#define MARK '('
33#define STOP '.'
34#define POP '0'
35#define POP_MARK '1'
36#define DUP '2'
37#define FLOAT 'F'
Guido van Rossum60456fd1997-04-09 17:36:32 +000038#define BINFLOAT 'G'
Guido van Rossum60456fd1997-04-09 17:36:32 +000039#define INT 'I'
40#define BININT 'J'
41#define BININT1 'K'
42#define LONG 'L'
43#define BININT2 'M'
44#define NONE 'N'
45#define PERSID 'P'
46#define BINPERSID 'Q'
47#define REDUCE 'R'
48#define STRING 'S'
49#define BINSTRING 'T'
Guido van Rossum2f4caa41997-01-06 22:59:08 +000050#define SHORT_BINSTRING 'U'
Guido van Rossum5fccb7c2000-03-10 23:11:40 +000051#define UNICODE 'V'
52#define BINUNICODE 'X'
Guido van Rossum60456fd1997-04-09 17:36:32 +000053#define APPEND 'a'
54#define BUILD 'b'
55#define GLOBAL 'c'
56#define DICT 'd'
57#define EMPTY_DICT '}'
58#define APPENDS 'e'
59#define GET 'g'
60#define BINGET 'h'
61#define INST 'i'
62#define LONG_BINGET 'j'
63#define LIST 'l'
64#define EMPTY_LIST ']'
65#define OBJ 'o'
66#define PUT 'p'
67#define BINPUT 'q'
68#define LONG_BINPUT 'r'
69#define SETITEM 's'
70#define TUPLE 't'
71#define EMPTY_TUPLE ')'
72#define SETITEMS 'u'
Tim Peters797ec242003-02-01 06:22:36 +000073
74/* Protocol 2. */
75#define PROTO '\x80' /* identify pickle protocol */
76#define NEWOBJ '\x81' /* build object by applying cls.__new__ to argtuple */
77#define EXT1 '\x82' /* push object from extension registry; 1-byte index */
78#define EXT2 '\x83' /* ditto, but 2-byte index */
79#define EXT4 '\x84' /* ditto, but 4-byte index */
80#define TUPLE1 '\x85' /* build 1-tuple from stack top */
81#define TUPLE2 '\x86' /* build 2-tuple from two topmost stack items */
82#define TUPLE3 '\x87' /* build 3-tuple from three topmost stack items */
83#define NEWTRUE '\x88' /* push True */
84#define NEWFALSE '\x89' /* push False */
85#define LONG1 '\x8a' /* push long from < 256 bytes */
86#define LONG4 '\x8b' /* push really big long */
87
88/* There aren't opcodes -- they're ways to pickle bools before protocol 2,
89 * so that unpicklers written before bools were introduced unpickle them
90 * as ints, but unpicklers after can recognize that bools were intended.
91 * Note that protocol 2 added direct ways to pickle bools.
92 */
Jack Jansen3a967022002-06-26 20:40:42 +000093#undef TRUE
Guido van Rossume2763392002-04-05 19:30:08 +000094#define TRUE "I01\n"
Jack Jansen3a967022002-06-26 20:40:42 +000095#undef FALSE
Guido van Rossume2763392002-04-05 19:30:08 +000096#define FALSE "I00\n"
Guido van Rossum77f6a652002-04-03 22:41:51 +000097
Tim Peters1092d642003-02-11 21:06:20 +000098/* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
Tim Peters42f08ac2003-02-11 22:43:24 +000099 * batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
100 * break if this gets out of synch with pickle.py, but it's unclear that
101 * would help anything either.
Tim Peters1092d642003-02-11 21:06:20 +0000102 */
103#define BATCHSIZE 1000
104
Guido van Rossum60456fd1997-04-09 17:36:32 +0000105static char MARKv = MARK;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000106
Guido van Rossumc03158b1999-06-09 15:23:31 +0000107static PyObject *PickleError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000108static PyObject *PicklingError;
Guido van Rossumc03158b1999-06-09 15:23:31 +0000109static PyObject *UnpickleableError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000110static PyObject *UnpicklingError;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000111static PyObject *BadPickleGet;
112
Tim Peters5b7da392003-02-04 00:21:07 +0000113/* As the name says, an empty tuple. */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000114static PyObject *empty_tuple;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000115
Georg Brandldffbf5f2008-05-20 07:49:57 +0000116/* copy_reg.dispatch_table, {type_object: pickling_function} */
Tim Peters5b7da392003-02-04 00:21:07 +0000117static PyObject *dispatch_table;
118
119/* For EXT[124] opcodes. */
Georg Brandldffbf5f2008-05-20 07:49:57 +0000120/* copy_reg._extension_registry, {(module_name, function_name): code} */
Tim Peters5b7da392003-02-04 00:21:07 +0000121static PyObject *extension_registry;
Georg Brandldffbf5f2008-05-20 07:49:57 +0000122/* copy_reg._inverted_registry, {code: (module_name, function_name)} */
Tim Peters5b7da392003-02-04 00:21:07 +0000123static PyObject *inverted_registry;
Georg Brandldffbf5f2008-05-20 07:49:57 +0000124/* copy_reg._extension_cache, {code: object} */
Tim Peters5b7da392003-02-04 00:21:07 +0000125static PyObject *extension_cache;
126
Georg Brandldffbf5f2008-05-20 07:49:57 +0000127/* For looking up name pairs in copy_reg._extension_registry. */
Tim Peters731098b2003-02-04 20:56:09 +0000128static PyObject *two_tuple;
129
Guido van Rossum60456fd1997-04-09 17:36:32 +0000130static PyObject *__class___str, *__getinitargs___str, *__dict___str,
131 *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
Guido van Rossumb289b872003-02-19 01:45:13 +0000132 *__reduce_ex___str,
Tim Peters1f1b2d22003-02-01 02:16:37 +0000133 *write_str, *append_str,
Neal Norwitzb183a252006-04-10 01:03:32 +0000134 *read_str, *readline_str, *__main___str,
Alexandre Vassalotti513c46e2009-11-24 18:06:51 +0000135 *dispatch_table_str;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000136
Guido van Rossum053b8df1998-11-25 16:18:00 +0000137/*************************************************************************
138 Internal Data type for pickle data. */
139
140typedef struct {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000141 PyObject_HEAD
Tim Peters1d63c9f2003-02-02 20:29:39 +0000142 int length; /* number of initial slots in data currently used */
143 int size; /* number of slots in data allocated */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000144 PyObject **data;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000145} Pdata;
146
Tim Peters84e87f32001-03-17 04:50:51 +0000147static void
Tim Peterscba30e22003-02-01 06:24:36 +0000148Pdata_dealloc(Pdata *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000149{
150 int i;
151 PyObject **p;
Tim Peterscba30e22003-02-01 06:24:36 +0000152
Tim Peters1d63c9f2003-02-02 20:29:39 +0000153 for (i = self->length, p = self->data; --i >= 0; p++) {
154 Py_DECREF(*p);
155 }
156 if (self->data)
157 free(self->data);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000158 PyObject_Del(self);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000159}
160
161static PyTypeObject PdataType = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000162 PyVarObject_HEAD_INIT(NULL, 0) "cPickle.Pdata", sizeof(Pdata), 0,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000163 (destructor)Pdata_dealloc,
164 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
Guido van Rossum053b8df1998-11-25 16:18:00 +0000165};
166
Christian Heimese93237d2007-12-19 02:37:44 +0000167#define Pdata_Check(O) (Py_TYPE(O) == &PdataType)
Guido van Rossum053b8df1998-11-25 16:18:00 +0000168
169static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000170Pdata_New(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000171{
172 Pdata *self;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000173
Tim Peters1d63c9f2003-02-02 20:29:39 +0000174 if (!(self = PyObject_New(Pdata, &PdataType)))
175 return NULL;
176 self->size = 8;
177 self->length = 0;
178 self->data = malloc(self->size * sizeof(PyObject*));
179 if (self->data)
180 return (PyObject*)self;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000181 Py_DECREF(self);
182 return PyErr_NoMemory();
Guido van Rossum053b8df1998-11-25 16:18:00 +0000183}
184
Tim Peters84e87f32001-03-17 04:50:51 +0000185static int
Tim Peterscba30e22003-02-01 06:24:36 +0000186stackUnderflow(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000187{
188 PyErr_SetString(UnpicklingError, "unpickling stack underflow");
189 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000190}
191
Tim Peters1d63c9f2003-02-02 20:29:39 +0000192/* Retain only the initial clearto items. If clearto >= the current
193 * number of items, this is a (non-erroneous) NOP.
194 */
Guido van Rossum053b8df1998-11-25 16:18:00 +0000195static int
Tim Peterscba30e22003-02-01 06:24:36 +0000196Pdata_clear(Pdata *self, int clearto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000197{
198 int i;
199 PyObject **p;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000200
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000201 if (clearto < 0) return stackUnderflow();
202 if (clearto >= self->length) return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000203
Tim Peters1d63c9f2003-02-02 20:29:39 +0000204 for (i = self->length, p = self->data + clearto;
205 --i >= clearto;
206 p++) {
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000207 Py_CLEAR(*p);
Tim Peters1d63c9f2003-02-02 20:29:39 +0000208 }
209 self->length = clearto;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000210
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000211 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000212}
213
Tim Peters84e87f32001-03-17 04:50:51 +0000214static int
Tim Peterscba30e22003-02-01 06:24:36 +0000215Pdata_grow(Pdata *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000216{
Tim Peters1d63c9f2003-02-02 20:29:39 +0000217 int bigger;
218 size_t nbytes;
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000219 PyObject **tmp;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000220
Tim Peters1d63c9f2003-02-02 20:29:39 +0000221 bigger = self->size << 1;
Tim Peterse0a39072003-02-03 15:45:56 +0000222 if (bigger <= 0) /* was 0, or new value overflows */
Tim Peters1d63c9f2003-02-02 20:29:39 +0000223 goto nomemory;
224 if ((int)(size_t)bigger != bigger)
225 goto nomemory;
226 nbytes = (size_t)bigger * sizeof(PyObject *);
227 if (nbytes / sizeof(PyObject *) != (size_t)bigger)
228 goto nomemory;
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000229 tmp = realloc(self->data, nbytes);
230 if (tmp == NULL)
Tim Peters1d63c9f2003-02-02 20:29:39 +0000231 goto nomemory;
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000232 self->data = tmp;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000233 self->size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000234 return 0;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000235
236 nomemory:
Tim Peters1d63c9f2003-02-02 20:29:39 +0000237 PyErr_NoMemory();
238 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000239}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000240
Tim Peterse0a39072003-02-03 15:45:56 +0000241/* D is a Pdata*. Pop the topmost element and store it into V, which
242 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
Tim Peters1d63c9f2003-02-02 20:29:39 +0000243 * is raised and V is set to NULL. D and V may be evaluated several times.
244 */
245#define PDATA_POP(D, V) { \
Tim Peterse0a39072003-02-03 15:45:56 +0000246 if ((D)->length) \
247 (V) = (D)->data[--((D)->length)]; \
248 else { \
249 PyErr_SetString(UnpicklingError, "bad pickle data"); \
250 (V) = NULL; \
251 } \
Guido van Rossum053b8df1998-11-25 16:18:00 +0000252}
253
Tim Peterse0a39072003-02-03 15:45:56 +0000254/* PDATA_PUSH and PDATA_APPEND both push rvalue PyObject* O on to Pdata*
255 * D. If the Pdata stack can't be grown to hold the new value, both
256 * raise MemoryError and execute "return ER". The difference is in ownership
257 * of O after: _PUSH transfers ownership of O from the caller to the stack
258 * (no incref of O is done, and in case of error O is decrefed), while
259 * _APPEND pushes a new reference.
260 */
261
262/* Push O on stack D, giving ownership of O to the stack. */
263#define PDATA_PUSH(D, O, ER) { \
264 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
265 Pdata_grow((Pdata*)(D)) < 0) { \
266 Py_DECREF(O); \
267 return ER; \
268 } \
269 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
270}
271
272/* Push O on stack D, pushing a new reference. */
273#define PDATA_APPEND(D, O, ER) { \
274 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
275 Pdata_grow((Pdata*)(D)) < 0) \
276 return ER; \
277 Py_INCREF(O); \
278 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
279}
280
281
Guido van Rossum053b8df1998-11-25 16:18:00 +0000282static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000283Pdata_popTuple(Pdata *self, int start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000284{
285 PyObject *r;
286 int i, j, l;
Tim Peterscba30e22003-02-01 06:24:36 +0000287
Tim Peters1d63c9f2003-02-02 20:29:39 +0000288 l = self->length-start;
289 r = PyTuple_New(l);
290 if (r == NULL)
291 return NULL;
292 for (i = start, j = 0 ; j < l; i++, j++)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000293 PyTuple_SET_ITEM(r, j, self->data[i]);
Tim Peterscba30e22003-02-01 06:24:36 +0000294
Tim Peters1d63c9f2003-02-02 20:29:39 +0000295 self->length = start;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000296 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000297}
298
299static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000300Pdata_popList(Pdata *self, int start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000301{
302 PyObject *r;
303 int i, j, l;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000304
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000305 l=self->length-start;
306 if (!( r=PyList_New(l))) return NULL;
307 for (i=start, j=0 ; j < l; i++, j++)
308 PyList_SET_ITEM(r, j, self->data[i]);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000309
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000310 self->length=start;
311 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000312}
313
Guido van Rossum053b8df1998-11-25 16:18:00 +0000314/*************************************************************************/
315
316#define ARG_TUP(self, o) { \
317 if (self->arg || (self->arg=PyTuple_New(1))) { \
318 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
319 PyTuple_SET_ITEM(self->arg,0,o); \
320 } \
321 else { \
322 Py_DECREF(o); \
323 } \
324}
325
326#define FREE_ARG_TUP(self) { \
Christian Heimese93237d2007-12-19 02:37:44 +0000327 if (Py_REFCNT(self->arg) > 1) { \
Guido van Rossum053b8df1998-11-25 16:18:00 +0000328 Py_DECREF(self->arg); \
329 self->arg=NULL; \
330 } \
331 }
332
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000333typedef struct Picklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000334 PyObject_HEAD
335 FILE *fp;
336 PyObject *write;
337 PyObject *file;
338 PyObject *memo;
339 PyObject *arg;
340 PyObject *pers_func;
341 PyObject *inst_pers_func;
Tim Peters797ec242003-02-01 06:22:36 +0000342
343 /* pickle protocol number, >= 0 */
344 int proto;
345
346 /* bool, true if proto > 0 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000347 int bin;
Tim Peters797ec242003-02-01 06:22:36 +0000348
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000349 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000350 int (*write_func)(struct Picklerobject *, const char *, Py_ssize_t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000351 char *write_buf;
352 int buf_size;
353 PyObject *dispatch_table;
354 int fast_container; /* count nested container dumps */
355 PyObject *fast_memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000356} Picklerobject;
357
Barry Warsaw52acb492001-12-21 20:04:22 +0000358#ifndef PY_CPICKLE_FAST_LIMIT
359#define PY_CPICKLE_FAST_LIMIT 50
360#endif
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000361
Jeremy Hylton938ace62002-07-17 16:30:39 +0000362static PyTypeObject Picklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000363
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000364typedef struct Unpicklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000365 PyObject_HEAD
366 FILE *fp;
367 PyObject *file;
368 PyObject *readline;
369 PyObject *read;
370 PyObject *memo;
371 PyObject *arg;
372 Pdata *stack;
373 PyObject *mark;
374 PyObject *pers_func;
375 PyObject *last_string;
376 int *marks;
377 int num_marks;
378 int marks_size;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000379 Py_ssize_t (*read_func)(struct Unpicklerobject *, char **, Py_ssize_t);
380 Py_ssize_t (*readline_func)(struct Unpicklerobject *, char **);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000381 int buf_size;
382 char *buf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000383 PyObject *find_class;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000384} Unpicklerobject;
Tim Peters84e87f32001-03-17 04:50:51 +0000385
Jeremy Hylton938ace62002-07-17 16:30:39 +0000386static PyTypeObject Unpicklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000387
Thomas Wouters4789b3a2000-07-24 11:36:47 +0000388/* Forward decls that need the above structs */
389static int save(Picklerobject *, PyObject *, int);
390static int put2(Picklerobject *, PyObject *);
391
Guido van Rossumd385d591997-04-09 17:47:47 +0000392static
Guido van Rossum60456fd1997-04-09 17:36:32 +0000393PyObject *
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000394cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
395{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000396 va_list va;
397 PyObject *args=0, *retval=0;
398 va_start(va, format);
Tim Peterscba30e22003-02-01 06:24:36 +0000399
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000400 if (format) args = Py_VaBuildValue(format, va);
401 va_end(va);
402 if (format && ! args) return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000403 if (stringformat && !(retval=PyString_FromString(stringformat)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000404 return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000405
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000406 if (retval) {
407 if (args) {
408 PyObject *v;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000409 v=PyString_Format(retval, args);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000410 Py_DECREF(retval);
411 Py_DECREF(args);
412 if (! v) return NULL;
413 retval=v;
414 }
415 }
416 else
417 if (args) retval=args;
418 else {
419 PyErr_SetObject(ErrType,Py_None);
420 return NULL;
421 }
422 PyErr_SetObject(ErrType,retval);
423 Py_DECREF(retval);
424 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000425}
426
Tim Peters84e87f32001-03-17 04:50:51 +0000427static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000428write_file(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000429{
430 size_t nbyteswritten;
Tim Peters84e87f32001-03-17 04:50:51 +0000431
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000432 if (s == NULL) {
433 return 0;
434 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000435
Martin v. Löwis18e16552006-02-15 17:27:45 +0000436 if (n > INT_MAX) {
437 /* String too large */
438 return -1;
439 }
440
Gregory P. Smithc20adf82008-04-07 06:33:21 +0000441 PyFile_IncUseCount((PyFileObject *)self->file);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000442 Py_BEGIN_ALLOW_THREADS
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000443 nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000444 Py_END_ALLOW_THREADS
Gregory P. Smithc20adf82008-04-07 06:33:21 +0000445 PyFile_DecUseCount((PyFileObject *)self->file);
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000446 if (nbyteswritten != (size_t)n) {
447 PyErr_SetFromErrno(PyExc_IOError);
448 return -1;
449 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000450
Martin v. Löwis18e16552006-02-15 17:27:45 +0000451 return (int)n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000452}
453
Tim Peters84e87f32001-03-17 04:50:51 +0000454static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000455write_cStringIO(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000456{
457 if (s == NULL) {
458 return 0;
459 }
Tim Peterscba30e22003-02-01 06:24:36 +0000460
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000461 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
462 return -1;
463 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000464
Martin v. Löwis18e16552006-02-15 17:27:45 +0000465 return (int)n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000466}
467
Tim Peters84e87f32001-03-17 04:50:51 +0000468static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000469write_none(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000470{
471 if (s == NULL) return 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000472 if (n > INT_MAX) return -1;
473 return (int)n;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000474}
475
Tim Peters84e87f32001-03-17 04:50:51 +0000476static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000477write_other(Picklerobject *self, const char *s, Py_ssize_t _n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000478{
479 PyObject *py_str = 0, *junk = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000480 int n;
Tim Peterscba30e22003-02-01 06:24:36 +0000481
Martin v. Löwis18e16552006-02-15 17:27:45 +0000482 if (_n > INT_MAX)
483 return -1;
484 n = (int)_n;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000485 if (s == NULL) {
486 if (!( self->buf_size )) return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000487 py_str = PyString_FromStringAndSize(self->write_buf,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000488 self->buf_size);
Tim Peterscba30e22003-02-01 06:24:36 +0000489 if (!py_str)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000490 return -1;
491 }
492 else {
493 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
494 if (write_other(self, NULL, 0) < 0)
495 return -1;
496 }
Tim Peterscba30e22003-02-01 06:24:36 +0000497
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000498 if (n > WRITE_BUF_SIZE) {
499 if (!( py_str =
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000500 PyString_FromStringAndSize(s, n)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000501 return -1;
502 }
503 else {
504 memcpy(self->write_buf + self->buf_size, s, n);
505 self->buf_size += n;
506 return n;
507 }
508 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000509
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000510 if (self->write) {
511 /* object with write method */
512 ARG_TUP(self, py_str);
513 if (self->arg) {
514 junk = PyObject_Call(self->write, self->arg, NULL);
515 FREE_ARG_TUP(self);
516 }
517 if (junk) Py_DECREF(junk);
518 else return -1;
519 }
520 else
521 PDATA_PUSH(self->file, py_str, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000522
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000523 self->buf_size = 0;
524 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000525}
526
527
Martin v. Löwis18e16552006-02-15 17:27:45 +0000528static Py_ssize_t
529read_file(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000530{
531 size_t nbytesread;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000532
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000533 if (self->buf_size == 0) {
534 int size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000535
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000536 size = ((n < 32) ? 32 : n);
Tim Petersee1a53c2003-02-02 02:57:53 +0000537 if (!( self->buf = (char *)malloc(size))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000538 PyErr_NoMemory();
539 return -1;
540 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000541
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000542 self->buf_size = size;
543 }
544 else if (n > self->buf_size) {
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000545 char *newbuf = (char *)realloc(self->buf, n);
546 if (!newbuf) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000547 PyErr_NoMemory();
548 return -1;
549 }
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000550 self->buf = newbuf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000551 self->buf_size = n;
552 }
Tim Peters84e87f32001-03-17 04:50:51 +0000553
Gregory P. Smithc20adf82008-04-07 06:33:21 +0000554 PyFile_IncUseCount((PyFileObject *)self->file);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000555 Py_BEGIN_ALLOW_THREADS
556 nbytesread = fread(self->buf, sizeof(char), n, self->fp);
557 Py_END_ALLOW_THREADS
Gregory P. Smithc20adf82008-04-07 06:33:21 +0000558 PyFile_DecUseCount((PyFileObject *)self->file);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000559 if (nbytesread != (size_t)n) {
560 if (feof(self->fp)) {
561 PyErr_SetNone(PyExc_EOFError);
562 return -1;
563 }
Tim Peterscba30e22003-02-01 06:24:36 +0000564
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000565 PyErr_SetFromErrno(PyExc_IOError);
566 return -1;
567 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000568
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000569 *s = self->buf;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000570
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000571 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000572}
573
574
Martin v. Löwis18e16552006-02-15 17:27:45 +0000575static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000576readline_file(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000577{
578 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000579
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000580 if (self->buf_size == 0) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000581 if (!( self->buf = (char *)malloc(40))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000582 PyErr_NoMemory();
583 return -1;
584 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000585 self->buf_size = 40;
586 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000587
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000588 i = 0;
589 while (1) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000590 int bigger;
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000591 char *newbuf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000592 for (; i < (self->buf_size - 1); i++) {
Tim Peterscba30e22003-02-01 06:24:36 +0000593 if (feof(self->fp) ||
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000594 (self->buf[i] = getc(self->fp)) == '\n') {
595 self->buf[i + 1] = '\0';
596 *s = self->buf;
597 return i + 1;
598 }
599 }
Tim Petersee1a53c2003-02-02 02:57:53 +0000600 bigger = self->buf_size << 1;
601 if (bigger <= 0) { /* overflow */
602 PyErr_NoMemory();
603 return -1;
604 }
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000605 newbuf = (char *)realloc(self->buf, bigger);
606 if (!newbuf) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000607 PyErr_NoMemory();
608 return -1;
609 }
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000610 self->buf = newbuf;
Tim Petersee1a53c2003-02-02 02:57:53 +0000611 self->buf_size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000612 }
Tim Peters84e87f32001-03-17 04:50:51 +0000613}
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000614
615
Martin v. Löwis18e16552006-02-15 17:27:45 +0000616static Py_ssize_t
617read_cStringIO(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000618{
619 char *ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000620
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000621 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
622 PyErr_SetNone(PyExc_EOFError);
623 return -1;
624 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000625
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000626 *s = ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000627
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000628 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000629}
630
631
Martin v. Löwis18e16552006-02-15 17:27:45 +0000632static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000633readline_cStringIO(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000634{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000635 Py_ssize_t n;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000636 char *ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000637
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000638 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
639 return -1;
640 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000641
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000642 *s = ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000643
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000644 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000645}
646
647
Martin v. Löwis18e16552006-02-15 17:27:45 +0000648static Py_ssize_t
649read_other(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000650{
651 PyObject *bytes, *str=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000652
Martin v. Löwis18e16552006-02-15 17:27:45 +0000653 if (!( bytes = PyInt_FromSsize_t(n))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000654
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000655 ARG_TUP(self, bytes);
656 if (self->arg) {
657 str = PyObject_Call(self->read, self->arg, NULL);
658 FREE_ARG_TUP(self);
659 }
660 if (! str) return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000661
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000662 Py_XDECREF(self->last_string);
663 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000664
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000665 if (! (*s = PyString_AsString(str))) return -1;
Amaury Forgeot d'Arc47ccf0c2009-07-23 22:31:47 +0000666
667 if (PyString_GET_SIZE(str) != n) {
668 PyErr_SetNone(PyExc_EOFError);
669 return -1;
670 }
671
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000672 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000673}
674
675
Martin v. Löwis18e16552006-02-15 17:27:45 +0000676static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000677readline_other(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000678{
679 PyObject *str;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000680 Py_ssize_t str_size;
Tim Peterscba30e22003-02-01 06:24:36 +0000681
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000682 if (!( str = PyObject_CallObject(self->readline, empty_tuple))) {
683 return -1;
684 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000685
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000686 if ((str_size = PyString_Size(str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000687 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000688
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000689 Py_XDECREF(self->last_string);
690 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000691
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000692 if (! (*s = PyString_AsString(str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000693 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000694
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000695 return str_size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000696}
697
Tim Petersee1a53c2003-02-02 02:57:53 +0000698/* Copy the first n bytes from s into newly malloc'ed memory, plus a
699 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
700 * The caller is responsible for free()'ing the return value.
701 */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000702static char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000703pystrndup(const char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000704{
Tim Petersee1a53c2003-02-02 02:57:53 +0000705 char *r = (char *)malloc(n+1);
706 if (r == NULL)
707 return (char*)PyErr_NoMemory();
708 memcpy(r, s, n);
709 r[n] = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000710 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000711}
712
713
714static int
Tim Peterscba30e22003-02-01 06:24:36 +0000715get(Picklerobject *self, PyObject *id)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000716{
717 PyObject *value, *mv;
718 long c_value;
719 char s[30];
720 size_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000721
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000722 if (!( mv = PyDict_GetItem(self->memo, id))) {
723 PyErr_SetObject(PyExc_KeyError, id);
724 return -1;
725 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000726
Tim Peterscba30e22003-02-01 06:24:36 +0000727 if (!( value = PyTuple_GetItem(mv, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000728 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000729
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000730 if (!( PyInt_Check(value))) {
731 PyErr_SetString(PicklingError, "no int where int expected in memo");
732 return -1;
733 }
734 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000735
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000736 if (!self->bin) {
737 s[0] = GET;
738 PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value);
739 len = strlen(s);
740 }
741 else if (Pdata_Check(self->file)) {
742 if (write_other(self, NULL, 0) < 0) return -1;
743 PDATA_APPEND(self->file, mv, -1);
744 return 0;
745 }
746 else {
747 if (c_value < 256) {
748 s[0] = BINGET;
749 s[1] = (int)(c_value & 0xff);
750 len = 2;
751 }
752 else {
753 s[0] = LONG_BINGET;
754 s[1] = (int)(c_value & 0xff);
755 s[2] = (int)((c_value >> 8) & 0xff);
756 s[3] = (int)((c_value >> 16) & 0xff);
757 s[4] = (int)((c_value >> 24) & 0xff);
758 len = 5;
759 }
760 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000761
Tim Peters0bc93f52003-02-02 18:29:33 +0000762 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000763 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000764
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000765 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000766}
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000767
Guido van Rossum60456fd1997-04-09 17:36:32 +0000768
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000769static int
Tim Peterscba30e22003-02-01 06:24:36 +0000770put(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000771{
Christian Heimese93237d2007-12-19 02:37:44 +0000772 if (Py_REFCNT(ob) < 2 || self->fast)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000773 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000774
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000775 return put2(self, ob);
776}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000777
Guido van Rossum053b8df1998-11-25 16:18:00 +0000778
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000779static int
Tim Peterscba30e22003-02-01 06:24:36 +0000780put2(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000781{
782 char c_str[30];
783 int p;
784 size_t len;
785 int res = -1;
786 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000787
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000788 if (self->fast)
789 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000790
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000791 if ((p = PyDict_Size(self->memo)) < 0)
792 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000793
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000794 /* Make sure memo keys are positive! */
Tim Peters70b02d72003-02-02 17:26:40 +0000795 /* XXX Why?
796 * XXX And does "positive" really mean non-negative?
797 * XXX pickle.py starts with PUT index 0, not 1. This makes for
798 * XXX gratuitous differences between the pickling modules.
799 */
Tim Peterscba30e22003-02-01 06:24:36 +0000800 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000801
Tim Peterscba30e22003-02-01 06:24:36 +0000802 if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000803 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000804
Tim Peterscba30e22003-02-01 06:24:36 +0000805 if (!( memo_len = PyInt_FromLong(p)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000806 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000807
Tim Peterscba30e22003-02-01 06:24:36 +0000808 if (!( t = PyTuple_New(2)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000809 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000810
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000811 PyTuple_SET_ITEM(t, 0, memo_len);
812 Py_INCREF(memo_len);
813 PyTuple_SET_ITEM(t, 1, ob);
814 Py_INCREF(ob);
815
816 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
817 goto finally;
818
819 if (!self->bin) {
820 c_str[0] = PUT;
821 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p);
822 len = strlen(c_str);
823 }
824 else if (Pdata_Check(self->file)) {
825 if (write_other(self, NULL, 0) < 0) return -1;
826 PDATA_APPEND(self->file, memo_len, -1);
827 res=0; /* Job well done ;) */
828 goto finally;
829 }
830 else {
831 if (p >= 256) {
832 c_str[0] = LONG_BINPUT;
833 c_str[1] = (int)(p & 0xff);
834 c_str[2] = (int)((p >> 8) & 0xff);
835 c_str[3] = (int)((p >> 16) & 0xff);
836 c_str[4] = (int)((p >> 24) & 0xff);
837 len = 5;
838 }
839 else {
840 c_str[0] = BINPUT;
841 c_str[1] = p;
842 len = 2;
843 }
844 }
845
Tim Peters0bc93f52003-02-02 18:29:33 +0000846 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000847 goto finally;
848
849 res = 0;
850
851 finally:
852 Py_XDECREF(py_ob_id);
853 Py_XDECREF(memo_len);
854 Py_XDECREF(t);
855
856 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000857}
858
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000859static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000860whichmodule(PyObject *global, PyObject *global_name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000861{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000862 Py_ssize_t i, j;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000863 PyObject *module = 0, *modules_dict = 0,
864 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000865
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000866 module = PyObject_GetAttrString(global, "__module__");
Jeremy Hyltonf00368f2003-06-16 20:19:49 +0000867 if (module)
868 return module;
869 if (PyErr_ExceptionMatches(PyExc_AttributeError))
870 PyErr_Clear();
871 else
872 return NULL;
Guido van Rossum45188231997-09-28 05:38:51 +0000873
Tim Peterscba30e22003-02-01 06:24:36 +0000874 if (!( modules_dict = PySys_GetObject("modules")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000875 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000876
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000877 i = 0;
878 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000879
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000880 if (PyObject_Compare(name, __main___str)==0) continue;
Tim Peters84e87f32001-03-17 04:50:51 +0000881
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000882 global_name_attr = PyObject_GetAttr(module, global_name);
883 if (!global_name_attr) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +0000884 if (PyErr_ExceptionMatches(PyExc_AttributeError))
885 PyErr_Clear();
886 else
887 return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000888 continue;
889 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000890
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000891 if (global_name_attr != global) {
892 Py_DECREF(global_name_attr);
893 continue;
894 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000895
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000896 Py_DECREF(global_name_attr);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000897
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000898 break;
899 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000900
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000901 /* The following implements the rule in pickle.py added in 1.5
902 that used __main__ if no module is found. I don't actually
903 like this rule. jlf
904 */
905 if (!j) {
906 j=1;
907 name=__main___str;
908 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000909
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000910 Py_INCREF(name);
911 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000912}
913
914
Guido van Rossum60456fd1997-04-09 17:36:32 +0000915static int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000916fast_save_enter(Picklerobject *self, PyObject *obj)
917{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000918 /* if fast_container < 0, we're doing an error exit. */
919 if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
920 PyObject *key = NULL;
921 if (self->fast_memo == NULL) {
922 self->fast_memo = PyDict_New();
923 if (self->fast_memo == NULL) {
924 self->fast_container = -1;
925 return 0;
926 }
927 }
928 key = PyLong_FromVoidPtr(obj);
929 if (key == NULL)
930 return 0;
931 if (PyDict_GetItem(self->fast_memo, key)) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000932 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000933 PyErr_Format(PyExc_ValueError,
Tim Peters92c8bb32003-02-13 23:00:26 +0000934 "fast mode: can't pickle cyclic objects "
935 "including object type %s at %p",
Christian Heimese93237d2007-12-19 02:37:44 +0000936 Py_TYPE(obj)->tp_name, obj);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000937 self->fast_container = -1;
938 return 0;
939 }
940 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000941 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000942 self->fast_container = -1;
943 return 0;
944 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000945 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000946 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000947 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000948}
949
Tim Peterscba30e22003-02-01 06:24:36 +0000950int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000951fast_save_leave(Picklerobject *self, PyObject *obj)
952{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000953 if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
954 PyObject *key = PyLong_FromVoidPtr(obj);
955 if (key == NULL)
956 return 0;
957 if (PyDict_DelItem(self->fast_memo, key) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000958 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000959 return 0;
960 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000961 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000962 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000963 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000964}
965
966static int
Tim Peterscba30e22003-02-01 06:24:36 +0000967save_none(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000968{
969 static char none = NONE;
Tim Peters0bc93f52003-02-02 18:29:33 +0000970 if (self->write_func(self, &none, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000971 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000972
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000973 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000974}
975
Guido van Rossum77f6a652002-04-03 22:41:51 +0000976static int
Tim Peterscba30e22003-02-01 06:24:36 +0000977save_bool(Picklerobject *self, PyObject *args)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000978{
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000979 static const char *buf[2] = {FALSE, TRUE};
Guido van Rossume2763392002-04-05 19:30:08 +0000980 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
Guido van Rossum77f6a652002-04-03 22:41:51 +0000981 long l = PyInt_AS_LONG((PyIntObject *)args);
982
Tim Peters3c67d792003-02-02 17:59:11 +0000983 if (self->proto >= 2) {
984 char opcode = l ? NEWTRUE : NEWFALSE;
985 if (self->write_func(self, &opcode, 1) < 0)
986 return -1;
987 }
988 else if (self->write_func(self, buf[l], len[l]) < 0)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000989 return -1;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000990 return 0;
991}
Tim Peters84e87f32001-03-17 04:50:51 +0000992
Guido van Rossum60456fd1997-04-09 17:36:32 +0000993static int
Tim Peterscba30e22003-02-01 06:24:36 +0000994save_int(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000995{
996 char c_str[32];
997 long l = PyInt_AS_LONG((PyIntObject *)args);
998 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000999
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001000 if (!self->bin
Guido van Rossumde8d6d71997-05-13 18:00:44 +00001001#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001002 || l > 0x7fffffffL
1003 || l < -0x80000000L
Guido van Rossumde8d6d71997-05-13 18:00:44 +00001004#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001005 ) {
1006 /* Text-mode pickle, or long too big to fit in the 4-byte
1007 * signed BININT format: store as a string.
1008 */
1009 c_str[0] = INT;
1010 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
Tim Peters0bc93f52003-02-02 18:29:33 +00001011 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001012 return -1;
1013 }
1014 else {
1015 /* Binary pickle and l fits in a signed 4-byte int. */
1016 c_str[1] = (int)( l & 0xff);
1017 c_str[2] = (int)((l >> 8) & 0xff);
1018 c_str[3] = (int)((l >> 16) & 0xff);
1019 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001020
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001021 if ((c_str[4] == 0) && (c_str[3] == 0)) {
1022 if (c_str[2] == 0) {
1023 c_str[0] = BININT1;
1024 len = 2;
1025 }
1026 else {
1027 c_str[0] = BININT2;
1028 len = 3;
1029 }
1030 }
1031 else {
1032 c_str[0] = BININT;
1033 len = 5;
1034 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001035
Tim Peters0bc93f52003-02-02 18:29:33 +00001036 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001037 return -1;
1038 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001039
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001040 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001041}
1042
1043
1044static int
Tim Peterscba30e22003-02-01 06:24:36 +00001045save_long(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001046{
Armin Rigo7ccbca92006-10-04 12:17:45 +00001047 Py_ssize_t size;
Tim Petersee1a53c2003-02-02 02:57:53 +00001048 int res = -1;
1049 PyObject *repr = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001050
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001051 static char l = LONG;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001052
Tim Petersee1a53c2003-02-02 02:57:53 +00001053 if (self->proto >= 2) {
1054 /* Linear-time pickling. */
1055 size_t nbits;
1056 size_t nbytes;
1057 unsigned char *pdata;
1058 char c_str[5];
1059 int i;
1060 int sign = _PyLong_Sign(args);
1061
1062 if (sign == 0) {
1063 /* It's 0 -- an empty bytestring. */
1064 c_str[0] = LONG1;
1065 c_str[1] = 0;
1066 i = self->write_func(self, c_str, 2);
1067 if (i < 0) goto finally;
1068 res = 0;
1069 goto finally;
1070 }
1071 nbits = _PyLong_NumBits(args);
1072 if (nbits == (size_t)-1 && PyErr_Occurred())
1073 goto finally;
1074 /* How many bytes do we need? There are nbits >> 3 full
1075 * bytes of data, and nbits & 7 leftover bits. If there
1076 * are any leftover bits, then we clearly need another
1077 * byte. Wnat's not so obvious is that we *probably*
1078 * need another byte even if there aren't any leftovers:
1079 * the most-significant bit of the most-significant byte
1080 * acts like a sign bit, and it's usually got a sense
1081 * opposite of the one we need. The exception is longs
1082 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1083 * its own 256's-complement, so has the right sign bit
1084 * even without the extra byte. That's a pain to check
1085 * for in advance, though, so we always grab an extra
1086 * byte at the start, and cut it back later if possible.
1087 */
1088 nbytes = (nbits >> 3) + 1;
Armin Rigo7ccbca92006-10-04 12:17:45 +00001089 if (nbytes > INT_MAX) {
Tim Petersee1a53c2003-02-02 02:57:53 +00001090 PyErr_SetString(PyExc_OverflowError, "long too large "
1091 "to pickle");
1092 goto finally;
1093 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001094 repr = PyString_FromStringAndSize(NULL, (int)nbytes);
Tim Petersee1a53c2003-02-02 02:57:53 +00001095 if (repr == NULL) goto finally;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001096 pdata = (unsigned char *)PyString_AS_STRING(repr);
Tim Petersee1a53c2003-02-02 02:57:53 +00001097 i = _PyLong_AsByteArray((PyLongObject *)args,
1098 pdata, nbytes,
1099 1 /* little endian */, 1 /* signed */);
1100 if (i < 0) goto finally;
1101 /* If the long is negative, this may be a byte more than
1102 * needed. This is so iff the MSB is all redundant sign
1103 * bits.
1104 */
1105 if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
1106 (pdata[nbytes - 2] & 0x80) != 0)
1107 --nbytes;
1108
1109 if (nbytes < 256) {
1110 c_str[0] = LONG1;
1111 c_str[1] = (char)nbytes;
1112 size = 2;
1113 }
1114 else {
1115 c_str[0] = LONG4;
1116 size = (int)nbytes;
1117 for (i = 1; i < 5; i++) {
1118 c_str[i] = (char)(size & 0xff);
1119 size >>= 8;
1120 }
1121 size = 5;
1122 }
1123 i = self->write_func(self, c_str, size);
1124 if (i < 0) goto finally;
1125 i = self->write_func(self, (char *)pdata, (int)nbytes);
1126 if (i < 0) goto finally;
1127 res = 0;
1128 goto finally;
1129 }
1130
1131 /* proto < 2: write the repr and newline. This is quadratic-time
1132 * (in the number of digits), in both directions.
1133 */
Tim Peterscba30e22003-02-01 06:24:36 +00001134 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001135 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001136
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001137 if ((size = PyString_Size(repr)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001138 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001139
Tim Peters0bc93f52003-02-02 18:29:33 +00001140 if (self->write_func(self, &l, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001141 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001142
Tim Peters0bc93f52003-02-02 18:29:33 +00001143 if (self->write_func(self,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001144 PyString_AS_STRING((PyStringObject *)repr),
Tim Peters0bc93f52003-02-02 18:29:33 +00001145 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001146 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001147
Tim Peters0bc93f52003-02-02 18:29:33 +00001148 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001149 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001150
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001151 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001152
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001153 finally:
1154 Py_XDECREF(repr);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001155 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001156}
1157
1158
1159static int
Tim Peterscba30e22003-02-01 06:24:36 +00001160save_float(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001161{
1162 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001163
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001164 if (self->bin) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001165 char str[9];
Tim Peters9905b942003-03-20 20:53:32 +00001166 str[0] = BINFLOAT;
1167 if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001168 return -1;
Tim Peters0bc93f52003-02-02 18:29:33 +00001169 if (self->write_func(self, str, 9) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001170 return -1;
1171 }
1172 else {
1173 char c_str[250];
1174 c_str[0] = FLOAT;
Georg Brandlde9b6242006-04-30 11:13:56 +00001175 PyOS_ascii_formatd(c_str + 1, sizeof(c_str) - 2, "%.17g", x);
1176 /* Extend the formatted string with a newline character */
1177 strcat(c_str, "\n");
Guido van Rossum60456fd1997-04-09 17:36:32 +00001178
Tim Peters0bc93f52003-02-02 18:29:33 +00001179 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001180 return -1;
1181 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001182
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001183 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001184}
1185
1186
1187static int
Tim Peterscba30e22003-02-01 06:24:36 +00001188save_string(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001189{
1190 int size, len;
1191 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001192
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001193 if ((size = PyString_Size(args)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001194 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001195
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001196 if (!self->bin) {
1197 char *repr_str;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001198
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001199 static char string = STRING;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001200
Tim Peterscba30e22003-02-01 06:24:36 +00001201 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001202 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001203
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001204 if ((len = PyString_Size(repr)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001205 goto err;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001206 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001207
Tim Peters0bc93f52003-02-02 18:29:33 +00001208 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001209 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001210
Tim Peters0bc93f52003-02-02 18:29:33 +00001211 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001212 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001213
Tim Peters0bc93f52003-02-02 18:29:33 +00001214 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001215 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001216
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001217 Py_XDECREF(repr);
1218 }
1219 else {
1220 int i;
1221 char c_str[5];
Guido van Rossum60456fd1997-04-09 17:36:32 +00001222
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001223 if ((size = PyString_Size(args)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001224 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001225
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001226 if (size < 256) {
1227 c_str[0] = SHORT_BINSTRING;
1228 c_str[1] = size;
1229 len = 2;
1230 }
Armin Rigo7ccbca92006-10-04 12:17:45 +00001231 else if (size <= INT_MAX) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001232 c_str[0] = BINSTRING;
1233 for (i = 1; i < 5; i++)
1234 c_str[i] = (int)(size >> ((i - 1) * 8));
1235 len = 5;
1236 }
Armin Rigo7ccbca92006-10-04 12:17:45 +00001237 else
1238 return -1; /* string too large */
Guido van Rossum60456fd1997-04-09 17:36:32 +00001239
Tim Peters0bc93f52003-02-02 18:29:33 +00001240 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001241 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001242
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001243 if (size > 128 && Pdata_Check(self->file)) {
1244 if (write_other(self, NULL, 0) < 0) return -1;
1245 PDATA_APPEND(self->file, args, -1);
1246 }
1247 else {
Tim Peters0bc93f52003-02-02 18:29:33 +00001248 if (self->write_func(self,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001249 PyString_AS_STRING(
1250 (PyStringObject *)args),
Tim Peters0bc93f52003-02-02 18:29:33 +00001251 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001252 return -1;
1253 }
1254 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001255
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001256 if (doput)
1257 if (put(self, args) < 0)
1258 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001259
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001260 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001261
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001262 err:
1263 Py_XDECREF(repr);
1264 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001265}
1266
1267
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001268#ifdef Py_USING_UNICODE
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001269/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1270 backslash and newline characters to \uXXXX escapes. */
1271static PyObject *
Alexandre Vassalotti966322f2008-12-27 07:16:40 +00001272modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size)
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001273{
Alexandre Vassalotti966322f2008-12-27 07:16:40 +00001274 PyObject *repr;
1275 char *p;
1276 char *q;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001277
Alexandre Vassalotti966322f2008-12-27 07:16:40 +00001278 static const char *hexdigit = "0123456789abcdef";
1279#ifdef Py_UNICODE_WIDE
1280 const Py_ssize_t expandsize = 10;
1281#else
1282 const Py_ssize_t expandsize = 6;
1283#endif
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001284
Alexandre Vassalotti966322f2008-12-27 07:16:40 +00001285 if (size > PY_SSIZE_T_MAX / expandsize)
1286 return PyErr_NoMemory();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001287
Alexandre Vassalotti966322f2008-12-27 07:16:40 +00001288 repr = PyString_FromStringAndSize(NULL, expandsize * size);
1289 if (repr == NULL)
1290 return NULL;
1291 if (size == 0)
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001292 return repr;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001293
Alexandre Vassalotti966322f2008-12-27 07:16:40 +00001294 p = q = PyString_AS_STRING(repr);
1295 while (size-- > 0) {
1296 Py_UNICODE ch = *s++;
1297#ifdef Py_UNICODE_WIDE
1298 /* Map 32-bit characters to '\Uxxxxxxxx' */
1299 if (ch >= 0x10000) {
1300 *p++ = '\\';
1301 *p++ = 'U';
1302 *p++ = hexdigit[(ch >> 28) & 0xf];
1303 *p++ = hexdigit[(ch >> 24) & 0xf];
1304 *p++ = hexdigit[(ch >> 20) & 0xf];
1305 *p++ = hexdigit[(ch >> 16) & 0xf];
1306 *p++ = hexdigit[(ch >> 12) & 0xf];
1307 *p++ = hexdigit[(ch >> 8) & 0xf];
1308 *p++ = hexdigit[(ch >> 4) & 0xf];
1309 *p++ = hexdigit[ch & 15];
1310 }
1311 else
1312#else
1313 /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */
1314 if (ch >= 0xD800 && ch < 0xDC00) {
1315 Py_UNICODE ch2;
1316 Py_UCS4 ucs;
1317
1318 ch2 = *s++;
1319 size--;
1320 if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) {
1321 ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000;
1322 *p++ = '\\';
1323 *p++ = 'U';
1324 *p++ = hexdigit[(ucs >> 28) & 0xf];
1325 *p++ = hexdigit[(ucs >> 24) & 0xf];
1326 *p++ = hexdigit[(ucs >> 20) & 0xf];
1327 *p++ = hexdigit[(ucs >> 16) & 0xf];
1328 *p++ = hexdigit[(ucs >> 12) & 0xf];
1329 *p++ = hexdigit[(ucs >> 8) & 0xf];
1330 *p++ = hexdigit[(ucs >> 4) & 0xf];
1331 *p++ = hexdigit[ucs & 0xf];
1332 continue;
1333 }
1334 /* Fall through: isolated surrogates are copied as-is */
1335 s--;
1336 size++;
1337 }
1338#endif
1339 /* Map 16-bit characters to '\uxxxx' */
1340 if (ch >= 256 || ch == '\\' || ch == '\n') {
1341 *p++ = '\\';
1342 *p++ = 'u';
1343 *p++ = hexdigit[(ch >> 12) & 0xf];
1344 *p++ = hexdigit[(ch >> 8) & 0xf];
1345 *p++ = hexdigit[(ch >> 4) & 0xf];
1346 *p++ = hexdigit[ch & 15];
1347 }
1348 /* Copy everything else as-is */
1349 else
1350 *p++ = (char) ch;
1351 }
1352 *p = '\0';
1353 _PyString_Resize(&repr, p - q);
1354 return repr;
1355}
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001356
Guido van Rossum60456fd1997-04-09 17:36:32 +00001357static int
Tim Peterscba30e22003-02-01 06:24:36 +00001358save_unicode(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001359{
Armin Rigo7ccbca92006-10-04 12:17:45 +00001360 Py_ssize_t size, len;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001361 PyObject *repr=0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001362
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001363 if (!PyUnicode_Check(args))
1364 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001365
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001366 if (!self->bin) {
1367 char *repr_str;
1368 static char string = UNICODE;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001369
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001370 repr = modified_EncodeRawUnicodeEscape(
1371 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
Tim Peterscba30e22003-02-01 06:24:36 +00001372 if (!repr)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001373 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001374
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001375 if ((len = PyString_Size(repr)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001376 goto err;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001377 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001378
Tim Peters0bc93f52003-02-02 18:29:33 +00001379 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001380 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001381
Tim Peters0bc93f52003-02-02 18:29:33 +00001382 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001383 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001384
Tim Peters0bc93f52003-02-02 18:29:33 +00001385 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001386 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001387
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001388 Py_XDECREF(repr);
1389 }
1390 else {
1391 int i;
1392 char c_str[5];
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001393
Tim Peterscba30e22003-02-01 06:24:36 +00001394 if (!( repr = PyUnicode_AsUTF8String(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001395 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001396
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001397 if ((size = PyString_Size(repr)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001398 goto err;
Armin Rigo7ccbca92006-10-04 12:17:45 +00001399 if (size > INT_MAX)
1400 return -1; /* string too large */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001401
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001402 c_str[0] = BINUNICODE;
1403 for (i = 1; i < 5; i++)
1404 c_str[i] = (int)(size >> ((i - 1) * 8));
1405 len = 5;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001406
Tim Peters0bc93f52003-02-02 18:29:33 +00001407 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001408 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001409
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001410 if (size > 128 && Pdata_Check(self->file)) {
1411 if (write_other(self, NULL, 0) < 0)
1412 goto err;
1413 PDATA_APPEND(self->file, repr, -1);
1414 }
1415 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001416 if (self->write_func(self, PyString_AS_STRING(repr),
Tim Peters0bc93f52003-02-02 18:29:33 +00001417 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001418 goto err;
1419 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001420
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001421 Py_DECREF(repr);
1422 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001423
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001424 if (doput)
1425 if (put(self, args) < 0)
1426 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001427
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001428 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001429
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001430 err:
1431 Py_XDECREF(repr);
1432 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001433}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001434#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001435
Tim Peters1d63c9f2003-02-02 20:29:39 +00001436/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1437static int
Tim Peters67920142003-02-05 03:46:17 +00001438store_tuple_elements(Picklerobject *self, PyObject *t, int len)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001439{
1440 int i;
1441 int res = -1; /* guilty until proved innocent */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001442
Tim Peters1d63c9f2003-02-02 20:29:39 +00001443 assert(PyTuple_Size(t) == len);
1444
1445 for (i = 0; i < len; i++) {
1446 PyObject *element = PyTuple_GET_ITEM(t, i);
1447
1448 if (element == NULL)
1449 goto finally;
1450 if (save(self, element, 0) < 0)
1451 goto finally;
1452 }
1453 res = 0;
1454
1455 finally:
1456 return res;
1457}
1458
1459/* Tuples are ubiquitous in the pickle protocols, so many techniques are
1460 * used across protocols to minimize the space needed to pickle them.
Tim Peters67920142003-02-05 03:46:17 +00001461 * Tuples are also the only builtin immutable type that can be recursive
Tim Peters1d63c9f2003-02-02 20:29:39 +00001462 * (a tuple can be reached from itself), and that requires some subtle
1463 * magic so that it works in all cases. IOW, this is a long routine.
1464 */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001465static int
Tim Peterscba30e22003-02-01 06:24:36 +00001466save_tuple(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001467{
Tim Peters1d63c9f2003-02-02 20:29:39 +00001468 PyObject *py_tuple_id = NULL;
1469 int len, i;
1470 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001471
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001472 static char tuple = TUPLE;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001473 static char pop = POP;
1474 static char pop_mark = POP_MARK;
1475 static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
Guido van Rossum60456fd1997-04-09 17:36:32 +00001476
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001477 if ((len = PyTuple_Size(args)) < 0)
1478 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001479
Tim Peters1d63c9f2003-02-02 20:29:39 +00001480 if (len == 0) {
1481 char c_str[2];
Tim Peters84e87f32001-03-17 04:50:51 +00001482
Tim Peters1d63c9f2003-02-02 20:29:39 +00001483 if (self->proto) {
1484 c_str[0] = EMPTY_TUPLE;
1485 len = 1;
1486 }
1487 else {
1488 c_str[0] = MARK;
1489 c_str[1] = TUPLE;
1490 len = 2;
1491 }
1492 if (self->write_func(self, c_str, len) >= 0)
1493 res = 0;
1494 /* Don't memoize an empty tuple. */
1495 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001496 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001497
Tim Peters1d63c9f2003-02-02 20:29:39 +00001498 /* A non-empty tuple. */
1499
1500 /* id(tuple) isn't in the memo now. If it shows up there after
1501 * saving the tuple elements, the tuple must be recursive, in
1502 * which case we'll pop everything we put on the stack, and fetch
1503 * its value from the memo.
1504 */
1505 py_tuple_id = PyLong_FromVoidPtr(args);
1506 if (py_tuple_id == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001507 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001508
Tim Peters1d63c9f2003-02-02 20:29:39 +00001509 if (len <= 3 && self->proto >= 2) {
1510 /* Use TUPLE{1,2,3} opcodes. */
Tim Peters67920142003-02-05 03:46:17 +00001511 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001512 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001513 if (PyDict_GetItem(self->memo, py_tuple_id)) {
Tim Peters1d63c9f2003-02-02 20:29:39 +00001514 /* pop the len elements */
1515 for (i = 0; i < len; ++i)
1516 if (self->write_func(self, &pop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001517 goto finally;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001518 /* fetch from memo */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001519 if (get(self, py_tuple_id) < 0)
1520 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001521 res = 0;
1522 goto finally;
1523 }
Tim Peters1d63c9f2003-02-02 20:29:39 +00001524 /* Not recursive. */
1525 if (self->write_func(self, len2opcode + len, 1) < 0)
1526 goto finally;
1527 goto memoize;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001528 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001529
Tim Peters1d63c9f2003-02-02 20:29:39 +00001530 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1531 * Generate MARK elt1 elt2 ... TUPLE
1532 */
1533 if (self->write_func(self, &MARKv, 1) < 0)
1534 goto finally;
1535
Tim Peters67920142003-02-05 03:46:17 +00001536 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001537 goto finally;
1538
1539 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1540 /* pop the stack stuff we pushed */
1541 if (self->bin) {
1542 if (self->write_func(self, &pop_mark, 1) < 0)
1543 goto finally;
1544 }
1545 else {
1546 /* Note that we pop one more than len, to remove
1547 * the MARK too.
1548 */
1549 for (i = 0; i <= len; i++)
1550 if (self->write_func(self, &pop, 1) < 0)
1551 goto finally;
1552 }
1553 /* fetch from memo */
1554 if (get(self, py_tuple_id) >= 0)
1555 res = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001556 goto finally;
1557 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001558
Tim Peters1d63c9f2003-02-02 20:29:39 +00001559 /* Not recursive. */
1560 if (self->write_func(self, &tuple, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001561 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001562
Tim Peters1d63c9f2003-02-02 20:29:39 +00001563 memoize:
1564 if (put(self, args) >= 0)
1565 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001566
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001567 finally:
1568 Py_XDECREF(py_tuple_id);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001569 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001570}
1571
Tim Peters1092d642003-02-11 21:06:20 +00001572/* iter is an iterator giving items, and we batch up chunks of
1573 * MARK item item ... item APPENDS
1574 * opcode sequences. Calling code should have arranged to first create an
1575 * empty list, or list-like object, for the APPENDS to operate on.
1576 * Returns 0 on success, <0 on error.
1577 */
1578static int
1579batch_list(Picklerobject *self, PyObject *iter)
1580{
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001581 PyObject *obj = NULL;
1582 PyObject *firstitem = NULL;
Tim Peters1092d642003-02-11 21:06:20 +00001583 int i, n;
1584
1585 static char append = APPEND;
1586 static char appends = APPENDS;
1587
1588 assert(iter != NULL);
1589
1590 if (self->proto == 0) {
1591 /* APPENDS isn't available; do one at a time. */
1592 for (;;) {
1593 obj = PyIter_Next(iter);
1594 if (obj == NULL) {
1595 if (PyErr_Occurred())
1596 return -1;
1597 break;
1598 }
1599 i = save(self, obj, 0);
1600 Py_DECREF(obj);
1601 if (i < 0)
1602 return -1;
1603 if (self->write_func(self, &append, 1) < 0)
1604 return -1;
Tim Peters1092d642003-02-11 21:06:20 +00001605 }
1606 return 0;
1607 }
1608
1609 /* proto > 0: write in batches of BATCHSIZE. */
1610 do {
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001611 /* Get first item */
1612 firstitem = PyIter_Next(iter);
1613 if (firstitem == NULL) {
1614 if (PyErr_Occurred())
1615 goto BatchFailed;
1616
1617 /* nothing more to add */
1618 break;
1619 }
1620
1621 /* Try to get a second item */
1622 obj = PyIter_Next(iter);
1623 if (obj == NULL) {
1624 if (PyErr_Occurred())
1625 goto BatchFailed;
1626
1627 /* Only one item to write */
1628 if (save(self, firstitem, 0) < 0)
1629 goto BatchFailed;
1630 if (self->write_func(self, &append, 1) < 0)
1631 goto BatchFailed;
1632 Py_CLEAR(firstitem);
1633 break;
1634 }
1635
1636 /* More than one item to write */
1637
1638 /* Pump out MARK, items, APPENDS. */
1639 if (self->write_func(self, &MARKv, 1) < 0)
1640 goto BatchFailed;
1641
1642 if (save(self, firstitem, 0) < 0)
1643 goto BatchFailed;
1644 Py_CLEAR(firstitem);
1645 n = 1;
1646
1647 /* Fetch and save up to BATCHSIZE items */
1648 while (obj) {
1649 if (save(self, obj, 0) < 0)
1650 goto BatchFailed;
1651 Py_CLEAR(obj);
1652 n += 1;
1653
1654 if (n == BATCHSIZE)
1655 break;
1656
Tim Peters1092d642003-02-11 21:06:20 +00001657 obj = PyIter_Next(iter);
1658 if (obj == NULL) {
1659 if (PyErr_Occurred())
1660 goto BatchFailed;
1661 break;
1662 }
Tim Peters1092d642003-02-11 21:06:20 +00001663 }
1664
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001665 if (self->write_func(self, &appends, 1) < 0)
1666 goto BatchFailed;
Tim Peters1092d642003-02-11 21:06:20 +00001667
Tim Peters90975f12003-02-12 05:28:58 +00001668 } while (n == BATCHSIZE);
Tim Peters1092d642003-02-11 21:06:20 +00001669 return 0;
1670
1671BatchFailed:
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001672 Py_XDECREF(firstitem);
1673 Py_XDECREF(obj);
Tim Peters1092d642003-02-11 21:06:20 +00001674 return -1;
1675}
1676
Guido van Rossum60456fd1997-04-09 17:36:32 +00001677static int
Tim Peterscba30e22003-02-01 06:24:36 +00001678save_list(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001679{
Tim Peters1092d642003-02-11 21:06:20 +00001680 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001681 char s[3];
Tim Peters1092d642003-02-11 21:06:20 +00001682 int len;
1683 PyObject *iter;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001684
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001685 if (self->fast && !fast_save_enter(self, args))
1686 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001687
Tim Peters1092d642003-02-11 21:06:20 +00001688 /* Create an empty list. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001689 if (self->bin) {
1690 s[0] = EMPTY_LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001691 len = 1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001692 }
1693 else {
1694 s[0] = MARK;
1695 s[1] = LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001696 len = 2;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001697 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001698
Tim Peters1092d642003-02-11 21:06:20 +00001699 if (self->write_func(self, s, len) < 0)
1700 goto finally;
1701
1702 /* Get list length, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001703 if ((len = PyList_Size(args)) < 0)
1704 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001705
Tim Peters1092d642003-02-11 21:06:20 +00001706 /* Memoize. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001707 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001708 if (put(self, args) >= 0)
1709 res = 0;
1710 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001711 }
Tim Peters90975f12003-02-12 05:28:58 +00001712 if (put2(self, args) < 0)
1713 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001714
Tim Peters1092d642003-02-11 21:06:20 +00001715 /* Materialize the list elements. */
1716 iter = PyObject_GetIter(args);
1717 if (iter == NULL)
1718 goto finally;
Facundo Batista763d3092008-06-30 01:10:55 +00001719
1720 if (Py_EnterRecursiveCall(" while pickling an object") == 0)
1721 {
1722 res = batch_list(self, iter);
1723 Py_LeaveRecursiveCall();
1724 }
Tim Peters1092d642003-02-11 21:06:20 +00001725 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001726
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001727 finally:
1728 if (self->fast && !fast_save_leave(self, args))
1729 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001730
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001731 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001732}
1733
1734
Tim Peters42f08ac2003-02-11 22:43:24 +00001735/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1736 * MARK key value ... key value SETITEMS
1737 * opcode sequences. Calling code should have arranged to first create an
1738 * empty dict, or dict-like object, for the SETITEMS to operate on.
1739 * Returns 0 on success, <0 on error.
1740 *
1741 * This is very much like batch_list(). The difference between saving
1742 * elements directly, and picking apart two-tuples, is so long-winded at
1743 * the C level, though, that attempts to combine these routines were too
1744 * ugly to bear.
1745 */
1746static int
1747batch_dict(Picklerobject *self, PyObject *iter)
1748{
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001749 PyObject *p = NULL;
1750 PyObject *firstitem = NULL;
Tim Peters42f08ac2003-02-11 22:43:24 +00001751 int i, n;
1752
1753 static char setitem = SETITEM;
1754 static char setitems = SETITEMS;
1755
1756 assert(iter != NULL);
1757
1758 if (self->proto == 0) {
1759 /* SETITEMS isn't available; do one at a time. */
1760 for (;;) {
1761 p = PyIter_Next(iter);
1762 if (p == NULL) {
1763 if (PyErr_Occurred())
1764 return -1;
1765 break;
1766 }
1767 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1768 PyErr_SetString(PyExc_TypeError, "dict items "
1769 "iterator must return 2-tuples");
1770 return -1;
1771 }
1772 i = save(self, PyTuple_GET_ITEM(p, 0), 0);
1773 if (i >= 0)
1774 i = save(self, PyTuple_GET_ITEM(p, 1), 0);
1775 Py_DECREF(p);
1776 if (i < 0)
1777 return -1;
1778 if (self->write_func(self, &setitem, 1) < 0)
1779 return -1;
Tim Peters42f08ac2003-02-11 22:43:24 +00001780 }
1781 return 0;
1782 }
1783
1784 /* proto > 0: write in batches of BATCHSIZE. */
1785 do {
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001786 /* Get first item */
1787 firstitem = PyIter_Next(iter);
1788 if (firstitem == NULL) {
1789 if (PyErr_Occurred())
1790 goto BatchFailed;
1791
1792 /* nothing more to add */
1793 break;
1794 }
1795 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
1796 PyErr_SetString(PyExc_TypeError, "dict items "
1797 "iterator must return 2-tuples");
1798 goto BatchFailed;
1799 }
1800
1801 /* Try to get a second item */
1802 p = PyIter_Next(iter);
1803 if (p == NULL) {
1804 if (PyErr_Occurred())
1805 goto BatchFailed;
1806
1807 /* Only one item to write */
1808 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
1809 goto BatchFailed;
1810 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
1811 goto BatchFailed;
1812 if (self->write_func(self, &setitem, 1) < 0)
1813 goto BatchFailed;
1814 Py_CLEAR(firstitem);
1815 break;
1816 }
1817
1818 /* More than one item to write */
1819
1820 /* Pump out MARK, items, SETITEMS. */
1821 if (self->write_func(self, &MARKv, 1) < 0)
1822 goto BatchFailed;
1823
1824 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
1825 goto BatchFailed;
1826 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
1827 goto BatchFailed;
1828 Py_CLEAR(firstitem);
1829 n = 1;
1830
1831 /* Fetch and save up to BATCHSIZE items */
1832 while (p) {
1833 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1834 PyErr_SetString(PyExc_TypeError, "dict items "
1835 "iterator must return 2-tuples");
1836 goto BatchFailed;
1837 }
1838 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1839 goto BatchFailed;
1840 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1841 goto BatchFailed;
1842 Py_CLEAR(p);
1843 n += 1;
1844
1845 if (n == BATCHSIZE)
1846 break;
1847
Tim Peters42f08ac2003-02-11 22:43:24 +00001848 p = PyIter_Next(iter);
1849 if (p == NULL) {
1850 if (PyErr_Occurred())
1851 goto BatchFailed;
1852 break;
1853 }
Tim Peters42f08ac2003-02-11 22:43:24 +00001854 }
1855
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001856 if (self->write_func(self, &setitems, 1) < 0)
1857 goto BatchFailed;
Tim Peters42f08ac2003-02-11 22:43:24 +00001858
Tim Peters90975f12003-02-12 05:28:58 +00001859 } while (n == BATCHSIZE);
Tim Peters42f08ac2003-02-11 22:43:24 +00001860 return 0;
1861
1862BatchFailed:
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001863 Py_XDECREF(firstitem);
1864 Py_XDECREF(p);
Tim Peters42f08ac2003-02-11 22:43:24 +00001865 return -1;
1866}
1867
Guido van Rossum60456fd1997-04-09 17:36:32 +00001868static int
Tim Peterscba30e22003-02-01 06:24:36 +00001869save_dict(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001870{
Tim Peters42f08ac2003-02-11 22:43:24 +00001871 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001872 char s[3];
Tim Peters42f08ac2003-02-11 22:43:24 +00001873 int len;
1874 PyObject *iter;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001875
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001876 if (self->fast && !fast_save_enter(self, args))
1877 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001878
Tim Peters42f08ac2003-02-11 22:43:24 +00001879 /* Create an empty dict. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001880 if (self->bin) {
1881 s[0] = EMPTY_DICT;
1882 len = 1;
1883 }
1884 else {
1885 s[0] = MARK;
1886 s[1] = DICT;
1887 len = 2;
1888 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001889
Tim Peters0bc93f52003-02-02 18:29:33 +00001890 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001891 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001892
Tim Peters42f08ac2003-02-11 22:43:24 +00001893 /* Get dict size, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001894 if ((len = PyDict_Size(args)) < 0)
1895 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001896
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001897 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001898 if (put(self, args) >= 0)
1899 res = 0;
1900 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001901 }
Tim Peters90975f12003-02-12 05:28:58 +00001902 if (put2(self, args) < 0)
1903 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001904
Tim Peters42f08ac2003-02-11 22:43:24 +00001905 /* Materialize the dict items. */
1906 iter = PyObject_CallMethod(args, "iteritems", "()");
1907 if (iter == NULL)
1908 goto finally;
Facundo Batista763d3092008-06-30 01:10:55 +00001909 if (Py_EnterRecursiveCall(" while pickling an object") == 0)
1910 {
1911 res = batch_dict(self, iter);
1912 Py_LeaveRecursiveCall();
1913 }
Tim Peters42f08ac2003-02-11 22:43:24 +00001914 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001915
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001916 finally:
1917 if (self->fast && !fast_save_leave(self, args))
1918 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001919
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001920 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001921}
1922
1923
Tim Peters84e87f32001-03-17 04:50:51 +00001924static int
Tim Peterscba30e22003-02-01 06:24:36 +00001925save_inst(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001926{
1927 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1928 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1929 char *module_str, *name_str;
1930 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001931
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001932 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001933
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001934 if (self->fast && !fast_save_enter(self, args))
1935 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001936
Tim Peters0bc93f52003-02-02 18:29:33 +00001937 if (self->write_func(self, &MARKv, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001938 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001939
Tim Peterscba30e22003-02-01 06:24:36 +00001940 if (!( class = PyObject_GetAttr(args, __class___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001941 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001942
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001943 if (self->bin) {
1944 if (save(self, class, 0) < 0)
1945 goto finally;
1946 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001947
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001948 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
1949 PyObject *element = 0;
1950 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001951
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001952 if (!( class_args =
Tim Peterscba30e22003-02-01 06:24:36 +00001953 PyObject_Call(getinitargs_func, empty_tuple, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001954 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001955
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001956 if ((len = PyObject_Size(class_args)) < 0)
1957 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001958
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001959 for (i = 0; i < len; i++) {
Tim Peterscba30e22003-02-01 06:24:36 +00001960 if (!( element = PySequence_GetItem(class_args, i)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001961 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001962
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001963 if (save(self, element, 0) < 0) {
1964 Py_DECREF(element);
1965 goto finally;
1966 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001967
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001968 Py_DECREF(element);
1969 }
1970 }
1971 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00001972 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1973 PyErr_Clear();
1974 else
1975 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001976 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001977
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001978 if (!self->bin) {
1979 if (!( name = ((PyClassObject *)class)->cl_name )) {
1980 PyErr_SetString(PicklingError, "class has no name");
1981 goto finally;
1982 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001983
Tim Peterscba30e22003-02-01 06:24:36 +00001984 if (!( module = whichmodule(class, name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001985 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001986
Tim Peters84e87f32001-03-17 04:50:51 +00001987
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001988 if ((module_size = PyString_Size(module)) < 0 ||
1989 (name_size = PyString_Size(name)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001990 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001991
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001992 module_str = PyString_AS_STRING((PyStringObject *)module);
1993 name_str = PyString_AS_STRING((PyStringObject *)name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001994
Tim Peters0bc93f52003-02-02 18:29:33 +00001995 if (self->write_func(self, &inst, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001996 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001997
Tim Peters0bc93f52003-02-02 18:29:33 +00001998 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001999 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002000
Tim Peters0bc93f52003-02-02 18:29:33 +00002001 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002002 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002003
Tim Peters0bc93f52003-02-02 18:29:33 +00002004 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002005 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002006
Tim Peters0bc93f52003-02-02 18:29:33 +00002007 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002008 goto finally;
2009 }
Tim Peters0bc93f52003-02-02 18:29:33 +00002010 else if (self->write_func(self, &obj, 1) < 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002011 goto finally;
2012 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002013
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002014 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
2015 state = PyObject_Call(getstate_func, empty_tuple, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00002016 if (!state)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002017 goto finally;
2018 }
2019 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002020 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2021 PyErr_Clear();
2022 else
2023 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002024
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002025 if (!( state = PyObject_GetAttr(args, __dict___str))) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002026 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2027 PyErr_Clear();
2028 else
2029 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002030 res = 0;
2031 goto finally;
2032 }
2033 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002034
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002035 if (!PyDict_Check(state)) {
2036 if (put2(self, args) < 0)
2037 goto finally;
2038 }
2039 else {
2040 if (put(self, args) < 0)
2041 goto finally;
2042 }
Tim Peters84e87f32001-03-17 04:50:51 +00002043
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002044 if (save(self, state, 0) < 0)
2045 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002046
Tim Peters0bc93f52003-02-02 18:29:33 +00002047 if (self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002048 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002049
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002050 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002051
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002052 finally:
2053 if (self->fast && !fast_save_leave(self, args))
2054 res = -1;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002055
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002056 Py_XDECREF(module);
2057 Py_XDECREF(class);
2058 Py_XDECREF(state);
2059 Py_XDECREF(getinitargs_func);
2060 Py_XDECREF(getstate_func);
2061 Py_XDECREF(class_args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002062
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002063 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002064}
2065
2066
Guido van Rossum60456fd1997-04-09 17:36:32 +00002067static int
Tim Peterscba30e22003-02-01 06:24:36 +00002068save_global(Picklerobject *self, PyObject *args, PyObject *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002069{
Jeremy Hylton9ee91f12002-07-11 22:02:33 +00002070 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002071 char *name_str, *module_str;
2072 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002073
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002074 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002075
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002076 if (name) {
2077 global_name = name;
2078 Py_INCREF(global_name);
2079 }
2080 else {
Tim Peterscba30e22003-02-01 06:24:36 +00002081 if (!( global_name = PyObject_GetAttr(args, __name___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002082 goto finally;
2083 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002084
Tim Peterscba30e22003-02-01 06:24:36 +00002085 if (!( module = whichmodule(args, global_name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002086 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002087
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002088 if ((module_size = PyString_Size(module)) < 0 ||
2089 (name_size = PyString_Size(global_name)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002090 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002091
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002092 module_str = PyString_AS_STRING((PyStringObject *)module);
2093 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002094
Guido van Rossum75bfd052002-12-24 18:10:07 +00002095 /* XXX This can be doing a relative import. Clearly it shouldn't,
2096 but I don't know how to stop it. :-( */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002097 mod = PyImport_ImportModule(module_str);
2098 if (mod == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002099 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00002100 "Can't pickle %s: import of module %s "
2101 "failed",
2102 "OS", args, module);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002103 goto finally;
2104 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00002105 klass = PyObject_GetAttrString(mod, name_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002106 if (klass == NULL) {
2107 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00002108 "Can't pickle %s: attribute lookup %s.%s "
2109 "failed",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002110 "OSS", args, module, global_name);
2111 goto finally;
2112 }
2113 if (klass != args) {
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00002114 Py_DECREF(klass);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002115 cPickle_ErrFormat(PicklingError,
Tim Peters731098b2003-02-04 20:56:09 +00002116 "Can't pickle %s: it's not the same object "
2117 "as %s.%s",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002118 "OSS", args, module, global_name);
2119 goto finally;
2120 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00002121 Py_DECREF(klass);
Guido van Rossuma92d16a2001-08-18 21:22:07 +00002122
Tim Peters731098b2003-02-04 20:56:09 +00002123 if (self->proto >= 2) {
2124 /* See whether this is in the extension registry, and if
2125 * so generate an EXT opcode.
2126 */
2127 PyObject *py_code; /* extension code as Python object */
Tim Peters3e667d52003-02-04 21:47:44 +00002128 long code; /* extension code as C value */
Tim Peters731098b2003-02-04 20:56:09 +00002129 char c_str[5];
2130 int n;
2131
2132 PyTuple_SET_ITEM(two_tuple, 0, module);
2133 PyTuple_SET_ITEM(two_tuple, 1, global_name);
2134 py_code = PyDict_GetItem(extension_registry, two_tuple);
2135 if (py_code == NULL)
2136 goto gen_global; /* not registered */
2137
2138 /* Verify py_code has the right type and value. */
2139 if (!PyInt_Check(py_code)) {
2140 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
Tim Peters6288e232003-02-05 03:53:10 +00002141 "extension code %s isn't an integer",
Tim Peters731098b2003-02-04 20:56:09 +00002142 "OO", args, py_code);
2143 goto finally;
2144 }
2145 code = PyInt_AS_LONG(py_code);
2146 if (code <= 0 || code > 0x7fffffffL) {
2147 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2148 "extension code %ld is out of range",
2149 "Ol", args, code);
2150 goto finally;
2151 }
2152
2153 /* Generate an EXT opcode. */
2154 if (code <= 0xff) {
2155 c_str[0] = EXT1;
2156 c_str[1] = (char)code;
2157 n = 2;
2158 }
2159 else if (code <= 0xffff) {
2160 c_str[0] = EXT2;
2161 c_str[1] = (char)(code & 0xff);
2162 c_str[2] = (char)((code >> 8) & 0xff);
2163 n = 3;
2164 }
2165 else {
2166 c_str[0] = EXT4;
2167 c_str[1] = (char)(code & 0xff);
2168 c_str[2] = (char)((code >> 8) & 0xff);
2169 c_str[3] = (char)((code >> 16) & 0xff);
2170 c_str[4] = (char)((code >> 24) & 0xff);
2171 n = 5;
2172 }
2173
2174 if (self->write_func(self, c_str, n) >= 0)
2175 res = 0;
2176 goto finally; /* and don't memoize */
2177 }
2178
2179 gen_global:
Tim Peters0bc93f52003-02-02 18:29:33 +00002180 if (self->write_func(self, &global, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002181 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002182
Tim Peters0bc93f52003-02-02 18:29:33 +00002183 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002184 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002185
Tim Peters0bc93f52003-02-02 18:29:33 +00002186 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002187 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002188
Tim Peters0bc93f52003-02-02 18:29:33 +00002189 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002190 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002191
Tim Peters0bc93f52003-02-02 18:29:33 +00002192 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002193 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002194
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002195 if (put(self, args) < 0)
2196 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002197
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002198 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002199
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002200 finally:
2201 Py_XDECREF(module);
2202 Py_XDECREF(global_name);
2203 Py_XDECREF(mod);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002204
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002205 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002206}
2207
Guido van Rossum60456fd1997-04-09 17:36:32 +00002208static int
Tim Peterscba30e22003-02-01 06:24:36 +00002209save_pers(Picklerobject *self, PyObject *args, PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002210{
2211 PyObject *pid = 0;
2212 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002213
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002214 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002215
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002216 Py_INCREF(args);
2217 ARG_TUP(self, args);
2218 if (self->arg) {
2219 pid = PyObject_Call(f, self->arg, NULL);
2220 FREE_ARG_TUP(self);
2221 }
2222 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002223
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002224 if (pid != Py_None) {
2225 if (!self->bin) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002226 if (!PyString_Check(pid)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002227 PyErr_SetString(PicklingError,
2228 "persistent id must be string");
2229 goto finally;
2230 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002231
Tim Peters0bc93f52003-02-02 18:29:33 +00002232 if (self->write_func(self, &persid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002233 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002234
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002235 if ((size = PyString_Size(pid)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002236 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002237
Tim Peters0bc93f52003-02-02 18:29:33 +00002238 if (self->write_func(self,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002239 PyString_AS_STRING(
2240 (PyStringObject *)pid),
Tim Peters0bc93f52003-02-02 18:29:33 +00002241 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002242 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002243
Tim Peters0bc93f52003-02-02 18:29:33 +00002244 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002245 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002246
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002247 res = 1;
2248 goto finally;
2249 }
2250 else if (save(self, pid, 1) >= 0) {
Tim Peters0bc93f52003-02-02 18:29:33 +00002251 if (self->write_func(self, &binpersid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002252 res = -1;
2253 else
2254 res = 1;
2255 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002256
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002257 goto finally;
2258 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002259
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002260 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002261
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002262 finally:
2263 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002264
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002265 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002266}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002267
Tim Peters71fcda52003-02-14 23:05:28 +00002268/* We're saving ob, and args is the 2-thru-5 tuple returned by the
2269 * appropriate __reduce__ method for ob.
2270 */
Tim Peters84e87f32001-03-17 04:50:51 +00002271static int
Amaury Forgeot d'Arcc353ea72008-10-30 21:29:12 +00002272save_reduce(Picklerobject *self, PyObject *args, PyObject *fn, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002273{
Tim Peters71fcda52003-02-14 23:05:28 +00002274 PyObject *callable;
2275 PyObject *argtup;
Amaury Forgeot d'Arcc353ea72008-10-30 21:29:12 +00002276 PyObject *state = NULL;
2277 PyObject *listitems = Py_None;
2278 PyObject *dictitems = Py_None;
2279 Py_ssize_t size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002280
Tim Peters71fcda52003-02-14 23:05:28 +00002281 int use_newobj = self->proto >= 2;
2282
2283 static char reduce = REDUCE;
2284 static char build = BUILD;
2285 static char newobj = NEWOBJ;
2286
Amaury Forgeot d'Arcc353ea72008-10-30 21:29:12 +00002287 size = PyTuple_Size(args);
2288 if (size < 2 || size > 5) {
2289 cPickle_ErrFormat(PicklingError, "tuple returned by "
2290 "%s must contain 2 through 5 elements",
2291 "O", fn);
2292 return -1;
2293 }
2294
Tim Peters71fcda52003-02-14 23:05:28 +00002295 if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2296 &callable,
2297 &argtup,
2298 &state,
2299 &listitems,
2300 &dictitems))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002301 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002302
Raymond Hettingera6b45cc2004-12-07 07:05:57 +00002303 if (!PyTuple_Check(argtup)) {
Amaury Forgeot d'Arcc353ea72008-10-30 21:29:12 +00002304 cPickle_ErrFormat(PicklingError, "Second element of "
2305 "tuple returned by %s must be a tuple",
2306 "O", fn);
Raymond Hettingera6b45cc2004-12-07 07:05:57 +00002307 return -1;
2308 }
2309
Tim Peters71fcda52003-02-14 23:05:28 +00002310 if (state == Py_None)
2311 state = NULL;
Amaury Forgeot d'Arcc353ea72008-10-30 21:29:12 +00002312
Tim Peters71fcda52003-02-14 23:05:28 +00002313 if (listitems == Py_None)
2314 listitems = NULL;
Amaury Forgeot d'Arcc353ea72008-10-30 21:29:12 +00002315 else if (!PyIter_Check(listitems)) {
2316 cPickle_ErrFormat(PicklingError, "Fourth element of "
2317 "tuple returned by %s must be an iterator, not %s",
2318 "Os", fn, Py_TYPE(listitems)->tp_name);
2319 return -1;
2320 }
2321
Tim Peters71fcda52003-02-14 23:05:28 +00002322 if (dictitems == Py_None)
2323 dictitems = NULL;
Amaury Forgeot d'Arcc353ea72008-10-30 21:29:12 +00002324 else if (!PyIter_Check(dictitems)) {
2325 cPickle_ErrFormat(PicklingError, "Fifth element of "
2326 "tuple returned by %s must be an iterator, not %s",
2327 "Os", fn, Py_TYPE(dictitems)->tp_name);
2328 return -1;
2329 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002330
Tim Peters71fcda52003-02-14 23:05:28 +00002331 /* Protocol 2 special case: if callable's name is __newobj__, use
2332 * NEWOBJ. This consumes a lot of code.
2333 */
2334 if (use_newobj) {
2335 PyObject *temp = PyObject_GetAttr(callable, __name___str);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002336
Tim Peters71fcda52003-02-14 23:05:28 +00002337 if (temp == NULL) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002338 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2339 PyErr_Clear();
2340 else
2341 return -1;
Tim Peters71fcda52003-02-14 23:05:28 +00002342 use_newobj = 0;
2343 }
2344 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002345 use_newobj = PyString_Check(temp) &&
2346 strcmp(PyString_AS_STRING(temp),
Tim Peters71fcda52003-02-14 23:05:28 +00002347 "__newobj__") == 0;
2348 Py_DECREF(temp);
2349 }
2350 }
2351 if (use_newobj) {
2352 PyObject *cls;
2353 PyObject *newargtup;
2354 int n, i;
2355
2356 /* Sanity checks. */
2357 n = PyTuple_Size(argtup);
2358 if (n < 1) {
2359 PyErr_SetString(PicklingError, "__newobj__ arglist "
2360 "is empty");
2361 return -1;
2362 }
2363
2364 cls = PyTuple_GET_ITEM(argtup, 0);
2365 if (! PyObject_HasAttrString(cls, "__new__")) {
2366 PyErr_SetString(PicklingError, "args[0] from "
2367 "__newobj__ args has no __new__");
2368 return -1;
2369 }
2370
2371 /* XXX How could ob be NULL? */
2372 if (ob != NULL) {
2373 PyObject *ob_dot_class;
2374
2375 ob_dot_class = PyObject_GetAttr(ob, __class___str);
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002376 if (ob_dot_class == NULL) {
2377 if (PyErr_ExceptionMatches(
2378 PyExc_AttributeError))
2379 PyErr_Clear();
2380 else
2381 return -1;
2382 }
Tim Peters71fcda52003-02-14 23:05:28 +00002383 i = ob_dot_class != cls; /* true iff a problem */
2384 Py_XDECREF(ob_dot_class);
2385 if (i) {
2386 PyErr_SetString(PicklingError, "args[0] from "
2387 "__newobj__ args has the wrong class");
2388 return -1;
2389 }
2390 }
2391
2392 /* Save the class and its __new__ arguments. */
2393 if (save(self, cls, 0) < 0)
2394 return -1;
2395
2396 newargtup = PyTuple_New(n-1); /* argtup[1:] */
2397 if (newargtup == NULL)
2398 return -1;
2399 for (i = 1; i < n; ++i) {
2400 PyObject *temp = PyTuple_GET_ITEM(argtup, i);
2401 Py_INCREF(temp);
2402 PyTuple_SET_ITEM(newargtup, i-1, temp);
2403 }
Neal Norwitz5a29dd32007-10-05 05:01:38 +00002404 i = save(self, newargtup, 0);
Tim Peters71fcda52003-02-14 23:05:28 +00002405 Py_DECREF(newargtup);
2406 if (i < 0)
2407 return -1;
2408
2409 /* Add NEWOBJ opcode. */
2410 if (self->write_func(self, &newobj, 1) < 0)
2411 return -1;
2412 }
2413 else {
2414 /* Not using NEWOBJ. */
2415 if (save(self, callable, 0) < 0 ||
2416 save(self, argtup, 0) < 0 ||
2417 self->write_func(self, &reduce, 1) < 0)
2418 return -1;
2419 }
2420
2421 /* Memoize. */
2422 /* XXX How can ob be NULL? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002423 if (ob != NULL) {
2424 if (state && !PyDict_Check(state)) {
2425 if (put2(self, ob) < 0)
2426 return -1;
2427 }
Tim Peters71fcda52003-02-14 23:05:28 +00002428 else if (put(self, ob) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002429 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002430 }
Tim Peters84e87f32001-03-17 04:50:51 +00002431
Guido van Rossum60456fd1997-04-09 17:36:32 +00002432
Tim Peters71fcda52003-02-14 23:05:28 +00002433 if (listitems && batch_list(self, listitems) < 0)
2434 return -1;
2435
2436 if (dictitems && batch_dict(self, dictitems) < 0)
2437 return -1;
2438
2439 if (state) {
2440 if (save(self, state, 0) < 0 ||
2441 self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002442 return -1;
2443 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002444
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002445 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002446}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002447
Guido van Rossum60456fd1997-04-09 17:36:32 +00002448static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00002449save(Picklerobject *self, PyObject *args, int pers_save)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002450{
2451 PyTypeObject *type;
Tim Peters71fcda52003-02-14 23:05:28 +00002452 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
Tim Peters71fcda52003-02-14 23:05:28 +00002453 int res = -1;
Amaury Forgeot d'Arcc353ea72008-10-30 21:29:12 +00002454 int tmp;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002455
Facundo Batista763d3092008-06-30 01:10:55 +00002456 if (Py_EnterRecursiveCall(" while pickling an object"))
2457 return -1;
Martin v. Löwis5a395302002-08-04 08:20:23 +00002458
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002459 if (!pers_save && self->pers_func) {
2460 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2461 res = tmp;
2462 goto finally;
2463 }
2464 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002465
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002466 if (args == Py_None) {
2467 res = save_none(self, args);
2468 goto finally;
2469 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002470
Christian Heimese93237d2007-12-19 02:37:44 +00002471 type = Py_TYPE(args);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002472
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002473 switch (type->tp_name[0]) {
Guido van Rossum77f6a652002-04-03 22:41:51 +00002474 case 'b':
2475 if (args == Py_False || args == Py_True) {
2476 res = save_bool(self, args);
2477 goto finally;
2478 }
2479 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002480 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002481 if (type == &PyInt_Type) {
2482 res = save_int(self, args);
2483 goto finally;
2484 }
2485 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002486
Guido van Rossum60456fd1997-04-09 17:36:32 +00002487 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002488 if (type == &PyLong_Type) {
2489 res = save_long(self, args);
2490 goto finally;
2491 }
2492 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002493
Guido van Rossum60456fd1997-04-09 17:36:32 +00002494 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002495 if (type == &PyFloat_Type) {
2496 res = save_float(self, args);
2497 goto finally;
2498 }
2499 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002500
Guido van Rossum60456fd1997-04-09 17:36:32 +00002501 case 't':
Tim Peters1d63c9f2003-02-02 20:29:39 +00002502 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2503 res = save_tuple(self, args);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002504 goto finally;
2505 }
2506 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002507
Guido van Rossum60456fd1997-04-09 17:36:32 +00002508 case 's':
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002509 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002510 res = save_string(self, args, 0);
2511 goto finally;
2512 }
Facundo Batista14618862008-06-22 15:27:10 +00002513 break;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002514
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002515#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002516 case 'u':
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002517 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002518 res = save_unicode(self, args, 0);
2519 goto finally;
2520 }
Facundo Batista14618862008-06-22 15:27:10 +00002521 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002522#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002523 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002524
Christian Heimese93237d2007-12-19 02:37:44 +00002525 if (Py_REFCNT(args) > 1) {
Tim Peterscba30e22003-02-01 06:24:36 +00002526 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002527 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002528
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002529 if (PyDict_GetItem(self->memo, py_ob_id)) {
2530 if (get(self, py_ob_id) < 0)
2531 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002532
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002533 res = 0;
2534 goto finally;
2535 }
2536 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002537
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002538 switch (type->tp_name[0]) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002539 case 's':
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002540 if (type == &PyString_Type) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002541 res = save_string(self, args, 1);
2542 goto finally;
2543 }
2544 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002545
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002546#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002547 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002548 if (type == &PyUnicode_Type) {
2549 res = save_unicode(self, args, 1);
2550 goto finally;
2551 }
2552 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002553#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002554
Guido van Rossum60456fd1997-04-09 17:36:32 +00002555 case 't':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002556 if (type == &PyTuple_Type) {
2557 res = save_tuple(self, args);
2558 goto finally;
2559 }
2560 if (type == &PyType_Type) {
2561 res = save_global(self, args, NULL);
2562 goto finally;
2563 }
2564 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002565
Guido van Rossum60456fd1997-04-09 17:36:32 +00002566 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002567 if (type == &PyList_Type) {
2568 res = save_list(self, args);
2569 goto finally;
2570 }
2571 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002572
2573 case 'd':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002574 if (type == &PyDict_Type) {
2575 res = save_dict(self, args);
2576 goto finally;
2577 }
2578 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002579
2580 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002581 if (type == &PyInstance_Type) {
2582 res = save_inst(self, args);
2583 goto finally;
2584 }
2585 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002586
2587 case 'c':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002588 if (type == &PyClass_Type) {
2589 res = save_global(self, args, NULL);
2590 goto finally;
2591 }
2592 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002593
2594 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002595 if (type == &PyFunction_Type) {
2596 res = save_global(self, args, NULL);
Christian Tismer2460c622004-02-26 16:21:45 +00002597 if (res && PyErr_ExceptionMatches(PickleError)) {
2598 /* fall back to reduce */
2599 PyErr_Clear();
2600 break;
2601 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002602 goto finally;
2603 }
2604 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002605
2606 case 'b':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002607 if (type == &PyCFunction_Type) {
2608 res = save_global(self, args, NULL);
2609 goto finally;
2610 }
2611 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002612
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002613 if (!pers_save && self->inst_pers_func) {
2614 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2615 res = tmp;
2616 goto finally;
2617 }
2618 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002619
Jeremy Hylton39c61162002-07-16 19:47:43 +00002620 if (PyType_IsSubtype(type, &PyType_Type)) {
2621 res = save_global(self, args, NULL);
2622 goto finally;
2623 }
2624
Guido van Rossumb289b872003-02-19 01:45:13 +00002625 /* Get a reduction callable, and call it. This may come from
Georg Brandldffbf5f2008-05-20 07:49:57 +00002626 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
Guido van Rossumb289b872003-02-19 01:45:13 +00002627 * or the object's __reduce__ method.
Tim Peters71fcda52003-02-14 23:05:28 +00002628 */
Tim Peters5aa3da62003-02-13 21:03:57 +00002629 __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2630 if (__reduce__ != NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002631 Py_INCREF(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00002632 Py_INCREF(args);
2633 ARG_TUP(self, args);
2634 if (self->arg) {
2635 t = PyObject_Call(__reduce__, self->arg, NULL);
2636 FREE_ARG_TUP(self);
2637 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002638 }
2639 else {
Guido van Rossumb289b872003-02-19 01:45:13 +00002640 /* Check for a __reduce_ex__ method. */
2641 __reduce__ = PyObject_GetAttr(args, __reduce_ex___str);
2642 if (__reduce__ != NULL) {
2643 t = PyInt_FromLong(self->proto);
2644 if (t != NULL) {
2645 ARG_TUP(self, t);
2646 t = NULL;
2647 if (self->arg) {
2648 t = PyObject_Call(__reduce__,
2649 self->arg, NULL);
2650 FREE_ARG_TUP(self);
2651 }
2652 }
2653 }
2654 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002655 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2656 PyErr_Clear();
2657 else
2658 goto finally;
Guido van Rossumb289b872003-02-19 01:45:13 +00002659 /* Check for a __reduce__ method. */
2660 __reduce__ = PyObject_GetAttr(args, __reduce___str);
2661 if (__reduce__ != NULL) {
2662 t = PyObject_Call(__reduce__,
2663 empty_tuple, NULL);
2664 }
2665 else {
2666 PyErr_SetObject(UnpickleableError, args);
2667 goto finally;
2668 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002669 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002670 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002671
Tim Peters71fcda52003-02-14 23:05:28 +00002672 if (t == NULL)
2673 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002674
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002675 if (PyString_Check(t)) {
Tim Peters71fcda52003-02-14 23:05:28 +00002676 res = save_global(self, args, t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002677 goto finally;
2678 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002679
Amaury Forgeot d'Arcc353ea72008-10-30 21:29:12 +00002680 if (!PyTuple_Check(t)) {
Tim Peters71fcda52003-02-14 23:05:28 +00002681 cPickle_ErrFormat(PicklingError, "Value returned by "
2682 "%s must be string or tuple",
2683 "O", __reduce__);
2684 goto finally;
2685 }
2686
Amaury Forgeot d'Arcc353ea72008-10-30 21:29:12 +00002687 res = save_reduce(self, t, __reduce__, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002688
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002689 finally:
Facundo Batista763d3092008-06-30 01:10:55 +00002690 Py_LeaveRecursiveCall();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002691 Py_XDECREF(py_ob_id);
2692 Py_XDECREF(__reduce__);
2693 Py_XDECREF(t);
Tim Peters84e87f32001-03-17 04:50:51 +00002694
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002695 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002696}
2697
2698
2699static int
Tim Peterscba30e22003-02-01 06:24:36 +00002700dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002701{
2702 static char stop = STOP;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002703
Tim Peters4190fb82003-02-02 16:09:05 +00002704 if (self->proto >= 2) {
2705 char bytes[2];
2706
2707 bytes[0] = PROTO;
Tim Peters87482ea2003-02-02 16:16:30 +00002708 assert(self->proto >= 0 && self->proto < 256);
2709 bytes[1] = (char)self->proto;
Tim Peters4190fb82003-02-02 16:09:05 +00002710 if (self->write_func(self, bytes, 2) < 0)
2711 return -1;
2712 }
2713
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002714 if (save(self, args, 0) < 0)
2715 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002716
Tim Peters4190fb82003-02-02 16:09:05 +00002717 if (self->write_func(self, &stop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002718 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002719
Tim Peters4190fb82003-02-02 16:09:05 +00002720 if (self->write_func(self, NULL, 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002721 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002722
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002723 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002724}
2725
2726static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002727Pickle_clear_memo(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002728{
Tim Peterscba30e22003-02-01 06:24:36 +00002729 if (self->memo)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002730 PyDict_Clear(self->memo);
2731 Py_INCREF(Py_None);
2732 return Py_None;
2733}
2734
2735static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002736Pickle_getvalue(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002737{
2738 int l, i, rsize, ssize, clear=1, lm;
2739 long ik;
2740 PyObject *k, *r;
2741 char *s, *p, *have_get;
2742 Pdata *data;
2743
2744 /* Can be called by Python code or C code */
Tim Peterscba30e22003-02-01 06:24:36 +00002745 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002746 return NULL;
2747
2748 /* Check to make sure we are based on a list */
2749 if (! Pdata_Check(self->file)) {
2750 PyErr_SetString(PicklingError,
2751 "Attempt to getvalue() a non-list-based pickler");
2752 return NULL;
2753 }
2754
2755 /* flush write buffer */
2756 if (write_other(self, NULL, 0) < 0) return NULL;
2757
2758 data=(Pdata*)self->file;
2759 l=data->length;
2760
2761 /* set up an array to hold get/put status */
Tim Petersac5687a2003-02-02 18:08:34 +00002762 lm = PyDict_Size(self->memo);
2763 if (lm < 0) return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002764 lm++;
Tim Petersac5687a2003-02-02 18:08:34 +00002765 have_get = malloc(lm);
2766 if (have_get == NULL) return PyErr_NoMemory();
2767 memset(have_get, 0, lm);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002768
2769 /* Scan for gets. */
Tim Petersac5687a2003-02-02 18:08:34 +00002770 for (rsize = 0, i = l; --i >= 0; ) {
2771 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002772
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002773 if (PyString_Check(k))
2774 rsize += PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002775
2776 else if (PyInt_Check(k)) { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002777 ik = PyInt_AS_LONG((PyIntObject*)k);
2778 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002779 PyErr_SetString(PicklingError,
2780 "Invalid get data");
Neal Norwitz98a96002006-07-23 07:57:11 +00002781 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002782 }
Tim Petersac5687a2003-02-02 18:08:34 +00002783 if (have_get[ik]) /* with matching get */
2784 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002785 }
2786
2787 else if (! (PyTuple_Check(k) &&
2788 PyTuple_GET_SIZE(k) == 2 &&
Tim Petersac5687a2003-02-02 18:08:34 +00002789 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002790 ) {
2791 PyErr_SetString(PicklingError,
2792 "Unexpected data in internal list");
Neal Norwitz98a96002006-07-23 07:57:11 +00002793 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002794 }
2795
2796 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002797 ik = PyInt_AS_LONG((PyIntObject *)k);
2798 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002799 PyErr_SetString(PicklingError,
2800 "Invalid get data");
2801 return NULL;
2802 }
Tim Petersac5687a2003-02-02 18:08:34 +00002803 have_get[ik] = 1;
2804 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002805 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002806 }
2807
2808 /* Now generate the result */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002809 r = PyString_FromStringAndSize(NULL, rsize);
Tim Petersac5687a2003-02-02 18:08:34 +00002810 if (r == NULL) goto err;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002811 s = PyString_AS_STRING((PyStringObject *)r);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002812
Tim Petersac5687a2003-02-02 18:08:34 +00002813 for (i = 0; i < l; i++) {
2814 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002815
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002816 if (PyString_Check(k)) {
2817 ssize = PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002818 if (ssize) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002819 p=PyString_AS_STRING((PyStringObject *)k);
Tim Petersac5687a2003-02-02 18:08:34 +00002820 while (--ssize >= 0)
2821 *s++ = *p++;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002822 }
2823 }
2824
2825 else if (PyTuple_Check(k)) { /* get */
Tim Petersac5687a2003-02-02 18:08:34 +00002826 ik = PyInt_AS_LONG((PyIntObject *)
2827 PyTuple_GET_ITEM(k, 0));
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002828 if (ik < 256) {
2829 *s++ = BINGET;
2830 *s++ = (int)(ik & 0xff);
2831 }
2832 else {
2833 *s++ = LONG_BINGET;
2834 *s++ = (int)(ik & 0xff);
2835 *s++ = (int)((ik >> 8) & 0xff);
2836 *s++ = (int)((ik >> 16) & 0xff);
2837 *s++ = (int)((ik >> 24) & 0xff);
2838 }
2839 }
2840
2841 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002842 ik = PyInt_AS_LONG((PyIntObject*)k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002843
2844 if (have_get[ik]) { /* with matching get */
2845 if (ik < 256) {
2846 *s++ = BINPUT;
2847 *s++ = (int)(ik & 0xff);
2848 }
2849 else {
2850 *s++ = LONG_BINPUT;
2851 *s++ = (int)(ik & 0xff);
2852 *s++ = (int)((ik >> 8) & 0xff);
2853 *s++ = (int)((ik >> 16) & 0xff);
2854 *s++ = (int)((ik >> 24) & 0xff);
2855 }
2856 }
2857 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002858 }
2859
2860 if (clear) {
2861 PyDict_Clear(self->memo);
Tim Petersac5687a2003-02-02 18:08:34 +00002862 Pdata_clear(data, 0);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002863 }
2864
2865 free(have_get);
2866 return r;
2867 err:
2868 free(have_get);
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002869 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002870}
2871
2872static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002873Pickler_dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002874{
2875 PyObject *ob;
2876 int get=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002877
Tim Peterscba30e22003-02-01 06:24:36 +00002878 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002879 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002880
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002881 if (dump(self, ob) < 0)
2882 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002883
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002884 if (get) return Pickle_getvalue(self, NULL);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002885
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002886 /* XXX Why does dump() return self? */
2887 Py_INCREF(self);
2888 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002889}
2890
2891
Tim Peterscba30e22003-02-01 06:24:36 +00002892static struct PyMethodDef Pickler_methods[] =
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002893{
Neal Norwitzb0493252002-03-31 14:44:22 +00002894 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002895 PyDoc_STR("dump(object) -- "
2896 "Write an object in pickle format to the object's pickle stream")},
Fred Drake0ebacc82002-05-01 20:36:39 +00002897 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002898 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
Neal Norwitzb0493252002-03-31 14:44:22 +00002899 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002900 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
Guido van Rossum60456fd1997-04-09 17:36:32 +00002901 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002902};
2903
2904
2905static Picklerobject *
Tim Peters5bd2a792003-02-01 16:45:06 +00002906newPicklerobject(PyObject *file, int proto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002907{
2908 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002909
Tim Peters5bd2a792003-02-01 16:45:06 +00002910 if (proto < 0)
Tim Peters8587b3c2003-02-13 15:44:41 +00002911 proto = HIGHEST_PROTOCOL;
2912 if (proto > HIGHEST_PROTOCOL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002913 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
2914 "the highest available protocol is %d",
Tim Peters8587b3c2003-02-13 15:44:41 +00002915 proto, HIGHEST_PROTOCOL);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002916 return NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002917 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002918
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002919 self = PyObject_GC_New(Picklerobject, &Picklertype);
Tim Peters5bd2a792003-02-01 16:45:06 +00002920 if (self == NULL)
2921 return NULL;
2922 self->proto = proto;
2923 self->bin = proto > 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002924 self->fp = NULL;
2925 self->write = NULL;
2926 self->memo = NULL;
2927 self->arg = NULL;
2928 self->pers_func = NULL;
2929 self->inst_pers_func = NULL;
2930 self->write_buf = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002931 self->fast = 0;
2932 self->fast_container = 0;
2933 self->fast_memo = NULL;
2934 self->buf_size = 0;
2935 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002936
Tim Peters5bd2a792003-02-01 16:45:06 +00002937 self->file = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002938 if (file)
2939 Py_INCREF(file);
Tim Peters5bd2a792003-02-01 16:45:06 +00002940 else {
2941 file = Pdata_New();
2942 if (file == NULL)
2943 goto err;
2944 }
2945 self->file = file;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002946
Tim Peterscba30e22003-02-01 06:24:36 +00002947 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002948 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002949
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002950 if (PyFile_Check(file)) {
2951 self->fp = PyFile_AsFile(file);
2952 if (self->fp == NULL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002953 PyErr_SetString(PyExc_ValueError,
2954 "I/O operation on closed file");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002955 goto err;
2956 }
2957 self->write_func = write_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002958 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002959 else if (PycStringIO_OutputCheck(file)) {
2960 self->write_func = write_cStringIO;
2961 }
2962 else if (file == Py_None) {
2963 self->write_func = write_none;
2964 }
2965 else {
2966 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002967
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002968 if (! Pdata_Check(file)) {
2969 self->write = PyObject_GetAttr(file, write_str);
2970 if (!self->write) {
2971 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00002972 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002973 "argument must have 'write' "
2974 "attribute");
2975 goto err;
2976 }
2977 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002978
Tim Peters5bd2a792003-02-01 16:45:06 +00002979 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
2980 if (self->write_buf == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002981 PyErr_NoMemory();
2982 goto err;
2983 }
2984 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002985
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002986 if (PyEval_GetRestricted()) {
2987 /* Restricted execution, get private tables */
Alexandre Vassalotti513c46e2009-11-24 18:06:51 +00002988 PyObject *m = PyImport_ImportModule("copy_reg");
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002989
Tim Peters5b7da392003-02-04 00:21:07 +00002990 if (m == NULL)
2991 goto err;
2992 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002993 Py_DECREF(m);
Tim Peters5b7da392003-02-04 00:21:07 +00002994 if (self->dispatch_table == NULL)
2995 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002996 }
2997 else {
Tim Peters5b7da392003-02-04 00:21:07 +00002998 self->dispatch_table = dispatch_table;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002999 Py_INCREF(dispatch_table);
3000 }
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003001 PyObject_GC_Track(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003002
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003003 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003004
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003005 err:
Tim Peters5bd2a792003-02-01 16:45:06 +00003006 Py_DECREF(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003007 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003008}
3009
3010
3011static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00003012get_Pickler(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003013{
Martin v. Löwis15e62742006-02-27 16:46:16 +00003014 static char *kwlist[] = {"file", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003015 PyObject *file = NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00003016 int proto = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003017
Tim Peters92c8bb32003-02-13 23:00:26 +00003018 /* XXX
Martin v. Löwis544f1192004-07-27 05:22:33 +00003019 * The documented signature is Pickler(file, protocol=0), but this
Tim Peters92c8bb32003-02-13 23:00:26 +00003020 * accepts Pickler() and Pickler(integer) too. The meaning then
3021 * is clear as mud, undocumented, and not supported by pickle.py.
3022 * I'm told Zope uses this, but I haven't traced into this code
3023 * far enough to figure out what it means.
Tim Peters5bd2a792003-02-01 16:45:06 +00003024 */
3025 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003026 PyErr_Clear();
Tim Peters5bd2a792003-02-01 16:45:06 +00003027 proto = 0;
Martin v. Löwis544f1192004-07-27 05:22:33 +00003028 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler",
3029 kwlist, &file, &proto))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003030 return NULL;
3031 }
Tim Peters5bd2a792003-02-01 16:45:06 +00003032 return (PyObject *)newPicklerobject(file, proto);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003033}
3034
3035
3036static void
Tim Peterscba30e22003-02-01 06:24:36 +00003037Pickler_dealloc(Picklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003038{
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003039 PyObject_GC_UnTrack(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003040 Py_XDECREF(self->write);
3041 Py_XDECREF(self->memo);
3042 Py_XDECREF(self->fast_memo);
3043 Py_XDECREF(self->arg);
3044 Py_XDECREF(self->file);
3045 Py_XDECREF(self->pers_func);
3046 Py_XDECREF(self->inst_pers_func);
3047 Py_XDECREF(self->dispatch_table);
Tim Peters5bd2a792003-02-01 16:45:06 +00003048 PyMem_Free(self->write_buf);
Christian Heimese93237d2007-12-19 02:37:44 +00003049 Py_TYPE(self)->tp_free((PyObject *)self);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003050}
3051
3052static int
3053Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)
3054{
Thomas Woutersc6e55062006-04-15 21:47:09 +00003055 Py_VISIT(self->write);
3056 Py_VISIT(self->memo);
3057 Py_VISIT(self->fast_memo);
3058 Py_VISIT(self->arg);
3059 Py_VISIT(self->file);
3060 Py_VISIT(self->pers_func);
3061 Py_VISIT(self->inst_pers_func);
3062 Py_VISIT(self->dispatch_table);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003063 return 0;
3064}
3065
3066static int
3067Pickler_clear(Picklerobject *self)
3068{
Thomas Woutersedf17d82006-04-15 17:28:34 +00003069 Py_CLEAR(self->write);
3070 Py_CLEAR(self->memo);
3071 Py_CLEAR(self->fast_memo);
3072 Py_CLEAR(self->arg);
3073 Py_CLEAR(self->file);
3074 Py_CLEAR(self->pers_func);
3075 Py_CLEAR(self->inst_pers_func);
3076 Py_CLEAR(self->dispatch_table);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003077 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003078}
3079
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003080static PyObject *
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003081Pickler_get_pers_func(Picklerobject *p)
3082{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003083 if (p->pers_func == NULL)
3084 PyErr_SetString(PyExc_AttributeError, "persistent_id");
3085 else
3086 Py_INCREF(p->pers_func);
3087 return p->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003088}
3089
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003090static int
3091Pickler_set_pers_func(Picklerobject *p, PyObject *v)
3092{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003093 if (v == NULL) {
3094 PyErr_SetString(PyExc_TypeError,
3095 "attribute deletion is not supported");
3096 return -1;
3097 }
3098 Py_XDECREF(p->pers_func);
3099 Py_INCREF(v);
3100 p->pers_func = v;
3101 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003102}
3103
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003104static int
3105Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
3106{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003107 if (v == NULL) {
3108 PyErr_SetString(PyExc_TypeError,
3109 "attribute deletion is not supported");
3110 return -1;
3111 }
3112 Py_XDECREF(p->inst_pers_func);
3113 Py_INCREF(v);
3114 p->inst_pers_func = v;
3115 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003116}
3117
3118static PyObject *
3119Pickler_get_memo(Picklerobject *p)
3120{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003121 if (p->memo == NULL)
3122 PyErr_SetString(PyExc_AttributeError, "memo");
3123 else
3124 Py_INCREF(p->memo);
3125 return p->memo;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003126}
3127
3128static int
3129Pickler_set_memo(Picklerobject *p, PyObject *v)
3130{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003131 if (v == NULL) {
3132 PyErr_SetString(PyExc_TypeError,
3133 "attribute deletion is not supported");
3134 return -1;
3135 }
3136 if (!PyDict_Check(v)) {
3137 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
3138 return -1;
3139 }
3140 Py_XDECREF(p->memo);
3141 Py_INCREF(v);
3142 p->memo = v;
3143 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003144}
3145
3146static PyObject *
3147Pickler_get_error(Picklerobject *p)
3148{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003149 /* why is this an attribute on the Pickler? */
3150 Py_INCREF(PicklingError);
3151 return PicklingError;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003152}
3153
3154static PyMemberDef Pickler_members[] = {
3155 {"binary", T_INT, offsetof(Picklerobject, bin)},
3156 {"fast", T_INT, offsetof(Picklerobject, fast)},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003157 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003158};
3159
3160static PyGetSetDef Pickler_getsets[] = {
Tim Peterscba30e22003-02-01 06:24:36 +00003161 {"persistent_id", (getter)Pickler_get_pers_func,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003162 (setter)Pickler_set_pers_func},
3163 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
3164 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003165 {"PicklingError", (getter)Pickler_get_error, NULL},
3166 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003167};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003168
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003169PyDoc_STRVAR(Picklertype__doc__,
3170"Objects that know how to pickle objects\n");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003171
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003172static PyTypeObject Picklertype = {
Martin v. Löwis68192102007-07-21 06:55:02 +00003173 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00003174 "cPickle.Pickler", /*tp_name*/
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003175 sizeof(Picklerobject), /*tp_basicsize*/
3176 0,
3177 (destructor)Pickler_dealloc, /* tp_dealloc */
3178 0, /* tp_print */
3179 0, /* tp_getattr */
3180 0, /* tp_setattr */
3181 0, /* tp_compare */
3182 0, /* tp_repr */
3183 0, /* tp_as_number */
3184 0, /* tp_as_sequence */
3185 0, /* tp_as_mapping */
3186 0, /* tp_hash */
3187 0, /* tp_call */
3188 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00003189 PyObject_GenericGetAttr, /* tp_getattro */
3190 PyObject_GenericSetAttr, /* tp_setattro */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003191 0, /* tp_as_buffer */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003192 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003193 Picklertype__doc__, /* tp_doc */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003194 (traverseproc)Pickler_traverse, /* tp_traverse */
3195 (inquiry)Pickler_clear, /* tp_clear */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003196 0, /* tp_richcompare */
3197 0, /* tp_weaklistoffset */
3198 0, /* tp_iter */
3199 0, /* tp_iternext */
3200 Pickler_methods, /* tp_methods */
3201 Pickler_members, /* tp_members */
3202 Pickler_getsets, /* tp_getset */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003203};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003204
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003205static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003206find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003207{
3208 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003209
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003210 if (fc) {
3211 if (fc==Py_None) {
Tim Peters92c8bb32003-02-13 23:00:26 +00003212 PyErr_SetString(UnpicklingError, "Global and instance "
3213 "pickles are not supported.");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003214 return NULL;
3215 }
Georg Brandl684fd0c2006-05-25 19:15:31 +00003216 return PyObject_CallFunctionObjArgs(fc, py_module_name,
3217 py_global_name, NULL);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003218 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003219
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003220 module = PySys_GetObject("modules");
3221 if (module == NULL)
3222 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00003223
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003224 module = PyDict_GetItem(module, py_module_name);
3225 if (module == NULL) {
3226 module = PyImport_Import(py_module_name);
3227 if (!module)
3228 return NULL;
3229 global = PyObject_GetAttr(module, py_global_name);
3230 Py_DECREF(module);
3231 }
3232 else
3233 global = PyObject_GetAttr(module, py_global_name);
3234 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003235}
3236
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003237static int
Tim Peterscba30e22003-02-01 06:24:36 +00003238marker(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003239{
3240 if (self->num_marks < 1) {
3241 PyErr_SetString(UnpicklingError, "could not find MARK");
3242 return -1;
3243 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003244
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003245 return self->marks[--self->num_marks];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003246}
3247
Tim Peters84e87f32001-03-17 04:50:51 +00003248
Guido van Rossum60456fd1997-04-09 17:36:32 +00003249static int
Tim Peterscba30e22003-02-01 06:24:36 +00003250load_none(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003251{
3252 PDATA_APPEND(self->stack, Py_None, -1);
3253 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003254}
3255
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003256static int
Tim Peterscba30e22003-02-01 06:24:36 +00003257bad_readline(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003258{
3259 PyErr_SetString(UnpicklingError, "pickle data was truncated");
3260 return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003261}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003262
3263static int
Tim Peterscba30e22003-02-01 06:24:36 +00003264load_int(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003265{
3266 PyObject *py_int = 0;
3267 char *endptr, *s;
3268 int len, res = -1;
3269 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003270
Tim Peters0bc93f52003-02-02 18:29:33 +00003271 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003272 if (len < 2) return bad_readline();
3273 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003274
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003275 errno = 0;
3276 l = strtol(s, &endptr, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003277
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003278 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
3279 /* Hm, maybe we've got something long. Let's try reading
3280 it as a Python long object. */
3281 errno = 0;
3282 py_int = PyLong_FromString(s, NULL, 0);
3283 if (py_int == NULL) {
3284 PyErr_SetString(PyExc_ValueError,
3285 "could not convert string to int");
3286 goto finally;
3287 }
3288 }
3289 else {
Guido van Rossume2763392002-04-05 19:30:08 +00003290 if (len == 3 && (l == 0 || l == 1)) {
3291 if (!( py_int = PyBool_FromLong(l))) goto finally;
3292 }
3293 else {
3294 if (!( py_int = PyInt_FromLong(l))) goto finally;
3295 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003296 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003297
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003298 free(s);
3299 PDATA_PUSH(self->stack, py_int, -1);
3300 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003301
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003302 finally:
3303 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003304
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003305 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003306}
3307
Tim Peters3c67d792003-02-02 17:59:11 +00003308static int
3309load_bool(Unpicklerobject *self, PyObject *boolean)
3310{
3311 assert(boolean == Py_True || boolean == Py_False);
Tim Peterse0a39072003-02-03 15:45:56 +00003312 PDATA_APPEND(self->stack, boolean, -1);
Tim Peters3c67d792003-02-02 17:59:11 +00003313 return 0;
3314}
3315
Tim Petersee1a53c2003-02-02 02:57:53 +00003316/* s contains x bytes of a little-endian integer. Return its value as a
3317 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3318 * int, but when x is 4 it's a signed one. This is an historical source
3319 * of x-platform bugs.
3320 */
Tim Peters84e87f32001-03-17 04:50:51 +00003321static long
Tim Petersee1a53c2003-02-02 02:57:53 +00003322calc_binint(char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003323{
3324 unsigned char c;
3325 int i;
3326 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003327
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003328 for (i = 0, l = 0L; i < x; i++) {
3329 c = (unsigned char)s[i];
3330 l |= (long)c << (i * 8);
3331 }
Tim Petersbfa18f72001-04-10 01:54:42 +00003332#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003333 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3334 * is signed, so on a box with longs bigger than 4 bytes we need
3335 * to extend a BININT's sign bit to the full width.
3336 */
3337 if (x == 4 && l & (1L << 31))
3338 l |= (~0L) << 32;
Tim Petersbfa18f72001-04-10 01:54:42 +00003339#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003340 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003341}
3342
3343
3344static int
Tim Peterscba30e22003-02-01 06:24:36 +00003345load_binintx(Unpicklerobject *self, char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003346{
3347 PyObject *py_int = 0;
3348 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003349
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003350 l = calc_binint(s, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003351
Tim Peterscba30e22003-02-01 06:24:36 +00003352 if (!( py_int = PyInt_FromLong(l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003353 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003354
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003355 PDATA_PUSH(self->stack, py_int, -1);
3356 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003357}
3358
3359
3360static int
Tim Peterscba30e22003-02-01 06:24:36 +00003361load_binint(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003362{
3363 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003364
Tim Peters0bc93f52003-02-02 18:29:33 +00003365 if (self->read_func(self, &s, 4) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003366 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003367
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003368 return load_binintx(self, s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003369}
3370
3371
3372static int
Tim Peterscba30e22003-02-01 06:24:36 +00003373load_binint1(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003374{
3375 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003376
Tim Peters0bc93f52003-02-02 18:29:33 +00003377 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003378 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003379
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003380 return load_binintx(self, s, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003381}
3382
3383
3384static int
Tim Peterscba30e22003-02-01 06:24:36 +00003385load_binint2(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003386{
3387 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003388
Tim Peters0bc93f52003-02-02 18:29:33 +00003389 if (self->read_func(self, &s, 2) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003390 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003391
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003392 return load_binintx(self, s, 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003393}
Tim Peters84e87f32001-03-17 04:50:51 +00003394
Guido van Rossum60456fd1997-04-09 17:36:32 +00003395static int
Tim Peterscba30e22003-02-01 06:24:36 +00003396load_long(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003397{
3398 PyObject *l = 0;
3399 char *end, *s;
3400 int len, res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003401
Tim Peters0bc93f52003-02-02 18:29:33 +00003402 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003403 if (len < 2) return bad_readline();
3404 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003405
Tim Peterscba30e22003-02-01 06:24:36 +00003406 if (!( l = PyLong_FromString(s, &end, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003407 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003408
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003409 free(s);
3410 PDATA_PUSH(self->stack, l, -1);
3411 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003412
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003413 finally:
3414 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003415
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003416 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003417}
3418
Tim Petersee1a53c2003-02-02 02:57:53 +00003419/* 'size' bytes contain the # of bytes of little-endian 256's-complement
3420 * data following.
3421 */
3422static int
3423load_counted_long(Unpicklerobject *self, int size)
3424{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003425 Py_ssize_t i;
Tim Petersee1a53c2003-02-02 02:57:53 +00003426 char *nbytes;
3427 unsigned char *pdata;
3428 PyObject *along;
3429
3430 assert(size == 1 || size == 4);
3431 i = self->read_func(self, &nbytes, size);
3432 if (i < 0) return -1;
3433
3434 size = calc_binint(nbytes, size);
3435 if (size < 0) {
3436 /* Corrupt or hostile pickle -- we never write one like
3437 * this.
3438 */
Tim Peters0c7c48e2003-02-03 22:07:24 +00003439 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
Tim Petersee1a53c2003-02-02 02:57:53 +00003440 "byte count");
3441 return -1;
3442 }
3443
3444 if (size == 0)
3445 along = PyLong_FromLong(0L);
3446 else {
3447 /* Read the raw little-endian bytes & convert. */
Neal Norwitz529baf22003-02-02 17:08:33 +00003448 i = self->read_func(self, (char **)&pdata, size);
Tim Petersee1a53c2003-02-02 02:57:53 +00003449 if (i < 0) return -1;
3450 along = _PyLong_FromByteArray(pdata, (size_t)size,
3451 1 /* little endian */, 1 /* signed */);
3452 }
3453 if (along == NULL)
3454 return -1;
3455 PDATA_PUSH(self->stack, along, -1);
3456 return 0;
3457}
Tim Peters84e87f32001-03-17 04:50:51 +00003458
Guido van Rossum60456fd1997-04-09 17:36:32 +00003459static int
Tim Peterscba30e22003-02-01 06:24:36 +00003460load_float(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003461{
3462 PyObject *py_float = 0;
3463 char *endptr, *s;
3464 int len, res = -1;
3465 double d;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003466
Tim Peters0bc93f52003-02-02 18:29:33 +00003467 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003468 if (len < 2) return bad_readline();
3469 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003470
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003471 errno = 0;
Martin v. Löwis737ea822004-06-08 18:52:54 +00003472 d = PyOS_ascii_strtod(s, &endptr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003473
Mark Dickinson3df16922009-01-24 21:30:14 +00003474 if ((errno == ERANGE && !(fabs(d) <= 1.0)) ||
3475 (endptr[0] != '\n') || (endptr[1] != '\0')) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003476 PyErr_SetString(PyExc_ValueError,
3477 "could not convert string to float");
3478 goto finally;
3479 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003480
Tim Peterscba30e22003-02-01 06:24:36 +00003481 if (!( py_float = PyFloat_FromDouble(d)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003482 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003483
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003484 free(s);
3485 PDATA_PUSH(self->stack, py_float, -1);
3486 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003487
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003488 finally:
3489 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003490
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003491 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003492}
3493
Guido van Rossum60456fd1997-04-09 17:36:32 +00003494static int
Tim Peterscba30e22003-02-01 06:24:36 +00003495load_binfloat(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003496{
Tim Peters9905b942003-03-20 20:53:32 +00003497 PyObject *py_float;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003498 double x;
3499 char *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003500
Tim Peters0bc93f52003-02-02 18:29:33 +00003501 if (self->read_func(self, &p, 8) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003502 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003503
Tim Peters9905b942003-03-20 20:53:32 +00003504 x = _PyFloat_Unpack8((unsigned char *)p, 0);
3505 if (x == -1.0 && PyErr_Occurred())
3506 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003507
Tim Peters9905b942003-03-20 20:53:32 +00003508 py_float = PyFloat_FromDouble(x);
3509 if (py_float == NULL)
3510 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003511
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003512 PDATA_PUSH(self->stack, py_float, -1);
3513 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003514}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003515
3516static int
Tim Peterscba30e22003-02-01 06:24:36 +00003517load_string(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003518{
3519 PyObject *str = 0;
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003520 int len, res = -1;
3521 char *s, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003522
Tim Peters0bc93f52003-02-02 18:29:33 +00003523 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003524 if (len < 2) return bad_readline();
3525 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003526
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003527
3528 /* Strip outermost quotes */
3529 while (s[len-1] <= ' ')
3530 len--;
3531 if(s[0]=='"' && s[len-1]=='"'){
3532 s[len-1] = '\0';
3533 p = s + 1 ;
3534 len -= 2;
3535 } else if(s[0]=='\'' && s[len-1]=='\''){
3536 s[len-1] = '\0';
3537 p = s + 1 ;
3538 len -= 2;
3539 } else
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003540 goto insecure;
3541 /********************************************/
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003542
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003543 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
Neal Norwitz99dfe3c2006-08-02 04:27:11 +00003544 free(s);
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003545 if (str) {
3546 PDATA_PUSH(self->stack, str, -1);
3547 res = 0;
3548 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003549 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003550
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003551 insecure:
3552 free(s);
3553 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3554 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003555}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003556
3557
3558static int
Tim Peterscba30e22003-02-01 06:24:36 +00003559load_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003560{
3561 PyObject *py_string = 0;
3562 long l;
3563 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003564
Tim Peters0bc93f52003-02-02 18:29:33 +00003565 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003566
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003567 l = calc_binint(s, 4);
Gregory P. Smith9d534572008-06-11 07:41:16 +00003568 if (l < 0) {
3569 /* Corrupt or hostile pickle -- we never write one like
3570 * this.
3571 */
3572 PyErr_SetString(UnpicklingError,
3573 "BINSTRING pickle has negative byte count");
3574 return -1;
3575 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003576
Tim Peters0bc93f52003-02-02 18:29:33 +00003577 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003578 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003579
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003580 if (!( py_string = PyString_FromStringAndSize(s, l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003581 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003582
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003583 PDATA_PUSH(self->stack, py_string, -1);
3584 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003585}
3586
3587
3588static int
Tim Peterscba30e22003-02-01 06:24:36 +00003589load_short_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003590{
3591 PyObject *py_string = 0;
3592 unsigned char l;
3593 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003594
Tim Peters0bc93f52003-02-02 18:29:33 +00003595 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003596 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003597
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003598 l = (unsigned char)s[0];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003599
Tim Peters0bc93f52003-02-02 18:29:33 +00003600 if (self->read_func(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003601
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003602 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003603
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003604 PDATA_PUSH(self->stack, py_string, -1);
3605 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00003606}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003607
3608
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003609#ifdef Py_USING_UNICODE
Guido van Rossum60456fd1997-04-09 17:36:32 +00003610static int
Tim Peterscba30e22003-02-01 06:24:36 +00003611load_unicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003612{
3613 PyObject *str = 0;
3614 int len, res = -1;
3615 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003616
Tim Peters0bc93f52003-02-02 18:29:33 +00003617 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003618 if (len < 1) return bad_readline();
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003619
Tim Peterscba30e22003-02-01 06:24:36 +00003620 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003621 goto finally;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003622
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003623 PDATA_PUSH(self->stack, str, -1);
3624 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003625
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003626 finally:
3627 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003628}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003629#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003630
3631
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003632#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003633static int
Tim Peterscba30e22003-02-01 06:24:36 +00003634load_binunicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003635{
3636 PyObject *unicode;
3637 long l;
3638 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003639
Tim Peters0bc93f52003-02-02 18:29:33 +00003640 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003641
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003642 l = calc_binint(s, 4);
Gregory P. Smith9d534572008-06-11 07:41:16 +00003643 if (l < 0) {
3644 /* Corrupt or hostile pickle -- we never write one like
3645 * this.
3646 */
3647 PyErr_SetString(UnpicklingError,
3648 "BINUNICODE pickle has negative byte count");
3649 return -1;
3650 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003651
Tim Peters0bc93f52003-02-02 18:29:33 +00003652 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003653 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003654
Tim Peterscba30e22003-02-01 06:24:36 +00003655 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003656 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003657
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003658 PDATA_PUSH(self->stack, unicode, -1);
3659 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003660}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003661#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003662
3663
3664static int
Tim Peterscba30e22003-02-01 06:24:36 +00003665load_tuple(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003666{
3667 PyObject *tup;
3668 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003669
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003670 if ((i = marker(self)) < 0) return -1;
3671 if (!( tup=Pdata_popTuple(self->stack, i))) return -1;
3672 PDATA_PUSH(self->stack, tup, -1);
3673 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003674}
3675
3676static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00003677load_counted_tuple(Unpicklerobject *self, int len)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003678{
Tim Peters1d63c9f2003-02-02 20:29:39 +00003679 PyObject *tup = PyTuple_New(len);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003680
Tim Peters1d63c9f2003-02-02 20:29:39 +00003681 if (tup == NULL)
3682 return -1;
3683
3684 while (--len >= 0) {
3685 PyObject *element;
3686
3687 PDATA_POP(self->stack, element);
3688 if (element == NULL)
3689 return -1;
3690 PyTuple_SET_ITEM(tup, len, element);
3691 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003692 PDATA_PUSH(self->stack, tup, -1);
3693 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003694}
3695
3696static int
Tim Peterscba30e22003-02-01 06:24:36 +00003697load_empty_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003698{
3699 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003700
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003701 if (!( list=PyList_New(0))) return -1;
3702 PDATA_PUSH(self->stack, list, -1);
3703 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003704}
3705
3706static int
Tim Peterscba30e22003-02-01 06:24:36 +00003707load_empty_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003708{
3709 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003710
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003711 if (!( dict=PyDict_New())) return -1;
3712 PDATA_PUSH(self->stack, dict, -1);
3713 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003714}
3715
3716
3717static int
Tim Peterscba30e22003-02-01 06:24:36 +00003718load_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003719{
3720 PyObject *list = 0;
3721 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003722
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003723 if ((i = marker(self)) < 0) return -1;
3724 if (!( list=Pdata_popList(self->stack, i))) return -1;
3725 PDATA_PUSH(self->stack, list, -1);
3726 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003727}
3728
3729static int
Tim Peterscba30e22003-02-01 06:24:36 +00003730load_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003731{
3732 PyObject *dict, *key, *value;
3733 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003734
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003735 if ((i = marker(self)) < 0) return -1;
3736 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003737
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003738 if (!( dict = PyDict_New())) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003739
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003740 for (k = i+1; k < j; k += 2) {
3741 key =self->stack->data[k-1];
3742 value=self->stack->data[k ];
3743 if (PyDict_SetItem(dict, key, value) < 0) {
3744 Py_DECREF(dict);
3745 return -1;
3746 }
3747 }
3748 Pdata_clear(self->stack, i);
3749 PDATA_PUSH(self->stack, dict, -1);
3750 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003751}
3752
3753static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003754Instance_New(PyObject *cls, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003755{
Tim Peters1f1b2d22003-02-01 02:16:37 +00003756 PyObject *r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003757
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003758 if (PyClass_Check(cls)) {
3759 int l;
Tim Peters84e87f32001-03-17 04:50:51 +00003760
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003761 if ((l=PyObject_Size(args)) < 0) goto err;
3762 if (!( l )) {
3763 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003764
Tim Peterscba30e22003-02-01 06:24:36 +00003765 __getinitargs__ = PyObject_GetAttr(cls,
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00003766 __getinitargs___str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003767 if (!__getinitargs__) {
Tim Peterscba30e22003-02-01 06:24:36 +00003768 /* We have a class with no __getinitargs__,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003769 so bypass usual construction */
3770 PyObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003771
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003772 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00003773 if (!( inst=PyInstance_NewRaw(cls, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003774 goto err;
3775 return inst;
3776 }
3777 Py_DECREF(__getinitargs__);
3778 }
Tim Peters84e87f32001-03-17 04:50:51 +00003779
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003780 if ((r=PyInstance_New(cls, args, NULL))) return r;
3781 else goto err;
3782 }
Tim Peters84e87f32001-03-17 04:50:51 +00003783
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003784 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00003785
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003786 err:
3787 {
Neal Norwitz0f8b31a2006-06-28 06:28:31 +00003788 PyObject *tp, *v, *tb, *tmp_value;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003789
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003790 PyErr_Fetch(&tp, &v, &tb);
Neal Norwitz0f8b31a2006-06-28 06:28:31 +00003791 tmp_value = v;
3792 /* NULL occurs when there was a KeyboardInterrupt */
3793 if (tmp_value == NULL)
3794 tmp_value = Py_None;
3795 if ((r = PyTuple_Pack(3, tmp_value, cls, args))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003796 Py_XDECREF(v);
3797 v=r;
3798 }
3799 PyErr_Restore(tp,v,tb);
3800 }
3801 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003802}
Tim Peters84e87f32001-03-17 04:50:51 +00003803
Guido van Rossum60456fd1997-04-09 17:36:32 +00003804
3805static int
Tim Peterscba30e22003-02-01 06:24:36 +00003806load_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003807{
3808 PyObject *class, *tup, *obj=0;
3809 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003810
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003811 if ((i = marker(self)) < 0) return -1;
3812 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3813 PDATA_POP(self->stack, class);
3814 if (class) {
3815 obj = Instance_New(class, tup);
3816 Py_DECREF(class);
3817 }
3818 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003819
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003820 if (! obj) return -1;
3821 PDATA_PUSH(self->stack, obj, -1);
3822 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003823}
3824
3825
3826static int
Tim Peterscba30e22003-02-01 06:24:36 +00003827load_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003828{
3829 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
3830 int i, len;
3831 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003832
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003833 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003834
Tim Peters0bc93f52003-02-02 18:29:33 +00003835 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003836 if (len < 2) return bad_readline();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003837 module_name = PyString_FromStringAndSize(s, len - 1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003838 if (!module_name) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003839
Tim Peters0bc93f52003-02-02 18:29:33 +00003840 if ((len = self->readline_func(self, &s)) >= 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003841 if (len < 2) return bad_readline();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003842 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003843 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003844 self->find_class);
3845 Py_DECREF(class_name);
3846 }
3847 }
3848 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003849
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003850 if (! class) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003851
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003852 if ((tup=Pdata_popTuple(self->stack, i))) {
3853 obj = Instance_New(class, tup);
3854 Py_DECREF(tup);
3855 }
3856 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003857
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003858 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003859
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003860 PDATA_PUSH(self->stack, obj, -1);
3861 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003862}
3863
Tim Peterseab7db32003-02-13 18:24:14 +00003864static int
3865load_newobj(Unpicklerobject *self)
3866{
3867 PyObject *args = NULL;
3868 PyObject *clsraw = NULL;
3869 PyTypeObject *cls; /* clsraw cast to its true type */
3870 PyObject *obj;
3871
3872 /* Stack is ... cls argtuple, and we want to call
3873 * cls.__new__(cls, *argtuple).
3874 */
3875 PDATA_POP(self->stack, args);
3876 if (args == NULL) goto Fail;
3877 if (! PyTuple_Check(args)) {
3878 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
3879 "tuple.");
3880 goto Fail;
3881 }
3882
3883 PDATA_POP(self->stack, clsraw);
3884 cls = (PyTypeObject *)clsraw;
3885 if (cls == NULL) goto Fail;
3886 if (! PyType_Check(cls)) {
3887 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3888 "isn't a type object");
3889 goto Fail;
3890 }
3891 if (cls->tp_new == NULL) {
3892 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3893 "has NULL tp_new");
3894 goto Fail;
3895 }
3896
3897 /* Call __new__. */
3898 obj = cls->tp_new(cls, args, NULL);
3899 if (obj == NULL) goto Fail;
3900
3901 Py_DECREF(args);
3902 Py_DECREF(clsraw);
3903 PDATA_PUSH(self->stack, obj, -1);
3904 return 0;
3905
3906 Fail:
3907 Py_XDECREF(args);
3908 Py_XDECREF(clsraw);
3909 return -1;
3910}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003911
3912static int
Tim Peterscba30e22003-02-01 06:24:36 +00003913load_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003914{
3915 PyObject *class = 0, *module_name = 0, *class_name = 0;
3916 int len;
3917 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003918
Tim Peters0bc93f52003-02-02 18:29:33 +00003919 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003920 if (len < 2) return bad_readline();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003921 module_name = PyString_FromStringAndSize(s, len - 1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003922 if (!module_name) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003923
Tim Peters0bc93f52003-02-02 18:29:33 +00003924 if ((len = self->readline_func(self, &s)) >= 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +00003925 if (len < 2) {
3926 Py_DECREF(module_name);
3927 return bad_readline();
3928 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003929 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003930 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003931 self->find_class);
3932 Py_DECREF(class_name);
3933 }
3934 }
3935 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003936
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003937 if (! class) return -1;
3938 PDATA_PUSH(self->stack, class, -1);
3939 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003940}
3941
3942
3943static int
Tim Peterscba30e22003-02-01 06:24:36 +00003944load_persid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003945{
3946 PyObject *pid = 0;
3947 int len;
3948 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003949
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003950 if (self->pers_func) {
Tim Peters0bc93f52003-02-02 18:29:33 +00003951 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003952 if (len < 2) return bad_readline();
3953
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003954 pid = PyString_FromStringAndSize(s, len - 1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003955 if (!pid) return -1;
3956
3957 if (PyList_Check(self->pers_func)) {
3958 if (PyList_Append(self->pers_func, pid) < 0) {
3959 Py_DECREF(pid);
3960 return -1;
3961 }
3962 }
3963 else {
3964 ARG_TUP(self, pid);
3965 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00003966 pid = PyObject_Call(self->pers_func, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003967 NULL);
3968 FREE_ARG_TUP(self);
3969 }
3970 }
3971
3972 if (! pid) return -1;
3973
3974 PDATA_PUSH(self->stack, pid, -1);
3975 return 0;
3976 }
3977 else {
3978 PyErr_SetString(UnpicklingError,
3979 "A load persistent id instruction was encountered,\n"
3980 "but no persistent_load function was specified.");
3981 return -1;
3982 }
3983}
3984
3985static int
Tim Peterscba30e22003-02-01 06:24:36 +00003986load_binpersid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003987{
3988 PyObject *pid = 0;
3989
3990 if (self->pers_func) {
3991 PDATA_POP(self->stack, pid);
3992 if (! pid) return -1;
3993
3994 if (PyList_Check(self->pers_func)) {
3995 if (PyList_Append(self->pers_func, pid) < 0) {
3996 Py_DECREF(pid);
3997 return -1;
3998 }
3999 }
4000 else {
4001 ARG_TUP(self, pid);
4002 if (self->arg) {
4003 pid = PyObject_Call(self->pers_func, self->arg,
4004 NULL);
4005 FREE_ARG_TUP(self);
4006 }
4007 if (! pid) return -1;
4008 }
4009
4010 PDATA_PUSH(self->stack, pid, -1);
4011 return 0;
4012 }
4013 else {
4014 PyErr_SetString(UnpicklingError,
4015 "A load persistent id instruction was encountered,\n"
4016 "but no persistent_load function was specified.");
4017 return -1;
4018 }
4019}
4020
4021
4022static int
Tim Peterscba30e22003-02-01 06:24:36 +00004023load_pop(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004024{
Collin Wintere9a65142009-05-26 05:37:22 +00004025 int len = self->stack->length;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004026
4027 /* Note that we split the (pickle.py) stack into two stacks,
4028 an object stack and a mark stack. We have to be clever and
4029 pop the right one. We do this by looking at the top of the
Collin Wintere9a65142009-05-26 05:37:22 +00004030 mark stack first, and only signalling a stack underflow if
4031 the object stack is empty and the mark stack doesn't match
4032 our expectations.
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004033 */
Collin Wintere9a65142009-05-26 05:37:22 +00004034 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004035 self->num_marks--;
Collin Wintere9a65142009-05-26 05:37:22 +00004036 } else if (len >= 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004037 len--;
4038 Py_DECREF(self->stack->data[len]);
Collin Wintere9a65142009-05-26 05:37:22 +00004039 self->stack->length = len;
4040 } else {
4041 return stackUnderflow();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004042 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004043 return 0;
4044}
4045
4046
4047static int
Tim Peterscba30e22003-02-01 06:24:36 +00004048load_pop_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004049{
4050 int i;
4051
4052 if ((i = marker(self)) < 0)
4053 return -1;
4054
4055 Pdata_clear(self->stack, i);
4056
4057 return 0;
4058}
4059
4060
4061static int
Tim Peterscba30e22003-02-01 06:24:36 +00004062load_dup(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004063{
4064 PyObject *last;
4065 int len;
4066
4067 if ((len = self->stack->length) <= 0) return stackUnderflow();
4068 last=self->stack->data[len-1];
4069 Py_INCREF(last);
4070 PDATA_PUSH(self->stack, last, -1);
4071 return 0;
4072}
4073
4074
4075static int
Tim Peterscba30e22003-02-01 06:24:36 +00004076load_get(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004077{
4078 PyObject *py_str = 0, *value = 0;
4079 int len;
4080 char *s;
4081 int rc;
4082
Tim Peters0bc93f52003-02-02 18:29:33 +00004083 if ((len = self->readline_func(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00004084 if (len < 2) return bad_readline();
Tim Peters84e87f32001-03-17 04:50:51 +00004085
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004086 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004087
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004088 value = PyDict_GetItem(self->memo, py_str);
4089 if (! value) {
4090 PyErr_SetObject(BadPickleGet, py_str);
4091 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00004092 }
4093 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004094 PDATA_APPEND(self->stack, value, -1);
4095 rc = 0;
4096 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004097
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004098 Py_DECREF(py_str);
4099 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004100}
4101
4102
4103static int
Tim Peterscba30e22003-02-01 06:24:36 +00004104load_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004105{
4106 PyObject *py_key = 0, *value = 0;
4107 unsigned char key;
4108 char *s;
4109 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004110
Tim Peters0bc93f52003-02-02 18:29:33 +00004111 if (self->read_func(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004112
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004113 key = (unsigned char)s[0];
4114 if (!( py_key = PyInt_FromLong((long)key))) return -1;
Guido van Rossumea2b7152000-05-09 18:14:50 +00004115
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004116 value = PyDict_GetItem(self->memo, py_key);
4117 if (! value) {
4118 PyErr_SetObject(BadPickleGet, py_key);
4119 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00004120 }
4121 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004122 PDATA_APPEND(self->stack, value, -1);
4123 rc = 0;
4124 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004125
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004126 Py_DECREF(py_key);
4127 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004128}
4129
4130
4131static int
Tim Peterscba30e22003-02-01 06:24:36 +00004132load_long_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004133{
4134 PyObject *py_key = 0, *value = 0;
4135 unsigned char c;
4136 char *s;
4137 long key;
4138 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004139
Tim Peters0bc93f52003-02-02 18:29:33 +00004140 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004141
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004142 c = (unsigned char)s[0];
4143 key = (long)c;
4144 c = (unsigned char)s[1];
4145 key |= (long)c << 8;
4146 c = (unsigned char)s[2];
4147 key |= (long)c << 16;
4148 c = (unsigned char)s[3];
4149 key |= (long)c << 24;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004150
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004151 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4152
4153 value = PyDict_GetItem(self->memo, py_key);
4154 if (! value) {
4155 PyErr_SetObject(BadPickleGet, py_key);
4156 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00004157 }
4158 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004159 PDATA_APPEND(self->stack, value, -1);
4160 rc = 0;
4161 }
4162
4163 Py_DECREF(py_key);
4164 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004165}
4166
Tim Peters2d629652003-02-04 05:06:17 +00004167/* Push an object from the extension registry (EXT[124]). nbytes is
4168 * the number of bytes following the opcode, holding the index (code) value.
4169 */
4170static int
4171load_extension(Unpicklerobject *self, int nbytes)
4172{
4173 char *codebytes; /* the nbytes bytes after the opcode */
4174 long code; /* calc_binint returns long */
4175 PyObject *py_code; /* code as a Python int */
4176 PyObject *obj; /* the object to push */
4177 PyObject *pair; /* (module_name, class_name) */
4178 PyObject *module_name, *class_name;
4179
4180 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4181 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4182 code = calc_binint(codebytes, nbytes);
4183 if (code <= 0) { /* note that 0 is forbidden */
4184 /* Corrupt or hostile pickle. */
4185 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
4186 return -1;
4187 }
4188
4189 /* Look for the code in the cache. */
4190 py_code = PyInt_FromLong(code);
4191 if (py_code == NULL) return -1;
4192 obj = PyDict_GetItem(extension_cache, py_code);
4193 if (obj != NULL) {
4194 /* Bingo. */
4195 Py_DECREF(py_code);
4196 PDATA_APPEND(self->stack, obj, -1);
4197 return 0;
4198 }
4199
4200 /* Look up the (module_name, class_name) pair. */
4201 pair = PyDict_GetItem(inverted_registry, py_code);
4202 if (pair == NULL) {
4203 Py_DECREF(py_code);
4204 PyErr_Format(PyExc_ValueError, "unregistered extension "
4205 "code %ld", code);
4206 return -1;
4207 }
4208 /* Since the extension registry is manipulable via Python code,
Tim Petersfa05ce32003-02-04 05:20:32 +00004209 * confirm that pair is really a 2-tuple of strings.
Tim Peters2d629652003-02-04 05:06:17 +00004210 */
4211 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004212 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
4213 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
Tim Peters2d629652003-02-04 05:06:17 +00004214 Py_DECREF(py_code);
4215 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
4216 "isn't a 2-tuple of strings", code);
4217 return -1;
4218 }
4219 /* Load the object. */
4220 obj = find_class(module_name, class_name, self->find_class);
4221 if (obj == NULL) {
4222 Py_DECREF(py_code);
4223 return -1;
4224 }
4225 /* Cache code -> obj. */
4226 code = PyDict_SetItem(extension_cache, py_code, obj);
4227 Py_DECREF(py_code);
4228 if (code < 0) {
4229 Py_DECREF(obj);
4230 return -1;
4231 }
4232 PDATA_PUSH(self->stack, obj, -1);
4233 return 0;
4234}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004235
4236static int
Tim Peterscba30e22003-02-01 06:24:36 +00004237load_put(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004238{
4239 PyObject *py_str = 0, *value = 0;
4240 int len, l;
4241 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004242
Tim Peters0bc93f52003-02-02 18:29:33 +00004243 if ((l = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004244 if (l < 2) return bad_readline();
4245 if (!( len=self->stack->length )) return stackUnderflow();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004246 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004247 value=self->stack->data[len-1];
4248 l=PyDict_SetItem(self->memo, py_str, value);
4249 Py_DECREF(py_str);
4250 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004251}
4252
4253
4254static int
Tim Peterscba30e22003-02-01 06:24:36 +00004255load_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004256{
4257 PyObject *py_key = 0, *value = 0;
4258 unsigned char key;
4259 char *s;
4260 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004261
Tim Peters0bc93f52003-02-02 18:29:33 +00004262 if (self->read_func(self, &s, 1) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004263 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004264
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004265 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00004266
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004267 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4268 value=self->stack->data[len-1];
4269 len=PyDict_SetItem(self->memo, py_key, value);
4270 Py_DECREF(py_key);
4271 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004272}
4273
4274
4275static int
Tim Peterscba30e22003-02-01 06:24:36 +00004276load_long_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004277{
4278 PyObject *py_key = 0, *value = 0;
4279 long key;
4280 unsigned char c;
4281 char *s;
4282 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004283
Tim Peters0bc93f52003-02-02 18:29:33 +00004284 if (self->read_func(self, &s, 4) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004285 if (!( len=self->stack->length )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004286
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004287 c = (unsigned char)s[0];
4288 key = (long)c;
4289 c = (unsigned char)s[1];
4290 key |= (long)c << 8;
4291 c = (unsigned char)s[2];
4292 key |= (long)c << 16;
4293 c = (unsigned char)s[3];
4294 key |= (long)c << 24;
Tim Peters84e87f32001-03-17 04:50:51 +00004295
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004296 if (!( py_key = PyInt_FromLong(key))) return -1;
4297 value=self->stack->data[len-1];
4298 len=PyDict_SetItem(self->memo, py_key, value);
4299 Py_DECREF(py_key);
4300 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004301}
4302
4303
4304static int
Tim Peterscba30e22003-02-01 06:24:36 +00004305do_append(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004306{
4307 PyObject *value = 0, *list = 0, *append_method = 0;
4308 int len, i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004309
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004310 len=self->stack->length;
4311 if (!( len >= x && x > 0 )) return stackUnderflow();
4312 /* nothing to do */
Tim Peterscba30e22003-02-01 06:24:36 +00004313 if (len==x) return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004314
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004315 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004316
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004317 if (PyList_Check(list)) {
4318 PyObject *slice;
4319 int list_len;
Tim Peters84e87f32001-03-17 04:50:51 +00004320
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004321 slice=Pdata_popList(self->stack, x);
Neal Norwitzb59d08c2006-07-22 16:20:49 +00004322 if (! slice) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004323 list_len = PyList_GET_SIZE(list);
4324 i=PyList_SetSlice(list, list_len, list_len, slice);
4325 Py_DECREF(slice);
4326 return i;
4327 }
4328 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004329
Tim Peterscba30e22003-02-01 06:24:36 +00004330 if (!( append_method = PyObject_GetAttr(list, append_str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004331 return -1;
4332
4333 for (i = x; i < len; i++) {
4334 PyObject *junk;
4335
4336 value=self->stack->data[i];
4337 junk=0;
4338 ARG_TUP(self, value);
4339 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00004340 junk = PyObject_Call(append_method, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004341 NULL);
4342 FREE_ARG_TUP(self);
4343 }
4344 if (! junk) {
4345 Pdata_clear(self->stack, i+1);
4346 self->stack->length=x;
4347 Py_DECREF(append_method);
4348 return -1;
4349 }
4350 Py_DECREF(junk);
4351 }
4352 self->stack->length=x;
4353 Py_DECREF(append_method);
4354 }
4355
4356 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004357}
4358
4359
4360static int
Tim Peterscba30e22003-02-01 06:24:36 +00004361load_append(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004362{
4363 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004364}
4365
4366
4367static int
Tim Peterscba30e22003-02-01 06:24:36 +00004368load_appends(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004369{
4370 return do_append(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004371}
4372
4373
4374static int
Tim Peterscba30e22003-02-01 06:24:36 +00004375do_setitems(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004376{
4377 PyObject *value = 0, *key = 0, *dict = 0;
4378 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004379
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004380 if (!( (len=self->stack->length) >= x
4381 && x > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004382
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004383 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004384
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004385 for (i = x+1; i < len; i += 2) {
4386 key =self->stack->data[i-1];
4387 value=self->stack->data[i ];
4388 if (PyObject_SetItem(dict, key, value) < 0) {
4389 r=-1;
4390 break;
4391 }
4392 }
4393
4394 Pdata_clear(self->stack, x);
4395
4396 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004397}
4398
4399
Tim Peters84e87f32001-03-17 04:50:51 +00004400static int
Tim Peterscba30e22003-02-01 06:24:36 +00004401load_setitem(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004402{
4403 return do_setitems(self, self->stack->length - 2);
4404}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004405
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004406static int
Tim Peterscba30e22003-02-01 06:24:36 +00004407load_setitems(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004408{
4409 return do_setitems(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004410}
4411
Tim Peters84e87f32001-03-17 04:50:51 +00004412
Guido van Rossum60456fd1997-04-09 17:36:32 +00004413static int
Tim Peterscba30e22003-02-01 06:24:36 +00004414load_build(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004415{
Tim Peters080c88b2003-02-15 03:01:11 +00004416 PyObject *state, *inst, *slotstate;
4417 PyObject *__setstate__;
4418 PyObject *d_key, *d_value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004419 Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00004420 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004421
Tim Peters080c88b2003-02-15 03:01:11 +00004422 /* Stack is ... instance, state. We want to leave instance at
4423 * the stack top, possibly mutated via instance.__setstate__(state).
4424 */
4425 if (self->stack->length < 2)
4426 return stackUnderflow();
4427 PDATA_POP(self->stack, state);
4428 if (state == NULL)
4429 return -1;
4430 inst = self->stack->data[self->stack->length - 1];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004431
Tim Peters080c88b2003-02-15 03:01:11 +00004432 __setstate__ = PyObject_GetAttr(inst, __setstate___str);
4433 if (__setstate__ != NULL) {
4434 PyObject *junk = NULL;
4435
4436 /* The explicit __setstate__ is responsible for everything. */
4437 ARG_TUP(self, state);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004438 if (self->arg) {
4439 junk = PyObject_Call(__setstate__, self->arg, NULL);
4440 FREE_ARG_TUP(self);
4441 }
4442 Py_DECREF(__setstate__);
Tim Peters080c88b2003-02-15 03:01:11 +00004443 if (junk == NULL)
4444 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004445 Py_DECREF(junk);
4446 return 0;
4447 }
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00004448 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4449 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004450 PyErr_Clear();
Tim Peters080c88b2003-02-15 03:01:11 +00004451
4452 /* A default __setstate__. First see whether state embeds a
4453 * slot state dict too (a proto 2 addition).
4454 */
4455 if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
4456 PyObject *temp = state;
4457 state = PyTuple_GET_ITEM(temp, 0);
4458 slotstate = PyTuple_GET_ITEM(temp, 1);
4459 Py_INCREF(state);
4460 Py_INCREF(slotstate);
4461 Py_DECREF(temp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004462 }
Tim Peters080c88b2003-02-15 03:01:11 +00004463 else
4464 slotstate = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004465
Tim Peters080c88b2003-02-15 03:01:11 +00004466 /* Set inst.__dict__ from the state dict (if any). */
4467 if (state != Py_None) {
4468 PyObject *dict;
4469 if (! PyDict_Check(state)) {
4470 PyErr_SetString(UnpicklingError, "state is not a "
4471 "dictionary");
4472 goto finally;
4473 }
4474 dict = PyObject_GetAttr(inst, __dict___str);
4475 if (dict == NULL)
4476 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004477
Tim Peters080c88b2003-02-15 03:01:11 +00004478 i = 0;
4479 while (PyDict_Next(state, &i, &d_key, &d_value)) {
4480 if (PyObject_SetItem(dict, d_key, d_value) < 0)
4481 goto finally;
4482 }
4483 Py_DECREF(dict);
4484 }
4485
4486 /* Also set instance attributes from the slotstate dict (if any). */
4487 if (slotstate != NULL) {
4488 if (! PyDict_Check(slotstate)) {
4489 PyErr_SetString(UnpicklingError, "slot state is not "
4490 "a dictionary");
4491 goto finally;
4492 }
4493 i = 0;
4494 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
4495 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
4496 goto finally;
4497 }
4498 }
4499 res = 0;
4500
4501 finally:
4502 Py_DECREF(state);
4503 Py_XDECREF(slotstate);
4504 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004505}
4506
4507
4508static int
Tim Peterscba30e22003-02-01 06:24:36 +00004509load_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004510{
4511 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004512
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004513 /* Note that we split the (pickle.py) stack into two stacks, an
4514 object stack and a mark stack. Here we push a mark onto the
4515 mark stack.
4516 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00004517
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004518 if ((self->num_marks + 1) >= self->marks_size) {
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004519 int *marks;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004520 s=self->marks_size+20;
4521 if (s <= self->num_marks) s=self->num_marks + 1;
4522 if (self->marks == NULL)
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004523 marks=(int *)malloc(s * sizeof(int));
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004524 else
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004525 marks=(int *)realloc(self->marks,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004526 s * sizeof(int));
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004527 if (!marks) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004528 PyErr_NoMemory();
4529 return -1;
4530 }
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004531 self->marks = marks;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004532 self->marks_size = s;
4533 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004534
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004535 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004536
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004537 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004538}
4539
Guido van Rossum60456fd1997-04-09 17:36:32 +00004540static int
Tim Peterscba30e22003-02-01 06:24:36 +00004541load_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004542{
4543 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004544
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004545 PDATA_POP(self->stack, arg_tup);
4546 if (! arg_tup) return -1;
4547 PDATA_POP(self->stack, callable);
4548 if (callable) {
4549 ob = Instance_New(callable, arg_tup);
4550 Py_DECREF(callable);
4551 }
4552 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004553
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004554 if (! ob) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004555
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004556 PDATA_PUSH(self->stack, ob, -1);
4557 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004558}
Tim Peters84e87f32001-03-17 04:50:51 +00004559
Tim Peters4190fb82003-02-02 16:09:05 +00004560/* Just raises an error if we don't know the protocol specified. PROTO
4561 * is the first opcode for protocols >= 2.
4562 */
4563static int
4564load_proto(Unpicklerobject *self)
4565{
4566 int i;
4567 char *protobyte;
4568
4569 i = self->read_func(self, &protobyte, 1);
4570 if (i < 0)
4571 return -1;
4572
4573 i = calc_binint(protobyte, 1);
4574 /* No point checking for < 0, since calc_binint returns an unsigned
4575 * int when chewing on 1 byte.
4576 */
4577 assert(i >= 0);
Tim Peters8587b3c2003-02-13 15:44:41 +00004578 if (i <= HIGHEST_PROTOCOL)
Tim Peters4190fb82003-02-02 16:09:05 +00004579 return 0;
4580
4581 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4582 return -1;
4583}
4584
Guido van Rossum60456fd1997-04-09 17:36:32 +00004585static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004586load(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004587{
4588 PyObject *err = 0, *val = 0;
4589 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004590
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004591 self->num_marks = 0;
4592 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004593
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004594 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004595 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004596 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004597
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004598 switch (s[0]) {
4599 case NONE:
4600 if (load_none(self) < 0)
4601 break;
4602 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004603
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004604 case BININT:
4605 if (load_binint(self) < 0)
4606 break;
4607 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004608
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004609 case BININT1:
4610 if (load_binint1(self) < 0)
4611 break;
4612 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004613
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004614 case BININT2:
4615 if (load_binint2(self) < 0)
4616 break;
4617 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004618
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004619 case INT:
4620 if (load_int(self) < 0)
4621 break;
4622 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004623
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004624 case LONG:
4625 if (load_long(self) < 0)
4626 break;
4627 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004628
Tim Petersee1a53c2003-02-02 02:57:53 +00004629 case LONG1:
4630 if (load_counted_long(self, 1) < 0)
4631 break;
4632 continue;
4633
4634 case LONG4:
4635 if (load_counted_long(self, 4) < 0)
4636 break;
4637 continue;
4638
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004639 case FLOAT:
4640 if (load_float(self) < 0)
4641 break;
4642 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004643
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004644 case BINFLOAT:
4645 if (load_binfloat(self) < 0)
4646 break;
4647 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004648
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004649 case BINSTRING:
4650 if (load_binstring(self) < 0)
4651 break;
4652 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004653
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004654 case SHORT_BINSTRING:
4655 if (load_short_binstring(self) < 0)
4656 break;
4657 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004658
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004659 case STRING:
4660 if (load_string(self) < 0)
4661 break;
4662 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004663
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004664#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004665 case UNICODE:
4666 if (load_unicode(self) < 0)
4667 break;
4668 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004669
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004670 case BINUNICODE:
4671 if (load_binunicode(self) < 0)
4672 break;
4673 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004674#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004675
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004676 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004677 if (load_counted_tuple(self, 0) < 0)
4678 break;
4679 continue;
4680
4681 case TUPLE1:
4682 if (load_counted_tuple(self, 1) < 0)
4683 break;
4684 continue;
4685
4686 case TUPLE2:
4687 if (load_counted_tuple(self, 2) < 0)
4688 break;
4689 continue;
4690
4691 case TUPLE3:
4692 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004693 break;
4694 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004695
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004696 case TUPLE:
4697 if (load_tuple(self) < 0)
4698 break;
4699 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004700
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004701 case EMPTY_LIST:
4702 if (load_empty_list(self) < 0)
4703 break;
4704 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004705
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004706 case LIST:
4707 if (load_list(self) < 0)
4708 break;
4709 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004710
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004711 case EMPTY_DICT:
4712 if (load_empty_dict(self) < 0)
4713 break;
4714 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004715
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004716 case DICT:
4717 if (load_dict(self) < 0)
4718 break;
4719 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004720
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004721 case OBJ:
4722 if (load_obj(self) < 0)
4723 break;
4724 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004725
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004726 case INST:
4727 if (load_inst(self) < 0)
4728 break;
4729 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004730
Tim Peterseab7db32003-02-13 18:24:14 +00004731 case NEWOBJ:
4732 if (load_newobj(self) < 0)
4733 break;
4734 continue;
4735
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004736 case GLOBAL:
4737 if (load_global(self) < 0)
4738 break;
4739 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004740
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004741 case APPEND:
4742 if (load_append(self) < 0)
4743 break;
4744 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004745
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004746 case APPENDS:
4747 if (load_appends(self) < 0)
4748 break;
4749 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004750
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004751 case BUILD:
4752 if (load_build(self) < 0)
4753 break;
4754 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004755
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004756 case DUP:
4757 if (load_dup(self) < 0)
4758 break;
4759 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004760
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004761 case BINGET:
4762 if (load_binget(self) < 0)
4763 break;
4764 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004765
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004766 case LONG_BINGET:
4767 if (load_long_binget(self) < 0)
4768 break;
4769 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004770
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004771 case GET:
4772 if (load_get(self) < 0)
4773 break;
4774 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004775
Tim Peters2d629652003-02-04 05:06:17 +00004776 case EXT1:
4777 if (load_extension(self, 1) < 0)
4778 break;
4779 continue;
4780
4781 case EXT2:
4782 if (load_extension(self, 2) < 0)
4783 break;
4784 continue;
4785
4786 case EXT4:
4787 if (load_extension(self, 4) < 0)
4788 break;
4789 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004790 case MARK:
4791 if (load_mark(self) < 0)
4792 break;
4793 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004794
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004795 case BINPUT:
4796 if (load_binput(self) < 0)
4797 break;
4798 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004799
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004800 case LONG_BINPUT:
4801 if (load_long_binput(self) < 0)
4802 break;
4803 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004804
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004805 case PUT:
4806 if (load_put(self) < 0)
4807 break;
4808 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004809
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004810 case POP:
4811 if (load_pop(self) < 0)
4812 break;
4813 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004814
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004815 case POP_MARK:
4816 if (load_pop_mark(self) < 0)
4817 break;
4818 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004819
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004820 case SETITEM:
4821 if (load_setitem(self) < 0)
4822 break;
4823 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004824
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004825 case SETITEMS:
4826 if (load_setitems(self) < 0)
4827 break;
4828 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004829
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004830 case STOP:
4831 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004832
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004833 case PERSID:
4834 if (load_persid(self) < 0)
4835 break;
4836 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004837
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004838 case BINPERSID:
4839 if (load_binpersid(self) < 0)
4840 break;
4841 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004842
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004843 case REDUCE:
4844 if (load_reduce(self) < 0)
4845 break;
4846 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004847
Tim Peters4190fb82003-02-02 16:09:05 +00004848 case PROTO:
4849 if (load_proto(self) < 0)
4850 break;
4851 continue;
4852
Tim Peters3c67d792003-02-02 17:59:11 +00004853 case NEWTRUE:
4854 if (load_bool(self, Py_True) < 0)
4855 break;
4856 continue;
4857
4858 case NEWFALSE:
4859 if (load_bool(self, Py_False) < 0)
4860 break;
4861 continue;
4862
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004863 case '\0':
4864 /* end of file */
4865 PyErr_SetNone(PyExc_EOFError);
4866 break;
Tim Peterscba30e22003-02-01 06:24:36 +00004867
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004868 default:
Tim Peterscba30e22003-02-01 06:24:36 +00004869 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004870 "invalid load key, '%s'.",
4871 "c", s[0]);
4872 return NULL;
4873 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004874
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004875 break;
4876 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004877
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004878 if ((err = PyErr_Occurred())) {
4879 if (err == PyExc_EOFError) {
4880 PyErr_SetNone(PyExc_EOFError);
4881 }
4882 return NULL;
4883 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004884
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004885 PDATA_POP(self->stack, val);
4886 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004887}
Tim Peters84e87f32001-03-17 04:50:51 +00004888
Guido van Rossum60456fd1997-04-09 17:36:32 +00004889
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004890/* No-load functions to support noload, which is used to
4891 find persistent references. */
4892
4893static int
Tim Peterscba30e22003-02-01 06:24:36 +00004894noload_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004895{
4896 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004897
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004898 if ((i = marker(self)) < 0) return -1;
4899 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004900}
4901
4902
4903static int
Tim Peterscba30e22003-02-01 06:24:36 +00004904noload_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004905{
4906 int i;
4907 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004908
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004909 if ((i = marker(self)) < 0) return -1;
4910 Pdata_clear(self->stack, i);
Tim Peters0bc93f52003-02-02 18:29:33 +00004911 if (self->readline_func(self, &s) < 0) return -1;
4912 if (self->readline_func(self, &s) < 0) return -1;
Tim Peterseab7db32003-02-13 18:24:14 +00004913 PDATA_APPEND(self->stack, Py_None, -1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004914 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004915}
4916
4917static int
Tim Peterseab7db32003-02-13 18:24:14 +00004918noload_newobj(Unpicklerobject *self)
4919{
4920 PyObject *obj;
4921
4922 PDATA_POP(self->stack, obj); /* pop argtuple */
4923 if (obj == NULL) return -1;
4924 Py_DECREF(obj);
4925
4926 PDATA_POP(self->stack, obj); /* pop cls */
4927 if (obj == NULL) return -1;
4928 Py_DECREF(obj);
4929
4930 PDATA_APPEND(self->stack, Py_None, -1);
4931 return 0;
4932}
4933
4934static int
Tim Peterscba30e22003-02-01 06:24:36 +00004935noload_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004936{
4937 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004938
Tim Peters0bc93f52003-02-02 18:29:33 +00004939 if (self->readline_func(self, &s) < 0) return -1;
4940 if (self->readline_func(self, &s) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004941 PDATA_APPEND(self->stack, Py_None,-1);
4942 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004943}
4944
4945static int
Tim Peterscba30e22003-02-01 06:24:36 +00004946noload_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004947{
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004948
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004949 if (self->stack->length < 2) return stackUnderflow();
4950 Pdata_clear(self->stack, self->stack->length-2);
4951 PDATA_APPEND(self->stack, Py_None,-1);
4952 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004953}
4954
4955static int
4956noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004957
Guido van Rossum053b8df1998-11-25 16:18:00 +00004958 if (self->stack->length < 1) return stackUnderflow();
4959 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00004960 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004961}
4962
Tim Peters2d629652003-02-04 05:06:17 +00004963static int
4964noload_extension(Unpicklerobject *self, int nbytes)
4965{
4966 char *codebytes;
4967
4968 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4969 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4970 PDATA_APPEND(self->stack, Py_None, -1);
4971 return 0;
4972}
4973
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004974
4975static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004976noload(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004977{
4978 PyObject *err = 0, *val = 0;
4979 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004980
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004981 self->num_marks = 0;
4982 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004983
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004984 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004985 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004986 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004987
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004988 switch (s[0]) {
4989 case NONE:
4990 if (load_none(self) < 0)
4991 break;
4992 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004993
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004994 case BININT:
4995 if (load_binint(self) < 0)
4996 break;
4997 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004998
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004999 case BININT1:
5000 if (load_binint1(self) < 0)
5001 break;
5002 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005003
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005004 case BININT2:
5005 if (load_binint2(self) < 0)
5006 break;
5007 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005008
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005009 case INT:
5010 if (load_int(self) < 0)
5011 break;
5012 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005013
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005014 case LONG:
5015 if (load_long(self) < 0)
5016 break;
5017 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005018
Tim Peters4190fb82003-02-02 16:09:05 +00005019 case LONG1:
5020 if (load_counted_long(self, 1) < 0)
5021 break;
5022 continue;
5023
5024 case LONG4:
5025 if (load_counted_long(self, 4) < 0)
5026 break;
5027 continue;
5028
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005029 case FLOAT:
5030 if (load_float(self) < 0)
5031 break;
5032 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005033
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005034 case BINFLOAT:
5035 if (load_binfloat(self) < 0)
5036 break;
5037 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005038
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005039 case BINSTRING:
5040 if (load_binstring(self) < 0)
5041 break;
5042 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005043
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005044 case SHORT_BINSTRING:
5045 if (load_short_binstring(self) < 0)
5046 break;
5047 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005048
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005049 case STRING:
5050 if (load_string(self) < 0)
5051 break;
5052 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005053
Martin v. Löwis339d0f72001-08-17 18:39:25 +00005054#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005055 case UNICODE:
5056 if (load_unicode(self) < 0)
5057 break;
5058 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00005059
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005060 case BINUNICODE:
5061 if (load_binunicode(self) < 0)
5062 break;
5063 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00005064#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00005065
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005066 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00005067 if (load_counted_tuple(self, 0) < 0)
5068 break;
5069 continue;
5070
5071 case TUPLE1:
5072 if (load_counted_tuple(self, 1) < 0)
5073 break;
5074 continue;
5075
5076 case TUPLE2:
5077 if (load_counted_tuple(self, 2) < 0)
5078 break;
5079 continue;
5080
5081 case TUPLE3:
5082 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005083 break;
5084 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005085
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005086 case TUPLE:
5087 if (load_tuple(self) < 0)
5088 break;
5089 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005090
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005091 case EMPTY_LIST:
5092 if (load_empty_list(self) < 0)
5093 break;
5094 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005095
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005096 case LIST:
5097 if (load_list(self) < 0)
5098 break;
5099 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005100
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005101 case EMPTY_DICT:
5102 if (load_empty_dict(self) < 0)
5103 break;
5104 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005105
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005106 case DICT:
5107 if (load_dict(self) < 0)
5108 break;
5109 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005110
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005111 case OBJ:
5112 if (noload_obj(self) < 0)
5113 break;
5114 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005115
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005116 case INST:
5117 if (noload_inst(self) < 0)
5118 break;
5119 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005120
Tim Peterseab7db32003-02-13 18:24:14 +00005121 case NEWOBJ:
5122 if (noload_newobj(self) < 0)
5123 break;
5124 continue;
5125
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005126 case GLOBAL:
5127 if (noload_global(self) < 0)
5128 break;
5129 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005130
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005131 case APPEND:
5132 if (load_append(self) < 0)
5133 break;
5134 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005135
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005136 case APPENDS:
5137 if (load_appends(self) < 0)
5138 break;
5139 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005140
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005141 case BUILD:
5142 if (noload_build(self) < 0)
5143 break;
5144 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005145
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005146 case DUP:
5147 if (load_dup(self) < 0)
5148 break;
5149 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005150
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005151 case BINGET:
5152 if (load_binget(self) < 0)
5153 break;
5154 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005155
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005156 case LONG_BINGET:
5157 if (load_long_binget(self) < 0)
5158 break;
5159 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005160
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005161 case GET:
5162 if (load_get(self) < 0)
5163 break;
5164 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005165
Tim Peters2d629652003-02-04 05:06:17 +00005166 case EXT1:
5167 if (noload_extension(self, 1) < 0)
5168 break;
5169 continue;
5170
5171 case EXT2:
5172 if (noload_extension(self, 2) < 0)
5173 break;
5174 continue;
5175
5176 case EXT4:
5177 if (noload_extension(self, 4) < 0)
5178 break;
5179 continue;
5180
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005181 case MARK:
5182 if (load_mark(self) < 0)
5183 break;
5184 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005185
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005186 case BINPUT:
5187 if (load_binput(self) < 0)
5188 break;
5189 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005190
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005191 case LONG_BINPUT:
5192 if (load_long_binput(self) < 0)
5193 break;
5194 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005195
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005196 case PUT:
5197 if (load_put(self) < 0)
5198 break;
5199 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005200
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005201 case POP:
5202 if (load_pop(self) < 0)
5203 break;
5204 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005205
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005206 case POP_MARK:
5207 if (load_pop_mark(self) < 0)
5208 break;
5209 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005210
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005211 case SETITEM:
5212 if (load_setitem(self) < 0)
5213 break;
5214 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005215
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005216 case SETITEMS:
5217 if (load_setitems(self) < 0)
5218 break;
5219 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005220
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005221 case STOP:
5222 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005223
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005224 case PERSID:
5225 if (load_persid(self) < 0)
5226 break;
5227 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005228
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005229 case BINPERSID:
5230 if (load_binpersid(self) < 0)
5231 break;
5232 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005233
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005234 case REDUCE:
5235 if (noload_reduce(self) < 0)
5236 break;
5237 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005238
Tim Peters4190fb82003-02-02 16:09:05 +00005239 case PROTO:
5240 if (load_proto(self) < 0)
5241 break;
5242 continue;
5243
Tim Peters3c67d792003-02-02 17:59:11 +00005244 case NEWTRUE:
5245 if (load_bool(self, Py_True) < 0)
5246 break;
5247 continue;
5248
5249 case NEWFALSE:
5250 if (load_bool(self, Py_False) < 0)
5251 break;
5252 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005253 default:
Tim Peterscba30e22003-02-01 06:24:36 +00005254 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005255 "invalid load key, '%s'.",
5256 "c", s[0]);
5257 return NULL;
5258 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005259
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005260 break;
5261 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005262
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005263 if ((err = PyErr_Occurred())) {
5264 if (err == PyExc_EOFError) {
5265 PyErr_SetNone(PyExc_EOFError);
5266 }
5267 return NULL;
5268 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005269
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005270 PDATA_POP(self->stack, val);
5271 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005272}
Tim Peters84e87f32001-03-17 04:50:51 +00005273
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005274
Guido van Rossum60456fd1997-04-09 17:36:32 +00005275static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005276Unpickler_load(Unpicklerobject *self, PyObject *unused)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005277{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005278 return load(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005279}
5280
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005281static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005282Unpickler_noload(Unpicklerobject *self, PyObject *unused)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005283{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005284 return noload(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005285}
5286
Guido van Rossum60456fd1997-04-09 17:36:32 +00005287
5288static struct PyMethodDef Unpickler_methods[] = {
Georg Brandl96a8c392006-05-29 21:04:52 +00005289 {"load", (PyCFunction)Unpickler_load, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005290 PyDoc_STR("load() -- Load a pickle")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005291 },
Georg Brandl96a8c392006-05-29 21:04:52 +00005292 {"noload", (PyCFunction)Unpickler_noload, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005293 PyDoc_STR(
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005294 "noload() -- not load a pickle, but go through most of the motions\n"
5295 "\n"
5296 "This function can be used to read past a pickle without instantiating\n"
5297 "any objects or importing any modules. It can also be used to find all\n"
5298 "persistent references without instantiating any objects or importing\n"
Neal Norwitz200788c2002-08-13 22:20:41 +00005299 "any modules.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005300 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00005301 {NULL, NULL} /* sentinel */
5302};
5303
5304
5305static Unpicklerobject *
Tim Peterscba30e22003-02-01 06:24:36 +00005306newUnpicklerobject(PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005307{
5308 Unpicklerobject *self;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005309
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005310 if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005311 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005312
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005313 self->file = NULL;
5314 self->arg = NULL;
5315 self->stack = (Pdata*)Pdata_New();
5316 self->pers_func = NULL;
5317 self->last_string = NULL;
5318 self->marks = NULL;
5319 self->num_marks = 0;
5320 self->marks_size = 0;
5321 self->buf_size = 0;
5322 self->read = NULL;
5323 self->readline = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005324 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005325
Tim Peterscba30e22003-02-01 06:24:36 +00005326 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005327 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005328
Neal Norwitzb59d08c2006-07-22 16:20:49 +00005329 if (!self->stack)
5330 goto err;
5331
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005332 Py_INCREF(f);
5333 self->file = f;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005334
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005335 /* Set read, readline based on type of f */
5336 if (PyFile_Check(f)) {
5337 self->fp = PyFile_AsFile(f);
5338 if (self->fp == NULL) {
Tim Peterscba30e22003-02-01 06:24:36 +00005339 PyErr_SetString(PyExc_ValueError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005340 "I/O operation on closed file");
5341 goto err;
5342 }
5343 self->read_func = read_file;
5344 self->readline_func = readline_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00005345 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005346 else if (PycStringIO_InputCheck(f)) {
5347 self->fp = NULL;
5348 self->read_func = read_cStringIO;
5349 self->readline_func = readline_cStringIO;
5350 }
5351 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00005352
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005353 self->fp = NULL;
5354 self->read_func = read_other;
5355 self->readline_func = readline_other;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005356
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005357 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5358 (self->read = PyObject_GetAttr(f, read_str)))) {
5359 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00005360 PyErr_SetString( PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005361 "argument must have 'read' and "
5362 "'readline' attributes" );
5363 goto err;
5364 }
5365 }
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005366 PyObject_GC_Track(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005367
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005368 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005369
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005370 err:
5371 Py_DECREF((PyObject *)self);
5372 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005373}
5374
5375
5376static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005377get_Unpickler(PyObject *self, PyObject *file)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005378{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005379 return (PyObject *)newUnpicklerobject(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005380}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005381
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005382
Guido van Rossum60456fd1997-04-09 17:36:32 +00005383static void
Tim Peterscba30e22003-02-01 06:24:36 +00005384Unpickler_dealloc(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005385{
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005386 PyObject_GC_UnTrack((PyObject *)self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005387 Py_XDECREF(self->readline);
5388 Py_XDECREF(self->read);
5389 Py_XDECREF(self->file);
5390 Py_XDECREF(self->memo);
5391 Py_XDECREF(self->stack);
5392 Py_XDECREF(self->pers_func);
5393 Py_XDECREF(self->arg);
5394 Py_XDECREF(self->last_string);
Jeremy Hyltonfff093f2003-07-11 19:42:49 +00005395 Py_XDECREF(self->find_class);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005396
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005397 if (self->marks) {
5398 free(self->marks);
5399 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005400
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005401 if (self->buf_size) {
5402 free(self->buf);
5403 }
Tim Peters84e87f32001-03-17 04:50:51 +00005404
Christian Heimese93237d2007-12-19 02:37:44 +00005405 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005406}
5407
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005408static int
5409Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg)
5410{
Thomas Woutersc6e55062006-04-15 21:47:09 +00005411 Py_VISIT(self->readline);
5412 Py_VISIT(self->read);
5413 Py_VISIT(self->file);
5414 Py_VISIT(self->memo);
5415 Py_VISIT(self->stack);
5416 Py_VISIT(self->pers_func);
5417 Py_VISIT(self->arg);
5418 Py_VISIT(self->last_string);
5419 Py_VISIT(self->find_class);
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005420 return 0;
5421}
5422
5423static int
5424Unpickler_clear(Unpicklerobject *self)
5425{
Thomas Woutersedf17d82006-04-15 17:28:34 +00005426 Py_CLEAR(self->readline);
5427 Py_CLEAR(self->read);
5428 Py_CLEAR(self->file);
5429 Py_CLEAR(self->memo);
5430 Py_CLEAR(self->stack);
5431 Py_CLEAR(self->pers_func);
5432 Py_CLEAR(self->arg);
5433 Py_CLEAR(self->last_string);
5434 Py_CLEAR(self->find_class);
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005435 return 0;
5436}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005437
5438static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005439Unpickler_getattr(Unpicklerobject *self, char *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005440{
5441 if (!strcmp(name, "persistent_load")) {
5442 if (!self->pers_func) {
5443 PyErr_SetString(PyExc_AttributeError, name);
5444 return NULL;
5445 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005446
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005447 Py_INCREF(self->pers_func);
5448 return self->pers_func;
5449 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005450
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005451 if (!strcmp(name, "find_global")) {
5452 if (!self->find_class) {
5453 PyErr_SetString(PyExc_AttributeError, name);
5454 return NULL;
5455 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005456
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005457 Py_INCREF(self->find_class);
5458 return self->find_class;
5459 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005460
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005461 if (!strcmp(name, "memo")) {
5462 if (!self->memo) {
5463 PyErr_SetString(PyExc_AttributeError, name);
5464 return NULL;
5465 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005466
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005467 Py_INCREF(self->memo);
5468 return self->memo;
5469 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005470
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005471 if (!strcmp(name, "UnpicklingError")) {
5472 Py_INCREF(UnpicklingError);
5473 return UnpicklingError;
5474 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005475
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005476 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005477}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005478
Guido van Rossum60456fd1997-04-09 17:36:32 +00005479
5480static int
Tim Peterscba30e22003-02-01 06:24:36 +00005481Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005482{
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005483
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005484 if (!strcmp(name, "persistent_load")) {
5485 Py_XDECREF(self->pers_func);
5486 self->pers_func = value;
5487 Py_XINCREF(value);
5488 return 0;
5489 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005490
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005491 if (!strcmp(name, "find_global")) {
5492 Py_XDECREF(self->find_class);
5493 self->find_class = value;
5494 Py_XINCREF(value);
5495 return 0;
5496 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005497
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005498 if (! value) {
5499 PyErr_SetString(PyExc_TypeError,
5500 "attribute deletion is not supported");
5501 return -1;
5502 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005503
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005504 if (strcmp(name, "memo") == 0) {
5505 if (!PyDict_Check(value)) {
Tim Peterscba30e22003-02-01 06:24:36 +00005506 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005507 "memo must be a dictionary");
5508 return -1;
5509 }
5510 Py_XDECREF(self->memo);
5511 self->memo = value;
5512 Py_INCREF(value);
5513 return 0;
5514 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005515
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005516 PyErr_SetString(PyExc_AttributeError, name);
5517 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005518}
5519
Tim Peters5bd2a792003-02-01 16:45:06 +00005520/* ---------------------------------------------------------------------------
5521 * Module-level functions.
5522 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005523
Martin v. Löwis544f1192004-07-27 05:22:33 +00005524/* dump(obj, file, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005525static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005526cpm_dump(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005527{
Martin v. Löwis15e62742006-02-27 16:46:16 +00005528 static char *kwlist[] = {"obj", "file", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005529 PyObject *ob, *file, *res = NULL;
5530 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005531 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005532
Martin v. Löwis544f1192004-07-27 05:22:33 +00005533 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist,
5534 &ob, &file, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005535 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005536
Tim Peters5bd2a792003-02-01 16:45:06 +00005537 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005538 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005539
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005540 if (dump(pickler, ob) < 0)
5541 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005542
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005543 Py_INCREF(Py_None);
5544 res = Py_None;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005545
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005546 finally:
5547 Py_XDECREF(pickler);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005548
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005549 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005550}
5551
5552
Martin v. Löwis544f1192004-07-27 05:22:33 +00005553/* dumps(obj, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005554static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005555cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005556{
Martin v. Löwis15e62742006-02-27 16:46:16 +00005557 static char *kwlist[] = {"obj", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005558 PyObject *ob, *file = 0, *res = NULL;
5559 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005560 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005561
Martin v. Löwis544f1192004-07-27 05:22:33 +00005562 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist,
5563 &ob, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005564 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005565
Tim Peterscba30e22003-02-01 06:24:36 +00005566 if (!( file = PycStringIO->NewOutput(128)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005567 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005568
Tim Peters5bd2a792003-02-01 16:45:06 +00005569 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005570 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005571
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005572 if (dump(pickler, ob) < 0)
5573 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005574
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005575 res = PycStringIO->cgetvalue(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005576
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005577 finally:
5578 Py_XDECREF(pickler);
5579 Py_XDECREF(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005580
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005581 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00005582}
5583
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005584
Tim Peters5bd2a792003-02-01 16:45:06 +00005585/* load(fileobj). */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005586static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005587cpm_load(PyObject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005588{
5589 Unpicklerobject *unpickler = 0;
Georg Brandl96a8c392006-05-29 21:04:52 +00005590 PyObject *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005591
Tim Peterscba30e22003-02-01 06:24:36 +00005592 if (!( unpickler = newUnpicklerobject(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005593 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005594
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005595 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005596
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005597 finally:
5598 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005599
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005600 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005601}
5602
5603
Tim Peters5bd2a792003-02-01 16:45:06 +00005604/* loads(string) */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005605static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005606cpm_loads(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005607{
5608 PyObject *ob, *file = 0, *res = NULL;
5609 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005610
Tim Peterscba30e22003-02-01 06:24:36 +00005611 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005612 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005613
Tim Peterscba30e22003-02-01 06:24:36 +00005614 if (!( file = PycStringIO->NewInput(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005615 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00005616
Tim Peterscba30e22003-02-01 06:24:36 +00005617 if (!( unpickler = newUnpicklerobject(file)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005618 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005619
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005620 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005621
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005622 finally:
5623 Py_XDECREF(file);
5624 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005625
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005626 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005627}
5628
5629
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005630PyDoc_STRVAR(Unpicklertype__doc__,
5631"Objects that know how to unpickle");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005632
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005633static PyTypeObject Unpicklertype = {
Martin v. Löwis68192102007-07-21 06:55:02 +00005634 PyVarObject_HEAD_INIT(NULL, 0)
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005635 "cPickle.Unpickler", /*tp_name*/
5636 sizeof(Unpicklerobject), /*tp_basicsize*/
5637 0,
5638 (destructor)Unpickler_dealloc, /* tp_dealloc */
5639 0, /* tp_print */
5640 (getattrfunc)Unpickler_getattr, /* tp_getattr */
5641 (setattrfunc)Unpickler_setattr, /* tp_setattr */
5642 0, /* tp_compare */
5643 0, /* tp_repr */
5644 0, /* tp_as_number */
5645 0, /* tp_as_sequence */
5646 0, /* tp_as_mapping */
5647 0, /* tp_hash */
5648 0, /* tp_call */
5649 0, /* tp_str */
5650 0, /* tp_getattro */
5651 0, /* tp_setattro */
5652 0, /* tp_as_buffer */
5653 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
5654 Unpicklertype__doc__, /* tp_doc */
5655 (traverseproc)Unpickler_traverse, /* tp_traverse */
5656 (inquiry)Unpickler_clear, /* tp_clear */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005657};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005658
Guido van Rossum60456fd1997-04-09 17:36:32 +00005659static struct PyMethodDef cPickle_methods[] = {
Martin v. Löwis544f1192004-07-27 05:22:33 +00005660 {"dump", (PyCFunction)cpm_dump, METH_VARARGS | METH_KEYWORDS,
5661 PyDoc_STR("dump(obj, file, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005662 "Write an object in pickle format to the given file.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005663 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005664 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005665 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005666
Martin v. Löwis544f1192004-07-27 05:22:33 +00005667 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS | METH_KEYWORDS,
5668 PyDoc_STR("dumps(obj, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005669 "Return a string containing an object in pickle format.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005670 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005671 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005672 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005673
Georg Brandl96a8c392006-05-29 21:04:52 +00005674 {"load", (PyCFunction)cpm_load, METH_O,
Neal Norwitz200788c2002-08-13 22:20:41 +00005675 PyDoc_STR("load(file) -- Load a pickle from the given file")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005676
Neal Norwitzb0493252002-03-31 14:44:22 +00005677 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005678 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005679
Martin v. Löwis544f1192004-07-27 05:22:33 +00005680 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS | METH_KEYWORDS,
5681 PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005682 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005683 "This takes a file-like object for writing a pickle data stream.\n"
5684 "The optional proto argument tells the pickler to use the given\n"
5685 "protocol; supported protocols are 0, 1, 2. The default\n"
5686 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5687 "only protocol that can be written to a file opened in text\n"
5688 "mode and read back successfully. When using a protocol higher\n"
5689 "than 0, make sure the file is opened in binary mode, both when\n"
5690 "pickling and unpickling.)\n"
5691 "\n"
5692 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5693 "more efficient than protocol 1.\n"
5694 "\n"
5695 "Specifying a negative protocol version selects the highest\n"
5696 "protocol version supported. The higher the protocol used, the\n"
5697 "more recent the version of Python needed to read the pickle\n"
5698 "produced.\n"
5699 "\n"
5700 "The file parameter must have a write() method that accepts a single\n"
5701 "string argument. It can thus be an open file object, a StringIO\n"
5702 "object, or any other custom object that meets this interface.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005703 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005704
Georg Brandl96a8c392006-05-29 21:04:52 +00005705 {"Unpickler", (PyCFunction)get_Unpickler, METH_O,
Tim Peters5bd2a792003-02-01 16:45:06 +00005706 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5707
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005708 { NULL, NULL }
5709};
5710
Guido van Rossum60456fd1997-04-09 17:36:32 +00005711static int
Tim Peterscba30e22003-02-01 06:24:36 +00005712init_stuff(PyObject *module_dict)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005713{
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005714 PyObject *copyreg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005715
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005716#define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005717
Tim Peters3cfe7542003-05-21 21:29:48 +00005718 if (PyType_Ready(&Unpicklertype) < 0)
5719 return -1;
5720 if (PyType_Ready(&Picklertype) < 0)
5721 return -1;
5722
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005723 INIT_STR(__class__);
5724 INIT_STR(__getinitargs__);
5725 INIT_STR(__dict__);
5726 INIT_STR(__getstate__);
5727 INIT_STR(__setstate__);
5728 INIT_STR(__name__);
5729 INIT_STR(__main__);
5730 INIT_STR(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00005731 INIT_STR(__reduce_ex__);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005732 INIT_STR(write);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005733 INIT_STR(append);
5734 INIT_STR(read);
5735 INIT_STR(readline);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005736 INIT_STR(dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005737
Georg Brandldffbf5f2008-05-20 07:49:57 +00005738 if (!( copyreg = PyImport_ImportModule("copy_reg")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005739 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005740
Tim Peters1f1b2d22003-02-01 02:16:37 +00005741 /* This is special because we want to use a different
5742 one in restricted mode. */
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005743 dispatch_table = PyObject_GetAttr(copyreg, dispatch_table_str);
Tim Peters5b7da392003-02-04 00:21:07 +00005744 if (!dispatch_table) return -1;
5745
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005746 extension_registry = PyObject_GetAttrString(copyreg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005747 "_extension_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005748 if (!extension_registry) return -1;
5749
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005750 inverted_registry = PyObject_GetAttrString(copyreg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005751 "_inverted_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005752 if (!inverted_registry) return -1;
5753
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005754 extension_cache = PyObject_GetAttrString(copyreg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005755 "_extension_cache");
Tim Peters5b7da392003-02-04 00:21:07 +00005756 if (!extension_cache) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005757
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005758 Py_DECREF(copyreg);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005759
Tim Peters731098b2003-02-04 20:56:09 +00005760 if (!(empty_tuple = PyTuple_New(0)))
5761 return -1;
5762
5763 two_tuple = PyTuple_New(2);
5764 if (two_tuple == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005765 return -1;
Tim Peters3e667d52003-02-04 21:47:44 +00005766 /* We use this temp container with no regard to refcounts, or to
5767 * keeping containees alive. Exempt from GC, because we don't
5768 * want anything looking at two_tuple() by magic.
5769 */
5770 PyObject_GC_UnTrack(two_tuple);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005771
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005772 /* Ugh */
5773 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5774 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5775 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005776
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005777 if (!( t=PyDict_New())) return -1;
5778 if (!( r=PyRun_String(
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005779 "def __str__(self):\n"
5780 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5781 Py_file_input,
5782 module_dict, t) )) return -1;
5783 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005784
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005785 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
Tim Peterscba30e22003-02-01 06:24:36 +00005786 if (!PickleError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005787 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005788
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005789 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005790
Tim Peterscba30e22003-02-01 06:24:36 +00005791 PicklingError = PyErr_NewException("cPickle.PicklingError",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005792 PickleError, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00005793 if (!PicklingError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005794 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005795
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005796 if (!( t=PyDict_New())) return -1;
5797 if (!( r=PyRun_String(
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005798 "def __str__(self):\n"
5799 " a=self.args\n"
5800 " a=a and type(a[0]) or '(what)'\n"
5801 " return 'Cannot pickle %s objects' % a\n"
5802 , Py_file_input,
5803 module_dict, t) )) return -1;
5804 Py_DECREF(r);
Tim Peters84e87f32001-03-17 04:50:51 +00005805
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005806 if (!( UnpickleableError = PyErr_NewException(
Tim Peterscba30e22003-02-01 06:24:36 +00005807 "cPickle.UnpickleableError", PicklingError, t)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005808 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005809
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005810 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005811
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005812 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
Tim Peterscba30e22003-02-01 06:24:36 +00005813 PickleError, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005814 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005815
Martin v. Löwis658009a2002-09-16 17:26:24 +00005816 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5817 UnpicklingError, NULL)))
5818 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +00005819
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005820 if (PyDict_SetItemString(module_dict, "PickleError",
5821 PickleError) < 0)
5822 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005823
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005824 if (PyDict_SetItemString(module_dict, "PicklingError",
5825 PicklingError) < 0)
5826 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005827
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005828 if (PyDict_SetItemString(module_dict, "UnpicklingError",
5829 UnpicklingError) < 0)
5830 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005831
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005832 if (PyDict_SetItemString(module_dict, "UnpickleableError",
5833 UnpickleableError) < 0)
5834 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005835
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005836 if (PyDict_SetItemString(module_dict, "BadPickleGet",
5837 BadPickleGet) < 0)
5838 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005839
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005840 PycString_IMPORT;
Guido van Rossum053b8df1998-11-25 16:18:00 +00005841
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005842 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005843}
5844
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005845#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5846#define PyMODINIT_FUNC void
Guido van Rossumf9ffb031999-02-04 14:54:04 +00005847#endif
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005848PyMODINIT_FUNC
Tim Peterscba30e22003-02-01 06:24:36 +00005849initcPickle(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005850{
5851 PyObject *m, *d, *di, *v, *k;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005852 Py_ssize_t i;
Tim Peters5b7da392003-02-04 00:21:07 +00005853 char *rev = "1.71"; /* XXX when does this change? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005854 PyObject *format_version;
5855 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005856
Alexandre Vassalotti3c4971c2008-05-16 19:14:31 +00005857 /* XXX: Should mention that the pickle module will include the C
5858 XXX: optimized implementation automatically. */
5859 if (PyErr_WarnPy3k("the cPickle module has been removed in "
5860 "Python 3.0", 2) < 0)
5861 return;
5862
Christian Heimese93237d2007-12-19 02:37:44 +00005863 Py_TYPE(&Picklertype) = &PyType_Type;
5864 Py_TYPE(&Unpicklertype) = &PyType_Type;
5865 Py_TYPE(&PdataType) = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005866
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005867 /* Initialize some pieces. We need to do this before module creation,
Tim Peters5b7da392003-02-04 00:21:07 +00005868 * so we're forced to use a temporary dictionary. :(
5869 */
5870 di = PyDict_New();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005871 if (!di) return;
5872 if (init_stuff(di) < 0) return;
Guido van Rossumebba4202000-09-07 14:35:37 +00005873
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005874 /* Create the module and add the functions */
5875 m = Py_InitModule4("cPickle", cPickle_methods,
5876 cPickle_module_documentation,
5877 (PyObject*)NULL,PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00005878 if (m == NULL)
5879 return;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005880
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005881 /* Add some symbolic constants to the module */
5882 d = PyModule_GetDict(m);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005883 v = PyString_FromString(rev);
Tim Peters5b7da392003-02-04 00:21:07 +00005884 PyDict_SetItemString(d, "__version__", v);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005885 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00005886
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005887 /* Copy data from di. Waaa. */
5888 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
5889 if (PyObject_SetItem(d, k, v) < 0) {
5890 Py_DECREF(di);
5891 return;
5892 }
5893 }
5894 Py_DECREF(di);
Guido van Rossumebba4202000-09-07 14:35:37 +00005895
Tim Peters8587b3c2003-02-13 15:44:41 +00005896 i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
5897 if (i < 0)
5898 return;
5899
Tim Peters5b7da392003-02-04 00:21:07 +00005900 /* These are purely informational; no code uses them. */
5901 /* File format version we write. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005902 format_version = PyString_FromString("2.0");
Tim Peters5b7da392003-02-04 00:21:07 +00005903 /* Format versions we can read. */
5904 compatible_formats = Py_BuildValue("[sssss]",
5905 "1.0", /* Original protocol 0 */
5906 "1.1", /* Protocol 0 + INST */
5907 "1.2", /* Original protocol 1 */
5908 "1.3", /* Protocol 1 + BINFLOAT */
Tim Peters0dd23aa2003-02-04 00:30:46 +00005909 "2.0"); /* Original protocol 2 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005910 PyDict_SetItemString(d, "format_version", format_version);
5911 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
5912 Py_XDECREF(format_version);
5913 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005914}