Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | #include "cStringIO.h" |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3 | #include "structmember.h" |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5 | PyDoc_STRVAR(cPickle_module_documentation, |
Tim Peters | 64c04d1 | 2003-02-01 06:27:59 +0000 | [diff] [blame] | 6 | "C implementation and optimization of the Python pickle module."); |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 8 | #ifndef Py_eval_input |
| 9 | #include <graminit.h> |
| 10 | #define Py_eval_input eval_input |
Guido van Rossum | c6ef204 | 1997-08-21 02:30:45 +0000 | [diff] [blame] | 11 | #endif /* Py_eval_input */ |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 13 | #define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL)) |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 15 | #define WRITE_BUF_SIZE 256 |
| 16 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 17 | /* Bump this when new opcodes are added to the pickle protocol. */ |
Tim Peters | 8587b3c | 2003-02-13 15:44:41 +0000 | [diff] [blame] | 18 | #define HIGHEST_PROTOCOL 2 |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 19 | |
Tim Peters | 797ec24 | 2003-02-01 06:22:36 +0000 | [diff] [blame] | 20 | /* |
| 21 | * Pickle opcodes. These must be kept in synch with pickle.py. Extensive |
| 22 | * docs are in pickletools.py. |
| 23 | */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 24 | #define MARK '(' |
| 25 | #define STOP '.' |
| 26 | #define POP '0' |
| 27 | #define POP_MARK '1' |
| 28 | #define DUP '2' |
| 29 | #define FLOAT 'F' |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 30 | #define BINFLOAT 'G' |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 31 | #define INT 'I' |
| 32 | #define BININT 'J' |
| 33 | #define BININT1 'K' |
| 34 | #define LONG 'L' |
| 35 | #define BININT2 'M' |
| 36 | #define NONE 'N' |
| 37 | #define PERSID 'P' |
| 38 | #define BINPERSID 'Q' |
| 39 | #define REDUCE 'R' |
| 40 | #define STRING 'S' |
| 41 | #define BINSTRING 'T' |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 42 | #define SHORT_BINSTRING 'U' |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 43 | #define UNICODE 'V' |
| 44 | #define BINUNICODE 'X' |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 45 | #define APPEND 'a' |
| 46 | #define BUILD 'b' |
| 47 | #define GLOBAL 'c' |
| 48 | #define DICT 'd' |
| 49 | #define EMPTY_DICT '}' |
| 50 | #define APPENDS 'e' |
| 51 | #define GET 'g' |
| 52 | #define BINGET 'h' |
| 53 | #define INST 'i' |
| 54 | #define LONG_BINGET 'j' |
| 55 | #define LIST 'l' |
| 56 | #define EMPTY_LIST ']' |
| 57 | #define OBJ 'o' |
| 58 | #define PUT 'p' |
| 59 | #define BINPUT 'q' |
| 60 | #define LONG_BINPUT 'r' |
| 61 | #define SETITEM 's' |
| 62 | #define TUPLE 't' |
| 63 | #define EMPTY_TUPLE ')' |
| 64 | #define SETITEMS 'u' |
Tim Peters | 797ec24 | 2003-02-01 06:22:36 +0000 | [diff] [blame] | 65 | |
| 66 | /* Protocol 2. */ |
| 67 | #define PROTO '\x80' /* identify pickle protocol */ |
| 68 | #define NEWOBJ '\x81' /* build object by applying cls.__new__ to argtuple */ |
| 69 | #define EXT1 '\x82' /* push object from extension registry; 1-byte index */ |
| 70 | #define EXT2 '\x83' /* ditto, but 2-byte index */ |
| 71 | #define EXT4 '\x84' /* ditto, but 4-byte index */ |
| 72 | #define TUPLE1 '\x85' /* build 1-tuple from stack top */ |
| 73 | #define TUPLE2 '\x86' /* build 2-tuple from two topmost stack items */ |
| 74 | #define TUPLE3 '\x87' /* build 3-tuple from three topmost stack items */ |
| 75 | #define NEWTRUE '\x88' /* push True */ |
| 76 | #define NEWFALSE '\x89' /* push False */ |
| 77 | #define LONG1 '\x8a' /* push long from < 256 bytes */ |
| 78 | #define LONG4 '\x8b' /* push really big long */ |
| 79 | |
| 80 | /* There aren't opcodes -- they're ways to pickle bools before protocol 2, |
| 81 | * so that unpicklers written before bools were introduced unpickle them |
| 82 | * as ints, but unpicklers after can recognize that bools were intended. |
| 83 | * Note that protocol 2 added direct ways to pickle bools. |
| 84 | */ |
Jack Jansen | 3a96702 | 2002-06-26 20:40:42 +0000 | [diff] [blame] | 85 | #undef TRUE |
Guido van Rossum | e276339 | 2002-04-05 19:30:08 +0000 | [diff] [blame] | 86 | #define TRUE "I01\n" |
Jack Jansen | 3a96702 | 2002-06-26 20:40:42 +0000 | [diff] [blame] | 87 | #undef FALSE |
Guido van Rossum | e276339 | 2002-04-05 19:30:08 +0000 | [diff] [blame] | 88 | #define FALSE "I00\n" |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 89 | |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 90 | /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 91 | * batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will |
| 92 | * break if this gets out of synch with pickle.py, but it's unclear that |
| 93 | * would help anything either. |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 94 | */ |
| 95 | #define BATCHSIZE 1000 |
| 96 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 97 | static char MARKv = MARK; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 98 | |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 99 | static PyObject *PickleError; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 100 | static PyObject *PicklingError; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 101 | static PyObject *UnpickleableError; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 102 | static PyObject *UnpicklingError; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 103 | static PyObject *BadPickleGet; |
| 104 | |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 105 | /* As the name says, an empty tuple. */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 106 | static PyObject *empty_tuple; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 107 | |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 108 | /* copy_reg.dispatch_table, {type_object: pickling_function} */ |
| 109 | static PyObject *dispatch_table; |
| 110 | |
| 111 | /* For EXT[124] opcodes. */ |
Guido van Rossum | d4b920c | 2003-02-04 01:54:49 +0000 | [diff] [blame] | 112 | /* copy_reg._extension_registry, {(module_name, function_name): code} */ |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 113 | static PyObject *extension_registry; |
Guido van Rossum | d4b920c | 2003-02-04 01:54:49 +0000 | [diff] [blame] | 114 | /* copy_reg._inverted_registry, {code: (module_name, function_name)} */ |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 115 | static PyObject *inverted_registry; |
Guido van Rossum | d4b920c | 2003-02-04 01:54:49 +0000 | [diff] [blame] | 116 | /* copy_reg._extension_cache, {code: object} */ |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 117 | static PyObject *extension_cache; |
| 118 | |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 119 | /* For looking up name pairs in copy_reg._extension_registry. */ |
| 120 | static PyObject *two_tuple; |
| 121 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 122 | static PyObject *__class___str, *__getinitargs___str, *__dict___str, |
| 123 | *__getstate___str, *__setstate___str, *__name___str, *__reduce___str, |
Guido van Rossum | b289b87 | 2003-02-19 01:45:13 +0000 | [diff] [blame] | 124 | *__reduce_ex___str, |
Tim Peters | 1f1b2d2 | 2003-02-01 02:16:37 +0000 | [diff] [blame] | 125 | *write_str, *append_str, |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 126 | *read_str, *readline_str, *__main___str, *__basicnew___str, |
Tim Peters | 1f1b2d2 | 2003-02-01 02:16:37 +0000 | [diff] [blame] | 127 | *copy_reg_str, *dispatch_table_str; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 128 | |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 129 | /************************************************************************* |
| 130 | Internal Data type for pickle data. */ |
| 131 | |
| 132 | typedef struct { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 133 | PyObject_HEAD |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 134 | int length; /* number of initial slots in data currently used */ |
| 135 | int size; /* number of slots in data allocated */ |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 136 | PyObject **data; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 137 | } Pdata; |
| 138 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 139 | static void |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 140 | Pdata_dealloc(Pdata *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 141 | { |
| 142 | int i; |
| 143 | PyObject **p; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 144 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 145 | for (i = self->length, p = self->data; --i >= 0; p++) { |
| 146 | Py_DECREF(*p); |
| 147 | } |
| 148 | if (self->data) |
| 149 | free(self->data); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 150 | PyObject_Del(self); |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static PyTypeObject PdataType = { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 154 | PyObject_HEAD_INIT(NULL) 0, "cPickle.Pdata", sizeof(Pdata), 0, |
| 155 | (destructor)Pdata_dealloc, |
| 156 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, "" |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | #define Pdata_Check(O) ((O)->ob_type == &PdataType) |
| 160 | |
| 161 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 162 | Pdata_New(void) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 163 | { |
| 164 | Pdata *self; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 165 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 166 | if (!(self = PyObject_New(Pdata, &PdataType))) |
| 167 | return NULL; |
| 168 | self->size = 8; |
| 169 | self->length = 0; |
| 170 | self->data = malloc(self->size * sizeof(PyObject*)); |
| 171 | if (self->data) |
| 172 | return (PyObject*)self; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 173 | Py_DECREF(self); |
| 174 | return PyErr_NoMemory(); |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 177 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 178 | stackUnderflow(void) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 179 | { |
| 180 | PyErr_SetString(UnpicklingError, "unpickling stack underflow"); |
| 181 | return -1; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 184 | /* Retain only the initial clearto items. If clearto >= the current |
| 185 | * number of items, this is a (non-erroneous) NOP. |
| 186 | */ |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 187 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 188 | Pdata_clear(Pdata *self, int clearto) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 189 | { |
| 190 | int i; |
| 191 | PyObject **p; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 192 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 193 | if (clearto < 0) return stackUnderflow(); |
| 194 | if (clearto >= self->length) return 0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 195 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 196 | for (i = self->length, p = self->data + clearto; |
| 197 | --i >= clearto; |
| 198 | p++) { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 199 | Py_DECREF(*p); |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 200 | } |
| 201 | self->length = clearto; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 202 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 203 | return 0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 206 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 207 | Pdata_grow(Pdata *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 208 | { |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 209 | int bigger; |
| 210 | size_t nbytes; |
| 211 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 212 | bigger = self->size << 1; |
Tim Peters | e0a3907 | 2003-02-03 15:45:56 +0000 | [diff] [blame] | 213 | if (bigger <= 0) /* was 0, or new value overflows */ |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 214 | goto nomemory; |
| 215 | if ((int)(size_t)bigger != bigger) |
| 216 | goto nomemory; |
| 217 | nbytes = (size_t)bigger * sizeof(PyObject *); |
| 218 | if (nbytes / sizeof(PyObject *) != (size_t)bigger) |
| 219 | goto nomemory; |
| 220 | self->data = realloc(self->data, nbytes); |
| 221 | if (self->data == NULL) |
| 222 | goto nomemory; |
| 223 | self->size = bigger; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 224 | return 0; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 225 | |
| 226 | nomemory: |
| 227 | self->size = 0; |
| 228 | PyErr_NoMemory(); |
| 229 | return -1; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 230 | } |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 231 | |
Tim Peters | e0a3907 | 2003-02-03 15:45:56 +0000 | [diff] [blame] | 232 | /* D is a Pdata*. Pop the topmost element and store it into V, which |
| 233 | * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 234 | * is raised and V is set to NULL. D and V may be evaluated several times. |
| 235 | */ |
| 236 | #define PDATA_POP(D, V) { \ |
Tim Peters | e0a3907 | 2003-02-03 15:45:56 +0000 | [diff] [blame] | 237 | if ((D)->length) \ |
| 238 | (V) = (D)->data[--((D)->length)]; \ |
| 239 | else { \ |
| 240 | PyErr_SetString(UnpicklingError, "bad pickle data"); \ |
| 241 | (V) = NULL; \ |
| 242 | } \ |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Tim Peters | e0a3907 | 2003-02-03 15:45:56 +0000 | [diff] [blame] | 245 | /* PDATA_PUSH and PDATA_APPEND both push rvalue PyObject* O on to Pdata* |
| 246 | * D. If the Pdata stack can't be grown to hold the new value, both |
| 247 | * raise MemoryError and execute "return ER". The difference is in ownership |
| 248 | * of O after: _PUSH transfers ownership of O from the caller to the stack |
| 249 | * (no incref of O is done, and in case of error O is decrefed), while |
| 250 | * _APPEND pushes a new reference. |
| 251 | */ |
| 252 | |
| 253 | /* Push O on stack D, giving ownership of O to the stack. */ |
| 254 | #define PDATA_PUSH(D, O, ER) { \ |
| 255 | if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \ |
| 256 | Pdata_grow((Pdata*)(D)) < 0) { \ |
| 257 | Py_DECREF(O); \ |
| 258 | return ER; \ |
| 259 | } \ |
| 260 | ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \ |
| 261 | } |
| 262 | |
| 263 | /* Push O on stack D, pushing a new reference. */ |
| 264 | #define PDATA_APPEND(D, O, ER) { \ |
| 265 | if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \ |
| 266 | Pdata_grow((Pdata*)(D)) < 0) \ |
| 267 | return ER; \ |
| 268 | Py_INCREF(O); \ |
| 269 | ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \ |
| 270 | } |
| 271 | |
| 272 | |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 273 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 274 | Pdata_popTuple(Pdata *self, int start) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 275 | { |
| 276 | PyObject *r; |
| 277 | int i, j, l; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 278 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 279 | l = self->length-start; |
| 280 | r = PyTuple_New(l); |
| 281 | if (r == NULL) |
| 282 | return NULL; |
| 283 | for (i = start, j = 0 ; j < l; i++, j++) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 284 | PyTuple_SET_ITEM(r, j, self->data[i]); |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 285 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 286 | self->length = start; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 287 | return r; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 291 | Pdata_popList(Pdata *self, int start) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 292 | { |
| 293 | PyObject *r; |
| 294 | int i, j, l; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 295 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 296 | l=self->length-start; |
| 297 | if (!( r=PyList_New(l))) return NULL; |
| 298 | for (i=start, j=0 ; j < l; i++, j++) |
| 299 | PyList_SET_ITEM(r, j, self->data[i]); |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 300 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 301 | self->length=start; |
| 302 | return r; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 305 | /*************************************************************************/ |
| 306 | |
| 307 | #define ARG_TUP(self, o) { \ |
| 308 | if (self->arg || (self->arg=PyTuple_New(1))) { \ |
| 309 | Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \ |
| 310 | PyTuple_SET_ITEM(self->arg,0,o); \ |
| 311 | } \ |
| 312 | else { \ |
| 313 | Py_DECREF(o); \ |
| 314 | } \ |
| 315 | } |
| 316 | |
| 317 | #define FREE_ARG_TUP(self) { \ |
| 318 | if (self->arg->ob_refcnt > 1) { \ |
| 319 | Py_DECREF(self->arg); \ |
| 320 | self->arg=NULL; \ |
| 321 | } \ |
| 322 | } |
| 323 | |
Thomas Wouters | 3b6448f | 2000-07-22 23:56:07 +0000 | [diff] [blame] | 324 | typedef struct Picklerobject { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 325 | PyObject_HEAD |
| 326 | FILE *fp; |
| 327 | PyObject *write; |
| 328 | PyObject *file; |
| 329 | PyObject *memo; |
| 330 | PyObject *arg; |
| 331 | PyObject *pers_func; |
| 332 | PyObject *inst_pers_func; |
Tim Peters | 797ec24 | 2003-02-01 06:22:36 +0000 | [diff] [blame] | 333 | |
| 334 | /* pickle protocol number, >= 0 */ |
| 335 | int proto; |
| 336 | |
| 337 | /* bool, true if proto > 0 */ |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 338 | int bin; |
Tim Peters | 797ec24 | 2003-02-01 06:22:36 +0000 | [diff] [blame] | 339 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 340 | int fast; /* Fast mode doesn't save in memo, don't use if circ ref */ |
Martin v. Löwis | 5a39530 | 2002-08-04 08:20:23 +0000 | [diff] [blame] | 341 | int nesting; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 342 | int (*write_func)(struct Picklerobject *, char *, int); |
| 343 | char *write_buf; |
| 344 | int buf_size; |
| 345 | PyObject *dispatch_table; |
| 346 | int fast_container; /* count nested container dumps */ |
| 347 | PyObject *fast_memo; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 348 | } Picklerobject; |
| 349 | |
Barry Warsaw | 52acb49 | 2001-12-21 20:04:22 +0000 | [diff] [blame] | 350 | #ifndef PY_CPICKLE_FAST_LIMIT |
| 351 | #define PY_CPICKLE_FAST_LIMIT 50 |
| 352 | #endif |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 353 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 354 | static PyTypeObject Picklertype; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 355 | |
Thomas Wouters | 3b6448f | 2000-07-22 23:56:07 +0000 | [diff] [blame] | 356 | typedef struct Unpicklerobject { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 357 | PyObject_HEAD |
| 358 | FILE *fp; |
| 359 | PyObject *file; |
| 360 | PyObject *readline; |
| 361 | PyObject *read; |
| 362 | PyObject *memo; |
| 363 | PyObject *arg; |
| 364 | Pdata *stack; |
| 365 | PyObject *mark; |
| 366 | PyObject *pers_func; |
| 367 | PyObject *last_string; |
| 368 | int *marks; |
| 369 | int num_marks; |
| 370 | int marks_size; |
| 371 | int (*read_func)(struct Unpicklerobject *, char **, int); |
| 372 | int (*readline_func)(struct Unpicklerobject *, char **); |
| 373 | int buf_size; |
| 374 | char *buf; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 375 | PyObject *find_class; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 376 | } Unpicklerobject; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 377 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 378 | static PyTypeObject Unpicklertype; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 379 | |
Thomas Wouters | 4789b3a | 2000-07-24 11:36:47 +0000 | [diff] [blame] | 380 | /* Forward decls that need the above structs */ |
| 381 | static int save(Picklerobject *, PyObject *, int); |
| 382 | static int put2(Picklerobject *, PyObject *); |
| 383 | |
Guido van Rossum | d385d59 | 1997-04-09 17:47:47 +0000 | [diff] [blame] | 384 | static |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 385 | PyObject * |
Thomas Wouters | 3b6448f | 2000-07-22 23:56:07 +0000 | [diff] [blame] | 386 | cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...) |
| 387 | { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 388 | va_list va; |
| 389 | PyObject *args=0, *retval=0; |
| 390 | va_start(va, format); |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 391 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 392 | if (format) args = Py_VaBuildValue(format, va); |
| 393 | va_end(va); |
| 394 | if (format && ! args) return NULL; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 395 | if (stringformat && !(retval=PyString_FromString(stringformat))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 396 | return NULL; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 397 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 398 | if (retval) { |
| 399 | if (args) { |
| 400 | PyObject *v; |
| 401 | v=PyString_Format(retval, args); |
| 402 | Py_DECREF(retval); |
| 403 | Py_DECREF(args); |
| 404 | if (! v) return NULL; |
| 405 | retval=v; |
| 406 | } |
| 407 | } |
| 408 | else |
| 409 | if (args) retval=args; |
| 410 | else { |
| 411 | PyErr_SetObject(ErrType,Py_None); |
| 412 | return NULL; |
| 413 | } |
| 414 | PyErr_SetObject(ErrType,retval); |
| 415 | Py_DECREF(retval); |
| 416 | return NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 419 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 420 | write_file(Picklerobject *self, char *s, int n) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 421 | { |
| 422 | size_t nbyteswritten; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 423 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 424 | if (s == NULL) { |
| 425 | return 0; |
| 426 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 427 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 428 | Py_BEGIN_ALLOW_THREADS |
Jeremy Hylton | 7fa4bfa | 2002-07-18 20:58:57 +0000 | [diff] [blame] | 429 | nbyteswritten = fwrite(s, sizeof(char), n, self->fp); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 430 | Py_END_ALLOW_THREADS |
Jeremy Hylton | 7fa4bfa | 2002-07-18 20:58:57 +0000 | [diff] [blame] | 431 | if (nbyteswritten != (size_t)n) { |
| 432 | PyErr_SetFromErrno(PyExc_IOError); |
| 433 | return -1; |
| 434 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 435 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 436 | return n; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 439 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 440 | write_cStringIO(Picklerobject *self, char *s, int n) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 441 | { |
| 442 | if (s == NULL) { |
| 443 | return 0; |
| 444 | } |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 445 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 446 | if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) { |
| 447 | return -1; |
| 448 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 449 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 450 | return n; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 453 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 454 | write_none(Picklerobject *self, char *s, int n) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 455 | { |
| 456 | if (s == NULL) return 0; |
| 457 | return n; |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 460 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 461 | write_other(Picklerobject *self, char *s, int n) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 462 | { |
| 463 | PyObject *py_str = 0, *junk = 0; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 464 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 465 | if (s == NULL) { |
| 466 | if (!( self->buf_size )) return 0; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 467 | py_str = PyString_FromStringAndSize(self->write_buf, |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 468 | self->buf_size); |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 469 | if (!py_str) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 470 | return -1; |
| 471 | } |
| 472 | else { |
| 473 | if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) { |
| 474 | if (write_other(self, NULL, 0) < 0) |
| 475 | return -1; |
| 476 | } |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 477 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 478 | if (n > WRITE_BUF_SIZE) { |
| 479 | if (!( py_str = |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 480 | PyString_FromStringAndSize(s, n))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 481 | return -1; |
| 482 | } |
| 483 | else { |
| 484 | memcpy(self->write_buf + self->buf_size, s, n); |
| 485 | self->buf_size += n; |
| 486 | return n; |
| 487 | } |
| 488 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 489 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 490 | if (self->write) { |
| 491 | /* object with write method */ |
| 492 | ARG_TUP(self, py_str); |
| 493 | if (self->arg) { |
| 494 | junk = PyObject_Call(self->write, self->arg, NULL); |
| 495 | FREE_ARG_TUP(self); |
| 496 | } |
| 497 | if (junk) Py_DECREF(junk); |
| 498 | else return -1; |
| 499 | } |
| 500 | else |
| 501 | PDATA_PUSH(self->file, py_str, -1); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 502 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 503 | self->buf_size = 0; |
| 504 | return n; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 508 | static int |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 509 | read_file(Unpicklerobject *self, char **s, int n) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 510 | { |
| 511 | size_t nbytesread; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 512 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 513 | if (self->buf_size == 0) { |
| 514 | int size; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 515 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 516 | size = ((n < 32) ? 32 : n); |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 517 | if (!( self->buf = (char *)malloc(size))) { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 518 | PyErr_NoMemory(); |
| 519 | return -1; |
| 520 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 521 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 522 | self->buf_size = size; |
| 523 | } |
| 524 | else if (n > self->buf_size) { |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 525 | self->buf = (char *)realloc(self->buf, n); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 526 | if (!self->buf) { |
| 527 | PyErr_NoMemory(); |
| 528 | return -1; |
| 529 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 530 | self->buf_size = n; |
| 531 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 532 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 533 | Py_BEGIN_ALLOW_THREADS |
| 534 | nbytesread = fread(self->buf, sizeof(char), n, self->fp); |
| 535 | Py_END_ALLOW_THREADS |
| 536 | if (nbytesread != (size_t)n) { |
| 537 | if (feof(self->fp)) { |
| 538 | PyErr_SetNone(PyExc_EOFError); |
| 539 | return -1; |
| 540 | } |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 541 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 542 | PyErr_SetFromErrno(PyExc_IOError); |
| 543 | return -1; |
| 544 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 545 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 546 | *s = self->buf; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 547 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 548 | return n; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 552 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 553 | readline_file(Unpicklerobject *self, char **s) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 554 | { |
| 555 | int i; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 556 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 557 | if (self->buf_size == 0) { |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 558 | if (!( self->buf = (char *)malloc(40))) { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 559 | PyErr_NoMemory(); |
| 560 | return -1; |
| 561 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 562 | self->buf_size = 40; |
| 563 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 564 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 565 | i = 0; |
| 566 | while (1) { |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 567 | int bigger; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 568 | for (; i < (self->buf_size - 1); i++) { |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 569 | if (feof(self->fp) || |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 570 | (self->buf[i] = getc(self->fp)) == '\n') { |
| 571 | self->buf[i + 1] = '\0'; |
| 572 | *s = self->buf; |
| 573 | return i + 1; |
| 574 | } |
| 575 | } |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 576 | bigger = self->buf_size << 1; |
| 577 | if (bigger <= 0) { /* overflow */ |
| 578 | PyErr_NoMemory(); |
| 579 | return -1; |
| 580 | } |
| 581 | self->buf = (char *)realloc(self->buf, bigger); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 582 | if (!self->buf) { |
| 583 | PyErr_NoMemory(); |
| 584 | return -1; |
| 585 | } |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 586 | self->buf_size = bigger; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 587 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 588 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 589 | |
| 590 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 591 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 592 | read_cStringIO(Unpicklerobject *self, char **s, int n) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 593 | { |
| 594 | char *ptr; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 595 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 596 | if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) { |
| 597 | PyErr_SetNone(PyExc_EOFError); |
| 598 | return -1; |
| 599 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 600 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 601 | *s = ptr; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 602 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 603 | return n; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 607 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 608 | readline_cStringIO(Unpicklerobject *self, char **s) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 609 | { |
| 610 | int n; |
| 611 | char *ptr; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 612 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 613 | if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) { |
| 614 | return -1; |
| 615 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 616 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 617 | *s = ptr; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 618 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 619 | return n; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 623 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 624 | read_other(Unpicklerobject *self, char **s, int n) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 625 | { |
| 626 | PyObject *bytes, *str=0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 627 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 628 | if (!( bytes = PyInt_FromLong(n))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 629 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 630 | ARG_TUP(self, bytes); |
| 631 | if (self->arg) { |
| 632 | str = PyObject_Call(self->read, self->arg, NULL); |
| 633 | FREE_ARG_TUP(self); |
| 634 | } |
| 635 | if (! str) return -1; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 636 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 637 | Py_XDECREF(self->last_string); |
| 638 | self->last_string = str; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 639 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 640 | if (! (*s = PyString_AsString(str))) return -1; |
| 641 | return n; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 645 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 646 | readline_other(Unpicklerobject *self, char **s) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 647 | { |
| 648 | PyObject *str; |
| 649 | int str_size; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 650 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 651 | if (!( str = PyObject_CallObject(self->readline, empty_tuple))) { |
| 652 | return -1; |
| 653 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 654 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 655 | if ((str_size = PyString_Size(str)) < 0) |
| 656 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 657 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 658 | Py_XDECREF(self->last_string); |
| 659 | self->last_string = str; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 660 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 661 | if (! (*s = PyString_AsString(str))) |
| 662 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 663 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 664 | return str_size; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 667 | /* Copy the first n bytes from s into newly malloc'ed memory, plus a |
| 668 | * trailing 0 byte. Return a pointer to that, or NULL if out of memory. |
| 669 | * The caller is responsible for free()'ing the return value. |
| 670 | */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 671 | static char * |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 672 | pystrndup(char *s, int n) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 673 | { |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 674 | char *r = (char *)malloc(n+1); |
| 675 | if (r == NULL) |
| 676 | return (char*)PyErr_NoMemory(); |
| 677 | memcpy(r, s, n); |
| 678 | r[n] = 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 679 | return r; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | |
| 683 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 684 | get(Picklerobject *self, PyObject *id) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 685 | { |
| 686 | PyObject *value, *mv; |
| 687 | long c_value; |
| 688 | char s[30]; |
| 689 | size_t len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 690 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 691 | if (!( mv = PyDict_GetItem(self->memo, id))) { |
| 692 | PyErr_SetObject(PyExc_KeyError, id); |
| 693 | return -1; |
| 694 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 695 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 696 | if (!( value = PyTuple_GetItem(mv, 0))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 697 | return -1; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 698 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 699 | if (!( PyInt_Check(value))) { |
| 700 | PyErr_SetString(PicklingError, "no int where int expected in memo"); |
| 701 | return -1; |
| 702 | } |
| 703 | c_value = PyInt_AS_LONG((PyIntObject*)value); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 704 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 705 | if (!self->bin) { |
| 706 | s[0] = GET; |
| 707 | PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value); |
| 708 | len = strlen(s); |
| 709 | } |
| 710 | else if (Pdata_Check(self->file)) { |
| 711 | if (write_other(self, NULL, 0) < 0) return -1; |
| 712 | PDATA_APPEND(self->file, mv, -1); |
| 713 | return 0; |
| 714 | } |
| 715 | else { |
| 716 | if (c_value < 256) { |
| 717 | s[0] = BINGET; |
| 718 | s[1] = (int)(c_value & 0xff); |
| 719 | len = 2; |
| 720 | } |
| 721 | else { |
| 722 | s[0] = LONG_BINGET; |
| 723 | s[1] = (int)(c_value & 0xff); |
| 724 | s[2] = (int)((c_value >> 8) & 0xff); |
| 725 | s[3] = (int)((c_value >> 16) & 0xff); |
| 726 | s[4] = (int)((c_value >> 24) & 0xff); |
| 727 | len = 5; |
| 728 | } |
| 729 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 730 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 731 | if (self->write_func(self, s, len) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 732 | return -1; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 733 | |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 734 | return 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 735 | } |
Jeremy Hylton | ce616e4 | 1998-08-13 23:13:52 +0000 | [diff] [blame] | 736 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 737 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 738 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 739 | put(Picklerobject *self, PyObject *ob) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 740 | { |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 741 | if (ob->ob_refcnt < 2 || self->fast) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 742 | return 0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 743 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 744 | return put2(self, ob); |
| 745 | } |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 746 | |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 747 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 748 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 749 | put2(Picklerobject *self, PyObject *ob) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 750 | { |
| 751 | char c_str[30]; |
| 752 | int p; |
| 753 | size_t len; |
| 754 | int res = -1; |
| 755 | PyObject *py_ob_id = 0, *memo_len = 0, *t = 0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 756 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 757 | if (self->fast) |
| 758 | return 0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 759 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 760 | if ((p = PyDict_Size(self->memo)) < 0) |
| 761 | goto finally; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 762 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 763 | /* Make sure memo keys are positive! */ |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 764 | /* XXX Why? |
| 765 | * XXX And does "positive" really mean non-negative? |
| 766 | * XXX pickle.py starts with PUT index 0, not 1. This makes for |
| 767 | * XXX gratuitous differences between the pickling modules. |
| 768 | */ |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 769 | p++; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 770 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 771 | if (!( py_ob_id = PyLong_FromVoidPtr(ob))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 772 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 773 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 774 | if (!( memo_len = PyInt_FromLong(p))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 775 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 776 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 777 | if (!( t = PyTuple_New(2))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 778 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 779 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 780 | PyTuple_SET_ITEM(t, 0, memo_len); |
| 781 | Py_INCREF(memo_len); |
| 782 | PyTuple_SET_ITEM(t, 1, ob); |
| 783 | Py_INCREF(ob); |
| 784 | |
| 785 | if (PyDict_SetItem(self->memo, py_ob_id, t) < 0) |
| 786 | goto finally; |
| 787 | |
| 788 | if (!self->bin) { |
| 789 | c_str[0] = PUT; |
| 790 | PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p); |
| 791 | len = strlen(c_str); |
| 792 | } |
| 793 | else if (Pdata_Check(self->file)) { |
| 794 | if (write_other(self, NULL, 0) < 0) return -1; |
| 795 | PDATA_APPEND(self->file, memo_len, -1); |
| 796 | res=0; /* Job well done ;) */ |
| 797 | goto finally; |
| 798 | } |
| 799 | else { |
| 800 | if (p >= 256) { |
| 801 | c_str[0] = LONG_BINPUT; |
| 802 | c_str[1] = (int)(p & 0xff); |
| 803 | c_str[2] = (int)((p >> 8) & 0xff); |
| 804 | c_str[3] = (int)((p >> 16) & 0xff); |
| 805 | c_str[4] = (int)((p >> 24) & 0xff); |
| 806 | len = 5; |
| 807 | } |
| 808 | else { |
| 809 | c_str[0] = BINPUT; |
| 810 | c_str[1] = p; |
| 811 | len = 2; |
| 812 | } |
| 813 | } |
| 814 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 815 | if (self->write_func(self, c_str, len) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 816 | goto finally; |
| 817 | |
| 818 | res = 0; |
| 819 | |
| 820 | finally: |
| 821 | Py_XDECREF(py_ob_id); |
| 822 | Py_XDECREF(memo_len); |
| 823 | Py_XDECREF(t); |
| 824 | |
| 825 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 828 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 829 | whichmodule(PyObject *global, PyObject *global_name) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 830 | { |
| 831 | int i, j; |
| 832 | PyObject *module = 0, *modules_dict = 0, |
| 833 | *global_name_attr = 0, *name = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 834 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 835 | module = PyObject_GetAttrString(global, "__module__"); |
| 836 | if (module) return module; |
| 837 | PyErr_Clear(); |
Guido van Rossum | 4518823 | 1997-09-28 05:38:51 +0000 | [diff] [blame] | 838 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 839 | if (!( modules_dict = PySys_GetObject("modules"))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 840 | return NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 841 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 842 | i = 0; |
| 843 | while ((j = PyDict_Next(modules_dict, &i, &name, &module))) { |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 844 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 845 | if (PyObject_Compare(name, __main___str)==0) continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 846 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 847 | global_name_attr = PyObject_GetAttr(module, global_name); |
| 848 | if (!global_name_attr) { |
| 849 | PyErr_Clear(); |
| 850 | continue; |
| 851 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 852 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 853 | if (global_name_attr != global) { |
| 854 | Py_DECREF(global_name_attr); |
| 855 | continue; |
| 856 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 857 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 858 | Py_DECREF(global_name_attr); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 859 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 860 | break; |
| 861 | } |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 862 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 863 | /* The following implements the rule in pickle.py added in 1.5 |
| 864 | that used __main__ if no module is found. I don't actually |
| 865 | like this rule. jlf |
| 866 | */ |
| 867 | if (!j) { |
| 868 | j=1; |
| 869 | name=__main___str; |
| 870 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 871 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 872 | Py_INCREF(name); |
| 873 | return name; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 877 | static int |
Jeremy Hylton | 499ab6a | 2001-10-15 21:37:58 +0000 | [diff] [blame] | 878 | fast_save_enter(Picklerobject *self, PyObject *obj) |
| 879 | { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 880 | /* if fast_container < 0, we're doing an error exit. */ |
| 881 | if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) { |
| 882 | PyObject *key = NULL; |
| 883 | if (self->fast_memo == NULL) { |
| 884 | self->fast_memo = PyDict_New(); |
| 885 | if (self->fast_memo == NULL) { |
| 886 | self->fast_container = -1; |
| 887 | return 0; |
| 888 | } |
| 889 | } |
| 890 | key = PyLong_FromVoidPtr(obj); |
| 891 | if (key == NULL) |
| 892 | return 0; |
| 893 | if (PyDict_GetItem(self->fast_memo, key)) { |
Tim Peters | 4e52ca8 | 2002-12-07 02:43:28 +0000 | [diff] [blame] | 894 | Py_DECREF(key); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 895 | PyErr_Format(PyExc_ValueError, |
Tim Peters | 92c8bb3 | 2003-02-13 23:00:26 +0000 | [diff] [blame] | 896 | "fast mode: can't pickle cyclic objects " |
| 897 | "including object type %s at %p", |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 898 | obj->ob_type->tp_name, obj); |
| 899 | self->fast_container = -1; |
| 900 | return 0; |
| 901 | } |
| 902 | if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) { |
Tim Peters | 4e52ca8 | 2002-12-07 02:43:28 +0000 | [diff] [blame] | 903 | Py_DECREF(key); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 904 | self->fast_container = -1; |
| 905 | return 0; |
| 906 | } |
Tim Peters | 4e52ca8 | 2002-12-07 02:43:28 +0000 | [diff] [blame] | 907 | Py_DECREF(key); |
Jeremy Hylton | 499ab6a | 2001-10-15 21:37:58 +0000 | [diff] [blame] | 908 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 909 | return 1; |
Jeremy Hylton | 499ab6a | 2001-10-15 21:37:58 +0000 | [diff] [blame] | 910 | } |
| 911 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 912 | int |
Jeremy Hylton | 499ab6a | 2001-10-15 21:37:58 +0000 | [diff] [blame] | 913 | fast_save_leave(Picklerobject *self, PyObject *obj) |
| 914 | { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 915 | if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) { |
| 916 | PyObject *key = PyLong_FromVoidPtr(obj); |
| 917 | if (key == NULL) |
| 918 | return 0; |
| 919 | if (PyDict_DelItem(self->fast_memo, key) < 0) { |
Tim Peters | 4e52ca8 | 2002-12-07 02:43:28 +0000 | [diff] [blame] | 920 | Py_DECREF(key); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 921 | return 0; |
| 922 | } |
Tim Peters | 4e52ca8 | 2002-12-07 02:43:28 +0000 | [diff] [blame] | 923 | Py_DECREF(key); |
Jeremy Hylton | 499ab6a | 2001-10-15 21:37:58 +0000 | [diff] [blame] | 924 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 925 | return 1; |
Jeremy Hylton | 499ab6a | 2001-10-15 21:37:58 +0000 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 929 | save_none(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 930 | { |
| 931 | static char none = NONE; |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 932 | if (self->write_func(self, &none, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 933 | return -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 934 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 935 | return 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 936 | } |
| 937 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 938 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 939 | save_bool(Picklerobject *self, PyObject *args) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 940 | { |
Guido van Rossum | e276339 | 2002-04-05 19:30:08 +0000 | [diff] [blame] | 941 | static char *buf[2] = {FALSE, TRUE}; |
| 942 | static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1}; |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 943 | long l = PyInt_AS_LONG((PyIntObject *)args); |
| 944 | |
Tim Peters | 3c67d79 | 2003-02-02 17:59:11 +0000 | [diff] [blame] | 945 | if (self->proto >= 2) { |
| 946 | char opcode = l ? NEWTRUE : NEWFALSE; |
| 947 | if (self->write_func(self, &opcode, 1) < 0) |
| 948 | return -1; |
| 949 | } |
| 950 | else if (self->write_func(self, buf[l], len[l]) < 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 951 | return -1; |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 952 | return 0; |
| 953 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 954 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 955 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 956 | save_int(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 957 | { |
| 958 | char c_str[32]; |
| 959 | long l = PyInt_AS_LONG((PyIntObject *)args); |
| 960 | int len = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 961 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 962 | if (!self->bin |
Guido van Rossum | de8d6d7 | 1997-05-13 18:00:44 +0000 | [diff] [blame] | 963 | #if SIZEOF_LONG > 4 |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 964 | || l > 0x7fffffffL |
| 965 | || l < -0x80000000L |
Guido van Rossum | de8d6d7 | 1997-05-13 18:00:44 +0000 | [diff] [blame] | 966 | #endif |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 967 | ) { |
| 968 | /* Text-mode pickle, or long too big to fit in the 4-byte |
| 969 | * signed BININT format: store as a string. |
| 970 | */ |
| 971 | c_str[0] = INT; |
| 972 | PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l); |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 973 | if (self->write_func(self, c_str, strlen(c_str)) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 974 | return -1; |
| 975 | } |
| 976 | else { |
| 977 | /* Binary pickle and l fits in a signed 4-byte int. */ |
| 978 | c_str[1] = (int)( l & 0xff); |
| 979 | c_str[2] = (int)((l >> 8) & 0xff); |
| 980 | c_str[3] = (int)((l >> 16) & 0xff); |
| 981 | c_str[4] = (int)((l >> 24) & 0xff); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 982 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 983 | if ((c_str[4] == 0) && (c_str[3] == 0)) { |
| 984 | if (c_str[2] == 0) { |
| 985 | c_str[0] = BININT1; |
| 986 | len = 2; |
| 987 | } |
| 988 | else { |
| 989 | c_str[0] = BININT2; |
| 990 | len = 3; |
| 991 | } |
| 992 | } |
| 993 | else { |
| 994 | c_str[0] = BININT; |
| 995 | len = 5; |
| 996 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 997 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 998 | if (self->write_func(self, c_str, len) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 999 | return -1; |
| 1000 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1001 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1002 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | |
| 1006 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1007 | save_long(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1008 | { |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 1009 | int size; |
| 1010 | int res = -1; |
| 1011 | PyObject *repr = NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1012 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1013 | static char l = LONG; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1014 | |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 1015 | if (self->proto >= 2) { |
| 1016 | /* Linear-time pickling. */ |
| 1017 | size_t nbits; |
| 1018 | size_t nbytes; |
| 1019 | unsigned char *pdata; |
| 1020 | char c_str[5]; |
| 1021 | int i; |
| 1022 | int sign = _PyLong_Sign(args); |
| 1023 | |
| 1024 | if (sign == 0) { |
| 1025 | /* It's 0 -- an empty bytestring. */ |
| 1026 | c_str[0] = LONG1; |
| 1027 | c_str[1] = 0; |
| 1028 | i = self->write_func(self, c_str, 2); |
| 1029 | if (i < 0) goto finally; |
| 1030 | res = 0; |
| 1031 | goto finally; |
| 1032 | } |
| 1033 | nbits = _PyLong_NumBits(args); |
| 1034 | if (nbits == (size_t)-1 && PyErr_Occurred()) |
| 1035 | goto finally; |
| 1036 | /* How many bytes do we need? There are nbits >> 3 full |
| 1037 | * bytes of data, and nbits & 7 leftover bits. If there |
| 1038 | * are any leftover bits, then we clearly need another |
| 1039 | * byte. Wnat's not so obvious is that we *probably* |
| 1040 | * need another byte even if there aren't any leftovers: |
| 1041 | * the most-significant bit of the most-significant byte |
| 1042 | * acts like a sign bit, and it's usually got a sense |
| 1043 | * opposite of the one we need. The exception is longs |
| 1044 | * of the form -(2**(8*j-1)) for j > 0. Such a long is |
| 1045 | * its own 256's-complement, so has the right sign bit |
| 1046 | * even without the extra byte. That's a pain to check |
| 1047 | * for in advance, though, so we always grab an extra |
| 1048 | * byte at the start, and cut it back later if possible. |
| 1049 | */ |
| 1050 | nbytes = (nbits >> 3) + 1; |
| 1051 | if ((int)nbytes < 0 || (size_t)(int)nbytes != nbytes) { |
| 1052 | PyErr_SetString(PyExc_OverflowError, "long too large " |
| 1053 | "to pickle"); |
| 1054 | goto finally; |
| 1055 | } |
| 1056 | repr = PyString_FromStringAndSize(NULL, (int)nbytes); |
| 1057 | if (repr == NULL) goto finally; |
| 1058 | pdata = (unsigned char *)PyString_AS_STRING(repr); |
| 1059 | i = _PyLong_AsByteArray((PyLongObject *)args, |
| 1060 | pdata, nbytes, |
| 1061 | 1 /* little endian */, 1 /* signed */); |
| 1062 | if (i < 0) goto finally; |
| 1063 | /* If the long is negative, this may be a byte more than |
| 1064 | * needed. This is so iff the MSB is all redundant sign |
| 1065 | * bits. |
| 1066 | */ |
| 1067 | if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff && |
| 1068 | (pdata[nbytes - 2] & 0x80) != 0) |
| 1069 | --nbytes; |
| 1070 | |
| 1071 | if (nbytes < 256) { |
| 1072 | c_str[0] = LONG1; |
| 1073 | c_str[1] = (char)nbytes; |
| 1074 | size = 2; |
| 1075 | } |
| 1076 | else { |
| 1077 | c_str[0] = LONG4; |
| 1078 | size = (int)nbytes; |
| 1079 | for (i = 1; i < 5; i++) { |
| 1080 | c_str[i] = (char)(size & 0xff); |
| 1081 | size >>= 8; |
| 1082 | } |
| 1083 | size = 5; |
| 1084 | } |
| 1085 | i = self->write_func(self, c_str, size); |
| 1086 | if (i < 0) goto finally; |
| 1087 | i = self->write_func(self, (char *)pdata, (int)nbytes); |
| 1088 | if (i < 0) goto finally; |
| 1089 | res = 0; |
| 1090 | goto finally; |
| 1091 | } |
| 1092 | |
| 1093 | /* proto < 2: write the repr and newline. This is quadratic-time |
| 1094 | * (in the number of digits), in both directions. |
| 1095 | */ |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1096 | if (!( repr = PyObject_Repr(args))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1097 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1098 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1099 | if ((size = PyString_Size(repr)) < 0) |
| 1100 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1101 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1102 | if (self->write_func(self, &l, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1103 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1104 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1105 | if (self->write_func(self, |
| 1106 | PyString_AS_STRING((PyStringObject *)repr), |
| 1107 | size) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1108 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1109 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1110 | if (self->write_func(self, "\n", 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1111 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1112 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1113 | res = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1114 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1115 | finally: |
| 1116 | Py_XDECREF(repr); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1117 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | |
| 1121 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1122 | save_float(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1123 | { |
| 1124 | double x = PyFloat_AS_DOUBLE((PyFloatObject *)args); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1125 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1126 | if (self->bin) { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1127 | char str[9]; |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 1128 | str[0] = BINFLOAT; |
| 1129 | if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1130 | return -1; |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1131 | if (self->write_func(self, str, 9) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1132 | return -1; |
| 1133 | } |
| 1134 | else { |
| 1135 | char c_str[250]; |
| 1136 | c_str[0] = FLOAT; |
| 1137 | PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%.17g\n", x); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1138 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1139 | if (self->write_func(self, c_str, strlen(c_str)) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1140 | return -1; |
| 1141 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1142 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1143 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | |
| 1147 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1148 | save_string(Picklerobject *self, PyObject *args, int doput) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1149 | { |
| 1150 | int size, len; |
| 1151 | PyObject *repr=0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1152 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1153 | if ((size = PyString_Size(args)) < 0) |
| 1154 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1155 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1156 | if (!self->bin) { |
| 1157 | char *repr_str; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1158 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1159 | static char string = STRING; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1160 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1161 | if (!( repr = PyObject_Repr(args))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1162 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1163 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1164 | if ((len = PyString_Size(repr)) < 0) |
| 1165 | goto err; |
| 1166 | repr_str = PyString_AS_STRING((PyStringObject *)repr); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1167 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1168 | if (self->write_func(self, &string, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1169 | goto err; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1170 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1171 | if (self->write_func(self, repr_str, len) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1172 | goto err; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1173 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1174 | if (self->write_func(self, "\n", 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1175 | goto err; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1176 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1177 | Py_XDECREF(repr); |
| 1178 | } |
| 1179 | else { |
| 1180 | int i; |
| 1181 | char c_str[5]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1182 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1183 | if ((size = PyString_Size(args)) < 0) |
| 1184 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1185 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1186 | if (size < 256) { |
| 1187 | c_str[0] = SHORT_BINSTRING; |
| 1188 | c_str[1] = size; |
| 1189 | len = 2; |
| 1190 | } |
| 1191 | else { |
| 1192 | c_str[0] = BINSTRING; |
| 1193 | for (i = 1; i < 5; i++) |
| 1194 | c_str[i] = (int)(size >> ((i - 1) * 8)); |
| 1195 | len = 5; |
| 1196 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1197 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1198 | if (self->write_func(self, c_str, len) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1199 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1200 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1201 | if (size > 128 && Pdata_Check(self->file)) { |
| 1202 | if (write_other(self, NULL, 0) < 0) return -1; |
| 1203 | PDATA_APPEND(self->file, args, -1); |
| 1204 | } |
| 1205 | else { |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1206 | if (self->write_func(self, |
| 1207 | PyString_AS_STRING( |
| 1208 | (PyStringObject *)args), |
| 1209 | size) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1210 | return -1; |
| 1211 | } |
| 1212 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1213 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1214 | if (doput) |
| 1215 | if (put(self, args) < 0) |
| 1216 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1217 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1218 | return 0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 1219 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1220 | err: |
| 1221 | Py_XDECREF(repr); |
| 1222 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1226 | #ifdef Py_USING_UNICODE |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1227 | /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates |
| 1228 | backslash and newline characters to \uXXXX escapes. */ |
| 1229 | static PyObject * |
| 1230 | modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, int size) |
| 1231 | { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1232 | PyObject *repr; |
| 1233 | char *p; |
| 1234 | char *q; |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1235 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1236 | static const char *hexdigit = "0123456789ABCDEF"; |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1237 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1238 | repr = PyString_FromStringAndSize(NULL, 6 * size); |
| 1239 | if (repr == NULL) |
| 1240 | return NULL; |
| 1241 | if (size == 0) |
| 1242 | return repr; |
| 1243 | |
| 1244 | p = q = PyString_AS_STRING(repr); |
| 1245 | while (size-- > 0) { |
| 1246 | Py_UNICODE ch = *s++; |
| 1247 | /* Map 16-bit characters to '\uxxxx' */ |
| 1248 | if (ch >= 256 || ch == '\\' || ch == '\n') { |
| 1249 | *p++ = '\\'; |
| 1250 | *p++ = 'u'; |
| 1251 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 1252 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 1253 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 1254 | *p++ = hexdigit[ch & 15]; |
| 1255 | } |
| 1256 | /* Copy everything else as-is */ |
| 1257 | else |
| 1258 | *p++ = (char) ch; |
| 1259 | } |
| 1260 | *p = '\0'; |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 1261 | _PyString_Resize(&repr, p - q); |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1262 | return repr; |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1266 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1267 | save_unicode(Picklerobject *self, PyObject *args, int doput) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1268 | { |
| 1269 | int size, len; |
| 1270 | PyObject *repr=0; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1271 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1272 | if (!PyUnicode_Check(args)) |
| 1273 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1274 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1275 | if (!self->bin) { |
| 1276 | char *repr_str; |
| 1277 | static char string = UNICODE; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1278 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1279 | repr = modified_EncodeRawUnicodeEscape( |
| 1280 | PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args)); |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1281 | if (!repr) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1282 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1283 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1284 | if ((len = PyString_Size(repr)) < 0) |
| 1285 | goto err; |
| 1286 | repr_str = PyString_AS_STRING((PyStringObject *)repr); |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1287 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1288 | if (self->write_func(self, &string, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1289 | goto err; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1290 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1291 | if (self->write_func(self, repr_str, len) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1292 | goto err; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1293 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1294 | if (self->write_func(self, "\n", 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1295 | goto err; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1296 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1297 | Py_XDECREF(repr); |
| 1298 | } |
| 1299 | else { |
| 1300 | int i; |
| 1301 | char c_str[5]; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1302 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1303 | if (!( repr = PyUnicode_AsUTF8String(args))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1304 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1305 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1306 | if ((size = PyString_Size(repr)) < 0) |
| 1307 | goto err; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1308 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1309 | c_str[0] = BINUNICODE; |
| 1310 | for (i = 1; i < 5; i++) |
| 1311 | c_str[i] = (int)(size >> ((i - 1) * 8)); |
| 1312 | len = 5; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1313 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1314 | if (self->write_func(self, c_str, len) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1315 | goto err; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1316 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1317 | if (size > 128 && Pdata_Check(self->file)) { |
| 1318 | if (write_other(self, NULL, 0) < 0) |
| 1319 | goto err; |
| 1320 | PDATA_APPEND(self->file, repr, -1); |
| 1321 | } |
| 1322 | else { |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1323 | if (self->write_func(self, PyString_AS_STRING(repr), |
| 1324 | size) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1325 | goto err; |
| 1326 | } |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1327 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1328 | Py_DECREF(repr); |
| 1329 | } |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1330 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1331 | if (doput) |
| 1332 | if (put(self, args) < 0) |
| 1333 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1334 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1335 | return 0; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1336 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1337 | err: |
| 1338 | Py_XDECREF(repr); |
| 1339 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1340 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1341 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1342 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1343 | /* A helper for save_tuple. Push the len elements in tuple t on the stack. */ |
| 1344 | static int |
Tim Peters | 6792014 | 2003-02-05 03:46:17 +0000 | [diff] [blame] | 1345 | store_tuple_elements(Picklerobject *self, PyObject *t, int len) |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1346 | { |
| 1347 | int i; |
| 1348 | int res = -1; /* guilty until proved innocent */ |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1349 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1350 | assert(PyTuple_Size(t) == len); |
| 1351 | |
| 1352 | for (i = 0; i < len; i++) { |
| 1353 | PyObject *element = PyTuple_GET_ITEM(t, i); |
| 1354 | |
| 1355 | if (element == NULL) |
| 1356 | goto finally; |
| 1357 | if (save(self, element, 0) < 0) |
| 1358 | goto finally; |
| 1359 | } |
| 1360 | res = 0; |
| 1361 | |
| 1362 | finally: |
| 1363 | return res; |
| 1364 | } |
| 1365 | |
| 1366 | /* Tuples are ubiquitous in the pickle protocols, so many techniques are |
| 1367 | * used across protocols to minimize the space needed to pickle them. |
Tim Peters | 6792014 | 2003-02-05 03:46:17 +0000 | [diff] [blame] | 1368 | * Tuples are also the only builtin immutable type that can be recursive |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1369 | * (a tuple can be reached from itself), and that requires some subtle |
| 1370 | * magic so that it works in all cases. IOW, this is a long routine. |
| 1371 | */ |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1372 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1373 | save_tuple(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1374 | { |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1375 | PyObject *py_tuple_id = NULL; |
| 1376 | int len, i; |
| 1377 | int res = -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1378 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1379 | static char tuple = TUPLE; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1380 | static char pop = POP; |
| 1381 | static char pop_mark = POP_MARK; |
| 1382 | static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3}; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1383 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1384 | if ((len = PyTuple_Size(args)) < 0) |
| 1385 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1386 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1387 | if (len == 0) { |
| 1388 | char c_str[2]; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 1389 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1390 | if (self->proto) { |
| 1391 | c_str[0] = EMPTY_TUPLE; |
| 1392 | len = 1; |
| 1393 | } |
| 1394 | else { |
| 1395 | c_str[0] = MARK; |
| 1396 | c_str[1] = TUPLE; |
| 1397 | len = 2; |
| 1398 | } |
| 1399 | if (self->write_func(self, c_str, len) >= 0) |
| 1400 | res = 0; |
| 1401 | /* Don't memoize an empty tuple. */ |
| 1402 | goto finally; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1403 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1404 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1405 | /* A non-empty tuple. */ |
| 1406 | |
| 1407 | /* id(tuple) isn't in the memo now. If it shows up there after |
| 1408 | * saving the tuple elements, the tuple must be recursive, in |
| 1409 | * which case we'll pop everything we put on the stack, and fetch |
| 1410 | * its value from the memo. |
| 1411 | */ |
| 1412 | py_tuple_id = PyLong_FromVoidPtr(args); |
| 1413 | if (py_tuple_id == NULL) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1414 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1415 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1416 | if (len <= 3 && self->proto >= 2) { |
| 1417 | /* Use TUPLE{1,2,3} opcodes. */ |
Tim Peters | 6792014 | 2003-02-05 03:46:17 +0000 | [diff] [blame] | 1418 | if (store_tuple_elements(self, args, len) < 0) |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1419 | goto finally; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1420 | if (PyDict_GetItem(self->memo, py_tuple_id)) { |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1421 | /* pop the len elements */ |
| 1422 | for (i = 0; i < len; ++i) |
| 1423 | if (self->write_func(self, &pop, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1424 | goto finally; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1425 | /* fetch from memo */ |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1426 | if (get(self, py_tuple_id) < 0) |
| 1427 | goto finally; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1428 | res = 0; |
| 1429 | goto finally; |
| 1430 | } |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1431 | /* Not recursive. */ |
| 1432 | if (self->write_func(self, len2opcode + len, 1) < 0) |
| 1433 | goto finally; |
| 1434 | goto memoize; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1435 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1436 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1437 | /* proto < 2 and len > 0, or proto >= 2 and len > 3. |
| 1438 | * Generate MARK elt1 elt2 ... TUPLE |
| 1439 | */ |
| 1440 | if (self->write_func(self, &MARKv, 1) < 0) |
| 1441 | goto finally; |
| 1442 | |
Tim Peters | 6792014 | 2003-02-05 03:46:17 +0000 | [diff] [blame] | 1443 | if (store_tuple_elements(self, args, len) < 0) |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1444 | goto finally; |
| 1445 | |
| 1446 | if (PyDict_GetItem(self->memo, py_tuple_id)) { |
| 1447 | /* pop the stack stuff we pushed */ |
| 1448 | if (self->bin) { |
| 1449 | if (self->write_func(self, &pop_mark, 1) < 0) |
| 1450 | goto finally; |
| 1451 | } |
| 1452 | else { |
| 1453 | /* Note that we pop one more than len, to remove |
| 1454 | * the MARK too. |
| 1455 | */ |
| 1456 | for (i = 0; i <= len; i++) |
| 1457 | if (self->write_func(self, &pop, 1) < 0) |
| 1458 | goto finally; |
| 1459 | } |
| 1460 | /* fetch from memo */ |
| 1461 | if (get(self, py_tuple_id) >= 0) |
| 1462 | res = 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1463 | goto finally; |
| 1464 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1465 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1466 | /* Not recursive. */ |
| 1467 | if (self->write_func(self, &tuple, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1468 | goto finally; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 1469 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1470 | memoize: |
| 1471 | if (put(self, args) >= 0) |
| 1472 | res = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1473 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1474 | finally: |
| 1475 | Py_XDECREF(py_tuple_id); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1476 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1479 | /* iter is an iterator giving items, and we batch up chunks of |
| 1480 | * MARK item item ... item APPENDS |
| 1481 | * opcode sequences. Calling code should have arranged to first create an |
| 1482 | * empty list, or list-like object, for the APPENDS to operate on. |
| 1483 | * Returns 0 on success, <0 on error. |
| 1484 | */ |
| 1485 | static int |
| 1486 | batch_list(Picklerobject *self, PyObject *iter) |
| 1487 | { |
| 1488 | PyObject *obj; |
| 1489 | PyObject *slice[BATCHSIZE]; |
| 1490 | int i, n; |
| 1491 | |
| 1492 | static char append = APPEND; |
| 1493 | static char appends = APPENDS; |
| 1494 | |
| 1495 | assert(iter != NULL); |
| 1496 | |
| 1497 | if (self->proto == 0) { |
| 1498 | /* APPENDS isn't available; do one at a time. */ |
| 1499 | for (;;) { |
| 1500 | obj = PyIter_Next(iter); |
| 1501 | if (obj == NULL) { |
| 1502 | if (PyErr_Occurred()) |
| 1503 | return -1; |
| 1504 | break; |
| 1505 | } |
| 1506 | i = save(self, obj, 0); |
| 1507 | Py_DECREF(obj); |
| 1508 | if (i < 0) |
| 1509 | return -1; |
| 1510 | if (self->write_func(self, &append, 1) < 0) |
| 1511 | return -1; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1512 | } |
| 1513 | return 0; |
| 1514 | } |
| 1515 | |
| 1516 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 1517 | do { |
| 1518 | /* Get next group of (no more than) BATCHSIZE elements. */ |
| 1519 | for (n = 0; n < BATCHSIZE; ++n) { |
| 1520 | obj = PyIter_Next(iter); |
| 1521 | if (obj == NULL) { |
| 1522 | if (PyErr_Occurred()) |
| 1523 | goto BatchFailed; |
| 1524 | break; |
| 1525 | } |
| 1526 | slice[n] = obj; |
| 1527 | } |
| 1528 | |
| 1529 | if (n > 1) { |
| 1530 | /* Pump out MARK, slice[0:n], APPENDS. */ |
| 1531 | if (self->write_func(self, &MARKv, 1) < 0) |
| 1532 | goto BatchFailed; |
| 1533 | for (i = 0; i < n; ++i) { |
| 1534 | if (save(self, slice[i], 0) < 0) |
| 1535 | goto BatchFailed; |
| 1536 | } |
| 1537 | if (self->write_func(self, &appends, 1) < 0) |
| 1538 | goto BatchFailed; |
| 1539 | } |
| 1540 | else if (n == 1) { |
| 1541 | if (save(self, slice[0], 0) < 0) |
| 1542 | goto BatchFailed; |
| 1543 | if (self->write_func(self, &append, 1) < 0) |
| 1544 | goto BatchFailed; |
| 1545 | } |
| 1546 | |
| 1547 | for (i = 0; i < n; ++i) { |
| 1548 | Py_DECREF(slice[i]); |
| 1549 | } |
Tim Peters | 90975f1 | 2003-02-12 05:28:58 +0000 | [diff] [blame] | 1550 | } while (n == BATCHSIZE); |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1551 | return 0; |
| 1552 | |
| 1553 | BatchFailed: |
| 1554 | while (--n >= 0) { |
| 1555 | Py_DECREF(slice[n]); |
| 1556 | } |
| 1557 | return -1; |
| 1558 | } |
| 1559 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1560 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1561 | save_list(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1562 | { |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1563 | int res = -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1564 | char s[3]; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1565 | int len; |
| 1566 | PyObject *iter; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1567 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1568 | if (self->fast && !fast_save_enter(self, args)) |
| 1569 | goto finally; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 1570 | |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1571 | /* Create an empty list. */ |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1572 | if (self->bin) { |
| 1573 | s[0] = EMPTY_LIST; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1574 | len = 1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1575 | } |
| 1576 | else { |
| 1577 | s[0] = MARK; |
| 1578 | s[1] = LIST; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1579 | len = 2; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1580 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1581 | |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1582 | if (self->write_func(self, s, len) < 0) |
| 1583 | goto finally; |
| 1584 | |
| 1585 | /* Get list length, and bow out early if empty. */ |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1586 | if ((len = PyList_Size(args)) < 0) |
| 1587 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1588 | |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1589 | /* Memoize. */ |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1590 | if (len == 0) { |
Tim Peters | 90975f1 | 2003-02-12 05:28:58 +0000 | [diff] [blame] | 1591 | if (put(self, args) >= 0) |
| 1592 | res = 0; |
| 1593 | goto finally; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1594 | } |
Tim Peters | 90975f1 | 2003-02-12 05:28:58 +0000 | [diff] [blame] | 1595 | if (put2(self, args) < 0) |
| 1596 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1597 | |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1598 | /* Materialize the list elements. */ |
| 1599 | iter = PyObject_GetIter(args); |
| 1600 | if (iter == NULL) |
| 1601 | goto finally; |
| 1602 | res = batch_list(self, iter); |
| 1603 | Py_DECREF(iter); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1604 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1605 | finally: |
| 1606 | if (self->fast && !fast_save_leave(self, args)) |
| 1607 | res = -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1608 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1609 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
| 1612 | |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1613 | /* iter is an iterator giving (key, value) pairs, and we batch up chunks of |
| 1614 | * MARK key value ... key value SETITEMS |
| 1615 | * opcode sequences. Calling code should have arranged to first create an |
| 1616 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 1617 | * Returns 0 on success, <0 on error. |
| 1618 | * |
| 1619 | * This is very much like batch_list(). The difference between saving |
| 1620 | * elements directly, and picking apart two-tuples, is so long-winded at |
| 1621 | * the C level, though, that attempts to combine these routines were too |
| 1622 | * ugly to bear. |
| 1623 | */ |
| 1624 | static int |
| 1625 | batch_dict(Picklerobject *self, PyObject *iter) |
| 1626 | { |
| 1627 | PyObject *p; |
| 1628 | PyObject *slice[BATCHSIZE]; |
| 1629 | int i, n; |
| 1630 | |
| 1631 | static char setitem = SETITEM; |
| 1632 | static char setitems = SETITEMS; |
| 1633 | |
| 1634 | assert(iter != NULL); |
| 1635 | |
| 1636 | if (self->proto == 0) { |
| 1637 | /* SETITEMS isn't available; do one at a time. */ |
| 1638 | for (;;) { |
| 1639 | p = PyIter_Next(iter); |
| 1640 | if (p == NULL) { |
| 1641 | if (PyErr_Occurred()) |
| 1642 | return -1; |
| 1643 | break; |
| 1644 | } |
| 1645 | if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) { |
| 1646 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 1647 | "iterator must return 2-tuples"); |
| 1648 | return -1; |
| 1649 | } |
| 1650 | i = save(self, PyTuple_GET_ITEM(p, 0), 0); |
| 1651 | if (i >= 0) |
| 1652 | i = save(self, PyTuple_GET_ITEM(p, 1), 0); |
| 1653 | Py_DECREF(p); |
| 1654 | if (i < 0) |
| 1655 | return -1; |
| 1656 | if (self->write_func(self, &setitem, 1) < 0) |
| 1657 | return -1; |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1658 | } |
| 1659 | return 0; |
| 1660 | } |
| 1661 | |
| 1662 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 1663 | do { |
| 1664 | /* Get next group of (no more than) BATCHSIZE elements. */ |
| 1665 | for (n = 0; n < BATCHSIZE; ++n) { |
| 1666 | p = PyIter_Next(iter); |
| 1667 | if (p == NULL) { |
| 1668 | if (PyErr_Occurred()) |
| 1669 | goto BatchFailed; |
| 1670 | break; |
| 1671 | } |
| 1672 | if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) { |
| 1673 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 1674 | "iterator must return 2-tuples"); |
| 1675 | goto BatchFailed; |
| 1676 | } |
| 1677 | slice[n] = p; |
| 1678 | } |
| 1679 | |
| 1680 | if (n > 1) { |
| 1681 | /* Pump out MARK, slice[0:n], SETITEMS. */ |
| 1682 | if (self->write_func(self, &MARKv, 1) < 0) |
| 1683 | goto BatchFailed; |
| 1684 | for (i = 0; i < n; ++i) { |
| 1685 | p = slice[i]; |
| 1686 | if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0) |
| 1687 | goto BatchFailed; |
| 1688 | if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0) |
| 1689 | goto BatchFailed; |
| 1690 | } |
| 1691 | if (self->write_func(self, &setitems, 1) < 0) |
| 1692 | goto BatchFailed; |
| 1693 | } |
| 1694 | else if (n == 1) { |
| 1695 | p = slice[0]; |
| 1696 | if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0) |
| 1697 | goto BatchFailed; |
| 1698 | if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0) |
| 1699 | goto BatchFailed; |
| 1700 | if (self->write_func(self, &setitem, 1) < 0) |
| 1701 | goto BatchFailed; |
| 1702 | } |
| 1703 | |
| 1704 | for (i = 0; i < n; ++i) { |
| 1705 | Py_DECREF(slice[i]); |
| 1706 | } |
Tim Peters | 90975f1 | 2003-02-12 05:28:58 +0000 | [diff] [blame] | 1707 | } while (n == BATCHSIZE); |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1708 | return 0; |
| 1709 | |
| 1710 | BatchFailed: |
| 1711 | while (--n >= 0) { |
| 1712 | Py_DECREF(slice[n]); |
| 1713 | } |
| 1714 | return -1; |
| 1715 | } |
| 1716 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1717 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1718 | save_dict(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1719 | { |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1720 | int res = -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1721 | char s[3]; |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1722 | int len; |
| 1723 | PyObject *iter; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1724 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1725 | if (self->fast && !fast_save_enter(self, args)) |
| 1726 | goto finally; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 1727 | |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1728 | /* Create an empty dict. */ |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1729 | if (self->bin) { |
| 1730 | s[0] = EMPTY_DICT; |
| 1731 | len = 1; |
| 1732 | } |
| 1733 | else { |
| 1734 | s[0] = MARK; |
| 1735 | s[1] = DICT; |
| 1736 | len = 2; |
| 1737 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1738 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1739 | if (self->write_func(self, s, len) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1740 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1741 | |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1742 | /* Get dict size, and bow out early if empty. */ |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1743 | if ((len = PyDict_Size(args)) < 0) |
| 1744 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1745 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1746 | if (len == 0) { |
Tim Peters | 90975f1 | 2003-02-12 05:28:58 +0000 | [diff] [blame] | 1747 | if (put(self, args) >= 0) |
| 1748 | res = 0; |
| 1749 | goto finally; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1750 | } |
Tim Peters | 90975f1 | 2003-02-12 05:28:58 +0000 | [diff] [blame] | 1751 | if (put2(self, args) < 0) |
| 1752 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1753 | |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1754 | /* Materialize the dict items. */ |
| 1755 | iter = PyObject_CallMethod(args, "iteritems", "()"); |
| 1756 | if (iter == NULL) |
| 1757 | goto finally; |
| 1758 | res = batch_dict(self, iter); |
| 1759 | Py_DECREF(iter); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1760 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1761 | finally: |
| 1762 | if (self->fast && !fast_save_leave(self, args)) |
| 1763 | res = -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1764 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1765 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
| 1768 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 1769 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1770 | save_inst(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1771 | { |
| 1772 | PyObject *class = 0, *module = 0, *name = 0, *state = 0, |
| 1773 | *getinitargs_func = 0, *getstate_func = 0, *class_args = 0; |
| 1774 | char *module_str, *name_str; |
| 1775 | int module_size, name_size, res = -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1776 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1777 | static char inst = INST, obj = OBJ, build = BUILD; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1778 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1779 | if (self->fast && !fast_save_enter(self, args)) |
| 1780 | goto finally; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 1781 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1782 | if (self->write_func(self, &MARKv, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1783 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1784 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1785 | if (!( class = PyObject_GetAttr(args, __class___str))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1786 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1787 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1788 | if (self->bin) { |
| 1789 | if (save(self, class, 0) < 0) |
| 1790 | goto finally; |
| 1791 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1792 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1793 | if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) { |
| 1794 | PyObject *element = 0; |
| 1795 | int i, len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1796 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1797 | if (!( class_args = |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1798 | PyObject_Call(getinitargs_func, empty_tuple, NULL))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1799 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1800 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1801 | if ((len = PyObject_Size(class_args)) < 0) |
| 1802 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1803 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1804 | for (i = 0; i < len; i++) { |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1805 | if (!( element = PySequence_GetItem(class_args, i))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1806 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1807 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1808 | if (save(self, element, 0) < 0) { |
| 1809 | Py_DECREF(element); |
| 1810 | goto finally; |
| 1811 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1812 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1813 | Py_DECREF(element); |
| 1814 | } |
| 1815 | } |
| 1816 | else { |
| 1817 | PyErr_Clear(); |
| 1818 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1819 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1820 | if (!self->bin) { |
| 1821 | if (!( name = ((PyClassObject *)class)->cl_name )) { |
| 1822 | PyErr_SetString(PicklingError, "class has no name"); |
| 1823 | goto finally; |
| 1824 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1825 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1826 | if (!( module = whichmodule(class, name))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1827 | goto finally; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 1828 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 1829 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1830 | if ((module_size = PyString_Size(module)) < 0 || |
| 1831 | (name_size = PyString_Size(name)) < 0) |
| 1832 | goto finally; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 1833 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1834 | module_str = PyString_AS_STRING((PyStringObject *)module); |
| 1835 | name_str = PyString_AS_STRING((PyStringObject *)name); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1836 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1837 | if (self->write_func(self, &inst, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1838 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1839 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1840 | if (self->write_func(self, module_str, module_size) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1841 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1842 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1843 | if (self->write_func(self, "\n", 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1844 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1845 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1846 | if (self->write_func(self, name_str, name_size) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1847 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1848 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1849 | if (self->write_func(self, "\n", 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1850 | goto finally; |
| 1851 | } |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1852 | else if (self->write_func(self, &obj, 1) < 0) { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1853 | goto finally; |
| 1854 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1855 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1856 | if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) { |
| 1857 | state = PyObject_Call(getstate_func, empty_tuple, NULL); |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1858 | if (!state) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1859 | goto finally; |
| 1860 | } |
| 1861 | else { |
| 1862 | PyErr_Clear(); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1863 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1864 | if (!( state = PyObject_GetAttr(args, __dict___str))) { |
| 1865 | PyErr_Clear(); |
| 1866 | res = 0; |
| 1867 | goto finally; |
| 1868 | } |
| 1869 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1870 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1871 | if (!PyDict_Check(state)) { |
| 1872 | if (put2(self, args) < 0) |
| 1873 | goto finally; |
| 1874 | } |
| 1875 | else { |
| 1876 | if (put(self, args) < 0) |
| 1877 | goto finally; |
| 1878 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 1879 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1880 | if (save(self, state, 0) < 0) |
| 1881 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1882 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1883 | if (self->write_func(self, &build, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1884 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1885 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1886 | res = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1887 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1888 | finally: |
| 1889 | if (self->fast && !fast_save_leave(self, args)) |
| 1890 | res = -1; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 1891 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1892 | Py_XDECREF(module); |
| 1893 | Py_XDECREF(class); |
| 1894 | Py_XDECREF(state); |
| 1895 | Py_XDECREF(getinitargs_func); |
| 1896 | Py_XDECREF(getstate_func); |
| 1897 | Py_XDECREF(class_args); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1898 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1899 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1900 | } |
| 1901 | |
| 1902 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1903 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1904 | save_global(Picklerobject *self, PyObject *args, PyObject *name) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1905 | { |
Jeremy Hylton | 9ee91f1 | 2002-07-11 22:02:33 +0000 | [diff] [blame] | 1906 | PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1907 | char *name_str, *module_str; |
| 1908 | int module_size, name_size, res = -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1909 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1910 | static char global = GLOBAL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1911 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1912 | if (name) { |
| 1913 | global_name = name; |
| 1914 | Py_INCREF(global_name); |
| 1915 | } |
| 1916 | else { |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1917 | if (!( global_name = PyObject_GetAttr(args, __name___str))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1918 | goto finally; |
| 1919 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1920 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1921 | if (!( module = whichmodule(args, global_name))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1922 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1923 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1924 | if ((module_size = PyString_Size(module)) < 0 || |
| 1925 | (name_size = PyString_Size(global_name)) < 0) |
| 1926 | goto finally; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 1927 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1928 | module_str = PyString_AS_STRING((PyStringObject *)module); |
| 1929 | name_str = PyString_AS_STRING((PyStringObject *)global_name); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1930 | |
Guido van Rossum | 75bfd05 | 2002-12-24 18:10:07 +0000 | [diff] [blame] | 1931 | /* XXX This can be doing a relative import. Clearly it shouldn't, |
| 1932 | but I don't know how to stop it. :-( */ |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1933 | mod = PyImport_ImportModule(module_str); |
| 1934 | if (mod == NULL) { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1935 | cPickle_ErrFormat(PicklingError, |
Tim Peters | b9ce7cd | 2003-02-18 20:50:45 +0000 | [diff] [blame] | 1936 | "Can't pickle %s: import of module %s " |
| 1937 | "failed", |
| 1938 | "OS", args, module); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1939 | goto finally; |
| 1940 | } |
Jeremy Hylton | 0e1f7a8 | 2002-07-11 22:01:40 +0000 | [diff] [blame] | 1941 | klass = PyObject_GetAttrString(mod, name_str); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1942 | if (klass == NULL) { |
| 1943 | cPickle_ErrFormat(PicklingError, |
Tim Peters | b9ce7cd | 2003-02-18 20:50:45 +0000 | [diff] [blame] | 1944 | "Can't pickle %s: attribute lookup %s.%s " |
| 1945 | "failed", |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1946 | "OSS", args, module, global_name); |
| 1947 | goto finally; |
| 1948 | } |
| 1949 | if (klass != args) { |
Jeremy Hylton | 0e1f7a8 | 2002-07-11 22:01:40 +0000 | [diff] [blame] | 1950 | Py_DECREF(klass); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1951 | cPickle_ErrFormat(PicklingError, |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 1952 | "Can't pickle %s: it's not the same object " |
| 1953 | "as %s.%s", |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1954 | "OSS", args, module, global_name); |
| 1955 | goto finally; |
| 1956 | } |
Jeremy Hylton | 0e1f7a8 | 2002-07-11 22:01:40 +0000 | [diff] [blame] | 1957 | Py_DECREF(klass); |
Guido van Rossum | a92d16a | 2001-08-18 21:22:07 +0000 | [diff] [blame] | 1958 | |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 1959 | if (self->proto >= 2) { |
| 1960 | /* See whether this is in the extension registry, and if |
| 1961 | * so generate an EXT opcode. |
| 1962 | */ |
| 1963 | PyObject *py_code; /* extension code as Python object */ |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 1964 | long code; /* extension code as C value */ |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 1965 | char c_str[5]; |
| 1966 | int n; |
| 1967 | |
| 1968 | PyTuple_SET_ITEM(two_tuple, 0, module); |
| 1969 | PyTuple_SET_ITEM(two_tuple, 1, global_name); |
| 1970 | py_code = PyDict_GetItem(extension_registry, two_tuple); |
| 1971 | if (py_code == NULL) |
| 1972 | goto gen_global; /* not registered */ |
| 1973 | |
| 1974 | /* Verify py_code has the right type and value. */ |
| 1975 | if (!PyInt_Check(py_code)) { |
| 1976 | cPickle_ErrFormat(PicklingError, "Can't pickle %s: " |
Tim Peters | 6288e23 | 2003-02-05 03:53:10 +0000 | [diff] [blame] | 1977 | "extension code %s isn't an integer", |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 1978 | "OO", args, py_code); |
| 1979 | goto finally; |
| 1980 | } |
| 1981 | code = PyInt_AS_LONG(py_code); |
| 1982 | if (code <= 0 || code > 0x7fffffffL) { |
| 1983 | cPickle_ErrFormat(PicklingError, "Can't pickle %s: " |
| 1984 | "extension code %ld is out of range", |
| 1985 | "Ol", args, code); |
| 1986 | goto finally; |
| 1987 | } |
| 1988 | |
| 1989 | /* Generate an EXT opcode. */ |
| 1990 | if (code <= 0xff) { |
| 1991 | c_str[0] = EXT1; |
| 1992 | c_str[1] = (char)code; |
| 1993 | n = 2; |
| 1994 | } |
| 1995 | else if (code <= 0xffff) { |
| 1996 | c_str[0] = EXT2; |
| 1997 | c_str[1] = (char)(code & 0xff); |
| 1998 | c_str[2] = (char)((code >> 8) & 0xff); |
| 1999 | n = 3; |
| 2000 | } |
| 2001 | else { |
| 2002 | c_str[0] = EXT4; |
| 2003 | c_str[1] = (char)(code & 0xff); |
| 2004 | c_str[2] = (char)((code >> 8) & 0xff); |
| 2005 | c_str[3] = (char)((code >> 16) & 0xff); |
| 2006 | c_str[4] = (char)((code >> 24) & 0xff); |
| 2007 | n = 5; |
| 2008 | } |
| 2009 | |
| 2010 | if (self->write_func(self, c_str, n) >= 0) |
| 2011 | res = 0; |
| 2012 | goto finally; /* and don't memoize */ |
| 2013 | } |
| 2014 | |
| 2015 | gen_global: |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 2016 | if (self->write_func(self, &global, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2017 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2018 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 2019 | if (self->write_func(self, module_str, module_size) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2020 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2021 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 2022 | if (self->write_func(self, "\n", 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2023 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2024 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 2025 | if (self->write_func(self, name_str, name_size) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2026 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2027 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 2028 | if (self->write_func(self, "\n", 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2029 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2030 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2031 | if (put(self, args) < 0) |
| 2032 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2033 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2034 | res = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2035 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2036 | finally: |
| 2037 | Py_XDECREF(module); |
| 2038 | Py_XDECREF(global_name); |
| 2039 | Py_XDECREF(mod); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2040 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2041 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2042 | } |
| 2043 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2044 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2045 | save_pers(Picklerobject *self, PyObject *args, PyObject *f) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2046 | { |
| 2047 | PyObject *pid = 0; |
| 2048 | int size, res = -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2049 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2050 | static char persid = PERSID, binpersid = BINPERSID; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2051 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2052 | Py_INCREF(args); |
| 2053 | ARG_TUP(self, args); |
| 2054 | if (self->arg) { |
| 2055 | pid = PyObject_Call(f, self->arg, NULL); |
| 2056 | FREE_ARG_TUP(self); |
| 2057 | } |
| 2058 | if (! pid) return -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2059 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2060 | if (pid != Py_None) { |
| 2061 | if (!self->bin) { |
| 2062 | if (!PyString_Check(pid)) { |
| 2063 | PyErr_SetString(PicklingError, |
| 2064 | "persistent id must be string"); |
| 2065 | goto finally; |
| 2066 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2067 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 2068 | if (self->write_func(self, &persid, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2069 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2070 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2071 | if ((size = PyString_Size(pid)) < 0) |
| 2072 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2073 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 2074 | if (self->write_func(self, |
| 2075 | PyString_AS_STRING( |
| 2076 | (PyStringObject *)pid), |
| 2077 | size) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2078 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2079 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 2080 | if (self->write_func(self, "\n", 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2081 | goto finally; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2082 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2083 | res = 1; |
| 2084 | goto finally; |
| 2085 | } |
| 2086 | else if (save(self, pid, 1) >= 0) { |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 2087 | if (self->write_func(self, &binpersid, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2088 | res = -1; |
| 2089 | else |
| 2090 | res = 1; |
| 2091 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2092 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2093 | goto finally; |
| 2094 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2095 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2096 | res = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2097 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2098 | finally: |
| 2099 | Py_XDECREF(pid); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2100 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2101 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2102 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2103 | |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2104 | /* We're saving ob, and args is the 2-thru-5 tuple returned by the |
| 2105 | * appropriate __reduce__ method for ob. |
| 2106 | */ |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2107 | static int |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2108 | save_reduce(Picklerobject *self, PyObject *args, PyObject *ob) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2109 | { |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2110 | PyObject *callable; |
| 2111 | PyObject *argtup; |
| 2112 | PyObject *state = NULL; |
| 2113 | PyObject *listitems = NULL; |
| 2114 | PyObject *dictitems = NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2115 | |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2116 | int use_newobj = self->proto >= 2; |
| 2117 | |
| 2118 | static char reduce = REDUCE; |
| 2119 | static char build = BUILD; |
| 2120 | static char newobj = NEWOBJ; |
| 2121 | |
| 2122 | if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5, |
| 2123 | &callable, |
| 2124 | &argtup, |
| 2125 | &state, |
| 2126 | &listitems, |
| 2127 | &dictitems)) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2128 | return -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2129 | |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2130 | if (state == Py_None) |
| 2131 | state = NULL; |
| 2132 | if (listitems == Py_None) |
| 2133 | listitems = NULL; |
| 2134 | if (dictitems == Py_None) |
| 2135 | dictitems = NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2136 | |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2137 | /* Protocol 2 special case: if callable's name is __newobj__, use |
| 2138 | * NEWOBJ. This consumes a lot of code. |
| 2139 | */ |
| 2140 | if (use_newobj) { |
| 2141 | PyObject *temp = PyObject_GetAttr(callable, __name___str); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2142 | |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2143 | if (temp == NULL) { |
| 2144 | PyErr_Clear(); |
| 2145 | use_newobj = 0; |
| 2146 | } |
| 2147 | else { |
| 2148 | use_newobj = PyString_Check(temp) && |
| 2149 | strcmp(PyString_AS_STRING(temp), |
| 2150 | "__newobj__") == 0; |
| 2151 | Py_DECREF(temp); |
| 2152 | } |
| 2153 | } |
| 2154 | if (use_newobj) { |
| 2155 | PyObject *cls; |
| 2156 | PyObject *newargtup; |
| 2157 | int n, i; |
| 2158 | |
| 2159 | /* Sanity checks. */ |
| 2160 | n = PyTuple_Size(argtup); |
| 2161 | if (n < 1) { |
| 2162 | PyErr_SetString(PicklingError, "__newobj__ arglist " |
| 2163 | "is empty"); |
| 2164 | return -1; |
| 2165 | } |
| 2166 | |
| 2167 | cls = PyTuple_GET_ITEM(argtup, 0); |
| 2168 | if (! PyObject_HasAttrString(cls, "__new__")) { |
| 2169 | PyErr_SetString(PicklingError, "args[0] from " |
| 2170 | "__newobj__ args has no __new__"); |
| 2171 | return -1; |
| 2172 | } |
| 2173 | |
| 2174 | /* XXX How could ob be NULL? */ |
| 2175 | if (ob != NULL) { |
| 2176 | PyObject *ob_dot_class; |
| 2177 | |
| 2178 | ob_dot_class = PyObject_GetAttr(ob, __class___str); |
| 2179 | if (ob_dot_class == NULL) |
| 2180 | PyErr_Clear(); |
| 2181 | i = ob_dot_class != cls; /* true iff a problem */ |
| 2182 | Py_XDECREF(ob_dot_class); |
| 2183 | if (i) { |
| 2184 | PyErr_SetString(PicklingError, "args[0] from " |
| 2185 | "__newobj__ args has the wrong class"); |
| 2186 | return -1; |
| 2187 | } |
| 2188 | } |
| 2189 | |
| 2190 | /* Save the class and its __new__ arguments. */ |
| 2191 | if (save(self, cls, 0) < 0) |
| 2192 | return -1; |
| 2193 | |
| 2194 | newargtup = PyTuple_New(n-1); /* argtup[1:] */ |
| 2195 | if (newargtup == NULL) |
| 2196 | return -1; |
| 2197 | for (i = 1; i < n; ++i) { |
| 2198 | PyObject *temp = PyTuple_GET_ITEM(argtup, i); |
| 2199 | Py_INCREF(temp); |
| 2200 | PyTuple_SET_ITEM(newargtup, i-1, temp); |
| 2201 | } |
| 2202 | i = save(self, newargtup, 0) < 0; |
| 2203 | Py_DECREF(newargtup); |
| 2204 | if (i < 0) |
| 2205 | return -1; |
| 2206 | |
| 2207 | /* Add NEWOBJ opcode. */ |
| 2208 | if (self->write_func(self, &newobj, 1) < 0) |
| 2209 | return -1; |
| 2210 | } |
| 2211 | else { |
| 2212 | /* Not using NEWOBJ. */ |
| 2213 | if (save(self, callable, 0) < 0 || |
| 2214 | save(self, argtup, 0) < 0 || |
| 2215 | self->write_func(self, &reduce, 1) < 0) |
| 2216 | return -1; |
| 2217 | } |
| 2218 | |
| 2219 | /* Memoize. */ |
| 2220 | /* XXX How can ob be NULL? */ |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2221 | if (ob != NULL) { |
| 2222 | if (state && !PyDict_Check(state)) { |
| 2223 | if (put2(self, ob) < 0) |
| 2224 | return -1; |
| 2225 | } |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2226 | else if (put(self, ob) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2227 | return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2228 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2229 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2230 | |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2231 | if (listitems && batch_list(self, listitems) < 0) |
| 2232 | return -1; |
| 2233 | |
| 2234 | if (dictitems && batch_dict(self, dictitems) < 0) |
| 2235 | return -1; |
| 2236 | |
| 2237 | if (state) { |
| 2238 | if (save(self, state, 0) < 0 || |
| 2239 | self->write_func(self, &build, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2240 | return -1; |
| 2241 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2242 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2243 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2244 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2245 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2246 | static int |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 2247 | save(Picklerobject *self, PyObject *args, int pers_save) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2248 | { |
| 2249 | PyTypeObject *type; |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2250 | PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0; |
| 2251 | PyObject *arg_tup; |
| 2252 | int res = -1; |
| 2253 | int tmp, size; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2254 | |
Martin v. Löwis | 5a39530 | 2002-08-04 08:20:23 +0000 | [diff] [blame] | 2255 | if (self->nesting++ > Py_GetRecursionLimit()){ |
| 2256 | PyErr_SetString(PyExc_RuntimeError, |
| 2257 | "maximum recursion depth exceeded"); |
| 2258 | goto finally; |
| 2259 | } |
| 2260 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2261 | if (!pers_save && self->pers_func) { |
| 2262 | if ((tmp = save_pers(self, args, self->pers_func)) != 0) { |
| 2263 | res = tmp; |
| 2264 | goto finally; |
| 2265 | } |
| 2266 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2267 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2268 | if (args == Py_None) { |
| 2269 | res = save_none(self, args); |
| 2270 | goto finally; |
| 2271 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2272 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2273 | type = args->ob_type; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2274 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2275 | switch (type->tp_name[0]) { |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 2276 | case 'b': |
| 2277 | if (args == Py_False || args == Py_True) { |
| 2278 | res = save_bool(self, args); |
| 2279 | goto finally; |
| 2280 | } |
| 2281 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2282 | case 'i': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2283 | if (type == &PyInt_Type) { |
| 2284 | res = save_int(self, args); |
| 2285 | goto finally; |
| 2286 | } |
| 2287 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2288 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2289 | case 'l': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2290 | if (type == &PyLong_Type) { |
| 2291 | res = save_long(self, args); |
| 2292 | goto finally; |
| 2293 | } |
| 2294 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2295 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2296 | case 'f': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2297 | if (type == &PyFloat_Type) { |
| 2298 | res = save_float(self, args); |
| 2299 | goto finally; |
| 2300 | } |
| 2301 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2302 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2303 | case 't': |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 2304 | if (type == &PyTuple_Type && PyTuple_Size(args) == 0) { |
| 2305 | res = save_tuple(self, args); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2306 | goto finally; |
| 2307 | } |
| 2308 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2309 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2310 | case 's': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2311 | if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) { |
| 2312 | res = save_string(self, args, 0); |
| 2313 | goto finally; |
| 2314 | } |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 2315 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2316 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 2317 | case 'u': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2318 | if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) { |
| 2319 | res = save_unicode(self, args, 0); |
| 2320 | goto finally; |
| 2321 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2322 | #endif |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2323 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2324 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2325 | if (args->ob_refcnt > 1) { |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2326 | if (!( py_ob_id = PyLong_FromVoidPtr(args))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2327 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2328 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2329 | if (PyDict_GetItem(self->memo, py_ob_id)) { |
| 2330 | if (get(self, py_ob_id) < 0) |
| 2331 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2332 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2333 | res = 0; |
| 2334 | goto finally; |
| 2335 | } |
| 2336 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2337 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2338 | switch (type->tp_name[0]) { |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2339 | case 's': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2340 | if (type == &PyString_Type) { |
| 2341 | res = save_string(self, args, 1); |
| 2342 | goto finally; |
| 2343 | } |
| 2344 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2345 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2346 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 2347 | case 'u': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2348 | if (type == &PyUnicode_Type) { |
| 2349 | res = save_unicode(self, args, 1); |
| 2350 | goto finally; |
| 2351 | } |
| 2352 | break; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2353 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 2354 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2355 | case 't': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2356 | if (type == &PyTuple_Type) { |
| 2357 | res = save_tuple(self, args); |
| 2358 | goto finally; |
| 2359 | } |
| 2360 | if (type == &PyType_Type) { |
| 2361 | res = save_global(self, args, NULL); |
| 2362 | goto finally; |
| 2363 | } |
| 2364 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2365 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2366 | case 'l': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2367 | if (type == &PyList_Type) { |
| 2368 | res = save_list(self, args); |
| 2369 | goto finally; |
| 2370 | } |
| 2371 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2372 | |
| 2373 | case 'd': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2374 | if (type == &PyDict_Type) { |
| 2375 | res = save_dict(self, args); |
| 2376 | goto finally; |
| 2377 | } |
| 2378 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2379 | |
| 2380 | case 'i': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2381 | if (type == &PyInstance_Type) { |
| 2382 | res = save_inst(self, args); |
| 2383 | goto finally; |
| 2384 | } |
| 2385 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2386 | |
| 2387 | case 'c': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2388 | if (type == &PyClass_Type) { |
| 2389 | res = save_global(self, args, NULL); |
| 2390 | goto finally; |
| 2391 | } |
| 2392 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2393 | |
| 2394 | case 'f': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2395 | if (type == &PyFunction_Type) { |
| 2396 | res = save_global(self, args, NULL); |
| 2397 | goto finally; |
| 2398 | } |
| 2399 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2400 | |
| 2401 | case 'b': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2402 | if (type == &PyCFunction_Type) { |
| 2403 | res = save_global(self, args, NULL); |
| 2404 | goto finally; |
| 2405 | } |
| 2406 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2407 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2408 | if (!pers_save && self->inst_pers_func) { |
| 2409 | if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) { |
| 2410 | res = tmp; |
| 2411 | goto finally; |
| 2412 | } |
| 2413 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2414 | |
Jeremy Hylton | 39c6116 | 2002-07-16 19:47:43 +0000 | [diff] [blame] | 2415 | if (PyType_IsSubtype(type, &PyType_Type)) { |
| 2416 | res = save_global(self, args, NULL); |
| 2417 | goto finally; |
| 2418 | } |
| 2419 | |
Guido van Rossum | b289b87 | 2003-02-19 01:45:13 +0000 | [diff] [blame] | 2420 | /* Get a reduction callable, and call it. This may come from |
| 2421 | * copy_reg.dispatch_table, the object's __reduce_ex__ method, |
| 2422 | * or the object's __reduce__ method. |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2423 | */ |
Tim Peters | 5aa3da6 | 2003-02-13 21:03:57 +0000 | [diff] [blame] | 2424 | __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type); |
| 2425 | if (__reduce__ != NULL) { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2426 | Py_INCREF(__reduce__); |
Guido van Rossum | b289b87 | 2003-02-19 01:45:13 +0000 | [diff] [blame] | 2427 | Py_INCREF(args); |
| 2428 | ARG_TUP(self, args); |
| 2429 | if (self->arg) { |
| 2430 | t = PyObject_Call(__reduce__, self->arg, NULL); |
| 2431 | FREE_ARG_TUP(self); |
| 2432 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2433 | } |
| 2434 | else { |
Guido van Rossum | b289b87 | 2003-02-19 01:45:13 +0000 | [diff] [blame] | 2435 | /* Check for a __reduce_ex__ method. */ |
| 2436 | __reduce__ = PyObject_GetAttr(args, __reduce_ex___str); |
| 2437 | if (__reduce__ != NULL) { |
| 2438 | t = PyInt_FromLong(self->proto); |
| 2439 | if (t != NULL) { |
| 2440 | ARG_TUP(self, t); |
| 2441 | t = NULL; |
| 2442 | if (self->arg) { |
| 2443 | t = PyObject_Call(__reduce__, |
| 2444 | self->arg, NULL); |
| 2445 | FREE_ARG_TUP(self); |
| 2446 | } |
| 2447 | } |
| 2448 | } |
| 2449 | else { |
Tim Peters | 5aa3da6 | 2003-02-13 21:03:57 +0000 | [diff] [blame] | 2450 | PyErr_Clear(); |
Guido van Rossum | b289b87 | 2003-02-19 01:45:13 +0000 | [diff] [blame] | 2451 | /* Check for a __reduce__ method. */ |
| 2452 | __reduce__ = PyObject_GetAttr(args, __reduce___str); |
| 2453 | if (__reduce__ != NULL) { |
| 2454 | t = PyObject_Call(__reduce__, |
| 2455 | empty_tuple, NULL); |
| 2456 | } |
| 2457 | else { |
| 2458 | PyErr_SetObject(UnpickleableError, args); |
| 2459 | goto finally; |
| 2460 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2461 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2462 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2463 | |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2464 | if (t == NULL) |
| 2465 | goto finally; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2466 | |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2467 | if (PyString_Check(t)) { |
| 2468 | res = save_global(self, args, t); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2469 | goto finally; |
| 2470 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2471 | |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2472 | if (! PyTuple_Check(t)) { |
| 2473 | cPickle_ErrFormat(PicklingError, "Value returned by " |
| 2474 | "%s must be string or tuple", |
| 2475 | "O", __reduce__); |
| 2476 | goto finally; |
| 2477 | } |
| 2478 | |
| 2479 | size = PyTuple_Size(t); |
| 2480 | if (size < 2 || size > 5) { |
| 2481 | cPickle_ErrFormat(PicklingError, "tuple returned by " |
| 2482 | "%s must contain 2 through 5 elements", |
| 2483 | "O", __reduce__); |
| 2484 | goto finally; |
| 2485 | } |
| 2486 | |
| 2487 | arg_tup = PyTuple_GET_ITEM(t, 1); |
| 2488 | if (!(PyTuple_Check(arg_tup) || arg_tup == Py_None)) { |
| 2489 | cPickle_ErrFormat(PicklingError, "Second element of " |
| 2490 | "tuple returned by %s must be a tuple", |
| 2491 | "O", __reduce__); |
| 2492 | goto finally; |
| 2493 | } |
| 2494 | |
| 2495 | res = save_reduce(self, t, args); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2496 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2497 | finally: |
Martin v. Löwis | 5a39530 | 2002-08-04 08:20:23 +0000 | [diff] [blame] | 2498 | self->nesting--; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2499 | Py_XDECREF(py_ob_id); |
| 2500 | Py_XDECREF(__reduce__); |
| 2501 | Py_XDECREF(t); |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2502 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2503 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2504 | } |
| 2505 | |
| 2506 | |
| 2507 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2508 | dump(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2509 | { |
| 2510 | static char stop = STOP; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2511 | |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 2512 | if (self->proto >= 2) { |
| 2513 | char bytes[2]; |
| 2514 | |
| 2515 | bytes[0] = PROTO; |
Tim Peters | 87482ea | 2003-02-02 16:16:30 +0000 | [diff] [blame] | 2516 | assert(self->proto >= 0 && self->proto < 256); |
| 2517 | bytes[1] = (char)self->proto; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 2518 | if (self->write_func(self, bytes, 2) < 0) |
| 2519 | return -1; |
| 2520 | } |
| 2521 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2522 | if (save(self, args, 0) < 0) |
| 2523 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2524 | |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 2525 | if (self->write_func(self, &stop, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2526 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2527 | |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 2528 | if (self->write_func(self, NULL, 0) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2529 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2530 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2531 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
| 2534 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2535 | Pickle_clear_memo(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2536 | { |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2537 | if (self->memo) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2538 | PyDict_Clear(self->memo); |
| 2539 | Py_INCREF(Py_None); |
| 2540 | return Py_None; |
| 2541 | } |
| 2542 | |
| 2543 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2544 | Pickle_getvalue(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2545 | { |
| 2546 | int l, i, rsize, ssize, clear=1, lm; |
| 2547 | long ik; |
| 2548 | PyObject *k, *r; |
| 2549 | char *s, *p, *have_get; |
| 2550 | Pdata *data; |
| 2551 | |
| 2552 | /* Can be called by Python code or C code */ |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2553 | if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear)) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2554 | return NULL; |
| 2555 | |
| 2556 | /* Check to make sure we are based on a list */ |
| 2557 | if (! Pdata_Check(self->file)) { |
| 2558 | PyErr_SetString(PicklingError, |
| 2559 | "Attempt to getvalue() a non-list-based pickler"); |
| 2560 | return NULL; |
| 2561 | } |
| 2562 | |
| 2563 | /* flush write buffer */ |
| 2564 | if (write_other(self, NULL, 0) < 0) return NULL; |
| 2565 | |
| 2566 | data=(Pdata*)self->file; |
| 2567 | l=data->length; |
| 2568 | |
| 2569 | /* set up an array to hold get/put status */ |
Tim Peters | ac5687a | 2003-02-02 18:08:34 +0000 | [diff] [blame] | 2570 | lm = PyDict_Size(self->memo); |
| 2571 | if (lm < 0) return NULL; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2572 | lm++; |
Tim Peters | ac5687a | 2003-02-02 18:08:34 +0000 | [diff] [blame] | 2573 | have_get = malloc(lm); |
| 2574 | if (have_get == NULL) return PyErr_NoMemory(); |
| 2575 | memset(have_get, 0, lm); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2576 | |
| 2577 | /* Scan for gets. */ |
Tim Peters | ac5687a | 2003-02-02 18:08:34 +0000 | [diff] [blame] | 2578 | for (rsize = 0, i = l; --i >= 0; ) { |
| 2579 | k = data->data[i]; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2580 | |
Tim Peters | ac5687a | 2003-02-02 18:08:34 +0000 | [diff] [blame] | 2581 | if (PyString_Check(k)) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2582 | rsize += PyString_GET_SIZE(k); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2583 | |
| 2584 | else if (PyInt_Check(k)) { /* put */ |
Tim Peters | ac5687a | 2003-02-02 18:08:34 +0000 | [diff] [blame] | 2585 | ik = PyInt_AS_LONG((PyIntObject*)k); |
| 2586 | if (ik >= lm || ik == 0) { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2587 | PyErr_SetString(PicklingError, |
| 2588 | "Invalid get data"); |
| 2589 | return NULL; |
| 2590 | } |
Tim Peters | ac5687a | 2003-02-02 18:08:34 +0000 | [diff] [blame] | 2591 | if (have_get[ik]) /* with matching get */ |
| 2592 | rsize += ik < 256 ? 2 : 5; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2593 | } |
| 2594 | |
| 2595 | else if (! (PyTuple_Check(k) && |
| 2596 | PyTuple_GET_SIZE(k) == 2 && |
Tim Peters | ac5687a | 2003-02-02 18:08:34 +0000 | [diff] [blame] | 2597 | PyInt_Check((k = PyTuple_GET_ITEM(k, 0)))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2598 | ) { |
| 2599 | PyErr_SetString(PicklingError, |
| 2600 | "Unexpected data in internal list"); |
| 2601 | return NULL; |
| 2602 | } |
| 2603 | |
| 2604 | else { /* put */ |
Tim Peters | ac5687a | 2003-02-02 18:08:34 +0000 | [diff] [blame] | 2605 | ik = PyInt_AS_LONG((PyIntObject *)k); |
| 2606 | if (ik >= lm || ik == 0) { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2607 | PyErr_SetString(PicklingError, |
| 2608 | "Invalid get data"); |
| 2609 | return NULL; |
| 2610 | } |
Tim Peters | ac5687a | 2003-02-02 18:08:34 +0000 | [diff] [blame] | 2611 | have_get[ik] = 1; |
| 2612 | rsize += ik < 256 ? 2 : 5; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2613 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2614 | } |
| 2615 | |
| 2616 | /* Now generate the result */ |
Tim Peters | ac5687a | 2003-02-02 18:08:34 +0000 | [diff] [blame] | 2617 | r = PyString_FromStringAndSize(NULL, rsize); |
| 2618 | if (r == NULL) goto err; |
| 2619 | s = PyString_AS_STRING((PyStringObject *)r); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2620 | |
Tim Peters | ac5687a | 2003-02-02 18:08:34 +0000 | [diff] [blame] | 2621 | for (i = 0; i < l; i++) { |
| 2622 | k = data->data[i]; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2623 | |
| 2624 | if (PyString_Check(k)) { |
Tim Peters | ac5687a | 2003-02-02 18:08:34 +0000 | [diff] [blame] | 2625 | ssize = PyString_GET_SIZE(k); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2626 | if (ssize) { |
Tim Peters | ac5687a | 2003-02-02 18:08:34 +0000 | [diff] [blame] | 2627 | p=PyString_AS_STRING((PyStringObject *)k); |
| 2628 | while (--ssize >= 0) |
| 2629 | *s++ = *p++; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2630 | } |
| 2631 | } |
| 2632 | |
| 2633 | else if (PyTuple_Check(k)) { /* get */ |
Tim Peters | ac5687a | 2003-02-02 18:08:34 +0000 | [diff] [blame] | 2634 | ik = PyInt_AS_LONG((PyIntObject *) |
| 2635 | PyTuple_GET_ITEM(k, 0)); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2636 | if (ik < 256) { |
| 2637 | *s++ = BINGET; |
| 2638 | *s++ = (int)(ik & 0xff); |
| 2639 | } |
| 2640 | else { |
| 2641 | *s++ = LONG_BINGET; |
| 2642 | *s++ = (int)(ik & 0xff); |
| 2643 | *s++ = (int)((ik >> 8) & 0xff); |
| 2644 | *s++ = (int)((ik >> 16) & 0xff); |
| 2645 | *s++ = (int)((ik >> 24) & 0xff); |
| 2646 | } |
| 2647 | } |
| 2648 | |
| 2649 | else { /* put */ |
Tim Peters | ac5687a | 2003-02-02 18:08:34 +0000 | [diff] [blame] | 2650 | ik = PyInt_AS_LONG((PyIntObject*)k); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2651 | |
| 2652 | if (have_get[ik]) { /* with matching get */ |
| 2653 | if (ik < 256) { |
| 2654 | *s++ = BINPUT; |
| 2655 | *s++ = (int)(ik & 0xff); |
| 2656 | } |
| 2657 | else { |
| 2658 | *s++ = LONG_BINPUT; |
| 2659 | *s++ = (int)(ik & 0xff); |
| 2660 | *s++ = (int)((ik >> 8) & 0xff); |
| 2661 | *s++ = (int)((ik >> 16) & 0xff); |
| 2662 | *s++ = (int)((ik >> 24) & 0xff); |
| 2663 | } |
| 2664 | } |
| 2665 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2666 | } |
| 2667 | |
| 2668 | if (clear) { |
| 2669 | PyDict_Clear(self->memo); |
Tim Peters | ac5687a | 2003-02-02 18:08:34 +0000 | [diff] [blame] | 2670 | Pdata_clear(data, 0); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2671 | } |
| 2672 | |
| 2673 | free(have_get); |
| 2674 | return r; |
| 2675 | err: |
| 2676 | free(have_get); |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2677 | return NULL; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2678 | } |
| 2679 | |
| 2680 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2681 | Pickler_dump(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2682 | { |
| 2683 | PyObject *ob; |
| 2684 | int get=0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2685 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2686 | if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2687 | return NULL; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2688 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2689 | if (dump(self, ob) < 0) |
| 2690 | return NULL; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2691 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2692 | if (get) return Pickle_getvalue(self, NULL); |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2693 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2694 | /* XXX Why does dump() return self? */ |
| 2695 | Py_INCREF(self); |
| 2696 | return (PyObject*)self; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2697 | } |
| 2698 | |
| 2699 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2700 | static struct PyMethodDef Pickler_methods[] = |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2701 | { |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 2702 | {"dump", (PyCFunction)Pickler_dump, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 2703 | PyDoc_STR("dump(object) -- " |
| 2704 | "Write an object in pickle format to the object's pickle stream")}, |
Fred Drake | 0ebacc8 | 2002-05-01 20:36:39 +0000 | [diff] [blame] | 2705 | {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 2706 | PyDoc_STR("clear_memo() -- Clear the picklers memo")}, |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 2707 | {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 2708 | PyDoc_STR("getvalue() -- Finish picking a list-based pickle")}, |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2709 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2710 | }; |
| 2711 | |
| 2712 | |
| 2713 | static Picklerobject * |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 2714 | newPicklerobject(PyObject *file, int proto) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2715 | { |
| 2716 | Picklerobject *self; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2717 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 2718 | if (proto < 0) |
Tim Peters | 8587b3c | 2003-02-13 15:44:41 +0000 | [diff] [blame] | 2719 | proto = HIGHEST_PROTOCOL; |
| 2720 | if (proto > HIGHEST_PROTOCOL) { |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 2721 | PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; " |
| 2722 | "the highest available protocol is %d", |
Tim Peters | 8587b3c | 2003-02-13 15:44:41 +0000 | [diff] [blame] | 2723 | proto, HIGHEST_PROTOCOL); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2724 | return NULL; |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 2725 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2726 | |
Jeremy Hylton | 4cf6319 | 2003-04-09 21:05:12 +0000 | [diff] [blame^] | 2727 | self = PyObject_GC_New(Picklerobject, &Picklertype); |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 2728 | if (self == NULL) |
| 2729 | return NULL; |
| 2730 | self->proto = proto; |
| 2731 | self->bin = proto > 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2732 | self->fp = NULL; |
| 2733 | self->write = NULL; |
| 2734 | self->memo = NULL; |
| 2735 | self->arg = NULL; |
| 2736 | self->pers_func = NULL; |
| 2737 | self->inst_pers_func = NULL; |
| 2738 | self->write_buf = NULL; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2739 | self->fast = 0; |
Martin v. Löwis | 5a39530 | 2002-08-04 08:20:23 +0000 | [diff] [blame] | 2740 | self->nesting = 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2741 | self->fast_container = 0; |
| 2742 | self->fast_memo = NULL; |
| 2743 | self->buf_size = 0; |
| 2744 | self->dispatch_table = NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2745 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 2746 | self->file = NULL; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2747 | if (file) |
| 2748 | Py_INCREF(file); |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 2749 | else { |
| 2750 | file = Pdata_New(); |
| 2751 | if (file == NULL) |
| 2752 | goto err; |
| 2753 | } |
| 2754 | self->file = file; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2755 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2756 | if (!( self->memo = PyDict_New())) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2757 | goto err; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2758 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2759 | if (PyFile_Check(file)) { |
| 2760 | self->fp = PyFile_AsFile(file); |
| 2761 | if (self->fp == NULL) { |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 2762 | PyErr_SetString(PyExc_ValueError, |
| 2763 | "I/O operation on closed file"); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2764 | goto err; |
| 2765 | } |
| 2766 | self->write_func = write_file; |
Guido van Rossum | c91fcaa | 1999-03-29 20:00:14 +0000 | [diff] [blame] | 2767 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2768 | else if (PycStringIO_OutputCheck(file)) { |
| 2769 | self->write_func = write_cStringIO; |
| 2770 | } |
| 2771 | else if (file == Py_None) { |
| 2772 | self->write_func = write_none; |
| 2773 | } |
| 2774 | else { |
| 2775 | self->write_func = write_other; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2776 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2777 | if (! Pdata_Check(file)) { |
| 2778 | self->write = PyObject_GetAttr(file, write_str); |
| 2779 | if (!self->write) { |
| 2780 | PyErr_Clear(); |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2781 | PyErr_SetString(PyExc_TypeError, |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2782 | "argument must have 'write' " |
| 2783 | "attribute"); |
| 2784 | goto err; |
| 2785 | } |
| 2786 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2787 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 2788 | self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE); |
| 2789 | if (self->write_buf == NULL) { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2790 | PyErr_NoMemory(); |
| 2791 | goto err; |
| 2792 | } |
| 2793 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2794 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2795 | if (PyEval_GetRestricted()) { |
| 2796 | /* Restricted execution, get private tables */ |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 2797 | PyObject *m = PyImport_Import(copy_reg_str); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 2798 | |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 2799 | if (m == NULL) |
| 2800 | goto err; |
| 2801 | self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2802 | Py_DECREF(m); |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 2803 | if (self->dispatch_table == NULL) |
| 2804 | goto err; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2805 | } |
| 2806 | else { |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 2807 | self->dispatch_table = dispatch_table; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2808 | Py_INCREF(dispatch_table); |
| 2809 | } |
Jeremy Hylton | 4cf6319 | 2003-04-09 21:05:12 +0000 | [diff] [blame^] | 2810 | PyObject_GC_Track(self); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 2811 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2812 | return self; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 2813 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2814 | err: |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 2815 | Py_DECREF(self); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2816 | return NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2817 | } |
| 2818 | |
| 2819 | |
| 2820 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2821 | get_Pickler(PyObject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2822 | { |
| 2823 | PyObject *file = NULL; |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 2824 | int proto = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2825 | |
Tim Peters | 92c8bb3 | 2003-02-13 23:00:26 +0000 | [diff] [blame] | 2826 | /* XXX |
| 2827 | * The documented signature is Pickler(file, proto=0), but this |
| 2828 | * accepts Pickler() and Pickler(integer) too. The meaning then |
| 2829 | * is clear as mud, undocumented, and not supported by pickle.py. |
| 2830 | * I'm told Zope uses this, but I haven't traced into this code |
| 2831 | * far enough to figure out what it means. |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 2832 | */ |
| 2833 | if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2834 | PyErr_Clear(); |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 2835 | proto = 0; |
| 2836 | if (!PyArg_ParseTuple(args, "O|i:Pickler", &file, &proto)) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2837 | return NULL; |
| 2838 | } |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 2839 | return (PyObject *)newPicklerobject(file, proto); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2840 | } |
| 2841 | |
| 2842 | |
| 2843 | static void |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2844 | Pickler_dealloc(Picklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2845 | { |
Jeremy Hylton | 4cf6319 | 2003-04-09 21:05:12 +0000 | [diff] [blame^] | 2846 | PyObject_GC_UnTrack(self); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2847 | Py_XDECREF(self->write); |
| 2848 | Py_XDECREF(self->memo); |
| 2849 | Py_XDECREF(self->fast_memo); |
| 2850 | Py_XDECREF(self->arg); |
| 2851 | Py_XDECREF(self->file); |
| 2852 | Py_XDECREF(self->pers_func); |
| 2853 | Py_XDECREF(self->inst_pers_func); |
| 2854 | Py_XDECREF(self->dispatch_table); |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 2855 | PyMem_Free(self->write_buf); |
Jeremy Hylton | 4cf6319 | 2003-04-09 21:05:12 +0000 | [diff] [blame^] | 2856 | PyObject_GC_Del(self); |
| 2857 | } |
| 2858 | |
| 2859 | static int |
| 2860 | Pickler_traverse(Picklerobject *self, visitproc visit, void *arg) |
| 2861 | { |
| 2862 | int err; |
| 2863 | #define VISIT(SLOT) \ |
| 2864 | if (SLOT) { \ |
| 2865 | err = visit((PyObject *)(SLOT), arg); \ |
| 2866 | if (err) \ |
| 2867 | return err; \ |
| 2868 | } |
| 2869 | VISIT(self->write); |
| 2870 | VISIT(self->memo); |
| 2871 | VISIT(self->fast_memo); |
| 2872 | VISIT(self->arg); |
| 2873 | VISIT(self->file); |
| 2874 | VISIT(self->pers_func); |
| 2875 | VISIT(self->inst_pers_func); |
| 2876 | VISIT(self->dispatch_table); |
| 2877 | #undef VISIT |
| 2878 | return 0; |
| 2879 | } |
| 2880 | |
| 2881 | static int |
| 2882 | Pickler_clear(Picklerobject *self) |
| 2883 | { |
| 2884 | #define CLEAR(SLOT) Py_XDECREF(SLOT); SLOT = NULL; |
| 2885 | CLEAR(self->write); |
| 2886 | CLEAR(self->memo); |
| 2887 | CLEAR(self->fast_memo); |
| 2888 | CLEAR(self->arg); |
| 2889 | CLEAR(self->file); |
| 2890 | CLEAR(self->pers_func); |
| 2891 | CLEAR(self->inst_pers_func); |
| 2892 | CLEAR(self->dispatch_table); |
| 2893 | #undef CLEAR |
| 2894 | return 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2895 | } |
| 2896 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2897 | static PyObject * |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2898 | Pickler_get_pers_func(Picklerobject *p) |
| 2899 | { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2900 | if (p->pers_func == NULL) |
| 2901 | PyErr_SetString(PyExc_AttributeError, "persistent_id"); |
| 2902 | else |
| 2903 | Py_INCREF(p->pers_func); |
| 2904 | return p->pers_func; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2905 | } |
| 2906 | |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2907 | static int |
| 2908 | Pickler_set_pers_func(Picklerobject *p, PyObject *v) |
| 2909 | { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2910 | if (v == NULL) { |
| 2911 | PyErr_SetString(PyExc_TypeError, |
| 2912 | "attribute deletion is not supported"); |
| 2913 | return -1; |
| 2914 | } |
| 2915 | Py_XDECREF(p->pers_func); |
| 2916 | Py_INCREF(v); |
| 2917 | p->pers_func = v; |
| 2918 | return 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2919 | } |
| 2920 | |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2921 | static int |
| 2922 | Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v) |
| 2923 | { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2924 | if (v == NULL) { |
| 2925 | PyErr_SetString(PyExc_TypeError, |
| 2926 | "attribute deletion is not supported"); |
| 2927 | return -1; |
| 2928 | } |
| 2929 | Py_XDECREF(p->inst_pers_func); |
| 2930 | Py_INCREF(v); |
| 2931 | p->inst_pers_func = v; |
| 2932 | return 0; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2933 | } |
| 2934 | |
| 2935 | static PyObject * |
| 2936 | Pickler_get_memo(Picklerobject *p) |
| 2937 | { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2938 | if (p->memo == NULL) |
| 2939 | PyErr_SetString(PyExc_AttributeError, "memo"); |
| 2940 | else |
| 2941 | Py_INCREF(p->memo); |
| 2942 | return p->memo; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2943 | } |
| 2944 | |
| 2945 | static int |
| 2946 | Pickler_set_memo(Picklerobject *p, PyObject *v) |
| 2947 | { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2948 | if (v == NULL) { |
| 2949 | PyErr_SetString(PyExc_TypeError, |
| 2950 | "attribute deletion is not supported"); |
| 2951 | return -1; |
| 2952 | } |
| 2953 | if (!PyDict_Check(v)) { |
| 2954 | PyErr_SetString(PyExc_TypeError, "memo must be a dictionary"); |
| 2955 | return -1; |
| 2956 | } |
| 2957 | Py_XDECREF(p->memo); |
| 2958 | Py_INCREF(v); |
| 2959 | p->memo = v; |
| 2960 | return 0; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2961 | } |
| 2962 | |
| 2963 | static PyObject * |
| 2964 | Pickler_get_error(Picklerobject *p) |
| 2965 | { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2966 | /* why is this an attribute on the Pickler? */ |
| 2967 | Py_INCREF(PicklingError); |
| 2968 | return PicklingError; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2969 | } |
| 2970 | |
| 2971 | static PyMemberDef Pickler_members[] = { |
| 2972 | {"binary", T_INT, offsetof(Picklerobject, bin)}, |
| 2973 | {"fast", T_INT, offsetof(Picklerobject, fast)}, |
Jeremy Hylton | 3eb46f3 | 2001-10-16 17:10:49 +0000 | [diff] [blame] | 2974 | {NULL} |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2975 | }; |
| 2976 | |
| 2977 | static PyGetSetDef Pickler_getsets[] = { |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2978 | {"persistent_id", (getter)Pickler_get_pers_func, |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2979 | (setter)Pickler_set_pers_func}, |
| 2980 | {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func}, |
| 2981 | {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo}, |
Jeremy Hylton | 3eb46f3 | 2001-10-16 17:10:49 +0000 | [diff] [blame] | 2982 | {"PicklingError", (getter)Pickler_get_error, NULL}, |
| 2983 | {NULL} |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2984 | }; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2985 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2986 | PyDoc_STRVAR(Picklertype__doc__, |
| 2987 | "Objects that know how to pickle objects\n"); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2988 | |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 2989 | static PyTypeObject Picklertype = { |
| 2990 | PyObject_HEAD_INIT(NULL) |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2991 | 0, /*ob_size*/ |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 2992 | "cPickle.Pickler", /*tp_name*/ |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2993 | sizeof(Picklerobject), /*tp_basicsize*/ |
| 2994 | 0, |
| 2995 | (destructor)Pickler_dealloc, /* tp_dealloc */ |
| 2996 | 0, /* tp_print */ |
| 2997 | 0, /* tp_getattr */ |
| 2998 | 0, /* tp_setattr */ |
| 2999 | 0, /* tp_compare */ |
| 3000 | 0, /* tp_repr */ |
| 3001 | 0, /* tp_as_number */ |
| 3002 | 0, /* tp_as_sequence */ |
| 3003 | 0, /* tp_as_mapping */ |
| 3004 | 0, /* tp_hash */ |
| 3005 | 0, /* tp_call */ |
| 3006 | 0, /* tp_str */ |
Jason Tishler | fb8595d | 2003-01-06 12:41:26 +0000 | [diff] [blame] | 3007 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3008 | PyObject_GenericSetAttr, /* tp_setattro */ |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3009 | 0, /* tp_as_buffer */ |
Jeremy Hylton | 4cf6319 | 2003-04-09 21:05:12 +0000 | [diff] [blame^] | 3010 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3011 | Picklertype__doc__, /* tp_doc */ |
Jeremy Hylton | 4cf6319 | 2003-04-09 21:05:12 +0000 | [diff] [blame^] | 3012 | (traverseproc)Pickler_traverse, /* tp_traverse */ |
| 3013 | (inquiry)Pickler_clear, /* tp_clear */ |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3014 | 0, /* tp_richcompare */ |
| 3015 | 0, /* tp_weaklistoffset */ |
| 3016 | 0, /* tp_iter */ |
| 3017 | 0, /* tp_iternext */ |
| 3018 | Pickler_methods, /* tp_methods */ |
| 3019 | Pickler_members, /* tp_members */ |
| 3020 | Pickler_getsets, /* tp_getset */ |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 3021 | }; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3022 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3023 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3024 | find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3025 | { |
| 3026 | PyObject *global = 0, *module; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3027 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3028 | if (fc) { |
| 3029 | if (fc==Py_None) { |
Tim Peters | 92c8bb3 | 2003-02-13 23:00:26 +0000 | [diff] [blame] | 3030 | PyErr_SetString(UnpicklingError, "Global and instance " |
| 3031 | "pickles are not supported."); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3032 | return NULL; |
| 3033 | } |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3034 | return PyObject_CallFunction(fc, "OO", py_module_name, |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3035 | py_global_name); |
| 3036 | } |
Guido van Rossum | 1b9e0aa | 1999-04-19 17:58:18 +0000 | [diff] [blame] | 3037 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3038 | module = PySys_GetObject("modules"); |
| 3039 | if (module == NULL) |
| 3040 | return NULL; |
Jeremy Hylton | d105523 | 1998-08-11 19:52:51 +0000 | [diff] [blame] | 3041 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3042 | module = PyDict_GetItem(module, py_module_name); |
| 3043 | if (module == NULL) { |
| 3044 | module = PyImport_Import(py_module_name); |
| 3045 | if (!module) |
| 3046 | return NULL; |
| 3047 | global = PyObject_GetAttr(module, py_global_name); |
| 3048 | Py_DECREF(module); |
| 3049 | } |
| 3050 | else |
| 3051 | global = PyObject_GetAttr(module, py_global_name); |
| 3052 | return global; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3053 | } |
| 3054 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3055 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3056 | marker(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3057 | { |
| 3058 | if (self->num_marks < 1) { |
| 3059 | PyErr_SetString(UnpicklingError, "could not find MARK"); |
| 3060 | return -1; |
| 3061 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3062 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3063 | return self->marks[--self->num_marks]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3064 | } |
| 3065 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3066 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3067 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3068 | load_none(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3069 | { |
| 3070 | PDATA_APPEND(self->stack, Py_None, -1); |
| 3071 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3072 | } |
| 3073 | |
Guido van Rossum | f9ffb03 | 1999-02-04 14:54:04 +0000 | [diff] [blame] | 3074 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3075 | bad_readline(void) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3076 | { |
| 3077 | PyErr_SetString(UnpicklingError, "pickle data was truncated"); |
| 3078 | return -1; |
Guido van Rossum | f9ffb03 | 1999-02-04 14:54:04 +0000 | [diff] [blame] | 3079 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3080 | |
| 3081 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3082 | load_int(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3083 | { |
| 3084 | PyObject *py_int = 0; |
| 3085 | char *endptr, *s; |
| 3086 | int len, res = -1; |
| 3087 | long l; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3088 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3089 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3090 | if (len < 2) return bad_readline(); |
| 3091 | if (!( s=pystrndup(s,len))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3092 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3093 | errno = 0; |
| 3094 | l = strtol(s, &endptr, 0); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3095 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3096 | if (errno || (*endptr != '\n') || (endptr[1] != '\0')) { |
| 3097 | /* Hm, maybe we've got something long. Let's try reading |
| 3098 | it as a Python long object. */ |
| 3099 | errno = 0; |
| 3100 | py_int = PyLong_FromString(s, NULL, 0); |
| 3101 | if (py_int == NULL) { |
| 3102 | PyErr_SetString(PyExc_ValueError, |
| 3103 | "could not convert string to int"); |
| 3104 | goto finally; |
| 3105 | } |
| 3106 | } |
| 3107 | else { |
Guido van Rossum | e276339 | 2002-04-05 19:30:08 +0000 | [diff] [blame] | 3108 | if (len == 3 && (l == 0 || l == 1)) { |
| 3109 | if (!( py_int = PyBool_FromLong(l))) goto finally; |
| 3110 | } |
| 3111 | else { |
| 3112 | if (!( py_int = PyInt_FromLong(l))) goto finally; |
| 3113 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3114 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3115 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3116 | free(s); |
| 3117 | PDATA_PUSH(self->stack, py_int, -1); |
| 3118 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3119 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3120 | finally: |
| 3121 | free(s); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3122 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3123 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3124 | } |
| 3125 | |
Tim Peters | 3c67d79 | 2003-02-02 17:59:11 +0000 | [diff] [blame] | 3126 | static int |
| 3127 | load_bool(Unpicklerobject *self, PyObject *boolean) |
| 3128 | { |
| 3129 | assert(boolean == Py_True || boolean == Py_False); |
Tim Peters | e0a3907 | 2003-02-03 15:45:56 +0000 | [diff] [blame] | 3130 | PDATA_APPEND(self->stack, boolean, -1); |
Tim Peters | 3c67d79 | 2003-02-02 17:59:11 +0000 | [diff] [blame] | 3131 | return 0; |
| 3132 | } |
| 3133 | |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3134 | /* s contains x bytes of a little-endian integer. Return its value as a |
| 3135 | * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian |
| 3136 | * int, but when x is 4 it's a signed one. This is an historical source |
| 3137 | * of x-platform bugs. |
| 3138 | */ |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3139 | static long |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3140 | calc_binint(char *s, int x) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3141 | { |
| 3142 | unsigned char c; |
| 3143 | int i; |
| 3144 | long l; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3145 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3146 | for (i = 0, l = 0L; i < x; i++) { |
| 3147 | c = (unsigned char)s[i]; |
| 3148 | l |= (long)c << (i * 8); |
| 3149 | } |
Tim Peters | bfa18f7 | 2001-04-10 01:54:42 +0000 | [diff] [blame] | 3150 | #if SIZEOF_LONG > 4 |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3151 | /* Unlike BININT1 and BININT2, BININT (more accurately BININT4) |
| 3152 | * is signed, so on a box with longs bigger than 4 bytes we need |
| 3153 | * to extend a BININT's sign bit to the full width. |
| 3154 | */ |
| 3155 | if (x == 4 && l & (1L << 31)) |
| 3156 | l |= (~0L) << 32; |
Tim Peters | bfa18f7 | 2001-04-10 01:54:42 +0000 | [diff] [blame] | 3157 | #endif |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3158 | return l; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3159 | } |
| 3160 | |
| 3161 | |
| 3162 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3163 | load_binintx(Unpicklerobject *self, char *s, int x) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3164 | { |
| 3165 | PyObject *py_int = 0; |
| 3166 | long l; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3167 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3168 | l = calc_binint(s, x); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3169 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3170 | if (!( py_int = PyInt_FromLong(l))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3171 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3172 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3173 | PDATA_PUSH(self->stack, py_int, -1); |
| 3174 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3175 | } |
| 3176 | |
| 3177 | |
| 3178 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3179 | load_binint(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3180 | { |
| 3181 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3182 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3183 | if (self->read_func(self, &s, 4) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3184 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3185 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3186 | return load_binintx(self, s, 4); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3187 | } |
| 3188 | |
| 3189 | |
| 3190 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3191 | load_binint1(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3192 | { |
| 3193 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3194 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3195 | if (self->read_func(self, &s, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3196 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3197 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3198 | return load_binintx(self, s, 1); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3199 | } |
| 3200 | |
| 3201 | |
| 3202 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3203 | load_binint2(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3204 | { |
| 3205 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3206 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3207 | if (self->read_func(self, &s, 2) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3208 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3209 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3210 | return load_binintx(self, s, 2); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3211 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3212 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3213 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3214 | load_long(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3215 | { |
| 3216 | PyObject *l = 0; |
| 3217 | char *end, *s; |
| 3218 | int len, res = -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3219 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3220 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3221 | if (len < 2) return bad_readline(); |
| 3222 | if (!( s=pystrndup(s,len))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3223 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3224 | if (!( l = PyLong_FromString(s, &end, 0))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3225 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3226 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3227 | free(s); |
| 3228 | PDATA_PUSH(self->stack, l, -1); |
| 3229 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3230 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3231 | finally: |
| 3232 | free(s); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3233 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3234 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3235 | } |
| 3236 | |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3237 | /* 'size' bytes contain the # of bytes of little-endian 256's-complement |
| 3238 | * data following. |
| 3239 | */ |
| 3240 | static int |
| 3241 | load_counted_long(Unpicklerobject *self, int size) |
| 3242 | { |
| 3243 | int i; |
| 3244 | char *nbytes; |
| 3245 | unsigned char *pdata; |
| 3246 | PyObject *along; |
| 3247 | |
| 3248 | assert(size == 1 || size == 4); |
| 3249 | i = self->read_func(self, &nbytes, size); |
| 3250 | if (i < 0) return -1; |
| 3251 | |
| 3252 | size = calc_binint(nbytes, size); |
| 3253 | if (size < 0) { |
| 3254 | /* Corrupt or hostile pickle -- we never write one like |
| 3255 | * this. |
| 3256 | */ |
Tim Peters | 0c7c48e | 2003-02-03 22:07:24 +0000 | [diff] [blame] | 3257 | PyErr_SetString(UnpicklingError, "LONG pickle has negative " |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3258 | "byte count"); |
| 3259 | return -1; |
| 3260 | } |
| 3261 | |
| 3262 | if (size == 0) |
| 3263 | along = PyLong_FromLong(0L); |
| 3264 | else { |
| 3265 | /* Read the raw little-endian bytes & convert. */ |
Neal Norwitz | 529baf2 | 2003-02-02 17:08:33 +0000 | [diff] [blame] | 3266 | i = self->read_func(self, (char **)&pdata, size); |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3267 | if (i < 0) return -1; |
| 3268 | along = _PyLong_FromByteArray(pdata, (size_t)size, |
| 3269 | 1 /* little endian */, 1 /* signed */); |
| 3270 | } |
| 3271 | if (along == NULL) |
| 3272 | return -1; |
| 3273 | PDATA_PUSH(self->stack, along, -1); |
| 3274 | return 0; |
| 3275 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3276 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3277 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3278 | load_float(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3279 | { |
| 3280 | PyObject *py_float = 0; |
| 3281 | char *endptr, *s; |
| 3282 | int len, res = -1; |
| 3283 | double d; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3284 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3285 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3286 | if (len < 2) return bad_readline(); |
| 3287 | if (!( s=pystrndup(s,len))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3288 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3289 | errno = 0; |
| 3290 | d = strtod(s, &endptr); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3291 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3292 | if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) { |
| 3293 | PyErr_SetString(PyExc_ValueError, |
| 3294 | "could not convert string to float"); |
| 3295 | goto finally; |
| 3296 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3297 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3298 | if (!( py_float = PyFloat_FromDouble(d))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3299 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3300 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3301 | free(s); |
| 3302 | PDATA_PUSH(self->stack, py_float, -1); |
| 3303 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3304 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3305 | finally: |
| 3306 | free(s); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3307 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3308 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3309 | } |
| 3310 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3311 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3312 | load_binfloat(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3313 | { |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 3314 | PyObject *py_float; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3315 | double x; |
| 3316 | char *p; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3317 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3318 | if (self->read_func(self, &p, 8) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3319 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3320 | |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 3321 | x = _PyFloat_Unpack8((unsigned char *)p, 0); |
| 3322 | if (x == -1.0 && PyErr_Occurred()) |
| 3323 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3324 | |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 3325 | py_float = PyFloat_FromDouble(x); |
| 3326 | if (py_float == NULL) |
| 3327 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3328 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3329 | PDATA_PUSH(self->stack, py_float, -1); |
| 3330 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3331 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3332 | |
| 3333 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3334 | load_string(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3335 | { |
| 3336 | PyObject *str = 0; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 3337 | int len, res = -1; |
| 3338 | char *s, *p; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3339 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3340 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3341 | if (len < 2) return bad_readline(); |
| 3342 | if (!( s=pystrndup(s,len))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3343 | |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 3344 | |
| 3345 | /* Strip outermost quotes */ |
| 3346 | while (s[len-1] <= ' ') |
| 3347 | len--; |
| 3348 | if(s[0]=='"' && s[len-1]=='"'){ |
| 3349 | s[len-1] = '\0'; |
| 3350 | p = s + 1 ; |
| 3351 | len -= 2; |
| 3352 | } else if(s[0]=='\'' && s[len-1]=='\''){ |
| 3353 | s[len-1] = '\0'; |
| 3354 | p = s + 1 ; |
| 3355 | len -= 2; |
| 3356 | } else |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3357 | goto insecure; |
| 3358 | /********************************************/ |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 3359 | |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 3360 | str = PyString_DecodeEscape(p, len, NULL, 0, NULL); |
| 3361 | if (str) { |
| 3362 | PDATA_PUSH(self->stack, str, -1); |
| 3363 | res = 0; |
| 3364 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3365 | free(s); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3366 | return res; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3367 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3368 | insecure: |
| 3369 | free(s); |
| 3370 | PyErr_SetString(PyExc_ValueError,"insecure string pickle"); |
| 3371 | return -1; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3372 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3373 | |
| 3374 | |
| 3375 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3376 | load_binstring(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3377 | { |
| 3378 | PyObject *py_string = 0; |
| 3379 | long l; |
| 3380 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3381 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3382 | if (self->read_func(self, &s, 4) < 0) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3383 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3384 | l = calc_binint(s, 4); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3385 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3386 | if (self->read_func(self, &s, l) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3387 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3388 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3389 | if (!( py_string = PyString_FromStringAndSize(s, l))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3390 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3391 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3392 | PDATA_PUSH(self->stack, py_string, -1); |
| 3393 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3394 | } |
| 3395 | |
| 3396 | |
| 3397 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3398 | load_short_binstring(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3399 | { |
| 3400 | PyObject *py_string = 0; |
| 3401 | unsigned char l; |
| 3402 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3403 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3404 | if (self->read_func(self, &s, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3405 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3406 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3407 | l = (unsigned char)s[0]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3408 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3409 | if (self->read_func(self, &s, l) < 0) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3410 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3411 | if (!( py_string = PyString_FromStringAndSize(s, l))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3412 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3413 | PDATA_PUSH(self->stack, py_string, -1); |
| 3414 | return 0; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3415 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3416 | |
| 3417 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3418 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3419 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3420 | load_unicode(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3421 | { |
| 3422 | PyObject *str = 0; |
| 3423 | int len, res = -1; |
| 3424 | char *s; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3425 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3426 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3427 | if (len < 1) return bad_readline(); |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3428 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3429 | if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3430 | goto finally; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3431 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3432 | PDATA_PUSH(self->stack, str, -1); |
| 3433 | return 0; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3434 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3435 | finally: |
| 3436 | return res; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3437 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3438 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3439 | |
| 3440 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3441 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3442 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3443 | load_binunicode(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3444 | { |
| 3445 | PyObject *unicode; |
| 3446 | long l; |
| 3447 | char *s; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3448 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3449 | if (self->read_func(self, &s, 4) < 0) return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3450 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3451 | l = calc_binint(s, 4); |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3452 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3453 | if (self->read_func(self, &s, l) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3454 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3455 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3456 | if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3457 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3458 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3459 | PDATA_PUSH(self->stack, unicode, -1); |
| 3460 | return 0; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3461 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3462 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3463 | |
| 3464 | |
| 3465 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3466 | load_tuple(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3467 | { |
| 3468 | PyObject *tup; |
| 3469 | int i; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3470 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3471 | if ((i = marker(self)) < 0) return -1; |
| 3472 | if (!( tup=Pdata_popTuple(self->stack, i))) return -1; |
| 3473 | PDATA_PUSH(self->stack, tup, -1); |
| 3474 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3475 | } |
| 3476 | |
| 3477 | static int |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 3478 | load_counted_tuple(Unpicklerobject *self, int len) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3479 | { |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 3480 | PyObject *tup = PyTuple_New(len); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3481 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 3482 | if (tup == NULL) |
| 3483 | return -1; |
| 3484 | |
| 3485 | while (--len >= 0) { |
| 3486 | PyObject *element; |
| 3487 | |
| 3488 | PDATA_POP(self->stack, element); |
| 3489 | if (element == NULL) |
| 3490 | return -1; |
| 3491 | PyTuple_SET_ITEM(tup, len, element); |
| 3492 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3493 | PDATA_PUSH(self->stack, tup, -1); |
| 3494 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3495 | } |
| 3496 | |
| 3497 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3498 | load_empty_list(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3499 | { |
| 3500 | PyObject *list; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3501 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3502 | if (!( list=PyList_New(0))) return -1; |
| 3503 | PDATA_PUSH(self->stack, list, -1); |
| 3504 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3505 | } |
| 3506 | |
| 3507 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3508 | load_empty_dict(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3509 | { |
| 3510 | PyObject *dict; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3511 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3512 | if (!( dict=PyDict_New())) return -1; |
| 3513 | PDATA_PUSH(self->stack, dict, -1); |
| 3514 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3515 | } |
| 3516 | |
| 3517 | |
| 3518 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3519 | load_list(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3520 | { |
| 3521 | PyObject *list = 0; |
| 3522 | int i; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3523 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3524 | if ((i = marker(self)) < 0) return -1; |
| 3525 | if (!( list=Pdata_popList(self->stack, i))) return -1; |
| 3526 | PDATA_PUSH(self->stack, list, -1); |
| 3527 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3528 | } |
| 3529 | |
| 3530 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3531 | load_dict(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3532 | { |
| 3533 | PyObject *dict, *key, *value; |
| 3534 | int i, j, k; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3535 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3536 | if ((i = marker(self)) < 0) return -1; |
| 3537 | j=self->stack->length; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3538 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3539 | if (!( dict = PyDict_New())) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3540 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3541 | for (k = i+1; k < j; k += 2) { |
| 3542 | key =self->stack->data[k-1]; |
| 3543 | value=self->stack->data[k ]; |
| 3544 | if (PyDict_SetItem(dict, key, value) < 0) { |
| 3545 | Py_DECREF(dict); |
| 3546 | return -1; |
| 3547 | } |
| 3548 | } |
| 3549 | Pdata_clear(self->stack, i); |
| 3550 | PDATA_PUSH(self->stack, dict, -1); |
| 3551 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3552 | } |
| 3553 | |
| 3554 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3555 | Instance_New(PyObject *cls, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3556 | { |
Tim Peters | 1f1b2d2 | 2003-02-01 02:16:37 +0000 | [diff] [blame] | 3557 | PyObject *r = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3558 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3559 | if (PyClass_Check(cls)) { |
| 3560 | int l; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3561 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3562 | if ((l=PyObject_Size(args)) < 0) goto err; |
| 3563 | if (!( l )) { |
| 3564 | PyObject *__getinitargs__; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3565 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3566 | __getinitargs__ = PyObject_GetAttr(cls, |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3567 | __getinitargs___str); |
| 3568 | if (!__getinitargs__) { |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3569 | /* We have a class with no __getinitargs__, |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3570 | so bypass usual construction */ |
| 3571 | PyObject *inst; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3572 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3573 | PyErr_Clear(); |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3574 | if (!( inst=PyInstance_NewRaw(cls, NULL))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3575 | goto err; |
| 3576 | return inst; |
| 3577 | } |
| 3578 | Py_DECREF(__getinitargs__); |
| 3579 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3580 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3581 | if ((r=PyInstance_New(cls, args, NULL))) return r; |
| 3582 | else goto err; |
| 3583 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3584 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3585 | if (args==Py_None) { |
| 3586 | /* Special case, call cls.__basicnew__() */ |
| 3587 | PyObject *basicnew; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3588 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3589 | basicnew = PyObject_GetAttr(cls, __basicnew___str); |
| 3590 | if (!basicnew) return NULL; |
| 3591 | r=PyObject_CallObject(basicnew, NULL); |
| 3592 | Py_DECREF(basicnew); |
| 3593 | if (r) return r; |
| 3594 | } |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 3595 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3596 | if ((r=PyObject_CallObject(cls, args))) return r; |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 3597 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3598 | err: |
| 3599 | { |
| 3600 | PyObject *tp, *v, *tb; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3601 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3602 | PyErr_Fetch(&tp, &v, &tb); |
| 3603 | if ((r=Py_BuildValue("OOO",v,cls,args))) { |
| 3604 | Py_XDECREF(v); |
| 3605 | v=r; |
| 3606 | } |
| 3607 | PyErr_Restore(tp,v,tb); |
| 3608 | } |
| 3609 | return NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3610 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3611 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3612 | |
| 3613 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3614 | load_obj(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3615 | { |
| 3616 | PyObject *class, *tup, *obj=0; |
| 3617 | int i; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3618 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3619 | if ((i = marker(self)) < 0) return -1; |
| 3620 | if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1; |
| 3621 | PDATA_POP(self->stack, class); |
| 3622 | if (class) { |
| 3623 | obj = Instance_New(class, tup); |
| 3624 | Py_DECREF(class); |
| 3625 | } |
| 3626 | Py_DECREF(tup); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3627 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3628 | if (! obj) return -1; |
| 3629 | PDATA_PUSH(self->stack, obj, -1); |
| 3630 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3631 | } |
| 3632 | |
| 3633 | |
| 3634 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3635 | load_inst(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3636 | { |
| 3637 | PyObject *tup, *class=0, *obj=0, *module_name, *class_name; |
| 3638 | int i, len; |
| 3639 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3640 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3641 | if ((i = marker(self)) < 0) return -1; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3642 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3643 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3644 | if (len < 2) return bad_readline(); |
| 3645 | module_name = PyString_FromStringAndSize(s, len - 1); |
| 3646 | if (!module_name) return -1; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3647 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3648 | if ((len = self->readline_func(self, &s)) >= 0) { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3649 | if (len < 2) return bad_readline(); |
| 3650 | if ((class_name = PyString_FromStringAndSize(s, len - 1))) { |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3651 | class = find_class(module_name, class_name, |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3652 | self->find_class); |
| 3653 | Py_DECREF(class_name); |
| 3654 | } |
| 3655 | } |
| 3656 | Py_DECREF(module_name); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3657 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3658 | if (! class) return -1; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3659 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3660 | if ((tup=Pdata_popTuple(self->stack, i))) { |
| 3661 | obj = Instance_New(class, tup); |
| 3662 | Py_DECREF(tup); |
| 3663 | } |
| 3664 | Py_DECREF(class); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3665 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3666 | if (! obj) return -1; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3667 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3668 | PDATA_PUSH(self->stack, obj, -1); |
| 3669 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3670 | } |
| 3671 | |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 3672 | static int |
| 3673 | load_newobj(Unpicklerobject *self) |
| 3674 | { |
| 3675 | PyObject *args = NULL; |
| 3676 | PyObject *clsraw = NULL; |
| 3677 | PyTypeObject *cls; /* clsraw cast to its true type */ |
| 3678 | PyObject *obj; |
| 3679 | |
| 3680 | /* Stack is ... cls argtuple, and we want to call |
| 3681 | * cls.__new__(cls, *argtuple). |
| 3682 | */ |
| 3683 | PDATA_POP(self->stack, args); |
| 3684 | if (args == NULL) goto Fail; |
| 3685 | if (! PyTuple_Check(args)) { |
| 3686 | PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg " |
| 3687 | "tuple."); |
| 3688 | goto Fail; |
| 3689 | } |
| 3690 | |
| 3691 | PDATA_POP(self->stack, clsraw); |
| 3692 | cls = (PyTypeObject *)clsraw; |
| 3693 | if (cls == NULL) goto Fail; |
| 3694 | if (! PyType_Check(cls)) { |
| 3695 | PyErr_SetString(UnpicklingError, "NEWOBJ class argument " |
| 3696 | "isn't a type object"); |
| 3697 | goto Fail; |
| 3698 | } |
| 3699 | if (cls->tp_new == NULL) { |
| 3700 | PyErr_SetString(UnpicklingError, "NEWOBJ class argument " |
| 3701 | "has NULL tp_new"); |
| 3702 | goto Fail; |
| 3703 | } |
| 3704 | |
| 3705 | /* Call __new__. */ |
| 3706 | obj = cls->tp_new(cls, args, NULL); |
| 3707 | if (obj == NULL) goto Fail; |
| 3708 | |
| 3709 | Py_DECREF(args); |
| 3710 | Py_DECREF(clsraw); |
| 3711 | PDATA_PUSH(self->stack, obj, -1); |
| 3712 | return 0; |
| 3713 | |
| 3714 | Fail: |
| 3715 | Py_XDECREF(args); |
| 3716 | Py_XDECREF(clsraw); |
| 3717 | return -1; |
| 3718 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3719 | |
| 3720 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3721 | load_global(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3722 | { |
| 3723 | PyObject *class = 0, *module_name = 0, *class_name = 0; |
| 3724 | int len; |
| 3725 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3726 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3727 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3728 | if (len < 2) return bad_readline(); |
| 3729 | module_name = PyString_FromStringAndSize(s, len - 1); |
| 3730 | if (!module_name) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3731 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3732 | if ((len = self->readline_func(self, &s)) >= 0) { |
Tim Peters | 4e52ca8 | 2002-12-07 02:43:28 +0000 | [diff] [blame] | 3733 | if (len < 2) { |
| 3734 | Py_DECREF(module_name); |
| 3735 | return bad_readline(); |
| 3736 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3737 | if ((class_name = PyString_FromStringAndSize(s, len - 1))) { |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3738 | class = find_class(module_name, class_name, |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3739 | self->find_class); |
| 3740 | Py_DECREF(class_name); |
| 3741 | } |
| 3742 | } |
| 3743 | Py_DECREF(module_name); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3744 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3745 | if (! class) return -1; |
| 3746 | PDATA_PUSH(self->stack, class, -1); |
| 3747 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3748 | } |
| 3749 | |
| 3750 | |
| 3751 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3752 | load_persid(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3753 | { |
| 3754 | PyObject *pid = 0; |
| 3755 | int len; |
| 3756 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3757 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3758 | if (self->pers_func) { |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3759 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3760 | if (len < 2) return bad_readline(); |
| 3761 | |
| 3762 | pid = PyString_FromStringAndSize(s, len - 1); |
| 3763 | if (!pid) return -1; |
| 3764 | |
| 3765 | if (PyList_Check(self->pers_func)) { |
| 3766 | if (PyList_Append(self->pers_func, pid) < 0) { |
| 3767 | Py_DECREF(pid); |
| 3768 | return -1; |
| 3769 | } |
| 3770 | } |
| 3771 | else { |
| 3772 | ARG_TUP(self, pid); |
| 3773 | if (self->arg) { |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3774 | pid = PyObject_Call(self->pers_func, self->arg, |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3775 | NULL); |
| 3776 | FREE_ARG_TUP(self); |
| 3777 | } |
| 3778 | } |
| 3779 | |
| 3780 | if (! pid) return -1; |
| 3781 | |
| 3782 | PDATA_PUSH(self->stack, pid, -1); |
| 3783 | return 0; |
| 3784 | } |
| 3785 | else { |
| 3786 | PyErr_SetString(UnpicklingError, |
| 3787 | "A load persistent id instruction was encountered,\n" |
| 3788 | "but no persistent_load function was specified."); |
| 3789 | return -1; |
| 3790 | } |
| 3791 | } |
| 3792 | |
| 3793 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3794 | load_binpersid(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3795 | { |
| 3796 | PyObject *pid = 0; |
| 3797 | |
| 3798 | if (self->pers_func) { |
| 3799 | PDATA_POP(self->stack, pid); |
| 3800 | if (! pid) return -1; |
| 3801 | |
| 3802 | if (PyList_Check(self->pers_func)) { |
| 3803 | if (PyList_Append(self->pers_func, pid) < 0) { |
| 3804 | Py_DECREF(pid); |
| 3805 | return -1; |
| 3806 | } |
| 3807 | } |
| 3808 | else { |
| 3809 | ARG_TUP(self, pid); |
| 3810 | if (self->arg) { |
| 3811 | pid = PyObject_Call(self->pers_func, self->arg, |
| 3812 | NULL); |
| 3813 | FREE_ARG_TUP(self); |
| 3814 | } |
| 3815 | if (! pid) return -1; |
| 3816 | } |
| 3817 | |
| 3818 | PDATA_PUSH(self->stack, pid, -1); |
| 3819 | return 0; |
| 3820 | } |
| 3821 | else { |
| 3822 | PyErr_SetString(UnpicklingError, |
| 3823 | "A load persistent id instruction was encountered,\n" |
| 3824 | "but no persistent_load function was specified."); |
| 3825 | return -1; |
| 3826 | } |
| 3827 | } |
| 3828 | |
| 3829 | |
| 3830 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3831 | load_pop(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3832 | { |
| 3833 | int len; |
| 3834 | |
| 3835 | if (!( (len=self->stack->length) > 0 )) return stackUnderflow(); |
| 3836 | |
| 3837 | /* Note that we split the (pickle.py) stack into two stacks, |
| 3838 | an object stack and a mark stack. We have to be clever and |
| 3839 | pop the right one. We do this by looking at the top of the |
| 3840 | mark stack. |
| 3841 | */ |
| 3842 | |
| 3843 | if ((self->num_marks > 0) && |
| 3844 | (self->marks[self->num_marks - 1] == len)) |
| 3845 | self->num_marks--; |
| 3846 | else { |
| 3847 | len--; |
| 3848 | Py_DECREF(self->stack->data[len]); |
| 3849 | self->stack->length=len; |
| 3850 | } |
| 3851 | |
| 3852 | return 0; |
| 3853 | } |
| 3854 | |
| 3855 | |
| 3856 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3857 | load_pop_mark(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3858 | { |
| 3859 | int i; |
| 3860 | |
| 3861 | if ((i = marker(self)) < 0) |
| 3862 | return -1; |
| 3863 | |
| 3864 | Pdata_clear(self->stack, i); |
| 3865 | |
| 3866 | return 0; |
| 3867 | } |
| 3868 | |
| 3869 | |
| 3870 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3871 | load_dup(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3872 | { |
| 3873 | PyObject *last; |
| 3874 | int len; |
| 3875 | |
| 3876 | if ((len = self->stack->length) <= 0) return stackUnderflow(); |
| 3877 | last=self->stack->data[len-1]; |
| 3878 | Py_INCREF(last); |
| 3879 | PDATA_PUSH(self->stack, last, -1); |
| 3880 | return 0; |
| 3881 | } |
| 3882 | |
| 3883 | |
| 3884 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3885 | load_get(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3886 | { |
| 3887 | PyObject *py_str = 0, *value = 0; |
| 3888 | int len; |
| 3889 | char *s; |
| 3890 | int rc; |
| 3891 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3892 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
Guido van Rossum | f9ffb03 | 1999-02-04 14:54:04 +0000 | [diff] [blame] | 3893 | if (len < 2) return bad_readline(); |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3894 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3895 | if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3896 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3897 | value = PyDict_GetItem(self->memo, py_str); |
| 3898 | if (! value) { |
| 3899 | PyErr_SetObject(BadPickleGet, py_str); |
| 3900 | rc = -1; |
Tim Peters | 92c8bb3 | 2003-02-13 23:00:26 +0000 | [diff] [blame] | 3901 | } |
| 3902 | else { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3903 | PDATA_APPEND(self->stack, value, -1); |
| 3904 | rc = 0; |
| 3905 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3906 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3907 | Py_DECREF(py_str); |
| 3908 | return rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3909 | } |
| 3910 | |
| 3911 | |
| 3912 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3913 | load_binget(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3914 | { |
| 3915 | PyObject *py_key = 0, *value = 0; |
| 3916 | unsigned char key; |
| 3917 | char *s; |
| 3918 | int rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3919 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3920 | if (self->read_func(self, &s, 1) < 0) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3921 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3922 | key = (unsigned char)s[0]; |
| 3923 | if (!( py_key = PyInt_FromLong((long)key))) return -1; |
Guido van Rossum | ea2b715 | 2000-05-09 18:14:50 +0000 | [diff] [blame] | 3924 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3925 | value = PyDict_GetItem(self->memo, py_key); |
| 3926 | if (! value) { |
| 3927 | PyErr_SetObject(BadPickleGet, py_key); |
| 3928 | rc = -1; |
Tim Peters | 92c8bb3 | 2003-02-13 23:00:26 +0000 | [diff] [blame] | 3929 | } |
| 3930 | else { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3931 | PDATA_APPEND(self->stack, value, -1); |
| 3932 | rc = 0; |
| 3933 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3934 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3935 | Py_DECREF(py_key); |
| 3936 | return rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3937 | } |
| 3938 | |
| 3939 | |
| 3940 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3941 | load_long_binget(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3942 | { |
| 3943 | PyObject *py_key = 0, *value = 0; |
| 3944 | unsigned char c; |
| 3945 | char *s; |
| 3946 | long key; |
| 3947 | int rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3948 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3949 | if (self->read_func(self, &s, 4) < 0) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3950 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3951 | c = (unsigned char)s[0]; |
| 3952 | key = (long)c; |
| 3953 | c = (unsigned char)s[1]; |
| 3954 | key |= (long)c << 8; |
| 3955 | c = (unsigned char)s[2]; |
| 3956 | key |= (long)c << 16; |
| 3957 | c = (unsigned char)s[3]; |
| 3958 | key |= (long)c << 24; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3959 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3960 | if (!( py_key = PyInt_FromLong((long)key))) return -1; |
| 3961 | |
| 3962 | value = PyDict_GetItem(self->memo, py_key); |
| 3963 | if (! value) { |
| 3964 | PyErr_SetObject(BadPickleGet, py_key); |
| 3965 | rc = -1; |
Tim Peters | 92c8bb3 | 2003-02-13 23:00:26 +0000 | [diff] [blame] | 3966 | } |
| 3967 | else { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3968 | PDATA_APPEND(self->stack, value, -1); |
| 3969 | rc = 0; |
| 3970 | } |
| 3971 | |
| 3972 | Py_DECREF(py_key); |
| 3973 | return rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3974 | } |
| 3975 | |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 3976 | /* Push an object from the extension registry (EXT[124]). nbytes is |
| 3977 | * the number of bytes following the opcode, holding the index (code) value. |
| 3978 | */ |
| 3979 | static int |
| 3980 | load_extension(Unpicklerobject *self, int nbytes) |
| 3981 | { |
| 3982 | char *codebytes; /* the nbytes bytes after the opcode */ |
| 3983 | long code; /* calc_binint returns long */ |
| 3984 | PyObject *py_code; /* code as a Python int */ |
| 3985 | PyObject *obj; /* the object to push */ |
| 3986 | PyObject *pair; /* (module_name, class_name) */ |
| 3987 | PyObject *module_name, *class_name; |
| 3988 | |
| 3989 | assert(nbytes == 1 || nbytes == 2 || nbytes == 4); |
| 3990 | if (self->read_func(self, &codebytes, nbytes) < 0) return -1; |
| 3991 | code = calc_binint(codebytes, nbytes); |
| 3992 | if (code <= 0) { /* note that 0 is forbidden */ |
| 3993 | /* Corrupt or hostile pickle. */ |
| 3994 | PyErr_SetString(UnpicklingError, "EXT specifies code <= 0"); |
| 3995 | return -1; |
| 3996 | } |
| 3997 | |
| 3998 | /* Look for the code in the cache. */ |
| 3999 | py_code = PyInt_FromLong(code); |
| 4000 | if (py_code == NULL) return -1; |
| 4001 | obj = PyDict_GetItem(extension_cache, py_code); |
| 4002 | if (obj != NULL) { |
| 4003 | /* Bingo. */ |
| 4004 | Py_DECREF(py_code); |
| 4005 | PDATA_APPEND(self->stack, obj, -1); |
| 4006 | return 0; |
| 4007 | } |
| 4008 | |
| 4009 | /* Look up the (module_name, class_name) pair. */ |
| 4010 | pair = PyDict_GetItem(inverted_registry, py_code); |
| 4011 | if (pair == NULL) { |
| 4012 | Py_DECREF(py_code); |
| 4013 | PyErr_Format(PyExc_ValueError, "unregistered extension " |
| 4014 | "code %ld", code); |
| 4015 | return -1; |
| 4016 | } |
| 4017 | /* Since the extension registry is manipulable via Python code, |
Tim Peters | fa05ce3 | 2003-02-04 05:20:32 +0000 | [diff] [blame] | 4018 | * confirm that pair is really a 2-tuple of strings. |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4019 | */ |
| 4020 | if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 || |
| 4021 | !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) || |
| 4022 | !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) { |
| 4023 | Py_DECREF(py_code); |
| 4024 | PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] " |
| 4025 | "isn't a 2-tuple of strings", code); |
| 4026 | return -1; |
| 4027 | } |
| 4028 | /* Load the object. */ |
| 4029 | obj = find_class(module_name, class_name, self->find_class); |
| 4030 | if (obj == NULL) { |
| 4031 | Py_DECREF(py_code); |
| 4032 | return -1; |
| 4033 | } |
| 4034 | /* Cache code -> obj. */ |
| 4035 | code = PyDict_SetItem(extension_cache, py_code, obj); |
| 4036 | Py_DECREF(py_code); |
| 4037 | if (code < 0) { |
| 4038 | Py_DECREF(obj); |
| 4039 | return -1; |
| 4040 | } |
| 4041 | PDATA_PUSH(self->stack, obj, -1); |
| 4042 | return 0; |
| 4043 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4044 | |
| 4045 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4046 | load_put(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4047 | { |
| 4048 | PyObject *py_str = 0, *value = 0; |
| 4049 | int len, l; |
| 4050 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4051 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 4052 | if ((l = self->readline_func(self, &s)) < 0) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4053 | if (l < 2) return bad_readline(); |
| 4054 | if (!( len=self->stack->length )) return stackUnderflow(); |
| 4055 | if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1; |
| 4056 | value=self->stack->data[len-1]; |
| 4057 | l=PyDict_SetItem(self->memo, py_str, value); |
| 4058 | Py_DECREF(py_str); |
| 4059 | return l; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4060 | } |
| 4061 | |
| 4062 | |
| 4063 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4064 | load_binput(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4065 | { |
| 4066 | PyObject *py_key = 0, *value = 0; |
| 4067 | unsigned char key; |
| 4068 | char *s; |
| 4069 | int len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4070 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 4071 | if (self->read_func(self, &s, 1) < 0) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4072 | if (!( (len=self->stack->length) > 0 )) return stackUnderflow(); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4073 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4074 | key = (unsigned char)s[0]; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 4075 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4076 | if (!( py_key = PyInt_FromLong((long)key))) return -1; |
| 4077 | value=self->stack->data[len-1]; |
| 4078 | len=PyDict_SetItem(self->memo, py_key, value); |
| 4079 | Py_DECREF(py_key); |
| 4080 | return len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4081 | } |
| 4082 | |
| 4083 | |
| 4084 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4085 | load_long_binput(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4086 | { |
| 4087 | PyObject *py_key = 0, *value = 0; |
| 4088 | long key; |
| 4089 | unsigned char c; |
| 4090 | char *s; |
| 4091 | int len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4092 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 4093 | if (self->read_func(self, &s, 4) < 0) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4094 | if (!( len=self->stack->length )) return stackUnderflow(); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4095 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4096 | c = (unsigned char)s[0]; |
| 4097 | key = (long)c; |
| 4098 | c = (unsigned char)s[1]; |
| 4099 | key |= (long)c << 8; |
| 4100 | c = (unsigned char)s[2]; |
| 4101 | key |= (long)c << 16; |
| 4102 | c = (unsigned char)s[3]; |
| 4103 | key |= (long)c << 24; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4104 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4105 | if (!( py_key = PyInt_FromLong(key))) return -1; |
| 4106 | value=self->stack->data[len-1]; |
| 4107 | len=PyDict_SetItem(self->memo, py_key, value); |
| 4108 | Py_DECREF(py_key); |
| 4109 | return len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4110 | } |
| 4111 | |
| 4112 | |
| 4113 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4114 | do_append(Unpicklerobject *self, int x) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4115 | { |
| 4116 | PyObject *value = 0, *list = 0, *append_method = 0; |
| 4117 | int len, i; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4118 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4119 | len=self->stack->length; |
| 4120 | if (!( len >= x && x > 0 )) return stackUnderflow(); |
| 4121 | /* nothing to do */ |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4122 | if (len==x) return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4123 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4124 | list=self->stack->data[x-1]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4125 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4126 | if (PyList_Check(list)) { |
| 4127 | PyObject *slice; |
| 4128 | int list_len; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4129 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4130 | slice=Pdata_popList(self->stack, x); |
| 4131 | list_len = PyList_GET_SIZE(list); |
| 4132 | i=PyList_SetSlice(list, list_len, list_len, slice); |
| 4133 | Py_DECREF(slice); |
| 4134 | return i; |
| 4135 | } |
| 4136 | else { |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4137 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4138 | if (!( append_method = PyObject_GetAttr(list, append_str))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4139 | return -1; |
| 4140 | |
| 4141 | for (i = x; i < len; i++) { |
| 4142 | PyObject *junk; |
| 4143 | |
| 4144 | value=self->stack->data[i]; |
| 4145 | junk=0; |
| 4146 | ARG_TUP(self, value); |
| 4147 | if (self->arg) { |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4148 | junk = PyObject_Call(append_method, self->arg, |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4149 | NULL); |
| 4150 | FREE_ARG_TUP(self); |
| 4151 | } |
| 4152 | if (! junk) { |
| 4153 | Pdata_clear(self->stack, i+1); |
| 4154 | self->stack->length=x; |
| 4155 | Py_DECREF(append_method); |
| 4156 | return -1; |
| 4157 | } |
| 4158 | Py_DECREF(junk); |
| 4159 | } |
| 4160 | self->stack->length=x; |
| 4161 | Py_DECREF(append_method); |
| 4162 | } |
| 4163 | |
| 4164 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4165 | } |
| 4166 | |
| 4167 | |
| 4168 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4169 | load_append(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4170 | { |
| 4171 | return do_append(self, self->stack->length - 1); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4172 | } |
| 4173 | |
| 4174 | |
| 4175 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4176 | load_appends(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4177 | { |
| 4178 | return do_append(self, marker(self)); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4179 | } |
| 4180 | |
| 4181 | |
| 4182 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4183 | do_setitems(Unpicklerobject *self, int x) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4184 | { |
| 4185 | PyObject *value = 0, *key = 0, *dict = 0; |
| 4186 | int len, i, r=0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4187 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4188 | if (!( (len=self->stack->length) >= x |
| 4189 | && x > 0 )) return stackUnderflow(); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4190 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4191 | dict=self->stack->data[x-1]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4192 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4193 | for (i = x+1; i < len; i += 2) { |
| 4194 | key =self->stack->data[i-1]; |
| 4195 | value=self->stack->data[i ]; |
| 4196 | if (PyObject_SetItem(dict, key, value) < 0) { |
| 4197 | r=-1; |
| 4198 | break; |
| 4199 | } |
| 4200 | } |
| 4201 | |
| 4202 | Pdata_clear(self->stack, x); |
| 4203 | |
| 4204 | return r; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4205 | } |
| 4206 | |
| 4207 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4208 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4209 | load_setitem(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4210 | { |
| 4211 | return do_setitems(self, self->stack->length - 2); |
| 4212 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4213 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4214 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4215 | load_setitems(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4216 | { |
| 4217 | return do_setitems(self, marker(self)); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4218 | } |
| 4219 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4220 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4221 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4222 | load_build(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4223 | { |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 4224 | PyObject *state, *inst, *slotstate; |
| 4225 | PyObject *__setstate__; |
| 4226 | PyObject *d_key, *d_value; |
| 4227 | int i; |
| 4228 | int res = -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4229 | |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 4230 | /* Stack is ... instance, state. We want to leave instance at |
| 4231 | * the stack top, possibly mutated via instance.__setstate__(state). |
| 4232 | */ |
| 4233 | if (self->stack->length < 2) |
| 4234 | return stackUnderflow(); |
| 4235 | PDATA_POP(self->stack, state); |
| 4236 | if (state == NULL) |
| 4237 | return -1; |
| 4238 | inst = self->stack->data[self->stack->length - 1]; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4239 | |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 4240 | __setstate__ = PyObject_GetAttr(inst, __setstate___str); |
| 4241 | if (__setstate__ != NULL) { |
| 4242 | PyObject *junk = NULL; |
| 4243 | |
| 4244 | /* The explicit __setstate__ is responsible for everything. */ |
| 4245 | ARG_TUP(self, state); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4246 | if (self->arg) { |
| 4247 | junk = PyObject_Call(__setstate__, self->arg, NULL); |
| 4248 | FREE_ARG_TUP(self); |
| 4249 | } |
| 4250 | Py_DECREF(__setstate__); |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 4251 | if (junk == NULL) |
| 4252 | return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4253 | Py_DECREF(junk); |
| 4254 | return 0; |
| 4255 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4256 | PyErr_Clear(); |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 4257 | |
| 4258 | /* A default __setstate__. First see whether state embeds a |
| 4259 | * slot state dict too (a proto 2 addition). |
| 4260 | */ |
| 4261 | if (PyTuple_Check(state) && PyTuple_Size(state) == 2) { |
| 4262 | PyObject *temp = state; |
| 4263 | state = PyTuple_GET_ITEM(temp, 0); |
| 4264 | slotstate = PyTuple_GET_ITEM(temp, 1); |
| 4265 | Py_INCREF(state); |
| 4266 | Py_INCREF(slotstate); |
| 4267 | Py_DECREF(temp); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4268 | } |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 4269 | else |
| 4270 | slotstate = NULL; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4271 | |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 4272 | /* Set inst.__dict__ from the state dict (if any). */ |
| 4273 | if (state != Py_None) { |
| 4274 | PyObject *dict; |
| 4275 | if (! PyDict_Check(state)) { |
| 4276 | PyErr_SetString(UnpicklingError, "state is not a " |
| 4277 | "dictionary"); |
| 4278 | goto finally; |
| 4279 | } |
| 4280 | dict = PyObject_GetAttr(inst, __dict___str); |
| 4281 | if (dict == NULL) |
| 4282 | goto finally; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4283 | |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 4284 | i = 0; |
| 4285 | while (PyDict_Next(state, &i, &d_key, &d_value)) { |
| 4286 | if (PyObject_SetItem(dict, d_key, d_value) < 0) |
| 4287 | goto finally; |
| 4288 | } |
| 4289 | Py_DECREF(dict); |
| 4290 | } |
| 4291 | |
| 4292 | /* Also set instance attributes from the slotstate dict (if any). */ |
| 4293 | if (slotstate != NULL) { |
| 4294 | if (! PyDict_Check(slotstate)) { |
| 4295 | PyErr_SetString(UnpicklingError, "slot state is not " |
| 4296 | "a dictionary"); |
| 4297 | goto finally; |
| 4298 | } |
| 4299 | i = 0; |
| 4300 | while (PyDict_Next(slotstate, &i, &d_key, &d_value)) { |
| 4301 | if (PyObject_SetAttr(inst, d_key, d_value) < 0) |
| 4302 | goto finally; |
| 4303 | } |
| 4304 | } |
| 4305 | res = 0; |
| 4306 | |
| 4307 | finally: |
| 4308 | Py_DECREF(state); |
| 4309 | Py_XDECREF(slotstate); |
| 4310 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4311 | } |
| 4312 | |
| 4313 | |
| 4314 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4315 | load_mark(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4316 | { |
| 4317 | int s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4318 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4319 | /* Note that we split the (pickle.py) stack into two stacks, an |
| 4320 | object stack and a mark stack. Here we push a mark onto the |
| 4321 | mark stack. |
| 4322 | */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4323 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4324 | if ((self->num_marks + 1) >= self->marks_size) { |
| 4325 | s=self->marks_size+20; |
| 4326 | if (s <= self->num_marks) s=self->num_marks + 1; |
| 4327 | if (self->marks == NULL) |
| 4328 | self->marks=(int *)malloc(s * sizeof(int)); |
| 4329 | else |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4330 | self->marks=(int *)realloc(self->marks, |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4331 | s * sizeof(int)); |
| 4332 | if (! self->marks) { |
| 4333 | PyErr_NoMemory(); |
| 4334 | return -1; |
| 4335 | } |
| 4336 | self->marks_size = s; |
| 4337 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4338 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4339 | self->marks[self->num_marks++] = self->stack->length; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4340 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4341 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4342 | } |
| 4343 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4344 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4345 | load_reduce(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4346 | { |
| 4347 | PyObject *callable = 0, *arg_tup = 0, *ob = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4348 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4349 | PDATA_POP(self->stack, arg_tup); |
| 4350 | if (! arg_tup) return -1; |
| 4351 | PDATA_POP(self->stack, callable); |
| 4352 | if (callable) { |
| 4353 | ob = Instance_New(callable, arg_tup); |
| 4354 | Py_DECREF(callable); |
| 4355 | } |
| 4356 | Py_DECREF(arg_tup); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4357 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4358 | if (! ob) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4359 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4360 | PDATA_PUSH(self->stack, ob, -1); |
| 4361 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4362 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4363 | |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 4364 | /* Just raises an error if we don't know the protocol specified. PROTO |
| 4365 | * is the first opcode for protocols >= 2. |
| 4366 | */ |
| 4367 | static int |
| 4368 | load_proto(Unpicklerobject *self) |
| 4369 | { |
| 4370 | int i; |
| 4371 | char *protobyte; |
| 4372 | |
| 4373 | i = self->read_func(self, &protobyte, 1); |
| 4374 | if (i < 0) |
| 4375 | return -1; |
| 4376 | |
| 4377 | i = calc_binint(protobyte, 1); |
| 4378 | /* No point checking for < 0, since calc_binint returns an unsigned |
| 4379 | * int when chewing on 1 byte. |
| 4380 | */ |
| 4381 | assert(i >= 0); |
Tim Peters | 8587b3c | 2003-02-13 15:44:41 +0000 | [diff] [blame] | 4382 | if (i <= HIGHEST_PROTOCOL) |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 4383 | return 0; |
| 4384 | |
| 4385 | PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i); |
| 4386 | return -1; |
| 4387 | } |
| 4388 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4389 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4390 | load(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4391 | { |
| 4392 | PyObject *err = 0, *val = 0; |
| 4393 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4394 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4395 | self->num_marks = 0; |
| 4396 | if (self->stack->length) Pdata_clear(self->stack, 0); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4397 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4398 | while (1) { |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 4399 | if (self->read_func(self, &s, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4400 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4401 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4402 | switch (s[0]) { |
| 4403 | case NONE: |
| 4404 | if (load_none(self) < 0) |
| 4405 | break; |
| 4406 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4407 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4408 | case BININT: |
| 4409 | if (load_binint(self) < 0) |
| 4410 | break; |
| 4411 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4412 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4413 | case BININT1: |
| 4414 | if (load_binint1(self) < 0) |
| 4415 | break; |
| 4416 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4417 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4418 | case BININT2: |
| 4419 | if (load_binint2(self) < 0) |
| 4420 | break; |
| 4421 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4422 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4423 | case INT: |
| 4424 | if (load_int(self) < 0) |
| 4425 | break; |
| 4426 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4427 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4428 | case LONG: |
| 4429 | if (load_long(self) < 0) |
| 4430 | break; |
| 4431 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4432 | |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 4433 | case LONG1: |
| 4434 | if (load_counted_long(self, 1) < 0) |
| 4435 | break; |
| 4436 | continue; |
| 4437 | |
| 4438 | case LONG4: |
| 4439 | if (load_counted_long(self, 4) < 0) |
| 4440 | break; |
| 4441 | continue; |
| 4442 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4443 | case FLOAT: |
| 4444 | if (load_float(self) < 0) |
| 4445 | break; |
| 4446 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4447 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4448 | case BINFLOAT: |
| 4449 | if (load_binfloat(self) < 0) |
| 4450 | break; |
| 4451 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4452 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4453 | case BINSTRING: |
| 4454 | if (load_binstring(self) < 0) |
| 4455 | break; |
| 4456 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4457 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4458 | case SHORT_BINSTRING: |
| 4459 | if (load_short_binstring(self) < 0) |
| 4460 | break; |
| 4461 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4462 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4463 | case STRING: |
| 4464 | if (load_string(self) < 0) |
| 4465 | break; |
| 4466 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4467 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4468 | #ifdef Py_USING_UNICODE |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4469 | case UNICODE: |
| 4470 | if (load_unicode(self) < 0) |
| 4471 | break; |
| 4472 | continue; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 4473 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4474 | case BINUNICODE: |
| 4475 | if (load_binunicode(self) < 0) |
| 4476 | break; |
| 4477 | continue; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4478 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 4479 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4480 | case EMPTY_TUPLE: |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 4481 | if (load_counted_tuple(self, 0) < 0) |
| 4482 | break; |
| 4483 | continue; |
| 4484 | |
| 4485 | case TUPLE1: |
| 4486 | if (load_counted_tuple(self, 1) < 0) |
| 4487 | break; |
| 4488 | continue; |
| 4489 | |
| 4490 | case TUPLE2: |
| 4491 | if (load_counted_tuple(self, 2) < 0) |
| 4492 | break; |
| 4493 | continue; |
| 4494 | |
| 4495 | case TUPLE3: |
| 4496 | if (load_counted_tuple(self, 3) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4497 | break; |
| 4498 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4499 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4500 | case TUPLE: |
| 4501 | if (load_tuple(self) < 0) |
| 4502 | break; |
| 4503 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4504 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4505 | case EMPTY_LIST: |
| 4506 | if (load_empty_list(self) < 0) |
| 4507 | break; |
| 4508 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4509 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4510 | case LIST: |
| 4511 | if (load_list(self) < 0) |
| 4512 | break; |
| 4513 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4514 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4515 | case EMPTY_DICT: |
| 4516 | if (load_empty_dict(self) < 0) |
| 4517 | break; |
| 4518 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4519 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4520 | case DICT: |
| 4521 | if (load_dict(self) < 0) |
| 4522 | break; |
| 4523 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4524 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4525 | case OBJ: |
| 4526 | if (load_obj(self) < 0) |
| 4527 | break; |
| 4528 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4529 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4530 | case INST: |
| 4531 | if (load_inst(self) < 0) |
| 4532 | break; |
| 4533 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4534 | |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 4535 | case NEWOBJ: |
| 4536 | if (load_newobj(self) < 0) |
| 4537 | break; |
| 4538 | continue; |
| 4539 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4540 | case GLOBAL: |
| 4541 | if (load_global(self) < 0) |
| 4542 | break; |
| 4543 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4544 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4545 | case APPEND: |
| 4546 | if (load_append(self) < 0) |
| 4547 | break; |
| 4548 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4549 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4550 | case APPENDS: |
| 4551 | if (load_appends(self) < 0) |
| 4552 | break; |
| 4553 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4554 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4555 | case BUILD: |
| 4556 | if (load_build(self) < 0) |
| 4557 | break; |
| 4558 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4559 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4560 | case DUP: |
| 4561 | if (load_dup(self) < 0) |
| 4562 | break; |
| 4563 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4564 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4565 | case BINGET: |
| 4566 | if (load_binget(self) < 0) |
| 4567 | break; |
| 4568 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4569 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4570 | case LONG_BINGET: |
| 4571 | if (load_long_binget(self) < 0) |
| 4572 | break; |
| 4573 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4574 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4575 | case GET: |
| 4576 | if (load_get(self) < 0) |
| 4577 | break; |
| 4578 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4579 | |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4580 | case EXT1: |
| 4581 | if (load_extension(self, 1) < 0) |
| 4582 | break; |
| 4583 | continue; |
| 4584 | |
| 4585 | case EXT2: |
| 4586 | if (load_extension(self, 2) < 0) |
| 4587 | break; |
| 4588 | continue; |
| 4589 | |
| 4590 | case EXT4: |
| 4591 | if (load_extension(self, 4) < 0) |
| 4592 | break; |
| 4593 | continue; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4594 | case MARK: |
| 4595 | if (load_mark(self) < 0) |
| 4596 | break; |
| 4597 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4598 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4599 | case BINPUT: |
| 4600 | if (load_binput(self) < 0) |
| 4601 | break; |
| 4602 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4603 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4604 | case LONG_BINPUT: |
| 4605 | if (load_long_binput(self) < 0) |
| 4606 | break; |
| 4607 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4608 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4609 | case PUT: |
| 4610 | if (load_put(self) < 0) |
| 4611 | break; |
| 4612 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4613 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4614 | case POP: |
| 4615 | if (load_pop(self) < 0) |
| 4616 | break; |
| 4617 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4618 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4619 | case POP_MARK: |
| 4620 | if (load_pop_mark(self) < 0) |
| 4621 | break; |
| 4622 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4623 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4624 | case SETITEM: |
| 4625 | if (load_setitem(self) < 0) |
| 4626 | break; |
| 4627 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4628 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4629 | case SETITEMS: |
| 4630 | if (load_setitems(self) < 0) |
| 4631 | break; |
| 4632 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4633 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4634 | case STOP: |
| 4635 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4636 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4637 | case PERSID: |
| 4638 | if (load_persid(self) < 0) |
| 4639 | break; |
| 4640 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4641 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4642 | case BINPERSID: |
| 4643 | if (load_binpersid(self) < 0) |
| 4644 | break; |
| 4645 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4646 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4647 | case REDUCE: |
| 4648 | if (load_reduce(self) < 0) |
| 4649 | break; |
| 4650 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4651 | |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 4652 | case PROTO: |
| 4653 | if (load_proto(self) < 0) |
| 4654 | break; |
| 4655 | continue; |
| 4656 | |
Tim Peters | 3c67d79 | 2003-02-02 17:59:11 +0000 | [diff] [blame] | 4657 | case NEWTRUE: |
| 4658 | if (load_bool(self, Py_True) < 0) |
| 4659 | break; |
| 4660 | continue; |
| 4661 | |
| 4662 | case NEWFALSE: |
| 4663 | if (load_bool(self, Py_False) < 0) |
| 4664 | break; |
| 4665 | continue; |
| 4666 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4667 | case '\0': |
| 4668 | /* end of file */ |
| 4669 | PyErr_SetNone(PyExc_EOFError); |
| 4670 | break; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4671 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4672 | default: |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4673 | cPickle_ErrFormat(UnpicklingError, |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4674 | "invalid load key, '%s'.", |
| 4675 | "c", s[0]); |
| 4676 | return NULL; |
| 4677 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4678 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4679 | break; |
| 4680 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4681 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4682 | if ((err = PyErr_Occurred())) { |
| 4683 | if (err == PyExc_EOFError) { |
| 4684 | PyErr_SetNone(PyExc_EOFError); |
| 4685 | } |
| 4686 | return NULL; |
| 4687 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4688 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4689 | PDATA_POP(self->stack, val); |
| 4690 | return val; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4691 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4692 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4693 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4694 | /* No-load functions to support noload, which is used to |
| 4695 | find persistent references. */ |
| 4696 | |
| 4697 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4698 | noload_obj(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4699 | { |
| 4700 | int i; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4701 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4702 | if ((i = marker(self)) < 0) return -1; |
| 4703 | return Pdata_clear(self->stack, i+1); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4704 | } |
| 4705 | |
| 4706 | |
| 4707 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4708 | noload_inst(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4709 | { |
| 4710 | int i; |
| 4711 | char *s; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4712 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4713 | if ((i = marker(self)) < 0) return -1; |
| 4714 | Pdata_clear(self->stack, i); |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 4715 | if (self->readline_func(self, &s) < 0) return -1; |
| 4716 | if (self->readline_func(self, &s) < 0) return -1; |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 4717 | PDATA_APPEND(self->stack, Py_None, -1); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4718 | return 0; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4719 | } |
| 4720 | |
| 4721 | static int |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 4722 | noload_newobj(Unpicklerobject *self) |
| 4723 | { |
| 4724 | PyObject *obj; |
| 4725 | |
| 4726 | PDATA_POP(self->stack, obj); /* pop argtuple */ |
| 4727 | if (obj == NULL) return -1; |
| 4728 | Py_DECREF(obj); |
| 4729 | |
| 4730 | PDATA_POP(self->stack, obj); /* pop cls */ |
| 4731 | if (obj == NULL) return -1; |
| 4732 | Py_DECREF(obj); |
| 4733 | |
| 4734 | PDATA_APPEND(self->stack, Py_None, -1); |
| 4735 | return 0; |
| 4736 | } |
| 4737 | |
| 4738 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4739 | noload_global(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4740 | { |
| 4741 | char *s; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4742 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 4743 | if (self->readline_func(self, &s) < 0) return -1; |
| 4744 | if (self->readline_func(self, &s) < 0) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4745 | PDATA_APPEND(self->stack, Py_None,-1); |
| 4746 | return 0; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4747 | } |
| 4748 | |
| 4749 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4750 | noload_reduce(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4751 | { |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4752 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4753 | if (self->stack->length < 2) return stackUnderflow(); |
| 4754 | Pdata_clear(self->stack, self->stack->length-2); |
| 4755 | PDATA_APPEND(self->stack, Py_None,-1); |
| 4756 | return 0; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4757 | } |
| 4758 | |
| 4759 | static int |
| 4760 | noload_build(Unpicklerobject *self) { |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4761 | |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 4762 | if (self->stack->length < 1) return stackUnderflow(); |
| 4763 | Pdata_clear(self->stack, self->stack->length-1); |
Guido van Rossum | 50f385c | 1998-12-04 18:48:44 +0000 | [diff] [blame] | 4764 | return 0; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4765 | } |
| 4766 | |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4767 | static int |
| 4768 | noload_extension(Unpicklerobject *self, int nbytes) |
| 4769 | { |
| 4770 | char *codebytes; |
| 4771 | |
| 4772 | assert(nbytes == 1 || nbytes == 2 || nbytes == 4); |
| 4773 | if (self->read_func(self, &codebytes, nbytes) < 0) return -1; |
| 4774 | PDATA_APPEND(self->stack, Py_None, -1); |
| 4775 | return 0; |
| 4776 | } |
| 4777 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4778 | |
| 4779 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4780 | noload(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4781 | { |
| 4782 | PyObject *err = 0, *val = 0; |
| 4783 | char *s; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4784 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4785 | self->num_marks = 0; |
| 4786 | Pdata_clear(self->stack, 0); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4787 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4788 | while (1) { |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 4789 | if (self->read_func(self, &s, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4790 | break; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4791 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4792 | switch (s[0]) { |
| 4793 | case NONE: |
| 4794 | if (load_none(self) < 0) |
| 4795 | break; |
| 4796 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4797 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4798 | case BININT: |
| 4799 | if (load_binint(self) < 0) |
| 4800 | break; |
| 4801 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4802 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4803 | case BININT1: |
| 4804 | if (load_binint1(self) < 0) |
| 4805 | break; |
| 4806 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4807 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4808 | case BININT2: |
| 4809 | if (load_binint2(self) < 0) |
| 4810 | break; |
| 4811 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4812 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4813 | case INT: |
| 4814 | if (load_int(self) < 0) |
| 4815 | break; |
| 4816 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4817 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4818 | case LONG: |
| 4819 | if (load_long(self) < 0) |
| 4820 | break; |
| 4821 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4822 | |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 4823 | case LONG1: |
| 4824 | if (load_counted_long(self, 1) < 0) |
| 4825 | break; |
| 4826 | continue; |
| 4827 | |
| 4828 | case LONG4: |
| 4829 | if (load_counted_long(self, 4) < 0) |
| 4830 | break; |
| 4831 | continue; |
| 4832 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4833 | case FLOAT: |
| 4834 | if (load_float(self) < 0) |
| 4835 | break; |
| 4836 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4837 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4838 | case BINFLOAT: |
| 4839 | if (load_binfloat(self) < 0) |
| 4840 | break; |
| 4841 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4842 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4843 | case BINSTRING: |
| 4844 | if (load_binstring(self) < 0) |
| 4845 | break; |
| 4846 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4847 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4848 | case SHORT_BINSTRING: |
| 4849 | if (load_short_binstring(self) < 0) |
| 4850 | break; |
| 4851 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4852 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4853 | case STRING: |
| 4854 | if (load_string(self) < 0) |
| 4855 | break; |
| 4856 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4857 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4858 | #ifdef Py_USING_UNICODE |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4859 | case UNICODE: |
| 4860 | if (load_unicode(self) < 0) |
| 4861 | break; |
| 4862 | continue; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 4863 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4864 | case BINUNICODE: |
| 4865 | if (load_binunicode(self) < 0) |
| 4866 | break; |
| 4867 | continue; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4868 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 4869 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4870 | case EMPTY_TUPLE: |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 4871 | if (load_counted_tuple(self, 0) < 0) |
| 4872 | break; |
| 4873 | continue; |
| 4874 | |
| 4875 | case TUPLE1: |
| 4876 | if (load_counted_tuple(self, 1) < 0) |
| 4877 | break; |
| 4878 | continue; |
| 4879 | |
| 4880 | case TUPLE2: |
| 4881 | if (load_counted_tuple(self, 2) < 0) |
| 4882 | break; |
| 4883 | continue; |
| 4884 | |
| 4885 | case TUPLE3: |
| 4886 | if (load_counted_tuple(self, 3) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4887 | break; |
| 4888 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4889 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4890 | case TUPLE: |
| 4891 | if (load_tuple(self) < 0) |
| 4892 | break; |
| 4893 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4894 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4895 | case EMPTY_LIST: |
| 4896 | if (load_empty_list(self) < 0) |
| 4897 | break; |
| 4898 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4899 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4900 | case LIST: |
| 4901 | if (load_list(self) < 0) |
| 4902 | break; |
| 4903 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4904 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4905 | case EMPTY_DICT: |
| 4906 | if (load_empty_dict(self) < 0) |
| 4907 | break; |
| 4908 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4909 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4910 | case DICT: |
| 4911 | if (load_dict(self) < 0) |
| 4912 | break; |
| 4913 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4914 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4915 | case OBJ: |
| 4916 | if (noload_obj(self) < 0) |
| 4917 | break; |
| 4918 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4919 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4920 | case INST: |
| 4921 | if (noload_inst(self) < 0) |
| 4922 | break; |
| 4923 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4924 | |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 4925 | case NEWOBJ: |
| 4926 | if (noload_newobj(self) < 0) |
| 4927 | break; |
| 4928 | continue; |
| 4929 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4930 | case GLOBAL: |
| 4931 | if (noload_global(self) < 0) |
| 4932 | break; |
| 4933 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4934 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4935 | case APPEND: |
| 4936 | if (load_append(self) < 0) |
| 4937 | break; |
| 4938 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4939 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4940 | case APPENDS: |
| 4941 | if (load_appends(self) < 0) |
| 4942 | break; |
| 4943 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4944 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4945 | case BUILD: |
| 4946 | if (noload_build(self) < 0) |
| 4947 | break; |
| 4948 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4949 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4950 | case DUP: |
| 4951 | if (load_dup(self) < 0) |
| 4952 | break; |
| 4953 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4954 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4955 | case BINGET: |
| 4956 | if (load_binget(self) < 0) |
| 4957 | break; |
| 4958 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4959 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4960 | case LONG_BINGET: |
| 4961 | if (load_long_binget(self) < 0) |
| 4962 | break; |
| 4963 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4964 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4965 | case GET: |
| 4966 | if (load_get(self) < 0) |
| 4967 | break; |
| 4968 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4969 | |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4970 | case EXT1: |
| 4971 | if (noload_extension(self, 1) < 0) |
| 4972 | break; |
| 4973 | continue; |
| 4974 | |
| 4975 | case EXT2: |
| 4976 | if (noload_extension(self, 2) < 0) |
| 4977 | break; |
| 4978 | continue; |
| 4979 | |
| 4980 | case EXT4: |
| 4981 | if (noload_extension(self, 4) < 0) |
| 4982 | break; |
| 4983 | continue; |
| 4984 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4985 | case MARK: |
| 4986 | if (load_mark(self) < 0) |
| 4987 | break; |
| 4988 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4989 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4990 | case BINPUT: |
| 4991 | if (load_binput(self) < 0) |
| 4992 | break; |
| 4993 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4994 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4995 | case LONG_BINPUT: |
| 4996 | if (load_long_binput(self) < 0) |
| 4997 | break; |
| 4998 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4999 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5000 | case PUT: |
| 5001 | if (load_put(self) < 0) |
| 5002 | break; |
| 5003 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5004 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5005 | case POP: |
| 5006 | if (load_pop(self) < 0) |
| 5007 | break; |
| 5008 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5009 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5010 | case POP_MARK: |
| 5011 | if (load_pop_mark(self) < 0) |
| 5012 | break; |
| 5013 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5014 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5015 | case SETITEM: |
| 5016 | if (load_setitem(self) < 0) |
| 5017 | break; |
| 5018 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5019 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5020 | case SETITEMS: |
| 5021 | if (load_setitems(self) < 0) |
| 5022 | break; |
| 5023 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5024 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5025 | case STOP: |
| 5026 | break; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5027 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5028 | case PERSID: |
| 5029 | if (load_persid(self) < 0) |
| 5030 | break; |
| 5031 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5032 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5033 | case BINPERSID: |
| 5034 | if (load_binpersid(self) < 0) |
| 5035 | break; |
| 5036 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5037 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5038 | case REDUCE: |
| 5039 | if (noload_reduce(self) < 0) |
| 5040 | break; |
| 5041 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5042 | |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 5043 | case PROTO: |
| 5044 | if (load_proto(self) < 0) |
| 5045 | break; |
| 5046 | continue; |
| 5047 | |
Tim Peters | 3c67d79 | 2003-02-02 17:59:11 +0000 | [diff] [blame] | 5048 | case NEWTRUE: |
| 5049 | if (load_bool(self, Py_True) < 0) |
| 5050 | break; |
| 5051 | continue; |
| 5052 | |
| 5053 | case NEWFALSE: |
| 5054 | if (load_bool(self, Py_False) < 0) |
| 5055 | break; |
| 5056 | continue; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5057 | default: |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5058 | cPickle_ErrFormat(UnpicklingError, |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5059 | "invalid load key, '%s'.", |
| 5060 | "c", s[0]); |
| 5061 | return NULL; |
| 5062 | } |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5063 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5064 | break; |
| 5065 | } |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5066 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5067 | if ((err = PyErr_Occurred())) { |
| 5068 | if (err == PyExc_EOFError) { |
| 5069 | PyErr_SetNone(PyExc_EOFError); |
| 5070 | } |
| 5071 | return NULL; |
| 5072 | } |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5073 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5074 | PDATA_POP(self->stack, val); |
| 5075 | return val; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5076 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5077 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5078 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5079 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5080 | Unpickler_load(Unpicklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5081 | { |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5082 | if (!( PyArg_ParseTuple(args, ":load"))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5083 | return NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5084 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5085 | return load(self); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5086 | } |
| 5087 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5088 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5089 | Unpickler_noload(Unpicklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5090 | { |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5091 | if (!( PyArg_ParseTuple(args, ":noload"))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5092 | return NULL; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5093 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5094 | return noload(self); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5095 | } |
| 5096 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5097 | |
| 5098 | static struct PyMethodDef Unpickler_methods[] = { |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 5099 | {"load", (PyCFunction)Unpickler_load, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 5100 | PyDoc_STR("load() -- Load a pickle") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5101 | }, |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 5102 | {"noload", (PyCFunction)Unpickler_noload, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 5103 | PyDoc_STR( |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5104 | "noload() -- not load a pickle, but go through most of the motions\n" |
| 5105 | "\n" |
| 5106 | "This function can be used to read past a pickle without instantiating\n" |
| 5107 | "any objects or importing any modules. It can also be used to find all\n" |
| 5108 | "persistent references without instantiating any objects or importing\n" |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 5109 | "any modules.\n") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5110 | }, |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5111 | {NULL, NULL} /* sentinel */ |
| 5112 | }; |
| 5113 | |
| 5114 | |
| 5115 | static Unpicklerobject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5116 | newUnpicklerobject(PyObject *f) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5117 | { |
| 5118 | Unpicklerobject *self; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5119 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5120 | if (!( self = PyObject_New(Unpicklerobject, &Unpicklertype))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5121 | return NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5122 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5123 | self->file = NULL; |
| 5124 | self->arg = NULL; |
| 5125 | self->stack = (Pdata*)Pdata_New(); |
| 5126 | self->pers_func = NULL; |
| 5127 | self->last_string = NULL; |
| 5128 | self->marks = NULL; |
| 5129 | self->num_marks = 0; |
| 5130 | self->marks_size = 0; |
| 5131 | self->buf_size = 0; |
| 5132 | self->read = NULL; |
| 5133 | self->readline = NULL; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5134 | self->find_class = NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5135 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5136 | if (!( self->memo = PyDict_New())) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5137 | goto err; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5138 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5139 | Py_INCREF(f); |
| 5140 | self->file = f; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5141 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5142 | /* Set read, readline based on type of f */ |
| 5143 | if (PyFile_Check(f)) { |
| 5144 | self->fp = PyFile_AsFile(f); |
| 5145 | if (self->fp == NULL) { |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5146 | PyErr_SetString(PyExc_ValueError, |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5147 | "I/O operation on closed file"); |
| 5148 | goto err; |
| 5149 | } |
| 5150 | self->read_func = read_file; |
| 5151 | self->readline_func = readline_file; |
Guido van Rossum | c91fcaa | 1999-03-29 20:00:14 +0000 | [diff] [blame] | 5152 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5153 | else if (PycStringIO_InputCheck(f)) { |
| 5154 | self->fp = NULL; |
| 5155 | self->read_func = read_cStringIO; |
| 5156 | self->readline_func = readline_cStringIO; |
| 5157 | } |
| 5158 | else { |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5159 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5160 | self->fp = NULL; |
| 5161 | self->read_func = read_other; |
| 5162 | self->readline_func = readline_other; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5163 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5164 | if (!( (self->readline = PyObject_GetAttr(f, readline_str)) && |
| 5165 | (self->read = PyObject_GetAttr(f, read_str)))) { |
| 5166 | PyErr_Clear(); |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5167 | PyErr_SetString( PyExc_TypeError, |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5168 | "argument must have 'read' and " |
| 5169 | "'readline' attributes" ); |
| 5170 | goto err; |
| 5171 | } |
| 5172 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5173 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5174 | return self; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5175 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5176 | err: |
| 5177 | Py_DECREF((PyObject *)self); |
| 5178 | return NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5179 | } |
| 5180 | |
| 5181 | |
| 5182 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5183 | get_Unpickler(PyObject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5184 | { |
| 5185 | PyObject *file; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5186 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5187 | if (!( PyArg_ParseTuple(args, "O:Unpickler", &file))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5188 | return NULL; |
| 5189 | return (PyObject *)newUnpicklerobject(file); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5190 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5191 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5192 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5193 | static void |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5194 | Unpickler_dealloc(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5195 | { |
| 5196 | Py_XDECREF(self->readline); |
| 5197 | Py_XDECREF(self->read); |
| 5198 | Py_XDECREF(self->file); |
| 5199 | Py_XDECREF(self->memo); |
| 5200 | Py_XDECREF(self->stack); |
| 5201 | Py_XDECREF(self->pers_func); |
| 5202 | Py_XDECREF(self->arg); |
| 5203 | Py_XDECREF(self->last_string); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5204 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5205 | if (self->marks) { |
| 5206 | free(self->marks); |
| 5207 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5208 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5209 | if (self->buf_size) { |
| 5210 | free(self->buf); |
| 5211 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5212 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5213 | PyObject_Del(self); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5214 | } |
| 5215 | |
| 5216 | |
| 5217 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5218 | Unpickler_getattr(Unpicklerobject *self, char *name) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5219 | { |
| 5220 | if (!strcmp(name, "persistent_load")) { |
| 5221 | if (!self->pers_func) { |
| 5222 | PyErr_SetString(PyExc_AttributeError, name); |
| 5223 | return NULL; |
| 5224 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5225 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5226 | Py_INCREF(self->pers_func); |
| 5227 | return self->pers_func; |
| 5228 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5229 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5230 | if (!strcmp(name, "find_global")) { |
| 5231 | if (!self->find_class) { |
| 5232 | PyErr_SetString(PyExc_AttributeError, name); |
| 5233 | return NULL; |
| 5234 | } |
Guido van Rossum | 1b9e0aa | 1999-04-19 17:58:18 +0000 | [diff] [blame] | 5235 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5236 | Py_INCREF(self->find_class); |
| 5237 | return self->find_class; |
| 5238 | } |
Guido van Rossum | 1b9e0aa | 1999-04-19 17:58:18 +0000 | [diff] [blame] | 5239 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5240 | if (!strcmp(name, "memo")) { |
| 5241 | if (!self->memo) { |
| 5242 | PyErr_SetString(PyExc_AttributeError, name); |
| 5243 | return NULL; |
| 5244 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5245 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5246 | Py_INCREF(self->memo); |
| 5247 | return self->memo; |
| 5248 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5249 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5250 | if (!strcmp(name, "UnpicklingError")) { |
| 5251 | Py_INCREF(UnpicklingError); |
| 5252 | return UnpicklingError; |
| 5253 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5254 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5255 | return Py_FindMethod(Unpickler_methods, (PyObject *)self, name); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5256 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5257 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5258 | |
| 5259 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5260 | Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5261 | { |
Jeremy Hylton | ce616e4 | 1998-08-13 23:13:52 +0000 | [diff] [blame] | 5262 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5263 | if (!strcmp(name, "persistent_load")) { |
| 5264 | Py_XDECREF(self->pers_func); |
| 5265 | self->pers_func = value; |
| 5266 | Py_XINCREF(value); |
| 5267 | return 0; |
| 5268 | } |
Guido van Rossum | 1b9e0aa | 1999-04-19 17:58:18 +0000 | [diff] [blame] | 5269 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5270 | if (!strcmp(name, "find_global")) { |
| 5271 | Py_XDECREF(self->find_class); |
| 5272 | self->find_class = value; |
| 5273 | Py_XINCREF(value); |
| 5274 | return 0; |
| 5275 | } |
Guido van Rossum | 1b9e0aa | 1999-04-19 17:58:18 +0000 | [diff] [blame] | 5276 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5277 | if (! value) { |
| 5278 | PyErr_SetString(PyExc_TypeError, |
| 5279 | "attribute deletion is not supported"); |
| 5280 | return -1; |
| 5281 | } |
Jeremy Hylton | ce616e4 | 1998-08-13 23:13:52 +0000 | [diff] [blame] | 5282 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5283 | if (strcmp(name, "memo") == 0) { |
| 5284 | if (!PyDict_Check(value)) { |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5285 | PyErr_SetString(PyExc_TypeError, |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5286 | "memo must be a dictionary"); |
| 5287 | return -1; |
| 5288 | } |
| 5289 | Py_XDECREF(self->memo); |
| 5290 | self->memo = value; |
| 5291 | Py_INCREF(value); |
| 5292 | return 0; |
| 5293 | } |
Jeremy Hylton | ce616e4 | 1998-08-13 23:13:52 +0000 | [diff] [blame] | 5294 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5295 | PyErr_SetString(PyExc_AttributeError, name); |
| 5296 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5297 | } |
| 5298 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5299 | /* --------------------------------------------------------------------------- |
| 5300 | * Module-level functions. |
| 5301 | */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5302 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5303 | /* dump(obj, file, proto=0). */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5304 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5305 | cpm_dump(PyObject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5306 | { |
| 5307 | PyObject *ob, *file, *res = NULL; |
| 5308 | Picklerobject *pickler = 0; |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5309 | int proto = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5310 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5311 | if (!( PyArg_ParseTuple(args, "OO|i", &ob, &file, &proto))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5312 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5313 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5314 | if (!( pickler = newPicklerobject(file, proto))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5315 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5316 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5317 | if (dump(pickler, ob) < 0) |
| 5318 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5319 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5320 | Py_INCREF(Py_None); |
| 5321 | res = Py_None; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5322 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5323 | finally: |
| 5324 | Py_XDECREF(pickler); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5325 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5326 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5327 | } |
| 5328 | |
| 5329 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5330 | /* dumps(obj, proto=0). */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5331 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5332 | cpm_dumps(PyObject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5333 | { |
| 5334 | PyObject *ob, *file = 0, *res = NULL; |
| 5335 | Picklerobject *pickler = 0; |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5336 | int proto = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5337 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5338 | if (!( PyArg_ParseTuple(args, "O|i:dumps", &ob, &proto))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5339 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5340 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5341 | if (!( file = PycStringIO->NewOutput(128))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5342 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5343 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5344 | if (!( pickler = newPicklerobject(file, proto))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5345 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5346 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5347 | if (dump(pickler, ob) < 0) |
| 5348 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5349 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5350 | res = PycStringIO->cgetvalue(file); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5351 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5352 | finally: |
| 5353 | Py_XDECREF(pickler); |
| 5354 | Py_XDECREF(file); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5355 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5356 | return res; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5357 | } |
| 5358 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5359 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5360 | /* load(fileobj). */ |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5361 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5362 | cpm_load(PyObject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5363 | { |
| 5364 | Unpicklerobject *unpickler = 0; |
| 5365 | PyObject *ob, *res = NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5366 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5367 | if (!( PyArg_ParseTuple(args, "O:load", &ob))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5368 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5369 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5370 | if (!( unpickler = newUnpicklerobject(ob))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5371 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5372 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5373 | res = load(unpickler); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5374 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5375 | finally: |
| 5376 | Py_XDECREF(unpickler); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5377 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5378 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5379 | } |
| 5380 | |
| 5381 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5382 | /* loads(string) */ |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5383 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5384 | cpm_loads(PyObject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5385 | { |
| 5386 | PyObject *ob, *file = 0, *res = NULL; |
| 5387 | Unpicklerobject *unpickler = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5388 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5389 | if (!( PyArg_ParseTuple(args, "S:loads", &ob))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5390 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5391 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5392 | if (!( file = PycStringIO->NewInput(ob))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5393 | goto finally; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5394 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5395 | if (!( unpickler = newUnpicklerobject(file))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5396 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5397 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5398 | res = load(unpickler); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5399 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5400 | finally: |
| 5401 | Py_XDECREF(file); |
| 5402 | Py_XDECREF(unpickler); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5403 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5404 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5405 | } |
| 5406 | |
| 5407 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5408 | PyDoc_STRVAR(Unpicklertype__doc__, |
| 5409 | "Objects that know how to unpickle"); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5410 | |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 5411 | static PyTypeObject Unpicklertype = { |
| 5412 | PyObject_HEAD_INIT(NULL) |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5413 | 0, /*ob_size*/ |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 5414 | "cPickle.Unpickler", /*tp_name*/ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5415 | sizeof(Unpicklerobject), /*tp_basicsize*/ |
| 5416 | 0, /*tp_itemsize*/ |
| 5417 | /* methods */ |
| 5418 | (destructor)Unpickler_dealloc, /*tp_dealloc*/ |
| 5419 | (printfunc)0, /*tp_print*/ |
| 5420 | (getattrfunc)Unpickler_getattr, /*tp_getattr*/ |
| 5421 | (setattrfunc)Unpickler_setattr, /*tp_setattr*/ |
| 5422 | (cmpfunc)0, /*tp_compare*/ |
| 5423 | (reprfunc)0, /*tp_repr*/ |
| 5424 | 0, /*tp_as_number*/ |
| 5425 | 0, /*tp_as_sequence*/ |
| 5426 | 0, /*tp_as_mapping*/ |
| 5427 | (hashfunc)0, /*tp_hash*/ |
| 5428 | (ternaryfunc)0, /*tp_call*/ |
| 5429 | (reprfunc)0, /*tp_str*/ |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5430 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5431 | /* Space for future expansion */ |
| 5432 | 0L,0L,0L,0L, |
| 5433 | Unpicklertype__doc__ /* Documentation string */ |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 5434 | }; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5435 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5436 | static struct PyMethodDef cPickle_methods[] = { |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 5437 | {"dump", (PyCFunction)cpm_dump, METH_VARARGS, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5438 | PyDoc_STR("dump(object, file, proto=0) -- " |
| 5439 | "Write an object in pickle format to the given file.\n" |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5440 | "\n" |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5441 | "See the Pickler docstring for the meaning of optional argument proto.") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5442 | }, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5443 | |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 5444 | {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5445 | PyDoc_STR("dumps(object, proto=0) -- " |
| 5446 | "Return a string containing an object in pickle format.\n" |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5447 | "\n" |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5448 | "See the Pickler docstring for the meaning of optional argument proto.") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5449 | }, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5450 | |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 5451 | {"load", (PyCFunction)cpm_load, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 5452 | PyDoc_STR("load(file) -- Load a pickle from the given file")}, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5453 | |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 5454 | {"loads", (PyCFunction)cpm_loads, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 5455 | PyDoc_STR("loads(string) -- Load a pickle from the given string")}, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5456 | |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 5457 | {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5458 | PyDoc_STR("Pickler(file, proto=0) -- Create a pickler.\n" |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5459 | "\n" |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5460 | "This takes a file-like object for writing a pickle data stream.\n" |
| 5461 | "The optional proto argument tells the pickler to use the given\n" |
| 5462 | "protocol; supported protocols are 0, 1, 2. The default\n" |
| 5463 | "protocol is 0, to be backwards compatible. (Protocol 0 is the\n" |
| 5464 | "only protocol that can be written to a file opened in text\n" |
| 5465 | "mode and read back successfully. When using a protocol higher\n" |
| 5466 | "than 0, make sure the file is opened in binary mode, both when\n" |
| 5467 | "pickling and unpickling.)\n" |
| 5468 | "\n" |
| 5469 | "Protocol 1 is more efficient than protocol 0; protocol 2 is\n" |
| 5470 | "more efficient than protocol 1.\n" |
| 5471 | "\n" |
| 5472 | "Specifying a negative protocol version selects the highest\n" |
| 5473 | "protocol version supported. The higher the protocol used, the\n" |
| 5474 | "more recent the version of Python needed to read the pickle\n" |
| 5475 | "produced.\n" |
| 5476 | "\n" |
| 5477 | "The file parameter must have a write() method that accepts a single\n" |
| 5478 | "string argument. It can thus be an open file object, a StringIO\n" |
| 5479 | "object, or any other custom object that meets this interface.\n") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5480 | }, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5481 | |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 5482 | {"Unpickler", (PyCFunction)get_Unpickler, METH_VARARGS, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5483 | PyDoc_STR("Unpickler(file) -- Create an unpickler.")}, |
| 5484 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5485 | { NULL, NULL } |
| 5486 | }; |
| 5487 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5488 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5489 | init_stuff(PyObject *module_dict) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5490 | { |
| 5491 | PyObject *copy_reg, *t, *r; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5492 | |
Martin v. Löwis | 43c9d8a | 2002-04-01 12:34:33 +0000 | [diff] [blame] | 5493 | #define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5494 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5495 | INIT_STR(__class__); |
| 5496 | INIT_STR(__getinitargs__); |
| 5497 | INIT_STR(__dict__); |
| 5498 | INIT_STR(__getstate__); |
| 5499 | INIT_STR(__setstate__); |
| 5500 | INIT_STR(__name__); |
| 5501 | INIT_STR(__main__); |
| 5502 | INIT_STR(__reduce__); |
Guido van Rossum | b289b87 | 2003-02-19 01:45:13 +0000 | [diff] [blame] | 5503 | INIT_STR(__reduce_ex__); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5504 | INIT_STR(write); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5505 | INIT_STR(append); |
| 5506 | INIT_STR(read); |
| 5507 | INIT_STR(readline); |
| 5508 | INIT_STR(copy_reg); |
| 5509 | INIT_STR(dispatch_table); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5510 | INIT_STR(__basicnew__); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5511 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5512 | if (!( copy_reg = PyImport_ImportModule("copy_reg"))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5513 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5514 | |
Tim Peters | 1f1b2d2 | 2003-02-01 02:16:37 +0000 | [diff] [blame] | 5515 | /* This is special because we want to use a different |
| 5516 | one in restricted mode. */ |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5517 | dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str); |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 5518 | if (!dispatch_table) return -1; |
| 5519 | |
| 5520 | extension_registry = PyObject_GetAttrString(copy_reg, |
Guido van Rossum | d4b920c | 2003-02-04 01:54:49 +0000 | [diff] [blame] | 5521 | "_extension_registry"); |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 5522 | if (!extension_registry) return -1; |
| 5523 | |
| 5524 | inverted_registry = PyObject_GetAttrString(copy_reg, |
Guido van Rossum | d4b920c | 2003-02-04 01:54:49 +0000 | [diff] [blame] | 5525 | "_inverted_registry"); |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 5526 | if (!inverted_registry) return -1; |
| 5527 | |
| 5528 | extension_cache = PyObject_GetAttrString(copy_reg, |
Guido van Rossum | d4b920c | 2003-02-04 01:54:49 +0000 | [diff] [blame] | 5529 | "_extension_cache"); |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 5530 | if (!extension_cache) return -1; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5531 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5532 | Py_DECREF(copy_reg); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5533 | |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 5534 | if (!(empty_tuple = PyTuple_New(0))) |
| 5535 | return -1; |
| 5536 | |
| 5537 | two_tuple = PyTuple_New(2); |
| 5538 | if (two_tuple == NULL) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5539 | return -1; |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 5540 | /* We use this temp container with no regard to refcounts, or to |
| 5541 | * keeping containees alive. Exempt from GC, because we don't |
| 5542 | * want anything looking at two_tuple() by magic. |
| 5543 | */ |
| 5544 | PyObject_GC_UnTrack(two_tuple); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5545 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5546 | /* Ugh */ |
| 5547 | if (!( t=PyImport_ImportModule("__builtin__"))) return -1; |
| 5548 | if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0) |
| 5549 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5550 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5551 | if (!( t=PyDict_New())) return -1; |
| 5552 | if (!( r=PyRun_String( |
| 5553 | "def __init__(self, *args): self.args=args\n\n" |
| 5554 | "def __str__(self):\n" |
| 5555 | " return self.args and ('%s' % self.args[0]) or '(what)'\n", |
| 5556 | Py_file_input, |
| 5557 | module_dict, t) )) return -1; |
| 5558 | Py_DECREF(r); |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5559 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5560 | PickleError = PyErr_NewException("cPickle.PickleError", NULL, t); |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5561 | if (!PickleError) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5562 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5563 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5564 | Py_DECREF(t); |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5565 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5566 | PicklingError = PyErr_NewException("cPickle.PicklingError", |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5567 | PickleError, NULL); |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5568 | if (!PicklingError) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5569 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5570 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5571 | if (!( t=PyDict_New())) return -1; |
| 5572 | if (!( r=PyRun_String( |
| 5573 | "def __init__(self, *args): self.args=args\n\n" |
| 5574 | "def __str__(self):\n" |
| 5575 | " a=self.args\n" |
| 5576 | " a=a and type(a[0]) or '(what)'\n" |
| 5577 | " return 'Cannot pickle %s objects' % a\n" |
| 5578 | , Py_file_input, |
| 5579 | module_dict, t) )) return -1; |
| 5580 | Py_DECREF(r); |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5581 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5582 | if (!( UnpickleableError = PyErr_NewException( |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5583 | "cPickle.UnpickleableError", PicklingError, t))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5584 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5585 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5586 | Py_DECREF(t); |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5587 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5588 | if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError", |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5589 | PickleError, NULL))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5590 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5591 | |
Martin v. Löwis | 658009a | 2002-09-16 17:26:24 +0000 | [diff] [blame] | 5592 | if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet", |
| 5593 | UnpicklingError, NULL))) |
| 5594 | return -1; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5595 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5596 | if (PyDict_SetItemString(module_dict, "PickleError", |
| 5597 | PickleError) < 0) |
| 5598 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5599 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5600 | if (PyDict_SetItemString(module_dict, "PicklingError", |
| 5601 | PicklingError) < 0) |
| 5602 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5603 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5604 | if (PyDict_SetItemString(module_dict, "UnpicklingError", |
| 5605 | UnpicklingError) < 0) |
| 5606 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5607 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5608 | if (PyDict_SetItemString(module_dict, "UnpickleableError", |
| 5609 | UnpickleableError) < 0) |
| 5610 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5611 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5612 | if (PyDict_SetItemString(module_dict, "BadPickleGet", |
| 5613 | BadPickleGet) < 0) |
| 5614 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5615 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5616 | PycString_IMPORT; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 5617 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5618 | return 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5619 | } |
| 5620 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 5621 | #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */ |
| 5622 | #define PyMODINIT_FUNC void |
Guido van Rossum | f9ffb03 | 1999-02-04 14:54:04 +0000 | [diff] [blame] | 5623 | #endif |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 5624 | PyMODINIT_FUNC |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5625 | initcPickle(void) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5626 | { |
| 5627 | PyObject *m, *d, *di, *v, *k; |
| 5628 | int i; |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 5629 | char *rev = "1.71"; /* XXX when does this change? */ |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5630 | PyObject *format_version; |
| 5631 | PyObject *compatible_formats; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5632 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5633 | Picklertype.ob_type = &PyType_Type; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5634 | Unpicklertype.ob_type = &PyType_Type; |
| 5635 | PdataType.ob_type = &PyType_Type; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5636 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5637 | /* Initialize some pieces. We need to do this before module creation, |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 5638 | * so we're forced to use a temporary dictionary. :( |
| 5639 | */ |
| 5640 | di = PyDict_New(); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5641 | if (!di) return; |
| 5642 | if (init_stuff(di) < 0) return; |
Guido van Rossum | ebba420 | 2000-09-07 14:35:37 +0000 | [diff] [blame] | 5643 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5644 | /* Create the module and add the functions */ |
| 5645 | m = Py_InitModule4("cPickle", cPickle_methods, |
| 5646 | cPickle_module_documentation, |
| 5647 | (PyObject*)NULL,PYTHON_API_VERSION); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5648 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5649 | /* Add some symbolic constants to the module */ |
| 5650 | d = PyModule_GetDict(m); |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 5651 | v = PyString_FromString(rev); |
| 5652 | PyDict_SetItemString(d, "__version__", v); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5653 | Py_XDECREF(v); |
Barry Warsaw | 93d29b6 | 1997-01-14 17:45:08 +0000 | [diff] [blame] | 5654 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5655 | /* Copy data from di. Waaa. */ |
| 5656 | for (i=0; PyDict_Next(di, &i, &k, &v); ) { |
| 5657 | if (PyObject_SetItem(d, k, v) < 0) { |
| 5658 | Py_DECREF(di); |
| 5659 | return; |
| 5660 | } |
| 5661 | } |
| 5662 | Py_DECREF(di); |
Guido van Rossum | ebba420 | 2000-09-07 14:35:37 +0000 | [diff] [blame] | 5663 | |
Tim Peters | 8587b3c | 2003-02-13 15:44:41 +0000 | [diff] [blame] | 5664 | i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL); |
| 5665 | if (i < 0) |
| 5666 | return; |
| 5667 | |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 5668 | /* These are purely informational; no code uses them. */ |
| 5669 | /* File format version we write. */ |
| 5670 | format_version = PyString_FromString("2.0"); |
| 5671 | /* Format versions we can read. */ |
| 5672 | compatible_formats = Py_BuildValue("[sssss]", |
| 5673 | "1.0", /* Original protocol 0 */ |
| 5674 | "1.1", /* Protocol 0 + INST */ |
| 5675 | "1.2", /* Original protocol 1 */ |
| 5676 | "1.3", /* Protocol 1 + BINFLOAT */ |
Tim Peters | 0dd23aa | 2003-02-04 00:30:46 +0000 | [diff] [blame] | 5677 | "2.0"); /* Original protocol 2 */ |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5678 | PyDict_SetItemString(d, "format_version", format_version); |
| 5679 | PyDict_SetItemString(d, "compatible_formats", compatible_formats); |
| 5680 | Py_XDECREF(format_version); |
| 5681 | Py_XDECREF(compatible_formats); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5682 | } |