blob: fb13ba18bc459ecc56a9df125a9d42624015ae47 [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 Vassalotti9510e4a2008-05-11 08:25:28 +0000135 *copyreg_str, *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;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000666 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000667}
668
669
Martin v. Löwis18e16552006-02-15 17:27:45 +0000670static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000671readline_other(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000672{
673 PyObject *str;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000674 Py_ssize_t str_size;
Tim Peterscba30e22003-02-01 06:24:36 +0000675
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000676 if (!( str = PyObject_CallObject(self->readline, empty_tuple))) {
677 return -1;
678 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000679
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000680 if ((str_size = PyString_Size(str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000681 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000682
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000683 Py_XDECREF(self->last_string);
684 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000685
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000686 if (! (*s = PyString_AsString(str)))
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 return str_size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000690}
691
Tim Petersee1a53c2003-02-02 02:57:53 +0000692/* Copy the first n bytes from s into newly malloc'ed memory, plus a
693 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
694 * The caller is responsible for free()'ing the return value.
695 */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000696static char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000697pystrndup(const char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000698{
Tim Petersee1a53c2003-02-02 02:57:53 +0000699 char *r = (char *)malloc(n+1);
700 if (r == NULL)
701 return (char*)PyErr_NoMemory();
702 memcpy(r, s, n);
703 r[n] = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000704 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000705}
706
707
708static int
Tim Peterscba30e22003-02-01 06:24:36 +0000709get(Picklerobject *self, PyObject *id)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000710{
711 PyObject *value, *mv;
712 long c_value;
713 char s[30];
714 size_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000715
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000716 if (!( mv = PyDict_GetItem(self->memo, id))) {
717 PyErr_SetObject(PyExc_KeyError, id);
718 return -1;
719 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000720
Tim Peterscba30e22003-02-01 06:24:36 +0000721 if (!( value = PyTuple_GetItem(mv, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000722 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000723
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000724 if (!( PyInt_Check(value))) {
725 PyErr_SetString(PicklingError, "no int where int expected in memo");
726 return -1;
727 }
728 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000729
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000730 if (!self->bin) {
731 s[0] = GET;
732 PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value);
733 len = strlen(s);
734 }
735 else if (Pdata_Check(self->file)) {
736 if (write_other(self, NULL, 0) < 0) return -1;
737 PDATA_APPEND(self->file, mv, -1);
738 return 0;
739 }
740 else {
741 if (c_value < 256) {
742 s[0] = BINGET;
743 s[1] = (int)(c_value & 0xff);
744 len = 2;
745 }
746 else {
747 s[0] = LONG_BINGET;
748 s[1] = (int)(c_value & 0xff);
749 s[2] = (int)((c_value >> 8) & 0xff);
750 s[3] = (int)((c_value >> 16) & 0xff);
751 s[4] = (int)((c_value >> 24) & 0xff);
752 len = 5;
753 }
754 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000755
Tim Peters0bc93f52003-02-02 18:29:33 +0000756 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000757 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000758
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000759 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000760}
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000761
Guido van Rossum60456fd1997-04-09 17:36:32 +0000762
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000763static int
Tim Peterscba30e22003-02-01 06:24:36 +0000764put(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000765{
Christian Heimese93237d2007-12-19 02:37:44 +0000766 if (Py_REFCNT(ob) < 2 || self->fast)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000767 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000768
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000769 return put2(self, ob);
770}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000771
Guido van Rossum053b8df1998-11-25 16:18:00 +0000772
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000773static int
Tim Peterscba30e22003-02-01 06:24:36 +0000774put2(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000775{
776 char c_str[30];
777 int p;
778 size_t len;
779 int res = -1;
780 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000781
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000782 if (self->fast)
783 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000784
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000785 if ((p = PyDict_Size(self->memo)) < 0)
786 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000787
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000788 /* Make sure memo keys are positive! */
Tim Peters70b02d72003-02-02 17:26:40 +0000789 /* XXX Why?
790 * XXX And does "positive" really mean non-negative?
791 * XXX pickle.py starts with PUT index 0, not 1. This makes for
792 * XXX gratuitous differences between the pickling modules.
793 */
Tim Peterscba30e22003-02-01 06:24:36 +0000794 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000795
Tim Peterscba30e22003-02-01 06:24:36 +0000796 if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000797 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000798
Tim Peterscba30e22003-02-01 06:24:36 +0000799 if (!( memo_len = PyInt_FromLong(p)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000800 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000801
Tim Peterscba30e22003-02-01 06:24:36 +0000802 if (!( t = PyTuple_New(2)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000803 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000804
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000805 PyTuple_SET_ITEM(t, 0, memo_len);
806 Py_INCREF(memo_len);
807 PyTuple_SET_ITEM(t, 1, ob);
808 Py_INCREF(ob);
809
810 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
811 goto finally;
812
813 if (!self->bin) {
814 c_str[0] = PUT;
815 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p);
816 len = strlen(c_str);
817 }
818 else if (Pdata_Check(self->file)) {
819 if (write_other(self, NULL, 0) < 0) return -1;
820 PDATA_APPEND(self->file, memo_len, -1);
821 res=0; /* Job well done ;) */
822 goto finally;
823 }
824 else {
825 if (p >= 256) {
826 c_str[0] = LONG_BINPUT;
827 c_str[1] = (int)(p & 0xff);
828 c_str[2] = (int)((p >> 8) & 0xff);
829 c_str[3] = (int)((p >> 16) & 0xff);
830 c_str[4] = (int)((p >> 24) & 0xff);
831 len = 5;
832 }
833 else {
834 c_str[0] = BINPUT;
835 c_str[1] = p;
836 len = 2;
837 }
838 }
839
Tim Peters0bc93f52003-02-02 18:29:33 +0000840 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000841 goto finally;
842
843 res = 0;
844
845 finally:
846 Py_XDECREF(py_ob_id);
847 Py_XDECREF(memo_len);
848 Py_XDECREF(t);
849
850 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000851}
852
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000853static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000854whichmodule(PyObject *global, PyObject *global_name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000855{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000856 Py_ssize_t i, j;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000857 PyObject *module = 0, *modules_dict = 0,
858 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000859
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000860 module = PyObject_GetAttrString(global, "__module__");
Jeremy Hyltonf00368f2003-06-16 20:19:49 +0000861 if (module)
862 return module;
863 if (PyErr_ExceptionMatches(PyExc_AttributeError))
864 PyErr_Clear();
865 else
866 return NULL;
Guido van Rossum45188231997-09-28 05:38:51 +0000867
Tim Peterscba30e22003-02-01 06:24:36 +0000868 if (!( modules_dict = PySys_GetObject("modules")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000869 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000870
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000871 i = 0;
872 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000873
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000874 if (PyObject_Compare(name, __main___str)==0) continue;
Tim Peters84e87f32001-03-17 04:50:51 +0000875
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000876 global_name_attr = PyObject_GetAttr(module, global_name);
877 if (!global_name_attr) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +0000878 if (PyErr_ExceptionMatches(PyExc_AttributeError))
879 PyErr_Clear();
880 else
881 return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000882 continue;
883 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000884
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000885 if (global_name_attr != global) {
886 Py_DECREF(global_name_attr);
887 continue;
888 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000889
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000890 Py_DECREF(global_name_attr);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000891
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000892 break;
893 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000894
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000895 /* The following implements the rule in pickle.py added in 1.5
896 that used __main__ if no module is found. I don't actually
897 like this rule. jlf
898 */
899 if (!j) {
900 j=1;
901 name=__main___str;
902 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000903
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000904 Py_INCREF(name);
905 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000906}
907
908
Guido van Rossum60456fd1997-04-09 17:36:32 +0000909static int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000910fast_save_enter(Picklerobject *self, PyObject *obj)
911{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000912 /* if fast_container < 0, we're doing an error exit. */
913 if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
914 PyObject *key = NULL;
915 if (self->fast_memo == NULL) {
916 self->fast_memo = PyDict_New();
917 if (self->fast_memo == NULL) {
918 self->fast_container = -1;
919 return 0;
920 }
921 }
922 key = PyLong_FromVoidPtr(obj);
923 if (key == NULL)
924 return 0;
925 if (PyDict_GetItem(self->fast_memo, key)) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000926 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000927 PyErr_Format(PyExc_ValueError,
Tim Peters92c8bb32003-02-13 23:00:26 +0000928 "fast mode: can't pickle cyclic objects "
929 "including object type %s at %p",
Christian Heimese93237d2007-12-19 02:37:44 +0000930 Py_TYPE(obj)->tp_name, obj);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000931 self->fast_container = -1;
932 return 0;
933 }
934 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000935 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000936 self->fast_container = -1;
937 return 0;
938 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000939 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000940 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000941 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000942}
943
Tim Peterscba30e22003-02-01 06:24:36 +0000944int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000945fast_save_leave(Picklerobject *self, PyObject *obj)
946{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000947 if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
948 PyObject *key = PyLong_FromVoidPtr(obj);
949 if (key == NULL)
950 return 0;
951 if (PyDict_DelItem(self->fast_memo, key) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000952 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000953 return 0;
954 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000955 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000956 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000957 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000958}
959
960static int
Tim Peterscba30e22003-02-01 06:24:36 +0000961save_none(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000962{
963 static char none = NONE;
Tim Peters0bc93f52003-02-02 18:29:33 +0000964 if (self->write_func(self, &none, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000965 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000966
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000967 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000968}
969
Guido van Rossum77f6a652002-04-03 22:41:51 +0000970static int
Tim Peterscba30e22003-02-01 06:24:36 +0000971save_bool(Picklerobject *self, PyObject *args)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000972{
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000973 static const char *buf[2] = {FALSE, TRUE};
Guido van Rossume2763392002-04-05 19:30:08 +0000974 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
Guido van Rossum77f6a652002-04-03 22:41:51 +0000975 long l = PyInt_AS_LONG((PyIntObject *)args);
976
Tim Peters3c67d792003-02-02 17:59:11 +0000977 if (self->proto >= 2) {
978 char opcode = l ? NEWTRUE : NEWFALSE;
979 if (self->write_func(self, &opcode, 1) < 0)
980 return -1;
981 }
982 else if (self->write_func(self, buf[l], len[l]) < 0)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000983 return -1;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000984 return 0;
985}
Tim Peters84e87f32001-03-17 04:50:51 +0000986
Guido van Rossum60456fd1997-04-09 17:36:32 +0000987static int
Tim Peterscba30e22003-02-01 06:24:36 +0000988save_int(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000989{
990 char c_str[32];
991 long l = PyInt_AS_LONG((PyIntObject *)args);
992 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000993
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000994 if (!self->bin
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000995#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000996 || l > 0x7fffffffL
997 || l < -0x80000000L
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000998#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000999 ) {
1000 /* Text-mode pickle, or long too big to fit in the 4-byte
1001 * signed BININT format: store as a string.
1002 */
1003 c_str[0] = INT;
1004 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
Tim Peters0bc93f52003-02-02 18:29:33 +00001005 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001006 return -1;
1007 }
1008 else {
1009 /* Binary pickle and l fits in a signed 4-byte int. */
1010 c_str[1] = (int)( l & 0xff);
1011 c_str[2] = (int)((l >> 8) & 0xff);
1012 c_str[3] = (int)((l >> 16) & 0xff);
1013 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001014
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001015 if ((c_str[4] == 0) && (c_str[3] == 0)) {
1016 if (c_str[2] == 0) {
1017 c_str[0] = BININT1;
1018 len = 2;
1019 }
1020 else {
1021 c_str[0] = BININT2;
1022 len = 3;
1023 }
1024 }
1025 else {
1026 c_str[0] = BININT;
1027 len = 5;
1028 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001029
Tim Peters0bc93f52003-02-02 18:29:33 +00001030 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001031 return -1;
1032 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001033
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001034 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001035}
1036
1037
1038static int
Tim Peterscba30e22003-02-01 06:24:36 +00001039save_long(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001040{
Armin Rigo7ccbca92006-10-04 12:17:45 +00001041 Py_ssize_t size;
Tim Petersee1a53c2003-02-02 02:57:53 +00001042 int res = -1;
1043 PyObject *repr = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001044
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001045 static char l = LONG;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001046
Tim Petersee1a53c2003-02-02 02:57:53 +00001047 if (self->proto >= 2) {
1048 /* Linear-time pickling. */
1049 size_t nbits;
1050 size_t nbytes;
1051 unsigned char *pdata;
1052 char c_str[5];
1053 int i;
1054 int sign = _PyLong_Sign(args);
1055
1056 if (sign == 0) {
1057 /* It's 0 -- an empty bytestring. */
1058 c_str[0] = LONG1;
1059 c_str[1] = 0;
1060 i = self->write_func(self, c_str, 2);
1061 if (i < 0) goto finally;
1062 res = 0;
1063 goto finally;
1064 }
1065 nbits = _PyLong_NumBits(args);
1066 if (nbits == (size_t)-1 && PyErr_Occurred())
1067 goto finally;
1068 /* How many bytes do we need? There are nbits >> 3 full
1069 * bytes of data, and nbits & 7 leftover bits. If there
1070 * are any leftover bits, then we clearly need another
1071 * byte. Wnat's not so obvious is that we *probably*
1072 * need another byte even if there aren't any leftovers:
1073 * the most-significant bit of the most-significant byte
1074 * acts like a sign bit, and it's usually got a sense
1075 * opposite of the one we need. The exception is longs
1076 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1077 * its own 256's-complement, so has the right sign bit
1078 * even without the extra byte. That's a pain to check
1079 * for in advance, though, so we always grab an extra
1080 * byte at the start, and cut it back later if possible.
1081 */
1082 nbytes = (nbits >> 3) + 1;
Armin Rigo7ccbca92006-10-04 12:17:45 +00001083 if (nbytes > INT_MAX) {
Tim Petersee1a53c2003-02-02 02:57:53 +00001084 PyErr_SetString(PyExc_OverflowError, "long too large "
1085 "to pickle");
1086 goto finally;
1087 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001088 repr = PyString_FromStringAndSize(NULL, (int)nbytes);
Tim Petersee1a53c2003-02-02 02:57:53 +00001089 if (repr == NULL) goto finally;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001090 pdata = (unsigned char *)PyString_AS_STRING(repr);
Tim Petersee1a53c2003-02-02 02:57:53 +00001091 i = _PyLong_AsByteArray((PyLongObject *)args,
1092 pdata, nbytes,
1093 1 /* little endian */, 1 /* signed */);
1094 if (i < 0) goto finally;
1095 /* If the long is negative, this may be a byte more than
1096 * needed. This is so iff the MSB is all redundant sign
1097 * bits.
1098 */
1099 if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
1100 (pdata[nbytes - 2] & 0x80) != 0)
1101 --nbytes;
1102
1103 if (nbytes < 256) {
1104 c_str[0] = LONG1;
1105 c_str[1] = (char)nbytes;
1106 size = 2;
1107 }
1108 else {
1109 c_str[0] = LONG4;
1110 size = (int)nbytes;
1111 for (i = 1; i < 5; i++) {
1112 c_str[i] = (char)(size & 0xff);
1113 size >>= 8;
1114 }
1115 size = 5;
1116 }
1117 i = self->write_func(self, c_str, size);
1118 if (i < 0) goto finally;
1119 i = self->write_func(self, (char *)pdata, (int)nbytes);
1120 if (i < 0) goto finally;
1121 res = 0;
1122 goto finally;
1123 }
1124
1125 /* proto < 2: write the repr and newline. This is quadratic-time
1126 * (in the number of digits), in both directions.
1127 */
Tim Peterscba30e22003-02-01 06:24:36 +00001128 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001129 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001130
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001131 if ((size = PyString_Size(repr)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001132 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001133
Tim Peters0bc93f52003-02-02 18:29:33 +00001134 if (self->write_func(self, &l, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001135 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001136
Tim Peters0bc93f52003-02-02 18:29:33 +00001137 if (self->write_func(self,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001138 PyString_AS_STRING((PyStringObject *)repr),
Tim Peters0bc93f52003-02-02 18:29:33 +00001139 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001140 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001141
Tim Peters0bc93f52003-02-02 18:29:33 +00001142 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001143 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001144
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001145 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001146
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001147 finally:
1148 Py_XDECREF(repr);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001149 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001150}
1151
1152
1153static int
Tim Peterscba30e22003-02-01 06:24:36 +00001154save_float(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001155{
1156 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001157
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001158 if (self->bin) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001159 char str[9];
Tim Peters9905b942003-03-20 20:53:32 +00001160 str[0] = BINFLOAT;
1161 if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001162 return -1;
Tim Peters0bc93f52003-02-02 18:29:33 +00001163 if (self->write_func(self, str, 9) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001164 return -1;
1165 }
1166 else {
1167 char c_str[250];
1168 c_str[0] = FLOAT;
Georg Brandlde9b6242006-04-30 11:13:56 +00001169 PyOS_ascii_formatd(c_str + 1, sizeof(c_str) - 2, "%.17g", x);
1170 /* Extend the formatted string with a newline character */
1171 strcat(c_str, "\n");
Guido van Rossum60456fd1997-04-09 17:36:32 +00001172
Tim Peters0bc93f52003-02-02 18:29:33 +00001173 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001174 return -1;
1175 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001176
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001177 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001178}
1179
1180
1181static int
Tim Peterscba30e22003-02-01 06:24:36 +00001182save_string(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001183{
1184 int size, len;
1185 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001186
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001187 if ((size = PyString_Size(args)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001188 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001189
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001190 if (!self->bin) {
1191 char *repr_str;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001192
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001193 static char string = STRING;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001194
Tim Peterscba30e22003-02-01 06:24:36 +00001195 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001196 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001197
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001198 if ((len = PyString_Size(repr)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001199 goto err;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001200 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001201
Tim Peters0bc93f52003-02-02 18:29:33 +00001202 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001203 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001204
Tim Peters0bc93f52003-02-02 18:29:33 +00001205 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001206 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001207
Tim Peters0bc93f52003-02-02 18:29:33 +00001208 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001209 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001210
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001211 Py_XDECREF(repr);
1212 }
1213 else {
1214 int i;
1215 char c_str[5];
Guido van Rossum60456fd1997-04-09 17:36:32 +00001216
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001217 if ((size = PyString_Size(args)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001218 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001219
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001220 if (size < 256) {
1221 c_str[0] = SHORT_BINSTRING;
1222 c_str[1] = size;
1223 len = 2;
1224 }
Armin Rigo7ccbca92006-10-04 12:17:45 +00001225 else if (size <= INT_MAX) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001226 c_str[0] = BINSTRING;
1227 for (i = 1; i < 5; i++)
1228 c_str[i] = (int)(size >> ((i - 1) * 8));
1229 len = 5;
1230 }
Armin Rigo7ccbca92006-10-04 12:17:45 +00001231 else
1232 return -1; /* string too large */
Guido van Rossum60456fd1997-04-09 17:36:32 +00001233
Tim Peters0bc93f52003-02-02 18:29:33 +00001234 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001235 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001236
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001237 if (size > 128 && Pdata_Check(self->file)) {
1238 if (write_other(self, NULL, 0) < 0) return -1;
1239 PDATA_APPEND(self->file, args, -1);
1240 }
1241 else {
Tim Peters0bc93f52003-02-02 18:29:33 +00001242 if (self->write_func(self,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001243 PyString_AS_STRING(
1244 (PyStringObject *)args),
Tim Peters0bc93f52003-02-02 18:29:33 +00001245 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001246 return -1;
1247 }
1248 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001249
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001250 if (doput)
1251 if (put(self, args) < 0)
1252 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001253
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001254 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001255
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001256 err:
1257 Py_XDECREF(repr);
1258 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001259}
1260
1261
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001262#ifdef Py_USING_UNICODE
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001263/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1264 backslash and newline characters to \uXXXX escapes. */
1265static PyObject *
Alexandre Vassalotti966322f2008-12-27 07:16:40 +00001266modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size)
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001267{
Alexandre Vassalotti966322f2008-12-27 07:16:40 +00001268 PyObject *repr;
1269 char *p;
1270 char *q;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001271
Alexandre Vassalotti966322f2008-12-27 07:16:40 +00001272 static const char *hexdigit = "0123456789abcdef";
1273#ifdef Py_UNICODE_WIDE
1274 const Py_ssize_t expandsize = 10;
1275#else
1276 const Py_ssize_t expandsize = 6;
1277#endif
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001278
Alexandre Vassalotti966322f2008-12-27 07:16:40 +00001279 if (size > PY_SSIZE_T_MAX / expandsize)
1280 return PyErr_NoMemory();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001281
Alexandre Vassalotti966322f2008-12-27 07:16:40 +00001282 repr = PyString_FromStringAndSize(NULL, expandsize * size);
1283 if (repr == NULL)
1284 return NULL;
1285 if (size == 0)
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001286 return repr;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001287
Alexandre Vassalotti966322f2008-12-27 07:16:40 +00001288 p = q = PyString_AS_STRING(repr);
1289 while (size-- > 0) {
1290 Py_UNICODE ch = *s++;
1291#ifdef Py_UNICODE_WIDE
1292 /* Map 32-bit characters to '\Uxxxxxxxx' */
1293 if (ch >= 0x10000) {
1294 *p++ = '\\';
1295 *p++ = 'U';
1296 *p++ = hexdigit[(ch >> 28) & 0xf];
1297 *p++ = hexdigit[(ch >> 24) & 0xf];
1298 *p++ = hexdigit[(ch >> 20) & 0xf];
1299 *p++ = hexdigit[(ch >> 16) & 0xf];
1300 *p++ = hexdigit[(ch >> 12) & 0xf];
1301 *p++ = hexdigit[(ch >> 8) & 0xf];
1302 *p++ = hexdigit[(ch >> 4) & 0xf];
1303 *p++ = hexdigit[ch & 15];
1304 }
1305 else
1306#else
1307 /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */
1308 if (ch >= 0xD800 && ch < 0xDC00) {
1309 Py_UNICODE ch2;
1310 Py_UCS4 ucs;
1311
1312 ch2 = *s++;
1313 size--;
1314 if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) {
1315 ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000;
1316 *p++ = '\\';
1317 *p++ = 'U';
1318 *p++ = hexdigit[(ucs >> 28) & 0xf];
1319 *p++ = hexdigit[(ucs >> 24) & 0xf];
1320 *p++ = hexdigit[(ucs >> 20) & 0xf];
1321 *p++ = hexdigit[(ucs >> 16) & 0xf];
1322 *p++ = hexdigit[(ucs >> 12) & 0xf];
1323 *p++ = hexdigit[(ucs >> 8) & 0xf];
1324 *p++ = hexdigit[(ucs >> 4) & 0xf];
1325 *p++ = hexdigit[ucs & 0xf];
1326 continue;
1327 }
1328 /* Fall through: isolated surrogates are copied as-is */
1329 s--;
1330 size++;
1331 }
1332#endif
1333 /* Map 16-bit characters to '\uxxxx' */
1334 if (ch >= 256 || ch == '\\' || ch == '\n') {
1335 *p++ = '\\';
1336 *p++ = 'u';
1337 *p++ = hexdigit[(ch >> 12) & 0xf];
1338 *p++ = hexdigit[(ch >> 8) & 0xf];
1339 *p++ = hexdigit[(ch >> 4) & 0xf];
1340 *p++ = hexdigit[ch & 15];
1341 }
1342 /* Copy everything else as-is */
1343 else
1344 *p++ = (char) ch;
1345 }
1346 *p = '\0';
1347 _PyString_Resize(&repr, p - q);
1348 return repr;
1349}
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001350
Guido van Rossum60456fd1997-04-09 17:36:32 +00001351static int
Tim Peterscba30e22003-02-01 06:24:36 +00001352save_unicode(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001353{
Armin Rigo7ccbca92006-10-04 12:17:45 +00001354 Py_ssize_t size, len;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001355 PyObject *repr=0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001356
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001357 if (!PyUnicode_Check(args))
1358 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001359
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001360 if (!self->bin) {
1361 char *repr_str;
1362 static char string = UNICODE;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001363
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001364 repr = modified_EncodeRawUnicodeEscape(
1365 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
Tim Peterscba30e22003-02-01 06:24:36 +00001366 if (!repr)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001367 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001368
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001369 if ((len = PyString_Size(repr)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001370 goto err;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001371 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001372
Tim Peters0bc93f52003-02-02 18:29:33 +00001373 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001374 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001375
Tim Peters0bc93f52003-02-02 18:29:33 +00001376 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001377 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001378
Tim Peters0bc93f52003-02-02 18:29:33 +00001379 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001380 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001381
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001382 Py_XDECREF(repr);
1383 }
1384 else {
1385 int i;
1386 char c_str[5];
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001387
Tim Peterscba30e22003-02-01 06:24:36 +00001388 if (!( repr = PyUnicode_AsUTF8String(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001389 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001390
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001391 if ((size = PyString_Size(repr)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001392 goto err;
Armin Rigo7ccbca92006-10-04 12:17:45 +00001393 if (size > INT_MAX)
1394 return -1; /* string too large */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001395
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001396 c_str[0] = BINUNICODE;
1397 for (i = 1; i < 5; i++)
1398 c_str[i] = (int)(size >> ((i - 1) * 8));
1399 len = 5;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001400
Tim Peters0bc93f52003-02-02 18:29:33 +00001401 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001402 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001403
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001404 if (size > 128 && Pdata_Check(self->file)) {
1405 if (write_other(self, NULL, 0) < 0)
1406 goto err;
1407 PDATA_APPEND(self->file, repr, -1);
1408 }
1409 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001410 if (self->write_func(self, PyString_AS_STRING(repr),
Tim Peters0bc93f52003-02-02 18:29:33 +00001411 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001412 goto err;
1413 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001414
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001415 Py_DECREF(repr);
1416 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001417
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001418 if (doput)
1419 if (put(self, args) < 0)
1420 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001421
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001422 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001423
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001424 err:
1425 Py_XDECREF(repr);
1426 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001427}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001428#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001429
Tim Peters1d63c9f2003-02-02 20:29:39 +00001430/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1431static int
Tim Peters67920142003-02-05 03:46:17 +00001432store_tuple_elements(Picklerobject *self, PyObject *t, int len)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001433{
1434 int i;
1435 int res = -1; /* guilty until proved innocent */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001436
Tim Peters1d63c9f2003-02-02 20:29:39 +00001437 assert(PyTuple_Size(t) == len);
1438
1439 for (i = 0; i < len; i++) {
1440 PyObject *element = PyTuple_GET_ITEM(t, i);
1441
1442 if (element == NULL)
1443 goto finally;
1444 if (save(self, element, 0) < 0)
1445 goto finally;
1446 }
1447 res = 0;
1448
1449 finally:
1450 return res;
1451}
1452
1453/* Tuples are ubiquitous in the pickle protocols, so many techniques are
1454 * used across protocols to minimize the space needed to pickle them.
Tim Peters67920142003-02-05 03:46:17 +00001455 * Tuples are also the only builtin immutable type that can be recursive
Tim Peters1d63c9f2003-02-02 20:29:39 +00001456 * (a tuple can be reached from itself), and that requires some subtle
1457 * magic so that it works in all cases. IOW, this is a long routine.
1458 */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001459static int
Tim Peterscba30e22003-02-01 06:24:36 +00001460save_tuple(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001461{
Tim Peters1d63c9f2003-02-02 20:29:39 +00001462 PyObject *py_tuple_id = NULL;
1463 int len, i;
1464 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001465
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001466 static char tuple = TUPLE;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001467 static char pop = POP;
1468 static char pop_mark = POP_MARK;
1469 static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
Guido van Rossum60456fd1997-04-09 17:36:32 +00001470
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001471 if ((len = PyTuple_Size(args)) < 0)
1472 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001473
Tim Peters1d63c9f2003-02-02 20:29:39 +00001474 if (len == 0) {
1475 char c_str[2];
Tim Peters84e87f32001-03-17 04:50:51 +00001476
Tim Peters1d63c9f2003-02-02 20:29:39 +00001477 if (self->proto) {
1478 c_str[0] = EMPTY_TUPLE;
1479 len = 1;
1480 }
1481 else {
1482 c_str[0] = MARK;
1483 c_str[1] = TUPLE;
1484 len = 2;
1485 }
1486 if (self->write_func(self, c_str, len) >= 0)
1487 res = 0;
1488 /* Don't memoize an empty tuple. */
1489 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001490 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001491
Tim Peters1d63c9f2003-02-02 20:29:39 +00001492 /* A non-empty tuple. */
1493
1494 /* id(tuple) isn't in the memo now. If it shows up there after
1495 * saving the tuple elements, the tuple must be recursive, in
1496 * which case we'll pop everything we put on the stack, and fetch
1497 * its value from the memo.
1498 */
1499 py_tuple_id = PyLong_FromVoidPtr(args);
1500 if (py_tuple_id == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001501 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001502
Tim Peters1d63c9f2003-02-02 20:29:39 +00001503 if (len <= 3 && self->proto >= 2) {
1504 /* Use TUPLE{1,2,3} opcodes. */
Tim Peters67920142003-02-05 03:46:17 +00001505 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001506 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001507 if (PyDict_GetItem(self->memo, py_tuple_id)) {
Tim Peters1d63c9f2003-02-02 20:29:39 +00001508 /* pop the len elements */
1509 for (i = 0; i < len; ++i)
1510 if (self->write_func(self, &pop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001511 goto finally;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001512 /* fetch from memo */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001513 if (get(self, py_tuple_id) < 0)
1514 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001515 res = 0;
1516 goto finally;
1517 }
Tim Peters1d63c9f2003-02-02 20:29:39 +00001518 /* Not recursive. */
1519 if (self->write_func(self, len2opcode + len, 1) < 0)
1520 goto finally;
1521 goto memoize;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001522 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001523
Tim Peters1d63c9f2003-02-02 20:29:39 +00001524 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1525 * Generate MARK elt1 elt2 ... TUPLE
1526 */
1527 if (self->write_func(self, &MARKv, 1) < 0)
1528 goto finally;
1529
Tim Peters67920142003-02-05 03:46:17 +00001530 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001531 goto finally;
1532
1533 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1534 /* pop the stack stuff we pushed */
1535 if (self->bin) {
1536 if (self->write_func(self, &pop_mark, 1) < 0)
1537 goto finally;
1538 }
1539 else {
1540 /* Note that we pop one more than len, to remove
1541 * the MARK too.
1542 */
1543 for (i = 0; i <= len; i++)
1544 if (self->write_func(self, &pop, 1) < 0)
1545 goto finally;
1546 }
1547 /* fetch from memo */
1548 if (get(self, py_tuple_id) >= 0)
1549 res = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001550 goto finally;
1551 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001552
Tim Peters1d63c9f2003-02-02 20:29:39 +00001553 /* Not recursive. */
1554 if (self->write_func(self, &tuple, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001555 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001556
Tim Peters1d63c9f2003-02-02 20:29:39 +00001557 memoize:
1558 if (put(self, args) >= 0)
1559 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001560
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001561 finally:
1562 Py_XDECREF(py_tuple_id);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001563 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001564}
1565
Tim Peters1092d642003-02-11 21:06:20 +00001566/* iter is an iterator giving items, and we batch up chunks of
1567 * MARK item item ... item APPENDS
1568 * opcode sequences. Calling code should have arranged to first create an
1569 * empty list, or list-like object, for the APPENDS to operate on.
1570 * Returns 0 on success, <0 on error.
1571 */
1572static int
1573batch_list(Picklerobject *self, PyObject *iter)
1574{
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001575 PyObject *obj = NULL;
1576 PyObject *firstitem = NULL;
Tim Peters1092d642003-02-11 21:06:20 +00001577 int i, n;
1578
1579 static char append = APPEND;
1580 static char appends = APPENDS;
1581
1582 assert(iter != NULL);
1583
1584 if (self->proto == 0) {
1585 /* APPENDS isn't available; do one at a time. */
1586 for (;;) {
1587 obj = PyIter_Next(iter);
1588 if (obj == NULL) {
1589 if (PyErr_Occurred())
1590 return -1;
1591 break;
1592 }
1593 i = save(self, obj, 0);
1594 Py_DECREF(obj);
1595 if (i < 0)
1596 return -1;
1597 if (self->write_func(self, &append, 1) < 0)
1598 return -1;
Tim Peters1092d642003-02-11 21:06:20 +00001599 }
1600 return 0;
1601 }
1602
1603 /* proto > 0: write in batches of BATCHSIZE. */
1604 do {
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001605 /* Get first item */
1606 firstitem = PyIter_Next(iter);
1607 if (firstitem == NULL) {
1608 if (PyErr_Occurred())
1609 goto BatchFailed;
1610
1611 /* nothing more to add */
1612 break;
1613 }
1614
1615 /* Try to get a second item */
1616 obj = PyIter_Next(iter);
1617 if (obj == NULL) {
1618 if (PyErr_Occurred())
1619 goto BatchFailed;
1620
1621 /* Only one item to write */
1622 if (save(self, firstitem, 0) < 0)
1623 goto BatchFailed;
1624 if (self->write_func(self, &append, 1) < 0)
1625 goto BatchFailed;
1626 Py_CLEAR(firstitem);
1627 break;
1628 }
1629
1630 /* More than one item to write */
1631
1632 /* Pump out MARK, items, APPENDS. */
1633 if (self->write_func(self, &MARKv, 1) < 0)
1634 goto BatchFailed;
1635
1636 if (save(self, firstitem, 0) < 0)
1637 goto BatchFailed;
1638 Py_CLEAR(firstitem);
1639 n = 1;
1640
1641 /* Fetch and save up to BATCHSIZE items */
1642 while (obj) {
1643 if (save(self, obj, 0) < 0)
1644 goto BatchFailed;
1645 Py_CLEAR(obj);
1646 n += 1;
1647
1648 if (n == BATCHSIZE)
1649 break;
1650
Tim Peters1092d642003-02-11 21:06:20 +00001651 obj = PyIter_Next(iter);
1652 if (obj == NULL) {
1653 if (PyErr_Occurred())
1654 goto BatchFailed;
1655 break;
1656 }
Tim Peters1092d642003-02-11 21:06:20 +00001657 }
1658
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001659 if (self->write_func(self, &appends, 1) < 0)
1660 goto BatchFailed;
Tim Peters1092d642003-02-11 21:06:20 +00001661
Tim Peters90975f12003-02-12 05:28:58 +00001662 } while (n == BATCHSIZE);
Tim Peters1092d642003-02-11 21:06:20 +00001663 return 0;
1664
1665BatchFailed:
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001666 Py_XDECREF(firstitem);
1667 Py_XDECREF(obj);
Tim Peters1092d642003-02-11 21:06:20 +00001668 return -1;
1669}
1670
Guido van Rossum60456fd1997-04-09 17:36:32 +00001671static int
Tim Peterscba30e22003-02-01 06:24:36 +00001672save_list(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001673{
Tim Peters1092d642003-02-11 21:06:20 +00001674 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001675 char s[3];
Tim Peters1092d642003-02-11 21:06:20 +00001676 int len;
1677 PyObject *iter;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001678
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001679 if (self->fast && !fast_save_enter(self, args))
1680 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001681
Tim Peters1092d642003-02-11 21:06:20 +00001682 /* Create an empty list. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001683 if (self->bin) {
1684 s[0] = EMPTY_LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001685 len = 1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001686 }
1687 else {
1688 s[0] = MARK;
1689 s[1] = LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001690 len = 2;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001691 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001692
Tim Peters1092d642003-02-11 21:06:20 +00001693 if (self->write_func(self, s, len) < 0)
1694 goto finally;
1695
1696 /* Get list length, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001697 if ((len = PyList_Size(args)) < 0)
1698 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001699
Tim Peters1092d642003-02-11 21:06:20 +00001700 /* Memoize. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001701 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001702 if (put(self, args) >= 0)
1703 res = 0;
1704 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001705 }
Tim Peters90975f12003-02-12 05:28:58 +00001706 if (put2(self, args) < 0)
1707 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001708
Tim Peters1092d642003-02-11 21:06:20 +00001709 /* Materialize the list elements. */
1710 iter = PyObject_GetIter(args);
1711 if (iter == NULL)
1712 goto finally;
Facundo Batista763d3092008-06-30 01:10:55 +00001713
1714 if (Py_EnterRecursiveCall(" while pickling an object") == 0)
1715 {
1716 res = batch_list(self, iter);
1717 Py_LeaveRecursiveCall();
1718 }
Tim Peters1092d642003-02-11 21:06:20 +00001719 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001720
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001721 finally:
1722 if (self->fast && !fast_save_leave(self, args))
1723 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001724
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001725 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001726}
1727
1728
Tim Peters42f08ac2003-02-11 22:43:24 +00001729/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1730 * MARK key value ... key value SETITEMS
1731 * opcode sequences. Calling code should have arranged to first create an
1732 * empty dict, or dict-like object, for the SETITEMS to operate on.
1733 * Returns 0 on success, <0 on error.
1734 *
1735 * This is very much like batch_list(). The difference between saving
1736 * elements directly, and picking apart two-tuples, is so long-winded at
1737 * the C level, though, that attempts to combine these routines were too
1738 * ugly to bear.
1739 */
1740static int
1741batch_dict(Picklerobject *self, PyObject *iter)
1742{
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001743 PyObject *p = NULL;
1744 PyObject *firstitem = NULL;
Tim Peters42f08ac2003-02-11 22:43:24 +00001745 int i, n;
1746
1747 static char setitem = SETITEM;
1748 static char setitems = SETITEMS;
1749
1750 assert(iter != NULL);
1751
1752 if (self->proto == 0) {
1753 /* SETITEMS isn't available; do one at a time. */
1754 for (;;) {
1755 p = PyIter_Next(iter);
1756 if (p == NULL) {
1757 if (PyErr_Occurred())
1758 return -1;
1759 break;
1760 }
1761 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1762 PyErr_SetString(PyExc_TypeError, "dict items "
1763 "iterator must return 2-tuples");
1764 return -1;
1765 }
1766 i = save(self, PyTuple_GET_ITEM(p, 0), 0);
1767 if (i >= 0)
1768 i = save(self, PyTuple_GET_ITEM(p, 1), 0);
1769 Py_DECREF(p);
1770 if (i < 0)
1771 return -1;
1772 if (self->write_func(self, &setitem, 1) < 0)
1773 return -1;
Tim Peters42f08ac2003-02-11 22:43:24 +00001774 }
1775 return 0;
1776 }
1777
1778 /* proto > 0: write in batches of BATCHSIZE. */
1779 do {
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001780 /* Get first item */
1781 firstitem = PyIter_Next(iter);
1782 if (firstitem == NULL) {
1783 if (PyErr_Occurred())
1784 goto BatchFailed;
1785
1786 /* nothing more to add */
1787 break;
1788 }
1789 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
1790 PyErr_SetString(PyExc_TypeError, "dict items "
1791 "iterator must return 2-tuples");
1792 goto BatchFailed;
1793 }
1794
1795 /* Try to get a second item */
1796 p = PyIter_Next(iter);
1797 if (p == NULL) {
1798 if (PyErr_Occurred())
1799 goto BatchFailed;
1800
1801 /* Only one item to write */
1802 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
1803 goto BatchFailed;
1804 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
1805 goto BatchFailed;
1806 if (self->write_func(self, &setitem, 1) < 0)
1807 goto BatchFailed;
1808 Py_CLEAR(firstitem);
1809 break;
1810 }
1811
1812 /* More than one item to write */
1813
1814 /* Pump out MARK, items, SETITEMS. */
1815 if (self->write_func(self, &MARKv, 1) < 0)
1816 goto BatchFailed;
1817
1818 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
1819 goto BatchFailed;
1820 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
1821 goto BatchFailed;
1822 Py_CLEAR(firstitem);
1823 n = 1;
1824
1825 /* Fetch and save up to BATCHSIZE items */
1826 while (p) {
1827 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1828 PyErr_SetString(PyExc_TypeError, "dict items "
1829 "iterator must return 2-tuples");
1830 goto BatchFailed;
1831 }
1832 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1833 goto BatchFailed;
1834 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1835 goto BatchFailed;
1836 Py_CLEAR(p);
1837 n += 1;
1838
1839 if (n == BATCHSIZE)
1840 break;
1841
Tim Peters42f08ac2003-02-11 22:43:24 +00001842 p = PyIter_Next(iter);
1843 if (p == NULL) {
1844 if (PyErr_Occurred())
1845 goto BatchFailed;
1846 break;
1847 }
Tim Peters42f08ac2003-02-11 22:43:24 +00001848 }
1849
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001850 if (self->write_func(self, &setitems, 1) < 0)
1851 goto BatchFailed;
Tim Peters42f08ac2003-02-11 22:43:24 +00001852
Tim Peters90975f12003-02-12 05:28:58 +00001853 } while (n == BATCHSIZE);
Tim Peters42f08ac2003-02-11 22:43:24 +00001854 return 0;
1855
1856BatchFailed:
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001857 Py_XDECREF(firstitem);
1858 Py_XDECREF(p);
Tim Peters42f08ac2003-02-11 22:43:24 +00001859 return -1;
1860}
1861
Guido van Rossum60456fd1997-04-09 17:36:32 +00001862static int
Tim Peterscba30e22003-02-01 06:24:36 +00001863save_dict(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001864{
Tim Peters42f08ac2003-02-11 22:43:24 +00001865 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001866 char s[3];
Tim Peters42f08ac2003-02-11 22:43:24 +00001867 int len;
1868 PyObject *iter;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001869
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001870 if (self->fast && !fast_save_enter(self, args))
1871 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001872
Tim Peters42f08ac2003-02-11 22:43:24 +00001873 /* Create an empty dict. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001874 if (self->bin) {
1875 s[0] = EMPTY_DICT;
1876 len = 1;
1877 }
1878 else {
1879 s[0] = MARK;
1880 s[1] = DICT;
1881 len = 2;
1882 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001883
Tim Peters0bc93f52003-02-02 18:29:33 +00001884 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001885 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001886
Tim Peters42f08ac2003-02-11 22:43:24 +00001887 /* Get dict size, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001888 if ((len = PyDict_Size(args)) < 0)
1889 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001890
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001891 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001892 if (put(self, args) >= 0)
1893 res = 0;
1894 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001895 }
Tim Peters90975f12003-02-12 05:28:58 +00001896 if (put2(self, args) < 0)
1897 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001898
Tim Peters42f08ac2003-02-11 22:43:24 +00001899 /* Materialize the dict items. */
1900 iter = PyObject_CallMethod(args, "iteritems", "()");
1901 if (iter == NULL)
1902 goto finally;
Facundo Batista763d3092008-06-30 01:10:55 +00001903 if (Py_EnterRecursiveCall(" while pickling an object") == 0)
1904 {
1905 res = batch_dict(self, iter);
1906 Py_LeaveRecursiveCall();
1907 }
Tim Peters42f08ac2003-02-11 22:43:24 +00001908 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001909
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001910 finally:
1911 if (self->fast && !fast_save_leave(self, args))
1912 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001913
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001914 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001915}
1916
1917
Tim Peters84e87f32001-03-17 04:50:51 +00001918static int
Tim Peterscba30e22003-02-01 06:24:36 +00001919save_inst(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001920{
1921 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1922 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1923 char *module_str, *name_str;
1924 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001925
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001926 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001927
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001928 if (self->fast && !fast_save_enter(self, args))
1929 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001930
Tim Peters0bc93f52003-02-02 18:29:33 +00001931 if (self->write_func(self, &MARKv, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001932 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001933
Tim Peterscba30e22003-02-01 06:24:36 +00001934 if (!( class = PyObject_GetAttr(args, __class___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001935 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001936
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001937 if (self->bin) {
1938 if (save(self, class, 0) < 0)
1939 goto finally;
1940 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001941
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001942 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
1943 PyObject *element = 0;
1944 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001945
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001946 if (!( class_args =
Tim Peterscba30e22003-02-01 06:24:36 +00001947 PyObject_Call(getinitargs_func, empty_tuple, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001948 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001949
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001950 if ((len = PyObject_Size(class_args)) < 0)
1951 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001952
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001953 for (i = 0; i < len; i++) {
Tim Peterscba30e22003-02-01 06:24:36 +00001954 if (!( element = PySequence_GetItem(class_args, i)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001955 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001956
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001957 if (save(self, element, 0) < 0) {
1958 Py_DECREF(element);
1959 goto finally;
1960 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001961
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001962 Py_DECREF(element);
1963 }
1964 }
1965 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00001966 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1967 PyErr_Clear();
1968 else
1969 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001970 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001971
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001972 if (!self->bin) {
1973 if (!( name = ((PyClassObject *)class)->cl_name )) {
1974 PyErr_SetString(PicklingError, "class has no name");
1975 goto finally;
1976 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001977
Tim Peterscba30e22003-02-01 06:24:36 +00001978 if (!( module = whichmodule(class, name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001979 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001980
Tim Peters84e87f32001-03-17 04:50:51 +00001981
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001982 if ((module_size = PyString_Size(module)) < 0 ||
1983 (name_size = PyString_Size(name)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001984 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001985
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001986 module_str = PyString_AS_STRING((PyStringObject *)module);
1987 name_str = PyString_AS_STRING((PyStringObject *)name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001988
Tim Peters0bc93f52003-02-02 18:29:33 +00001989 if (self->write_func(self, &inst, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001990 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001991
Tim Peters0bc93f52003-02-02 18:29:33 +00001992 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001993 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001994
Tim Peters0bc93f52003-02-02 18:29:33 +00001995 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001996 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001997
Tim Peters0bc93f52003-02-02 18:29:33 +00001998 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001999 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +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;
2003 }
Tim Peters0bc93f52003-02-02 18:29:33 +00002004 else if (self->write_func(self, &obj, 1) < 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002005 goto finally;
2006 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002007
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002008 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
2009 state = PyObject_Call(getstate_func, empty_tuple, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00002010 if (!state)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002011 goto finally;
2012 }
2013 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002014 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2015 PyErr_Clear();
2016 else
2017 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002018
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002019 if (!( state = PyObject_GetAttr(args, __dict___str))) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002020 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2021 PyErr_Clear();
2022 else
2023 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002024 res = 0;
2025 goto finally;
2026 }
2027 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002028
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002029 if (!PyDict_Check(state)) {
2030 if (put2(self, args) < 0)
2031 goto finally;
2032 }
2033 else {
2034 if (put(self, args) < 0)
2035 goto finally;
2036 }
Tim Peters84e87f32001-03-17 04:50:51 +00002037
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002038 if (save(self, state, 0) < 0)
2039 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002040
Tim Peters0bc93f52003-02-02 18:29:33 +00002041 if (self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002042 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002043
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002044 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002045
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002046 finally:
2047 if (self->fast && !fast_save_leave(self, args))
2048 res = -1;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002049
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002050 Py_XDECREF(module);
2051 Py_XDECREF(class);
2052 Py_XDECREF(state);
2053 Py_XDECREF(getinitargs_func);
2054 Py_XDECREF(getstate_func);
2055 Py_XDECREF(class_args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002056
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002057 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002058}
2059
2060
Guido van Rossum60456fd1997-04-09 17:36:32 +00002061static int
Tim Peterscba30e22003-02-01 06:24:36 +00002062save_global(Picklerobject *self, PyObject *args, PyObject *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002063{
Jeremy Hylton9ee91f12002-07-11 22:02:33 +00002064 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002065 char *name_str, *module_str;
2066 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002067
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002068 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002069
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002070 if (name) {
2071 global_name = name;
2072 Py_INCREF(global_name);
2073 }
2074 else {
Tim Peterscba30e22003-02-01 06:24:36 +00002075 if (!( global_name = PyObject_GetAttr(args, __name___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002076 goto finally;
2077 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002078
Tim Peterscba30e22003-02-01 06:24:36 +00002079 if (!( module = whichmodule(args, global_name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002080 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002081
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002082 if ((module_size = PyString_Size(module)) < 0 ||
2083 (name_size = PyString_Size(global_name)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002084 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002085
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002086 module_str = PyString_AS_STRING((PyStringObject *)module);
2087 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002088
Guido van Rossum75bfd052002-12-24 18:10:07 +00002089 /* XXX This can be doing a relative import. Clearly it shouldn't,
2090 but I don't know how to stop it. :-( */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002091 mod = PyImport_ImportModule(module_str);
2092 if (mod == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002093 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00002094 "Can't pickle %s: import of module %s "
2095 "failed",
2096 "OS", args, module);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002097 goto finally;
2098 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00002099 klass = PyObject_GetAttrString(mod, name_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002100 if (klass == NULL) {
2101 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00002102 "Can't pickle %s: attribute lookup %s.%s "
2103 "failed",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002104 "OSS", args, module, global_name);
2105 goto finally;
2106 }
2107 if (klass != args) {
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00002108 Py_DECREF(klass);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002109 cPickle_ErrFormat(PicklingError,
Tim Peters731098b2003-02-04 20:56:09 +00002110 "Can't pickle %s: it's not the same object "
2111 "as %s.%s",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002112 "OSS", args, module, global_name);
2113 goto finally;
2114 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00002115 Py_DECREF(klass);
Guido van Rossuma92d16a2001-08-18 21:22:07 +00002116
Tim Peters731098b2003-02-04 20:56:09 +00002117 if (self->proto >= 2) {
2118 /* See whether this is in the extension registry, and if
2119 * so generate an EXT opcode.
2120 */
2121 PyObject *py_code; /* extension code as Python object */
Tim Peters3e667d52003-02-04 21:47:44 +00002122 long code; /* extension code as C value */
Tim Peters731098b2003-02-04 20:56:09 +00002123 char c_str[5];
2124 int n;
2125
2126 PyTuple_SET_ITEM(two_tuple, 0, module);
2127 PyTuple_SET_ITEM(two_tuple, 1, global_name);
2128 py_code = PyDict_GetItem(extension_registry, two_tuple);
2129 if (py_code == NULL)
2130 goto gen_global; /* not registered */
2131
2132 /* Verify py_code has the right type and value. */
2133 if (!PyInt_Check(py_code)) {
2134 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
Tim Peters6288e232003-02-05 03:53:10 +00002135 "extension code %s isn't an integer",
Tim Peters731098b2003-02-04 20:56:09 +00002136 "OO", args, py_code);
2137 goto finally;
2138 }
2139 code = PyInt_AS_LONG(py_code);
2140 if (code <= 0 || code > 0x7fffffffL) {
2141 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2142 "extension code %ld is out of range",
2143 "Ol", args, code);
2144 goto finally;
2145 }
2146
2147 /* Generate an EXT opcode. */
2148 if (code <= 0xff) {
2149 c_str[0] = EXT1;
2150 c_str[1] = (char)code;
2151 n = 2;
2152 }
2153 else if (code <= 0xffff) {
2154 c_str[0] = EXT2;
2155 c_str[1] = (char)(code & 0xff);
2156 c_str[2] = (char)((code >> 8) & 0xff);
2157 n = 3;
2158 }
2159 else {
2160 c_str[0] = EXT4;
2161 c_str[1] = (char)(code & 0xff);
2162 c_str[2] = (char)((code >> 8) & 0xff);
2163 c_str[3] = (char)((code >> 16) & 0xff);
2164 c_str[4] = (char)((code >> 24) & 0xff);
2165 n = 5;
2166 }
2167
2168 if (self->write_func(self, c_str, n) >= 0)
2169 res = 0;
2170 goto finally; /* and don't memoize */
2171 }
2172
2173 gen_global:
Tim Peters0bc93f52003-02-02 18:29:33 +00002174 if (self->write_func(self, &global, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002175 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002176
Tim Peters0bc93f52003-02-02 18:29:33 +00002177 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002178 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002179
Tim Peters0bc93f52003-02-02 18:29:33 +00002180 if (self->write_func(self, "\n", 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, name_str, name_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
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002189 if (put(self, args) < 0)
2190 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002191
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002192 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002193
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002194 finally:
2195 Py_XDECREF(module);
2196 Py_XDECREF(global_name);
2197 Py_XDECREF(mod);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002198
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002199 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002200}
2201
Guido van Rossum60456fd1997-04-09 17:36:32 +00002202static int
Tim Peterscba30e22003-02-01 06:24:36 +00002203save_pers(Picklerobject *self, PyObject *args, PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002204{
2205 PyObject *pid = 0;
2206 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002207
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002208 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002209
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002210 Py_INCREF(args);
2211 ARG_TUP(self, args);
2212 if (self->arg) {
2213 pid = PyObject_Call(f, self->arg, NULL);
2214 FREE_ARG_TUP(self);
2215 }
2216 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002217
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002218 if (pid != Py_None) {
2219 if (!self->bin) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002220 if (!PyString_Check(pid)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002221 PyErr_SetString(PicklingError,
2222 "persistent id must be string");
2223 goto finally;
2224 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002225
Tim Peters0bc93f52003-02-02 18:29:33 +00002226 if (self->write_func(self, &persid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002227 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002228
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002229 if ((size = PyString_Size(pid)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002230 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002231
Tim Peters0bc93f52003-02-02 18:29:33 +00002232 if (self->write_func(self,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002233 PyString_AS_STRING(
2234 (PyStringObject *)pid),
Tim Peters0bc93f52003-02-02 18:29:33 +00002235 size) < 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, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002239 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002240
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002241 res = 1;
2242 goto finally;
2243 }
2244 else if (save(self, pid, 1) >= 0) {
Tim Peters0bc93f52003-02-02 18:29:33 +00002245 if (self->write_func(self, &binpersid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002246 res = -1;
2247 else
2248 res = 1;
2249 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002250
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002251 goto finally;
2252 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002253
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002254 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002255
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002256 finally:
2257 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002258
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002259 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002260}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002261
Tim Peters71fcda52003-02-14 23:05:28 +00002262/* We're saving ob, and args is the 2-thru-5 tuple returned by the
2263 * appropriate __reduce__ method for ob.
2264 */
Tim Peters84e87f32001-03-17 04:50:51 +00002265static int
Amaury Forgeot d'Arcc353ea72008-10-30 21:29:12 +00002266save_reduce(Picklerobject *self, PyObject *args, PyObject *fn, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002267{
Tim Peters71fcda52003-02-14 23:05:28 +00002268 PyObject *callable;
2269 PyObject *argtup;
Amaury Forgeot d'Arcc353ea72008-10-30 21:29:12 +00002270 PyObject *state = NULL;
2271 PyObject *listitems = Py_None;
2272 PyObject *dictitems = Py_None;
2273 Py_ssize_t size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002274
Tim Peters71fcda52003-02-14 23:05:28 +00002275 int use_newobj = self->proto >= 2;
2276
2277 static char reduce = REDUCE;
2278 static char build = BUILD;
2279 static char newobj = NEWOBJ;
2280
Amaury Forgeot d'Arcc353ea72008-10-30 21:29:12 +00002281 size = PyTuple_Size(args);
2282 if (size < 2 || size > 5) {
2283 cPickle_ErrFormat(PicklingError, "tuple returned by "
2284 "%s must contain 2 through 5 elements",
2285 "O", fn);
2286 return -1;
2287 }
2288
Tim Peters71fcda52003-02-14 23:05:28 +00002289 if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2290 &callable,
2291 &argtup,
2292 &state,
2293 &listitems,
2294 &dictitems))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002295 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002296
Raymond Hettingera6b45cc2004-12-07 07:05:57 +00002297 if (!PyTuple_Check(argtup)) {
Amaury Forgeot d'Arcc353ea72008-10-30 21:29:12 +00002298 cPickle_ErrFormat(PicklingError, "Second element of "
2299 "tuple returned by %s must be a tuple",
2300 "O", fn);
Raymond Hettingera6b45cc2004-12-07 07:05:57 +00002301 return -1;
2302 }
2303
Tim Peters71fcda52003-02-14 23:05:28 +00002304 if (state == Py_None)
2305 state = NULL;
Amaury Forgeot d'Arcc353ea72008-10-30 21:29:12 +00002306
Tim Peters71fcda52003-02-14 23:05:28 +00002307 if (listitems == Py_None)
2308 listitems = NULL;
Amaury Forgeot d'Arcc353ea72008-10-30 21:29:12 +00002309 else if (!PyIter_Check(listitems)) {
2310 cPickle_ErrFormat(PicklingError, "Fourth element of "
2311 "tuple returned by %s must be an iterator, not %s",
2312 "Os", fn, Py_TYPE(listitems)->tp_name);
2313 return -1;
2314 }
2315
Tim Peters71fcda52003-02-14 23:05:28 +00002316 if (dictitems == Py_None)
2317 dictitems = NULL;
Amaury Forgeot d'Arcc353ea72008-10-30 21:29:12 +00002318 else if (!PyIter_Check(dictitems)) {
2319 cPickle_ErrFormat(PicklingError, "Fifth element of "
2320 "tuple returned by %s must be an iterator, not %s",
2321 "Os", fn, Py_TYPE(dictitems)->tp_name);
2322 return -1;
2323 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002324
Tim Peters71fcda52003-02-14 23:05:28 +00002325 /* Protocol 2 special case: if callable's name is __newobj__, use
2326 * NEWOBJ. This consumes a lot of code.
2327 */
2328 if (use_newobj) {
2329 PyObject *temp = PyObject_GetAttr(callable, __name___str);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002330
Tim Peters71fcda52003-02-14 23:05:28 +00002331 if (temp == NULL) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002332 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2333 PyErr_Clear();
2334 else
2335 return -1;
Tim Peters71fcda52003-02-14 23:05:28 +00002336 use_newobj = 0;
2337 }
2338 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002339 use_newobj = PyString_Check(temp) &&
2340 strcmp(PyString_AS_STRING(temp),
Tim Peters71fcda52003-02-14 23:05:28 +00002341 "__newobj__") == 0;
2342 Py_DECREF(temp);
2343 }
2344 }
2345 if (use_newobj) {
2346 PyObject *cls;
2347 PyObject *newargtup;
2348 int n, i;
2349
2350 /* Sanity checks. */
2351 n = PyTuple_Size(argtup);
2352 if (n < 1) {
2353 PyErr_SetString(PicklingError, "__newobj__ arglist "
2354 "is empty");
2355 return -1;
2356 }
2357
2358 cls = PyTuple_GET_ITEM(argtup, 0);
2359 if (! PyObject_HasAttrString(cls, "__new__")) {
2360 PyErr_SetString(PicklingError, "args[0] from "
2361 "__newobj__ args has no __new__");
2362 return -1;
2363 }
2364
2365 /* XXX How could ob be NULL? */
2366 if (ob != NULL) {
2367 PyObject *ob_dot_class;
2368
2369 ob_dot_class = PyObject_GetAttr(ob, __class___str);
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002370 if (ob_dot_class == NULL) {
2371 if (PyErr_ExceptionMatches(
2372 PyExc_AttributeError))
2373 PyErr_Clear();
2374 else
2375 return -1;
2376 }
Tim Peters71fcda52003-02-14 23:05:28 +00002377 i = ob_dot_class != cls; /* true iff a problem */
2378 Py_XDECREF(ob_dot_class);
2379 if (i) {
2380 PyErr_SetString(PicklingError, "args[0] from "
2381 "__newobj__ args has the wrong class");
2382 return -1;
2383 }
2384 }
2385
2386 /* Save the class and its __new__ arguments. */
2387 if (save(self, cls, 0) < 0)
2388 return -1;
2389
2390 newargtup = PyTuple_New(n-1); /* argtup[1:] */
2391 if (newargtup == NULL)
2392 return -1;
2393 for (i = 1; i < n; ++i) {
2394 PyObject *temp = PyTuple_GET_ITEM(argtup, i);
2395 Py_INCREF(temp);
2396 PyTuple_SET_ITEM(newargtup, i-1, temp);
2397 }
Neal Norwitz5a29dd32007-10-05 05:01:38 +00002398 i = save(self, newargtup, 0);
Tim Peters71fcda52003-02-14 23:05:28 +00002399 Py_DECREF(newargtup);
2400 if (i < 0)
2401 return -1;
2402
2403 /* Add NEWOBJ opcode. */
2404 if (self->write_func(self, &newobj, 1) < 0)
2405 return -1;
2406 }
2407 else {
2408 /* Not using NEWOBJ. */
2409 if (save(self, callable, 0) < 0 ||
2410 save(self, argtup, 0) < 0 ||
2411 self->write_func(self, &reduce, 1) < 0)
2412 return -1;
2413 }
2414
2415 /* Memoize. */
2416 /* XXX How can ob be NULL? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002417 if (ob != NULL) {
2418 if (state && !PyDict_Check(state)) {
2419 if (put2(self, ob) < 0)
2420 return -1;
2421 }
Tim Peters71fcda52003-02-14 23:05:28 +00002422 else if (put(self, ob) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002423 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002424 }
Tim Peters84e87f32001-03-17 04:50:51 +00002425
Guido van Rossum60456fd1997-04-09 17:36:32 +00002426
Tim Peters71fcda52003-02-14 23:05:28 +00002427 if (listitems && batch_list(self, listitems) < 0)
2428 return -1;
2429
2430 if (dictitems && batch_dict(self, dictitems) < 0)
2431 return -1;
2432
2433 if (state) {
2434 if (save(self, state, 0) < 0 ||
2435 self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002436 return -1;
2437 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002438
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002439 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002440}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002441
Guido van Rossum60456fd1997-04-09 17:36:32 +00002442static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00002443save(Picklerobject *self, PyObject *args, int pers_save)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002444{
2445 PyTypeObject *type;
Tim Peters71fcda52003-02-14 23:05:28 +00002446 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
Tim Peters71fcda52003-02-14 23:05:28 +00002447 int res = -1;
Amaury Forgeot d'Arcc353ea72008-10-30 21:29:12 +00002448 int tmp;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002449
Facundo Batista763d3092008-06-30 01:10:55 +00002450 if (Py_EnterRecursiveCall(" while pickling an object"))
2451 return -1;
Martin v. Löwis5a395302002-08-04 08:20:23 +00002452
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002453 if (!pers_save && self->pers_func) {
2454 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2455 res = tmp;
2456 goto finally;
2457 }
2458 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002459
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002460 if (args == Py_None) {
2461 res = save_none(self, args);
2462 goto finally;
2463 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002464
Christian Heimese93237d2007-12-19 02:37:44 +00002465 type = Py_TYPE(args);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002466
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002467 switch (type->tp_name[0]) {
Guido van Rossum77f6a652002-04-03 22:41:51 +00002468 case 'b':
2469 if (args == Py_False || args == Py_True) {
2470 res = save_bool(self, args);
2471 goto finally;
2472 }
2473 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002474 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002475 if (type == &PyInt_Type) {
2476 res = save_int(self, args);
2477 goto finally;
2478 }
2479 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002480
Guido van Rossum60456fd1997-04-09 17:36:32 +00002481 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002482 if (type == &PyLong_Type) {
2483 res = save_long(self, args);
2484 goto finally;
2485 }
2486 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002487
Guido van Rossum60456fd1997-04-09 17:36:32 +00002488 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002489 if (type == &PyFloat_Type) {
2490 res = save_float(self, args);
2491 goto finally;
2492 }
2493 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002494
Guido van Rossum60456fd1997-04-09 17:36:32 +00002495 case 't':
Tim Peters1d63c9f2003-02-02 20:29:39 +00002496 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2497 res = save_tuple(self, args);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002498 goto finally;
2499 }
2500 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002501
Guido van Rossum60456fd1997-04-09 17:36:32 +00002502 case 's':
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002503 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002504 res = save_string(self, args, 0);
2505 goto finally;
2506 }
Facundo Batista14618862008-06-22 15:27:10 +00002507 break;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002508
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002509#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002510 case 'u':
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002511 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002512 res = save_unicode(self, args, 0);
2513 goto finally;
2514 }
Facundo Batista14618862008-06-22 15:27:10 +00002515 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002516#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002517 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002518
Christian Heimese93237d2007-12-19 02:37:44 +00002519 if (Py_REFCNT(args) > 1) {
Tim Peterscba30e22003-02-01 06:24:36 +00002520 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002521 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002522
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002523 if (PyDict_GetItem(self->memo, py_ob_id)) {
2524 if (get(self, py_ob_id) < 0)
2525 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002526
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002527 res = 0;
2528 goto finally;
2529 }
2530 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002531
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002532 switch (type->tp_name[0]) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002533 case 's':
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002534 if (type == &PyString_Type) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002535 res = save_string(self, args, 1);
2536 goto finally;
2537 }
2538 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002539
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002540#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002541 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002542 if (type == &PyUnicode_Type) {
2543 res = save_unicode(self, args, 1);
2544 goto finally;
2545 }
2546 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002547#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002548
Guido van Rossum60456fd1997-04-09 17:36:32 +00002549 case 't':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002550 if (type == &PyTuple_Type) {
2551 res = save_tuple(self, args);
2552 goto finally;
2553 }
2554 if (type == &PyType_Type) {
2555 res = save_global(self, args, NULL);
2556 goto finally;
2557 }
2558 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002559
Guido van Rossum60456fd1997-04-09 17:36:32 +00002560 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002561 if (type == &PyList_Type) {
2562 res = save_list(self, args);
2563 goto finally;
2564 }
2565 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002566
2567 case 'd':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002568 if (type == &PyDict_Type) {
2569 res = save_dict(self, args);
2570 goto finally;
2571 }
2572 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002573
2574 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002575 if (type == &PyInstance_Type) {
2576 res = save_inst(self, args);
2577 goto finally;
2578 }
2579 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002580
2581 case 'c':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002582 if (type == &PyClass_Type) {
2583 res = save_global(self, args, NULL);
2584 goto finally;
2585 }
2586 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002587
2588 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002589 if (type == &PyFunction_Type) {
2590 res = save_global(self, args, NULL);
Christian Tismer2460c622004-02-26 16:21:45 +00002591 if (res && PyErr_ExceptionMatches(PickleError)) {
2592 /* fall back to reduce */
2593 PyErr_Clear();
2594 break;
2595 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002596 goto finally;
2597 }
2598 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002599
2600 case 'b':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002601 if (type == &PyCFunction_Type) {
2602 res = save_global(self, args, NULL);
2603 goto finally;
2604 }
2605 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002606
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002607 if (!pers_save && self->inst_pers_func) {
2608 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2609 res = tmp;
2610 goto finally;
2611 }
2612 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002613
Jeremy Hylton39c61162002-07-16 19:47:43 +00002614 if (PyType_IsSubtype(type, &PyType_Type)) {
2615 res = save_global(self, args, NULL);
2616 goto finally;
2617 }
2618
Guido van Rossumb289b872003-02-19 01:45:13 +00002619 /* Get a reduction callable, and call it. This may come from
Georg Brandldffbf5f2008-05-20 07:49:57 +00002620 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
Guido van Rossumb289b872003-02-19 01:45:13 +00002621 * or the object's __reduce__ method.
Tim Peters71fcda52003-02-14 23:05:28 +00002622 */
Tim Peters5aa3da62003-02-13 21:03:57 +00002623 __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2624 if (__reduce__ != NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002625 Py_INCREF(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00002626 Py_INCREF(args);
2627 ARG_TUP(self, args);
2628 if (self->arg) {
2629 t = PyObject_Call(__reduce__, self->arg, NULL);
2630 FREE_ARG_TUP(self);
2631 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002632 }
2633 else {
Guido van Rossumb289b872003-02-19 01:45:13 +00002634 /* Check for a __reduce_ex__ method. */
2635 __reduce__ = PyObject_GetAttr(args, __reduce_ex___str);
2636 if (__reduce__ != NULL) {
2637 t = PyInt_FromLong(self->proto);
2638 if (t != NULL) {
2639 ARG_TUP(self, t);
2640 t = NULL;
2641 if (self->arg) {
2642 t = PyObject_Call(__reduce__,
2643 self->arg, NULL);
2644 FREE_ARG_TUP(self);
2645 }
2646 }
2647 }
2648 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002649 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2650 PyErr_Clear();
2651 else
2652 goto finally;
Guido van Rossumb289b872003-02-19 01:45:13 +00002653 /* Check for a __reduce__ method. */
2654 __reduce__ = PyObject_GetAttr(args, __reduce___str);
2655 if (__reduce__ != NULL) {
2656 t = PyObject_Call(__reduce__,
2657 empty_tuple, NULL);
2658 }
2659 else {
2660 PyErr_SetObject(UnpickleableError, args);
2661 goto finally;
2662 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002663 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002664 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002665
Tim Peters71fcda52003-02-14 23:05:28 +00002666 if (t == NULL)
2667 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002668
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002669 if (PyString_Check(t)) {
Tim Peters71fcda52003-02-14 23:05:28 +00002670 res = save_global(self, args, t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002671 goto finally;
2672 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002673
Amaury Forgeot d'Arcc353ea72008-10-30 21:29:12 +00002674 if (!PyTuple_Check(t)) {
Tim Peters71fcda52003-02-14 23:05:28 +00002675 cPickle_ErrFormat(PicklingError, "Value returned by "
2676 "%s must be string or tuple",
2677 "O", __reduce__);
2678 goto finally;
2679 }
2680
Amaury Forgeot d'Arcc353ea72008-10-30 21:29:12 +00002681 res = save_reduce(self, t, __reduce__, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002682
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002683 finally:
Facundo Batista763d3092008-06-30 01:10:55 +00002684 Py_LeaveRecursiveCall();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002685 Py_XDECREF(py_ob_id);
2686 Py_XDECREF(__reduce__);
2687 Py_XDECREF(t);
Tim Peters84e87f32001-03-17 04:50:51 +00002688
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002689 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002690}
2691
2692
2693static int
Tim Peterscba30e22003-02-01 06:24:36 +00002694dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002695{
2696 static char stop = STOP;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002697
Tim Peters4190fb82003-02-02 16:09:05 +00002698 if (self->proto >= 2) {
2699 char bytes[2];
2700
2701 bytes[0] = PROTO;
Tim Peters87482ea2003-02-02 16:16:30 +00002702 assert(self->proto >= 0 && self->proto < 256);
2703 bytes[1] = (char)self->proto;
Tim Peters4190fb82003-02-02 16:09:05 +00002704 if (self->write_func(self, bytes, 2) < 0)
2705 return -1;
2706 }
2707
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002708 if (save(self, args, 0) < 0)
2709 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002710
Tim Peters4190fb82003-02-02 16:09:05 +00002711 if (self->write_func(self, &stop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002712 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002713
Tim Peters4190fb82003-02-02 16:09:05 +00002714 if (self->write_func(self, NULL, 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002715 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002716
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002717 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002718}
2719
2720static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002721Pickle_clear_memo(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002722{
Tim Peterscba30e22003-02-01 06:24:36 +00002723 if (self->memo)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002724 PyDict_Clear(self->memo);
2725 Py_INCREF(Py_None);
2726 return Py_None;
2727}
2728
2729static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002730Pickle_getvalue(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002731{
2732 int l, i, rsize, ssize, clear=1, lm;
2733 long ik;
2734 PyObject *k, *r;
2735 char *s, *p, *have_get;
2736 Pdata *data;
2737
2738 /* Can be called by Python code or C code */
Tim Peterscba30e22003-02-01 06:24:36 +00002739 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002740 return NULL;
2741
2742 /* Check to make sure we are based on a list */
2743 if (! Pdata_Check(self->file)) {
2744 PyErr_SetString(PicklingError,
2745 "Attempt to getvalue() a non-list-based pickler");
2746 return NULL;
2747 }
2748
2749 /* flush write buffer */
2750 if (write_other(self, NULL, 0) < 0) return NULL;
2751
2752 data=(Pdata*)self->file;
2753 l=data->length;
2754
2755 /* set up an array to hold get/put status */
Tim Petersac5687a2003-02-02 18:08:34 +00002756 lm = PyDict_Size(self->memo);
2757 if (lm < 0) return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002758 lm++;
Tim Petersac5687a2003-02-02 18:08:34 +00002759 have_get = malloc(lm);
2760 if (have_get == NULL) return PyErr_NoMemory();
2761 memset(have_get, 0, lm);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002762
2763 /* Scan for gets. */
Tim Petersac5687a2003-02-02 18:08:34 +00002764 for (rsize = 0, i = l; --i >= 0; ) {
2765 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002766
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002767 if (PyString_Check(k))
2768 rsize += PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002769
2770 else if (PyInt_Check(k)) { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002771 ik = PyInt_AS_LONG((PyIntObject*)k);
2772 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002773 PyErr_SetString(PicklingError,
2774 "Invalid get data");
Neal Norwitz98a96002006-07-23 07:57:11 +00002775 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002776 }
Tim Petersac5687a2003-02-02 18:08:34 +00002777 if (have_get[ik]) /* with matching get */
2778 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002779 }
2780
2781 else if (! (PyTuple_Check(k) &&
2782 PyTuple_GET_SIZE(k) == 2 &&
Tim Petersac5687a2003-02-02 18:08:34 +00002783 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002784 ) {
2785 PyErr_SetString(PicklingError,
2786 "Unexpected data in internal list");
Neal Norwitz98a96002006-07-23 07:57:11 +00002787 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002788 }
2789
2790 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002791 ik = PyInt_AS_LONG((PyIntObject *)k);
2792 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002793 PyErr_SetString(PicklingError,
2794 "Invalid get data");
2795 return NULL;
2796 }
Tim Petersac5687a2003-02-02 18:08:34 +00002797 have_get[ik] = 1;
2798 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002799 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002800 }
2801
2802 /* Now generate the result */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002803 r = PyString_FromStringAndSize(NULL, rsize);
Tim Petersac5687a2003-02-02 18:08:34 +00002804 if (r == NULL) goto err;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002805 s = PyString_AS_STRING((PyStringObject *)r);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002806
Tim Petersac5687a2003-02-02 18:08:34 +00002807 for (i = 0; i < l; i++) {
2808 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002809
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002810 if (PyString_Check(k)) {
2811 ssize = PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002812 if (ssize) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002813 p=PyString_AS_STRING((PyStringObject *)k);
Tim Petersac5687a2003-02-02 18:08:34 +00002814 while (--ssize >= 0)
2815 *s++ = *p++;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002816 }
2817 }
2818
2819 else if (PyTuple_Check(k)) { /* get */
Tim Petersac5687a2003-02-02 18:08:34 +00002820 ik = PyInt_AS_LONG((PyIntObject *)
2821 PyTuple_GET_ITEM(k, 0));
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002822 if (ik < 256) {
2823 *s++ = BINGET;
2824 *s++ = (int)(ik & 0xff);
2825 }
2826 else {
2827 *s++ = LONG_BINGET;
2828 *s++ = (int)(ik & 0xff);
2829 *s++ = (int)((ik >> 8) & 0xff);
2830 *s++ = (int)((ik >> 16) & 0xff);
2831 *s++ = (int)((ik >> 24) & 0xff);
2832 }
2833 }
2834
2835 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002836 ik = PyInt_AS_LONG((PyIntObject*)k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002837
2838 if (have_get[ik]) { /* with matching get */
2839 if (ik < 256) {
2840 *s++ = BINPUT;
2841 *s++ = (int)(ik & 0xff);
2842 }
2843 else {
2844 *s++ = LONG_BINPUT;
2845 *s++ = (int)(ik & 0xff);
2846 *s++ = (int)((ik >> 8) & 0xff);
2847 *s++ = (int)((ik >> 16) & 0xff);
2848 *s++ = (int)((ik >> 24) & 0xff);
2849 }
2850 }
2851 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002852 }
2853
2854 if (clear) {
2855 PyDict_Clear(self->memo);
Tim Petersac5687a2003-02-02 18:08:34 +00002856 Pdata_clear(data, 0);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002857 }
2858
2859 free(have_get);
2860 return r;
2861 err:
2862 free(have_get);
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002863 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002864}
2865
2866static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002867Pickler_dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002868{
2869 PyObject *ob;
2870 int get=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002871
Tim Peterscba30e22003-02-01 06:24:36 +00002872 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002873 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002874
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002875 if (dump(self, ob) < 0)
2876 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002877
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002878 if (get) return Pickle_getvalue(self, NULL);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002879
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002880 /* XXX Why does dump() return self? */
2881 Py_INCREF(self);
2882 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002883}
2884
2885
Tim Peterscba30e22003-02-01 06:24:36 +00002886static struct PyMethodDef Pickler_methods[] =
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002887{
Neal Norwitzb0493252002-03-31 14:44:22 +00002888 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002889 PyDoc_STR("dump(object) -- "
2890 "Write an object in pickle format to the object's pickle stream")},
Fred Drake0ebacc82002-05-01 20:36:39 +00002891 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002892 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
Neal Norwitzb0493252002-03-31 14:44:22 +00002893 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002894 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
Guido van Rossum60456fd1997-04-09 17:36:32 +00002895 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002896};
2897
2898
2899static Picklerobject *
Tim Peters5bd2a792003-02-01 16:45:06 +00002900newPicklerobject(PyObject *file, int proto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002901{
2902 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002903
Tim Peters5bd2a792003-02-01 16:45:06 +00002904 if (proto < 0)
Tim Peters8587b3c2003-02-13 15:44:41 +00002905 proto = HIGHEST_PROTOCOL;
2906 if (proto > HIGHEST_PROTOCOL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002907 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
2908 "the highest available protocol is %d",
Tim Peters8587b3c2003-02-13 15:44:41 +00002909 proto, HIGHEST_PROTOCOL);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002910 return NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002911 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002912
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002913 self = PyObject_GC_New(Picklerobject, &Picklertype);
Tim Peters5bd2a792003-02-01 16:45:06 +00002914 if (self == NULL)
2915 return NULL;
2916 self->proto = proto;
2917 self->bin = proto > 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002918 self->fp = NULL;
2919 self->write = NULL;
2920 self->memo = NULL;
2921 self->arg = NULL;
2922 self->pers_func = NULL;
2923 self->inst_pers_func = NULL;
2924 self->write_buf = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002925 self->fast = 0;
2926 self->fast_container = 0;
2927 self->fast_memo = NULL;
2928 self->buf_size = 0;
2929 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002930
Tim Peters5bd2a792003-02-01 16:45:06 +00002931 self->file = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002932 if (file)
2933 Py_INCREF(file);
Tim Peters5bd2a792003-02-01 16:45:06 +00002934 else {
2935 file = Pdata_New();
2936 if (file == NULL)
2937 goto err;
2938 }
2939 self->file = file;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002940
Tim Peterscba30e22003-02-01 06:24:36 +00002941 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002942 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002943
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002944 if (PyFile_Check(file)) {
2945 self->fp = PyFile_AsFile(file);
2946 if (self->fp == NULL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002947 PyErr_SetString(PyExc_ValueError,
2948 "I/O operation on closed file");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002949 goto err;
2950 }
2951 self->write_func = write_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002952 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002953 else if (PycStringIO_OutputCheck(file)) {
2954 self->write_func = write_cStringIO;
2955 }
2956 else if (file == Py_None) {
2957 self->write_func = write_none;
2958 }
2959 else {
2960 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002961
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002962 if (! Pdata_Check(file)) {
2963 self->write = PyObject_GetAttr(file, write_str);
2964 if (!self->write) {
2965 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00002966 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002967 "argument must have 'write' "
2968 "attribute");
2969 goto err;
2970 }
2971 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002972
Tim Peters5bd2a792003-02-01 16:45:06 +00002973 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
2974 if (self->write_buf == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002975 PyErr_NoMemory();
2976 goto err;
2977 }
2978 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002979
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002980 if (PyEval_GetRestricted()) {
2981 /* Restricted execution, get private tables */
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00002982 PyObject *m = PyImport_Import(copyreg_str);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002983
Tim Peters5b7da392003-02-04 00:21:07 +00002984 if (m == NULL)
2985 goto err;
2986 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002987 Py_DECREF(m);
Tim Peters5b7da392003-02-04 00:21:07 +00002988 if (self->dispatch_table == NULL)
2989 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002990 }
2991 else {
Tim Peters5b7da392003-02-04 00:21:07 +00002992 self->dispatch_table = dispatch_table;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002993 Py_INCREF(dispatch_table);
2994 }
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002995 PyObject_GC_Track(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002996
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002997 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002998
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002999 err:
Tim Peters5bd2a792003-02-01 16:45:06 +00003000 Py_DECREF(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003001 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003002}
3003
3004
3005static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00003006get_Pickler(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003007{
Martin v. Löwis15e62742006-02-27 16:46:16 +00003008 static char *kwlist[] = {"file", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003009 PyObject *file = NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00003010 int proto = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003011
Tim Peters92c8bb32003-02-13 23:00:26 +00003012 /* XXX
Martin v. Löwis544f1192004-07-27 05:22:33 +00003013 * The documented signature is Pickler(file, protocol=0), but this
Tim Peters92c8bb32003-02-13 23:00:26 +00003014 * accepts Pickler() and Pickler(integer) too. The meaning then
3015 * is clear as mud, undocumented, and not supported by pickle.py.
3016 * I'm told Zope uses this, but I haven't traced into this code
3017 * far enough to figure out what it means.
Tim Peters5bd2a792003-02-01 16:45:06 +00003018 */
3019 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003020 PyErr_Clear();
Tim Peters5bd2a792003-02-01 16:45:06 +00003021 proto = 0;
Martin v. Löwis544f1192004-07-27 05:22:33 +00003022 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler",
3023 kwlist, &file, &proto))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003024 return NULL;
3025 }
Tim Peters5bd2a792003-02-01 16:45:06 +00003026 return (PyObject *)newPicklerobject(file, proto);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003027}
3028
3029
3030static void
Tim Peterscba30e22003-02-01 06:24:36 +00003031Pickler_dealloc(Picklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003032{
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003033 PyObject_GC_UnTrack(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003034 Py_XDECREF(self->write);
3035 Py_XDECREF(self->memo);
3036 Py_XDECREF(self->fast_memo);
3037 Py_XDECREF(self->arg);
3038 Py_XDECREF(self->file);
3039 Py_XDECREF(self->pers_func);
3040 Py_XDECREF(self->inst_pers_func);
3041 Py_XDECREF(self->dispatch_table);
Tim Peters5bd2a792003-02-01 16:45:06 +00003042 PyMem_Free(self->write_buf);
Christian Heimese93237d2007-12-19 02:37:44 +00003043 Py_TYPE(self)->tp_free((PyObject *)self);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003044}
3045
3046static int
3047Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)
3048{
Thomas Woutersc6e55062006-04-15 21:47:09 +00003049 Py_VISIT(self->write);
3050 Py_VISIT(self->memo);
3051 Py_VISIT(self->fast_memo);
3052 Py_VISIT(self->arg);
3053 Py_VISIT(self->file);
3054 Py_VISIT(self->pers_func);
3055 Py_VISIT(self->inst_pers_func);
3056 Py_VISIT(self->dispatch_table);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003057 return 0;
3058}
3059
3060static int
3061Pickler_clear(Picklerobject *self)
3062{
Thomas Woutersedf17d82006-04-15 17:28:34 +00003063 Py_CLEAR(self->write);
3064 Py_CLEAR(self->memo);
3065 Py_CLEAR(self->fast_memo);
3066 Py_CLEAR(self->arg);
3067 Py_CLEAR(self->file);
3068 Py_CLEAR(self->pers_func);
3069 Py_CLEAR(self->inst_pers_func);
3070 Py_CLEAR(self->dispatch_table);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003071 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003072}
3073
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003074static PyObject *
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003075Pickler_get_pers_func(Picklerobject *p)
3076{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003077 if (p->pers_func == NULL)
3078 PyErr_SetString(PyExc_AttributeError, "persistent_id");
3079 else
3080 Py_INCREF(p->pers_func);
3081 return p->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003082}
3083
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003084static int
3085Pickler_set_pers_func(Picklerobject *p, PyObject *v)
3086{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003087 if (v == NULL) {
3088 PyErr_SetString(PyExc_TypeError,
3089 "attribute deletion is not supported");
3090 return -1;
3091 }
3092 Py_XDECREF(p->pers_func);
3093 Py_INCREF(v);
3094 p->pers_func = v;
3095 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003096}
3097
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003098static int
3099Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
3100{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003101 if (v == NULL) {
3102 PyErr_SetString(PyExc_TypeError,
3103 "attribute deletion is not supported");
3104 return -1;
3105 }
3106 Py_XDECREF(p->inst_pers_func);
3107 Py_INCREF(v);
3108 p->inst_pers_func = v;
3109 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003110}
3111
3112static PyObject *
3113Pickler_get_memo(Picklerobject *p)
3114{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003115 if (p->memo == NULL)
3116 PyErr_SetString(PyExc_AttributeError, "memo");
3117 else
3118 Py_INCREF(p->memo);
3119 return p->memo;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003120}
3121
3122static int
3123Pickler_set_memo(Picklerobject *p, PyObject *v)
3124{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003125 if (v == NULL) {
3126 PyErr_SetString(PyExc_TypeError,
3127 "attribute deletion is not supported");
3128 return -1;
3129 }
3130 if (!PyDict_Check(v)) {
3131 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
3132 return -1;
3133 }
3134 Py_XDECREF(p->memo);
3135 Py_INCREF(v);
3136 p->memo = v;
3137 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003138}
3139
3140static PyObject *
3141Pickler_get_error(Picklerobject *p)
3142{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003143 /* why is this an attribute on the Pickler? */
3144 Py_INCREF(PicklingError);
3145 return PicklingError;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003146}
3147
3148static PyMemberDef Pickler_members[] = {
3149 {"binary", T_INT, offsetof(Picklerobject, bin)},
3150 {"fast", T_INT, offsetof(Picklerobject, fast)},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003151 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003152};
3153
3154static PyGetSetDef Pickler_getsets[] = {
Tim Peterscba30e22003-02-01 06:24:36 +00003155 {"persistent_id", (getter)Pickler_get_pers_func,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003156 (setter)Pickler_set_pers_func},
3157 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
3158 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003159 {"PicklingError", (getter)Pickler_get_error, NULL},
3160 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003161};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003162
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003163PyDoc_STRVAR(Picklertype__doc__,
3164"Objects that know how to pickle objects\n");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003165
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003166static PyTypeObject Picklertype = {
Martin v. Löwis68192102007-07-21 06:55:02 +00003167 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00003168 "cPickle.Pickler", /*tp_name*/
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003169 sizeof(Picklerobject), /*tp_basicsize*/
3170 0,
3171 (destructor)Pickler_dealloc, /* tp_dealloc */
3172 0, /* tp_print */
3173 0, /* tp_getattr */
3174 0, /* tp_setattr */
3175 0, /* tp_compare */
3176 0, /* tp_repr */
3177 0, /* tp_as_number */
3178 0, /* tp_as_sequence */
3179 0, /* tp_as_mapping */
3180 0, /* tp_hash */
3181 0, /* tp_call */
3182 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00003183 PyObject_GenericGetAttr, /* tp_getattro */
3184 PyObject_GenericSetAttr, /* tp_setattro */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003185 0, /* tp_as_buffer */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003186 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003187 Picklertype__doc__, /* tp_doc */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003188 (traverseproc)Pickler_traverse, /* tp_traverse */
3189 (inquiry)Pickler_clear, /* tp_clear */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003190 0, /* tp_richcompare */
3191 0, /* tp_weaklistoffset */
3192 0, /* tp_iter */
3193 0, /* tp_iternext */
3194 Pickler_methods, /* tp_methods */
3195 Pickler_members, /* tp_members */
3196 Pickler_getsets, /* tp_getset */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003197};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003198
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003199static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003200find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003201{
3202 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003203
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003204 if (fc) {
3205 if (fc==Py_None) {
Tim Peters92c8bb32003-02-13 23:00:26 +00003206 PyErr_SetString(UnpicklingError, "Global and instance "
3207 "pickles are not supported.");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003208 return NULL;
3209 }
Georg Brandl684fd0c2006-05-25 19:15:31 +00003210 return PyObject_CallFunctionObjArgs(fc, py_module_name,
3211 py_global_name, NULL);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003212 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003213
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003214 module = PySys_GetObject("modules");
3215 if (module == NULL)
3216 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00003217
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003218 module = PyDict_GetItem(module, py_module_name);
3219 if (module == NULL) {
3220 module = PyImport_Import(py_module_name);
3221 if (!module)
3222 return NULL;
3223 global = PyObject_GetAttr(module, py_global_name);
3224 Py_DECREF(module);
3225 }
3226 else
3227 global = PyObject_GetAttr(module, py_global_name);
3228 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003229}
3230
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003231static int
Tim Peterscba30e22003-02-01 06:24:36 +00003232marker(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003233{
3234 if (self->num_marks < 1) {
3235 PyErr_SetString(UnpicklingError, "could not find MARK");
3236 return -1;
3237 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003238
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003239 return self->marks[--self->num_marks];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003240}
3241
Tim Peters84e87f32001-03-17 04:50:51 +00003242
Guido van Rossum60456fd1997-04-09 17:36:32 +00003243static int
Tim Peterscba30e22003-02-01 06:24:36 +00003244load_none(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003245{
3246 PDATA_APPEND(self->stack, Py_None, -1);
3247 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003248}
3249
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003250static int
Tim Peterscba30e22003-02-01 06:24:36 +00003251bad_readline(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003252{
3253 PyErr_SetString(UnpicklingError, "pickle data was truncated");
3254 return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003255}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003256
3257static int
Tim Peterscba30e22003-02-01 06:24:36 +00003258load_int(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003259{
3260 PyObject *py_int = 0;
3261 char *endptr, *s;
3262 int len, res = -1;
3263 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003264
Tim Peters0bc93f52003-02-02 18:29:33 +00003265 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003266 if (len < 2) return bad_readline();
3267 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003268
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003269 errno = 0;
3270 l = strtol(s, &endptr, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003271
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003272 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
3273 /* Hm, maybe we've got something long. Let's try reading
3274 it as a Python long object. */
3275 errno = 0;
3276 py_int = PyLong_FromString(s, NULL, 0);
3277 if (py_int == NULL) {
3278 PyErr_SetString(PyExc_ValueError,
3279 "could not convert string to int");
3280 goto finally;
3281 }
3282 }
3283 else {
Guido van Rossume2763392002-04-05 19:30:08 +00003284 if (len == 3 && (l == 0 || l == 1)) {
3285 if (!( py_int = PyBool_FromLong(l))) goto finally;
3286 }
3287 else {
3288 if (!( py_int = PyInt_FromLong(l))) goto finally;
3289 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003290 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003291
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003292 free(s);
3293 PDATA_PUSH(self->stack, py_int, -1);
3294 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003295
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003296 finally:
3297 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003298
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003299 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003300}
3301
Tim Peters3c67d792003-02-02 17:59:11 +00003302static int
3303load_bool(Unpicklerobject *self, PyObject *boolean)
3304{
3305 assert(boolean == Py_True || boolean == Py_False);
Tim Peterse0a39072003-02-03 15:45:56 +00003306 PDATA_APPEND(self->stack, boolean, -1);
Tim Peters3c67d792003-02-02 17:59:11 +00003307 return 0;
3308}
3309
Tim Petersee1a53c2003-02-02 02:57:53 +00003310/* s contains x bytes of a little-endian integer. Return its value as a
3311 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3312 * int, but when x is 4 it's a signed one. This is an historical source
3313 * of x-platform bugs.
3314 */
Tim Peters84e87f32001-03-17 04:50:51 +00003315static long
Tim Petersee1a53c2003-02-02 02:57:53 +00003316calc_binint(char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003317{
3318 unsigned char c;
3319 int i;
3320 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003321
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003322 for (i = 0, l = 0L; i < x; i++) {
3323 c = (unsigned char)s[i];
3324 l |= (long)c << (i * 8);
3325 }
Tim Petersbfa18f72001-04-10 01:54:42 +00003326#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003327 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3328 * is signed, so on a box with longs bigger than 4 bytes we need
3329 * to extend a BININT's sign bit to the full width.
3330 */
3331 if (x == 4 && l & (1L << 31))
3332 l |= (~0L) << 32;
Tim Petersbfa18f72001-04-10 01:54:42 +00003333#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003334 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003335}
3336
3337
3338static int
Tim Peterscba30e22003-02-01 06:24:36 +00003339load_binintx(Unpicklerobject *self, char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003340{
3341 PyObject *py_int = 0;
3342 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003343
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003344 l = calc_binint(s, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003345
Tim Peterscba30e22003-02-01 06:24:36 +00003346 if (!( py_int = PyInt_FromLong(l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003347 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003348
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003349 PDATA_PUSH(self->stack, py_int, -1);
3350 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003351}
3352
3353
3354static int
Tim Peterscba30e22003-02-01 06:24:36 +00003355load_binint(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003356{
3357 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003358
Tim Peters0bc93f52003-02-02 18:29:33 +00003359 if (self->read_func(self, &s, 4) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003360 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003361
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003362 return load_binintx(self, s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003363}
3364
3365
3366static int
Tim Peterscba30e22003-02-01 06:24:36 +00003367load_binint1(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003368{
3369 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003370
Tim Peters0bc93f52003-02-02 18:29:33 +00003371 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003372 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003373
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003374 return load_binintx(self, s, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003375}
3376
3377
3378static int
Tim Peterscba30e22003-02-01 06:24:36 +00003379load_binint2(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003380{
3381 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003382
Tim Peters0bc93f52003-02-02 18:29:33 +00003383 if (self->read_func(self, &s, 2) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003384 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003385
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003386 return load_binintx(self, s, 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003387}
Tim Peters84e87f32001-03-17 04:50:51 +00003388
Guido van Rossum60456fd1997-04-09 17:36:32 +00003389static int
Tim Peterscba30e22003-02-01 06:24:36 +00003390load_long(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003391{
3392 PyObject *l = 0;
3393 char *end, *s;
3394 int len, res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003395
Tim Peters0bc93f52003-02-02 18:29:33 +00003396 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003397 if (len < 2) return bad_readline();
3398 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003399
Tim Peterscba30e22003-02-01 06:24:36 +00003400 if (!( l = PyLong_FromString(s, &end, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003401 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003402
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003403 free(s);
3404 PDATA_PUSH(self->stack, l, -1);
3405 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003406
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003407 finally:
3408 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003409
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003410 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003411}
3412
Tim Petersee1a53c2003-02-02 02:57:53 +00003413/* 'size' bytes contain the # of bytes of little-endian 256's-complement
3414 * data following.
3415 */
3416static int
3417load_counted_long(Unpicklerobject *self, int size)
3418{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003419 Py_ssize_t i;
Tim Petersee1a53c2003-02-02 02:57:53 +00003420 char *nbytes;
3421 unsigned char *pdata;
3422 PyObject *along;
3423
3424 assert(size == 1 || size == 4);
3425 i = self->read_func(self, &nbytes, size);
3426 if (i < 0) return -1;
3427
3428 size = calc_binint(nbytes, size);
3429 if (size < 0) {
3430 /* Corrupt or hostile pickle -- we never write one like
3431 * this.
3432 */
Tim Peters0c7c48e2003-02-03 22:07:24 +00003433 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
Tim Petersee1a53c2003-02-02 02:57:53 +00003434 "byte count");
3435 return -1;
3436 }
3437
3438 if (size == 0)
3439 along = PyLong_FromLong(0L);
3440 else {
3441 /* Read the raw little-endian bytes & convert. */
Neal Norwitz529baf22003-02-02 17:08:33 +00003442 i = self->read_func(self, (char **)&pdata, size);
Tim Petersee1a53c2003-02-02 02:57:53 +00003443 if (i < 0) return -1;
3444 along = _PyLong_FromByteArray(pdata, (size_t)size,
3445 1 /* little endian */, 1 /* signed */);
3446 }
3447 if (along == NULL)
3448 return -1;
3449 PDATA_PUSH(self->stack, along, -1);
3450 return 0;
3451}
Tim Peters84e87f32001-03-17 04:50:51 +00003452
Guido van Rossum60456fd1997-04-09 17:36:32 +00003453static int
Tim Peterscba30e22003-02-01 06:24:36 +00003454load_float(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003455{
3456 PyObject *py_float = 0;
3457 char *endptr, *s;
3458 int len, res = -1;
3459 double d;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003460
Tim Peters0bc93f52003-02-02 18:29:33 +00003461 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003462 if (len < 2) return bad_readline();
3463 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003464
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003465 errno = 0;
Martin v. Löwis737ea822004-06-08 18:52:54 +00003466 d = PyOS_ascii_strtod(s, &endptr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003467
Mark Dickinson3df16922009-01-24 21:30:14 +00003468 if ((errno == ERANGE && !(fabs(d) <= 1.0)) ||
3469 (endptr[0] != '\n') || (endptr[1] != '\0')) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003470 PyErr_SetString(PyExc_ValueError,
3471 "could not convert string to float");
3472 goto finally;
3473 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003474
Tim Peterscba30e22003-02-01 06:24:36 +00003475 if (!( py_float = PyFloat_FromDouble(d)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003476 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003477
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003478 free(s);
3479 PDATA_PUSH(self->stack, py_float, -1);
3480 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003481
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003482 finally:
3483 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003484
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003485 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003486}
3487
Guido van Rossum60456fd1997-04-09 17:36:32 +00003488static int
Tim Peterscba30e22003-02-01 06:24:36 +00003489load_binfloat(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003490{
Tim Peters9905b942003-03-20 20:53:32 +00003491 PyObject *py_float;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003492 double x;
3493 char *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003494
Tim Peters0bc93f52003-02-02 18:29:33 +00003495 if (self->read_func(self, &p, 8) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003496 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003497
Tim Peters9905b942003-03-20 20:53:32 +00003498 x = _PyFloat_Unpack8((unsigned char *)p, 0);
3499 if (x == -1.0 && PyErr_Occurred())
3500 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003501
Tim Peters9905b942003-03-20 20:53:32 +00003502 py_float = PyFloat_FromDouble(x);
3503 if (py_float == NULL)
3504 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003505
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003506 PDATA_PUSH(self->stack, py_float, -1);
3507 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003508}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003509
3510static int
Tim Peterscba30e22003-02-01 06:24:36 +00003511load_string(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003512{
3513 PyObject *str = 0;
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003514 int len, res = -1;
3515 char *s, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003516
Tim Peters0bc93f52003-02-02 18:29:33 +00003517 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003518 if (len < 2) return bad_readline();
3519 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003520
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003521
3522 /* Strip outermost quotes */
3523 while (s[len-1] <= ' ')
3524 len--;
3525 if(s[0]=='"' && s[len-1]=='"'){
3526 s[len-1] = '\0';
3527 p = s + 1 ;
3528 len -= 2;
3529 } else if(s[0]=='\'' && s[len-1]=='\''){
3530 s[len-1] = '\0';
3531 p = s + 1 ;
3532 len -= 2;
3533 } else
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003534 goto insecure;
3535 /********************************************/
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003536
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003537 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
Neal Norwitz99dfe3c2006-08-02 04:27:11 +00003538 free(s);
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003539 if (str) {
3540 PDATA_PUSH(self->stack, str, -1);
3541 res = 0;
3542 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003543 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003544
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003545 insecure:
3546 free(s);
3547 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3548 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003549}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003550
3551
3552static int
Tim Peterscba30e22003-02-01 06:24:36 +00003553load_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003554{
3555 PyObject *py_string = 0;
3556 long l;
3557 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003558
Tim Peters0bc93f52003-02-02 18:29:33 +00003559 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003560
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003561 l = calc_binint(s, 4);
Gregory P. Smith9d534572008-06-11 07:41:16 +00003562 if (l < 0) {
3563 /* Corrupt or hostile pickle -- we never write one like
3564 * this.
3565 */
3566 PyErr_SetString(UnpicklingError,
3567 "BINSTRING pickle has negative byte count");
3568 return -1;
3569 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003570
Tim Peters0bc93f52003-02-02 18:29:33 +00003571 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003572 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003573
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003574 if (!( py_string = PyString_FromStringAndSize(s, l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003575 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003576
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003577 PDATA_PUSH(self->stack, py_string, -1);
3578 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003579}
3580
3581
3582static int
Tim Peterscba30e22003-02-01 06:24:36 +00003583load_short_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003584{
3585 PyObject *py_string = 0;
3586 unsigned char l;
3587 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003588
Tim Peters0bc93f52003-02-02 18:29:33 +00003589 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003590 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003591
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003592 l = (unsigned char)s[0];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003593
Tim Peters0bc93f52003-02-02 18:29:33 +00003594 if (self->read_func(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003595
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003596 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003597
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003598 PDATA_PUSH(self->stack, py_string, -1);
3599 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00003600}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003601
3602
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003603#ifdef Py_USING_UNICODE
Guido van Rossum60456fd1997-04-09 17:36:32 +00003604static int
Tim Peterscba30e22003-02-01 06:24:36 +00003605load_unicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003606{
3607 PyObject *str = 0;
3608 int len, res = -1;
3609 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003610
Tim Peters0bc93f52003-02-02 18:29:33 +00003611 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003612 if (len < 1) return bad_readline();
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003613
Tim Peterscba30e22003-02-01 06:24:36 +00003614 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003615 goto finally;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003616
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003617 PDATA_PUSH(self->stack, str, -1);
3618 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003619
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003620 finally:
3621 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003622}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003623#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003624
3625
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003626#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003627static int
Tim Peterscba30e22003-02-01 06:24:36 +00003628load_binunicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003629{
3630 PyObject *unicode;
3631 long l;
3632 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003633
Tim Peters0bc93f52003-02-02 18:29:33 +00003634 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003635
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003636 l = calc_binint(s, 4);
Gregory P. Smith9d534572008-06-11 07:41:16 +00003637 if (l < 0) {
3638 /* Corrupt or hostile pickle -- we never write one like
3639 * this.
3640 */
3641 PyErr_SetString(UnpicklingError,
3642 "BINUNICODE pickle has negative byte count");
3643 return -1;
3644 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003645
Tim Peters0bc93f52003-02-02 18:29:33 +00003646 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003647 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003648
Tim Peterscba30e22003-02-01 06:24:36 +00003649 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003650 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003651
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003652 PDATA_PUSH(self->stack, unicode, -1);
3653 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003654}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003655#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003656
3657
3658static int
Tim Peterscba30e22003-02-01 06:24:36 +00003659load_tuple(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003660{
3661 PyObject *tup;
3662 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003663
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003664 if ((i = marker(self)) < 0) return -1;
3665 if (!( tup=Pdata_popTuple(self->stack, i))) return -1;
3666 PDATA_PUSH(self->stack, tup, -1);
3667 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003668}
3669
3670static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00003671load_counted_tuple(Unpicklerobject *self, int len)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003672{
Tim Peters1d63c9f2003-02-02 20:29:39 +00003673 PyObject *tup = PyTuple_New(len);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003674
Tim Peters1d63c9f2003-02-02 20:29:39 +00003675 if (tup == NULL)
3676 return -1;
3677
3678 while (--len >= 0) {
3679 PyObject *element;
3680
3681 PDATA_POP(self->stack, element);
3682 if (element == NULL)
3683 return -1;
3684 PyTuple_SET_ITEM(tup, len, element);
3685 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003686 PDATA_PUSH(self->stack, tup, -1);
3687 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003688}
3689
3690static int
Tim Peterscba30e22003-02-01 06:24:36 +00003691load_empty_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003692{
3693 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003694
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003695 if (!( list=PyList_New(0))) return -1;
3696 PDATA_PUSH(self->stack, list, -1);
3697 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003698}
3699
3700static int
Tim Peterscba30e22003-02-01 06:24:36 +00003701load_empty_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003702{
3703 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003704
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003705 if (!( dict=PyDict_New())) return -1;
3706 PDATA_PUSH(self->stack, dict, -1);
3707 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003708}
3709
3710
3711static int
Tim Peterscba30e22003-02-01 06:24:36 +00003712load_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003713{
3714 PyObject *list = 0;
3715 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003716
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003717 if ((i = marker(self)) < 0) return -1;
3718 if (!( list=Pdata_popList(self->stack, i))) return -1;
3719 PDATA_PUSH(self->stack, list, -1);
3720 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003721}
3722
3723static int
Tim Peterscba30e22003-02-01 06:24:36 +00003724load_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003725{
3726 PyObject *dict, *key, *value;
3727 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003728
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003729 if ((i = marker(self)) < 0) return -1;
3730 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003731
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003732 if (!( dict = PyDict_New())) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003733
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003734 for (k = i+1; k < j; k += 2) {
3735 key =self->stack->data[k-1];
3736 value=self->stack->data[k ];
3737 if (PyDict_SetItem(dict, key, value) < 0) {
3738 Py_DECREF(dict);
3739 return -1;
3740 }
3741 }
3742 Pdata_clear(self->stack, i);
3743 PDATA_PUSH(self->stack, dict, -1);
3744 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003745}
3746
3747static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003748Instance_New(PyObject *cls, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003749{
Tim Peters1f1b2d22003-02-01 02:16:37 +00003750 PyObject *r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003751
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003752 if (PyClass_Check(cls)) {
3753 int l;
Tim Peters84e87f32001-03-17 04:50:51 +00003754
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003755 if ((l=PyObject_Size(args)) < 0) goto err;
3756 if (!( l )) {
3757 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003758
Tim Peterscba30e22003-02-01 06:24:36 +00003759 __getinitargs__ = PyObject_GetAttr(cls,
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00003760 __getinitargs___str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003761 if (!__getinitargs__) {
Tim Peterscba30e22003-02-01 06:24:36 +00003762 /* We have a class with no __getinitargs__,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003763 so bypass usual construction */
3764 PyObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003765
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003766 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00003767 if (!( inst=PyInstance_NewRaw(cls, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003768 goto err;
3769 return inst;
3770 }
3771 Py_DECREF(__getinitargs__);
3772 }
Tim Peters84e87f32001-03-17 04:50:51 +00003773
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003774 if ((r=PyInstance_New(cls, args, NULL))) return r;
3775 else goto err;
3776 }
Tim Peters84e87f32001-03-17 04:50:51 +00003777
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003778 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00003779
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003780 err:
3781 {
Neal Norwitz0f8b31a2006-06-28 06:28:31 +00003782 PyObject *tp, *v, *tb, *tmp_value;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003783
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003784 PyErr_Fetch(&tp, &v, &tb);
Neal Norwitz0f8b31a2006-06-28 06:28:31 +00003785 tmp_value = v;
3786 /* NULL occurs when there was a KeyboardInterrupt */
3787 if (tmp_value == NULL)
3788 tmp_value = Py_None;
3789 if ((r = PyTuple_Pack(3, tmp_value, cls, args))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003790 Py_XDECREF(v);
3791 v=r;
3792 }
3793 PyErr_Restore(tp,v,tb);
3794 }
3795 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003796}
Tim Peters84e87f32001-03-17 04:50:51 +00003797
Guido van Rossum60456fd1997-04-09 17:36:32 +00003798
3799static int
Tim Peterscba30e22003-02-01 06:24:36 +00003800load_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003801{
3802 PyObject *class, *tup, *obj=0;
3803 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003804
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003805 if ((i = marker(self)) < 0) return -1;
3806 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3807 PDATA_POP(self->stack, class);
3808 if (class) {
3809 obj = Instance_New(class, tup);
3810 Py_DECREF(class);
3811 }
3812 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003813
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003814 if (! obj) return -1;
3815 PDATA_PUSH(self->stack, obj, -1);
3816 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003817}
3818
3819
3820static int
Tim Peterscba30e22003-02-01 06:24:36 +00003821load_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003822{
3823 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
3824 int i, len;
3825 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003826
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003827 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003828
Tim Peters0bc93f52003-02-02 18:29:33 +00003829 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003830 if (len < 2) return bad_readline();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003831 module_name = PyString_FromStringAndSize(s, len - 1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003832 if (!module_name) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003833
Tim Peters0bc93f52003-02-02 18:29:33 +00003834 if ((len = self->readline_func(self, &s)) >= 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003835 if (len < 2) return bad_readline();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003836 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003837 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003838 self->find_class);
3839 Py_DECREF(class_name);
3840 }
3841 }
3842 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003843
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003844 if (! class) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003845
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003846 if ((tup=Pdata_popTuple(self->stack, i))) {
3847 obj = Instance_New(class, tup);
3848 Py_DECREF(tup);
3849 }
3850 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003851
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003852 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003853
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003854 PDATA_PUSH(self->stack, obj, -1);
3855 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003856}
3857
Tim Peterseab7db32003-02-13 18:24:14 +00003858static int
3859load_newobj(Unpicklerobject *self)
3860{
3861 PyObject *args = NULL;
3862 PyObject *clsraw = NULL;
3863 PyTypeObject *cls; /* clsraw cast to its true type */
3864 PyObject *obj;
3865
3866 /* Stack is ... cls argtuple, and we want to call
3867 * cls.__new__(cls, *argtuple).
3868 */
3869 PDATA_POP(self->stack, args);
3870 if (args == NULL) goto Fail;
3871 if (! PyTuple_Check(args)) {
3872 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
3873 "tuple.");
3874 goto Fail;
3875 }
3876
3877 PDATA_POP(self->stack, clsraw);
3878 cls = (PyTypeObject *)clsraw;
3879 if (cls == NULL) goto Fail;
3880 if (! PyType_Check(cls)) {
3881 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3882 "isn't a type object");
3883 goto Fail;
3884 }
3885 if (cls->tp_new == NULL) {
3886 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3887 "has NULL tp_new");
3888 goto Fail;
3889 }
3890
3891 /* Call __new__. */
3892 obj = cls->tp_new(cls, args, NULL);
3893 if (obj == NULL) goto Fail;
3894
3895 Py_DECREF(args);
3896 Py_DECREF(clsraw);
3897 PDATA_PUSH(self->stack, obj, -1);
3898 return 0;
3899
3900 Fail:
3901 Py_XDECREF(args);
3902 Py_XDECREF(clsraw);
3903 return -1;
3904}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003905
3906static int
Tim Peterscba30e22003-02-01 06:24:36 +00003907load_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003908{
3909 PyObject *class = 0, *module_name = 0, *class_name = 0;
3910 int len;
3911 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003912
Tim Peters0bc93f52003-02-02 18:29:33 +00003913 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003914 if (len < 2) return bad_readline();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003915 module_name = PyString_FromStringAndSize(s, len - 1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003916 if (!module_name) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003917
Tim Peters0bc93f52003-02-02 18:29:33 +00003918 if ((len = self->readline_func(self, &s)) >= 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +00003919 if (len < 2) {
3920 Py_DECREF(module_name);
3921 return bad_readline();
3922 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003923 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003924 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003925 self->find_class);
3926 Py_DECREF(class_name);
3927 }
3928 }
3929 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003930
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003931 if (! class) return -1;
3932 PDATA_PUSH(self->stack, class, -1);
3933 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003934}
3935
3936
3937static int
Tim Peterscba30e22003-02-01 06:24:36 +00003938load_persid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003939{
3940 PyObject *pid = 0;
3941 int len;
3942 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003943
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003944 if (self->pers_func) {
Tim Peters0bc93f52003-02-02 18:29:33 +00003945 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003946 if (len < 2) return bad_readline();
3947
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003948 pid = PyString_FromStringAndSize(s, len - 1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003949 if (!pid) return -1;
3950
3951 if (PyList_Check(self->pers_func)) {
3952 if (PyList_Append(self->pers_func, pid) < 0) {
3953 Py_DECREF(pid);
3954 return -1;
3955 }
3956 }
3957 else {
3958 ARG_TUP(self, pid);
3959 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00003960 pid = PyObject_Call(self->pers_func, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003961 NULL);
3962 FREE_ARG_TUP(self);
3963 }
3964 }
3965
3966 if (! pid) return -1;
3967
3968 PDATA_PUSH(self->stack, pid, -1);
3969 return 0;
3970 }
3971 else {
3972 PyErr_SetString(UnpicklingError,
3973 "A load persistent id instruction was encountered,\n"
3974 "but no persistent_load function was specified.");
3975 return -1;
3976 }
3977}
3978
3979static int
Tim Peterscba30e22003-02-01 06:24:36 +00003980load_binpersid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003981{
3982 PyObject *pid = 0;
3983
3984 if (self->pers_func) {
3985 PDATA_POP(self->stack, pid);
3986 if (! pid) return -1;
3987
3988 if (PyList_Check(self->pers_func)) {
3989 if (PyList_Append(self->pers_func, pid) < 0) {
3990 Py_DECREF(pid);
3991 return -1;
3992 }
3993 }
3994 else {
3995 ARG_TUP(self, pid);
3996 if (self->arg) {
3997 pid = PyObject_Call(self->pers_func, self->arg,
3998 NULL);
3999 FREE_ARG_TUP(self);
4000 }
4001 if (! pid) return -1;
4002 }
4003
4004 PDATA_PUSH(self->stack, pid, -1);
4005 return 0;
4006 }
4007 else {
4008 PyErr_SetString(UnpicklingError,
4009 "A load persistent id instruction was encountered,\n"
4010 "but no persistent_load function was specified.");
4011 return -1;
4012 }
4013}
4014
4015
4016static int
Tim Peterscba30e22003-02-01 06:24:36 +00004017load_pop(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004018{
4019 int len;
4020
4021 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
4022
4023 /* Note that we split the (pickle.py) stack into two stacks,
4024 an object stack and a mark stack. We have to be clever and
4025 pop the right one. We do this by looking at the top of the
4026 mark stack.
4027 */
4028
4029 if ((self->num_marks > 0) &&
4030 (self->marks[self->num_marks - 1] == len))
4031 self->num_marks--;
4032 else {
4033 len--;
4034 Py_DECREF(self->stack->data[len]);
4035 self->stack->length=len;
4036 }
4037
4038 return 0;
4039}
4040
4041
4042static int
Tim Peterscba30e22003-02-01 06:24:36 +00004043load_pop_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004044{
4045 int i;
4046
4047 if ((i = marker(self)) < 0)
4048 return -1;
4049
4050 Pdata_clear(self->stack, i);
4051
4052 return 0;
4053}
4054
4055
4056static int
Tim Peterscba30e22003-02-01 06:24:36 +00004057load_dup(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004058{
4059 PyObject *last;
4060 int len;
4061
4062 if ((len = self->stack->length) <= 0) return stackUnderflow();
4063 last=self->stack->data[len-1];
4064 Py_INCREF(last);
4065 PDATA_PUSH(self->stack, last, -1);
4066 return 0;
4067}
4068
4069
4070static int
Tim Peterscba30e22003-02-01 06:24:36 +00004071load_get(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004072{
4073 PyObject *py_str = 0, *value = 0;
4074 int len;
4075 char *s;
4076 int rc;
4077
Tim Peters0bc93f52003-02-02 18:29:33 +00004078 if ((len = self->readline_func(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00004079 if (len < 2) return bad_readline();
Tim Peters84e87f32001-03-17 04:50:51 +00004080
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004081 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004082
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004083 value = PyDict_GetItem(self->memo, py_str);
4084 if (! value) {
4085 PyErr_SetObject(BadPickleGet, py_str);
4086 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00004087 }
4088 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004089 PDATA_APPEND(self->stack, value, -1);
4090 rc = 0;
4091 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004092
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004093 Py_DECREF(py_str);
4094 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004095}
4096
4097
4098static int
Tim Peterscba30e22003-02-01 06:24:36 +00004099load_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004100{
4101 PyObject *py_key = 0, *value = 0;
4102 unsigned char key;
4103 char *s;
4104 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004105
Tim Peters0bc93f52003-02-02 18:29:33 +00004106 if (self->read_func(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004107
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004108 key = (unsigned char)s[0];
4109 if (!( py_key = PyInt_FromLong((long)key))) return -1;
Guido van Rossumea2b7152000-05-09 18:14:50 +00004110
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004111 value = PyDict_GetItem(self->memo, py_key);
4112 if (! value) {
4113 PyErr_SetObject(BadPickleGet, py_key);
4114 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00004115 }
4116 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004117 PDATA_APPEND(self->stack, value, -1);
4118 rc = 0;
4119 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004120
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004121 Py_DECREF(py_key);
4122 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004123}
4124
4125
4126static int
Tim Peterscba30e22003-02-01 06:24:36 +00004127load_long_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004128{
4129 PyObject *py_key = 0, *value = 0;
4130 unsigned char c;
4131 char *s;
4132 long key;
4133 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004134
Tim Peters0bc93f52003-02-02 18:29:33 +00004135 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004136
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004137 c = (unsigned char)s[0];
4138 key = (long)c;
4139 c = (unsigned char)s[1];
4140 key |= (long)c << 8;
4141 c = (unsigned char)s[2];
4142 key |= (long)c << 16;
4143 c = (unsigned char)s[3];
4144 key |= (long)c << 24;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004145
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004146 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4147
4148 value = PyDict_GetItem(self->memo, py_key);
4149 if (! value) {
4150 PyErr_SetObject(BadPickleGet, py_key);
4151 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00004152 }
4153 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004154 PDATA_APPEND(self->stack, value, -1);
4155 rc = 0;
4156 }
4157
4158 Py_DECREF(py_key);
4159 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004160}
4161
Tim Peters2d629652003-02-04 05:06:17 +00004162/* Push an object from the extension registry (EXT[124]). nbytes is
4163 * the number of bytes following the opcode, holding the index (code) value.
4164 */
4165static int
4166load_extension(Unpicklerobject *self, int nbytes)
4167{
4168 char *codebytes; /* the nbytes bytes after the opcode */
4169 long code; /* calc_binint returns long */
4170 PyObject *py_code; /* code as a Python int */
4171 PyObject *obj; /* the object to push */
4172 PyObject *pair; /* (module_name, class_name) */
4173 PyObject *module_name, *class_name;
4174
4175 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4176 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4177 code = calc_binint(codebytes, nbytes);
4178 if (code <= 0) { /* note that 0 is forbidden */
4179 /* Corrupt or hostile pickle. */
4180 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
4181 return -1;
4182 }
4183
4184 /* Look for the code in the cache. */
4185 py_code = PyInt_FromLong(code);
4186 if (py_code == NULL) return -1;
4187 obj = PyDict_GetItem(extension_cache, py_code);
4188 if (obj != NULL) {
4189 /* Bingo. */
4190 Py_DECREF(py_code);
4191 PDATA_APPEND(self->stack, obj, -1);
4192 return 0;
4193 }
4194
4195 /* Look up the (module_name, class_name) pair. */
4196 pair = PyDict_GetItem(inverted_registry, py_code);
4197 if (pair == NULL) {
4198 Py_DECREF(py_code);
4199 PyErr_Format(PyExc_ValueError, "unregistered extension "
4200 "code %ld", code);
4201 return -1;
4202 }
4203 /* Since the extension registry is manipulable via Python code,
Tim Petersfa05ce32003-02-04 05:20:32 +00004204 * confirm that pair is really a 2-tuple of strings.
Tim Peters2d629652003-02-04 05:06:17 +00004205 */
4206 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004207 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
4208 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
Tim Peters2d629652003-02-04 05:06:17 +00004209 Py_DECREF(py_code);
4210 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
4211 "isn't a 2-tuple of strings", code);
4212 return -1;
4213 }
4214 /* Load the object. */
4215 obj = find_class(module_name, class_name, self->find_class);
4216 if (obj == NULL) {
4217 Py_DECREF(py_code);
4218 return -1;
4219 }
4220 /* Cache code -> obj. */
4221 code = PyDict_SetItem(extension_cache, py_code, obj);
4222 Py_DECREF(py_code);
4223 if (code < 0) {
4224 Py_DECREF(obj);
4225 return -1;
4226 }
4227 PDATA_PUSH(self->stack, obj, -1);
4228 return 0;
4229}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004230
4231static int
Tim Peterscba30e22003-02-01 06:24:36 +00004232load_put(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004233{
4234 PyObject *py_str = 0, *value = 0;
4235 int len, l;
4236 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004237
Tim Peters0bc93f52003-02-02 18:29:33 +00004238 if ((l = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004239 if (l < 2) return bad_readline();
4240 if (!( len=self->stack->length )) return stackUnderflow();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004241 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004242 value=self->stack->data[len-1];
4243 l=PyDict_SetItem(self->memo, py_str, value);
4244 Py_DECREF(py_str);
4245 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004246}
4247
4248
4249static int
Tim Peterscba30e22003-02-01 06:24:36 +00004250load_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004251{
4252 PyObject *py_key = 0, *value = 0;
4253 unsigned char key;
4254 char *s;
4255 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004256
Tim Peters0bc93f52003-02-02 18:29:33 +00004257 if (self->read_func(self, &s, 1) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004258 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004259
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004260 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00004261
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004262 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4263 value=self->stack->data[len-1];
4264 len=PyDict_SetItem(self->memo, py_key, value);
4265 Py_DECREF(py_key);
4266 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004267}
4268
4269
4270static int
Tim Peterscba30e22003-02-01 06:24:36 +00004271load_long_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004272{
4273 PyObject *py_key = 0, *value = 0;
4274 long key;
4275 unsigned char c;
4276 char *s;
4277 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004278
Tim Peters0bc93f52003-02-02 18:29:33 +00004279 if (self->read_func(self, &s, 4) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004280 if (!( len=self->stack->length )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004281
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004282 c = (unsigned char)s[0];
4283 key = (long)c;
4284 c = (unsigned char)s[1];
4285 key |= (long)c << 8;
4286 c = (unsigned char)s[2];
4287 key |= (long)c << 16;
4288 c = (unsigned char)s[3];
4289 key |= (long)c << 24;
Tim Peters84e87f32001-03-17 04:50:51 +00004290
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004291 if (!( py_key = PyInt_FromLong(key))) return -1;
4292 value=self->stack->data[len-1];
4293 len=PyDict_SetItem(self->memo, py_key, value);
4294 Py_DECREF(py_key);
4295 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004296}
4297
4298
4299static int
Tim Peterscba30e22003-02-01 06:24:36 +00004300do_append(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004301{
4302 PyObject *value = 0, *list = 0, *append_method = 0;
4303 int len, i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004304
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004305 len=self->stack->length;
4306 if (!( len >= x && x > 0 )) return stackUnderflow();
4307 /* nothing to do */
Tim Peterscba30e22003-02-01 06:24:36 +00004308 if (len==x) return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004309
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004310 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004311
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004312 if (PyList_Check(list)) {
4313 PyObject *slice;
4314 int list_len;
Tim Peters84e87f32001-03-17 04:50:51 +00004315
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004316 slice=Pdata_popList(self->stack, x);
Neal Norwitzb59d08c2006-07-22 16:20:49 +00004317 if (! slice) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004318 list_len = PyList_GET_SIZE(list);
4319 i=PyList_SetSlice(list, list_len, list_len, slice);
4320 Py_DECREF(slice);
4321 return i;
4322 }
4323 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004324
Tim Peterscba30e22003-02-01 06:24:36 +00004325 if (!( append_method = PyObject_GetAttr(list, append_str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004326 return -1;
4327
4328 for (i = x; i < len; i++) {
4329 PyObject *junk;
4330
4331 value=self->stack->data[i];
4332 junk=0;
4333 ARG_TUP(self, value);
4334 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00004335 junk = PyObject_Call(append_method, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004336 NULL);
4337 FREE_ARG_TUP(self);
4338 }
4339 if (! junk) {
4340 Pdata_clear(self->stack, i+1);
4341 self->stack->length=x;
4342 Py_DECREF(append_method);
4343 return -1;
4344 }
4345 Py_DECREF(junk);
4346 }
4347 self->stack->length=x;
4348 Py_DECREF(append_method);
4349 }
4350
4351 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004352}
4353
4354
4355static int
Tim Peterscba30e22003-02-01 06:24:36 +00004356load_append(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004357{
4358 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004359}
4360
4361
4362static int
Tim Peterscba30e22003-02-01 06:24:36 +00004363load_appends(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004364{
4365 return do_append(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004366}
4367
4368
4369static int
Tim Peterscba30e22003-02-01 06:24:36 +00004370do_setitems(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004371{
4372 PyObject *value = 0, *key = 0, *dict = 0;
4373 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004374
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004375 if (!( (len=self->stack->length) >= x
4376 && x > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004377
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004378 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004379
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004380 for (i = x+1; i < len; i += 2) {
4381 key =self->stack->data[i-1];
4382 value=self->stack->data[i ];
4383 if (PyObject_SetItem(dict, key, value) < 0) {
4384 r=-1;
4385 break;
4386 }
4387 }
4388
4389 Pdata_clear(self->stack, x);
4390
4391 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004392}
4393
4394
Tim Peters84e87f32001-03-17 04:50:51 +00004395static int
Tim Peterscba30e22003-02-01 06:24:36 +00004396load_setitem(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004397{
4398 return do_setitems(self, self->stack->length - 2);
4399}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004400
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004401static int
Tim Peterscba30e22003-02-01 06:24:36 +00004402load_setitems(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004403{
4404 return do_setitems(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004405}
4406
Tim Peters84e87f32001-03-17 04:50:51 +00004407
Guido van Rossum60456fd1997-04-09 17:36:32 +00004408static int
Tim Peterscba30e22003-02-01 06:24:36 +00004409load_build(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004410{
Tim Peters080c88b2003-02-15 03:01:11 +00004411 PyObject *state, *inst, *slotstate;
4412 PyObject *__setstate__;
4413 PyObject *d_key, *d_value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004414 Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00004415 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004416
Tim Peters080c88b2003-02-15 03:01:11 +00004417 /* Stack is ... instance, state. We want to leave instance at
4418 * the stack top, possibly mutated via instance.__setstate__(state).
4419 */
4420 if (self->stack->length < 2)
4421 return stackUnderflow();
4422 PDATA_POP(self->stack, state);
4423 if (state == NULL)
4424 return -1;
4425 inst = self->stack->data[self->stack->length - 1];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004426
Tim Peters080c88b2003-02-15 03:01:11 +00004427 __setstate__ = PyObject_GetAttr(inst, __setstate___str);
4428 if (__setstate__ != NULL) {
4429 PyObject *junk = NULL;
4430
4431 /* The explicit __setstate__ is responsible for everything. */
4432 ARG_TUP(self, state);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004433 if (self->arg) {
4434 junk = PyObject_Call(__setstate__, self->arg, NULL);
4435 FREE_ARG_TUP(self);
4436 }
4437 Py_DECREF(__setstate__);
Tim Peters080c88b2003-02-15 03:01:11 +00004438 if (junk == NULL)
4439 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004440 Py_DECREF(junk);
4441 return 0;
4442 }
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00004443 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4444 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004445 PyErr_Clear();
Tim Peters080c88b2003-02-15 03:01:11 +00004446
4447 /* A default __setstate__. First see whether state embeds a
4448 * slot state dict too (a proto 2 addition).
4449 */
4450 if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
4451 PyObject *temp = state;
4452 state = PyTuple_GET_ITEM(temp, 0);
4453 slotstate = PyTuple_GET_ITEM(temp, 1);
4454 Py_INCREF(state);
4455 Py_INCREF(slotstate);
4456 Py_DECREF(temp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004457 }
Tim Peters080c88b2003-02-15 03:01:11 +00004458 else
4459 slotstate = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004460
Tim Peters080c88b2003-02-15 03:01:11 +00004461 /* Set inst.__dict__ from the state dict (if any). */
4462 if (state != Py_None) {
4463 PyObject *dict;
4464 if (! PyDict_Check(state)) {
4465 PyErr_SetString(UnpicklingError, "state is not a "
4466 "dictionary");
4467 goto finally;
4468 }
4469 dict = PyObject_GetAttr(inst, __dict___str);
4470 if (dict == NULL)
4471 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004472
Tim Peters080c88b2003-02-15 03:01:11 +00004473 i = 0;
4474 while (PyDict_Next(state, &i, &d_key, &d_value)) {
4475 if (PyObject_SetItem(dict, d_key, d_value) < 0)
4476 goto finally;
4477 }
4478 Py_DECREF(dict);
4479 }
4480
4481 /* Also set instance attributes from the slotstate dict (if any). */
4482 if (slotstate != NULL) {
4483 if (! PyDict_Check(slotstate)) {
4484 PyErr_SetString(UnpicklingError, "slot state is not "
4485 "a dictionary");
4486 goto finally;
4487 }
4488 i = 0;
4489 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
4490 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
4491 goto finally;
4492 }
4493 }
4494 res = 0;
4495
4496 finally:
4497 Py_DECREF(state);
4498 Py_XDECREF(slotstate);
4499 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004500}
4501
4502
4503static int
Tim Peterscba30e22003-02-01 06:24:36 +00004504load_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004505{
4506 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004507
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004508 /* Note that we split the (pickle.py) stack into two stacks, an
4509 object stack and a mark stack. Here we push a mark onto the
4510 mark stack.
4511 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00004512
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004513 if ((self->num_marks + 1) >= self->marks_size) {
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004514 int *marks;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004515 s=self->marks_size+20;
4516 if (s <= self->num_marks) s=self->num_marks + 1;
4517 if (self->marks == NULL)
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004518 marks=(int *)malloc(s * sizeof(int));
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004519 else
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004520 marks=(int *)realloc(self->marks,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004521 s * sizeof(int));
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004522 if (!marks) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004523 PyErr_NoMemory();
4524 return -1;
4525 }
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004526 self->marks = marks;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004527 self->marks_size = s;
4528 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004529
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004530 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004531
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004532 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004533}
4534
Guido van Rossum60456fd1997-04-09 17:36:32 +00004535static int
Tim Peterscba30e22003-02-01 06:24:36 +00004536load_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004537{
4538 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004539
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004540 PDATA_POP(self->stack, arg_tup);
4541 if (! arg_tup) return -1;
4542 PDATA_POP(self->stack, callable);
4543 if (callable) {
4544 ob = Instance_New(callable, arg_tup);
4545 Py_DECREF(callable);
4546 }
4547 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004548
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004549 if (! ob) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004550
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004551 PDATA_PUSH(self->stack, ob, -1);
4552 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004553}
Tim Peters84e87f32001-03-17 04:50:51 +00004554
Tim Peters4190fb82003-02-02 16:09:05 +00004555/* Just raises an error if we don't know the protocol specified. PROTO
4556 * is the first opcode for protocols >= 2.
4557 */
4558static int
4559load_proto(Unpicklerobject *self)
4560{
4561 int i;
4562 char *protobyte;
4563
4564 i = self->read_func(self, &protobyte, 1);
4565 if (i < 0)
4566 return -1;
4567
4568 i = calc_binint(protobyte, 1);
4569 /* No point checking for < 0, since calc_binint returns an unsigned
4570 * int when chewing on 1 byte.
4571 */
4572 assert(i >= 0);
Tim Peters8587b3c2003-02-13 15:44:41 +00004573 if (i <= HIGHEST_PROTOCOL)
Tim Peters4190fb82003-02-02 16:09:05 +00004574 return 0;
4575
4576 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4577 return -1;
4578}
4579
Guido van Rossum60456fd1997-04-09 17:36:32 +00004580static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004581load(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004582{
4583 PyObject *err = 0, *val = 0;
4584 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004585
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004586 self->num_marks = 0;
4587 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004588
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004589 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004590 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004591 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004592
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004593 switch (s[0]) {
4594 case NONE:
4595 if (load_none(self) < 0)
4596 break;
4597 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004598
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004599 case BININT:
4600 if (load_binint(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 BININT1:
4605 if (load_binint1(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 BININT2:
4610 if (load_binint2(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 INT:
4615 if (load_int(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 LONG:
4620 if (load_long(self) < 0)
4621 break;
4622 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004623
Tim Petersee1a53c2003-02-02 02:57:53 +00004624 case LONG1:
4625 if (load_counted_long(self, 1) < 0)
4626 break;
4627 continue;
4628
4629 case LONG4:
4630 if (load_counted_long(self, 4) < 0)
4631 break;
4632 continue;
4633
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004634 case FLOAT:
4635 if (load_float(self) < 0)
4636 break;
4637 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004638
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004639 case BINFLOAT:
4640 if (load_binfloat(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 BINSTRING:
4645 if (load_binstring(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 SHORT_BINSTRING:
4650 if (load_short_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 STRING:
4655 if (load_string(self) < 0)
4656 break;
4657 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004658
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004659#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004660 case UNICODE:
4661 if (load_unicode(self) < 0)
4662 break;
4663 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004664
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004665 case BINUNICODE:
4666 if (load_binunicode(self) < 0)
4667 break;
4668 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004669#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004670
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004671 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004672 if (load_counted_tuple(self, 0) < 0)
4673 break;
4674 continue;
4675
4676 case TUPLE1:
4677 if (load_counted_tuple(self, 1) < 0)
4678 break;
4679 continue;
4680
4681 case TUPLE2:
4682 if (load_counted_tuple(self, 2) < 0)
4683 break;
4684 continue;
4685
4686 case TUPLE3:
4687 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004688 break;
4689 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004690
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004691 case TUPLE:
4692 if (load_tuple(self) < 0)
4693 break;
4694 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004695
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004696 case EMPTY_LIST:
4697 if (load_empty_list(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 LIST:
4702 if (load_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 EMPTY_DICT:
4707 if (load_empty_dict(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 DICT:
4712 if (load_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 OBJ:
4717 if (load_obj(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 INST:
4722 if (load_inst(self) < 0)
4723 break;
4724 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004725
Tim Peterseab7db32003-02-13 18:24:14 +00004726 case NEWOBJ:
4727 if (load_newobj(self) < 0)
4728 break;
4729 continue;
4730
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004731 case GLOBAL:
4732 if (load_global(self) < 0)
4733 break;
4734 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004735
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004736 case APPEND:
4737 if (load_append(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 APPENDS:
4742 if (load_appends(self) < 0)
4743 break;
4744 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004745
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004746 case BUILD:
4747 if (load_build(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 DUP:
4752 if (load_dup(self) < 0)
4753 break;
4754 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004755
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004756 case BINGET:
4757 if (load_binget(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 LONG_BINGET:
4762 if (load_long_binget(self) < 0)
4763 break;
4764 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004765
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004766 case GET:
4767 if (load_get(self) < 0)
4768 break;
4769 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004770
Tim Peters2d629652003-02-04 05:06:17 +00004771 case EXT1:
4772 if (load_extension(self, 1) < 0)
4773 break;
4774 continue;
4775
4776 case EXT2:
4777 if (load_extension(self, 2) < 0)
4778 break;
4779 continue;
4780
4781 case EXT4:
4782 if (load_extension(self, 4) < 0)
4783 break;
4784 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004785 case MARK:
4786 if (load_mark(self) < 0)
4787 break;
4788 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004789
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004790 case BINPUT:
4791 if (load_binput(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 LONG_BINPUT:
4796 if (load_long_binput(self) < 0)
4797 break;
4798 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004799
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004800 case PUT:
4801 if (load_put(self) < 0)
4802 break;
4803 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004804
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004805 case POP:
4806 if (load_pop(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_MARK:
4811 if (load_pop_mark(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 SETITEM:
4816 if (load_setitem(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 SETITEMS:
4821 if (load_setitems(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 STOP:
4826 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004827
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004828 case PERSID:
4829 if (load_persid(self) < 0)
4830 break;
4831 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004832
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004833 case BINPERSID:
4834 if (load_binpersid(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 REDUCE:
4839 if (load_reduce(self) < 0)
4840 break;
4841 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004842
Tim Peters4190fb82003-02-02 16:09:05 +00004843 case PROTO:
4844 if (load_proto(self) < 0)
4845 break;
4846 continue;
4847
Tim Peters3c67d792003-02-02 17:59:11 +00004848 case NEWTRUE:
4849 if (load_bool(self, Py_True) < 0)
4850 break;
4851 continue;
4852
4853 case NEWFALSE:
4854 if (load_bool(self, Py_False) < 0)
4855 break;
4856 continue;
4857
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004858 case '\0':
4859 /* end of file */
4860 PyErr_SetNone(PyExc_EOFError);
4861 break;
Tim Peterscba30e22003-02-01 06:24:36 +00004862
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004863 default:
Tim Peterscba30e22003-02-01 06:24:36 +00004864 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004865 "invalid load key, '%s'.",
4866 "c", s[0]);
4867 return NULL;
4868 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004869
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004870 break;
4871 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004872
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004873 if ((err = PyErr_Occurred())) {
4874 if (err == PyExc_EOFError) {
4875 PyErr_SetNone(PyExc_EOFError);
4876 }
4877 return NULL;
4878 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004879
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004880 PDATA_POP(self->stack, val);
4881 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004882}
Tim Peters84e87f32001-03-17 04:50:51 +00004883
Guido van Rossum60456fd1997-04-09 17:36:32 +00004884
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004885/* No-load functions to support noload, which is used to
4886 find persistent references. */
4887
4888static int
Tim Peterscba30e22003-02-01 06:24:36 +00004889noload_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004890{
4891 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004892
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004893 if ((i = marker(self)) < 0) return -1;
4894 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004895}
4896
4897
4898static int
Tim Peterscba30e22003-02-01 06:24:36 +00004899noload_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004900{
4901 int i;
4902 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004903
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004904 if ((i = marker(self)) < 0) return -1;
4905 Pdata_clear(self->stack, i);
Tim Peters0bc93f52003-02-02 18:29:33 +00004906 if (self->readline_func(self, &s) < 0) return -1;
4907 if (self->readline_func(self, &s) < 0) return -1;
Tim Peterseab7db32003-02-13 18:24:14 +00004908 PDATA_APPEND(self->stack, Py_None, -1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004909 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004910}
4911
4912static int
Tim Peterseab7db32003-02-13 18:24:14 +00004913noload_newobj(Unpicklerobject *self)
4914{
4915 PyObject *obj;
4916
4917 PDATA_POP(self->stack, obj); /* pop argtuple */
4918 if (obj == NULL) return -1;
4919 Py_DECREF(obj);
4920
4921 PDATA_POP(self->stack, obj); /* pop cls */
4922 if (obj == NULL) return -1;
4923 Py_DECREF(obj);
4924
4925 PDATA_APPEND(self->stack, Py_None, -1);
4926 return 0;
4927}
4928
4929static int
Tim Peterscba30e22003-02-01 06:24:36 +00004930noload_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004931{
4932 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004933
Tim Peters0bc93f52003-02-02 18:29:33 +00004934 if (self->readline_func(self, &s) < 0) return -1;
4935 if (self->readline_func(self, &s) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004936 PDATA_APPEND(self->stack, Py_None,-1);
4937 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004938}
4939
4940static int
Tim Peterscba30e22003-02-01 06:24:36 +00004941noload_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004942{
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004943
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004944 if (self->stack->length < 2) return stackUnderflow();
4945 Pdata_clear(self->stack, self->stack->length-2);
4946 PDATA_APPEND(self->stack, Py_None,-1);
4947 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004948}
4949
4950static int
4951noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004952
Guido van Rossum053b8df1998-11-25 16:18:00 +00004953 if (self->stack->length < 1) return stackUnderflow();
4954 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00004955 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004956}
4957
Tim Peters2d629652003-02-04 05:06:17 +00004958static int
4959noload_extension(Unpicklerobject *self, int nbytes)
4960{
4961 char *codebytes;
4962
4963 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4964 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4965 PDATA_APPEND(self->stack, Py_None, -1);
4966 return 0;
4967}
4968
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004969
4970static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004971noload(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004972{
4973 PyObject *err = 0, *val = 0;
4974 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004975
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004976 self->num_marks = 0;
4977 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004978
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004979 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004980 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004981 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004982
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004983 switch (s[0]) {
4984 case NONE:
4985 if (load_none(self) < 0)
4986 break;
4987 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004988
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004989 case BININT:
4990 if (load_binint(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 BININT1:
4995 if (load_binint1(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 BININT2:
5000 if (load_binint2(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 INT:
5005 if (load_int(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 LONG:
5010 if (load_long(self) < 0)
5011 break;
5012 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005013
Tim Peters4190fb82003-02-02 16:09:05 +00005014 case LONG1:
5015 if (load_counted_long(self, 1) < 0)
5016 break;
5017 continue;
5018
5019 case LONG4:
5020 if (load_counted_long(self, 4) < 0)
5021 break;
5022 continue;
5023
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005024 case FLOAT:
5025 if (load_float(self) < 0)
5026 break;
5027 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005028
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005029 case BINFLOAT:
5030 if (load_binfloat(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 BINSTRING:
5035 if (load_binstring(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 SHORT_BINSTRING:
5040 if (load_short_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 STRING:
5045 if (load_string(self) < 0)
5046 break;
5047 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005048
Martin v. Löwis339d0f72001-08-17 18:39:25 +00005049#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005050 case UNICODE:
5051 if (load_unicode(self) < 0)
5052 break;
5053 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00005054
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005055 case BINUNICODE:
5056 if (load_binunicode(self) < 0)
5057 break;
5058 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00005059#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00005060
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005061 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00005062 if (load_counted_tuple(self, 0) < 0)
5063 break;
5064 continue;
5065
5066 case TUPLE1:
5067 if (load_counted_tuple(self, 1) < 0)
5068 break;
5069 continue;
5070
5071 case TUPLE2:
5072 if (load_counted_tuple(self, 2) < 0)
5073 break;
5074 continue;
5075
5076 case TUPLE3:
5077 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005078 break;
5079 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005080
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005081 case TUPLE:
5082 if (load_tuple(self) < 0)
5083 break;
5084 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005085
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005086 case EMPTY_LIST:
5087 if (load_empty_list(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 LIST:
5092 if (load_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 EMPTY_DICT:
5097 if (load_empty_dict(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 DICT:
5102 if (load_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 OBJ:
5107 if (noload_obj(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 INST:
5112 if (noload_inst(self) < 0)
5113 break;
5114 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005115
Tim Peterseab7db32003-02-13 18:24:14 +00005116 case NEWOBJ:
5117 if (noload_newobj(self) < 0)
5118 break;
5119 continue;
5120
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005121 case GLOBAL:
5122 if (noload_global(self) < 0)
5123 break;
5124 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005125
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005126 case APPEND:
5127 if (load_append(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 APPENDS:
5132 if (load_appends(self) < 0)
5133 break;
5134 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005135
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005136 case BUILD:
5137 if (noload_build(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 DUP:
5142 if (load_dup(self) < 0)
5143 break;
5144 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005145
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005146 case BINGET:
5147 if (load_binget(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 LONG_BINGET:
5152 if (load_long_binget(self) < 0)
5153 break;
5154 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005155
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005156 case GET:
5157 if (load_get(self) < 0)
5158 break;
5159 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005160
Tim Peters2d629652003-02-04 05:06:17 +00005161 case EXT1:
5162 if (noload_extension(self, 1) < 0)
5163 break;
5164 continue;
5165
5166 case EXT2:
5167 if (noload_extension(self, 2) < 0)
5168 break;
5169 continue;
5170
5171 case EXT4:
5172 if (noload_extension(self, 4) < 0)
5173 break;
5174 continue;
5175
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005176 case MARK:
5177 if (load_mark(self) < 0)
5178 break;
5179 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005180
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005181 case BINPUT:
5182 if (load_binput(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 LONG_BINPUT:
5187 if (load_long_binput(self) < 0)
5188 break;
5189 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005190
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005191 case PUT:
5192 if (load_put(self) < 0)
5193 break;
5194 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005195
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005196 case POP:
5197 if (load_pop(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_MARK:
5202 if (load_pop_mark(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 SETITEM:
5207 if (load_setitem(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 SETITEMS:
5212 if (load_setitems(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 STOP:
5217 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005218
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005219 case PERSID:
5220 if (load_persid(self) < 0)
5221 break;
5222 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005223
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005224 case BINPERSID:
5225 if (load_binpersid(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 REDUCE:
5230 if (noload_reduce(self) < 0)
5231 break;
5232 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005233
Tim Peters4190fb82003-02-02 16:09:05 +00005234 case PROTO:
5235 if (load_proto(self) < 0)
5236 break;
5237 continue;
5238
Tim Peters3c67d792003-02-02 17:59:11 +00005239 case NEWTRUE:
5240 if (load_bool(self, Py_True) < 0)
5241 break;
5242 continue;
5243
5244 case NEWFALSE:
5245 if (load_bool(self, Py_False) < 0)
5246 break;
5247 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005248 default:
Tim Peterscba30e22003-02-01 06:24:36 +00005249 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005250 "invalid load key, '%s'.",
5251 "c", s[0]);
5252 return NULL;
5253 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005254
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005255 break;
5256 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005257
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005258 if ((err = PyErr_Occurred())) {
5259 if (err == PyExc_EOFError) {
5260 PyErr_SetNone(PyExc_EOFError);
5261 }
5262 return NULL;
5263 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005264
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005265 PDATA_POP(self->stack, val);
5266 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005267}
Tim Peters84e87f32001-03-17 04:50:51 +00005268
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005269
Guido van Rossum60456fd1997-04-09 17:36:32 +00005270static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005271Unpickler_load(Unpicklerobject *self, PyObject *unused)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005272{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005273 return load(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005274}
5275
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005276static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005277Unpickler_noload(Unpicklerobject *self, PyObject *unused)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005278{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005279 return noload(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005280}
5281
Guido van Rossum60456fd1997-04-09 17:36:32 +00005282
5283static struct PyMethodDef Unpickler_methods[] = {
Georg Brandl96a8c392006-05-29 21:04:52 +00005284 {"load", (PyCFunction)Unpickler_load, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005285 PyDoc_STR("load() -- Load a pickle")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005286 },
Georg Brandl96a8c392006-05-29 21:04:52 +00005287 {"noload", (PyCFunction)Unpickler_noload, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005288 PyDoc_STR(
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005289 "noload() -- not load a pickle, but go through most of the motions\n"
5290 "\n"
5291 "This function can be used to read past a pickle without instantiating\n"
5292 "any objects or importing any modules. It can also be used to find all\n"
5293 "persistent references without instantiating any objects or importing\n"
Neal Norwitz200788c2002-08-13 22:20:41 +00005294 "any modules.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005295 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00005296 {NULL, NULL} /* sentinel */
5297};
5298
5299
5300static Unpicklerobject *
Tim Peterscba30e22003-02-01 06:24:36 +00005301newUnpicklerobject(PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005302{
5303 Unpicklerobject *self;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005304
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005305 if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005306 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005307
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005308 self->file = NULL;
5309 self->arg = NULL;
5310 self->stack = (Pdata*)Pdata_New();
5311 self->pers_func = NULL;
5312 self->last_string = NULL;
5313 self->marks = NULL;
5314 self->num_marks = 0;
5315 self->marks_size = 0;
5316 self->buf_size = 0;
5317 self->read = NULL;
5318 self->readline = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005319 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005320
Tim Peterscba30e22003-02-01 06:24:36 +00005321 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005322 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005323
Neal Norwitzb59d08c2006-07-22 16:20:49 +00005324 if (!self->stack)
5325 goto err;
5326
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005327 Py_INCREF(f);
5328 self->file = f;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005329
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005330 /* Set read, readline based on type of f */
5331 if (PyFile_Check(f)) {
5332 self->fp = PyFile_AsFile(f);
5333 if (self->fp == NULL) {
Tim Peterscba30e22003-02-01 06:24:36 +00005334 PyErr_SetString(PyExc_ValueError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005335 "I/O operation on closed file");
5336 goto err;
5337 }
5338 self->read_func = read_file;
5339 self->readline_func = readline_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00005340 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005341 else if (PycStringIO_InputCheck(f)) {
5342 self->fp = NULL;
5343 self->read_func = read_cStringIO;
5344 self->readline_func = readline_cStringIO;
5345 }
5346 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00005347
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005348 self->fp = NULL;
5349 self->read_func = read_other;
5350 self->readline_func = readline_other;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005351
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005352 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5353 (self->read = PyObject_GetAttr(f, read_str)))) {
5354 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00005355 PyErr_SetString( PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005356 "argument must have 'read' and "
5357 "'readline' attributes" );
5358 goto err;
5359 }
5360 }
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005361 PyObject_GC_Track(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005362
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005363 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005364
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005365 err:
5366 Py_DECREF((PyObject *)self);
5367 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005368}
5369
5370
5371static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005372get_Unpickler(PyObject *self, PyObject *file)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005373{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005374 return (PyObject *)newUnpicklerobject(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005375}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005376
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005377
Guido van Rossum60456fd1997-04-09 17:36:32 +00005378static void
Tim Peterscba30e22003-02-01 06:24:36 +00005379Unpickler_dealloc(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005380{
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005381 PyObject_GC_UnTrack((PyObject *)self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005382 Py_XDECREF(self->readline);
5383 Py_XDECREF(self->read);
5384 Py_XDECREF(self->file);
5385 Py_XDECREF(self->memo);
5386 Py_XDECREF(self->stack);
5387 Py_XDECREF(self->pers_func);
5388 Py_XDECREF(self->arg);
5389 Py_XDECREF(self->last_string);
Jeremy Hyltonfff093f2003-07-11 19:42:49 +00005390 Py_XDECREF(self->find_class);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005391
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005392 if (self->marks) {
5393 free(self->marks);
5394 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005395
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005396 if (self->buf_size) {
5397 free(self->buf);
5398 }
Tim Peters84e87f32001-03-17 04:50:51 +00005399
Christian Heimese93237d2007-12-19 02:37:44 +00005400 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005401}
5402
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005403static int
5404Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg)
5405{
Thomas Woutersc6e55062006-04-15 21:47:09 +00005406 Py_VISIT(self->readline);
5407 Py_VISIT(self->read);
5408 Py_VISIT(self->file);
5409 Py_VISIT(self->memo);
5410 Py_VISIT(self->stack);
5411 Py_VISIT(self->pers_func);
5412 Py_VISIT(self->arg);
5413 Py_VISIT(self->last_string);
5414 Py_VISIT(self->find_class);
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005415 return 0;
5416}
5417
5418static int
5419Unpickler_clear(Unpicklerobject *self)
5420{
Thomas Woutersedf17d82006-04-15 17:28:34 +00005421 Py_CLEAR(self->readline);
5422 Py_CLEAR(self->read);
5423 Py_CLEAR(self->file);
5424 Py_CLEAR(self->memo);
5425 Py_CLEAR(self->stack);
5426 Py_CLEAR(self->pers_func);
5427 Py_CLEAR(self->arg);
5428 Py_CLEAR(self->last_string);
5429 Py_CLEAR(self->find_class);
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005430 return 0;
5431}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005432
5433static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005434Unpickler_getattr(Unpicklerobject *self, char *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005435{
5436 if (!strcmp(name, "persistent_load")) {
5437 if (!self->pers_func) {
5438 PyErr_SetString(PyExc_AttributeError, name);
5439 return NULL;
5440 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005441
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005442 Py_INCREF(self->pers_func);
5443 return self->pers_func;
5444 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005445
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005446 if (!strcmp(name, "find_global")) {
5447 if (!self->find_class) {
5448 PyErr_SetString(PyExc_AttributeError, name);
5449 return NULL;
5450 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005451
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005452 Py_INCREF(self->find_class);
5453 return self->find_class;
5454 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005455
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005456 if (!strcmp(name, "memo")) {
5457 if (!self->memo) {
5458 PyErr_SetString(PyExc_AttributeError, name);
5459 return NULL;
5460 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005461
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005462 Py_INCREF(self->memo);
5463 return self->memo;
5464 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005465
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005466 if (!strcmp(name, "UnpicklingError")) {
5467 Py_INCREF(UnpicklingError);
5468 return UnpicklingError;
5469 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005470
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005471 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005472}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005473
Guido van Rossum60456fd1997-04-09 17:36:32 +00005474
5475static int
Tim Peterscba30e22003-02-01 06:24:36 +00005476Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005477{
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005478
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005479 if (!strcmp(name, "persistent_load")) {
5480 Py_XDECREF(self->pers_func);
5481 self->pers_func = value;
5482 Py_XINCREF(value);
5483 return 0;
5484 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005485
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005486 if (!strcmp(name, "find_global")) {
5487 Py_XDECREF(self->find_class);
5488 self->find_class = value;
5489 Py_XINCREF(value);
5490 return 0;
5491 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005492
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005493 if (! value) {
5494 PyErr_SetString(PyExc_TypeError,
5495 "attribute deletion is not supported");
5496 return -1;
5497 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005498
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005499 if (strcmp(name, "memo") == 0) {
5500 if (!PyDict_Check(value)) {
Tim Peterscba30e22003-02-01 06:24:36 +00005501 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005502 "memo must be a dictionary");
5503 return -1;
5504 }
5505 Py_XDECREF(self->memo);
5506 self->memo = value;
5507 Py_INCREF(value);
5508 return 0;
5509 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005510
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005511 PyErr_SetString(PyExc_AttributeError, name);
5512 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005513}
5514
Tim Peters5bd2a792003-02-01 16:45:06 +00005515/* ---------------------------------------------------------------------------
5516 * Module-level functions.
5517 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005518
Martin v. Löwis544f1192004-07-27 05:22:33 +00005519/* dump(obj, file, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005520static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005521cpm_dump(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005522{
Martin v. Löwis15e62742006-02-27 16:46:16 +00005523 static char *kwlist[] = {"obj", "file", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005524 PyObject *ob, *file, *res = NULL;
5525 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005526 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005527
Martin v. Löwis544f1192004-07-27 05:22:33 +00005528 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist,
5529 &ob, &file, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005530 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005531
Tim Peters5bd2a792003-02-01 16:45:06 +00005532 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005533 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005534
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005535 if (dump(pickler, ob) < 0)
5536 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005537
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005538 Py_INCREF(Py_None);
5539 res = Py_None;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005540
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005541 finally:
5542 Py_XDECREF(pickler);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005543
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005544 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005545}
5546
5547
Martin v. Löwis544f1192004-07-27 05:22:33 +00005548/* dumps(obj, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005549static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005550cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005551{
Martin v. Löwis15e62742006-02-27 16:46:16 +00005552 static char *kwlist[] = {"obj", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005553 PyObject *ob, *file = 0, *res = NULL;
5554 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005555 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005556
Martin v. Löwis544f1192004-07-27 05:22:33 +00005557 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist,
5558 &ob, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005559 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005560
Tim Peterscba30e22003-02-01 06:24:36 +00005561 if (!( file = PycStringIO->NewOutput(128)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005562 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005563
Tim Peters5bd2a792003-02-01 16:45:06 +00005564 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005565 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005566
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005567 if (dump(pickler, ob) < 0)
5568 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005569
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005570 res = PycStringIO->cgetvalue(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005571
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005572 finally:
5573 Py_XDECREF(pickler);
5574 Py_XDECREF(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005575
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005576 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00005577}
5578
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005579
Tim Peters5bd2a792003-02-01 16:45:06 +00005580/* load(fileobj). */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005581static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005582cpm_load(PyObject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005583{
5584 Unpicklerobject *unpickler = 0;
Georg Brandl96a8c392006-05-29 21:04:52 +00005585 PyObject *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005586
Tim Peterscba30e22003-02-01 06:24:36 +00005587 if (!( unpickler = newUnpicklerobject(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005588 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005589
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005590 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005591
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005592 finally:
5593 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005594
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005595 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005596}
5597
5598
Tim Peters5bd2a792003-02-01 16:45:06 +00005599/* loads(string) */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005600static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005601cpm_loads(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005602{
5603 PyObject *ob, *file = 0, *res = NULL;
5604 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005605
Tim Peterscba30e22003-02-01 06:24:36 +00005606 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005607 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005608
Tim Peterscba30e22003-02-01 06:24:36 +00005609 if (!( file = PycStringIO->NewInput(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005610 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00005611
Tim Peterscba30e22003-02-01 06:24:36 +00005612 if (!( unpickler = newUnpicklerobject(file)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005613 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005614
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005615 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005616
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005617 finally:
5618 Py_XDECREF(file);
5619 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005620
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005621 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005622}
5623
5624
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005625PyDoc_STRVAR(Unpicklertype__doc__,
5626"Objects that know how to unpickle");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005627
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005628static PyTypeObject Unpicklertype = {
Martin v. Löwis68192102007-07-21 06:55:02 +00005629 PyVarObject_HEAD_INIT(NULL, 0)
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005630 "cPickle.Unpickler", /*tp_name*/
5631 sizeof(Unpicklerobject), /*tp_basicsize*/
5632 0,
5633 (destructor)Unpickler_dealloc, /* tp_dealloc */
5634 0, /* tp_print */
5635 (getattrfunc)Unpickler_getattr, /* tp_getattr */
5636 (setattrfunc)Unpickler_setattr, /* tp_setattr */
5637 0, /* tp_compare */
5638 0, /* tp_repr */
5639 0, /* tp_as_number */
5640 0, /* tp_as_sequence */
5641 0, /* tp_as_mapping */
5642 0, /* tp_hash */
5643 0, /* tp_call */
5644 0, /* tp_str */
5645 0, /* tp_getattro */
5646 0, /* tp_setattro */
5647 0, /* tp_as_buffer */
5648 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
5649 Unpicklertype__doc__, /* tp_doc */
5650 (traverseproc)Unpickler_traverse, /* tp_traverse */
5651 (inquiry)Unpickler_clear, /* tp_clear */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005652};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005653
Guido van Rossum60456fd1997-04-09 17:36:32 +00005654static struct PyMethodDef cPickle_methods[] = {
Martin v. Löwis544f1192004-07-27 05:22:33 +00005655 {"dump", (PyCFunction)cpm_dump, METH_VARARGS | METH_KEYWORDS,
5656 PyDoc_STR("dump(obj, file, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005657 "Write an object in pickle format to the given file.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005658 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005659 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005660 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005661
Martin v. Löwis544f1192004-07-27 05:22:33 +00005662 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS | METH_KEYWORDS,
5663 PyDoc_STR("dumps(obj, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005664 "Return a string containing an object in pickle format.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005665 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005666 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005667 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005668
Georg Brandl96a8c392006-05-29 21:04:52 +00005669 {"load", (PyCFunction)cpm_load, METH_O,
Neal Norwitz200788c2002-08-13 22:20:41 +00005670 PyDoc_STR("load(file) -- Load a pickle from the given file")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005671
Neal Norwitzb0493252002-03-31 14:44:22 +00005672 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005673 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005674
Martin v. Löwis544f1192004-07-27 05:22:33 +00005675 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS | METH_KEYWORDS,
5676 PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005677 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005678 "This takes a file-like object for writing a pickle data stream.\n"
5679 "The optional proto argument tells the pickler to use the given\n"
5680 "protocol; supported protocols are 0, 1, 2. The default\n"
5681 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5682 "only protocol that can be written to a file opened in text\n"
5683 "mode and read back successfully. When using a protocol higher\n"
5684 "than 0, make sure the file is opened in binary mode, both when\n"
5685 "pickling and unpickling.)\n"
5686 "\n"
5687 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5688 "more efficient than protocol 1.\n"
5689 "\n"
5690 "Specifying a negative protocol version selects the highest\n"
5691 "protocol version supported. The higher the protocol used, the\n"
5692 "more recent the version of Python needed to read the pickle\n"
5693 "produced.\n"
5694 "\n"
5695 "The file parameter must have a write() method that accepts a single\n"
5696 "string argument. It can thus be an open file object, a StringIO\n"
5697 "object, or any other custom object that meets this interface.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005698 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005699
Georg Brandl96a8c392006-05-29 21:04:52 +00005700 {"Unpickler", (PyCFunction)get_Unpickler, METH_O,
Tim Peters5bd2a792003-02-01 16:45:06 +00005701 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5702
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005703 { NULL, NULL }
5704};
5705
Guido van Rossum60456fd1997-04-09 17:36:32 +00005706static int
Tim Peterscba30e22003-02-01 06:24:36 +00005707init_stuff(PyObject *module_dict)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005708{
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005709 PyObject *copyreg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005710
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005711#define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005712
Tim Peters3cfe7542003-05-21 21:29:48 +00005713 if (PyType_Ready(&Unpicklertype) < 0)
5714 return -1;
5715 if (PyType_Ready(&Picklertype) < 0)
5716 return -1;
5717
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005718 INIT_STR(__class__);
5719 INIT_STR(__getinitargs__);
5720 INIT_STR(__dict__);
5721 INIT_STR(__getstate__);
5722 INIT_STR(__setstate__);
5723 INIT_STR(__name__);
5724 INIT_STR(__main__);
5725 INIT_STR(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00005726 INIT_STR(__reduce_ex__);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005727 INIT_STR(write);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005728 INIT_STR(append);
5729 INIT_STR(read);
5730 INIT_STR(readline);
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005731 INIT_STR(copyreg);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005732 INIT_STR(dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005733
Georg Brandldffbf5f2008-05-20 07:49:57 +00005734 if (!( copyreg = PyImport_ImportModule("copy_reg")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005735 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005736
Tim Peters1f1b2d22003-02-01 02:16:37 +00005737 /* This is special because we want to use a different
5738 one in restricted mode. */
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005739 dispatch_table = PyObject_GetAttr(copyreg, dispatch_table_str);
Tim Peters5b7da392003-02-04 00:21:07 +00005740 if (!dispatch_table) return -1;
5741
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005742 extension_registry = PyObject_GetAttrString(copyreg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005743 "_extension_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005744 if (!extension_registry) return -1;
5745
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005746 inverted_registry = PyObject_GetAttrString(copyreg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005747 "_inverted_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005748 if (!inverted_registry) return -1;
5749
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005750 extension_cache = PyObject_GetAttrString(copyreg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005751 "_extension_cache");
Tim Peters5b7da392003-02-04 00:21:07 +00005752 if (!extension_cache) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005753
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005754 Py_DECREF(copyreg);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005755
Tim Peters731098b2003-02-04 20:56:09 +00005756 if (!(empty_tuple = PyTuple_New(0)))
5757 return -1;
5758
5759 two_tuple = PyTuple_New(2);
5760 if (two_tuple == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005761 return -1;
Tim Peters3e667d52003-02-04 21:47:44 +00005762 /* We use this temp container with no regard to refcounts, or to
5763 * keeping containees alive. Exempt from GC, because we don't
5764 * want anything looking at two_tuple() by magic.
5765 */
5766 PyObject_GC_UnTrack(two_tuple);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005767
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005768 /* Ugh */
5769 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5770 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5771 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005772
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005773 if (!( t=PyDict_New())) return -1;
5774 if (!( r=PyRun_String(
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005775 "def __str__(self):\n"
5776 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5777 Py_file_input,
5778 module_dict, t) )) return -1;
5779 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005780
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005781 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
Tim Peterscba30e22003-02-01 06:24:36 +00005782 if (!PickleError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005783 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005784
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005785 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005786
Tim Peterscba30e22003-02-01 06:24:36 +00005787 PicklingError = PyErr_NewException("cPickle.PicklingError",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005788 PickleError, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00005789 if (!PicklingError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005790 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005791
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005792 if (!( t=PyDict_New())) return -1;
5793 if (!( r=PyRun_String(
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005794 "def __str__(self):\n"
5795 " a=self.args\n"
5796 " a=a and type(a[0]) or '(what)'\n"
5797 " return 'Cannot pickle %s objects' % a\n"
5798 , Py_file_input,
5799 module_dict, t) )) return -1;
5800 Py_DECREF(r);
Tim Peters84e87f32001-03-17 04:50:51 +00005801
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005802 if (!( UnpickleableError = PyErr_NewException(
Tim Peterscba30e22003-02-01 06:24:36 +00005803 "cPickle.UnpickleableError", PicklingError, t)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005804 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005805
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005806 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005807
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005808 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
Tim Peterscba30e22003-02-01 06:24:36 +00005809 PickleError, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005810 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005811
Martin v. Löwis658009a2002-09-16 17:26:24 +00005812 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5813 UnpicklingError, NULL)))
5814 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +00005815
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005816 if (PyDict_SetItemString(module_dict, "PickleError",
5817 PickleError) < 0)
5818 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005819
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005820 if (PyDict_SetItemString(module_dict, "PicklingError",
5821 PicklingError) < 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, "UnpicklingError",
5825 UnpicklingError) < 0)
5826 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005827
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005828 if (PyDict_SetItemString(module_dict, "UnpickleableError",
5829 UnpickleableError) < 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, "BadPickleGet",
5833 BadPickleGet) < 0)
5834 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005835
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005836 PycString_IMPORT;
Guido van Rossum053b8df1998-11-25 16:18:00 +00005837
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005838 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005839}
5840
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005841#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5842#define PyMODINIT_FUNC void
Guido van Rossumf9ffb031999-02-04 14:54:04 +00005843#endif
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005844PyMODINIT_FUNC
Tim Peterscba30e22003-02-01 06:24:36 +00005845initcPickle(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005846{
5847 PyObject *m, *d, *di, *v, *k;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005848 Py_ssize_t i;
Tim Peters5b7da392003-02-04 00:21:07 +00005849 char *rev = "1.71"; /* XXX when does this change? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005850 PyObject *format_version;
5851 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005852
Alexandre Vassalotti3c4971c2008-05-16 19:14:31 +00005853 /* XXX: Should mention that the pickle module will include the C
5854 XXX: optimized implementation automatically. */
5855 if (PyErr_WarnPy3k("the cPickle module has been removed in "
5856 "Python 3.0", 2) < 0)
5857 return;
5858
Christian Heimese93237d2007-12-19 02:37:44 +00005859 Py_TYPE(&Picklertype) = &PyType_Type;
5860 Py_TYPE(&Unpicklertype) = &PyType_Type;
5861 Py_TYPE(&PdataType) = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005862
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005863 /* Initialize some pieces. We need to do this before module creation,
Tim Peters5b7da392003-02-04 00:21:07 +00005864 * so we're forced to use a temporary dictionary. :(
5865 */
5866 di = PyDict_New();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005867 if (!di) return;
5868 if (init_stuff(di) < 0) return;
Guido van Rossumebba4202000-09-07 14:35:37 +00005869
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005870 /* Create the module and add the functions */
5871 m = Py_InitModule4("cPickle", cPickle_methods,
5872 cPickle_module_documentation,
5873 (PyObject*)NULL,PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00005874 if (m == NULL)
5875 return;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005876
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005877 /* Add some symbolic constants to the module */
5878 d = PyModule_GetDict(m);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005879 v = PyString_FromString(rev);
Tim Peters5b7da392003-02-04 00:21:07 +00005880 PyDict_SetItemString(d, "__version__", v);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005881 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00005882
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005883 /* Copy data from di. Waaa. */
5884 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
5885 if (PyObject_SetItem(d, k, v) < 0) {
5886 Py_DECREF(di);
5887 return;
5888 }
5889 }
5890 Py_DECREF(di);
Guido van Rossumebba4202000-09-07 14:35:37 +00005891
Tim Peters8587b3c2003-02-13 15:44:41 +00005892 i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
5893 if (i < 0)
5894 return;
5895
Tim Peters5b7da392003-02-04 00:21:07 +00005896 /* These are purely informational; no code uses them. */
5897 /* File format version we write. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005898 format_version = PyString_FromString("2.0");
Tim Peters5b7da392003-02-04 00:21:07 +00005899 /* Format versions we can read. */
5900 compatible_formats = Py_BuildValue("[sssss]",
5901 "1.0", /* Original protocol 0 */
5902 "1.1", /* Protocol 0 + INST */
5903 "1.2", /* Original protocol 1 */
5904 "1.3", /* Protocol 1 + BINFLOAT */
Tim Peters0dd23aa2003-02-04 00:30:46 +00005905 "2.0"); /* Original protocol 2 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005906 PyDict_SetItemString(d, "format_version", format_version);
5907 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
5908 Py_XDECREF(format_version);
5909 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005910}