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__"); |
Jeremy Hylton | f00368f | 2003-06-16 20:19:49 +0000 | [diff] [blame] | 836 | if (module) |
| 837 | return module; |
| 838 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 839 | PyErr_Clear(); |
| 840 | else |
| 841 | return NULL; |
Guido van Rossum | 4518823 | 1997-09-28 05:38:51 +0000 | [diff] [blame] | 842 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 843 | if (!( modules_dict = PySys_GetObject("modules"))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 844 | return NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 845 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 846 | i = 0; |
| 847 | while ((j = PyDict_Next(modules_dict, &i, &name, &module))) { |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 848 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 849 | if (PyObject_Compare(name, __main___str)==0) continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 850 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 851 | global_name_attr = PyObject_GetAttr(module, global_name); |
| 852 | if (!global_name_attr) { |
Jeremy Hylton | f00368f | 2003-06-16 20:19:49 +0000 | [diff] [blame] | 853 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 854 | PyErr_Clear(); |
| 855 | else |
| 856 | return NULL; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 857 | continue; |
| 858 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 859 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 860 | if (global_name_attr != global) { |
| 861 | Py_DECREF(global_name_attr); |
| 862 | continue; |
| 863 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 864 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 865 | Py_DECREF(global_name_attr); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 866 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 867 | break; |
| 868 | } |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 869 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 870 | /* The following implements the rule in pickle.py added in 1.5 |
| 871 | that used __main__ if no module is found. I don't actually |
| 872 | like this rule. jlf |
| 873 | */ |
| 874 | if (!j) { |
| 875 | j=1; |
| 876 | name=__main___str; |
| 877 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 878 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 879 | Py_INCREF(name); |
| 880 | return name; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 884 | static int |
Jeremy Hylton | 499ab6a | 2001-10-15 21:37:58 +0000 | [diff] [blame] | 885 | fast_save_enter(Picklerobject *self, PyObject *obj) |
| 886 | { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 887 | /* if fast_container < 0, we're doing an error exit. */ |
| 888 | if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) { |
| 889 | PyObject *key = NULL; |
| 890 | if (self->fast_memo == NULL) { |
| 891 | self->fast_memo = PyDict_New(); |
| 892 | if (self->fast_memo == NULL) { |
| 893 | self->fast_container = -1; |
| 894 | return 0; |
| 895 | } |
| 896 | } |
| 897 | key = PyLong_FromVoidPtr(obj); |
| 898 | if (key == NULL) |
| 899 | return 0; |
| 900 | if (PyDict_GetItem(self->fast_memo, key)) { |
Tim Peters | 4e52ca8 | 2002-12-07 02:43:28 +0000 | [diff] [blame] | 901 | Py_DECREF(key); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 902 | PyErr_Format(PyExc_ValueError, |
Tim Peters | 92c8bb3 | 2003-02-13 23:00:26 +0000 | [diff] [blame] | 903 | "fast mode: can't pickle cyclic objects " |
| 904 | "including object type %s at %p", |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 905 | obj->ob_type->tp_name, obj); |
| 906 | self->fast_container = -1; |
| 907 | return 0; |
| 908 | } |
| 909 | if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) { |
Tim Peters | 4e52ca8 | 2002-12-07 02:43:28 +0000 | [diff] [blame] | 910 | Py_DECREF(key); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 911 | self->fast_container = -1; |
| 912 | return 0; |
| 913 | } |
Tim Peters | 4e52ca8 | 2002-12-07 02:43:28 +0000 | [diff] [blame] | 914 | Py_DECREF(key); |
Jeremy Hylton | 499ab6a | 2001-10-15 21:37:58 +0000 | [diff] [blame] | 915 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 916 | return 1; |
Jeremy Hylton | 499ab6a | 2001-10-15 21:37:58 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 919 | int |
Jeremy Hylton | 499ab6a | 2001-10-15 21:37:58 +0000 | [diff] [blame] | 920 | fast_save_leave(Picklerobject *self, PyObject *obj) |
| 921 | { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 922 | if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) { |
| 923 | PyObject *key = PyLong_FromVoidPtr(obj); |
| 924 | if (key == NULL) |
| 925 | return 0; |
| 926 | if (PyDict_DelItem(self->fast_memo, key) < 0) { |
Tim Peters | 4e52ca8 | 2002-12-07 02:43:28 +0000 | [diff] [blame] | 927 | Py_DECREF(key); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 928 | return 0; |
| 929 | } |
Tim Peters | 4e52ca8 | 2002-12-07 02:43:28 +0000 | [diff] [blame] | 930 | Py_DECREF(key); |
Jeremy Hylton | 499ab6a | 2001-10-15 21:37:58 +0000 | [diff] [blame] | 931 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 932 | return 1; |
Jeremy Hylton | 499ab6a | 2001-10-15 21:37:58 +0000 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 936 | save_none(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 937 | { |
| 938 | static char none = NONE; |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 939 | if (self->write_func(self, &none, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 940 | return -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 941 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 942 | return 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 945 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 946 | save_bool(Picklerobject *self, PyObject *args) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 947 | { |
Guido van Rossum | e276339 | 2002-04-05 19:30:08 +0000 | [diff] [blame] | 948 | static char *buf[2] = {FALSE, TRUE}; |
| 949 | static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1}; |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 950 | long l = PyInt_AS_LONG((PyIntObject *)args); |
| 951 | |
Tim Peters | 3c67d79 | 2003-02-02 17:59:11 +0000 | [diff] [blame] | 952 | if (self->proto >= 2) { |
| 953 | char opcode = l ? NEWTRUE : NEWFALSE; |
| 954 | if (self->write_func(self, &opcode, 1) < 0) |
| 955 | return -1; |
| 956 | } |
| 957 | else if (self->write_func(self, buf[l], len[l]) < 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 958 | return -1; |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 959 | return 0; |
| 960 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 961 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 962 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 963 | save_int(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 964 | { |
| 965 | char c_str[32]; |
| 966 | long l = PyInt_AS_LONG((PyIntObject *)args); |
| 967 | int len = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 968 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 969 | if (!self->bin |
Guido van Rossum | de8d6d7 | 1997-05-13 18:00:44 +0000 | [diff] [blame] | 970 | #if SIZEOF_LONG > 4 |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 971 | || l > 0x7fffffffL |
| 972 | || l < -0x80000000L |
Guido van Rossum | de8d6d7 | 1997-05-13 18:00:44 +0000 | [diff] [blame] | 973 | #endif |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 974 | ) { |
| 975 | /* Text-mode pickle, or long too big to fit in the 4-byte |
| 976 | * signed BININT format: store as a string. |
| 977 | */ |
| 978 | c_str[0] = INT; |
| 979 | PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l); |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 980 | 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] | 981 | return -1; |
| 982 | } |
| 983 | else { |
| 984 | /* Binary pickle and l fits in a signed 4-byte int. */ |
| 985 | c_str[1] = (int)( l & 0xff); |
| 986 | c_str[2] = (int)((l >> 8) & 0xff); |
| 987 | c_str[3] = (int)((l >> 16) & 0xff); |
| 988 | c_str[4] = (int)((l >> 24) & 0xff); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 989 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 990 | if ((c_str[4] == 0) && (c_str[3] == 0)) { |
| 991 | if (c_str[2] == 0) { |
| 992 | c_str[0] = BININT1; |
| 993 | len = 2; |
| 994 | } |
| 995 | else { |
| 996 | c_str[0] = BININT2; |
| 997 | len = 3; |
| 998 | } |
| 999 | } |
| 1000 | else { |
| 1001 | c_str[0] = BININT; |
| 1002 | len = 5; |
| 1003 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1004 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1005 | if (self->write_func(self, c_str, len) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1006 | return -1; |
| 1007 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1008 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1009 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | |
| 1013 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1014 | save_long(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1015 | { |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 1016 | int size; |
| 1017 | int res = -1; |
| 1018 | PyObject *repr = NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1019 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1020 | static char l = LONG; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1021 | |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 1022 | if (self->proto >= 2) { |
| 1023 | /* Linear-time pickling. */ |
| 1024 | size_t nbits; |
| 1025 | size_t nbytes; |
| 1026 | unsigned char *pdata; |
| 1027 | char c_str[5]; |
| 1028 | int i; |
| 1029 | int sign = _PyLong_Sign(args); |
| 1030 | |
| 1031 | if (sign == 0) { |
| 1032 | /* It's 0 -- an empty bytestring. */ |
| 1033 | c_str[0] = LONG1; |
| 1034 | c_str[1] = 0; |
| 1035 | i = self->write_func(self, c_str, 2); |
| 1036 | if (i < 0) goto finally; |
| 1037 | res = 0; |
| 1038 | goto finally; |
| 1039 | } |
| 1040 | nbits = _PyLong_NumBits(args); |
| 1041 | if (nbits == (size_t)-1 && PyErr_Occurred()) |
| 1042 | goto finally; |
| 1043 | /* How many bytes do we need? There are nbits >> 3 full |
| 1044 | * bytes of data, and nbits & 7 leftover bits. If there |
| 1045 | * are any leftover bits, then we clearly need another |
| 1046 | * byte. Wnat's not so obvious is that we *probably* |
| 1047 | * need another byte even if there aren't any leftovers: |
| 1048 | * the most-significant bit of the most-significant byte |
| 1049 | * acts like a sign bit, and it's usually got a sense |
| 1050 | * opposite of the one we need. The exception is longs |
| 1051 | * of the form -(2**(8*j-1)) for j > 0. Such a long is |
| 1052 | * its own 256's-complement, so has the right sign bit |
| 1053 | * even without the extra byte. That's a pain to check |
| 1054 | * for in advance, though, so we always grab an extra |
| 1055 | * byte at the start, and cut it back later if possible. |
| 1056 | */ |
| 1057 | nbytes = (nbits >> 3) + 1; |
| 1058 | if ((int)nbytes < 0 || (size_t)(int)nbytes != nbytes) { |
| 1059 | PyErr_SetString(PyExc_OverflowError, "long too large " |
| 1060 | "to pickle"); |
| 1061 | goto finally; |
| 1062 | } |
| 1063 | repr = PyString_FromStringAndSize(NULL, (int)nbytes); |
| 1064 | if (repr == NULL) goto finally; |
| 1065 | pdata = (unsigned char *)PyString_AS_STRING(repr); |
| 1066 | i = _PyLong_AsByteArray((PyLongObject *)args, |
| 1067 | pdata, nbytes, |
| 1068 | 1 /* little endian */, 1 /* signed */); |
| 1069 | if (i < 0) goto finally; |
| 1070 | /* If the long is negative, this may be a byte more than |
| 1071 | * needed. This is so iff the MSB is all redundant sign |
| 1072 | * bits. |
| 1073 | */ |
| 1074 | if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff && |
| 1075 | (pdata[nbytes - 2] & 0x80) != 0) |
| 1076 | --nbytes; |
| 1077 | |
| 1078 | if (nbytes < 256) { |
| 1079 | c_str[0] = LONG1; |
| 1080 | c_str[1] = (char)nbytes; |
| 1081 | size = 2; |
| 1082 | } |
| 1083 | else { |
| 1084 | c_str[0] = LONG4; |
| 1085 | size = (int)nbytes; |
| 1086 | for (i = 1; i < 5; i++) { |
| 1087 | c_str[i] = (char)(size & 0xff); |
| 1088 | size >>= 8; |
| 1089 | } |
| 1090 | size = 5; |
| 1091 | } |
| 1092 | i = self->write_func(self, c_str, size); |
| 1093 | if (i < 0) goto finally; |
| 1094 | i = self->write_func(self, (char *)pdata, (int)nbytes); |
| 1095 | if (i < 0) goto finally; |
| 1096 | res = 0; |
| 1097 | goto finally; |
| 1098 | } |
| 1099 | |
| 1100 | /* proto < 2: write the repr and newline. This is quadratic-time |
| 1101 | * (in the number of digits), in both directions. |
| 1102 | */ |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1103 | if (!( repr = PyObject_Repr(args))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1104 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1105 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1106 | if ((size = PyString_Size(repr)) < 0) |
| 1107 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1108 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1109 | if (self->write_func(self, &l, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1110 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1111 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1112 | if (self->write_func(self, |
| 1113 | PyString_AS_STRING((PyStringObject *)repr), |
| 1114 | size) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1115 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1116 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1117 | if (self->write_func(self, "\n", 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1118 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1119 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1120 | res = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1121 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1122 | finally: |
| 1123 | Py_XDECREF(repr); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1124 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | |
| 1128 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1129 | save_float(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1130 | { |
| 1131 | double x = PyFloat_AS_DOUBLE((PyFloatObject *)args); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1132 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1133 | if (self->bin) { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1134 | char str[9]; |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 1135 | str[0] = BINFLOAT; |
| 1136 | if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1137 | return -1; |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1138 | if (self->write_func(self, str, 9) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1139 | return -1; |
| 1140 | } |
| 1141 | else { |
| 1142 | char c_str[250]; |
| 1143 | c_str[0] = FLOAT; |
| 1144 | 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] | 1145 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1146 | 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] | 1147 | return -1; |
| 1148 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1149 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1150 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | |
| 1154 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1155 | save_string(Picklerobject *self, PyObject *args, int doput) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1156 | { |
| 1157 | int size, len; |
| 1158 | PyObject *repr=0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1159 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1160 | if ((size = PyString_Size(args)) < 0) |
| 1161 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1162 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1163 | if (!self->bin) { |
| 1164 | char *repr_str; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1165 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1166 | static char string = STRING; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1167 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1168 | if (!( repr = PyObject_Repr(args))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1169 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1170 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1171 | if ((len = PyString_Size(repr)) < 0) |
| 1172 | goto err; |
| 1173 | repr_str = PyString_AS_STRING((PyStringObject *)repr); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1174 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1175 | if (self->write_func(self, &string, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1176 | goto err; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1177 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1178 | if (self->write_func(self, repr_str, len) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1179 | goto err; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1180 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1181 | if (self->write_func(self, "\n", 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1182 | goto err; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1183 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1184 | Py_XDECREF(repr); |
| 1185 | } |
| 1186 | else { |
| 1187 | int i; |
| 1188 | char c_str[5]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1189 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1190 | if ((size = PyString_Size(args)) < 0) |
| 1191 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1192 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1193 | if (size < 256) { |
| 1194 | c_str[0] = SHORT_BINSTRING; |
| 1195 | c_str[1] = size; |
| 1196 | len = 2; |
| 1197 | } |
| 1198 | else { |
| 1199 | c_str[0] = BINSTRING; |
| 1200 | for (i = 1; i < 5; i++) |
| 1201 | c_str[i] = (int)(size >> ((i - 1) * 8)); |
| 1202 | len = 5; |
| 1203 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1204 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1205 | if (self->write_func(self, c_str, len) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1206 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1207 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1208 | if (size > 128 && Pdata_Check(self->file)) { |
| 1209 | if (write_other(self, NULL, 0) < 0) return -1; |
| 1210 | PDATA_APPEND(self->file, args, -1); |
| 1211 | } |
| 1212 | else { |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1213 | if (self->write_func(self, |
| 1214 | PyString_AS_STRING( |
| 1215 | (PyStringObject *)args), |
| 1216 | size) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1217 | return -1; |
| 1218 | } |
| 1219 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1220 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1221 | if (doput) |
| 1222 | if (put(self, args) < 0) |
| 1223 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1224 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1225 | return 0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 1226 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1227 | err: |
| 1228 | Py_XDECREF(repr); |
| 1229 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1233 | #ifdef Py_USING_UNICODE |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1234 | /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates |
| 1235 | backslash and newline characters to \uXXXX escapes. */ |
| 1236 | static PyObject * |
| 1237 | modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, int size) |
| 1238 | { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1239 | PyObject *repr; |
| 1240 | char *p; |
| 1241 | char *q; |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1242 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1243 | static const char *hexdigit = "0123456789ABCDEF"; |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1244 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1245 | repr = PyString_FromStringAndSize(NULL, 6 * size); |
| 1246 | if (repr == NULL) |
| 1247 | return NULL; |
| 1248 | if (size == 0) |
| 1249 | return repr; |
| 1250 | |
| 1251 | p = q = PyString_AS_STRING(repr); |
| 1252 | while (size-- > 0) { |
| 1253 | Py_UNICODE ch = *s++; |
| 1254 | /* Map 16-bit characters to '\uxxxx' */ |
| 1255 | if (ch >= 256 || ch == '\\' || ch == '\n') { |
| 1256 | *p++ = '\\'; |
| 1257 | *p++ = 'u'; |
| 1258 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 1259 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 1260 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 1261 | *p++ = hexdigit[ch & 15]; |
| 1262 | } |
| 1263 | /* Copy everything else as-is */ |
| 1264 | else |
| 1265 | *p++ = (char) ch; |
| 1266 | } |
| 1267 | *p = '\0'; |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 1268 | _PyString_Resize(&repr, p - q); |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1269 | return repr; |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1273 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1274 | save_unicode(Picklerobject *self, PyObject *args, int doput) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1275 | { |
| 1276 | int size, len; |
| 1277 | PyObject *repr=0; |
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 | if (!PyUnicode_Check(args)) |
| 1280 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1281 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1282 | if (!self->bin) { |
| 1283 | char *repr_str; |
| 1284 | static char string = UNICODE; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1285 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1286 | repr = modified_EncodeRawUnicodeEscape( |
| 1287 | PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args)); |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1288 | if (!repr) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1289 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1290 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1291 | if ((len = PyString_Size(repr)) < 0) |
| 1292 | goto err; |
| 1293 | repr_str = PyString_AS_STRING((PyStringObject *)repr); |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1294 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1295 | if (self->write_func(self, &string, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1296 | goto err; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1297 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1298 | if (self->write_func(self, repr_str, len) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1299 | goto err; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1300 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1301 | if (self->write_func(self, "\n", 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1302 | goto err; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1303 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1304 | Py_XDECREF(repr); |
| 1305 | } |
| 1306 | else { |
| 1307 | int i; |
| 1308 | char c_str[5]; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1309 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1310 | if (!( repr = PyUnicode_AsUTF8String(args))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1311 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1312 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1313 | if ((size = PyString_Size(repr)) < 0) |
| 1314 | goto err; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1315 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1316 | c_str[0] = BINUNICODE; |
| 1317 | for (i = 1; i < 5; i++) |
| 1318 | c_str[i] = (int)(size >> ((i - 1) * 8)); |
| 1319 | len = 5; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1320 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1321 | if (self->write_func(self, c_str, len) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1322 | goto err; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1323 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1324 | if (size > 128 && Pdata_Check(self->file)) { |
| 1325 | if (write_other(self, NULL, 0) < 0) |
| 1326 | goto err; |
| 1327 | PDATA_APPEND(self->file, repr, -1); |
| 1328 | } |
| 1329 | else { |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1330 | if (self->write_func(self, PyString_AS_STRING(repr), |
| 1331 | size) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1332 | goto err; |
| 1333 | } |
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 | Py_DECREF(repr); |
| 1336 | } |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1337 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1338 | if (doput) |
| 1339 | if (put(self, args) < 0) |
| 1340 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1341 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1342 | return 0; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1343 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1344 | err: |
| 1345 | Py_XDECREF(repr); |
| 1346 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1347 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1348 | #endif |
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 | /* A helper for save_tuple. Push the len elements in tuple t on the stack. */ |
| 1351 | static int |
Tim Peters | 6792014 | 2003-02-05 03:46:17 +0000 | [diff] [blame] | 1352 | store_tuple_elements(Picklerobject *self, PyObject *t, int len) |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1353 | { |
| 1354 | int i; |
| 1355 | int res = -1; /* guilty until proved innocent */ |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1356 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1357 | assert(PyTuple_Size(t) == len); |
| 1358 | |
| 1359 | for (i = 0; i < len; i++) { |
| 1360 | PyObject *element = PyTuple_GET_ITEM(t, i); |
| 1361 | |
| 1362 | if (element == NULL) |
| 1363 | goto finally; |
| 1364 | if (save(self, element, 0) < 0) |
| 1365 | goto finally; |
| 1366 | } |
| 1367 | res = 0; |
| 1368 | |
| 1369 | finally: |
| 1370 | return res; |
| 1371 | } |
| 1372 | |
| 1373 | /* Tuples are ubiquitous in the pickle protocols, so many techniques are |
| 1374 | * used across protocols to minimize the space needed to pickle them. |
Tim Peters | 6792014 | 2003-02-05 03:46:17 +0000 | [diff] [blame] | 1375 | * Tuples are also the only builtin immutable type that can be recursive |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1376 | * (a tuple can be reached from itself), and that requires some subtle |
| 1377 | * magic so that it works in all cases. IOW, this is a long routine. |
| 1378 | */ |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1379 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1380 | save_tuple(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1381 | { |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1382 | PyObject *py_tuple_id = NULL; |
| 1383 | int len, i; |
| 1384 | int res = -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1385 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1386 | static char tuple = TUPLE; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1387 | static char pop = POP; |
| 1388 | static char pop_mark = POP_MARK; |
| 1389 | static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3}; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1390 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1391 | if ((len = PyTuple_Size(args)) < 0) |
| 1392 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1393 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1394 | if (len == 0) { |
| 1395 | char c_str[2]; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 1396 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1397 | if (self->proto) { |
| 1398 | c_str[0] = EMPTY_TUPLE; |
| 1399 | len = 1; |
| 1400 | } |
| 1401 | else { |
| 1402 | c_str[0] = MARK; |
| 1403 | c_str[1] = TUPLE; |
| 1404 | len = 2; |
| 1405 | } |
| 1406 | if (self->write_func(self, c_str, len) >= 0) |
| 1407 | res = 0; |
| 1408 | /* Don't memoize an empty tuple. */ |
| 1409 | goto finally; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1410 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1411 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1412 | /* A non-empty tuple. */ |
| 1413 | |
| 1414 | /* id(tuple) isn't in the memo now. If it shows up there after |
| 1415 | * saving the tuple elements, the tuple must be recursive, in |
| 1416 | * which case we'll pop everything we put on the stack, and fetch |
| 1417 | * its value from the memo. |
| 1418 | */ |
| 1419 | py_tuple_id = PyLong_FromVoidPtr(args); |
| 1420 | if (py_tuple_id == NULL) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1421 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1422 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1423 | if (len <= 3 && self->proto >= 2) { |
| 1424 | /* Use TUPLE{1,2,3} opcodes. */ |
Tim Peters | 6792014 | 2003-02-05 03:46:17 +0000 | [diff] [blame] | 1425 | if (store_tuple_elements(self, args, len) < 0) |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1426 | goto finally; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1427 | if (PyDict_GetItem(self->memo, py_tuple_id)) { |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1428 | /* pop the len elements */ |
| 1429 | for (i = 0; i < len; ++i) |
| 1430 | if (self->write_func(self, &pop, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1431 | goto finally; |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1432 | /* fetch from memo */ |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1433 | if (get(self, py_tuple_id) < 0) |
| 1434 | goto finally; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1435 | res = 0; |
| 1436 | goto finally; |
| 1437 | } |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1438 | /* Not recursive. */ |
| 1439 | if (self->write_func(self, len2opcode + len, 1) < 0) |
| 1440 | goto finally; |
| 1441 | goto memoize; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1442 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1443 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1444 | /* proto < 2 and len > 0, or proto >= 2 and len > 3. |
| 1445 | * Generate MARK elt1 elt2 ... TUPLE |
| 1446 | */ |
| 1447 | if (self->write_func(self, &MARKv, 1) < 0) |
| 1448 | goto finally; |
| 1449 | |
Tim Peters | 6792014 | 2003-02-05 03:46:17 +0000 | [diff] [blame] | 1450 | if (store_tuple_elements(self, args, len) < 0) |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1451 | goto finally; |
| 1452 | |
| 1453 | if (PyDict_GetItem(self->memo, py_tuple_id)) { |
| 1454 | /* pop the stack stuff we pushed */ |
| 1455 | if (self->bin) { |
| 1456 | if (self->write_func(self, &pop_mark, 1) < 0) |
| 1457 | goto finally; |
| 1458 | } |
| 1459 | else { |
| 1460 | /* Note that we pop one more than len, to remove |
| 1461 | * the MARK too. |
| 1462 | */ |
| 1463 | for (i = 0; i <= len; i++) |
| 1464 | if (self->write_func(self, &pop, 1) < 0) |
| 1465 | goto finally; |
| 1466 | } |
| 1467 | /* fetch from memo */ |
| 1468 | if (get(self, py_tuple_id) >= 0) |
| 1469 | res = 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1470 | goto finally; |
| 1471 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1472 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1473 | /* Not recursive. */ |
| 1474 | if (self->write_func(self, &tuple, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1475 | goto finally; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 1476 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1477 | memoize: |
| 1478 | if (put(self, args) >= 0) |
| 1479 | res = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1480 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1481 | finally: |
| 1482 | Py_XDECREF(py_tuple_id); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1483 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1486 | /* iter is an iterator giving items, and we batch up chunks of |
| 1487 | * MARK item item ... item APPENDS |
| 1488 | * opcode sequences. Calling code should have arranged to first create an |
| 1489 | * empty list, or list-like object, for the APPENDS to operate on. |
| 1490 | * Returns 0 on success, <0 on error. |
| 1491 | */ |
| 1492 | static int |
| 1493 | batch_list(Picklerobject *self, PyObject *iter) |
| 1494 | { |
| 1495 | PyObject *obj; |
| 1496 | PyObject *slice[BATCHSIZE]; |
| 1497 | int i, n; |
| 1498 | |
| 1499 | static char append = APPEND; |
| 1500 | static char appends = APPENDS; |
| 1501 | |
| 1502 | assert(iter != NULL); |
| 1503 | |
| 1504 | if (self->proto == 0) { |
| 1505 | /* APPENDS isn't available; do one at a time. */ |
| 1506 | for (;;) { |
| 1507 | obj = PyIter_Next(iter); |
| 1508 | if (obj == NULL) { |
| 1509 | if (PyErr_Occurred()) |
| 1510 | return -1; |
| 1511 | break; |
| 1512 | } |
| 1513 | i = save(self, obj, 0); |
| 1514 | Py_DECREF(obj); |
| 1515 | if (i < 0) |
| 1516 | return -1; |
| 1517 | if (self->write_func(self, &append, 1) < 0) |
| 1518 | return -1; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1519 | } |
| 1520 | return 0; |
| 1521 | } |
| 1522 | |
| 1523 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 1524 | do { |
| 1525 | /* Get next group of (no more than) BATCHSIZE elements. */ |
| 1526 | for (n = 0; n < BATCHSIZE; ++n) { |
| 1527 | obj = PyIter_Next(iter); |
| 1528 | if (obj == NULL) { |
| 1529 | if (PyErr_Occurred()) |
| 1530 | goto BatchFailed; |
| 1531 | break; |
| 1532 | } |
| 1533 | slice[n] = obj; |
| 1534 | } |
| 1535 | |
| 1536 | if (n > 1) { |
| 1537 | /* Pump out MARK, slice[0:n], APPENDS. */ |
| 1538 | if (self->write_func(self, &MARKv, 1) < 0) |
| 1539 | goto BatchFailed; |
| 1540 | for (i = 0; i < n; ++i) { |
| 1541 | if (save(self, slice[i], 0) < 0) |
| 1542 | goto BatchFailed; |
| 1543 | } |
| 1544 | if (self->write_func(self, &appends, 1) < 0) |
| 1545 | goto BatchFailed; |
| 1546 | } |
| 1547 | else if (n == 1) { |
| 1548 | if (save(self, slice[0], 0) < 0) |
| 1549 | goto BatchFailed; |
| 1550 | if (self->write_func(self, &append, 1) < 0) |
| 1551 | goto BatchFailed; |
| 1552 | } |
| 1553 | |
| 1554 | for (i = 0; i < n; ++i) { |
| 1555 | Py_DECREF(slice[i]); |
| 1556 | } |
Tim Peters | 90975f1 | 2003-02-12 05:28:58 +0000 | [diff] [blame] | 1557 | } while (n == BATCHSIZE); |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1558 | return 0; |
| 1559 | |
| 1560 | BatchFailed: |
| 1561 | while (--n >= 0) { |
| 1562 | Py_DECREF(slice[n]); |
| 1563 | } |
| 1564 | return -1; |
| 1565 | } |
| 1566 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1567 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1568 | save_list(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1569 | { |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1570 | int res = -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1571 | char s[3]; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1572 | int len; |
| 1573 | PyObject *iter; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1574 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1575 | if (self->fast && !fast_save_enter(self, args)) |
| 1576 | goto finally; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 1577 | |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1578 | /* Create an empty list. */ |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1579 | if (self->bin) { |
| 1580 | s[0] = EMPTY_LIST; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1581 | len = 1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1582 | } |
| 1583 | else { |
| 1584 | s[0] = MARK; |
| 1585 | s[1] = LIST; |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1586 | len = 2; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1587 | } |
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 | if (self->write_func(self, s, len) < 0) |
| 1590 | goto finally; |
| 1591 | |
| 1592 | /* Get list length, and bow out early if empty. */ |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1593 | if ((len = PyList_Size(args)) < 0) |
| 1594 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1595 | |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1596 | /* Memoize. */ |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1597 | if (len == 0) { |
Tim Peters | 90975f1 | 2003-02-12 05:28:58 +0000 | [diff] [blame] | 1598 | if (put(self, args) >= 0) |
| 1599 | res = 0; |
| 1600 | goto finally; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1601 | } |
Tim Peters | 90975f1 | 2003-02-12 05:28:58 +0000 | [diff] [blame] | 1602 | if (put2(self, args) < 0) |
| 1603 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1604 | |
Tim Peters | 1092d64 | 2003-02-11 21:06:20 +0000 | [diff] [blame] | 1605 | /* Materialize the list elements. */ |
| 1606 | iter = PyObject_GetIter(args); |
| 1607 | if (iter == NULL) |
| 1608 | goto finally; |
| 1609 | res = batch_list(self, iter); |
| 1610 | Py_DECREF(iter); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1611 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1612 | finally: |
| 1613 | if (self->fast && !fast_save_leave(self, args)) |
| 1614 | res = -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1615 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1616 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
| 1619 | |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1620 | /* iter is an iterator giving (key, value) pairs, and we batch up chunks of |
| 1621 | * MARK key value ... key value SETITEMS |
| 1622 | * opcode sequences. Calling code should have arranged to first create an |
| 1623 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 1624 | * Returns 0 on success, <0 on error. |
| 1625 | * |
| 1626 | * This is very much like batch_list(). The difference between saving |
| 1627 | * elements directly, and picking apart two-tuples, is so long-winded at |
| 1628 | * the C level, though, that attempts to combine these routines were too |
| 1629 | * ugly to bear. |
| 1630 | */ |
| 1631 | static int |
| 1632 | batch_dict(Picklerobject *self, PyObject *iter) |
| 1633 | { |
| 1634 | PyObject *p; |
| 1635 | PyObject *slice[BATCHSIZE]; |
| 1636 | int i, n; |
| 1637 | |
| 1638 | static char setitem = SETITEM; |
| 1639 | static char setitems = SETITEMS; |
| 1640 | |
| 1641 | assert(iter != NULL); |
| 1642 | |
| 1643 | if (self->proto == 0) { |
| 1644 | /* SETITEMS isn't available; do one at a time. */ |
| 1645 | for (;;) { |
| 1646 | p = PyIter_Next(iter); |
| 1647 | if (p == NULL) { |
| 1648 | if (PyErr_Occurred()) |
| 1649 | return -1; |
| 1650 | break; |
| 1651 | } |
| 1652 | if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) { |
| 1653 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 1654 | "iterator must return 2-tuples"); |
| 1655 | return -1; |
| 1656 | } |
| 1657 | i = save(self, PyTuple_GET_ITEM(p, 0), 0); |
| 1658 | if (i >= 0) |
| 1659 | i = save(self, PyTuple_GET_ITEM(p, 1), 0); |
| 1660 | Py_DECREF(p); |
| 1661 | if (i < 0) |
| 1662 | return -1; |
| 1663 | if (self->write_func(self, &setitem, 1) < 0) |
| 1664 | return -1; |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1665 | } |
| 1666 | return 0; |
| 1667 | } |
| 1668 | |
| 1669 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 1670 | do { |
| 1671 | /* Get next group of (no more than) BATCHSIZE elements. */ |
| 1672 | for (n = 0; n < BATCHSIZE; ++n) { |
| 1673 | p = PyIter_Next(iter); |
| 1674 | if (p == NULL) { |
| 1675 | if (PyErr_Occurred()) |
| 1676 | goto BatchFailed; |
| 1677 | break; |
| 1678 | } |
| 1679 | if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) { |
| 1680 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 1681 | "iterator must return 2-tuples"); |
| 1682 | goto BatchFailed; |
| 1683 | } |
| 1684 | slice[n] = p; |
| 1685 | } |
| 1686 | |
| 1687 | if (n > 1) { |
| 1688 | /* Pump out MARK, slice[0:n], SETITEMS. */ |
| 1689 | if (self->write_func(self, &MARKv, 1) < 0) |
| 1690 | goto BatchFailed; |
| 1691 | for (i = 0; i < n; ++i) { |
| 1692 | p = slice[i]; |
| 1693 | if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0) |
| 1694 | goto BatchFailed; |
| 1695 | if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0) |
| 1696 | goto BatchFailed; |
| 1697 | } |
| 1698 | if (self->write_func(self, &setitems, 1) < 0) |
| 1699 | goto BatchFailed; |
| 1700 | } |
| 1701 | else if (n == 1) { |
| 1702 | p = slice[0]; |
| 1703 | if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0) |
| 1704 | goto BatchFailed; |
| 1705 | if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0) |
| 1706 | goto BatchFailed; |
| 1707 | if (self->write_func(self, &setitem, 1) < 0) |
| 1708 | goto BatchFailed; |
| 1709 | } |
| 1710 | |
| 1711 | for (i = 0; i < n; ++i) { |
| 1712 | Py_DECREF(slice[i]); |
| 1713 | } |
Tim Peters | 90975f1 | 2003-02-12 05:28:58 +0000 | [diff] [blame] | 1714 | } while (n == BATCHSIZE); |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1715 | return 0; |
| 1716 | |
| 1717 | BatchFailed: |
| 1718 | while (--n >= 0) { |
| 1719 | Py_DECREF(slice[n]); |
| 1720 | } |
| 1721 | return -1; |
| 1722 | } |
| 1723 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1724 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1725 | save_dict(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1726 | { |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1727 | int res = -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1728 | char s[3]; |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1729 | int len; |
| 1730 | PyObject *iter; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1731 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1732 | if (self->fast && !fast_save_enter(self, args)) |
| 1733 | goto finally; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 1734 | |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1735 | /* Create an empty dict. */ |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1736 | if (self->bin) { |
| 1737 | s[0] = EMPTY_DICT; |
| 1738 | len = 1; |
| 1739 | } |
| 1740 | else { |
| 1741 | s[0] = MARK; |
| 1742 | s[1] = DICT; |
| 1743 | len = 2; |
| 1744 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1745 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1746 | if (self->write_func(self, s, len) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1747 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1748 | |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1749 | /* Get dict size, and bow out early if empty. */ |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1750 | if ((len = PyDict_Size(args)) < 0) |
| 1751 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1752 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1753 | if (len == 0) { |
Tim Peters | 90975f1 | 2003-02-12 05:28:58 +0000 | [diff] [blame] | 1754 | if (put(self, args) >= 0) |
| 1755 | res = 0; |
| 1756 | goto finally; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1757 | } |
Tim Peters | 90975f1 | 2003-02-12 05:28:58 +0000 | [diff] [blame] | 1758 | if (put2(self, args) < 0) |
| 1759 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1760 | |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1761 | /* Materialize the dict items. */ |
| 1762 | iter = PyObject_CallMethod(args, "iteritems", "()"); |
| 1763 | if (iter == NULL) |
| 1764 | goto finally; |
| 1765 | res = batch_dict(self, iter); |
| 1766 | Py_DECREF(iter); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1767 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1768 | finally: |
| 1769 | if (self->fast && !fast_save_leave(self, args)) |
| 1770 | res = -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1771 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1772 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1773 | } |
| 1774 | |
| 1775 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 1776 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1777 | save_inst(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1778 | { |
| 1779 | PyObject *class = 0, *module = 0, *name = 0, *state = 0, |
| 1780 | *getinitargs_func = 0, *getstate_func = 0, *class_args = 0; |
| 1781 | char *module_str, *name_str; |
| 1782 | int module_size, name_size, res = -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1783 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1784 | static char inst = INST, obj = OBJ, build = BUILD; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1785 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1786 | if (self->fast && !fast_save_enter(self, args)) |
| 1787 | goto finally; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 1788 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1789 | if (self->write_func(self, &MARKv, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1790 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1791 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1792 | if (!( class = PyObject_GetAttr(args, __class___str))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1793 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1794 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1795 | if (self->bin) { |
| 1796 | if (save(self, class, 0) < 0) |
| 1797 | goto finally; |
| 1798 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1799 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1800 | if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) { |
| 1801 | PyObject *element = 0; |
| 1802 | int i, len; |
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 | if (!( class_args = |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1805 | PyObject_Call(getinitargs_func, empty_tuple, NULL))) |
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 ((len = PyObject_Size(class_args)) < 0) |
| 1809 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1810 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1811 | for (i = 0; i < len; i++) { |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1812 | if (!( element = PySequence_GetItem(class_args, i))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1813 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1814 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1815 | if (save(self, element, 0) < 0) { |
| 1816 | Py_DECREF(element); |
| 1817 | goto finally; |
| 1818 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1819 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1820 | Py_DECREF(element); |
| 1821 | } |
| 1822 | } |
| 1823 | else { |
Jeremy Hylton | f00368f | 2003-06-16 20:19:49 +0000 | [diff] [blame] | 1824 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1825 | PyErr_Clear(); |
| 1826 | else |
| 1827 | goto finally; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1828 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1829 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1830 | if (!self->bin) { |
| 1831 | if (!( name = ((PyClassObject *)class)->cl_name )) { |
| 1832 | PyErr_SetString(PicklingError, "class has no name"); |
| 1833 | goto finally; |
| 1834 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1835 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1836 | if (!( module = whichmodule(class, name))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1837 | goto finally; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 1838 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 1839 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1840 | if ((module_size = PyString_Size(module)) < 0 || |
| 1841 | (name_size = PyString_Size(name)) < 0) |
| 1842 | goto finally; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 1843 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1844 | module_str = PyString_AS_STRING((PyStringObject *)module); |
| 1845 | name_str = PyString_AS_STRING((PyStringObject *)name); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1846 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1847 | if (self->write_func(self, &inst, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1848 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1849 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1850 | if (self->write_func(self, module_str, module_size) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1851 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1852 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1853 | if (self->write_func(self, "\n", 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1854 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1855 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1856 | if (self->write_func(self, name_str, name_size) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1857 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1858 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1859 | if (self->write_func(self, "\n", 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1860 | goto finally; |
| 1861 | } |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1862 | else if (self->write_func(self, &obj, 1) < 0) { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1863 | goto finally; |
| 1864 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1865 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1866 | if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) { |
| 1867 | state = PyObject_Call(getstate_func, empty_tuple, NULL); |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1868 | if (!state) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1869 | goto finally; |
| 1870 | } |
| 1871 | else { |
Jeremy Hylton | f00368f | 2003-06-16 20:19:49 +0000 | [diff] [blame] | 1872 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1873 | PyErr_Clear(); |
| 1874 | else |
| 1875 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1876 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1877 | if (!( state = PyObject_GetAttr(args, __dict___str))) { |
Jeremy Hylton | f00368f | 2003-06-16 20:19:49 +0000 | [diff] [blame] | 1878 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1879 | PyErr_Clear(); |
| 1880 | else |
| 1881 | goto finally; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1882 | res = 0; |
| 1883 | goto finally; |
| 1884 | } |
| 1885 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1886 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1887 | if (!PyDict_Check(state)) { |
| 1888 | if (put2(self, args) < 0) |
| 1889 | goto finally; |
| 1890 | } |
| 1891 | else { |
| 1892 | if (put(self, args) < 0) |
| 1893 | goto finally; |
| 1894 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 1895 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1896 | if (save(self, state, 0) < 0) |
| 1897 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1898 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 1899 | if (self->write_func(self, &build, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1900 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1901 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1902 | res = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1903 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1904 | finally: |
| 1905 | if (self->fast && !fast_save_leave(self, args)) |
| 1906 | res = -1; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 1907 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1908 | Py_XDECREF(module); |
| 1909 | Py_XDECREF(class); |
| 1910 | Py_XDECREF(state); |
| 1911 | Py_XDECREF(getinitargs_func); |
| 1912 | Py_XDECREF(getstate_func); |
| 1913 | Py_XDECREF(class_args); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1914 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1915 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1916 | } |
| 1917 | |
| 1918 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1919 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1920 | save_global(Picklerobject *self, PyObject *args, PyObject *name) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1921 | { |
Jeremy Hylton | 9ee91f1 | 2002-07-11 22:02:33 +0000 | [diff] [blame] | 1922 | PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1923 | char *name_str, *module_str; |
| 1924 | int module_size, name_size, res = -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1925 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1926 | static char global = GLOBAL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1927 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1928 | if (name) { |
| 1929 | global_name = name; |
| 1930 | Py_INCREF(global_name); |
| 1931 | } |
| 1932 | else { |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1933 | if (!( global_name = PyObject_GetAttr(args, __name___str))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1934 | goto finally; |
| 1935 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1936 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 1937 | if (!( module = whichmodule(args, global_name))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1938 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1939 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1940 | if ((module_size = PyString_Size(module)) < 0 || |
| 1941 | (name_size = PyString_Size(global_name)) < 0) |
| 1942 | goto finally; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 1943 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1944 | module_str = PyString_AS_STRING((PyStringObject *)module); |
| 1945 | name_str = PyString_AS_STRING((PyStringObject *)global_name); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1946 | |
Guido van Rossum | 75bfd05 | 2002-12-24 18:10:07 +0000 | [diff] [blame] | 1947 | /* XXX This can be doing a relative import. Clearly it shouldn't, |
| 1948 | but I don't know how to stop it. :-( */ |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1949 | mod = PyImport_ImportModule(module_str); |
| 1950 | if (mod == NULL) { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1951 | cPickle_ErrFormat(PicklingError, |
Tim Peters | b9ce7cd | 2003-02-18 20:50:45 +0000 | [diff] [blame] | 1952 | "Can't pickle %s: import of module %s " |
| 1953 | "failed", |
| 1954 | "OS", args, module); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1955 | goto finally; |
| 1956 | } |
Jeremy Hylton | 0e1f7a8 | 2002-07-11 22:01:40 +0000 | [diff] [blame] | 1957 | klass = PyObject_GetAttrString(mod, name_str); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1958 | if (klass == NULL) { |
| 1959 | cPickle_ErrFormat(PicklingError, |
Tim Peters | b9ce7cd | 2003-02-18 20:50:45 +0000 | [diff] [blame] | 1960 | "Can't pickle %s: attribute lookup %s.%s " |
| 1961 | "failed", |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1962 | "OSS", args, module, global_name); |
| 1963 | goto finally; |
| 1964 | } |
| 1965 | if (klass != args) { |
Jeremy Hylton | 0e1f7a8 | 2002-07-11 22:01:40 +0000 | [diff] [blame] | 1966 | Py_DECREF(klass); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1967 | cPickle_ErrFormat(PicklingError, |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 1968 | "Can't pickle %s: it's not the same object " |
| 1969 | "as %s.%s", |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1970 | "OSS", args, module, global_name); |
| 1971 | goto finally; |
| 1972 | } |
Jeremy Hylton | 0e1f7a8 | 2002-07-11 22:01:40 +0000 | [diff] [blame] | 1973 | Py_DECREF(klass); |
Guido van Rossum | a92d16a | 2001-08-18 21:22:07 +0000 | [diff] [blame] | 1974 | |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 1975 | if (self->proto >= 2) { |
| 1976 | /* See whether this is in the extension registry, and if |
| 1977 | * so generate an EXT opcode. |
| 1978 | */ |
| 1979 | PyObject *py_code; /* extension code as Python object */ |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 1980 | long code; /* extension code as C value */ |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 1981 | char c_str[5]; |
| 1982 | int n; |
| 1983 | |
| 1984 | PyTuple_SET_ITEM(two_tuple, 0, module); |
| 1985 | PyTuple_SET_ITEM(two_tuple, 1, global_name); |
| 1986 | py_code = PyDict_GetItem(extension_registry, two_tuple); |
| 1987 | if (py_code == NULL) |
| 1988 | goto gen_global; /* not registered */ |
| 1989 | |
| 1990 | /* Verify py_code has the right type and value. */ |
| 1991 | if (!PyInt_Check(py_code)) { |
| 1992 | cPickle_ErrFormat(PicklingError, "Can't pickle %s: " |
Tim Peters | 6288e23 | 2003-02-05 03:53:10 +0000 | [diff] [blame] | 1993 | "extension code %s isn't an integer", |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 1994 | "OO", args, py_code); |
| 1995 | goto finally; |
| 1996 | } |
| 1997 | code = PyInt_AS_LONG(py_code); |
| 1998 | if (code <= 0 || code > 0x7fffffffL) { |
| 1999 | cPickle_ErrFormat(PicklingError, "Can't pickle %s: " |
| 2000 | "extension code %ld is out of range", |
| 2001 | "Ol", args, code); |
| 2002 | goto finally; |
| 2003 | } |
| 2004 | |
| 2005 | /* Generate an EXT opcode. */ |
| 2006 | if (code <= 0xff) { |
| 2007 | c_str[0] = EXT1; |
| 2008 | c_str[1] = (char)code; |
| 2009 | n = 2; |
| 2010 | } |
| 2011 | else if (code <= 0xffff) { |
| 2012 | c_str[0] = EXT2; |
| 2013 | c_str[1] = (char)(code & 0xff); |
| 2014 | c_str[2] = (char)((code >> 8) & 0xff); |
| 2015 | n = 3; |
| 2016 | } |
| 2017 | else { |
| 2018 | c_str[0] = EXT4; |
| 2019 | c_str[1] = (char)(code & 0xff); |
| 2020 | c_str[2] = (char)((code >> 8) & 0xff); |
| 2021 | c_str[3] = (char)((code >> 16) & 0xff); |
| 2022 | c_str[4] = (char)((code >> 24) & 0xff); |
| 2023 | n = 5; |
| 2024 | } |
| 2025 | |
| 2026 | if (self->write_func(self, c_str, n) >= 0) |
| 2027 | res = 0; |
| 2028 | goto finally; /* and don't memoize */ |
| 2029 | } |
| 2030 | |
| 2031 | gen_global: |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 2032 | if (self->write_func(self, &global, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2033 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2034 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 2035 | if (self->write_func(self, module_str, module_size) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2036 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2037 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 2038 | if (self->write_func(self, "\n", 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2039 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2040 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 2041 | if (self->write_func(self, name_str, name_size) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2042 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2043 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 2044 | if (self->write_func(self, "\n", 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2045 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2046 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2047 | if (put(self, args) < 0) |
| 2048 | goto finally; |
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 | res = 0; |
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 | finally: |
| 2053 | Py_XDECREF(module); |
| 2054 | Py_XDECREF(global_name); |
| 2055 | Py_XDECREF(mod); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2056 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2057 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2058 | } |
| 2059 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2060 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2061 | save_pers(Picklerobject *self, PyObject *args, PyObject *f) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2062 | { |
| 2063 | PyObject *pid = 0; |
| 2064 | int size, res = -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2065 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2066 | static char persid = PERSID, binpersid = BINPERSID; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2067 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2068 | Py_INCREF(args); |
| 2069 | ARG_TUP(self, args); |
| 2070 | if (self->arg) { |
| 2071 | pid = PyObject_Call(f, self->arg, NULL); |
| 2072 | FREE_ARG_TUP(self); |
| 2073 | } |
| 2074 | if (! pid) return -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2075 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2076 | if (pid != Py_None) { |
| 2077 | if (!self->bin) { |
| 2078 | if (!PyString_Check(pid)) { |
| 2079 | PyErr_SetString(PicklingError, |
| 2080 | "persistent id must be string"); |
| 2081 | goto finally; |
| 2082 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2083 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 2084 | if (self->write_func(self, &persid, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2085 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2086 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2087 | if ((size = PyString_Size(pid)) < 0) |
| 2088 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2089 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 2090 | if (self->write_func(self, |
| 2091 | PyString_AS_STRING( |
| 2092 | (PyStringObject *)pid), |
| 2093 | size) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2094 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2095 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 2096 | if (self->write_func(self, "\n", 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2097 | goto finally; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2098 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2099 | res = 1; |
| 2100 | goto finally; |
| 2101 | } |
| 2102 | else if (save(self, pid, 1) >= 0) { |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 2103 | if (self->write_func(self, &binpersid, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2104 | res = -1; |
| 2105 | else |
| 2106 | res = 1; |
| 2107 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2108 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2109 | goto finally; |
| 2110 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2111 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2112 | res = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2113 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2114 | finally: |
| 2115 | Py_XDECREF(pid); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2116 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2117 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2118 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2119 | |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2120 | /* We're saving ob, and args is the 2-thru-5 tuple returned by the |
| 2121 | * appropriate __reduce__ method for ob. |
| 2122 | */ |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2123 | static int |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2124 | save_reduce(Picklerobject *self, PyObject *args, PyObject *ob) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2125 | { |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2126 | PyObject *callable; |
| 2127 | PyObject *argtup; |
| 2128 | PyObject *state = NULL; |
| 2129 | PyObject *listitems = NULL; |
| 2130 | PyObject *dictitems = NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2131 | |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2132 | int use_newobj = self->proto >= 2; |
| 2133 | |
| 2134 | static char reduce = REDUCE; |
| 2135 | static char build = BUILD; |
| 2136 | static char newobj = NEWOBJ; |
| 2137 | |
| 2138 | if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5, |
| 2139 | &callable, |
| 2140 | &argtup, |
| 2141 | &state, |
| 2142 | &listitems, |
| 2143 | &dictitems)) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2144 | return -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2145 | |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2146 | if (state == Py_None) |
| 2147 | state = NULL; |
| 2148 | if (listitems == Py_None) |
| 2149 | listitems = NULL; |
| 2150 | if (dictitems == Py_None) |
| 2151 | dictitems = NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2152 | |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2153 | /* Protocol 2 special case: if callable's name is __newobj__, use |
| 2154 | * NEWOBJ. This consumes a lot of code. |
| 2155 | */ |
| 2156 | if (use_newobj) { |
| 2157 | PyObject *temp = PyObject_GetAttr(callable, __name___str); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2158 | |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2159 | if (temp == NULL) { |
Jeremy Hylton | f00368f | 2003-06-16 20:19:49 +0000 | [diff] [blame] | 2160 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2161 | PyErr_Clear(); |
| 2162 | else |
| 2163 | return -1; |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2164 | use_newobj = 0; |
| 2165 | } |
| 2166 | else { |
| 2167 | use_newobj = PyString_Check(temp) && |
| 2168 | strcmp(PyString_AS_STRING(temp), |
| 2169 | "__newobj__") == 0; |
| 2170 | Py_DECREF(temp); |
| 2171 | } |
| 2172 | } |
| 2173 | if (use_newobj) { |
| 2174 | PyObject *cls; |
| 2175 | PyObject *newargtup; |
| 2176 | int n, i; |
| 2177 | |
| 2178 | /* Sanity checks. */ |
| 2179 | n = PyTuple_Size(argtup); |
| 2180 | if (n < 1) { |
| 2181 | PyErr_SetString(PicklingError, "__newobj__ arglist " |
| 2182 | "is empty"); |
| 2183 | return -1; |
| 2184 | } |
| 2185 | |
| 2186 | cls = PyTuple_GET_ITEM(argtup, 0); |
| 2187 | if (! PyObject_HasAttrString(cls, "__new__")) { |
| 2188 | PyErr_SetString(PicklingError, "args[0] from " |
| 2189 | "__newobj__ args has no __new__"); |
| 2190 | return -1; |
| 2191 | } |
| 2192 | |
| 2193 | /* XXX How could ob be NULL? */ |
| 2194 | if (ob != NULL) { |
| 2195 | PyObject *ob_dot_class; |
| 2196 | |
| 2197 | ob_dot_class = PyObject_GetAttr(ob, __class___str); |
Jeremy Hylton | f00368f | 2003-06-16 20:19:49 +0000 | [diff] [blame] | 2198 | if (ob_dot_class == NULL) { |
| 2199 | if (PyErr_ExceptionMatches( |
| 2200 | PyExc_AttributeError)) |
| 2201 | PyErr_Clear(); |
| 2202 | else |
| 2203 | return -1; |
| 2204 | } |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2205 | i = ob_dot_class != cls; /* true iff a problem */ |
| 2206 | Py_XDECREF(ob_dot_class); |
| 2207 | if (i) { |
| 2208 | PyErr_SetString(PicklingError, "args[0] from " |
| 2209 | "__newobj__ args has the wrong class"); |
| 2210 | return -1; |
| 2211 | } |
| 2212 | } |
| 2213 | |
| 2214 | /* Save the class and its __new__ arguments. */ |
| 2215 | if (save(self, cls, 0) < 0) |
| 2216 | return -1; |
| 2217 | |
| 2218 | newargtup = PyTuple_New(n-1); /* argtup[1:] */ |
| 2219 | if (newargtup == NULL) |
| 2220 | return -1; |
| 2221 | for (i = 1; i < n; ++i) { |
| 2222 | PyObject *temp = PyTuple_GET_ITEM(argtup, i); |
| 2223 | Py_INCREF(temp); |
| 2224 | PyTuple_SET_ITEM(newargtup, i-1, temp); |
| 2225 | } |
| 2226 | i = save(self, newargtup, 0) < 0; |
| 2227 | Py_DECREF(newargtup); |
| 2228 | if (i < 0) |
| 2229 | return -1; |
| 2230 | |
| 2231 | /* Add NEWOBJ opcode. */ |
| 2232 | if (self->write_func(self, &newobj, 1) < 0) |
| 2233 | return -1; |
| 2234 | } |
| 2235 | else { |
| 2236 | /* Not using NEWOBJ. */ |
| 2237 | if (save(self, callable, 0) < 0 || |
| 2238 | save(self, argtup, 0) < 0 || |
| 2239 | self->write_func(self, &reduce, 1) < 0) |
| 2240 | return -1; |
| 2241 | } |
| 2242 | |
| 2243 | /* Memoize. */ |
| 2244 | /* XXX How can ob be NULL? */ |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2245 | if (ob != NULL) { |
| 2246 | if (state && !PyDict_Check(state)) { |
| 2247 | if (put2(self, ob) < 0) |
| 2248 | return -1; |
| 2249 | } |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2250 | else if (put(self, ob) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2251 | return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2252 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2253 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2254 | |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2255 | if (listitems && batch_list(self, listitems) < 0) |
| 2256 | return -1; |
| 2257 | |
| 2258 | if (dictitems && batch_dict(self, dictitems) < 0) |
| 2259 | return -1; |
| 2260 | |
| 2261 | if (state) { |
| 2262 | if (save(self, state, 0) < 0 || |
| 2263 | self->write_func(self, &build, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2264 | return -1; |
| 2265 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2266 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2267 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2268 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2269 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2270 | static int |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 2271 | save(Picklerobject *self, PyObject *args, int pers_save) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2272 | { |
| 2273 | PyTypeObject *type; |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2274 | PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0; |
| 2275 | PyObject *arg_tup; |
| 2276 | int res = -1; |
| 2277 | int tmp, size; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2278 | |
Martin v. Löwis | 5a39530 | 2002-08-04 08:20:23 +0000 | [diff] [blame] | 2279 | if (self->nesting++ > Py_GetRecursionLimit()){ |
| 2280 | PyErr_SetString(PyExc_RuntimeError, |
| 2281 | "maximum recursion depth exceeded"); |
| 2282 | goto finally; |
| 2283 | } |
| 2284 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2285 | if (!pers_save && self->pers_func) { |
| 2286 | if ((tmp = save_pers(self, args, self->pers_func)) != 0) { |
| 2287 | res = tmp; |
| 2288 | goto finally; |
| 2289 | } |
| 2290 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2291 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2292 | if (args == Py_None) { |
| 2293 | res = save_none(self, args); |
| 2294 | goto finally; |
| 2295 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2296 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2297 | type = args->ob_type; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2298 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2299 | switch (type->tp_name[0]) { |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 2300 | case 'b': |
| 2301 | if (args == Py_False || args == Py_True) { |
| 2302 | res = save_bool(self, args); |
| 2303 | goto finally; |
| 2304 | } |
| 2305 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2306 | case 'i': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2307 | if (type == &PyInt_Type) { |
| 2308 | res = save_int(self, args); |
| 2309 | goto finally; |
| 2310 | } |
| 2311 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2312 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2313 | case 'l': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2314 | if (type == &PyLong_Type) { |
| 2315 | res = save_long(self, args); |
| 2316 | goto finally; |
| 2317 | } |
| 2318 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2319 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2320 | case 'f': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2321 | if (type == &PyFloat_Type) { |
| 2322 | res = save_float(self, args); |
| 2323 | goto finally; |
| 2324 | } |
| 2325 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2326 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2327 | case 't': |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 2328 | if (type == &PyTuple_Type && PyTuple_Size(args) == 0) { |
| 2329 | res = save_tuple(self, args); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2330 | goto finally; |
| 2331 | } |
| 2332 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2333 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2334 | case 's': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2335 | if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) { |
| 2336 | res = save_string(self, args, 0); |
| 2337 | goto finally; |
| 2338 | } |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 2339 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2340 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 2341 | case 'u': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2342 | if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) { |
| 2343 | res = save_unicode(self, args, 0); |
| 2344 | goto finally; |
| 2345 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2346 | #endif |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2347 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2348 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2349 | if (args->ob_refcnt > 1) { |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2350 | if (!( py_ob_id = PyLong_FromVoidPtr(args))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2351 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2352 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2353 | if (PyDict_GetItem(self->memo, py_ob_id)) { |
| 2354 | if (get(self, py_ob_id) < 0) |
| 2355 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2356 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2357 | res = 0; |
| 2358 | goto finally; |
| 2359 | } |
| 2360 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2361 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2362 | switch (type->tp_name[0]) { |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2363 | case 's': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2364 | if (type == &PyString_Type) { |
| 2365 | res = save_string(self, args, 1); |
| 2366 | goto finally; |
| 2367 | } |
| 2368 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2369 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2370 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 2371 | case 'u': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2372 | if (type == &PyUnicode_Type) { |
| 2373 | res = save_unicode(self, args, 1); |
| 2374 | goto finally; |
| 2375 | } |
| 2376 | break; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2377 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 2378 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2379 | case 't': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2380 | if (type == &PyTuple_Type) { |
| 2381 | res = save_tuple(self, args); |
| 2382 | goto finally; |
| 2383 | } |
| 2384 | if (type == &PyType_Type) { |
| 2385 | res = save_global(self, args, NULL); |
| 2386 | goto finally; |
| 2387 | } |
| 2388 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2389 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2390 | case 'l': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2391 | if (type == &PyList_Type) { |
| 2392 | res = save_list(self, args); |
| 2393 | goto finally; |
| 2394 | } |
| 2395 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2396 | |
| 2397 | case 'd': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2398 | if (type == &PyDict_Type) { |
| 2399 | res = save_dict(self, args); |
| 2400 | goto finally; |
| 2401 | } |
| 2402 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2403 | |
| 2404 | case 'i': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2405 | if (type == &PyInstance_Type) { |
| 2406 | res = save_inst(self, args); |
| 2407 | goto finally; |
| 2408 | } |
| 2409 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2410 | |
| 2411 | case 'c': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2412 | if (type == &PyClass_Type) { |
| 2413 | res = save_global(self, args, NULL); |
| 2414 | goto finally; |
| 2415 | } |
| 2416 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2417 | |
| 2418 | case 'f': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2419 | if (type == &PyFunction_Type) { |
| 2420 | res = save_global(self, args, NULL); |
Christian Tismer | 2460c62 | 2004-02-26 16:21:45 +0000 | [diff] [blame] | 2421 | if (res && PyErr_ExceptionMatches(PickleError)) { |
| 2422 | /* fall back to reduce */ |
| 2423 | PyErr_Clear(); |
| 2424 | break; |
| 2425 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2426 | goto finally; |
| 2427 | } |
| 2428 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2429 | |
| 2430 | case 'b': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2431 | if (type == &PyCFunction_Type) { |
| 2432 | res = save_global(self, args, NULL); |
| 2433 | goto finally; |
| 2434 | } |
| 2435 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2436 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2437 | if (!pers_save && self->inst_pers_func) { |
| 2438 | if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) { |
| 2439 | res = tmp; |
| 2440 | goto finally; |
| 2441 | } |
| 2442 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2443 | |
Jeremy Hylton | 39c6116 | 2002-07-16 19:47:43 +0000 | [diff] [blame] | 2444 | if (PyType_IsSubtype(type, &PyType_Type)) { |
| 2445 | res = save_global(self, args, NULL); |
| 2446 | goto finally; |
| 2447 | } |
| 2448 | |
Guido van Rossum | b289b87 | 2003-02-19 01:45:13 +0000 | [diff] [blame] | 2449 | /* Get a reduction callable, and call it. This may come from |
| 2450 | * copy_reg.dispatch_table, the object's __reduce_ex__ method, |
| 2451 | * or the object's __reduce__ method. |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2452 | */ |
Tim Peters | 5aa3da6 | 2003-02-13 21:03:57 +0000 | [diff] [blame] | 2453 | __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type); |
| 2454 | if (__reduce__ != NULL) { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2455 | Py_INCREF(__reduce__); |
Guido van Rossum | b289b87 | 2003-02-19 01:45:13 +0000 | [diff] [blame] | 2456 | Py_INCREF(args); |
| 2457 | ARG_TUP(self, args); |
| 2458 | if (self->arg) { |
| 2459 | t = PyObject_Call(__reduce__, self->arg, NULL); |
| 2460 | FREE_ARG_TUP(self); |
| 2461 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2462 | } |
| 2463 | else { |
Guido van Rossum | b289b87 | 2003-02-19 01:45:13 +0000 | [diff] [blame] | 2464 | /* Check for a __reduce_ex__ method. */ |
| 2465 | __reduce__ = PyObject_GetAttr(args, __reduce_ex___str); |
| 2466 | if (__reduce__ != NULL) { |
| 2467 | t = PyInt_FromLong(self->proto); |
| 2468 | if (t != NULL) { |
| 2469 | ARG_TUP(self, t); |
| 2470 | t = NULL; |
| 2471 | if (self->arg) { |
| 2472 | t = PyObject_Call(__reduce__, |
| 2473 | self->arg, NULL); |
| 2474 | FREE_ARG_TUP(self); |
| 2475 | } |
| 2476 | } |
| 2477 | } |
| 2478 | else { |
Jeremy Hylton | f00368f | 2003-06-16 20:19:49 +0000 | [diff] [blame] | 2479 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2480 | PyErr_Clear(); |
| 2481 | else |
| 2482 | goto finally; |
Guido van Rossum | b289b87 | 2003-02-19 01:45:13 +0000 | [diff] [blame] | 2483 | /* Check for a __reduce__ method. */ |
| 2484 | __reduce__ = PyObject_GetAttr(args, __reduce___str); |
| 2485 | if (__reduce__ != NULL) { |
| 2486 | t = PyObject_Call(__reduce__, |
| 2487 | empty_tuple, NULL); |
| 2488 | } |
| 2489 | else { |
| 2490 | PyErr_SetObject(UnpickleableError, args); |
| 2491 | goto finally; |
| 2492 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2493 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2494 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2495 | |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2496 | if (t == NULL) |
| 2497 | goto finally; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2498 | |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2499 | if (PyString_Check(t)) { |
| 2500 | res = save_global(self, args, t); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2501 | goto finally; |
| 2502 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2503 | |
Tim Peters | 71fcda5 | 2003-02-14 23:05:28 +0000 | [diff] [blame] | 2504 | if (! PyTuple_Check(t)) { |
| 2505 | cPickle_ErrFormat(PicklingError, "Value returned by " |
| 2506 | "%s must be string or tuple", |
| 2507 | "O", __reduce__); |
| 2508 | goto finally; |
| 2509 | } |
| 2510 | |
| 2511 | size = PyTuple_Size(t); |
| 2512 | if (size < 2 || size > 5) { |
| 2513 | cPickle_ErrFormat(PicklingError, "tuple returned by " |
| 2514 | "%s must contain 2 through 5 elements", |
| 2515 | "O", __reduce__); |
| 2516 | goto finally; |
| 2517 | } |
| 2518 | |
| 2519 | arg_tup = PyTuple_GET_ITEM(t, 1); |
| 2520 | if (!(PyTuple_Check(arg_tup) || arg_tup == Py_None)) { |
| 2521 | cPickle_ErrFormat(PicklingError, "Second element of " |
| 2522 | "tuple returned by %s must be a tuple", |
| 2523 | "O", __reduce__); |
| 2524 | goto finally; |
| 2525 | } |
| 2526 | |
| 2527 | res = save_reduce(self, t, args); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2528 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2529 | finally: |
Martin v. Löwis | 5a39530 | 2002-08-04 08:20:23 +0000 | [diff] [blame] | 2530 | self->nesting--; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2531 | Py_XDECREF(py_ob_id); |
| 2532 | Py_XDECREF(__reduce__); |
| 2533 | Py_XDECREF(t); |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2534 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2535 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2536 | } |
| 2537 | |
| 2538 | |
| 2539 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2540 | dump(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2541 | { |
| 2542 | static char stop = STOP; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2543 | |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 2544 | if (self->proto >= 2) { |
| 2545 | char bytes[2]; |
| 2546 | |
| 2547 | bytes[0] = PROTO; |
Tim Peters | 87482ea | 2003-02-02 16:16:30 +0000 | [diff] [blame] | 2548 | assert(self->proto >= 0 && self->proto < 256); |
| 2549 | bytes[1] = (char)self->proto; |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 2550 | if (self->write_func(self, bytes, 2) < 0) |
| 2551 | return -1; |
| 2552 | } |
| 2553 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2554 | if (save(self, args, 0) < 0) |
| 2555 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2556 | |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 2557 | if (self->write_func(self, &stop, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2558 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2559 | |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 2560 | if (self->write_func(self, NULL, 0) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2561 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2562 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2563 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2564 | } |
| 2565 | |
| 2566 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2567 | Pickle_clear_memo(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2568 | { |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2569 | if (self->memo) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2570 | PyDict_Clear(self->memo); |
| 2571 | Py_INCREF(Py_None); |
| 2572 | return Py_None; |
| 2573 | } |
| 2574 | |
| 2575 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2576 | Pickle_getvalue(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2577 | { |
| 2578 | int l, i, rsize, ssize, clear=1, lm; |
| 2579 | long ik; |
| 2580 | PyObject *k, *r; |
| 2581 | char *s, *p, *have_get; |
| 2582 | Pdata *data; |
| 2583 | |
| 2584 | /* Can be called by Python code or C code */ |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2585 | if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear)) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2586 | return NULL; |
| 2587 | |
| 2588 | /* Check to make sure we are based on a list */ |
| 2589 | if (! Pdata_Check(self->file)) { |
| 2590 | PyErr_SetString(PicklingError, |
| 2591 | "Attempt to getvalue() a non-list-based pickler"); |
| 2592 | return NULL; |
| 2593 | } |
| 2594 | |
| 2595 | /* flush write buffer */ |
| 2596 | if (write_other(self, NULL, 0) < 0) return NULL; |
| 2597 | |
| 2598 | data=(Pdata*)self->file; |
| 2599 | l=data->length; |
| 2600 | |
| 2601 | /* set up an array to hold get/put status */ |
Tim Peters | ac5687a | 2003-02-02 18:08:34 +0000 | [diff] [blame] | 2602 | lm = PyDict_Size(self->memo); |
| 2603 | if (lm < 0) return NULL; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2604 | lm++; |
Tim Peters | ac5687a | 2003-02-02 18:08:34 +0000 | [diff] [blame] | 2605 | have_get = malloc(lm); |
| 2606 | if (have_get == NULL) return PyErr_NoMemory(); |
| 2607 | memset(have_get, 0, lm); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2608 | |
| 2609 | /* Scan for gets. */ |
Tim Peters | ac5687a | 2003-02-02 18:08:34 +0000 | [diff] [blame] | 2610 | for (rsize = 0, i = l; --i >= 0; ) { |
| 2611 | k = data->data[i]; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2612 | |
Tim Peters | ac5687a | 2003-02-02 18:08:34 +0000 | [diff] [blame] | 2613 | if (PyString_Check(k)) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2614 | rsize += PyString_GET_SIZE(k); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2615 | |
| 2616 | else if (PyInt_Check(k)) { /* put */ |
Tim Peters | ac5687a | 2003-02-02 18:08:34 +0000 | [diff] [blame] | 2617 | ik = PyInt_AS_LONG((PyIntObject*)k); |
| 2618 | if (ik >= lm || ik == 0) { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2619 | PyErr_SetString(PicklingError, |
| 2620 | "Invalid get data"); |
| 2621 | return NULL; |
| 2622 | } |
Tim Peters | ac5687a | 2003-02-02 18:08:34 +0000 | [diff] [blame] | 2623 | if (have_get[ik]) /* with matching get */ |
| 2624 | rsize += ik < 256 ? 2 : 5; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2625 | } |
| 2626 | |
| 2627 | else if (! (PyTuple_Check(k) && |
| 2628 | PyTuple_GET_SIZE(k) == 2 && |
Tim Peters | ac5687a | 2003-02-02 18:08:34 +0000 | [diff] [blame] | 2629 | PyInt_Check((k = PyTuple_GET_ITEM(k, 0)))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2630 | ) { |
| 2631 | PyErr_SetString(PicklingError, |
| 2632 | "Unexpected data in internal list"); |
| 2633 | return NULL; |
| 2634 | } |
| 2635 | |
| 2636 | else { /* put */ |
Tim Peters | ac5687a | 2003-02-02 18:08:34 +0000 | [diff] [blame] | 2637 | ik = PyInt_AS_LONG((PyIntObject *)k); |
| 2638 | if (ik >= lm || ik == 0) { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2639 | PyErr_SetString(PicklingError, |
| 2640 | "Invalid get data"); |
| 2641 | return NULL; |
| 2642 | } |
Tim Peters | ac5687a | 2003-02-02 18:08:34 +0000 | [diff] [blame] | 2643 | have_get[ik] = 1; |
| 2644 | rsize += ik < 256 ? 2 : 5; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2645 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2646 | } |
| 2647 | |
| 2648 | /* Now generate the result */ |
Tim Peters | ac5687a | 2003-02-02 18:08:34 +0000 | [diff] [blame] | 2649 | r = PyString_FromStringAndSize(NULL, rsize); |
| 2650 | if (r == NULL) goto err; |
| 2651 | s = PyString_AS_STRING((PyStringObject *)r); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2652 | |
Tim Peters | ac5687a | 2003-02-02 18:08:34 +0000 | [diff] [blame] | 2653 | for (i = 0; i < l; i++) { |
| 2654 | k = data->data[i]; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2655 | |
| 2656 | if (PyString_Check(k)) { |
Tim Peters | ac5687a | 2003-02-02 18:08:34 +0000 | [diff] [blame] | 2657 | ssize = PyString_GET_SIZE(k); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2658 | if (ssize) { |
Tim Peters | ac5687a | 2003-02-02 18:08:34 +0000 | [diff] [blame] | 2659 | p=PyString_AS_STRING((PyStringObject *)k); |
| 2660 | while (--ssize >= 0) |
| 2661 | *s++ = *p++; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2662 | } |
| 2663 | } |
| 2664 | |
| 2665 | else if (PyTuple_Check(k)) { /* get */ |
Tim Peters | ac5687a | 2003-02-02 18:08:34 +0000 | [diff] [blame] | 2666 | ik = PyInt_AS_LONG((PyIntObject *) |
| 2667 | PyTuple_GET_ITEM(k, 0)); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2668 | if (ik < 256) { |
| 2669 | *s++ = BINGET; |
| 2670 | *s++ = (int)(ik & 0xff); |
| 2671 | } |
| 2672 | else { |
| 2673 | *s++ = LONG_BINGET; |
| 2674 | *s++ = (int)(ik & 0xff); |
| 2675 | *s++ = (int)((ik >> 8) & 0xff); |
| 2676 | *s++ = (int)((ik >> 16) & 0xff); |
| 2677 | *s++ = (int)((ik >> 24) & 0xff); |
| 2678 | } |
| 2679 | } |
| 2680 | |
| 2681 | else { /* put */ |
Tim Peters | ac5687a | 2003-02-02 18:08:34 +0000 | [diff] [blame] | 2682 | ik = PyInt_AS_LONG((PyIntObject*)k); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2683 | |
| 2684 | if (have_get[ik]) { /* with matching get */ |
| 2685 | if (ik < 256) { |
| 2686 | *s++ = BINPUT; |
| 2687 | *s++ = (int)(ik & 0xff); |
| 2688 | } |
| 2689 | else { |
| 2690 | *s++ = LONG_BINPUT; |
| 2691 | *s++ = (int)(ik & 0xff); |
| 2692 | *s++ = (int)((ik >> 8) & 0xff); |
| 2693 | *s++ = (int)((ik >> 16) & 0xff); |
| 2694 | *s++ = (int)((ik >> 24) & 0xff); |
| 2695 | } |
| 2696 | } |
| 2697 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2698 | } |
| 2699 | |
| 2700 | if (clear) { |
| 2701 | PyDict_Clear(self->memo); |
Tim Peters | ac5687a | 2003-02-02 18:08:34 +0000 | [diff] [blame] | 2702 | Pdata_clear(data, 0); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2703 | } |
| 2704 | |
| 2705 | free(have_get); |
| 2706 | return r; |
| 2707 | err: |
| 2708 | free(have_get); |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2709 | return NULL; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2710 | } |
| 2711 | |
| 2712 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2713 | Pickler_dump(Picklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2714 | { |
| 2715 | PyObject *ob; |
| 2716 | int get=0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2717 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2718 | if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2719 | return NULL; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2720 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2721 | if (dump(self, ob) < 0) |
| 2722 | return NULL; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2723 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2724 | if (get) return Pickle_getvalue(self, NULL); |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2725 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2726 | /* XXX Why does dump() return self? */ |
| 2727 | Py_INCREF(self); |
| 2728 | return (PyObject*)self; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2729 | } |
| 2730 | |
| 2731 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2732 | static struct PyMethodDef Pickler_methods[] = |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2733 | { |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 2734 | {"dump", (PyCFunction)Pickler_dump, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 2735 | PyDoc_STR("dump(object) -- " |
| 2736 | "Write an object in pickle format to the object's pickle stream")}, |
Fred Drake | 0ebacc8 | 2002-05-01 20:36:39 +0000 | [diff] [blame] | 2737 | {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 2738 | PyDoc_STR("clear_memo() -- Clear the picklers memo")}, |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 2739 | {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 2740 | PyDoc_STR("getvalue() -- Finish picking a list-based pickle")}, |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2741 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2742 | }; |
| 2743 | |
| 2744 | |
| 2745 | static Picklerobject * |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 2746 | newPicklerobject(PyObject *file, int proto) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2747 | { |
| 2748 | Picklerobject *self; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2749 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 2750 | if (proto < 0) |
Tim Peters | 8587b3c | 2003-02-13 15:44:41 +0000 | [diff] [blame] | 2751 | proto = HIGHEST_PROTOCOL; |
| 2752 | if (proto > HIGHEST_PROTOCOL) { |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 2753 | PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; " |
| 2754 | "the highest available protocol is %d", |
Tim Peters | 8587b3c | 2003-02-13 15:44:41 +0000 | [diff] [blame] | 2755 | proto, HIGHEST_PROTOCOL); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2756 | return NULL; |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 2757 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2758 | |
Jeremy Hylton | 4cf6319 | 2003-04-09 21:05:12 +0000 | [diff] [blame] | 2759 | self = PyObject_GC_New(Picklerobject, &Picklertype); |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 2760 | if (self == NULL) |
| 2761 | return NULL; |
| 2762 | self->proto = proto; |
| 2763 | self->bin = proto > 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2764 | self->fp = NULL; |
| 2765 | self->write = NULL; |
| 2766 | self->memo = NULL; |
| 2767 | self->arg = NULL; |
| 2768 | self->pers_func = NULL; |
| 2769 | self->inst_pers_func = NULL; |
| 2770 | self->write_buf = NULL; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2771 | self->fast = 0; |
Martin v. Löwis | 5a39530 | 2002-08-04 08:20:23 +0000 | [diff] [blame] | 2772 | self->nesting = 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2773 | self->fast_container = 0; |
| 2774 | self->fast_memo = NULL; |
| 2775 | self->buf_size = 0; |
| 2776 | self->dispatch_table = NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2777 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 2778 | self->file = NULL; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2779 | if (file) |
| 2780 | Py_INCREF(file); |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 2781 | else { |
| 2782 | file = Pdata_New(); |
| 2783 | if (file == NULL) |
| 2784 | goto err; |
| 2785 | } |
| 2786 | self->file = file; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2787 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2788 | if (!( self->memo = PyDict_New())) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2789 | goto err; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2790 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2791 | if (PyFile_Check(file)) { |
| 2792 | self->fp = PyFile_AsFile(file); |
| 2793 | if (self->fp == NULL) { |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 2794 | PyErr_SetString(PyExc_ValueError, |
| 2795 | "I/O operation on closed file"); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2796 | goto err; |
| 2797 | } |
| 2798 | self->write_func = write_file; |
Guido van Rossum | c91fcaa | 1999-03-29 20:00:14 +0000 | [diff] [blame] | 2799 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2800 | else if (PycStringIO_OutputCheck(file)) { |
| 2801 | self->write_func = write_cStringIO; |
| 2802 | } |
| 2803 | else if (file == Py_None) { |
| 2804 | self->write_func = write_none; |
| 2805 | } |
| 2806 | else { |
| 2807 | self->write_func = write_other; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2808 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2809 | if (! Pdata_Check(file)) { |
| 2810 | self->write = PyObject_GetAttr(file, write_str); |
| 2811 | if (!self->write) { |
| 2812 | PyErr_Clear(); |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2813 | PyErr_SetString(PyExc_TypeError, |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2814 | "argument must have 'write' " |
| 2815 | "attribute"); |
| 2816 | goto err; |
| 2817 | } |
| 2818 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2819 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 2820 | self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE); |
| 2821 | if (self->write_buf == NULL) { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2822 | PyErr_NoMemory(); |
| 2823 | goto err; |
| 2824 | } |
| 2825 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2826 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2827 | if (PyEval_GetRestricted()) { |
| 2828 | /* Restricted execution, get private tables */ |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 2829 | PyObject *m = PyImport_Import(copy_reg_str); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 2830 | |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 2831 | if (m == NULL) |
| 2832 | goto err; |
| 2833 | self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2834 | Py_DECREF(m); |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 2835 | if (self->dispatch_table == NULL) |
| 2836 | goto err; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2837 | } |
| 2838 | else { |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 2839 | self->dispatch_table = dispatch_table; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2840 | Py_INCREF(dispatch_table); |
| 2841 | } |
Jeremy Hylton | 4cf6319 | 2003-04-09 21:05:12 +0000 | [diff] [blame] | 2842 | PyObject_GC_Track(self); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 2843 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2844 | return self; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 2845 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2846 | err: |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 2847 | Py_DECREF(self); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2848 | return NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2849 | } |
| 2850 | |
| 2851 | |
| 2852 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2853 | get_Pickler(PyObject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2854 | { |
| 2855 | PyObject *file = NULL; |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 2856 | int proto = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2857 | |
Tim Peters | 92c8bb3 | 2003-02-13 23:00:26 +0000 | [diff] [blame] | 2858 | /* XXX |
| 2859 | * The documented signature is Pickler(file, proto=0), but this |
| 2860 | * accepts Pickler() and Pickler(integer) too. The meaning then |
| 2861 | * is clear as mud, undocumented, and not supported by pickle.py. |
| 2862 | * I'm told Zope uses this, but I haven't traced into this code |
| 2863 | * far enough to figure out what it means. |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 2864 | */ |
| 2865 | if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2866 | PyErr_Clear(); |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 2867 | proto = 0; |
| 2868 | if (!PyArg_ParseTuple(args, "O|i:Pickler", &file, &proto)) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2869 | return NULL; |
| 2870 | } |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 2871 | return (PyObject *)newPicklerobject(file, proto); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2872 | } |
| 2873 | |
| 2874 | |
| 2875 | static void |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 2876 | Pickler_dealloc(Picklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2877 | { |
Jeremy Hylton | 4cf6319 | 2003-04-09 21:05:12 +0000 | [diff] [blame] | 2878 | PyObject_GC_UnTrack(self); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2879 | Py_XDECREF(self->write); |
| 2880 | Py_XDECREF(self->memo); |
| 2881 | Py_XDECREF(self->fast_memo); |
| 2882 | Py_XDECREF(self->arg); |
| 2883 | Py_XDECREF(self->file); |
| 2884 | Py_XDECREF(self->pers_func); |
| 2885 | Py_XDECREF(self->inst_pers_func); |
| 2886 | Py_XDECREF(self->dispatch_table); |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 2887 | PyMem_Free(self->write_buf); |
Tim Peters | 3cfe754 | 2003-05-21 21:29:48 +0000 | [diff] [blame] | 2888 | self->ob_type->tp_free((PyObject *)self); |
Jeremy Hylton | 4cf6319 | 2003-04-09 21:05:12 +0000 | [diff] [blame] | 2889 | } |
| 2890 | |
| 2891 | static int |
| 2892 | Pickler_traverse(Picklerobject *self, visitproc visit, void *arg) |
| 2893 | { |
| 2894 | int err; |
| 2895 | #define VISIT(SLOT) \ |
| 2896 | if (SLOT) { \ |
| 2897 | err = visit((PyObject *)(SLOT), arg); \ |
| 2898 | if (err) \ |
| 2899 | return err; \ |
| 2900 | } |
| 2901 | VISIT(self->write); |
| 2902 | VISIT(self->memo); |
| 2903 | VISIT(self->fast_memo); |
| 2904 | VISIT(self->arg); |
| 2905 | VISIT(self->file); |
| 2906 | VISIT(self->pers_func); |
| 2907 | VISIT(self->inst_pers_func); |
| 2908 | VISIT(self->dispatch_table); |
| 2909 | #undef VISIT |
| 2910 | return 0; |
| 2911 | } |
| 2912 | |
| 2913 | static int |
| 2914 | Pickler_clear(Picklerobject *self) |
| 2915 | { |
| 2916 | #define CLEAR(SLOT) Py_XDECREF(SLOT); SLOT = NULL; |
| 2917 | CLEAR(self->write); |
| 2918 | CLEAR(self->memo); |
| 2919 | CLEAR(self->fast_memo); |
| 2920 | CLEAR(self->arg); |
| 2921 | CLEAR(self->file); |
| 2922 | CLEAR(self->pers_func); |
| 2923 | CLEAR(self->inst_pers_func); |
| 2924 | CLEAR(self->dispatch_table); |
| 2925 | #undef CLEAR |
| 2926 | return 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2927 | } |
| 2928 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2929 | static PyObject * |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2930 | Pickler_get_pers_func(Picklerobject *p) |
| 2931 | { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2932 | if (p->pers_func == NULL) |
| 2933 | PyErr_SetString(PyExc_AttributeError, "persistent_id"); |
| 2934 | else |
| 2935 | Py_INCREF(p->pers_func); |
| 2936 | return p->pers_func; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2939 | static int |
| 2940 | Pickler_set_pers_func(Picklerobject *p, PyObject *v) |
| 2941 | { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2942 | if (v == NULL) { |
| 2943 | PyErr_SetString(PyExc_TypeError, |
| 2944 | "attribute deletion is not supported"); |
| 2945 | return -1; |
| 2946 | } |
| 2947 | Py_XDECREF(p->pers_func); |
| 2948 | Py_INCREF(v); |
| 2949 | p->pers_func = v; |
| 2950 | return 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2951 | } |
| 2952 | |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2953 | static int |
| 2954 | Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v) |
| 2955 | { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2956 | if (v == NULL) { |
| 2957 | PyErr_SetString(PyExc_TypeError, |
| 2958 | "attribute deletion is not supported"); |
| 2959 | return -1; |
| 2960 | } |
| 2961 | Py_XDECREF(p->inst_pers_func); |
| 2962 | Py_INCREF(v); |
| 2963 | p->inst_pers_func = v; |
| 2964 | return 0; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2965 | } |
| 2966 | |
| 2967 | static PyObject * |
| 2968 | Pickler_get_memo(Picklerobject *p) |
| 2969 | { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2970 | if (p->memo == NULL) |
| 2971 | PyErr_SetString(PyExc_AttributeError, "memo"); |
| 2972 | else |
| 2973 | Py_INCREF(p->memo); |
| 2974 | return p->memo; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2975 | } |
| 2976 | |
| 2977 | static int |
| 2978 | Pickler_set_memo(Picklerobject *p, PyObject *v) |
| 2979 | { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2980 | if (v == NULL) { |
| 2981 | PyErr_SetString(PyExc_TypeError, |
| 2982 | "attribute deletion is not supported"); |
| 2983 | return -1; |
| 2984 | } |
| 2985 | if (!PyDict_Check(v)) { |
| 2986 | PyErr_SetString(PyExc_TypeError, "memo must be a dictionary"); |
| 2987 | return -1; |
| 2988 | } |
| 2989 | Py_XDECREF(p->memo); |
| 2990 | Py_INCREF(v); |
| 2991 | p->memo = v; |
| 2992 | return 0; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2993 | } |
| 2994 | |
| 2995 | static PyObject * |
| 2996 | Pickler_get_error(Picklerobject *p) |
| 2997 | { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2998 | /* why is this an attribute on the Pickler? */ |
| 2999 | Py_INCREF(PicklingError); |
| 3000 | return PicklingError; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3001 | } |
| 3002 | |
| 3003 | static PyMemberDef Pickler_members[] = { |
| 3004 | {"binary", T_INT, offsetof(Picklerobject, bin)}, |
| 3005 | {"fast", T_INT, offsetof(Picklerobject, fast)}, |
Jeremy Hylton | 3eb46f3 | 2001-10-16 17:10:49 +0000 | [diff] [blame] | 3006 | {NULL} |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3007 | }; |
| 3008 | |
| 3009 | static PyGetSetDef Pickler_getsets[] = { |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3010 | {"persistent_id", (getter)Pickler_get_pers_func, |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3011 | (setter)Pickler_set_pers_func}, |
| 3012 | {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func}, |
| 3013 | {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo}, |
Jeremy Hylton | 3eb46f3 | 2001-10-16 17:10:49 +0000 | [diff] [blame] | 3014 | {"PicklingError", (getter)Pickler_get_error, NULL}, |
| 3015 | {NULL} |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3016 | }; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3017 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3018 | PyDoc_STRVAR(Picklertype__doc__, |
| 3019 | "Objects that know how to pickle objects\n"); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3020 | |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 3021 | static PyTypeObject Picklertype = { |
| 3022 | PyObject_HEAD_INIT(NULL) |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3023 | 0, /*ob_size*/ |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 3024 | "cPickle.Pickler", /*tp_name*/ |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3025 | sizeof(Picklerobject), /*tp_basicsize*/ |
| 3026 | 0, |
| 3027 | (destructor)Pickler_dealloc, /* tp_dealloc */ |
| 3028 | 0, /* tp_print */ |
| 3029 | 0, /* tp_getattr */ |
| 3030 | 0, /* tp_setattr */ |
| 3031 | 0, /* tp_compare */ |
| 3032 | 0, /* tp_repr */ |
| 3033 | 0, /* tp_as_number */ |
| 3034 | 0, /* tp_as_sequence */ |
| 3035 | 0, /* tp_as_mapping */ |
| 3036 | 0, /* tp_hash */ |
| 3037 | 0, /* tp_call */ |
| 3038 | 0, /* tp_str */ |
Jason Tishler | fb8595d | 2003-01-06 12:41:26 +0000 | [diff] [blame] | 3039 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3040 | PyObject_GenericSetAttr, /* tp_setattro */ |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3041 | 0, /* tp_as_buffer */ |
Jeremy Hylton | 4cf6319 | 2003-04-09 21:05:12 +0000 | [diff] [blame] | 3042 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3043 | Picklertype__doc__, /* tp_doc */ |
Jeremy Hylton | 4cf6319 | 2003-04-09 21:05:12 +0000 | [diff] [blame] | 3044 | (traverseproc)Pickler_traverse, /* tp_traverse */ |
| 3045 | (inquiry)Pickler_clear, /* tp_clear */ |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3046 | 0, /* tp_richcompare */ |
| 3047 | 0, /* tp_weaklistoffset */ |
| 3048 | 0, /* tp_iter */ |
| 3049 | 0, /* tp_iternext */ |
| 3050 | Pickler_methods, /* tp_methods */ |
| 3051 | Pickler_members, /* tp_members */ |
| 3052 | Pickler_getsets, /* tp_getset */ |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 3053 | }; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3054 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3055 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3056 | 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] | 3057 | { |
| 3058 | PyObject *global = 0, *module; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3059 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3060 | if (fc) { |
| 3061 | if (fc==Py_None) { |
Tim Peters | 92c8bb3 | 2003-02-13 23:00:26 +0000 | [diff] [blame] | 3062 | PyErr_SetString(UnpicklingError, "Global and instance " |
| 3063 | "pickles are not supported."); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3064 | return NULL; |
| 3065 | } |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3066 | return PyObject_CallFunction(fc, "OO", py_module_name, |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3067 | py_global_name); |
| 3068 | } |
Guido van Rossum | 1b9e0aa | 1999-04-19 17:58:18 +0000 | [diff] [blame] | 3069 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3070 | module = PySys_GetObject("modules"); |
| 3071 | if (module == NULL) |
| 3072 | return NULL; |
Jeremy Hylton | d105523 | 1998-08-11 19:52:51 +0000 | [diff] [blame] | 3073 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3074 | module = PyDict_GetItem(module, py_module_name); |
| 3075 | if (module == NULL) { |
| 3076 | module = PyImport_Import(py_module_name); |
| 3077 | if (!module) |
| 3078 | return NULL; |
| 3079 | global = PyObject_GetAttr(module, py_global_name); |
| 3080 | Py_DECREF(module); |
| 3081 | } |
| 3082 | else |
| 3083 | global = PyObject_GetAttr(module, py_global_name); |
| 3084 | return global; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3085 | } |
| 3086 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3087 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3088 | marker(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3089 | { |
| 3090 | if (self->num_marks < 1) { |
| 3091 | PyErr_SetString(UnpicklingError, "could not find MARK"); |
| 3092 | return -1; |
| 3093 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3094 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3095 | return self->marks[--self->num_marks]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3096 | } |
| 3097 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3098 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3099 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3100 | load_none(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3101 | { |
| 3102 | PDATA_APPEND(self->stack, Py_None, -1); |
| 3103 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3104 | } |
| 3105 | |
Guido van Rossum | f9ffb03 | 1999-02-04 14:54:04 +0000 | [diff] [blame] | 3106 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3107 | bad_readline(void) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3108 | { |
| 3109 | PyErr_SetString(UnpicklingError, "pickle data was truncated"); |
| 3110 | return -1; |
Guido van Rossum | f9ffb03 | 1999-02-04 14:54:04 +0000 | [diff] [blame] | 3111 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3112 | |
| 3113 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3114 | load_int(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3115 | { |
| 3116 | PyObject *py_int = 0; |
| 3117 | char *endptr, *s; |
| 3118 | int len, res = -1; |
| 3119 | long l; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3120 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3121 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3122 | if (len < 2) return bad_readline(); |
| 3123 | if (!( s=pystrndup(s,len))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3124 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3125 | errno = 0; |
| 3126 | l = strtol(s, &endptr, 0); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3127 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3128 | if (errno || (*endptr != '\n') || (endptr[1] != '\0')) { |
| 3129 | /* Hm, maybe we've got something long. Let's try reading |
| 3130 | it as a Python long object. */ |
| 3131 | errno = 0; |
| 3132 | py_int = PyLong_FromString(s, NULL, 0); |
| 3133 | if (py_int == NULL) { |
| 3134 | PyErr_SetString(PyExc_ValueError, |
| 3135 | "could not convert string to int"); |
| 3136 | goto finally; |
| 3137 | } |
| 3138 | } |
| 3139 | else { |
Guido van Rossum | e276339 | 2002-04-05 19:30:08 +0000 | [diff] [blame] | 3140 | if (len == 3 && (l == 0 || l == 1)) { |
| 3141 | if (!( py_int = PyBool_FromLong(l))) goto finally; |
| 3142 | } |
| 3143 | else { |
| 3144 | if (!( py_int = PyInt_FromLong(l))) goto finally; |
| 3145 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3146 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3147 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3148 | free(s); |
| 3149 | PDATA_PUSH(self->stack, py_int, -1); |
| 3150 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3151 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3152 | finally: |
| 3153 | free(s); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3154 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3155 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3156 | } |
| 3157 | |
Tim Peters | 3c67d79 | 2003-02-02 17:59:11 +0000 | [diff] [blame] | 3158 | static int |
| 3159 | load_bool(Unpicklerobject *self, PyObject *boolean) |
| 3160 | { |
| 3161 | assert(boolean == Py_True || boolean == Py_False); |
Tim Peters | e0a3907 | 2003-02-03 15:45:56 +0000 | [diff] [blame] | 3162 | PDATA_APPEND(self->stack, boolean, -1); |
Tim Peters | 3c67d79 | 2003-02-02 17:59:11 +0000 | [diff] [blame] | 3163 | return 0; |
| 3164 | } |
| 3165 | |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3166 | /* s contains x bytes of a little-endian integer. Return its value as a |
| 3167 | * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian |
| 3168 | * int, but when x is 4 it's a signed one. This is an historical source |
| 3169 | * of x-platform bugs. |
| 3170 | */ |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3171 | static long |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3172 | calc_binint(char *s, int x) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3173 | { |
| 3174 | unsigned char c; |
| 3175 | int i; |
| 3176 | long l; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3177 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3178 | for (i = 0, l = 0L; i < x; i++) { |
| 3179 | c = (unsigned char)s[i]; |
| 3180 | l |= (long)c << (i * 8); |
| 3181 | } |
Tim Peters | bfa18f7 | 2001-04-10 01:54:42 +0000 | [diff] [blame] | 3182 | #if SIZEOF_LONG > 4 |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3183 | /* Unlike BININT1 and BININT2, BININT (more accurately BININT4) |
| 3184 | * is signed, so on a box with longs bigger than 4 bytes we need |
| 3185 | * to extend a BININT's sign bit to the full width. |
| 3186 | */ |
| 3187 | if (x == 4 && l & (1L << 31)) |
| 3188 | l |= (~0L) << 32; |
Tim Peters | bfa18f7 | 2001-04-10 01:54:42 +0000 | [diff] [blame] | 3189 | #endif |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3190 | return l; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3191 | } |
| 3192 | |
| 3193 | |
| 3194 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3195 | load_binintx(Unpicklerobject *self, char *s, int x) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3196 | { |
| 3197 | PyObject *py_int = 0; |
| 3198 | long l; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3199 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3200 | l = calc_binint(s, x); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3201 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3202 | if (!( py_int = PyInt_FromLong(l))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3203 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3204 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3205 | PDATA_PUSH(self->stack, py_int, -1); |
| 3206 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3207 | } |
| 3208 | |
| 3209 | |
| 3210 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3211 | load_binint(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3212 | { |
| 3213 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3214 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3215 | if (self->read_func(self, &s, 4) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3216 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3217 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3218 | return load_binintx(self, s, 4); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3219 | } |
| 3220 | |
| 3221 | |
| 3222 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3223 | load_binint1(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3224 | { |
| 3225 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3226 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3227 | if (self->read_func(self, &s, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3228 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3229 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3230 | return load_binintx(self, s, 1); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3231 | } |
| 3232 | |
| 3233 | |
| 3234 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3235 | load_binint2(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3236 | { |
| 3237 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3238 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3239 | if (self->read_func(self, &s, 2) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3240 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3241 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3242 | return load_binintx(self, s, 2); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3243 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3244 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3245 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3246 | load_long(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3247 | { |
| 3248 | PyObject *l = 0; |
| 3249 | char *end, *s; |
| 3250 | int len, res = -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3251 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3252 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3253 | if (len < 2) return bad_readline(); |
| 3254 | if (!( s=pystrndup(s,len))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3255 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3256 | if (!( l = PyLong_FromString(s, &end, 0))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3257 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3258 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3259 | free(s); |
| 3260 | PDATA_PUSH(self->stack, l, -1); |
| 3261 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3262 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3263 | finally: |
| 3264 | free(s); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3265 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3266 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3267 | } |
| 3268 | |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3269 | /* 'size' bytes contain the # of bytes of little-endian 256's-complement |
| 3270 | * data following. |
| 3271 | */ |
| 3272 | static int |
| 3273 | load_counted_long(Unpicklerobject *self, int size) |
| 3274 | { |
| 3275 | int i; |
| 3276 | char *nbytes; |
| 3277 | unsigned char *pdata; |
| 3278 | PyObject *along; |
| 3279 | |
| 3280 | assert(size == 1 || size == 4); |
| 3281 | i = self->read_func(self, &nbytes, size); |
| 3282 | if (i < 0) return -1; |
| 3283 | |
| 3284 | size = calc_binint(nbytes, size); |
| 3285 | if (size < 0) { |
| 3286 | /* Corrupt or hostile pickle -- we never write one like |
| 3287 | * this. |
| 3288 | */ |
Tim Peters | 0c7c48e | 2003-02-03 22:07:24 +0000 | [diff] [blame] | 3289 | PyErr_SetString(UnpicklingError, "LONG pickle has negative " |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3290 | "byte count"); |
| 3291 | return -1; |
| 3292 | } |
| 3293 | |
| 3294 | if (size == 0) |
| 3295 | along = PyLong_FromLong(0L); |
| 3296 | else { |
| 3297 | /* Read the raw little-endian bytes & convert. */ |
Neal Norwitz | 529baf2 | 2003-02-02 17:08:33 +0000 | [diff] [blame] | 3298 | i = self->read_func(self, (char **)&pdata, size); |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 3299 | if (i < 0) return -1; |
| 3300 | along = _PyLong_FromByteArray(pdata, (size_t)size, |
| 3301 | 1 /* little endian */, 1 /* signed */); |
| 3302 | } |
| 3303 | if (along == NULL) |
| 3304 | return -1; |
| 3305 | PDATA_PUSH(self->stack, along, -1); |
| 3306 | return 0; |
| 3307 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3308 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3309 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3310 | load_float(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3311 | { |
| 3312 | PyObject *py_float = 0; |
| 3313 | char *endptr, *s; |
| 3314 | int len, res = -1; |
| 3315 | double d; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3316 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3317 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3318 | if (len < 2) return bad_readline(); |
| 3319 | if (!( s=pystrndup(s,len))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3320 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3321 | errno = 0; |
| 3322 | d = strtod(s, &endptr); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3323 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3324 | if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) { |
| 3325 | PyErr_SetString(PyExc_ValueError, |
| 3326 | "could not convert string to float"); |
| 3327 | goto finally; |
| 3328 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3329 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3330 | if (!( py_float = PyFloat_FromDouble(d))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3331 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3332 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3333 | free(s); |
| 3334 | PDATA_PUSH(self->stack, py_float, -1); |
| 3335 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3336 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3337 | finally: |
| 3338 | free(s); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3339 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3340 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3341 | } |
| 3342 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3343 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3344 | load_binfloat(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3345 | { |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 3346 | PyObject *py_float; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3347 | double x; |
| 3348 | char *p; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3349 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3350 | if (self->read_func(self, &p, 8) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3351 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3352 | |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 3353 | x = _PyFloat_Unpack8((unsigned char *)p, 0); |
| 3354 | if (x == -1.0 && PyErr_Occurred()) |
| 3355 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3356 | |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 3357 | py_float = PyFloat_FromDouble(x); |
| 3358 | if (py_float == NULL) |
| 3359 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3360 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3361 | PDATA_PUSH(self->stack, py_float, -1); |
| 3362 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3363 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3364 | |
| 3365 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3366 | load_string(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3367 | { |
| 3368 | PyObject *str = 0; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 3369 | int len, res = -1; |
| 3370 | char *s, *p; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3371 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3372 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3373 | if (len < 2) return bad_readline(); |
| 3374 | if (!( s=pystrndup(s,len))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3375 | |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 3376 | |
| 3377 | /* Strip outermost quotes */ |
| 3378 | while (s[len-1] <= ' ') |
| 3379 | len--; |
| 3380 | if(s[0]=='"' && s[len-1]=='"'){ |
| 3381 | s[len-1] = '\0'; |
| 3382 | p = s + 1 ; |
| 3383 | len -= 2; |
| 3384 | } else if(s[0]=='\'' && s[len-1]=='\''){ |
| 3385 | s[len-1] = '\0'; |
| 3386 | p = s + 1 ; |
| 3387 | len -= 2; |
| 3388 | } else |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3389 | goto insecure; |
| 3390 | /********************************************/ |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 3391 | |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 3392 | str = PyString_DecodeEscape(p, len, NULL, 0, NULL); |
| 3393 | if (str) { |
| 3394 | PDATA_PUSH(self->stack, str, -1); |
| 3395 | res = 0; |
| 3396 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3397 | free(s); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3398 | return res; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3399 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3400 | insecure: |
| 3401 | free(s); |
| 3402 | PyErr_SetString(PyExc_ValueError,"insecure string pickle"); |
| 3403 | return -1; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3404 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3405 | |
| 3406 | |
| 3407 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3408 | load_binstring(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3409 | { |
| 3410 | PyObject *py_string = 0; |
| 3411 | long l; |
| 3412 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3413 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3414 | if (self->read_func(self, &s, 4) < 0) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3415 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3416 | l = calc_binint(s, 4); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3417 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3418 | if (self->read_func(self, &s, l) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3419 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3420 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3421 | if (!( py_string = PyString_FromStringAndSize(s, l))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3422 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3423 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3424 | PDATA_PUSH(self->stack, py_string, -1); |
| 3425 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3426 | } |
| 3427 | |
| 3428 | |
| 3429 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3430 | load_short_binstring(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3431 | { |
| 3432 | PyObject *py_string = 0; |
| 3433 | unsigned char l; |
| 3434 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3435 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3436 | if (self->read_func(self, &s, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3437 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3438 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3439 | l = (unsigned char)s[0]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3440 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3441 | if (self->read_func(self, &s, l) < 0) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3442 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3443 | if (!( py_string = PyString_FromStringAndSize(s, l))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3444 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3445 | PDATA_PUSH(self->stack, py_string, -1); |
| 3446 | return 0; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3447 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3448 | |
| 3449 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3450 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3451 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3452 | load_unicode(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3453 | { |
| 3454 | PyObject *str = 0; |
| 3455 | int len, res = -1; |
| 3456 | char *s; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3457 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3458 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3459 | if (len < 1) return bad_readline(); |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3460 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3461 | if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3462 | goto finally; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3463 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3464 | PDATA_PUSH(self->stack, str, -1); |
| 3465 | return 0; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3466 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3467 | finally: |
| 3468 | return res; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3469 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3470 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3471 | |
| 3472 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3473 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3474 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3475 | load_binunicode(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3476 | { |
| 3477 | PyObject *unicode; |
| 3478 | long l; |
| 3479 | char *s; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3480 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3481 | if (self->read_func(self, &s, 4) < 0) return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3482 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3483 | l = calc_binint(s, 4); |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3484 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3485 | if (self->read_func(self, &s, l) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3486 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3487 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3488 | if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3489 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3490 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3491 | PDATA_PUSH(self->stack, unicode, -1); |
| 3492 | return 0; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3493 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3494 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3495 | |
| 3496 | |
| 3497 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3498 | load_tuple(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3499 | { |
| 3500 | PyObject *tup; |
| 3501 | int i; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3502 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3503 | if ((i = marker(self)) < 0) return -1; |
| 3504 | if (!( tup=Pdata_popTuple(self->stack, i))) return -1; |
| 3505 | PDATA_PUSH(self->stack, tup, -1); |
| 3506 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3507 | } |
| 3508 | |
| 3509 | static int |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 3510 | load_counted_tuple(Unpicklerobject *self, int len) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3511 | { |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 3512 | PyObject *tup = PyTuple_New(len); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3513 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 3514 | if (tup == NULL) |
| 3515 | return -1; |
| 3516 | |
| 3517 | while (--len >= 0) { |
| 3518 | PyObject *element; |
| 3519 | |
| 3520 | PDATA_POP(self->stack, element); |
| 3521 | if (element == NULL) |
| 3522 | return -1; |
| 3523 | PyTuple_SET_ITEM(tup, len, element); |
| 3524 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3525 | PDATA_PUSH(self->stack, tup, -1); |
| 3526 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3527 | } |
| 3528 | |
| 3529 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3530 | load_empty_list(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3531 | { |
| 3532 | PyObject *list; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3533 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3534 | if (!( list=PyList_New(0))) return -1; |
| 3535 | PDATA_PUSH(self->stack, list, -1); |
| 3536 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3537 | } |
| 3538 | |
| 3539 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3540 | load_empty_dict(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3541 | { |
| 3542 | PyObject *dict; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3543 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3544 | if (!( dict=PyDict_New())) return -1; |
| 3545 | PDATA_PUSH(self->stack, dict, -1); |
| 3546 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3547 | } |
| 3548 | |
| 3549 | |
| 3550 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3551 | load_list(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3552 | { |
| 3553 | PyObject *list = 0; |
| 3554 | int i; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3555 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3556 | if ((i = marker(self)) < 0) return -1; |
| 3557 | if (!( list=Pdata_popList(self->stack, i))) return -1; |
| 3558 | PDATA_PUSH(self->stack, list, -1); |
| 3559 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3560 | } |
| 3561 | |
| 3562 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3563 | load_dict(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3564 | { |
| 3565 | PyObject *dict, *key, *value; |
| 3566 | int i, j, k; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3567 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3568 | if ((i = marker(self)) < 0) return -1; |
| 3569 | j=self->stack->length; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3570 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3571 | if (!( dict = PyDict_New())) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3572 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3573 | for (k = i+1; k < j; k += 2) { |
| 3574 | key =self->stack->data[k-1]; |
| 3575 | value=self->stack->data[k ]; |
| 3576 | if (PyDict_SetItem(dict, key, value) < 0) { |
| 3577 | Py_DECREF(dict); |
| 3578 | return -1; |
| 3579 | } |
| 3580 | } |
| 3581 | Pdata_clear(self->stack, i); |
| 3582 | PDATA_PUSH(self->stack, dict, -1); |
| 3583 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3584 | } |
| 3585 | |
| 3586 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3587 | Instance_New(PyObject *cls, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3588 | { |
Tim Peters | 1f1b2d2 | 2003-02-01 02:16:37 +0000 | [diff] [blame] | 3589 | PyObject *r = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3590 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3591 | if (PyClass_Check(cls)) { |
| 3592 | int l; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3593 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3594 | if ((l=PyObject_Size(args)) < 0) goto err; |
| 3595 | if (!( l )) { |
| 3596 | PyObject *__getinitargs__; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3597 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3598 | __getinitargs__ = PyObject_GetAttr(cls, |
Jeremy Hylton | f00368f | 2003-06-16 20:19:49 +0000 | [diff] [blame] | 3599 | __getinitargs___str); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3600 | if (!__getinitargs__) { |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3601 | /* We have a class with no __getinitargs__, |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3602 | so bypass usual construction */ |
| 3603 | PyObject *inst; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3604 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3605 | PyErr_Clear(); |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3606 | if (!( inst=PyInstance_NewRaw(cls, NULL))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3607 | goto err; |
| 3608 | return inst; |
| 3609 | } |
| 3610 | Py_DECREF(__getinitargs__); |
| 3611 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3612 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3613 | if ((r=PyInstance_New(cls, args, NULL))) return r; |
| 3614 | else goto err; |
| 3615 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3616 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3617 | if (args==Py_None) { |
| 3618 | /* Special case, call cls.__basicnew__() */ |
| 3619 | PyObject *basicnew; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3620 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3621 | basicnew = PyObject_GetAttr(cls, __basicnew___str); |
| 3622 | if (!basicnew) return NULL; |
| 3623 | r=PyObject_CallObject(basicnew, NULL); |
| 3624 | Py_DECREF(basicnew); |
| 3625 | if (r) return r; |
| 3626 | } |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 3627 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3628 | if ((r=PyObject_CallObject(cls, args))) return r; |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 3629 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3630 | err: |
| 3631 | { |
| 3632 | PyObject *tp, *v, *tb; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3633 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3634 | PyErr_Fetch(&tp, &v, &tb); |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 3635 | if ((r=PyTuple_Pack(3,v,cls,args))) { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3636 | Py_XDECREF(v); |
| 3637 | v=r; |
| 3638 | } |
| 3639 | PyErr_Restore(tp,v,tb); |
| 3640 | } |
| 3641 | return NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3642 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3643 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3644 | |
| 3645 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3646 | load_obj(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3647 | { |
| 3648 | PyObject *class, *tup, *obj=0; |
| 3649 | int i; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3650 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3651 | if ((i = marker(self)) < 0) return -1; |
| 3652 | if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1; |
| 3653 | PDATA_POP(self->stack, class); |
| 3654 | if (class) { |
| 3655 | obj = Instance_New(class, tup); |
| 3656 | Py_DECREF(class); |
| 3657 | } |
| 3658 | Py_DECREF(tup); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3659 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3660 | if (! obj) return -1; |
| 3661 | PDATA_PUSH(self->stack, obj, -1); |
| 3662 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3663 | } |
| 3664 | |
| 3665 | |
| 3666 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3667 | load_inst(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3668 | { |
| 3669 | PyObject *tup, *class=0, *obj=0, *module_name, *class_name; |
| 3670 | int i, len; |
| 3671 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3672 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3673 | if ((i = marker(self)) < 0) return -1; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3674 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3675 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3676 | if (len < 2) return bad_readline(); |
| 3677 | module_name = PyString_FromStringAndSize(s, len - 1); |
| 3678 | if (!module_name) return -1; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3679 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3680 | if ((len = self->readline_func(self, &s)) >= 0) { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3681 | if (len < 2) return bad_readline(); |
| 3682 | if ((class_name = PyString_FromStringAndSize(s, len - 1))) { |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3683 | class = find_class(module_name, class_name, |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3684 | self->find_class); |
| 3685 | Py_DECREF(class_name); |
| 3686 | } |
| 3687 | } |
| 3688 | Py_DECREF(module_name); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3689 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3690 | if (! class) return -1; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3691 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3692 | if ((tup=Pdata_popTuple(self->stack, i))) { |
| 3693 | obj = Instance_New(class, tup); |
| 3694 | Py_DECREF(tup); |
| 3695 | } |
| 3696 | Py_DECREF(class); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3697 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3698 | if (! obj) return -1; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3699 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3700 | PDATA_PUSH(self->stack, obj, -1); |
| 3701 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3702 | } |
| 3703 | |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 3704 | static int |
| 3705 | load_newobj(Unpicklerobject *self) |
| 3706 | { |
| 3707 | PyObject *args = NULL; |
| 3708 | PyObject *clsraw = NULL; |
| 3709 | PyTypeObject *cls; /* clsraw cast to its true type */ |
| 3710 | PyObject *obj; |
| 3711 | |
| 3712 | /* Stack is ... cls argtuple, and we want to call |
| 3713 | * cls.__new__(cls, *argtuple). |
| 3714 | */ |
| 3715 | PDATA_POP(self->stack, args); |
| 3716 | if (args == NULL) goto Fail; |
| 3717 | if (! PyTuple_Check(args)) { |
| 3718 | PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg " |
| 3719 | "tuple."); |
| 3720 | goto Fail; |
| 3721 | } |
| 3722 | |
| 3723 | PDATA_POP(self->stack, clsraw); |
| 3724 | cls = (PyTypeObject *)clsraw; |
| 3725 | if (cls == NULL) goto Fail; |
| 3726 | if (! PyType_Check(cls)) { |
| 3727 | PyErr_SetString(UnpicklingError, "NEWOBJ class argument " |
| 3728 | "isn't a type object"); |
| 3729 | goto Fail; |
| 3730 | } |
| 3731 | if (cls->tp_new == NULL) { |
| 3732 | PyErr_SetString(UnpicklingError, "NEWOBJ class argument " |
| 3733 | "has NULL tp_new"); |
| 3734 | goto Fail; |
| 3735 | } |
| 3736 | |
| 3737 | /* Call __new__. */ |
| 3738 | obj = cls->tp_new(cls, args, NULL); |
| 3739 | if (obj == NULL) goto Fail; |
| 3740 | |
| 3741 | Py_DECREF(args); |
| 3742 | Py_DECREF(clsraw); |
| 3743 | PDATA_PUSH(self->stack, obj, -1); |
| 3744 | return 0; |
| 3745 | |
| 3746 | Fail: |
| 3747 | Py_XDECREF(args); |
| 3748 | Py_XDECREF(clsraw); |
| 3749 | return -1; |
| 3750 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3751 | |
| 3752 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3753 | load_global(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3754 | { |
| 3755 | PyObject *class = 0, *module_name = 0, *class_name = 0; |
| 3756 | int len; |
| 3757 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3758 | |
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 | module_name = PyString_FromStringAndSize(s, len - 1); |
| 3762 | if (!module_name) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3763 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3764 | if ((len = self->readline_func(self, &s)) >= 0) { |
Tim Peters | 4e52ca8 | 2002-12-07 02:43:28 +0000 | [diff] [blame] | 3765 | if (len < 2) { |
| 3766 | Py_DECREF(module_name); |
| 3767 | return bad_readline(); |
| 3768 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3769 | if ((class_name = PyString_FromStringAndSize(s, len - 1))) { |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3770 | class = find_class(module_name, class_name, |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3771 | self->find_class); |
| 3772 | Py_DECREF(class_name); |
| 3773 | } |
| 3774 | } |
| 3775 | Py_DECREF(module_name); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3776 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3777 | if (! class) return -1; |
| 3778 | PDATA_PUSH(self->stack, class, -1); |
| 3779 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3780 | } |
| 3781 | |
| 3782 | |
| 3783 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3784 | load_persid(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3785 | { |
| 3786 | PyObject *pid = 0; |
| 3787 | int len; |
| 3788 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3789 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3790 | if (self->pers_func) { |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3791 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3792 | if (len < 2) return bad_readline(); |
| 3793 | |
| 3794 | pid = PyString_FromStringAndSize(s, len - 1); |
| 3795 | if (!pid) return -1; |
| 3796 | |
| 3797 | if (PyList_Check(self->pers_func)) { |
| 3798 | if (PyList_Append(self->pers_func, pid) < 0) { |
| 3799 | Py_DECREF(pid); |
| 3800 | return -1; |
| 3801 | } |
| 3802 | } |
| 3803 | else { |
| 3804 | ARG_TUP(self, pid); |
| 3805 | if (self->arg) { |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3806 | pid = PyObject_Call(self->pers_func, self->arg, |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3807 | NULL); |
| 3808 | FREE_ARG_TUP(self); |
| 3809 | } |
| 3810 | } |
| 3811 | |
| 3812 | if (! pid) return -1; |
| 3813 | |
| 3814 | PDATA_PUSH(self->stack, pid, -1); |
| 3815 | return 0; |
| 3816 | } |
| 3817 | else { |
| 3818 | PyErr_SetString(UnpicklingError, |
| 3819 | "A load persistent id instruction was encountered,\n" |
| 3820 | "but no persistent_load function was specified."); |
| 3821 | return -1; |
| 3822 | } |
| 3823 | } |
| 3824 | |
| 3825 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3826 | load_binpersid(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3827 | { |
| 3828 | PyObject *pid = 0; |
| 3829 | |
| 3830 | if (self->pers_func) { |
| 3831 | PDATA_POP(self->stack, pid); |
| 3832 | if (! pid) return -1; |
| 3833 | |
| 3834 | if (PyList_Check(self->pers_func)) { |
| 3835 | if (PyList_Append(self->pers_func, pid) < 0) { |
| 3836 | Py_DECREF(pid); |
| 3837 | return -1; |
| 3838 | } |
| 3839 | } |
| 3840 | else { |
| 3841 | ARG_TUP(self, pid); |
| 3842 | if (self->arg) { |
| 3843 | pid = PyObject_Call(self->pers_func, self->arg, |
| 3844 | NULL); |
| 3845 | FREE_ARG_TUP(self); |
| 3846 | } |
| 3847 | if (! pid) return -1; |
| 3848 | } |
| 3849 | |
| 3850 | PDATA_PUSH(self->stack, pid, -1); |
| 3851 | return 0; |
| 3852 | } |
| 3853 | else { |
| 3854 | PyErr_SetString(UnpicklingError, |
| 3855 | "A load persistent id instruction was encountered,\n" |
| 3856 | "but no persistent_load function was specified."); |
| 3857 | return -1; |
| 3858 | } |
| 3859 | } |
| 3860 | |
| 3861 | |
| 3862 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3863 | load_pop(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3864 | { |
| 3865 | int len; |
| 3866 | |
| 3867 | if (!( (len=self->stack->length) > 0 )) return stackUnderflow(); |
| 3868 | |
| 3869 | /* Note that we split the (pickle.py) stack into two stacks, |
| 3870 | an object stack and a mark stack. We have to be clever and |
| 3871 | pop the right one. We do this by looking at the top of the |
| 3872 | mark stack. |
| 3873 | */ |
| 3874 | |
| 3875 | if ((self->num_marks > 0) && |
| 3876 | (self->marks[self->num_marks - 1] == len)) |
| 3877 | self->num_marks--; |
| 3878 | else { |
| 3879 | len--; |
| 3880 | Py_DECREF(self->stack->data[len]); |
| 3881 | self->stack->length=len; |
| 3882 | } |
| 3883 | |
| 3884 | return 0; |
| 3885 | } |
| 3886 | |
| 3887 | |
| 3888 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3889 | load_pop_mark(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3890 | { |
| 3891 | int i; |
| 3892 | |
| 3893 | if ((i = marker(self)) < 0) |
| 3894 | return -1; |
| 3895 | |
| 3896 | Pdata_clear(self->stack, i); |
| 3897 | |
| 3898 | return 0; |
| 3899 | } |
| 3900 | |
| 3901 | |
| 3902 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3903 | load_dup(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3904 | { |
| 3905 | PyObject *last; |
| 3906 | int len; |
| 3907 | |
| 3908 | if ((len = self->stack->length) <= 0) return stackUnderflow(); |
| 3909 | last=self->stack->data[len-1]; |
| 3910 | Py_INCREF(last); |
| 3911 | PDATA_PUSH(self->stack, last, -1); |
| 3912 | return 0; |
| 3913 | } |
| 3914 | |
| 3915 | |
| 3916 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3917 | load_get(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3918 | { |
| 3919 | PyObject *py_str = 0, *value = 0; |
| 3920 | int len; |
| 3921 | char *s; |
| 3922 | int rc; |
| 3923 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3924 | if ((len = self->readline_func(self, &s)) < 0) return -1; |
Guido van Rossum | f9ffb03 | 1999-02-04 14:54:04 +0000 | [diff] [blame] | 3925 | if (len < 2) return bad_readline(); |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3926 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3927 | if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3928 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3929 | value = PyDict_GetItem(self->memo, py_str); |
| 3930 | if (! value) { |
| 3931 | PyErr_SetObject(BadPickleGet, py_str); |
| 3932 | rc = -1; |
Tim Peters | 92c8bb3 | 2003-02-13 23:00:26 +0000 | [diff] [blame] | 3933 | } |
| 3934 | else { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3935 | PDATA_APPEND(self->stack, value, -1); |
| 3936 | rc = 0; |
| 3937 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3938 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3939 | Py_DECREF(py_str); |
| 3940 | return rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3941 | } |
| 3942 | |
| 3943 | |
| 3944 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3945 | load_binget(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3946 | { |
| 3947 | PyObject *py_key = 0, *value = 0; |
| 3948 | unsigned char key; |
| 3949 | char *s; |
| 3950 | int rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3951 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3952 | if (self->read_func(self, &s, 1) < 0) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3953 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3954 | key = (unsigned char)s[0]; |
| 3955 | if (!( py_key = PyInt_FromLong((long)key))) return -1; |
Guido van Rossum | ea2b715 | 2000-05-09 18:14:50 +0000 | [diff] [blame] | 3956 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3957 | value = PyDict_GetItem(self->memo, py_key); |
| 3958 | if (! value) { |
| 3959 | PyErr_SetObject(BadPickleGet, py_key); |
| 3960 | rc = -1; |
Tim Peters | 92c8bb3 | 2003-02-13 23:00:26 +0000 | [diff] [blame] | 3961 | } |
| 3962 | else { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3963 | PDATA_APPEND(self->stack, value, -1); |
| 3964 | rc = 0; |
| 3965 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3966 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3967 | Py_DECREF(py_key); |
| 3968 | return rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3969 | } |
| 3970 | |
| 3971 | |
| 3972 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 3973 | load_long_binget(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3974 | { |
| 3975 | PyObject *py_key = 0, *value = 0; |
| 3976 | unsigned char c; |
| 3977 | char *s; |
| 3978 | long key; |
| 3979 | int rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3980 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 3981 | if (self->read_func(self, &s, 4) < 0) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3982 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3983 | c = (unsigned char)s[0]; |
| 3984 | key = (long)c; |
| 3985 | c = (unsigned char)s[1]; |
| 3986 | key |= (long)c << 8; |
| 3987 | c = (unsigned char)s[2]; |
| 3988 | key |= (long)c << 16; |
| 3989 | c = (unsigned char)s[3]; |
| 3990 | key |= (long)c << 24; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3991 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3992 | if (!( py_key = PyInt_FromLong((long)key))) return -1; |
| 3993 | |
| 3994 | value = PyDict_GetItem(self->memo, py_key); |
| 3995 | if (! value) { |
| 3996 | PyErr_SetObject(BadPickleGet, py_key); |
| 3997 | rc = -1; |
Tim Peters | 92c8bb3 | 2003-02-13 23:00:26 +0000 | [diff] [blame] | 3998 | } |
| 3999 | else { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4000 | PDATA_APPEND(self->stack, value, -1); |
| 4001 | rc = 0; |
| 4002 | } |
| 4003 | |
| 4004 | Py_DECREF(py_key); |
| 4005 | return rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4006 | } |
| 4007 | |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4008 | /* Push an object from the extension registry (EXT[124]). nbytes is |
| 4009 | * the number of bytes following the opcode, holding the index (code) value. |
| 4010 | */ |
| 4011 | static int |
| 4012 | load_extension(Unpicklerobject *self, int nbytes) |
| 4013 | { |
| 4014 | char *codebytes; /* the nbytes bytes after the opcode */ |
| 4015 | long code; /* calc_binint returns long */ |
| 4016 | PyObject *py_code; /* code as a Python int */ |
| 4017 | PyObject *obj; /* the object to push */ |
| 4018 | PyObject *pair; /* (module_name, class_name) */ |
| 4019 | PyObject *module_name, *class_name; |
| 4020 | |
| 4021 | assert(nbytes == 1 || nbytes == 2 || nbytes == 4); |
| 4022 | if (self->read_func(self, &codebytes, nbytes) < 0) return -1; |
| 4023 | code = calc_binint(codebytes, nbytes); |
| 4024 | if (code <= 0) { /* note that 0 is forbidden */ |
| 4025 | /* Corrupt or hostile pickle. */ |
| 4026 | PyErr_SetString(UnpicklingError, "EXT specifies code <= 0"); |
| 4027 | return -1; |
| 4028 | } |
| 4029 | |
| 4030 | /* Look for the code in the cache. */ |
| 4031 | py_code = PyInt_FromLong(code); |
| 4032 | if (py_code == NULL) return -1; |
| 4033 | obj = PyDict_GetItem(extension_cache, py_code); |
| 4034 | if (obj != NULL) { |
| 4035 | /* Bingo. */ |
| 4036 | Py_DECREF(py_code); |
| 4037 | PDATA_APPEND(self->stack, obj, -1); |
| 4038 | return 0; |
| 4039 | } |
| 4040 | |
| 4041 | /* Look up the (module_name, class_name) pair. */ |
| 4042 | pair = PyDict_GetItem(inverted_registry, py_code); |
| 4043 | if (pair == NULL) { |
| 4044 | Py_DECREF(py_code); |
| 4045 | PyErr_Format(PyExc_ValueError, "unregistered extension " |
| 4046 | "code %ld", code); |
| 4047 | return -1; |
| 4048 | } |
| 4049 | /* Since the extension registry is manipulable via Python code, |
Tim Peters | fa05ce3 | 2003-02-04 05:20:32 +0000 | [diff] [blame] | 4050 | * confirm that pair is really a 2-tuple of strings. |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4051 | */ |
| 4052 | if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 || |
| 4053 | !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) || |
| 4054 | !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) { |
| 4055 | Py_DECREF(py_code); |
| 4056 | PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] " |
| 4057 | "isn't a 2-tuple of strings", code); |
| 4058 | return -1; |
| 4059 | } |
| 4060 | /* Load the object. */ |
| 4061 | obj = find_class(module_name, class_name, self->find_class); |
| 4062 | if (obj == NULL) { |
| 4063 | Py_DECREF(py_code); |
| 4064 | return -1; |
| 4065 | } |
| 4066 | /* Cache code -> obj. */ |
| 4067 | code = PyDict_SetItem(extension_cache, py_code, obj); |
| 4068 | Py_DECREF(py_code); |
| 4069 | if (code < 0) { |
| 4070 | Py_DECREF(obj); |
| 4071 | return -1; |
| 4072 | } |
| 4073 | PDATA_PUSH(self->stack, obj, -1); |
| 4074 | return 0; |
| 4075 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4076 | |
| 4077 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4078 | load_put(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4079 | { |
| 4080 | PyObject *py_str = 0, *value = 0; |
| 4081 | int len, l; |
| 4082 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4083 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 4084 | if ((l = self->readline_func(self, &s)) < 0) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4085 | if (l < 2) return bad_readline(); |
| 4086 | if (!( len=self->stack->length )) return stackUnderflow(); |
| 4087 | if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1; |
| 4088 | value=self->stack->data[len-1]; |
| 4089 | l=PyDict_SetItem(self->memo, py_str, value); |
| 4090 | Py_DECREF(py_str); |
| 4091 | return l; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4092 | } |
| 4093 | |
| 4094 | |
| 4095 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4096 | load_binput(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4097 | { |
| 4098 | PyObject *py_key = 0, *value = 0; |
| 4099 | unsigned char key; |
| 4100 | char *s; |
| 4101 | int len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4102 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 4103 | if (self->read_func(self, &s, 1) < 0) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4104 | if (!( (len=self->stack->length) > 0 )) return stackUnderflow(); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4105 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4106 | key = (unsigned char)s[0]; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 4107 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4108 | if (!( py_key = PyInt_FromLong((long)key))) return -1; |
| 4109 | value=self->stack->data[len-1]; |
| 4110 | len=PyDict_SetItem(self->memo, py_key, value); |
| 4111 | Py_DECREF(py_key); |
| 4112 | return len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4113 | } |
| 4114 | |
| 4115 | |
| 4116 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4117 | load_long_binput(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4118 | { |
| 4119 | PyObject *py_key = 0, *value = 0; |
| 4120 | long key; |
| 4121 | unsigned char c; |
| 4122 | char *s; |
| 4123 | int len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4124 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 4125 | if (self->read_func(self, &s, 4) < 0) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4126 | if (!( len=self->stack->length )) return stackUnderflow(); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4127 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4128 | c = (unsigned char)s[0]; |
| 4129 | key = (long)c; |
| 4130 | c = (unsigned char)s[1]; |
| 4131 | key |= (long)c << 8; |
| 4132 | c = (unsigned char)s[2]; |
| 4133 | key |= (long)c << 16; |
| 4134 | c = (unsigned char)s[3]; |
| 4135 | key |= (long)c << 24; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4136 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4137 | if (!( py_key = PyInt_FromLong(key))) return -1; |
| 4138 | value=self->stack->data[len-1]; |
| 4139 | len=PyDict_SetItem(self->memo, py_key, value); |
| 4140 | Py_DECREF(py_key); |
| 4141 | return len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4142 | } |
| 4143 | |
| 4144 | |
| 4145 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4146 | do_append(Unpicklerobject *self, int x) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4147 | { |
| 4148 | PyObject *value = 0, *list = 0, *append_method = 0; |
| 4149 | int len, i; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4150 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4151 | len=self->stack->length; |
| 4152 | if (!( len >= x && x > 0 )) return stackUnderflow(); |
| 4153 | /* nothing to do */ |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4154 | if (len==x) return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4155 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4156 | list=self->stack->data[x-1]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4157 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4158 | if (PyList_Check(list)) { |
| 4159 | PyObject *slice; |
| 4160 | int list_len; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4161 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4162 | slice=Pdata_popList(self->stack, x); |
| 4163 | list_len = PyList_GET_SIZE(list); |
| 4164 | i=PyList_SetSlice(list, list_len, list_len, slice); |
| 4165 | Py_DECREF(slice); |
| 4166 | return i; |
| 4167 | } |
| 4168 | else { |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4169 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4170 | if (!( append_method = PyObject_GetAttr(list, append_str))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4171 | return -1; |
| 4172 | |
| 4173 | for (i = x; i < len; i++) { |
| 4174 | PyObject *junk; |
| 4175 | |
| 4176 | value=self->stack->data[i]; |
| 4177 | junk=0; |
| 4178 | ARG_TUP(self, value); |
| 4179 | if (self->arg) { |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4180 | junk = PyObject_Call(append_method, self->arg, |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4181 | NULL); |
| 4182 | FREE_ARG_TUP(self); |
| 4183 | } |
| 4184 | if (! junk) { |
| 4185 | Pdata_clear(self->stack, i+1); |
| 4186 | self->stack->length=x; |
| 4187 | Py_DECREF(append_method); |
| 4188 | return -1; |
| 4189 | } |
| 4190 | Py_DECREF(junk); |
| 4191 | } |
| 4192 | self->stack->length=x; |
| 4193 | Py_DECREF(append_method); |
| 4194 | } |
| 4195 | |
| 4196 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4197 | } |
| 4198 | |
| 4199 | |
| 4200 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4201 | load_append(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4202 | { |
| 4203 | return do_append(self, self->stack->length - 1); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4204 | } |
| 4205 | |
| 4206 | |
| 4207 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4208 | load_appends(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4209 | { |
| 4210 | return do_append(self, marker(self)); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4211 | } |
| 4212 | |
| 4213 | |
| 4214 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4215 | do_setitems(Unpicklerobject *self, int x) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4216 | { |
| 4217 | PyObject *value = 0, *key = 0, *dict = 0; |
| 4218 | int len, i, r=0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4219 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4220 | if (!( (len=self->stack->length) >= x |
| 4221 | && x > 0 )) return stackUnderflow(); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4222 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4223 | dict=self->stack->data[x-1]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4224 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4225 | for (i = x+1; i < len; i += 2) { |
| 4226 | key =self->stack->data[i-1]; |
| 4227 | value=self->stack->data[i ]; |
| 4228 | if (PyObject_SetItem(dict, key, value) < 0) { |
| 4229 | r=-1; |
| 4230 | break; |
| 4231 | } |
| 4232 | } |
| 4233 | |
| 4234 | Pdata_clear(self->stack, x); |
| 4235 | |
| 4236 | return r; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4237 | } |
| 4238 | |
| 4239 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4240 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4241 | load_setitem(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4242 | { |
| 4243 | return do_setitems(self, self->stack->length - 2); |
| 4244 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4245 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4246 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4247 | load_setitems(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4248 | { |
| 4249 | return do_setitems(self, marker(self)); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4250 | } |
| 4251 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4252 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4253 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4254 | load_build(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4255 | { |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 4256 | PyObject *state, *inst, *slotstate; |
| 4257 | PyObject *__setstate__; |
| 4258 | PyObject *d_key, *d_value; |
| 4259 | int i; |
| 4260 | int res = -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4261 | |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 4262 | /* Stack is ... instance, state. We want to leave instance at |
| 4263 | * the stack top, possibly mutated via instance.__setstate__(state). |
| 4264 | */ |
| 4265 | if (self->stack->length < 2) |
| 4266 | return stackUnderflow(); |
| 4267 | PDATA_POP(self->stack, state); |
| 4268 | if (state == NULL) |
| 4269 | return -1; |
| 4270 | inst = self->stack->data[self->stack->length - 1]; |
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 | __setstate__ = PyObject_GetAttr(inst, __setstate___str); |
| 4273 | if (__setstate__ != NULL) { |
| 4274 | PyObject *junk = NULL; |
| 4275 | |
| 4276 | /* The explicit __setstate__ is responsible for everything. */ |
| 4277 | ARG_TUP(self, state); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4278 | if (self->arg) { |
| 4279 | junk = PyObject_Call(__setstate__, self->arg, NULL); |
| 4280 | FREE_ARG_TUP(self); |
| 4281 | } |
| 4282 | Py_DECREF(__setstate__); |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 4283 | if (junk == NULL) |
| 4284 | return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4285 | Py_DECREF(junk); |
| 4286 | return 0; |
| 4287 | } |
Jeremy Hylton | f00368f | 2003-06-16 20:19:49 +0000 | [diff] [blame] | 4288 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 4289 | return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4290 | PyErr_Clear(); |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 4291 | |
| 4292 | /* A default __setstate__. First see whether state embeds a |
| 4293 | * slot state dict too (a proto 2 addition). |
| 4294 | */ |
| 4295 | if (PyTuple_Check(state) && PyTuple_Size(state) == 2) { |
| 4296 | PyObject *temp = state; |
| 4297 | state = PyTuple_GET_ITEM(temp, 0); |
| 4298 | slotstate = PyTuple_GET_ITEM(temp, 1); |
| 4299 | Py_INCREF(state); |
| 4300 | Py_INCREF(slotstate); |
| 4301 | Py_DECREF(temp); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4302 | } |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 4303 | else |
| 4304 | slotstate = NULL; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4305 | |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 4306 | /* Set inst.__dict__ from the state dict (if any). */ |
| 4307 | if (state != Py_None) { |
| 4308 | PyObject *dict; |
| 4309 | if (! PyDict_Check(state)) { |
| 4310 | PyErr_SetString(UnpicklingError, "state is not a " |
| 4311 | "dictionary"); |
| 4312 | goto finally; |
| 4313 | } |
| 4314 | dict = PyObject_GetAttr(inst, __dict___str); |
| 4315 | if (dict == NULL) |
| 4316 | goto finally; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4317 | |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 4318 | i = 0; |
| 4319 | while (PyDict_Next(state, &i, &d_key, &d_value)) { |
| 4320 | if (PyObject_SetItem(dict, d_key, d_value) < 0) |
| 4321 | goto finally; |
| 4322 | } |
| 4323 | Py_DECREF(dict); |
| 4324 | } |
| 4325 | |
| 4326 | /* Also set instance attributes from the slotstate dict (if any). */ |
| 4327 | if (slotstate != NULL) { |
| 4328 | if (! PyDict_Check(slotstate)) { |
| 4329 | PyErr_SetString(UnpicklingError, "slot state is not " |
| 4330 | "a dictionary"); |
| 4331 | goto finally; |
| 4332 | } |
| 4333 | i = 0; |
| 4334 | while (PyDict_Next(slotstate, &i, &d_key, &d_value)) { |
| 4335 | if (PyObject_SetAttr(inst, d_key, d_value) < 0) |
| 4336 | goto finally; |
| 4337 | } |
| 4338 | } |
| 4339 | res = 0; |
| 4340 | |
| 4341 | finally: |
| 4342 | Py_DECREF(state); |
| 4343 | Py_XDECREF(slotstate); |
| 4344 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4345 | } |
| 4346 | |
| 4347 | |
| 4348 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4349 | load_mark(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4350 | { |
| 4351 | int s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4352 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4353 | /* Note that we split the (pickle.py) stack into two stacks, an |
| 4354 | object stack and a mark stack. Here we push a mark onto the |
| 4355 | mark stack. |
| 4356 | */ |
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 ((self->num_marks + 1) >= self->marks_size) { |
| 4359 | s=self->marks_size+20; |
| 4360 | if (s <= self->num_marks) s=self->num_marks + 1; |
| 4361 | if (self->marks == NULL) |
| 4362 | self->marks=(int *)malloc(s * sizeof(int)); |
| 4363 | else |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4364 | self->marks=(int *)realloc(self->marks, |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4365 | s * sizeof(int)); |
| 4366 | if (! self->marks) { |
| 4367 | PyErr_NoMemory(); |
| 4368 | return -1; |
| 4369 | } |
| 4370 | self->marks_size = s; |
| 4371 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4372 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4373 | self->marks[self->num_marks++] = self->stack->length; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4374 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4375 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4376 | } |
| 4377 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4378 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4379 | load_reduce(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4380 | { |
| 4381 | PyObject *callable = 0, *arg_tup = 0, *ob = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4382 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4383 | PDATA_POP(self->stack, arg_tup); |
| 4384 | if (! arg_tup) return -1; |
| 4385 | PDATA_POP(self->stack, callable); |
| 4386 | if (callable) { |
| 4387 | ob = Instance_New(callable, arg_tup); |
| 4388 | Py_DECREF(callable); |
| 4389 | } |
| 4390 | Py_DECREF(arg_tup); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4391 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4392 | if (! ob) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4393 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4394 | PDATA_PUSH(self->stack, ob, -1); |
| 4395 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4396 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4397 | |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 4398 | /* Just raises an error if we don't know the protocol specified. PROTO |
| 4399 | * is the first opcode for protocols >= 2. |
| 4400 | */ |
| 4401 | static int |
| 4402 | load_proto(Unpicklerobject *self) |
| 4403 | { |
| 4404 | int i; |
| 4405 | char *protobyte; |
| 4406 | |
| 4407 | i = self->read_func(self, &protobyte, 1); |
| 4408 | if (i < 0) |
| 4409 | return -1; |
| 4410 | |
| 4411 | i = calc_binint(protobyte, 1); |
| 4412 | /* No point checking for < 0, since calc_binint returns an unsigned |
| 4413 | * int when chewing on 1 byte. |
| 4414 | */ |
| 4415 | assert(i >= 0); |
Tim Peters | 8587b3c | 2003-02-13 15:44:41 +0000 | [diff] [blame] | 4416 | if (i <= HIGHEST_PROTOCOL) |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 4417 | return 0; |
| 4418 | |
| 4419 | PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i); |
| 4420 | return -1; |
| 4421 | } |
| 4422 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4423 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4424 | load(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4425 | { |
| 4426 | PyObject *err = 0, *val = 0; |
| 4427 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4428 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4429 | self->num_marks = 0; |
| 4430 | if (self->stack->length) Pdata_clear(self->stack, 0); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4431 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4432 | while (1) { |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 4433 | if (self->read_func(self, &s, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4434 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4435 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4436 | switch (s[0]) { |
| 4437 | case NONE: |
| 4438 | if (load_none(self) < 0) |
| 4439 | break; |
| 4440 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4441 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4442 | case BININT: |
| 4443 | if (load_binint(self) < 0) |
| 4444 | break; |
| 4445 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4446 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4447 | case BININT1: |
| 4448 | if (load_binint1(self) < 0) |
| 4449 | break; |
| 4450 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4451 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4452 | case BININT2: |
| 4453 | if (load_binint2(self) < 0) |
| 4454 | break; |
| 4455 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4456 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4457 | case INT: |
| 4458 | if (load_int(self) < 0) |
| 4459 | break; |
| 4460 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4461 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4462 | case LONG: |
| 4463 | if (load_long(self) < 0) |
| 4464 | break; |
| 4465 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4466 | |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 4467 | case LONG1: |
| 4468 | if (load_counted_long(self, 1) < 0) |
| 4469 | break; |
| 4470 | continue; |
| 4471 | |
| 4472 | case LONG4: |
| 4473 | if (load_counted_long(self, 4) < 0) |
| 4474 | break; |
| 4475 | continue; |
| 4476 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4477 | case FLOAT: |
| 4478 | if (load_float(self) < 0) |
| 4479 | break; |
| 4480 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4481 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4482 | case BINFLOAT: |
| 4483 | if (load_binfloat(self) < 0) |
| 4484 | break; |
| 4485 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4486 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4487 | case BINSTRING: |
| 4488 | if (load_binstring(self) < 0) |
| 4489 | break; |
| 4490 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4491 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4492 | case SHORT_BINSTRING: |
| 4493 | if (load_short_binstring(self) < 0) |
| 4494 | break; |
| 4495 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4496 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4497 | case STRING: |
| 4498 | if (load_string(self) < 0) |
| 4499 | break; |
| 4500 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4501 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4502 | #ifdef Py_USING_UNICODE |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4503 | case UNICODE: |
| 4504 | if (load_unicode(self) < 0) |
| 4505 | break; |
| 4506 | continue; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 4507 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4508 | case BINUNICODE: |
| 4509 | if (load_binunicode(self) < 0) |
| 4510 | break; |
| 4511 | continue; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4512 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 4513 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4514 | case EMPTY_TUPLE: |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 4515 | if (load_counted_tuple(self, 0) < 0) |
| 4516 | break; |
| 4517 | continue; |
| 4518 | |
| 4519 | case TUPLE1: |
| 4520 | if (load_counted_tuple(self, 1) < 0) |
| 4521 | break; |
| 4522 | continue; |
| 4523 | |
| 4524 | case TUPLE2: |
| 4525 | if (load_counted_tuple(self, 2) < 0) |
| 4526 | break; |
| 4527 | continue; |
| 4528 | |
| 4529 | case TUPLE3: |
| 4530 | if (load_counted_tuple(self, 3) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4531 | break; |
| 4532 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4533 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4534 | case TUPLE: |
| 4535 | if (load_tuple(self) < 0) |
| 4536 | break; |
| 4537 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4538 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4539 | case EMPTY_LIST: |
| 4540 | if (load_empty_list(self) < 0) |
| 4541 | break; |
| 4542 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4543 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4544 | case LIST: |
| 4545 | if (load_list(self) < 0) |
| 4546 | break; |
| 4547 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4548 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4549 | case EMPTY_DICT: |
| 4550 | if (load_empty_dict(self) < 0) |
| 4551 | break; |
| 4552 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4553 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4554 | case DICT: |
| 4555 | if (load_dict(self) < 0) |
| 4556 | break; |
| 4557 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4558 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4559 | case OBJ: |
| 4560 | if (load_obj(self) < 0) |
| 4561 | break; |
| 4562 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4563 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4564 | case INST: |
| 4565 | if (load_inst(self) < 0) |
| 4566 | break; |
| 4567 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4568 | |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 4569 | case NEWOBJ: |
| 4570 | if (load_newobj(self) < 0) |
| 4571 | break; |
| 4572 | continue; |
| 4573 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4574 | case GLOBAL: |
| 4575 | if (load_global(self) < 0) |
| 4576 | break; |
| 4577 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4578 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4579 | case APPEND: |
| 4580 | if (load_append(self) < 0) |
| 4581 | break; |
| 4582 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4583 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4584 | case APPENDS: |
| 4585 | if (load_appends(self) < 0) |
| 4586 | break; |
| 4587 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4588 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4589 | case BUILD: |
| 4590 | if (load_build(self) < 0) |
| 4591 | break; |
| 4592 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4593 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4594 | case DUP: |
| 4595 | if (load_dup(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 BINGET: |
| 4600 | if (load_binget(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_BINGET: |
| 4605 | if (load_long_binget(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 GET: |
| 4610 | if (load_get(self) < 0) |
| 4611 | break; |
| 4612 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4613 | |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4614 | case EXT1: |
| 4615 | if (load_extension(self, 1) < 0) |
| 4616 | break; |
| 4617 | continue; |
| 4618 | |
| 4619 | case EXT2: |
| 4620 | if (load_extension(self, 2) < 0) |
| 4621 | break; |
| 4622 | continue; |
| 4623 | |
| 4624 | case EXT4: |
| 4625 | if (load_extension(self, 4) < 0) |
| 4626 | break; |
| 4627 | continue; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4628 | case MARK: |
| 4629 | if (load_mark(self) < 0) |
| 4630 | break; |
| 4631 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4632 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4633 | case BINPUT: |
| 4634 | if (load_binput(self) < 0) |
| 4635 | break; |
| 4636 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4637 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4638 | case LONG_BINPUT: |
| 4639 | if (load_long_binput(self) < 0) |
| 4640 | break; |
| 4641 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4642 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4643 | case PUT: |
| 4644 | if (load_put(self) < 0) |
| 4645 | break; |
| 4646 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4647 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4648 | case POP: |
| 4649 | if (load_pop(self) < 0) |
| 4650 | break; |
| 4651 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4652 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4653 | case POP_MARK: |
| 4654 | if (load_pop_mark(self) < 0) |
| 4655 | break; |
| 4656 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4657 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4658 | case SETITEM: |
| 4659 | if (load_setitem(self) < 0) |
| 4660 | break; |
| 4661 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4662 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4663 | case SETITEMS: |
| 4664 | if (load_setitems(self) < 0) |
| 4665 | break; |
| 4666 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4667 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4668 | case STOP: |
| 4669 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4670 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4671 | case PERSID: |
| 4672 | if (load_persid(self) < 0) |
| 4673 | break; |
| 4674 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4675 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4676 | case BINPERSID: |
| 4677 | if (load_binpersid(self) < 0) |
| 4678 | break; |
| 4679 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4680 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4681 | case REDUCE: |
| 4682 | if (load_reduce(self) < 0) |
| 4683 | break; |
| 4684 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4685 | |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 4686 | case PROTO: |
| 4687 | if (load_proto(self) < 0) |
| 4688 | break; |
| 4689 | continue; |
| 4690 | |
Tim Peters | 3c67d79 | 2003-02-02 17:59:11 +0000 | [diff] [blame] | 4691 | case NEWTRUE: |
| 4692 | if (load_bool(self, Py_True) < 0) |
| 4693 | break; |
| 4694 | continue; |
| 4695 | |
| 4696 | case NEWFALSE: |
| 4697 | if (load_bool(self, Py_False) < 0) |
| 4698 | break; |
| 4699 | continue; |
| 4700 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4701 | case '\0': |
| 4702 | /* end of file */ |
| 4703 | PyErr_SetNone(PyExc_EOFError); |
| 4704 | break; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4705 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4706 | default: |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4707 | cPickle_ErrFormat(UnpicklingError, |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4708 | "invalid load key, '%s'.", |
| 4709 | "c", s[0]); |
| 4710 | return NULL; |
| 4711 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4712 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4713 | break; |
| 4714 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4715 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4716 | if ((err = PyErr_Occurred())) { |
| 4717 | if (err == PyExc_EOFError) { |
| 4718 | PyErr_SetNone(PyExc_EOFError); |
| 4719 | } |
| 4720 | return NULL; |
| 4721 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4722 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4723 | PDATA_POP(self->stack, val); |
| 4724 | return val; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4725 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4726 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4727 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4728 | /* No-load functions to support noload, which is used to |
| 4729 | find persistent references. */ |
| 4730 | |
| 4731 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4732 | noload_obj(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4733 | { |
| 4734 | int i; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4735 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4736 | if ((i = marker(self)) < 0) return -1; |
| 4737 | return Pdata_clear(self->stack, i+1); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4738 | } |
| 4739 | |
| 4740 | |
| 4741 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4742 | noload_inst(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4743 | { |
| 4744 | int i; |
| 4745 | char *s; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4746 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4747 | if ((i = marker(self)) < 0) return -1; |
| 4748 | Pdata_clear(self->stack, i); |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 4749 | if (self->readline_func(self, &s) < 0) return -1; |
| 4750 | if (self->readline_func(self, &s) < 0) return -1; |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 4751 | PDATA_APPEND(self->stack, Py_None, -1); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4752 | return 0; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4753 | } |
| 4754 | |
| 4755 | static int |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 4756 | noload_newobj(Unpicklerobject *self) |
| 4757 | { |
| 4758 | PyObject *obj; |
| 4759 | |
| 4760 | PDATA_POP(self->stack, obj); /* pop argtuple */ |
| 4761 | if (obj == NULL) return -1; |
| 4762 | Py_DECREF(obj); |
| 4763 | |
| 4764 | PDATA_POP(self->stack, obj); /* pop cls */ |
| 4765 | if (obj == NULL) return -1; |
| 4766 | Py_DECREF(obj); |
| 4767 | |
| 4768 | PDATA_APPEND(self->stack, Py_None, -1); |
| 4769 | return 0; |
| 4770 | } |
| 4771 | |
| 4772 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4773 | noload_global(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4774 | { |
| 4775 | char *s; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4776 | |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 4777 | if (self->readline_func(self, &s) < 0) return -1; |
| 4778 | if (self->readline_func(self, &s) < 0) return -1; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4779 | PDATA_APPEND(self->stack, Py_None,-1); |
| 4780 | return 0; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4781 | } |
| 4782 | |
| 4783 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4784 | noload_reduce(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4785 | { |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4786 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4787 | if (self->stack->length < 2) return stackUnderflow(); |
| 4788 | Pdata_clear(self->stack, self->stack->length-2); |
| 4789 | PDATA_APPEND(self->stack, Py_None,-1); |
| 4790 | return 0; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4791 | } |
| 4792 | |
| 4793 | static int |
| 4794 | noload_build(Unpicklerobject *self) { |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4795 | |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 4796 | if (self->stack->length < 1) return stackUnderflow(); |
| 4797 | Pdata_clear(self->stack, self->stack->length-1); |
Guido van Rossum | 50f385c | 1998-12-04 18:48:44 +0000 | [diff] [blame] | 4798 | return 0; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4799 | } |
| 4800 | |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 4801 | static int |
| 4802 | noload_extension(Unpicklerobject *self, int nbytes) |
| 4803 | { |
| 4804 | char *codebytes; |
| 4805 | |
| 4806 | assert(nbytes == 1 || nbytes == 2 || nbytes == 4); |
| 4807 | if (self->read_func(self, &codebytes, nbytes) < 0) return -1; |
| 4808 | PDATA_APPEND(self->stack, Py_None, -1); |
| 4809 | return 0; |
| 4810 | } |
| 4811 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4812 | |
| 4813 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 4814 | noload(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4815 | { |
| 4816 | PyObject *err = 0, *val = 0; |
| 4817 | char *s; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4818 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4819 | self->num_marks = 0; |
| 4820 | Pdata_clear(self->stack, 0); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4821 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4822 | while (1) { |
Tim Peters | 0bc93f5 | 2003-02-02 18:29:33 +0000 | [diff] [blame] | 4823 | if (self->read_func(self, &s, 1) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4824 | break; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4825 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4826 | switch (s[0]) { |
| 4827 | case NONE: |
| 4828 | if (load_none(self) < 0) |
| 4829 | break; |
| 4830 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4831 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4832 | case BININT: |
| 4833 | if (load_binint(self) < 0) |
| 4834 | break; |
| 4835 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4836 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4837 | case BININT1: |
| 4838 | if (load_binint1(self) < 0) |
| 4839 | break; |
| 4840 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4841 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4842 | case BININT2: |
| 4843 | if (load_binint2(self) < 0) |
| 4844 | break; |
| 4845 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4846 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4847 | case INT: |
| 4848 | if (load_int(self) < 0) |
| 4849 | break; |
| 4850 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4851 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4852 | case LONG: |
| 4853 | if (load_long(self) < 0) |
| 4854 | break; |
| 4855 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4856 | |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 4857 | case LONG1: |
| 4858 | if (load_counted_long(self, 1) < 0) |
| 4859 | break; |
| 4860 | continue; |
| 4861 | |
| 4862 | case LONG4: |
| 4863 | if (load_counted_long(self, 4) < 0) |
| 4864 | break; |
| 4865 | continue; |
| 4866 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4867 | case FLOAT: |
| 4868 | if (load_float(self) < 0) |
| 4869 | break; |
| 4870 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4871 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4872 | case BINFLOAT: |
| 4873 | if (load_binfloat(self) < 0) |
| 4874 | break; |
| 4875 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4876 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4877 | case BINSTRING: |
| 4878 | if (load_binstring(self) < 0) |
| 4879 | break; |
| 4880 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4881 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4882 | case SHORT_BINSTRING: |
| 4883 | if (load_short_binstring(self) < 0) |
| 4884 | break; |
| 4885 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4886 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4887 | case STRING: |
| 4888 | if (load_string(self) < 0) |
| 4889 | break; |
| 4890 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4891 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4892 | #ifdef Py_USING_UNICODE |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4893 | case UNICODE: |
| 4894 | if (load_unicode(self) < 0) |
| 4895 | break; |
| 4896 | continue; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 4897 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4898 | case BINUNICODE: |
| 4899 | if (load_binunicode(self) < 0) |
| 4900 | break; |
| 4901 | continue; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4902 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 4903 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4904 | case EMPTY_TUPLE: |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 4905 | if (load_counted_tuple(self, 0) < 0) |
| 4906 | break; |
| 4907 | continue; |
| 4908 | |
| 4909 | case TUPLE1: |
| 4910 | if (load_counted_tuple(self, 1) < 0) |
| 4911 | break; |
| 4912 | continue; |
| 4913 | |
| 4914 | case TUPLE2: |
| 4915 | if (load_counted_tuple(self, 2) < 0) |
| 4916 | break; |
| 4917 | continue; |
| 4918 | |
| 4919 | case TUPLE3: |
| 4920 | if (load_counted_tuple(self, 3) < 0) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4921 | break; |
| 4922 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4923 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4924 | case TUPLE: |
| 4925 | if (load_tuple(self) < 0) |
| 4926 | break; |
| 4927 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4928 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4929 | case EMPTY_LIST: |
| 4930 | if (load_empty_list(self) < 0) |
| 4931 | break; |
| 4932 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4933 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4934 | case LIST: |
| 4935 | if (load_list(self) < 0) |
| 4936 | break; |
| 4937 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4938 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4939 | case EMPTY_DICT: |
| 4940 | if (load_empty_dict(self) < 0) |
| 4941 | break; |
| 4942 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4943 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4944 | case DICT: |
| 4945 | if (load_dict(self) < 0) |
| 4946 | break; |
| 4947 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4948 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4949 | case OBJ: |
| 4950 | if (noload_obj(self) < 0) |
| 4951 | break; |
| 4952 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4953 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4954 | case INST: |
| 4955 | if (noload_inst(self) < 0) |
| 4956 | break; |
| 4957 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4958 | |
Tim Peters | eab7db3 | 2003-02-13 18:24:14 +0000 | [diff] [blame] | 4959 | case NEWOBJ: |
| 4960 | if (noload_newobj(self) < 0) |
| 4961 | break; |
| 4962 | continue; |
| 4963 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4964 | case GLOBAL: |
| 4965 | if (noload_global(self) < 0) |
| 4966 | break; |
| 4967 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4968 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4969 | case APPEND: |
| 4970 | if (load_append(self) < 0) |
| 4971 | break; |
| 4972 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4973 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4974 | case APPENDS: |
| 4975 | if (load_appends(self) < 0) |
| 4976 | break; |
| 4977 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4978 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4979 | case BUILD: |
| 4980 | if (noload_build(self) < 0) |
| 4981 | break; |
| 4982 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4983 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4984 | case DUP: |
| 4985 | if (load_dup(self) < 0) |
| 4986 | break; |
| 4987 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4988 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4989 | case BINGET: |
| 4990 | if (load_binget(self) < 0) |
| 4991 | break; |
| 4992 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4993 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4994 | case LONG_BINGET: |
| 4995 | if (load_long_binget(self) < 0) |
| 4996 | break; |
| 4997 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4998 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4999 | case GET: |
| 5000 | if (load_get(self) < 0) |
| 5001 | break; |
| 5002 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5003 | |
Tim Peters | 2d62965 | 2003-02-04 05:06:17 +0000 | [diff] [blame] | 5004 | case EXT1: |
| 5005 | if (noload_extension(self, 1) < 0) |
| 5006 | break; |
| 5007 | continue; |
| 5008 | |
| 5009 | case EXT2: |
| 5010 | if (noload_extension(self, 2) < 0) |
| 5011 | break; |
| 5012 | continue; |
| 5013 | |
| 5014 | case EXT4: |
| 5015 | if (noload_extension(self, 4) < 0) |
| 5016 | break; |
| 5017 | continue; |
| 5018 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5019 | case MARK: |
| 5020 | if (load_mark(self) < 0) |
| 5021 | break; |
| 5022 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5023 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5024 | case BINPUT: |
| 5025 | if (load_binput(self) < 0) |
| 5026 | break; |
| 5027 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5028 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5029 | case LONG_BINPUT: |
| 5030 | if (load_long_binput(self) < 0) |
| 5031 | break; |
| 5032 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5033 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5034 | case PUT: |
| 5035 | if (load_put(self) < 0) |
| 5036 | break; |
| 5037 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5038 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5039 | case POP: |
| 5040 | if (load_pop(self) < 0) |
| 5041 | break; |
| 5042 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5043 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5044 | case POP_MARK: |
| 5045 | if (load_pop_mark(self) < 0) |
| 5046 | break; |
| 5047 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5048 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5049 | case SETITEM: |
| 5050 | if (load_setitem(self) < 0) |
| 5051 | break; |
| 5052 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5053 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5054 | case SETITEMS: |
| 5055 | if (load_setitems(self) < 0) |
| 5056 | break; |
| 5057 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5058 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5059 | case STOP: |
| 5060 | break; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5061 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5062 | case PERSID: |
| 5063 | if (load_persid(self) < 0) |
| 5064 | break; |
| 5065 | continue; |
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 | case BINPERSID: |
| 5068 | if (load_binpersid(self) < 0) |
| 5069 | break; |
| 5070 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5071 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5072 | case REDUCE: |
| 5073 | if (noload_reduce(self) < 0) |
| 5074 | break; |
| 5075 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5076 | |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 5077 | case PROTO: |
| 5078 | if (load_proto(self) < 0) |
| 5079 | break; |
| 5080 | continue; |
| 5081 | |
Tim Peters | 3c67d79 | 2003-02-02 17:59:11 +0000 | [diff] [blame] | 5082 | case NEWTRUE: |
| 5083 | if (load_bool(self, Py_True) < 0) |
| 5084 | break; |
| 5085 | continue; |
| 5086 | |
| 5087 | case NEWFALSE: |
| 5088 | if (load_bool(self, Py_False) < 0) |
| 5089 | break; |
| 5090 | continue; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5091 | default: |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5092 | cPickle_ErrFormat(UnpicklingError, |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5093 | "invalid load key, '%s'.", |
| 5094 | "c", s[0]); |
| 5095 | return NULL; |
| 5096 | } |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5097 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5098 | break; |
| 5099 | } |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5100 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5101 | if ((err = PyErr_Occurred())) { |
| 5102 | if (err == PyExc_EOFError) { |
| 5103 | PyErr_SetNone(PyExc_EOFError); |
| 5104 | } |
| 5105 | return NULL; |
| 5106 | } |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5107 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5108 | PDATA_POP(self->stack, val); |
| 5109 | return val; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5110 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5111 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5112 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5113 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5114 | Unpickler_load(Unpicklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5115 | { |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5116 | if (!( PyArg_ParseTuple(args, ":load"))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5117 | return NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5118 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5119 | return load(self); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5120 | } |
| 5121 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5122 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5123 | Unpickler_noload(Unpicklerobject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5124 | { |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5125 | if (!( PyArg_ParseTuple(args, ":noload"))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5126 | return NULL; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5127 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5128 | return noload(self); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5129 | } |
| 5130 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5131 | |
| 5132 | static struct PyMethodDef Unpickler_methods[] = { |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 5133 | {"load", (PyCFunction)Unpickler_load, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 5134 | PyDoc_STR("load() -- Load a pickle") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5135 | }, |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 5136 | {"noload", (PyCFunction)Unpickler_noload, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 5137 | PyDoc_STR( |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5138 | "noload() -- not load a pickle, but go through most of the motions\n" |
| 5139 | "\n" |
| 5140 | "This function can be used to read past a pickle without instantiating\n" |
| 5141 | "any objects or importing any modules. It can also be used to find all\n" |
| 5142 | "persistent references without instantiating any objects or importing\n" |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 5143 | "any modules.\n") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5144 | }, |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5145 | {NULL, NULL} /* sentinel */ |
| 5146 | }; |
| 5147 | |
| 5148 | |
| 5149 | static Unpicklerobject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5150 | newUnpicklerobject(PyObject *f) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5151 | { |
| 5152 | Unpicklerobject *self; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5153 | |
Jeremy Hylton | 7b5ce7f | 2003-04-09 21:25:30 +0000 | [diff] [blame] | 5154 | if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5155 | return NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5156 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5157 | self->file = NULL; |
| 5158 | self->arg = NULL; |
| 5159 | self->stack = (Pdata*)Pdata_New(); |
| 5160 | self->pers_func = NULL; |
| 5161 | self->last_string = NULL; |
| 5162 | self->marks = NULL; |
| 5163 | self->num_marks = 0; |
| 5164 | self->marks_size = 0; |
| 5165 | self->buf_size = 0; |
| 5166 | self->read = NULL; |
| 5167 | self->readline = NULL; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5168 | self->find_class = NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5169 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5170 | if (!( self->memo = PyDict_New())) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5171 | goto err; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5172 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5173 | Py_INCREF(f); |
| 5174 | self->file = f; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5175 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5176 | /* Set read, readline based on type of f */ |
| 5177 | if (PyFile_Check(f)) { |
| 5178 | self->fp = PyFile_AsFile(f); |
| 5179 | if (self->fp == NULL) { |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5180 | PyErr_SetString(PyExc_ValueError, |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5181 | "I/O operation on closed file"); |
| 5182 | goto err; |
| 5183 | } |
| 5184 | self->read_func = read_file; |
| 5185 | self->readline_func = readline_file; |
Guido van Rossum | c91fcaa | 1999-03-29 20:00:14 +0000 | [diff] [blame] | 5186 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5187 | else if (PycStringIO_InputCheck(f)) { |
| 5188 | self->fp = NULL; |
| 5189 | self->read_func = read_cStringIO; |
| 5190 | self->readline_func = readline_cStringIO; |
| 5191 | } |
| 5192 | else { |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5193 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5194 | self->fp = NULL; |
| 5195 | self->read_func = read_other; |
| 5196 | self->readline_func = readline_other; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5197 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5198 | if (!( (self->readline = PyObject_GetAttr(f, readline_str)) && |
| 5199 | (self->read = PyObject_GetAttr(f, read_str)))) { |
| 5200 | PyErr_Clear(); |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5201 | PyErr_SetString( PyExc_TypeError, |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5202 | "argument must have 'read' and " |
| 5203 | "'readline' attributes" ); |
| 5204 | goto err; |
| 5205 | } |
| 5206 | } |
Jeremy Hylton | 7b5ce7f | 2003-04-09 21:25:30 +0000 | [diff] [blame] | 5207 | PyObject_GC_Track(self); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5208 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5209 | return self; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5210 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5211 | err: |
| 5212 | Py_DECREF((PyObject *)self); |
| 5213 | return NULL; |
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 | get_Unpickler(PyObject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5219 | { |
| 5220 | PyObject *file; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5221 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5222 | if (!( PyArg_ParseTuple(args, "O:Unpickler", &file))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5223 | return NULL; |
| 5224 | return (PyObject *)newUnpicklerobject(file); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5225 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5226 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5227 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5228 | static void |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5229 | Unpickler_dealloc(Unpicklerobject *self) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5230 | { |
Jeremy Hylton | 7b5ce7f | 2003-04-09 21:25:30 +0000 | [diff] [blame] | 5231 | PyObject_GC_UnTrack((PyObject *)self); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5232 | Py_XDECREF(self->readline); |
| 5233 | Py_XDECREF(self->read); |
| 5234 | Py_XDECREF(self->file); |
| 5235 | Py_XDECREF(self->memo); |
| 5236 | Py_XDECREF(self->stack); |
| 5237 | Py_XDECREF(self->pers_func); |
| 5238 | Py_XDECREF(self->arg); |
| 5239 | Py_XDECREF(self->last_string); |
Jeremy Hylton | fff093f | 2003-07-11 19:42:49 +0000 | [diff] [blame] | 5240 | Py_XDECREF(self->find_class); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5241 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5242 | if (self->marks) { |
| 5243 | free(self->marks); |
| 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 | if (self->buf_size) { |
| 5247 | free(self->buf); |
| 5248 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5249 | |
Tim Peters | 3cfe754 | 2003-05-21 21:29:48 +0000 | [diff] [blame] | 5250 | self->ob_type->tp_free((PyObject *)self); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5251 | } |
| 5252 | |
Jeremy Hylton | 7b5ce7f | 2003-04-09 21:25:30 +0000 | [diff] [blame] | 5253 | static int |
| 5254 | Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg) |
| 5255 | { |
| 5256 | int err; |
| 5257 | |
| 5258 | #define VISIT(SLOT) \ |
| 5259 | if (SLOT) { \ |
| 5260 | err = visit((PyObject *)(SLOT), arg); \ |
| 5261 | if (err) \ |
| 5262 | return err; \ |
| 5263 | } |
| 5264 | VISIT(self->readline); |
| 5265 | VISIT(self->read); |
| 5266 | VISIT(self->file); |
| 5267 | VISIT(self->memo); |
| 5268 | VISIT(self->stack); |
| 5269 | VISIT(self->pers_func); |
| 5270 | VISIT(self->arg); |
| 5271 | VISIT(self->last_string); |
Jeremy Hylton | fff093f | 2003-07-11 19:42:49 +0000 | [diff] [blame] | 5272 | VISIT(self->find_class); |
Jeremy Hylton | 7b5ce7f | 2003-04-09 21:25:30 +0000 | [diff] [blame] | 5273 | #undef VISIT |
| 5274 | return 0; |
| 5275 | } |
| 5276 | |
| 5277 | static int |
| 5278 | Unpickler_clear(Unpicklerobject *self) |
| 5279 | { |
| 5280 | #define CLEAR(SLOT) Py_XDECREF(SLOT); SLOT = NULL |
| 5281 | CLEAR(self->readline); |
| 5282 | CLEAR(self->read); |
| 5283 | CLEAR(self->file); |
| 5284 | CLEAR(self->memo); |
| 5285 | CLEAR(self->stack); |
| 5286 | CLEAR(self->pers_func); |
| 5287 | CLEAR(self->arg); |
| 5288 | CLEAR(self->last_string); |
Jeremy Hylton | fff093f | 2003-07-11 19:42:49 +0000 | [diff] [blame] | 5289 | CLEAR(self->find_class); |
Jeremy Hylton | 7b5ce7f | 2003-04-09 21:25:30 +0000 | [diff] [blame] | 5290 | #undef CLEAR |
| 5291 | return 0; |
| 5292 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5293 | |
| 5294 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5295 | Unpickler_getattr(Unpicklerobject *self, char *name) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5296 | { |
| 5297 | if (!strcmp(name, "persistent_load")) { |
| 5298 | if (!self->pers_func) { |
| 5299 | PyErr_SetString(PyExc_AttributeError, name); |
| 5300 | return NULL; |
| 5301 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5302 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5303 | Py_INCREF(self->pers_func); |
| 5304 | return self->pers_func; |
| 5305 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5306 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5307 | if (!strcmp(name, "find_global")) { |
| 5308 | if (!self->find_class) { |
| 5309 | PyErr_SetString(PyExc_AttributeError, name); |
| 5310 | return NULL; |
| 5311 | } |
Guido van Rossum | 1b9e0aa | 1999-04-19 17:58:18 +0000 | [diff] [blame] | 5312 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5313 | Py_INCREF(self->find_class); |
| 5314 | return self->find_class; |
| 5315 | } |
Guido van Rossum | 1b9e0aa | 1999-04-19 17:58:18 +0000 | [diff] [blame] | 5316 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5317 | if (!strcmp(name, "memo")) { |
| 5318 | if (!self->memo) { |
| 5319 | PyErr_SetString(PyExc_AttributeError, name); |
| 5320 | return NULL; |
| 5321 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5322 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5323 | Py_INCREF(self->memo); |
| 5324 | return self->memo; |
| 5325 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5326 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5327 | if (!strcmp(name, "UnpicklingError")) { |
| 5328 | Py_INCREF(UnpicklingError); |
| 5329 | return UnpicklingError; |
| 5330 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5331 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5332 | return Py_FindMethod(Unpickler_methods, (PyObject *)self, name); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5333 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5334 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5335 | |
| 5336 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5337 | Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5338 | { |
Jeremy Hylton | ce616e4 | 1998-08-13 23:13:52 +0000 | [diff] [blame] | 5339 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5340 | if (!strcmp(name, "persistent_load")) { |
| 5341 | Py_XDECREF(self->pers_func); |
| 5342 | self->pers_func = value; |
| 5343 | Py_XINCREF(value); |
| 5344 | return 0; |
| 5345 | } |
Guido van Rossum | 1b9e0aa | 1999-04-19 17:58:18 +0000 | [diff] [blame] | 5346 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5347 | if (!strcmp(name, "find_global")) { |
| 5348 | Py_XDECREF(self->find_class); |
| 5349 | self->find_class = value; |
| 5350 | Py_XINCREF(value); |
| 5351 | return 0; |
| 5352 | } |
Guido van Rossum | 1b9e0aa | 1999-04-19 17:58:18 +0000 | [diff] [blame] | 5353 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5354 | if (! value) { |
| 5355 | PyErr_SetString(PyExc_TypeError, |
| 5356 | "attribute deletion is not supported"); |
| 5357 | return -1; |
| 5358 | } |
Jeremy Hylton | ce616e4 | 1998-08-13 23:13:52 +0000 | [diff] [blame] | 5359 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5360 | if (strcmp(name, "memo") == 0) { |
| 5361 | if (!PyDict_Check(value)) { |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5362 | PyErr_SetString(PyExc_TypeError, |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5363 | "memo must be a dictionary"); |
| 5364 | return -1; |
| 5365 | } |
| 5366 | Py_XDECREF(self->memo); |
| 5367 | self->memo = value; |
| 5368 | Py_INCREF(value); |
| 5369 | return 0; |
| 5370 | } |
Jeremy Hylton | ce616e4 | 1998-08-13 23:13:52 +0000 | [diff] [blame] | 5371 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5372 | PyErr_SetString(PyExc_AttributeError, name); |
| 5373 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5374 | } |
| 5375 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5376 | /* --------------------------------------------------------------------------- |
| 5377 | * Module-level functions. |
| 5378 | */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5379 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5380 | /* dump(obj, file, proto=0). */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5381 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5382 | cpm_dump(PyObject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5383 | { |
| 5384 | PyObject *ob, *file, *res = NULL; |
| 5385 | Picklerobject *pickler = 0; |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5386 | int proto = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5387 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5388 | if (!( PyArg_ParseTuple(args, "OO|i", &ob, &file, &proto))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5389 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5390 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5391 | if (!( pickler = newPicklerobject(file, proto))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5392 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5393 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5394 | if (dump(pickler, ob) < 0) |
| 5395 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5396 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5397 | Py_INCREF(Py_None); |
| 5398 | res = Py_None; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5399 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5400 | finally: |
| 5401 | Py_XDECREF(pickler); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5402 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5403 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5404 | } |
| 5405 | |
| 5406 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5407 | /* dumps(obj, proto=0). */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5408 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5409 | cpm_dumps(PyObject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5410 | { |
| 5411 | PyObject *ob, *file = 0, *res = NULL; |
| 5412 | Picklerobject *pickler = 0; |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5413 | int proto = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5414 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5415 | if (!( PyArg_ParseTuple(args, "O|i:dumps", &ob, &proto))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5416 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5417 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5418 | if (!( file = PycStringIO->NewOutput(128))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5419 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5420 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5421 | if (!( pickler = newPicklerobject(file, proto))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5422 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5423 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5424 | if (dump(pickler, ob) < 0) |
| 5425 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5426 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5427 | res = PycStringIO->cgetvalue(file); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5428 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5429 | finally: |
| 5430 | Py_XDECREF(pickler); |
| 5431 | Py_XDECREF(file); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5432 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5433 | return res; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5434 | } |
| 5435 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5436 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5437 | /* load(fileobj). */ |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5438 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5439 | cpm_load(PyObject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5440 | { |
| 5441 | Unpicklerobject *unpickler = 0; |
| 5442 | PyObject *ob, *res = NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5443 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5444 | if (!( PyArg_ParseTuple(args, "O:load", &ob))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5445 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5446 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5447 | if (!( unpickler = newUnpicklerobject(ob))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5448 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5449 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5450 | res = load(unpickler); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5451 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5452 | finally: |
| 5453 | Py_XDECREF(unpickler); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5454 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5455 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5456 | } |
| 5457 | |
| 5458 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5459 | /* loads(string) */ |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5460 | static PyObject * |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5461 | cpm_loads(PyObject *self, PyObject *args) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5462 | { |
| 5463 | PyObject *ob, *file = 0, *res = NULL; |
| 5464 | Unpicklerobject *unpickler = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5465 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5466 | if (!( PyArg_ParseTuple(args, "S:loads", &ob))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5467 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5468 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5469 | if (!( file = PycStringIO->NewInput(ob))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5470 | goto finally; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5471 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5472 | if (!( unpickler = newUnpicklerobject(file))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5473 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5474 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5475 | res = load(unpickler); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5476 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5477 | finally: |
| 5478 | Py_XDECREF(file); |
| 5479 | Py_XDECREF(unpickler); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5480 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5481 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5482 | } |
| 5483 | |
| 5484 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5485 | PyDoc_STRVAR(Unpicklertype__doc__, |
| 5486 | "Objects that know how to unpickle"); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5487 | |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 5488 | static PyTypeObject Unpicklertype = { |
| 5489 | PyObject_HEAD_INIT(NULL) |
Jeremy Hylton | 7b5ce7f | 2003-04-09 21:25:30 +0000 | [diff] [blame] | 5490 | 0, /*ob_size*/ |
| 5491 | "cPickle.Unpickler", /*tp_name*/ |
| 5492 | sizeof(Unpicklerobject), /*tp_basicsize*/ |
| 5493 | 0, |
| 5494 | (destructor)Unpickler_dealloc, /* tp_dealloc */ |
| 5495 | 0, /* tp_print */ |
| 5496 | (getattrfunc)Unpickler_getattr, /* tp_getattr */ |
| 5497 | (setattrfunc)Unpickler_setattr, /* tp_setattr */ |
| 5498 | 0, /* tp_compare */ |
| 5499 | 0, /* tp_repr */ |
| 5500 | 0, /* tp_as_number */ |
| 5501 | 0, /* tp_as_sequence */ |
| 5502 | 0, /* tp_as_mapping */ |
| 5503 | 0, /* tp_hash */ |
| 5504 | 0, /* tp_call */ |
| 5505 | 0, /* tp_str */ |
| 5506 | 0, /* tp_getattro */ |
| 5507 | 0, /* tp_setattro */ |
| 5508 | 0, /* tp_as_buffer */ |
| 5509 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 5510 | Unpicklertype__doc__, /* tp_doc */ |
| 5511 | (traverseproc)Unpickler_traverse, /* tp_traverse */ |
| 5512 | (inquiry)Unpickler_clear, /* tp_clear */ |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 5513 | }; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5514 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5515 | static struct PyMethodDef cPickle_methods[] = { |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 5516 | {"dump", (PyCFunction)cpm_dump, METH_VARARGS, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5517 | PyDoc_STR("dump(object, file, proto=0) -- " |
| 5518 | "Write an object in pickle format to the given file.\n" |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5519 | "\n" |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5520 | "See the Pickler docstring for the meaning of optional argument proto.") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5521 | }, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5522 | |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 5523 | {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5524 | PyDoc_STR("dumps(object, proto=0) -- " |
| 5525 | "Return a string containing an object in pickle format.\n" |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5526 | "\n" |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5527 | "See the Pickler docstring for the meaning of optional argument proto.") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5528 | }, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5529 | |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 5530 | {"load", (PyCFunction)cpm_load, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 5531 | PyDoc_STR("load(file) -- Load a pickle from the given file")}, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5532 | |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 5533 | {"loads", (PyCFunction)cpm_loads, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 5534 | PyDoc_STR("loads(string) -- Load a pickle from the given string")}, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5535 | |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 5536 | {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5537 | PyDoc_STR("Pickler(file, proto=0) -- Create a pickler.\n" |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5538 | "\n" |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5539 | "This takes a file-like object for writing a pickle data stream.\n" |
| 5540 | "The optional proto argument tells the pickler to use the given\n" |
| 5541 | "protocol; supported protocols are 0, 1, 2. The default\n" |
| 5542 | "protocol is 0, to be backwards compatible. (Protocol 0 is the\n" |
| 5543 | "only protocol that can be written to a file opened in text\n" |
| 5544 | "mode and read back successfully. When using a protocol higher\n" |
| 5545 | "than 0, make sure the file is opened in binary mode, both when\n" |
| 5546 | "pickling and unpickling.)\n" |
| 5547 | "\n" |
| 5548 | "Protocol 1 is more efficient than protocol 0; protocol 2 is\n" |
| 5549 | "more efficient than protocol 1.\n" |
| 5550 | "\n" |
| 5551 | "Specifying a negative protocol version selects the highest\n" |
| 5552 | "protocol version supported. The higher the protocol used, the\n" |
| 5553 | "more recent the version of Python needed to read the pickle\n" |
| 5554 | "produced.\n" |
| 5555 | "\n" |
| 5556 | "The file parameter must have a write() method that accepts a single\n" |
| 5557 | "string argument. It can thus be an open file object, a StringIO\n" |
| 5558 | "object, or any other custom object that meets this interface.\n") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5559 | }, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5560 | |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 5561 | {"Unpickler", (PyCFunction)get_Unpickler, METH_VARARGS, |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 5562 | PyDoc_STR("Unpickler(file) -- Create an unpickler.")}, |
| 5563 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5564 | { NULL, NULL } |
| 5565 | }; |
| 5566 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5567 | static int |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5568 | init_stuff(PyObject *module_dict) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5569 | { |
| 5570 | PyObject *copy_reg, *t, *r; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5571 | |
Martin v. Löwis | 43c9d8a | 2002-04-01 12:34:33 +0000 | [diff] [blame] | 5572 | #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] | 5573 | |
Tim Peters | 3cfe754 | 2003-05-21 21:29:48 +0000 | [diff] [blame] | 5574 | if (PyType_Ready(&Unpicklertype) < 0) |
| 5575 | return -1; |
| 5576 | if (PyType_Ready(&Picklertype) < 0) |
| 5577 | return -1; |
| 5578 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5579 | INIT_STR(__class__); |
| 5580 | INIT_STR(__getinitargs__); |
| 5581 | INIT_STR(__dict__); |
| 5582 | INIT_STR(__getstate__); |
| 5583 | INIT_STR(__setstate__); |
| 5584 | INIT_STR(__name__); |
| 5585 | INIT_STR(__main__); |
| 5586 | INIT_STR(__reduce__); |
Guido van Rossum | b289b87 | 2003-02-19 01:45:13 +0000 | [diff] [blame] | 5587 | INIT_STR(__reduce_ex__); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5588 | INIT_STR(write); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5589 | INIT_STR(append); |
| 5590 | INIT_STR(read); |
| 5591 | INIT_STR(readline); |
| 5592 | INIT_STR(copy_reg); |
| 5593 | INIT_STR(dispatch_table); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5594 | INIT_STR(__basicnew__); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5595 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5596 | if (!( copy_reg = PyImport_ImportModule("copy_reg"))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5597 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5598 | |
Tim Peters | 1f1b2d2 | 2003-02-01 02:16:37 +0000 | [diff] [blame] | 5599 | /* This is special because we want to use a different |
| 5600 | one in restricted mode. */ |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5601 | dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str); |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 5602 | if (!dispatch_table) return -1; |
| 5603 | |
| 5604 | extension_registry = PyObject_GetAttrString(copy_reg, |
Guido van Rossum | d4b920c | 2003-02-04 01:54:49 +0000 | [diff] [blame] | 5605 | "_extension_registry"); |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 5606 | if (!extension_registry) return -1; |
| 5607 | |
| 5608 | inverted_registry = PyObject_GetAttrString(copy_reg, |
Guido van Rossum | d4b920c | 2003-02-04 01:54:49 +0000 | [diff] [blame] | 5609 | "_inverted_registry"); |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 5610 | if (!inverted_registry) return -1; |
| 5611 | |
| 5612 | extension_cache = PyObject_GetAttrString(copy_reg, |
Guido van Rossum | d4b920c | 2003-02-04 01:54:49 +0000 | [diff] [blame] | 5613 | "_extension_cache"); |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 5614 | if (!extension_cache) return -1; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5615 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5616 | Py_DECREF(copy_reg); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5617 | |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 5618 | if (!(empty_tuple = PyTuple_New(0))) |
| 5619 | return -1; |
| 5620 | |
| 5621 | two_tuple = PyTuple_New(2); |
| 5622 | if (two_tuple == NULL) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5623 | return -1; |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 5624 | /* We use this temp container with no regard to refcounts, or to |
| 5625 | * keeping containees alive. Exempt from GC, because we don't |
| 5626 | * want anything looking at two_tuple() by magic. |
| 5627 | */ |
| 5628 | PyObject_GC_UnTrack(two_tuple); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 5629 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5630 | /* Ugh */ |
| 5631 | if (!( t=PyImport_ImportModule("__builtin__"))) return -1; |
| 5632 | if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0) |
| 5633 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5634 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5635 | if (!( t=PyDict_New())) return -1; |
| 5636 | if (!( r=PyRun_String( |
| 5637 | "def __init__(self, *args): self.args=args\n\n" |
| 5638 | "def __str__(self):\n" |
| 5639 | " return self.args and ('%s' % self.args[0]) or '(what)'\n", |
| 5640 | Py_file_input, |
| 5641 | module_dict, t) )) return -1; |
| 5642 | Py_DECREF(r); |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5643 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5644 | PickleError = PyErr_NewException("cPickle.PickleError", NULL, t); |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5645 | if (!PickleError) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5646 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5647 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5648 | Py_DECREF(t); |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5649 | |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5650 | PicklingError = PyErr_NewException("cPickle.PicklingError", |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5651 | PickleError, NULL); |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5652 | if (!PicklingError) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5653 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5654 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5655 | if (!( t=PyDict_New())) return -1; |
| 5656 | if (!( r=PyRun_String( |
| 5657 | "def __init__(self, *args): self.args=args\n\n" |
| 5658 | "def __str__(self):\n" |
| 5659 | " a=self.args\n" |
| 5660 | " a=a and type(a[0]) or '(what)'\n" |
| 5661 | " return 'Cannot pickle %s objects' % a\n" |
| 5662 | , Py_file_input, |
| 5663 | module_dict, t) )) return -1; |
| 5664 | Py_DECREF(r); |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 5665 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5666 | if (!( UnpickleableError = PyErr_NewException( |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5667 | "cPickle.UnpickleableError", PicklingError, t))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5668 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5669 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5670 | Py_DECREF(t); |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5671 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5672 | if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError", |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5673 | PickleError, NULL))) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5674 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5675 | |
Martin v. Löwis | 658009a | 2002-09-16 17:26:24 +0000 | [diff] [blame] | 5676 | if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet", |
| 5677 | UnpicklingError, NULL))) |
| 5678 | return -1; |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5679 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5680 | if (PyDict_SetItemString(module_dict, "PickleError", |
| 5681 | PickleError) < 0) |
| 5682 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5683 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5684 | if (PyDict_SetItemString(module_dict, "PicklingError", |
| 5685 | PicklingError) < 0) |
| 5686 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5687 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5688 | if (PyDict_SetItemString(module_dict, "UnpicklingError", |
| 5689 | UnpicklingError) < 0) |
| 5690 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5691 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5692 | if (PyDict_SetItemString(module_dict, "UnpickleableError", |
| 5693 | UnpickleableError) < 0) |
| 5694 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 5695 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5696 | if (PyDict_SetItemString(module_dict, "BadPickleGet", |
| 5697 | BadPickleGet) < 0) |
| 5698 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 5699 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5700 | PycString_IMPORT; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 5701 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5702 | return 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5703 | } |
| 5704 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 5705 | #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */ |
| 5706 | #define PyMODINIT_FUNC void |
Guido van Rossum | f9ffb03 | 1999-02-04 14:54:04 +0000 | [diff] [blame] | 5707 | #endif |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 5708 | PyMODINIT_FUNC |
Tim Peters | cba30e2 | 2003-02-01 06:24:36 +0000 | [diff] [blame] | 5709 | initcPickle(void) |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5710 | { |
| 5711 | PyObject *m, *d, *di, *v, *k; |
| 5712 | int i; |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 5713 | char *rev = "1.71"; /* XXX when does this change? */ |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5714 | PyObject *format_version; |
| 5715 | PyObject *compatible_formats; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5716 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5717 | Picklertype.ob_type = &PyType_Type; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5718 | Unpicklertype.ob_type = &PyType_Type; |
| 5719 | PdataType.ob_type = &PyType_Type; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5720 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5721 | /* Initialize some pieces. We need to do this before module creation, |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 5722 | * so we're forced to use a temporary dictionary. :( |
| 5723 | */ |
| 5724 | di = PyDict_New(); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5725 | if (!di) return; |
| 5726 | if (init_stuff(di) < 0) return; |
Guido van Rossum | ebba420 | 2000-09-07 14:35:37 +0000 | [diff] [blame] | 5727 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5728 | /* Create the module and add the functions */ |
| 5729 | m = Py_InitModule4("cPickle", cPickle_methods, |
| 5730 | cPickle_module_documentation, |
| 5731 | (PyObject*)NULL,PYTHON_API_VERSION); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5732 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5733 | /* Add some symbolic constants to the module */ |
| 5734 | d = PyModule_GetDict(m); |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 5735 | v = PyString_FromString(rev); |
| 5736 | PyDict_SetItemString(d, "__version__", v); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5737 | Py_XDECREF(v); |
Barry Warsaw | 93d29b6 | 1997-01-14 17:45:08 +0000 | [diff] [blame] | 5738 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5739 | /* Copy data from di. Waaa. */ |
| 5740 | for (i=0; PyDict_Next(di, &i, &k, &v); ) { |
| 5741 | if (PyObject_SetItem(d, k, v) < 0) { |
| 5742 | Py_DECREF(di); |
| 5743 | return; |
| 5744 | } |
| 5745 | } |
| 5746 | Py_DECREF(di); |
Guido van Rossum | ebba420 | 2000-09-07 14:35:37 +0000 | [diff] [blame] | 5747 | |
Tim Peters | 8587b3c | 2003-02-13 15:44:41 +0000 | [diff] [blame] | 5748 | i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL); |
| 5749 | if (i < 0) |
| 5750 | return; |
| 5751 | |
Tim Peters | 5b7da39 | 2003-02-04 00:21:07 +0000 | [diff] [blame] | 5752 | /* These are purely informational; no code uses them. */ |
| 5753 | /* File format version we write. */ |
| 5754 | format_version = PyString_FromString("2.0"); |
| 5755 | /* Format versions we can read. */ |
| 5756 | compatible_formats = Py_BuildValue("[sssss]", |
| 5757 | "1.0", /* Original protocol 0 */ |
| 5758 | "1.1", /* Protocol 0 + INST */ |
| 5759 | "1.2", /* Original protocol 1 */ |
| 5760 | "1.3", /* Protocol 1 + BINFLOAT */ |
Tim Peters | 0dd23aa | 2003-02-04 00:30:46 +0000 | [diff] [blame] | 5761 | "2.0"); /* Original protocol 2 */ |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 5762 | PyDict_SetItemString(d, "format_version", format_version); |
| 5763 | PyDict_SetItemString(d, "compatible_formats", compatible_formats); |
| 5764 | Py_XDECREF(format_version); |
| 5765 | Py_XDECREF(compatible_formats); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 5766 | } |