Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | #include "structmember.h" |
| 3 | |
| 4 | PyDoc_STRVAR(pickle_module_doc, |
| 5 | "Optimized C implementation for the Python pickle module."); |
| 6 | |
| 7 | /* Bump this when new opcodes are added to the pickle protocol. */ |
| 8 | enum { |
| 9 | HIGHEST_PROTOCOL = 3, |
| 10 | DEFAULT_PROTOCOL = 3 |
| 11 | }; |
| 12 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 13 | /* Pickle opcodes. These must be kept updated with pickle.py. |
| 14 | Extensive docs are in pickletools.py. */ |
| 15 | enum opcode { |
| 16 | MARK = '(', |
| 17 | STOP = '.', |
| 18 | POP = '0', |
| 19 | POP_MARK = '1', |
| 20 | DUP = '2', |
| 21 | FLOAT = 'F', |
| 22 | INT = 'I', |
| 23 | BININT = 'J', |
| 24 | BININT1 = 'K', |
| 25 | LONG = 'L', |
| 26 | BININT2 = 'M', |
| 27 | NONE = 'N', |
| 28 | PERSID = 'P', |
| 29 | BINPERSID = 'Q', |
| 30 | REDUCE = 'R', |
| 31 | STRING = 'S', |
| 32 | BINSTRING = 'T', |
| 33 | SHORT_BINSTRING = 'U', |
| 34 | UNICODE = 'V', |
| 35 | BINUNICODE = 'X', |
| 36 | APPEND = 'a', |
| 37 | BUILD = 'b', |
| 38 | GLOBAL = 'c', |
| 39 | DICT = 'd', |
| 40 | EMPTY_DICT = '}', |
| 41 | APPENDS = 'e', |
| 42 | GET = 'g', |
| 43 | BINGET = 'h', |
| 44 | INST = 'i', |
| 45 | LONG_BINGET = 'j', |
| 46 | LIST = 'l', |
| 47 | EMPTY_LIST = ']', |
| 48 | OBJ = 'o', |
| 49 | PUT = 'p', |
| 50 | BINPUT = 'q', |
| 51 | LONG_BINPUT = 'r', |
| 52 | SETITEM = 's', |
| 53 | TUPLE = 't', |
| 54 | EMPTY_TUPLE = ')', |
| 55 | SETITEMS = 'u', |
| 56 | BINFLOAT = 'G', |
| 57 | |
| 58 | /* Protocol 2. */ |
| 59 | PROTO = '\x80', |
| 60 | NEWOBJ = '\x81', |
| 61 | EXT1 = '\x82', |
| 62 | EXT2 = '\x83', |
| 63 | EXT4 = '\x84', |
| 64 | TUPLE1 = '\x85', |
| 65 | TUPLE2 = '\x86', |
| 66 | TUPLE3 = '\x87', |
| 67 | NEWTRUE = '\x88', |
| 68 | NEWFALSE = '\x89', |
| 69 | LONG1 = '\x8a', |
| 70 | LONG4 = '\x8b', |
| 71 | |
| 72 | /* Protocol 3 (Python 3.x) */ |
| 73 | BINBYTES = 'B', |
Victor Stinner | 132ef6c | 2010-11-09 09:39:41 +0000 | [diff] [blame] | 74 | SHORT_BINBYTES = 'C' |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | /* These aren't opcodes -- they're ways to pickle bools before protocol 2 |
| 78 | * so that unpicklers written before bools were introduced unpickle them |
| 79 | * as ints, but unpicklers after can recognize that bools were intended. |
| 80 | * Note that protocol 2 added direct ways to pickle bools. |
| 81 | */ |
| 82 | #undef TRUE |
| 83 | #define TRUE "I01\n" |
| 84 | #undef FALSE |
| 85 | #define FALSE "I00\n" |
| 86 | |
| 87 | enum { |
| 88 | /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements |
| 89 | batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will |
| 90 | break if this gets out of synch with pickle.py, but it's unclear that would |
| 91 | help anything either. */ |
| 92 | BATCHSIZE = 1000, |
| 93 | |
| 94 | /* Nesting limit until Pickler, when running in "fast mode", starts |
| 95 | checking for self-referential data-structures. */ |
| 96 | FAST_NESTING_LIMIT = 50, |
| 97 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 98 | /* Initial size of the write buffer of Pickler. */ |
| 99 | WRITE_BUF_SIZE = 4096, |
| 100 | |
| 101 | /* Maximum size of the write buffer of Pickler when pickling to a |
| 102 | stream. This is ignored for in-memory pickling. */ |
| 103 | MAX_WRITE_BUF_SIZE = 64 * 1024, |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 104 | |
| 105 | /* Prefetch size when unpickling (disabled on unpeekable streams) */ |
Victor Stinner | 132ef6c | 2010-11-09 09:39:41 +0000 | [diff] [blame] | 106 | PREFETCH = 8192 * 16 |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | /* Exception classes for pickle. These should override the ones defined in |
| 110 | pickle.py, when the C-optimized Pickler and Unpickler are used. */ |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 111 | static PyObject *PickleError = NULL; |
| 112 | static PyObject *PicklingError = NULL; |
| 113 | static PyObject *UnpicklingError = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 114 | |
| 115 | /* copyreg.dispatch_table, {type_object: pickling_function} */ |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 116 | static PyObject *dispatch_table = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 117 | /* For EXT[124] opcodes. */ |
| 118 | /* copyreg._extension_registry, {(module_name, function_name): code} */ |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 119 | static PyObject *extension_registry = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 120 | /* copyreg._inverted_registry, {code: (module_name, function_name)} */ |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 121 | static PyObject *inverted_registry = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 122 | /* copyreg._extension_cache, {code: object} */ |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 123 | static PyObject *extension_cache = NULL; |
| 124 | |
| 125 | /* _compat_pickle.NAME_MAPPING, {(oldmodule, oldname): (newmodule, newname)} */ |
| 126 | static PyObject *name_mapping_2to3 = NULL; |
| 127 | /* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */ |
| 128 | static PyObject *import_mapping_2to3 = NULL; |
| 129 | /* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */ |
| 130 | static PyObject *name_mapping_3to2 = NULL; |
| 131 | static PyObject *import_mapping_3to2 = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 132 | |
| 133 | /* XXX: Are these really nescessary? */ |
| 134 | /* As the name says, an empty tuple. */ |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 135 | static PyObject *empty_tuple = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 136 | /* For looking up name pairs in copyreg._extension_registry. */ |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 137 | static PyObject *two_tuple = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 138 | |
| 139 | static int |
| 140 | stack_underflow(void) |
| 141 | { |
| 142 | PyErr_SetString(UnpicklingError, "unpickling stack underflow"); |
| 143 | return -1; |
| 144 | } |
| 145 | |
| 146 | /* Internal data type used as the unpickling stack. */ |
| 147 | typedef struct { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 148 | PyObject_VAR_HEAD |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 149 | PyObject **data; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 150 | Py_ssize_t allocated; /* number of slots in data allocated */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 151 | } Pdata; |
| 152 | |
| 153 | static void |
| 154 | Pdata_dealloc(Pdata *self) |
| 155 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 156 | Py_ssize_t i = Py_SIZE(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 157 | while (--i >= 0) { |
| 158 | Py_DECREF(self->data[i]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 159 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 160 | PyMem_FREE(self->data); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 161 | PyObject_Del(self); |
| 162 | } |
| 163 | |
| 164 | static PyTypeObject Pdata_Type = { |
| 165 | PyVarObject_HEAD_INIT(NULL, 0) |
| 166 | "_pickle.Pdata", /*tp_name*/ |
| 167 | sizeof(Pdata), /*tp_basicsize*/ |
| 168 | 0, /*tp_itemsize*/ |
| 169 | (destructor)Pdata_dealloc, /*tp_dealloc*/ |
| 170 | }; |
| 171 | |
| 172 | static PyObject * |
| 173 | Pdata_New(void) |
| 174 | { |
| 175 | Pdata *self; |
| 176 | |
| 177 | if (!(self = PyObject_New(Pdata, &Pdata_Type))) |
| 178 | return NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 179 | Py_SIZE(self) = 0; |
| 180 | self->allocated = 8; |
| 181 | self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 182 | if (self->data) |
| 183 | return (PyObject *)self; |
| 184 | Py_DECREF(self); |
| 185 | return PyErr_NoMemory(); |
| 186 | } |
| 187 | |
| 188 | |
| 189 | /* Retain only the initial clearto items. If clearto >= the current |
| 190 | * number of items, this is a (non-erroneous) NOP. |
| 191 | */ |
| 192 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 193 | Pdata_clear(Pdata *self, Py_ssize_t clearto) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 194 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 195 | Py_ssize_t i = Py_SIZE(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 196 | |
| 197 | if (clearto < 0) |
| 198 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 199 | if (clearto >= i) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 200 | return 0; |
| 201 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 202 | while (--i >= clearto) { |
| 203 | Py_CLEAR(self->data[i]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 204 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 205 | Py_SIZE(self) = clearto; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | static int |
| 210 | Pdata_grow(Pdata *self) |
| 211 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 212 | PyObject **data = self->data; |
| 213 | Py_ssize_t allocated = self->allocated; |
| 214 | Py_ssize_t new_allocated; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 215 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 216 | new_allocated = (allocated >> 3) + 6; |
| 217 | /* check for integer overflow */ |
| 218 | if (new_allocated > PY_SSIZE_T_MAX - allocated) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 219 | goto nomemory; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 220 | new_allocated += allocated; |
| 221 | if (new_allocated > (PY_SSIZE_T_MAX / sizeof(PyObject *))) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 222 | goto nomemory; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 223 | data = PyMem_REALLOC(data, new_allocated * sizeof(PyObject *)); |
| 224 | if (data == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 225 | goto nomemory; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 226 | |
| 227 | self->data = data; |
| 228 | self->allocated = new_allocated; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 229 | return 0; |
| 230 | |
| 231 | nomemory: |
| 232 | PyErr_NoMemory(); |
| 233 | return -1; |
| 234 | } |
| 235 | |
| 236 | /* D is a Pdata*. Pop the topmost element and store it into V, which |
| 237 | * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError |
| 238 | * is raised and V is set to NULL. |
| 239 | */ |
| 240 | static PyObject * |
| 241 | Pdata_pop(Pdata *self) |
| 242 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 243 | if (Py_SIZE(self) == 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 244 | PyErr_SetString(UnpicklingError, "bad pickle data"); |
| 245 | return NULL; |
| 246 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 247 | return self->data[--Py_SIZE(self)]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 248 | } |
| 249 | #define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0) |
| 250 | |
| 251 | static int |
| 252 | Pdata_push(Pdata *self, PyObject *obj) |
| 253 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 254 | if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 255 | return -1; |
| 256 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 257 | self->data[Py_SIZE(self)++] = obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | /* Push an object on stack, transferring its ownership to the stack. */ |
| 262 | #define PDATA_PUSH(D, O, ER) do { \ |
| 263 | if (Pdata_push((D), (O)) < 0) return (ER); } while(0) |
| 264 | |
| 265 | /* Push an object on stack, adding a new reference to the object. */ |
| 266 | #define PDATA_APPEND(D, O, ER) do { \ |
| 267 | Py_INCREF((O)); \ |
| 268 | if (Pdata_push((D), (O)) < 0) return (ER); } while(0) |
| 269 | |
| 270 | static PyObject * |
| 271 | Pdata_poptuple(Pdata *self, Py_ssize_t start) |
| 272 | { |
| 273 | PyObject *tuple; |
| 274 | Py_ssize_t len, i, j; |
| 275 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 276 | len = Py_SIZE(self) - start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 277 | tuple = PyTuple_New(len); |
| 278 | if (tuple == NULL) |
| 279 | return NULL; |
| 280 | for (i = start, j = 0; j < len; i++, j++) |
| 281 | PyTuple_SET_ITEM(tuple, j, self->data[i]); |
| 282 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 283 | Py_SIZE(self) = start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 284 | return tuple; |
| 285 | } |
| 286 | |
| 287 | static PyObject * |
| 288 | Pdata_poplist(Pdata *self, Py_ssize_t start) |
| 289 | { |
| 290 | PyObject *list; |
| 291 | Py_ssize_t len, i, j; |
| 292 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 293 | len = Py_SIZE(self) - start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 294 | list = PyList_New(len); |
| 295 | if (list == NULL) |
| 296 | return NULL; |
| 297 | for (i = start, j = 0; j < len; i++, j++) |
| 298 | PyList_SET_ITEM(list, j, self->data[i]); |
| 299 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 300 | Py_SIZE(self) = start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 301 | return list; |
| 302 | } |
| 303 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 304 | typedef struct { |
| 305 | PyObject *me_key; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 306 | Py_ssize_t me_value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 307 | } PyMemoEntry; |
| 308 | |
| 309 | typedef struct { |
| 310 | Py_ssize_t mt_mask; |
| 311 | Py_ssize_t mt_used; |
| 312 | Py_ssize_t mt_allocated; |
| 313 | PyMemoEntry *mt_table; |
| 314 | } PyMemoTable; |
| 315 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 316 | typedef struct PicklerObject { |
| 317 | PyObject_HEAD |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 318 | PyMemoTable *memo; /* Memo table, keep track of the seen |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 319 | objects to support self-referential objects |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 320 | pickling. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 321 | PyObject *pers_func; /* persistent_id() method, can be NULL */ |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 322 | PyObject *dispatch_table; /* private dispatch_table, can be NULL */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 323 | PyObject *arg; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 324 | |
| 325 | PyObject *write; /* write() method of the output stream. */ |
| 326 | PyObject *output_buffer; /* Write into a local bytearray buffer before |
| 327 | flushing to the stream. */ |
| 328 | Py_ssize_t output_len; /* Length of output_buffer. */ |
| 329 | Py_ssize_t max_output_len; /* Allocation size of output_buffer. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 330 | int proto; /* Pickle protocol number, >= 0 */ |
| 331 | int bin; /* Boolean, true if proto > 0 */ |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 332 | Py_ssize_t buf_size; /* Size of the current buffered pickle data */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 333 | int fast; /* Enable fast mode if set to a true value. |
| 334 | The fast mode disable the usage of memo, |
| 335 | therefore speeding the pickling process by |
| 336 | not generating superfluous PUT opcodes. It |
| 337 | should not be used if with self-referential |
| 338 | objects. */ |
| 339 | int fast_nesting; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 340 | int fix_imports; /* Indicate whether Pickler should fix |
| 341 | the name of globals for Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 342 | PyObject *fast_memo; |
| 343 | } PicklerObject; |
| 344 | |
| 345 | typedef struct UnpicklerObject { |
| 346 | PyObject_HEAD |
| 347 | Pdata *stack; /* Pickle data stack, store unpickled objects. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 348 | |
| 349 | /* The unpickler memo is just an array of PyObject *s. Using a dict |
| 350 | is unnecessary, since the keys are contiguous ints. */ |
| 351 | PyObject **memo; |
| 352 | Py_ssize_t memo_size; |
| 353 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 354 | PyObject *arg; |
| 355 | PyObject *pers_func; /* persistent_load() method, can be NULL. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 356 | |
| 357 | Py_buffer buffer; |
| 358 | char *input_buffer; |
| 359 | char *input_line; |
| 360 | Py_ssize_t input_len; |
| 361 | Py_ssize_t next_read_idx; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 362 | Py_ssize_t prefetched_idx; /* index of first prefetched byte */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 363 | PyObject *read; /* read() method of the input stream. */ |
| 364 | PyObject *readline; /* readline() method of the input stream. */ |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 365 | PyObject *peek; /* peek() method of the input stream, or NULL */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 366 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 367 | char *encoding; /* Name of the encoding to be used for |
| 368 | decoding strings pickled using Python |
| 369 | 2.x. The default value is "ASCII" */ |
| 370 | char *errors; /* Name of errors handling scheme to used when |
| 371 | decoding strings. The default value is |
| 372 | "strict". */ |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 373 | Py_ssize_t *marks; /* Mark stack, used for unpickling container |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 374 | objects. */ |
| 375 | Py_ssize_t num_marks; /* Number of marks in the mark stack. */ |
| 376 | Py_ssize_t marks_size; /* Current allocated size of the mark stack. */ |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 377 | int proto; /* Protocol of the pickle loaded. */ |
| 378 | int fix_imports; /* Indicate whether Unpickler should fix |
| 379 | the name of globals pickled by Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 380 | } UnpicklerObject; |
| 381 | |
| 382 | /* Forward declarations */ |
| 383 | static int save(PicklerObject *, PyObject *, int); |
| 384 | static int save_reduce(PicklerObject *, PyObject *, PyObject *); |
| 385 | static PyTypeObject Pickler_Type; |
| 386 | static PyTypeObject Unpickler_Type; |
| 387 | |
| 388 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 389 | /************************************************************************* |
| 390 | A custom hashtable mapping void* to longs. This is used by the pickler for |
| 391 | memoization. Using a custom hashtable rather than PyDict allows us to skip |
| 392 | a bunch of unnecessary object creation. This makes a huge performance |
| 393 | difference. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 394 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 395 | #define MT_MINSIZE 8 |
| 396 | #define PERTURB_SHIFT 5 |
| 397 | |
| 398 | |
| 399 | static PyMemoTable * |
| 400 | PyMemoTable_New(void) |
| 401 | { |
| 402 | PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable)); |
| 403 | if (memo == NULL) { |
| 404 | PyErr_NoMemory(); |
| 405 | return NULL; |
| 406 | } |
| 407 | |
| 408 | memo->mt_used = 0; |
| 409 | memo->mt_allocated = MT_MINSIZE; |
| 410 | memo->mt_mask = MT_MINSIZE - 1; |
| 411 | memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry)); |
| 412 | if (memo->mt_table == NULL) { |
| 413 | PyMem_FREE(memo); |
| 414 | PyErr_NoMemory(); |
| 415 | return NULL; |
| 416 | } |
| 417 | memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry)); |
| 418 | |
| 419 | return memo; |
| 420 | } |
| 421 | |
| 422 | static PyMemoTable * |
| 423 | PyMemoTable_Copy(PyMemoTable *self) |
| 424 | { |
| 425 | Py_ssize_t i; |
| 426 | PyMemoTable *new = PyMemoTable_New(); |
| 427 | if (new == NULL) |
| 428 | return NULL; |
| 429 | |
| 430 | new->mt_used = self->mt_used; |
| 431 | new->mt_allocated = self->mt_allocated; |
| 432 | new->mt_mask = self->mt_mask; |
| 433 | /* The table we get from _New() is probably smaller than we wanted. |
| 434 | Free it and allocate one that's the right size. */ |
| 435 | PyMem_FREE(new->mt_table); |
| 436 | new->mt_table = PyMem_MALLOC(self->mt_allocated * sizeof(PyMemoEntry)); |
| 437 | if (new->mt_table == NULL) { |
| 438 | PyMem_FREE(new); |
| 439 | return NULL; |
| 440 | } |
| 441 | for (i = 0; i < self->mt_allocated; i++) { |
| 442 | Py_XINCREF(self->mt_table[i].me_key); |
| 443 | } |
| 444 | memcpy(new->mt_table, self->mt_table, |
| 445 | sizeof(PyMemoEntry) * self->mt_allocated); |
| 446 | |
| 447 | return new; |
| 448 | } |
| 449 | |
| 450 | static Py_ssize_t |
| 451 | PyMemoTable_Size(PyMemoTable *self) |
| 452 | { |
| 453 | return self->mt_used; |
| 454 | } |
| 455 | |
| 456 | static int |
| 457 | PyMemoTable_Clear(PyMemoTable *self) |
| 458 | { |
| 459 | Py_ssize_t i = self->mt_allocated; |
| 460 | |
| 461 | while (--i >= 0) { |
| 462 | Py_XDECREF(self->mt_table[i].me_key); |
| 463 | } |
| 464 | self->mt_used = 0; |
| 465 | memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry)); |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | static void |
| 470 | PyMemoTable_Del(PyMemoTable *self) |
| 471 | { |
| 472 | if (self == NULL) |
| 473 | return; |
| 474 | PyMemoTable_Clear(self); |
| 475 | |
| 476 | PyMem_FREE(self->mt_table); |
| 477 | PyMem_FREE(self); |
| 478 | } |
| 479 | |
| 480 | /* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup() |
| 481 | can be considerably simpler than dictobject.c's lookdict(). */ |
| 482 | static PyMemoEntry * |
| 483 | _PyMemoTable_Lookup(PyMemoTable *self, PyObject *key) |
| 484 | { |
| 485 | size_t i; |
| 486 | size_t perturb; |
| 487 | size_t mask = (size_t)self->mt_mask; |
| 488 | PyMemoEntry *table = self->mt_table; |
| 489 | PyMemoEntry *entry; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 490 | Py_hash_t hash = (Py_hash_t)key >> 3; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 491 | |
| 492 | i = hash & mask; |
| 493 | entry = &table[i]; |
| 494 | if (entry->me_key == NULL || entry->me_key == key) |
| 495 | return entry; |
| 496 | |
| 497 | for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
| 498 | i = (i << 2) + i + perturb + 1; |
| 499 | entry = &table[i & mask]; |
| 500 | if (entry->me_key == NULL || entry->me_key == key) |
| 501 | return entry; |
| 502 | } |
| 503 | assert(0); /* Never reached */ |
| 504 | return NULL; |
| 505 | } |
| 506 | |
| 507 | /* Returns -1 on failure, 0 on success. */ |
| 508 | static int |
| 509 | _PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size) |
| 510 | { |
| 511 | PyMemoEntry *oldtable = NULL; |
| 512 | PyMemoEntry *oldentry, *newentry; |
| 513 | Py_ssize_t new_size = MT_MINSIZE; |
| 514 | Py_ssize_t to_process; |
| 515 | |
| 516 | assert(min_size > 0); |
| 517 | |
| 518 | /* Find the smallest valid table size >= min_size. */ |
| 519 | while (new_size < min_size && new_size > 0) |
| 520 | new_size <<= 1; |
| 521 | if (new_size <= 0) { |
| 522 | PyErr_NoMemory(); |
| 523 | return -1; |
| 524 | } |
| 525 | /* new_size needs to be a power of two. */ |
| 526 | assert((new_size & (new_size - 1)) == 0); |
| 527 | |
| 528 | /* Allocate new table. */ |
| 529 | oldtable = self->mt_table; |
| 530 | self->mt_table = PyMem_MALLOC(new_size * sizeof(PyMemoEntry)); |
| 531 | if (self->mt_table == NULL) { |
| 532 | PyMem_FREE(oldtable); |
| 533 | PyErr_NoMemory(); |
| 534 | return -1; |
| 535 | } |
| 536 | self->mt_allocated = new_size; |
| 537 | self->mt_mask = new_size - 1; |
| 538 | memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size); |
| 539 | |
| 540 | /* Copy entries from the old table. */ |
| 541 | to_process = self->mt_used; |
| 542 | for (oldentry = oldtable; to_process > 0; oldentry++) { |
| 543 | if (oldentry->me_key != NULL) { |
| 544 | to_process--; |
| 545 | /* newentry is a pointer to a chunk of the new |
| 546 | mt_table, so we're setting the key:value pair |
| 547 | in-place. */ |
| 548 | newentry = _PyMemoTable_Lookup(self, oldentry->me_key); |
| 549 | newentry->me_key = oldentry->me_key; |
| 550 | newentry->me_value = oldentry->me_value; |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | /* Deallocate the old table. */ |
| 555 | PyMem_FREE(oldtable); |
| 556 | return 0; |
| 557 | } |
| 558 | |
| 559 | /* Returns NULL on failure, a pointer to the value otherwise. */ |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 560 | static Py_ssize_t * |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 561 | PyMemoTable_Get(PyMemoTable *self, PyObject *key) |
| 562 | { |
| 563 | PyMemoEntry *entry = _PyMemoTable_Lookup(self, key); |
| 564 | if (entry->me_key == NULL) |
| 565 | return NULL; |
| 566 | return &entry->me_value; |
| 567 | } |
| 568 | |
| 569 | /* Returns -1 on failure, 0 on success. */ |
| 570 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 571 | PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 572 | { |
| 573 | PyMemoEntry *entry; |
| 574 | |
| 575 | assert(key != NULL); |
| 576 | |
| 577 | entry = _PyMemoTable_Lookup(self, key); |
| 578 | if (entry->me_key != NULL) { |
| 579 | entry->me_value = value; |
| 580 | return 0; |
| 581 | } |
| 582 | Py_INCREF(key); |
| 583 | entry->me_key = key; |
| 584 | entry->me_value = value; |
| 585 | self->mt_used++; |
| 586 | |
| 587 | /* If we added a key, we can safely resize. Otherwise just return! |
| 588 | * If used >= 2/3 size, adjust size. Normally, this quaduples the size. |
| 589 | * |
| 590 | * Quadrupling the size improves average table sparseness |
| 591 | * (reducing collisions) at the cost of some memory. It also halves |
| 592 | * the number of expensive resize operations in a growing memo table. |
| 593 | * |
| 594 | * Very large memo tables (over 50K items) use doubling instead. |
| 595 | * This may help applications with severe memory constraints. |
| 596 | */ |
| 597 | if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2)) |
| 598 | return 0; |
| 599 | return _PyMemoTable_ResizeTable(self, |
| 600 | (self->mt_used > 50000 ? 2 : 4) * self->mt_used); |
| 601 | } |
| 602 | |
| 603 | #undef MT_MINSIZE |
| 604 | #undef PERTURB_SHIFT |
| 605 | |
| 606 | /*************************************************************************/ |
| 607 | |
| 608 | /* Helpers for creating the argument tuple passed to functions. This has the |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 609 | performance advantage of calling PyTuple_New() only once. |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 610 | |
| 611 | XXX(avassalotti): Inline directly in _Pickler_FastCall() and |
| 612 | _Unpickler_FastCall(). */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 613 | #define ARG_TUP(self, obj) do { \ |
| 614 | if ((self)->arg || ((self)->arg=PyTuple_New(1))) { \ |
| 615 | Py_XDECREF(PyTuple_GET_ITEM((self)->arg, 0)); \ |
| 616 | PyTuple_SET_ITEM((self)->arg, 0, (obj)); \ |
| 617 | } \ |
| 618 | else { \ |
| 619 | Py_DECREF((obj)); \ |
| 620 | } \ |
| 621 | } while (0) |
| 622 | |
| 623 | #define FREE_ARG_TUP(self) do { \ |
| 624 | if ((self)->arg->ob_refcnt > 1) \ |
| 625 | Py_CLEAR((self)->arg); \ |
| 626 | } while (0) |
| 627 | |
| 628 | /* A temporary cleaner API for fast single argument function call. |
| 629 | |
| 630 | XXX: Does caching the argument tuple provides any real performance benefits? |
| 631 | |
| 632 | A quick benchmark, on a 2.0GHz Athlon64 3200+ running Linux 2.6.24 with |
| 633 | glibc 2.7, tells me that it takes roughly 20,000,000 PyTuple_New(1) calls |
| 634 | when the tuple is retrieved from the freelist (i.e, call PyTuple_New() then |
| 635 | immediately DECREF it) and 1,200,000 calls when allocating brand new tuples |
| 636 | (i.e, call PyTuple_New() and store the returned value in an array), to save |
| 637 | one second (wall clock time). Either ways, the loading time a pickle stream |
| 638 | large enough to generate this number of calls would be massively |
| 639 | overwhelmed by other factors, like I/O throughput, the GC traversal and |
| 640 | object allocation overhead. So, I really doubt these functions provide any |
| 641 | real benefits. |
| 642 | |
| 643 | On the other hand, oprofile reports that pickle spends a lot of time in |
| 644 | these functions. But, that is probably more related to the function call |
| 645 | overhead, than the argument tuple allocation. |
| 646 | |
| 647 | XXX: And, what is the reference behavior of these? Steal, borrow? At first |
| 648 | glance, it seems to steal the reference of 'arg' and borrow the reference |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 649 | of 'func'. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 650 | static PyObject * |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 651 | _Pickler_FastCall(PicklerObject *self, PyObject *func, PyObject *arg) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 652 | { |
| 653 | PyObject *result = NULL; |
| 654 | |
| 655 | ARG_TUP(self, arg); |
| 656 | if (self->arg) { |
| 657 | result = PyObject_Call(func, self->arg, NULL); |
| 658 | FREE_ARG_TUP(self); |
| 659 | } |
| 660 | return result; |
| 661 | } |
| 662 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 663 | static int |
| 664 | _Pickler_ClearBuffer(PicklerObject *self) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 665 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 666 | Py_CLEAR(self->output_buffer); |
| 667 | self->output_buffer = |
| 668 | PyBytes_FromStringAndSize(NULL, self->max_output_len); |
| 669 | if (self->output_buffer == NULL) |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 670 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 671 | self->output_len = 0; |
| 672 | return 0; |
| 673 | } |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 674 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 675 | static PyObject * |
| 676 | _Pickler_GetString(PicklerObject *self) |
| 677 | { |
| 678 | PyObject *output_buffer = self->output_buffer; |
| 679 | |
| 680 | assert(self->output_buffer != NULL); |
| 681 | self->output_buffer = NULL; |
| 682 | /* Resize down to exact size */ |
| 683 | if (_PyBytes_Resize(&output_buffer, self->output_len) < 0) |
| 684 | return NULL; |
| 685 | return output_buffer; |
| 686 | } |
| 687 | |
| 688 | static int |
| 689 | _Pickler_FlushToFile(PicklerObject *self) |
| 690 | { |
| 691 | PyObject *output, *result; |
| 692 | |
| 693 | assert(self->write != NULL); |
| 694 | |
| 695 | output = _Pickler_GetString(self); |
| 696 | if (output == NULL) |
| 697 | return -1; |
| 698 | |
| 699 | result = _Pickler_FastCall(self, self->write, output); |
| 700 | Py_XDECREF(result); |
| 701 | return (result == NULL) ? -1 : 0; |
| 702 | } |
| 703 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 704 | static Py_ssize_t |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 705 | _Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t n) |
| 706 | { |
| 707 | Py_ssize_t i, required; |
| 708 | char *buffer; |
| 709 | |
| 710 | assert(s != NULL); |
| 711 | |
| 712 | required = self->output_len + n; |
| 713 | if (required > self->max_output_len) { |
| 714 | if (self->write != NULL && required > MAX_WRITE_BUF_SIZE) { |
| 715 | /* XXX This reallocates a new buffer every time, which is a bit |
| 716 | wasteful. */ |
| 717 | if (_Pickler_FlushToFile(self) < 0) |
| 718 | return -1; |
| 719 | if (_Pickler_ClearBuffer(self) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 720 | return -1; |
| 721 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 722 | if (self->write != NULL && n > MAX_WRITE_BUF_SIZE) { |
| 723 | /* we already flushed above, so the buffer is empty */ |
| 724 | PyObject *result; |
| 725 | /* XXX we could spare an intermediate copy and pass |
| 726 | a memoryview instead */ |
| 727 | PyObject *output = PyBytes_FromStringAndSize(s, n); |
| 728 | if (s == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 729 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 730 | result = _Pickler_FastCall(self, self->write, output); |
| 731 | Py_XDECREF(result); |
| 732 | return (result == NULL) ? -1 : 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 733 | } |
| 734 | else { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 735 | if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) { |
| 736 | PyErr_NoMemory(); |
| 737 | return -1; |
| 738 | } |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 739 | self->max_output_len = (self->output_len + n) / 2 * 3; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 740 | if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0) |
| 741 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 742 | } |
| 743 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 744 | buffer = PyBytes_AS_STRING(self->output_buffer); |
| 745 | if (n < 8) { |
| 746 | /* This is faster than memcpy when the string is short. */ |
| 747 | for (i = 0; i < n; i++) { |
| 748 | buffer[self->output_len + i] = s[i]; |
| 749 | } |
| 750 | } |
| 751 | else { |
| 752 | memcpy(buffer + self->output_len, s, n); |
| 753 | } |
| 754 | self->output_len += n; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 755 | return n; |
| 756 | } |
| 757 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 758 | static PicklerObject * |
| 759 | _Pickler_New(void) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 760 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 761 | PicklerObject *self; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 762 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 763 | self = PyObject_GC_New(PicklerObject, &Pickler_Type); |
| 764 | if (self == NULL) |
| 765 | return NULL; |
| 766 | |
| 767 | self->pers_func = NULL; |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 768 | self->dispatch_table = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 769 | self->arg = NULL; |
| 770 | self->write = NULL; |
| 771 | self->proto = 0; |
| 772 | self->bin = 0; |
| 773 | self->fast = 0; |
| 774 | self->fast_nesting = 0; |
| 775 | self->fix_imports = 0; |
| 776 | self->fast_memo = NULL; |
| 777 | |
| 778 | self->memo = PyMemoTable_New(); |
| 779 | if (self->memo == NULL) { |
| 780 | Py_DECREF(self); |
| 781 | return NULL; |
| 782 | } |
| 783 | self->max_output_len = WRITE_BUF_SIZE; |
| 784 | self->output_len = 0; |
| 785 | self->output_buffer = PyBytes_FromStringAndSize(NULL, |
| 786 | self->max_output_len); |
| 787 | if (self->output_buffer == NULL) { |
| 788 | Py_DECREF(self); |
| 789 | return NULL; |
| 790 | } |
| 791 | return self; |
| 792 | } |
| 793 | |
| 794 | static int |
| 795 | _Pickler_SetProtocol(PicklerObject *self, PyObject *proto_obj, |
| 796 | PyObject *fix_imports_obj) |
| 797 | { |
| 798 | long proto = 0; |
| 799 | int fix_imports; |
| 800 | |
| 801 | if (proto_obj == NULL || proto_obj == Py_None) |
| 802 | proto = DEFAULT_PROTOCOL; |
| 803 | else { |
| 804 | proto = PyLong_AsLong(proto_obj); |
| 805 | if (proto == -1 && PyErr_Occurred()) |
| 806 | return -1; |
| 807 | } |
| 808 | if (proto < 0) |
| 809 | proto = HIGHEST_PROTOCOL; |
| 810 | if (proto > HIGHEST_PROTOCOL) { |
| 811 | PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d", |
| 812 | HIGHEST_PROTOCOL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 813 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 814 | } |
| 815 | fix_imports = PyObject_IsTrue(fix_imports_obj); |
| 816 | if (fix_imports == -1) |
| 817 | return -1; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 818 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 819 | self->proto = proto; |
| 820 | self->bin = proto > 0; |
| 821 | self->fix_imports = fix_imports && proto < 3; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 822 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 823 | return 0; |
| 824 | } |
| 825 | |
| 826 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 827 | be called once on a freshly created Pickler. */ |
| 828 | static int |
| 829 | _Pickler_SetOutputStream(PicklerObject *self, PyObject *file) |
| 830 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 831 | _Py_IDENTIFIER(write); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 832 | assert(file != NULL); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 833 | self->write = _PyObject_GetAttrId(file, &PyId_write); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 834 | if (self->write == NULL) { |
| 835 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 836 | PyErr_SetString(PyExc_TypeError, |
| 837 | "file must have a 'write' attribute"); |
| 838 | return -1; |
| 839 | } |
| 840 | |
| 841 | return 0; |
| 842 | } |
| 843 | |
| 844 | /* See documentation for _Pickler_FastCall(). */ |
| 845 | static PyObject * |
| 846 | _Unpickler_FastCall(UnpicklerObject *self, PyObject *func, PyObject *arg) |
| 847 | { |
| 848 | PyObject *result = NULL; |
| 849 | |
| 850 | ARG_TUP(self, arg); |
| 851 | if (self->arg) { |
| 852 | result = PyObject_Call(func, self->arg, NULL); |
| 853 | FREE_ARG_TUP(self); |
| 854 | } |
| 855 | return result; |
| 856 | } |
| 857 | |
| 858 | /* Returns the size of the input on success, -1 on failure. This takes its |
| 859 | own reference to `input`. */ |
| 860 | static Py_ssize_t |
| 861 | _Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input) |
| 862 | { |
| 863 | if (self->buffer.buf != NULL) |
| 864 | PyBuffer_Release(&self->buffer); |
| 865 | if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0) |
| 866 | return -1; |
| 867 | self->input_buffer = self->buffer.buf; |
| 868 | self->input_len = self->buffer.len; |
| 869 | self->next_read_idx = 0; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 870 | self->prefetched_idx = self->input_len; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 871 | return self->input_len; |
| 872 | } |
| 873 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 874 | static int |
| 875 | _Unpickler_SkipConsumed(UnpicklerObject *self) |
| 876 | { |
| 877 | Py_ssize_t consumed = self->next_read_idx - self->prefetched_idx; |
| 878 | |
| 879 | if (consumed > 0) { |
| 880 | PyObject *r; |
| 881 | assert(self->peek); /* otherwise we did something wrong */ |
| 882 | /* This makes an useless copy... */ |
| 883 | r = PyObject_CallFunction(self->read, "n", consumed); |
| 884 | if (r == NULL) |
| 885 | return -1; |
| 886 | Py_DECREF(r); |
| 887 | self->prefetched_idx = self->next_read_idx; |
| 888 | } |
| 889 | return 0; |
| 890 | } |
| 891 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 892 | static const Py_ssize_t READ_WHOLE_LINE = -1; |
| 893 | |
| 894 | /* If reading from a file, we need to only pull the bytes we need, since there |
| 895 | may be multiple pickle objects arranged contiguously in the same input |
| 896 | buffer. |
| 897 | |
| 898 | If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n` |
| 899 | bytes from the input stream/buffer. |
| 900 | |
| 901 | Update the unpickler's input buffer with the newly-read data. Returns -1 on |
| 902 | failure; on success, returns the number of bytes read from the file. |
| 903 | |
| 904 | On success, self->input_len will be 0; this is intentional so that when |
| 905 | unpickling from a file, the "we've run out of data" code paths will trigger, |
| 906 | causing the Unpickler to go back to the file for more data. Use the returned |
| 907 | size to tell you how much data you can process. */ |
| 908 | static Py_ssize_t |
| 909 | _Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n) |
| 910 | { |
| 911 | PyObject *data; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 912 | Py_ssize_t read_size, prefetched_size = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 913 | |
| 914 | assert(self->read != NULL); |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 915 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 916 | if (_Unpickler_SkipConsumed(self) < 0) |
| 917 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 918 | |
| 919 | if (n == READ_WHOLE_LINE) |
| 920 | data = PyObject_Call(self->readline, empty_tuple, NULL); |
| 921 | else { |
| 922 | PyObject *len = PyLong_FromSsize_t(n); |
| 923 | if (len == NULL) |
| 924 | return -1; |
| 925 | data = _Unpickler_FastCall(self, self->read, len); |
| 926 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 927 | if (data == NULL) |
| 928 | return -1; |
| 929 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 930 | /* Prefetch some data without advancing the file pointer, if possible */ |
| 931 | if (self->peek) { |
| 932 | PyObject *len, *prefetched; |
| 933 | len = PyLong_FromSsize_t(PREFETCH); |
| 934 | if (len == NULL) { |
| 935 | Py_DECREF(data); |
| 936 | return -1; |
| 937 | } |
| 938 | prefetched = _Unpickler_FastCall(self, self->peek, len); |
| 939 | if (prefetched == NULL) { |
| 940 | if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 941 | /* peek() is probably not supported by the given file object */ |
| 942 | PyErr_Clear(); |
| 943 | Py_CLEAR(self->peek); |
| 944 | } |
| 945 | else { |
| 946 | Py_DECREF(data); |
| 947 | return -1; |
| 948 | } |
| 949 | } |
| 950 | else { |
| 951 | assert(PyBytes_Check(prefetched)); |
| 952 | prefetched_size = PyBytes_GET_SIZE(prefetched); |
| 953 | PyBytes_ConcatAndDel(&data, prefetched); |
| 954 | if (data == NULL) |
| 955 | return -1; |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | read_size = _Unpickler_SetStringInput(self, data) - prefetched_size; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 960 | Py_DECREF(data); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 961 | self->prefetched_idx = read_size; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 962 | return read_size; |
| 963 | } |
| 964 | |
| 965 | /* Read `n` bytes from the unpickler's data source, storing the result in `*s`. |
| 966 | |
| 967 | This should be used for all data reads, rather than accessing the unpickler's |
| 968 | input buffer directly. This method deals correctly with reading from input |
| 969 | streams, which the input buffer doesn't deal with. |
| 970 | |
| 971 | Note that when reading from a file-like object, self->next_read_idx won't |
| 972 | be updated (it should remain at 0 for the entire unpickling process). You |
| 973 | should use this function's return value to know how many bytes you can |
| 974 | consume. |
| 975 | |
| 976 | Returns -1 (with an exception set) on failure. On success, return the |
| 977 | number of chars read. */ |
| 978 | static Py_ssize_t |
| 979 | _Unpickler_Read(UnpicklerObject *self, char **s, Py_ssize_t n) |
| 980 | { |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 981 | Py_ssize_t num_read; |
| 982 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 983 | if (self->next_read_idx + n <= self->input_len) { |
| 984 | *s = self->input_buffer + self->next_read_idx; |
| 985 | self->next_read_idx += n; |
| 986 | return n; |
| 987 | } |
| 988 | if (!self->read) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 989 | PyErr_Format(PyExc_EOFError, "Ran out of input"); |
Amaury Forgeot d'Arc | 3e4e72f | 2008-11-11 20:05:06 +0000 | [diff] [blame] | 990 | return -1; |
| 991 | } |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 992 | num_read = _Unpickler_ReadFromFile(self, n); |
| 993 | if (num_read < 0) |
| 994 | return -1; |
| 995 | if (num_read < n) { |
| 996 | PyErr_Format(PyExc_EOFError, "Ran out of input"); |
| 997 | return -1; |
| 998 | } |
| 999 | *s = self->input_buffer; |
| 1000 | self->next_read_idx = n; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1001 | return n; |
| 1002 | } |
| 1003 | |
| 1004 | static Py_ssize_t |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1005 | _Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len, |
| 1006 | char **result) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1007 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1008 | char *input_line = PyMem_Realloc(self->input_line, len + 1); |
| 1009 | if (input_line == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1010 | return -1; |
| 1011 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1012 | memcpy(input_line, line, len); |
| 1013 | input_line[len] = '\0'; |
| 1014 | self->input_line = input_line; |
| 1015 | *result = self->input_line; |
| 1016 | return len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1019 | /* Read a line from the input stream/buffer. If we run off the end of the input |
| 1020 | before hitting \n, return the data we found. |
| 1021 | |
| 1022 | Returns the number of chars read, or -1 on failure. */ |
| 1023 | static Py_ssize_t |
| 1024 | _Unpickler_Readline(UnpicklerObject *self, char **result) |
| 1025 | { |
| 1026 | Py_ssize_t i, num_read; |
| 1027 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1028 | for (i = self->next_read_idx; i < self->input_len; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1029 | if (self->input_buffer[i] == '\n') { |
| 1030 | char *line_start = self->input_buffer + self->next_read_idx; |
| 1031 | num_read = i - self->next_read_idx + 1; |
| 1032 | self->next_read_idx = i + 1; |
| 1033 | return _Unpickler_CopyLine(self, line_start, num_read, result); |
| 1034 | } |
| 1035 | } |
| 1036 | if (self->read) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1037 | num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE); |
| 1038 | if (num_read < 0) |
| 1039 | return -1; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1040 | self->next_read_idx = num_read; |
Antoine Pitrou | f6c7a85 | 2011-08-11 21:04:02 +0200 | [diff] [blame] | 1041 | return _Unpickler_CopyLine(self, self->input_buffer, num_read, result); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1042 | } |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1043 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1044 | /* If we get here, we've run off the end of the input string. Return the |
| 1045 | remaining string and let the caller figure it out. */ |
| 1046 | *result = self->input_buffer + self->next_read_idx; |
| 1047 | num_read = i - self->next_read_idx; |
| 1048 | self->next_read_idx = i; |
| 1049 | return num_read; |
| 1050 | } |
| 1051 | |
| 1052 | /* Returns -1 (with an exception set) on failure, 0 on success. The memo array |
| 1053 | will be modified in place. */ |
| 1054 | static int |
| 1055 | _Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size) |
| 1056 | { |
| 1057 | Py_ssize_t i; |
| 1058 | PyObject **memo; |
| 1059 | |
| 1060 | assert(new_size > self->memo_size); |
| 1061 | |
| 1062 | memo = PyMem_REALLOC(self->memo, new_size * sizeof(PyObject *)); |
| 1063 | if (memo == NULL) { |
| 1064 | PyErr_NoMemory(); |
| 1065 | return -1; |
| 1066 | } |
| 1067 | self->memo = memo; |
| 1068 | for (i = self->memo_size; i < new_size; i++) |
| 1069 | self->memo[i] = NULL; |
| 1070 | self->memo_size = new_size; |
| 1071 | return 0; |
| 1072 | } |
| 1073 | |
| 1074 | /* Returns NULL if idx is out of bounds. */ |
| 1075 | static PyObject * |
| 1076 | _Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx) |
| 1077 | { |
| 1078 | if (idx < 0 || idx >= self->memo_size) |
| 1079 | return NULL; |
| 1080 | |
| 1081 | return self->memo[idx]; |
| 1082 | } |
| 1083 | |
| 1084 | /* Returns -1 (with an exception set) on failure, 0 on success. |
| 1085 | This takes its own reference to `value`. */ |
| 1086 | static int |
| 1087 | _Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value) |
| 1088 | { |
| 1089 | PyObject *old_item; |
| 1090 | |
| 1091 | if (idx >= self->memo_size) { |
| 1092 | if (_Unpickler_ResizeMemoList(self, idx * 2) < 0) |
| 1093 | return -1; |
| 1094 | assert(idx < self->memo_size); |
| 1095 | } |
| 1096 | Py_INCREF(value); |
| 1097 | old_item = self->memo[idx]; |
| 1098 | self->memo[idx] = value; |
| 1099 | Py_XDECREF(old_item); |
| 1100 | return 0; |
| 1101 | } |
| 1102 | |
| 1103 | static PyObject ** |
| 1104 | _Unpickler_NewMemo(Py_ssize_t new_size) |
| 1105 | { |
| 1106 | PyObject **memo = PyMem_MALLOC(new_size * sizeof(PyObject *)); |
| 1107 | if (memo == NULL) |
| 1108 | return NULL; |
| 1109 | memset(memo, 0, new_size * sizeof(PyObject *)); |
| 1110 | return memo; |
| 1111 | } |
| 1112 | |
| 1113 | /* Free the unpickler's memo, taking care to decref any items left in it. */ |
| 1114 | static void |
| 1115 | _Unpickler_MemoCleanup(UnpicklerObject *self) |
| 1116 | { |
| 1117 | Py_ssize_t i; |
| 1118 | PyObject **memo = self->memo; |
| 1119 | |
| 1120 | if (self->memo == NULL) |
| 1121 | return; |
| 1122 | self->memo = NULL; |
| 1123 | i = self->memo_size; |
| 1124 | while (--i >= 0) { |
| 1125 | Py_XDECREF(memo[i]); |
| 1126 | } |
| 1127 | PyMem_FREE(memo); |
| 1128 | } |
| 1129 | |
| 1130 | static UnpicklerObject * |
| 1131 | _Unpickler_New(void) |
| 1132 | { |
| 1133 | UnpicklerObject *self; |
| 1134 | |
| 1135 | self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type); |
| 1136 | if (self == NULL) |
| 1137 | return NULL; |
| 1138 | |
| 1139 | self->stack = (Pdata *)Pdata_New(); |
| 1140 | if (self->stack == NULL) { |
| 1141 | Py_DECREF(self); |
| 1142 | return NULL; |
| 1143 | } |
| 1144 | memset(&self->buffer, 0, sizeof(Py_buffer)); |
| 1145 | |
| 1146 | self->memo_size = 32; |
| 1147 | self->memo = _Unpickler_NewMemo(self->memo_size); |
| 1148 | if (self->memo == NULL) { |
| 1149 | Py_DECREF(self); |
| 1150 | return NULL; |
| 1151 | } |
| 1152 | |
| 1153 | self->arg = NULL; |
| 1154 | self->pers_func = NULL; |
| 1155 | self->input_buffer = NULL; |
| 1156 | self->input_line = NULL; |
| 1157 | self->input_len = 0; |
| 1158 | self->next_read_idx = 0; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1159 | self->prefetched_idx = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1160 | self->read = NULL; |
| 1161 | self->readline = NULL; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1162 | self->peek = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1163 | self->encoding = NULL; |
| 1164 | self->errors = NULL; |
| 1165 | self->marks = NULL; |
| 1166 | self->num_marks = 0; |
| 1167 | self->marks_size = 0; |
| 1168 | self->proto = 0; |
| 1169 | self->fix_imports = 0; |
| 1170 | |
| 1171 | return self; |
| 1172 | } |
| 1173 | |
| 1174 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1175 | be called once on a freshly created Pickler. */ |
| 1176 | static int |
| 1177 | _Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file) |
| 1178 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1179 | _Py_IDENTIFIER(peek); |
| 1180 | _Py_IDENTIFIER(read); |
| 1181 | _Py_IDENTIFIER(readline); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1182 | |
| 1183 | self->peek = _PyObject_GetAttrId(file, &PyId_peek); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1184 | if (self->peek == NULL) { |
| 1185 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1186 | PyErr_Clear(); |
| 1187 | else |
| 1188 | return -1; |
| 1189 | } |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1190 | self->read = _PyObject_GetAttrId(file, &PyId_read); |
| 1191 | self->readline = _PyObject_GetAttrId(file, &PyId_readline); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1192 | if (self->readline == NULL || self->read == NULL) { |
| 1193 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1194 | PyErr_SetString(PyExc_TypeError, |
| 1195 | "file must have 'read' and 'readline' attributes"); |
| 1196 | Py_CLEAR(self->read); |
| 1197 | Py_CLEAR(self->readline); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1198 | Py_CLEAR(self->peek); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1199 | return -1; |
| 1200 | } |
| 1201 | return 0; |
| 1202 | } |
| 1203 | |
| 1204 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1205 | be called once on a freshly created Pickler. */ |
| 1206 | static int |
| 1207 | _Unpickler_SetInputEncoding(UnpicklerObject *self, |
| 1208 | const char *encoding, |
| 1209 | const char *errors) |
| 1210 | { |
| 1211 | if (encoding == NULL) |
| 1212 | encoding = "ASCII"; |
| 1213 | if (errors == NULL) |
| 1214 | errors = "strict"; |
| 1215 | |
| 1216 | self->encoding = strdup(encoding); |
| 1217 | self->errors = strdup(errors); |
| 1218 | if (self->encoding == NULL || self->errors == NULL) { |
| 1219 | PyErr_NoMemory(); |
| 1220 | return -1; |
| 1221 | } |
| 1222 | return 0; |
| 1223 | } |
| 1224 | |
| 1225 | /* Generate a GET opcode for an object stored in the memo. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1226 | static int |
| 1227 | memo_get(PicklerObject *self, PyObject *key) |
| 1228 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1229 | Py_ssize_t *value; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1230 | char pdata[30]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1231 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1232 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1233 | value = PyMemoTable_Get(self->memo, key); |
| 1234 | if (value == NULL) { |
| 1235 | PyErr_SetObject(PyExc_KeyError, key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1236 | return -1; |
| 1237 | } |
| 1238 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1239 | if (!self->bin) { |
| 1240 | pdata[0] = GET; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1241 | PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, |
| 1242 | "%" PY_FORMAT_SIZE_T "d\n", *value); |
| 1243 | len = strlen(pdata); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1244 | } |
| 1245 | else { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1246 | if (*value < 256) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1247 | pdata[0] = BINGET; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1248 | pdata[1] = (unsigned char)(*value & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1249 | len = 2; |
| 1250 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1251 | else if (*value <= 0xffffffffL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1252 | pdata[0] = LONG_BINGET; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1253 | pdata[1] = (unsigned char)(*value & 0xff); |
| 1254 | pdata[2] = (unsigned char)((*value >> 8) & 0xff); |
| 1255 | pdata[3] = (unsigned char)((*value >> 16) & 0xff); |
| 1256 | pdata[4] = (unsigned char)((*value >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1257 | len = 5; |
| 1258 | } |
| 1259 | else { /* unlikely */ |
| 1260 | PyErr_SetString(PicklingError, |
| 1261 | "memo id too large for LONG_BINGET"); |
| 1262 | return -1; |
| 1263 | } |
| 1264 | } |
| 1265 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1266 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1267 | return -1; |
| 1268 | |
| 1269 | return 0; |
| 1270 | } |
| 1271 | |
| 1272 | /* Store an object in the memo, assign it a new unique ID based on the number |
| 1273 | of objects currently stored in the memo and generate a PUT opcode. */ |
| 1274 | static int |
| 1275 | memo_put(PicklerObject *self, PyObject *obj) |
| 1276 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1277 | Py_ssize_t x; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1278 | char pdata[30]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1279 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1280 | int status = 0; |
| 1281 | |
| 1282 | if (self->fast) |
| 1283 | return 0; |
| 1284 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1285 | x = PyMemoTable_Size(self->memo); |
| 1286 | if (PyMemoTable_Set(self->memo, obj, x) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1287 | goto error; |
| 1288 | |
| 1289 | if (!self->bin) { |
| 1290 | pdata[0] = PUT; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1291 | PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, |
| 1292 | "%" PY_FORMAT_SIZE_T "d\n", x); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1293 | len = strlen(pdata); |
| 1294 | } |
| 1295 | else { |
| 1296 | if (x < 256) { |
| 1297 | pdata[0] = BINPUT; |
Alexandre Vassalotti | 7634ff5 | 2008-06-13 02:16:06 +0000 | [diff] [blame] | 1298 | pdata[1] = (unsigned char)x; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1299 | len = 2; |
| 1300 | } |
| 1301 | else if (x <= 0xffffffffL) { |
| 1302 | pdata[0] = LONG_BINPUT; |
| 1303 | pdata[1] = (unsigned char)(x & 0xff); |
| 1304 | pdata[2] = (unsigned char)((x >> 8) & 0xff); |
| 1305 | pdata[3] = (unsigned char)((x >> 16) & 0xff); |
| 1306 | pdata[4] = (unsigned char)((x >> 24) & 0xff); |
| 1307 | len = 5; |
| 1308 | } |
| 1309 | else { /* unlikely */ |
| 1310 | PyErr_SetString(PicklingError, |
| 1311 | "memo id too large for LONG_BINPUT"); |
| 1312 | return -1; |
| 1313 | } |
| 1314 | } |
| 1315 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1316 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1317 | goto error; |
| 1318 | |
| 1319 | if (0) { |
| 1320 | error: |
| 1321 | status = -1; |
| 1322 | } |
| 1323 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1324 | return status; |
| 1325 | } |
| 1326 | |
| 1327 | static PyObject * |
| 1328 | whichmodule(PyObject *global, PyObject *global_name) |
| 1329 | { |
| 1330 | Py_ssize_t i, j; |
| 1331 | static PyObject *module_str = NULL; |
| 1332 | static PyObject *main_str = NULL; |
| 1333 | PyObject *module_name; |
| 1334 | PyObject *modules_dict; |
| 1335 | PyObject *module; |
| 1336 | PyObject *obj; |
| 1337 | |
| 1338 | if (module_str == NULL) { |
| 1339 | module_str = PyUnicode_InternFromString("__module__"); |
| 1340 | if (module_str == NULL) |
| 1341 | return NULL; |
| 1342 | main_str = PyUnicode_InternFromString("__main__"); |
| 1343 | if (main_str == NULL) |
| 1344 | return NULL; |
| 1345 | } |
| 1346 | |
| 1347 | module_name = PyObject_GetAttr(global, module_str); |
| 1348 | |
Alexandre Vassalotti | 0e7aa8c | 2009-04-03 04:17:41 +0000 | [diff] [blame] | 1349 | /* In some rare cases (e.g., bound methods of extension types), |
| 1350 | __module__ can be None. If it is so, then search sys.modules |
| 1351 | for the module of global. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1352 | if (module_name == Py_None) { |
| 1353 | Py_DECREF(module_name); |
| 1354 | goto search; |
| 1355 | } |
| 1356 | |
| 1357 | if (module_name) { |
| 1358 | return module_name; |
| 1359 | } |
| 1360 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1361 | PyErr_Clear(); |
| 1362 | else |
| 1363 | return NULL; |
| 1364 | |
| 1365 | search: |
| 1366 | modules_dict = PySys_GetObject("modules"); |
| 1367 | if (modules_dict == NULL) |
| 1368 | return NULL; |
| 1369 | |
| 1370 | i = 0; |
| 1371 | module_name = NULL; |
| 1372 | while ((j = PyDict_Next(modules_dict, &i, &module_name, &module))) { |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 1373 | if (PyObject_RichCompareBool(module_name, main_str, Py_EQ) == 1) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1374 | continue; |
| 1375 | |
| 1376 | obj = PyObject_GetAttr(module, global_name); |
| 1377 | if (obj == NULL) { |
| 1378 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1379 | PyErr_Clear(); |
| 1380 | else |
| 1381 | return NULL; |
| 1382 | continue; |
| 1383 | } |
| 1384 | |
| 1385 | if (obj != global) { |
| 1386 | Py_DECREF(obj); |
| 1387 | continue; |
| 1388 | } |
| 1389 | |
| 1390 | Py_DECREF(obj); |
| 1391 | break; |
| 1392 | } |
| 1393 | |
| 1394 | /* If no module is found, use __main__. */ |
| 1395 | if (!j) { |
| 1396 | module_name = main_str; |
| 1397 | } |
| 1398 | |
| 1399 | Py_INCREF(module_name); |
| 1400 | return module_name; |
| 1401 | } |
| 1402 | |
| 1403 | /* fast_save_enter() and fast_save_leave() are guards against recursive |
| 1404 | objects when Pickler is used with the "fast mode" (i.e., with object |
| 1405 | memoization disabled). If the nesting of a list or dict object exceed |
| 1406 | FAST_NESTING_LIMIT, these guards will start keeping an internal |
| 1407 | reference to the seen list or dict objects and check whether these objects |
| 1408 | are recursive. These are not strictly necessary, since save() has a |
| 1409 | hard-coded recursion limit, but they give a nicer error message than the |
| 1410 | typical RuntimeError. */ |
| 1411 | static int |
| 1412 | fast_save_enter(PicklerObject *self, PyObject *obj) |
| 1413 | { |
| 1414 | /* if fast_nesting < 0, we're doing an error exit. */ |
| 1415 | if (++self->fast_nesting >= FAST_NESTING_LIMIT) { |
| 1416 | PyObject *key = NULL; |
| 1417 | if (self->fast_memo == NULL) { |
| 1418 | self->fast_memo = PyDict_New(); |
| 1419 | if (self->fast_memo == NULL) { |
| 1420 | self->fast_nesting = -1; |
| 1421 | return 0; |
| 1422 | } |
| 1423 | } |
| 1424 | key = PyLong_FromVoidPtr(obj); |
| 1425 | if (key == NULL) |
| 1426 | return 0; |
| 1427 | if (PyDict_GetItem(self->fast_memo, key)) { |
| 1428 | Py_DECREF(key); |
| 1429 | PyErr_Format(PyExc_ValueError, |
| 1430 | "fast mode: can't pickle cyclic objects " |
| 1431 | "including object type %.200s at %p", |
| 1432 | obj->ob_type->tp_name, obj); |
| 1433 | self->fast_nesting = -1; |
| 1434 | return 0; |
| 1435 | } |
| 1436 | if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) { |
| 1437 | Py_DECREF(key); |
| 1438 | self->fast_nesting = -1; |
| 1439 | return 0; |
| 1440 | } |
| 1441 | Py_DECREF(key); |
| 1442 | } |
| 1443 | return 1; |
| 1444 | } |
| 1445 | |
| 1446 | static int |
| 1447 | fast_save_leave(PicklerObject *self, PyObject *obj) |
| 1448 | { |
| 1449 | if (self->fast_nesting-- >= FAST_NESTING_LIMIT) { |
| 1450 | PyObject *key = PyLong_FromVoidPtr(obj); |
| 1451 | if (key == NULL) |
| 1452 | return 0; |
| 1453 | if (PyDict_DelItem(self->fast_memo, key) < 0) { |
| 1454 | Py_DECREF(key); |
| 1455 | return 0; |
| 1456 | } |
| 1457 | Py_DECREF(key); |
| 1458 | } |
| 1459 | return 1; |
| 1460 | } |
| 1461 | |
| 1462 | static int |
| 1463 | save_none(PicklerObject *self, PyObject *obj) |
| 1464 | { |
| 1465 | const char none_op = NONE; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1466 | if (_Pickler_Write(self, &none_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1467 | return -1; |
| 1468 | |
| 1469 | return 0; |
| 1470 | } |
| 1471 | |
| 1472 | static int |
| 1473 | save_bool(PicklerObject *self, PyObject *obj) |
| 1474 | { |
| 1475 | static const char *buf[2] = { FALSE, TRUE }; |
| 1476 | const char len[2] = {sizeof(FALSE) - 1, sizeof(TRUE) - 1}; |
| 1477 | int p = (obj == Py_True); |
| 1478 | |
| 1479 | if (self->proto >= 2) { |
| 1480 | const char bool_op = p ? NEWTRUE : NEWFALSE; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1481 | if (_Pickler_Write(self, &bool_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1482 | return -1; |
| 1483 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1484 | else if (_Pickler_Write(self, buf[p], len[p]) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1485 | return -1; |
| 1486 | |
| 1487 | return 0; |
| 1488 | } |
| 1489 | |
| 1490 | static int |
| 1491 | save_int(PicklerObject *self, long x) |
| 1492 | { |
| 1493 | char pdata[32]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1494 | Py_ssize_t len = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1495 | |
| 1496 | if (!self->bin |
| 1497 | #if SIZEOF_LONG > 4 |
| 1498 | || x > 0x7fffffffL || x < -0x80000000L |
| 1499 | #endif |
| 1500 | ) { |
| 1501 | /* Text-mode pickle, or long too big to fit in the 4-byte |
| 1502 | * signed BININT format: store as a string. |
| 1503 | */ |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 1504 | pdata[0] = LONG; /* use LONG for consistency with pickle.py */ |
| 1505 | PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, "%ldL\n", x); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1506 | if (_Pickler_Write(self, pdata, strlen(pdata)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1507 | return -1; |
| 1508 | } |
| 1509 | else { |
| 1510 | /* Binary pickle and x fits in a signed 4-byte int. */ |
| 1511 | pdata[1] = (unsigned char)(x & 0xff); |
| 1512 | pdata[2] = (unsigned char)((x >> 8) & 0xff); |
| 1513 | pdata[3] = (unsigned char)((x >> 16) & 0xff); |
| 1514 | pdata[4] = (unsigned char)((x >> 24) & 0xff); |
| 1515 | |
| 1516 | if ((pdata[4] == 0) && (pdata[3] == 0)) { |
| 1517 | if (pdata[2] == 0) { |
| 1518 | pdata[0] = BININT1; |
| 1519 | len = 2; |
| 1520 | } |
| 1521 | else { |
| 1522 | pdata[0] = BININT2; |
| 1523 | len = 3; |
| 1524 | } |
| 1525 | } |
| 1526 | else { |
| 1527 | pdata[0] = BININT; |
| 1528 | len = 5; |
| 1529 | } |
| 1530 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1531 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1532 | return -1; |
| 1533 | } |
| 1534 | |
| 1535 | return 0; |
| 1536 | } |
| 1537 | |
| 1538 | static int |
| 1539 | save_long(PicklerObject *self, PyObject *obj) |
| 1540 | { |
| 1541 | PyObject *repr = NULL; |
| 1542 | Py_ssize_t size; |
| 1543 | long val = PyLong_AsLong(obj); |
| 1544 | int status = 0; |
| 1545 | |
| 1546 | const char long_op = LONG; |
| 1547 | |
| 1548 | if (val == -1 && PyErr_Occurred()) { |
| 1549 | /* out of range for int pickling */ |
| 1550 | PyErr_Clear(); |
| 1551 | } |
Antoine Pitrou | e58bffb | 2011-08-13 20:40:32 +0200 | [diff] [blame] | 1552 | else |
| 1553 | #if SIZEOF_LONG > 4 |
| 1554 | if (val <= 0x7fffffffL && val >= -0x80000000L) |
| 1555 | #endif |
| 1556 | return save_int(self, val); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1557 | |
| 1558 | if (self->proto >= 2) { |
| 1559 | /* Linear-time pickling. */ |
| 1560 | size_t nbits; |
| 1561 | size_t nbytes; |
| 1562 | unsigned char *pdata; |
| 1563 | char header[5]; |
| 1564 | int i; |
| 1565 | int sign = _PyLong_Sign(obj); |
| 1566 | |
| 1567 | if (sign == 0) { |
| 1568 | header[0] = LONG1; |
| 1569 | header[1] = 0; /* It's 0 -- an empty bytestring. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1570 | if (_Pickler_Write(self, header, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1571 | goto error; |
| 1572 | return 0; |
| 1573 | } |
| 1574 | nbits = _PyLong_NumBits(obj); |
| 1575 | if (nbits == (size_t)-1 && PyErr_Occurred()) |
| 1576 | goto error; |
| 1577 | /* How many bytes do we need? There are nbits >> 3 full |
| 1578 | * bytes of data, and nbits & 7 leftover bits. If there |
| 1579 | * are any leftover bits, then we clearly need another |
| 1580 | * byte. Wnat's not so obvious is that we *probably* |
| 1581 | * need another byte even if there aren't any leftovers: |
| 1582 | * the most-significant bit of the most-significant byte |
| 1583 | * acts like a sign bit, and it's usually got a sense |
| 1584 | * opposite of the one we need. The exception is longs |
| 1585 | * of the form -(2**(8*j-1)) for j > 0. Such a long is |
| 1586 | * its own 256's-complement, so has the right sign bit |
| 1587 | * even without the extra byte. That's a pain to check |
| 1588 | * for in advance, though, so we always grab an extra |
| 1589 | * byte at the start, and cut it back later if possible. |
| 1590 | */ |
| 1591 | nbytes = (nbits >> 3) + 1; |
Antoine Pitrou | bf6ecf9 | 2012-11-24 20:40:21 +0100 | [diff] [blame] | 1592 | if (nbytes > 0x7fffffffL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1593 | PyErr_SetString(PyExc_OverflowError, |
| 1594 | "long too large to pickle"); |
| 1595 | goto error; |
| 1596 | } |
Neal Norwitz | 6ae2eb2 | 2008-08-24 23:50:08 +0000 | [diff] [blame] | 1597 | repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1598 | if (repr == NULL) |
| 1599 | goto error; |
Neal Norwitz | 6ae2eb2 | 2008-08-24 23:50:08 +0000 | [diff] [blame] | 1600 | pdata = (unsigned char *)PyBytes_AS_STRING(repr); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1601 | i = _PyLong_AsByteArray((PyLongObject *)obj, |
| 1602 | pdata, nbytes, |
| 1603 | 1 /* little endian */ , 1 /* signed */ ); |
| 1604 | if (i < 0) |
| 1605 | goto error; |
| 1606 | /* If the long is negative, this may be a byte more than |
| 1607 | * needed. This is so iff the MSB is all redundant sign |
| 1608 | * bits. |
| 1609 | */ |
| 1610 | if (sign < 0 && |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1611 | nbytes > 1 && |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1612 | pdata[nbytes - 1] == 0xff && |
| 1613 | (pdata[nbytes - 2] & 0x80) != 0) { |
| 1614 | nbytes--; |
| 1615 | } |
| 1616 | |
| 1617 | if (nbytes < 256) { |
| 1618 | header[0] = LONG1; |
| 1619 | header[1] = (unsigned char)nbytes; |
| 1620 | size = 2; |
| 1621 | } |
| 1622 | else { |
| 1623 | header[0] = LONG4; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1624 | size = (Py_ssize_t) nbytes; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1625 | for (i = 1; i < 5; i++) { |
| 1626 | header[i] = (unsigned char)(size & 0xff); |
| 1627 | size >>= 8; |
| 1628 | } |
| 1629 | size = 5; |
| 1630 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1631 | if (_Pickler_Write(self, header, size) < 0 || |
| 1632 | _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1633 | goto error; |
| 1634 | } |
| 1635 | else { |
| 1636 | char *string; |
| 1637 | |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 1638 | /* proto < 2: write the repr and newline. This is quadratic-time (in |
| 1639 | the number of digits), in both directions. We add a trailing 'L' |
| 1640 | to the repr, for compatibility with Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1641 | |
| 1642 | repr = PyObject_Repr(obj); |
| 1643 | if (repr == NULL) |
| 1644 | goto error; |
| 1645 | |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1646 | string = _PyUnicode_AsStringAndSize(repr, &size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1647 | if (string == NULL) |
| 1648 | goto error; |
| 1649 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1650 | if (_Pickler_Write(self, &long_op, 1) < 0 || |
| 1651 | _Pickler_Write(self, string, size) < 0 || |
| 1652 | _Pickler_Write(self, "L\n", 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1653 | goto error; |
| 1654 | } |
| 1655 | |
| 1656 | if (0) { |
| 1657 | error: |
| 1658 | status = -1; |
| 1659 | } |
| 1660 | Py_XDECREF(repr); |
| 1661 | |
| 1662 | return status; |
| 1663 | } |
| 1664 | |
| 1665 | static int |
| 1666 | save_float(PicklerObject *self, PyObject *obj) |
| 1667 | { |
| 1668 | double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj); |
| 1669 | |
| 1670 | if (self->bin) { |
| 1671 | char pdata[9]; |
| 1672 | pdata[0] = BINFLOAT; |
| 1673 | if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0) |
| 1674 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1675 | if (_Pickler_Write(self, pdata, 9) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1676 | return -1; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1677 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1678 | else { |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1679 | int result = -1; |
| 1680 | char *buf = NULL; |
| 1681 | char op = FLOAT; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1682 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1683 | if (_Pickler_Write(self, &op, 1) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1684 | goto done; |
| 1685 | |
Mark Dickinson | 3e09f43 | 2009-04-17 08:41:23 +0000 | [diff] [blame] | 1686 | buf = PyOS_double_to_string(x, 'g', 17, 0, NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1687 | if (!buf) { |
| 1688 | PyErr_NoMemory(); |
| 1689 | goto done; |
| 1690 | } |
| 1691 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1692 | if (_Pickler_Write(self, buf, strlen(buf)) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1693 | goto done; |
| 1694 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1695 | if (_Pickler_Write(self, "\n", 1) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1696 | goto done; |
| 1697 | |
| 1698 | result = 0; |
| 1699 | done: |
| 1700 | PyMem_Free(buf); |
| 1701 | return result; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
| 1704 | return 0; |
| 1705 | } |
| 1706 | |
| 1707 | static int |
| 1708 | save_bytes(PicklerObject *self, PyObject *obj) |
| 1709 | { |
| 1710 | if (self->proto < 3) { |
| 1711 | /* Older pickle protocols do not have an opcode for pickling bytes |
| 1712 | objects. Therefore, we need to fake the copy protocol (i.e., |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 1713 | the __reduce__ method) to permit bytes object unpickling. |
| 1714 | |
| 1715 | Here we use a hack to be compatible with Python 2. Since in Python |
| 1716 | 2 'bytes' is just an alias for 'str' (which has different |
| 1717 | parameters than the actual bytes object), we use codecs.encode |
| 1718 | to create the appropriate 'str' object when unpickled using |
| 1719 | Python 2 *and* the appropriate 'bytes' object when unpickled |
| 1720 | using Python 3. Again this is a hack and we don't need to do this |
| 1721 | with newer protocols. */ |
| 1722 | static PyObject *codecs_encode = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1723 | PyObject *reduce_value = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1724 | int status; |
| 1725 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 1726 | if (codecs_encode == NULL) { |
| 1727 | PyObject *codecs_module = PyImport_ImportModule("codecs"); |
| 1728 | if (codecs_module == NULL) { |
| 1729 | return -1; |
| 1730 | } |
| 1731 | codecs_encode = PyObject_GetAttrString(codecs_module, "encode"); |
| 1732 | Py_DECREF(codecs_module); |
| 1733 | if (codecs_encode == NULL) { |
| 1734 | return -1; |
| 1735 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 1738 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 1739 | reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type); |
| 1740 | } |
| 1741 | else { |
| 1742 | static PyObject *latin1 = NULL; |
| 1743 | PyObject *unicode_str = |
| 1744 | PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj), |
| 1745 | PyBytes_GET_SIZE(obj), |
| 1746 | "strict"); |
| 1747 | if (unicode_str == NULL) |
| 1748 | return -1; |
| 1749 | if (latin1 == NULL) { |
| 1750 | latin1 = PyUnicode_InternFromString("latin1"); |
| 1751 | if (latin1 == NULL) |
| 1752 | return -1; |
| 1753 | } |
| 1754 | reduce_value = Py_BuildValue("(O(OO))", |
| 1755 | codecs_encode, unicode_str, latin1); |
| 1756 | Py_DECREF(unicode_str); |
| 1757 | } |
| 1758 | |
| 1759 | if (reduce_value == NULL) |
| 1760 | return -1; |
| 1761 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1762 | /* save_reduce() will memoize the object automatically. */ |
| 1763 | status = save_reduce(self, reduce_value, obj); |
| 1764 | Py_DECREF(reduce_value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1765 | return status; |
| 1766 | } |
| 1767 | else { |
| 1768 | Py_ssize_t size; |
| 1769 | char header[5]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1770 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1771 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 1772 | size = PyBytes_GET_SIZE(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1773 | if (size < 0) |
| 1774 | return -1; |
| 1775 | |
| 1776 | if (size < 256) { |
| 1777 | header[0] = SHORT_BINBYTES; |
| 1778 | header[1] = (unsigned char)size; |
| 1779 | len = 2; |
| 1780 | } |
| 1781 | else if (size <= 0xffffffffL) { |
| 1782 | header[0] = BINBYTES; |
| 1783 | header[1] = (unsigned char)(size & 0xff); |
| 1784 | header[2] = (unsigned char)((size >> 8) & 0xff); |
| 1785 | header[3] = (unsigned char)((size >> 16) & 0xff); |
| 1786 | header[4] = (unsigned char)((size >> 24) & 0xff); |
| 1787 | len = 5; |
| 1788 | } |
| 1789 | else { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1790 | PyErr_SetString(PyExc_OverflowError, |
| 1791 | "cannot serialize a bytes object larger than 4GB"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1792 | return -1; /* string too large */ |
| 1793 | } |
| 1794 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1795 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1796 | return -1; |
| 1797 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1798 | if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1799 | return -1; |
| 1800 | |
| 1801 | if (memo_put(self, obj) < 0) |
| 1802 | return -1; |
| 1803 | |
| 1804 | return 0; |
| 1805 | } |
| 1806 | } |
| 1807 | |
| 1808 | /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates |
| 1809 | backslash and newline characters to \uXXXX escapes. */ |
| 1810 | static PyObject * |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 1811 | raw_unicode_escape(PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1812 | { |
| 1813 | PyObject *repr, *result; |
| 1814 | char *p; |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 1815 | Py_ssize_t i, size, expandsize; |
| 1816 | void *data; |
| 1817 | unsigned int kind; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1818 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 1819 | if (PyUnicode_READY(obj)) |
| 1820 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1821 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 1822 | size = PyUnicode_GET_LENGTH(obj); |
| 1823 | data = PyUnicode_DATA(obj); |
| 1824 | kind = PyUnicode_KIND(obj); |
| 1825 | if (kind == PyUnicode_4BYTE_KIND) |
| 1826 | expandsize = 10; |
| 1827 | else |
| 1828 | expandsize = 6; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1829 | |
Alexandre Vassalotti | 554d878 | 2008-12-27 07:32:41 +0000 | [diff] [blame] | 1830 | if (size > PY_SSIZE_T_MAX / expandsize) |
| 1831 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 554d878 | 2008-12-27 07:32:41 +0000 | [diff] [blame] | 1832 | repr = PyByteArray_FromStringAndSize(NULL, expandsize * size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1833 | if (repr == NULL) |
| 1834 | return NULL; |
| 1835 | if (size == 0) |
| 1836 | goto done; |
| 1837 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 1838 | p = PyByteArray_AS_STRING(repr); |
| 1839 | for (i=0; i < size; i++) { |
| 1840 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1841 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 1842 | if (ch >= 0x10000) { |
| 1843 | *p++ = '\\'; |
| 1844 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 1845 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 1846 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 1847 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 1848 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 1849 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 1850 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 1851 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 1852 | *p++ = Py_hexdigits[ch & 15]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1853 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1854 | /* Map 16-bit characters to '\uxxxx' */ |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 1855 | else if (ch >= 256 || ch == '\\' || ch == '\n') { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1856 | *p++ = '\\'; |
| 1857 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 1858 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 1859 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 1860 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 1861 | *p++ = Py_hexdigits[ch & 15]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1862 | } |
Alexandre Vassalotti | 554d878 | 2008-12-27 07:32:41 +0000 | [diff] [blame] | 1863 | /* Copy everything else as-is */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1864 | else |
| 1865 | *p++ = (char) ch; |
| 1866 | } |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 1867 | size = p - PyByteArray_AS_STRING(repr); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1868 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 1869 | done: |
Alexandre Vassalotti | 554d878 | 2008-12-27 07:32:41 +0000 | [diff] [blame] | 1870 | result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(repr), size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1871 | Py_DECREF(repr); |
| 1872 | return result; |
| 1873 | } |
| 1874 | |
| 1875 | static int |
| 1876 | save_unicode(PicklerObject *self, PyObject *obj) |
| 1877 | { |
| 1878 | Py_ssize_t size; |
| 1879 | PyObject *encoded = NULL; |
| 1880 | |
| 1881 | if (self->bin) { |
| 1882 | char pdata[5]; |
| 1883 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1884 | encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1885 | if (encoded == NULL) |
| 1886 | goto error; |
| 1887 | |
| 1888 | size = PyBytes_GET_SIZE(encoded); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1889 | if (size > 0xffffffffL) { |
| 1890 | PyErr_SetString(PyExc_OverflowError, |
| 1891 | "cannot serialize a string larger than 4GB"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1892 | goto error; /* string too large */ |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1893 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1894 | |
| 1895 | pdata[0] = BINUNICODE; |
| 1896 | pdata[1] = (unsigned char)(size & 0xff); |
| 1897 | pdata[2] = (unsigned char)((size >> 8) & 0xff); |
| 1898 | pdata[3] = (unsigned char)((size >> 16) & 0xff); |
| 1899 | pdata[4] = (unsigned char)((size >> 24) & 0xff); |
| 1900 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1901 | if (_Pickler_Write(self, pdata, 5) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1902 | goto error; |
| 1903 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1904 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1905 | goto error; |
| 1906 | } |
| 1907 | else { |
| 1908 | const char unicode_op = UNICODE; |
| 1909 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 1910 | encoded = raw_unicode_escape(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1911 | if (encoded == NULL) |
| 1912 | goto error; |
| 1913 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1914 | if (_Pickler_Write(self, &unicode_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1915 | goto error; |
| 1916 | |
| 1917 | size = PyBytes_GET_SIZE(encoded); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1918 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1919 | goto error; |
| 1920 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1921 | if (_Pickler_Write(self, "\n", 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1922 | goto error; |
| 1923 | } |
| 1924 | if (memo_put(self, obj) < 0) |
| 1925 | goto error; |
| 1926 | |
| 1927 | Py_DECREF(encoded); |
| 1928 | return 0; |
| 1929 | |
| 1930 | error: |
| 1931 | Py_XDECREF(encoded); |
| 1932 | return -1; |
| 1933 | } |
| 1934 | |
| 1935 | /* A helper for save_tuple. Push the len elements in tuple t on the stack. */ |
| 1936 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1937 | store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1938 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1939 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1940 | |
| 1941 | assert(PyTuple_Size(t) == len); |
| 1942 | |
| 1943 | for (i = 0; i < len; i++) { |
| 1944 | PyObject *element = PyTuple_GET_ITEM(t, i); |
| 1945 | |
| 1946 | if (element == NULL) |
| 1947 | return -1; |
| 1948 | if (save(self, element, 0) < 0) |
| 1949 | return -1; |
| 1950 | } |
| 1951 | |
| 1952 | return 0; |
| 1953 | } |
| 1954 | |
| 1955 | /* Tuples are ubiquitous in the pickle protocols, so many techniques are |
| 1956 | * used across protocols to minimize the space needed to pickle them. |
| 1957 | * Tuples are also the only builtin immutable type that can be recursive |
| 1958 | * (a tuple can be reached from itself), and that requires some subtle |
| 1959 | * magic so that it works in all cases. IOW, this is a long routine. |
| 1960 | */ |
| 1961 | static int |
| 1962 | save_tuple(PicklerObject *self, PyObject *obj) |
| 1963 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1964 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1965 | |
| 1966 | const char mark_op = MARK; |
| 1967 | const char tuple_op = TUPLE; |
| 1968 | const char pop_op = POP; |
| 1969 | const char pop_mark_op = POP_MARK; |
| 1970 | const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3}; |
| 1971 | |
| 1972 | if ((len = PyTuple_Size(obj)) < 0) |
| 1973 | return -1; |
| 1974 | |
| 1975 | if (len == 0) { |
| 1976 | char pdata[2]; |
| 1977 | |
| 1978 | if (self->proto) { |
| 1979 | pdata[0] = EMPTY_TUPLE; |
| 1980 | len = 1; |
| 1981 | } |
| 1982 | else { |
| 1983 | pdata[0] = MARK; |
| 1984 | pdata[1] = TUPLE; |
| 1985 | len = 2; |
| 1986 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1987 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1988 | return -1; |
| 1989 | return 0; |
| 1990 | } |
| 1991 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1992 | /* The tuple isn't in the memo now. If it shows up there after |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1993 | * saving the tuple elements, the tuple must be recursive, in |
| 1994 | * which case we'll pop everything we put on the stack, and fetch |
| 1995 | * its value from the memo. |
| 1996 | */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1997 | if (len <= 3 && self->proto >= 2) { |
| 1998 | /* Use TUPLE{1,2,3} opcodes. */ |
| 1999 | if (store_tuple_elements(self, obj, len) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2000 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2001 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2002 | if (PyMemoTable_Get(self->memo, obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2003 | /* pop the len elements */ |
| 2004 | for (i = 0; i < len; i++) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2005 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 2006 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2007 | /* fetch from memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2008 | if (memo_get(self, obj) < 0) |
| 2009 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2010 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2011 | return 0; |
| 2012 | } |
| 2013 | else { /* Not recursive. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2014 | if (_Pickler_Write(self, len2opcode + len, 1) < 0) |
| 2015 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2016 | } |
| 2017 | goto memoize; |
| 2018 | } |
| 2019 | |
| 2020 | /* proto < 2 and len > 0, or proto >= 2 and len > 3. |
| 2021 | * Generate MARK e1 e2 ... TUPLE |
| 2022 | */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2023 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2024 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2025 | |
| 2026 | if (store_tuple_elements(self, obj, len) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2027 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2028 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2029 | if (PyMemoTable_Get(self->memo, obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2030 | /* pop the stack stuff we pushed */ |
| 2031 | if (self->bin) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2032 | if (_Pickler_Write(self, &pop_mark_op, 1) < 0) |
| 2033 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2034 | } |
| 2035 | else { |
| 2036 | /* Note that we pop one more than len, to remove |
| 2037 | * the MARK too. |
| 2038 | */ |
| 2039 | for (i = 0; i <= len; i++) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2040 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 2041 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2042 | } |
| 2043 | /* fetch from memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2044 | if (memo_get(self, obj) < 0) |
| 2045 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2046 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2047 | return 0; |
| 2048 | } |
| 2049 | else { /* Not recursive. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2050 | if (_Pickler_Write(self, &tuple_op, 1) < 0) |
| 2051 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2052 | } |
| 2053 | |
| 2054 | memoize: |
| 2055 | if (memo_put(self, obj) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2056 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2057 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2058 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
| 2061 | /* iter is an iterator giving items, and we batch up chunks of |
| 2062 | * MARK item item ... item APPENDS |
| 2063 | * opcode sequences. Calling code should have arranged to first create an |
| 2064 | * empty list, or list-like object, for the APPENDS to operate on. |
| 2065 | * Returns 0 on success, <0 on error. |
| 2066 | */ |
| 2067 | static int |
| 2068 | batch_list(PicklerObject *self, PyObject *iter) |
| 2069 | { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2070 | PyObject *obj = NULL; |
| 2071 | PyObject *firstitem = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2072 | int i, n; |
| 2073 | |
| 2074 | const char mark_op = MARK; |
| 2075 | const char append_op = APPEND; |
| 2076 | const char appends_op = APPENDS; |
| 2077 | |
| 2078 | assert(iter != NULL); |
| 2079 | |
| 2080 | /* XXX: I think this function could be made faster by avoiding the |
| 2081 | iterator interface and fetching objects directly from list using |
| 2082 | PyList_GET_ITEM. |
| 2083 | */ |
| 2084 | |
| 2085 | if (self->proto == 0) { |
| 2086 | /* APPENDS isn't available; do one at a time. */ |
| 2087 | for (;;) { |
| 2088 | obj = PyIter_Next(iter); |
| 2089 | if (obj == NULL) { |
| 2090 | if (PyErr_Occurred()) |
| 2091 | return -1; |
| 2092 | break; |
| 2093 | } |
| 2094 | i = save(self, obj, 0); |
| 2095 | Py_DECREF(obj); |
| 2096 | if (i < 0) |
| 2097 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2098 | if (_Pickler_Write(self, &append_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2099 | return -1; |
| 2100 | } |
| 2101 | return 0; |
| 2102 | } |
| 2103 | |
| 2104 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 2105 | do { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2106 | /* Get first item */ |
| 2107 | firstitem = PyIter_Next(iter); |
| 2108 | if (firstitem == NULL) { |
| 2109 | if (PyErr_Occurred()) |
| 2110 | goto error; |
| 2111 | |
| 2112 | /* nothing more to add */ |
| 2113 | break; |
| 2114 | } |
| 2115 | |
| 2116 | /* Try to get a second item */ |
| 2117 | obj = PyIter_Next(iter); |
| 2118 | if (obj == NULL) { |
| 2119 | if (PyErr_Occurred()) |
| 2120 | goto error; |
| 2121 | |
| 2122 | /* Only one item to write */ |
| 2123 | if (save(self, firstitem, 0) < 0) |
| 2124 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2125 | if (_Pickler_Write(self, &append_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2126 | goto error; |
| 2127 | Py_CLEAR(firstitem); |
| 2128 | break; |
| 2129 | } |
| 2130 | |
| 2131 | /* More than one item to write */ |
| 2132 | |
| 2133 | /* Pump out MARK, items, APPENDS. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2134 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2135 | goto error; |
| 2136 | |
| 2137 | if (save(self, firstitem, 0) < 0) |
| 2138 | goto error; |
| 2139 | Py_CLEAR(firstitem); |
| 2140 | n = 1; |
| 2141 | |
| 2142 | /* Fetch and save up to BATCHSIZE items */ |
| 2143 | while (obj) { |
| 2144 | if (save(self, obj, 0) < 0) |
| 2145 | goto error; |
| 2146 | Py_CLEAR(obj); |
| 2147 | n += 1; |
| 2148 | |
| 2149 | if (n == BATCHSIZE) |
| 2150 | break; |
| 2151 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2152 | obj = PyIter_Next(iter); |
| 2153 | if (obj == NULL) { |
| 2154 | if (PyErr_Occurred()) |
| 2155 | goto error; |
| 2156 | break; |
| 2157 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2158 | } |
| 2159 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2160 | if (_Pickler_Write(self, &appends_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2161 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2162 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2163 | } while (n == BATCHSIZE); |
| 2164 | return 0; |
| 2165 | |
| 2166 | error: |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2167 | Py_XDECREF(firstitem); |
| 2168 | Py_XDECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2169 | return -1; |
| 2170 | } |
| 2171 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2172 | /* This is a variant of batch_list() above, specialized for lists (with no |
| 2173 | * support for list subclasses). Like batch_list(), we batch up chunks of |
| 2174 | * MARK item item ... item APPENDS |
| 2175 | * opcode sequences. Calling code should have arranged to first create an |
| 2176 | * empty list, or list-like object, for the APPENDS to operate on. |
| 2177 | * Returns 0 on success, -1 on error. |
| 2178 | * |
| 2179 | * This version is considerably faster than batch_list(), if less general. |
| 2180 | * |
| 2181 | * Note that this only works for protocols > 0. |
| 2182 | */ |
| 2183 | static int |
| 2184 | batch_list_exact(PicklerObject *self, PyObject *obj) |
| 2185 | { |
| 2186 | PyObject *item = NULL; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2187 | Py_ssize_t this_batch, total; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2188 | |
| 2189 | const char append_op = APPEND; |
| 2190 | const char appends_op = APPENDS; |
| 2191 | const char mark_op = MARK; |
| 2192 | |
| 2193 | assert(obj != NULL); |
| 2194 | assert(self->proto > 0); |
| 2195 | assert(PyList_CheckExact(obj)); |
| 2196 | |
| 2197 | if (PyList_GET_SIZE(obj) == 1) { |
| 2198 | item = PyList_GET_ITEM(obj, 0); |
| 2199 | if (save(self, item, 0) < 0) |
| 2200 | return -1; |
| 2201 | if (_Pickler_Write(self, &append_op, 1) < 0) |
| 2202 | return -1; |
| 2203 | return 0; |
| 2204 | } |
| 2205 | |
| 2206 | /* Write in batches of BATCHSIZE. */ |
| 2207 | total = 0; |
| 2208 | do { |
| 2209 | this_batch = 0; |
| 2210 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2211 | return -1; |
| 2212 | while (total < PyList_GET_SIZE(obj)) { |
| 2213 | item = PyList_GET_ITEM(obj, total); |
| 2214 | if (save(self, item, 0) < 0) |
| 2215 | return -1; |
| 2216 | total++; |
| 2217 | if (++this_batch == BATCHSIZE) |
| 2218 | break; |
| 2219 | } |
| 2220 | if (_Pickler_Write(self, &appends_op, 1) < 0) |
| 2221 | return -1; |
| 2222 | |
| 2223 | } while (total < PyList_GET_SIZE(obj)); |
| 2224 | |
| 2225 | return 0; |
| 2226 | } |
| 2227 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2228 | static int |
| 2229 | save_list(PicklerObject *self, PyObject *obj) |
| 2230 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2231 | char header[3]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2232 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2233 | int status = 0; |
| 2234 | |
| 2235 | if (self->fast && !fast_save_enter(self, obj)) |
| 2236 | goto error; |
| 2237 | |
| 2238 | /* Create an empty list. */ |
| 2239 | if (self->bin) { |
| 2240 | header[0] = EMPTY_LIST; |
| 2241 | len = 1; |
| 2242 | } |
| 2243 | else { |
| 2244 | header[0] = MARK; |
| 2245 | header[1] = LIST; |
| 2246 | len = 2; |
| 2247 | } |
| 2248 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2249 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2250 | goto error; |
| 2251 | |
| 2252 | /* Get list length, and bow out early if empty. */ |
| 2253 | if ((len = PyList_Size(obj)) < 0) |
| 2254 | goto error; |
| 2255 | |
| 2256 | if (memo_put(self, obj) < 0) |
| 2257 | goto error; |
| 2258 | |
| 2259 | if (len != 0) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2260 | /* Materialize the list elements. */ |
| 2261 | if (PyList_CheckExact(obj) && self->proto > 0) { |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2262 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 2263 | goto error; |
| 2264 | status = batch_list_exact(self, obj); |
| 2265 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2266 | } else { |
| 2267 | PyObject *iter = PyObject_GetIter(obj); |
| 2268 | if (iter == NULL) |
| 2269 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2270 | |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2271 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 2272 | Py_DECREF(iter); |
| 2273 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2274 | } |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2275 | status = batch_list(self, iter); |
| 2276 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2277 | Py_DECREF(iter); |
| 2278 | } |
| 2279 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2280 | if (0) { |
| 2281 | error: |
| 2282 | status = -1; |
| 2283 | } |
| 2284 | |
| 2285 | if (self->fast && !fast_save_leave(self, obj)) |
| 2286 | status = -1; |
| 2287 | |
| 2288 | return status; |
| 2289 | } |
| 2290 | |
| 2291 | /* iter is an iterator giving (key, value) pairs, and we batch up chunks of |
| 2292 | * MARK key value ... key value SETITEMS |
| 2293 | * opcode sequences. Calling code should have arranged to first create an |
| 2294 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 2295 | * Returns 0 on success, <0 on error. |
| 2296 | * |
| 2297 | * This is very much like batch_list(). The difference between saving |
| 2298 | * elements directly, and picking apart two-tuples, is so long-winded at |
| 2299 | * the C level, though, that attempts to combine these routines were too |
| 2300 | * ugly to bear. |
| 2301 | */ |
| 2302 | static int |
| 2303 | batch_dict(PicklerObject *self, PyObject *iter) |
| 2304 | { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2305 | PyObject *obj = NULL; |
| 2306 | PyObject *firstitem = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2307 | int i, n; |
| 2308 | |
| 2309 | const char mark_op = MARK; |
| 2310 | const char setitem_op = SETITEM; |
| 2311 | const char setitems_op = SETITEMS; |
| 2312 | |
| 2313 | assert(iter != NULL); |
| 2314 | |
| 2315 | if (self->proto == 0) { |
| 2316 | /* SETITEMS isn't available; do one at a time. */ |
| 2317 | for (;;) { |
| 2318 | obj = PyIter_Next(iter); |
| 2319 | if (obj == NULL) { |
| 2320 | if (PyErr_Occurred()) |
| 2321 | return -1; |
| 2322 | break; |
| 2323 | } |
| 2324 | if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) { |
| 2325 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2326 | "iterator must return 2-tuples"); |
| 2327 | return -1; |
| 2328 | } |
| 2329 | i = save(self, PyTuple_GET_ITEM(obj, 0), 0); |
| 2330 | if (i >= 0) |
| 2331 | i = save(self, PyTuple_GET_ITEM(obj, 1), 0); |
| 2332 | Py_DECREF(obj); |
| 2333 | if (i < 0) |
| 2334 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2335 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2336 | return -1; |
| 2337 | } |
| 2338 | return 0; |
| 2339 | } |
| 2340 | |
| 2341 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 2342 | do { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2343 | /* Get first item */ |
| 2344 | firstitem = PyIter_Next(iter); |
| 2345 | if (firstitem == NULL) { |
| 2346 | if (PyErr_Occurred()) |
| 2347 | goto error; |
| 2348 | |
| 2349 | /* nothing more to add */ |
| 2350 | break; |
| 2351 | } |
| 2352 | if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) { |
| 2353 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2354 | "iterator must return 2-tuples"); |
| 2355 | goto error; |
| 2356 | } |
| 2357 | |
| 2358 | /* Try to get a second item */ |
| 2359 | obj = PyIter_Next(iter); |
| 2360 | if (obj == NULL) { |
| 2361 | if (PyErr_Occurred()) |
| 2362 | goto error; |
| 2363 | |
| 2364 | /* Only one item to write */ |
| 2365 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 2366 | goto error; |
| 2367 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 2368 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2369 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2370 | goto error; |
| 2371 | Py_CLEAR(firstitem); |
| 2372 | break; |
| 2373 | } |
| 2374 | |
| 2375 | /* More than one item to write */ |
| 2376 | |
| 2377 | /* Pump out MARK, items, SETITEMS. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2378 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2379 | goto error; |
| 2380 | |
| 2381 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 2382 | goto error; |
| 2383 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 2384 | goto error; |
| 2385 | Py_CLEAR(firstitem); |
| 2386 | n = 1; |
| 2387 | |
| 2388 | /* Fetch and save up to BATCHSIZE items */ |
| 2389 | while (obj) { |
| 2390 | if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) { |
| 2391 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2392 | "iterator must return 2-tuples"); |
| 2393 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2394 | } |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2395 | if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 || |
| 2396 | save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0) |
| 2397 | goto error; |
| 2398 | Py_CLEAR(obj); |
| 2399 | n += 1; |
| 2400 | |
| 2401 | if (n == BATCHSIZE) |
| 2402 | break; |
| 2403 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2404 | obj = PyIter_Next(iter); |
| 2405 | if (obj == NULL) { |
| 2406 | if (PyErr_Occurred()) |
| 2407 | goto error; |
| 2408 | break; |
| 2409 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2410 | } |
| 2411 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2412 | if (_Pickler_Write(self, &setitems_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2413 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2414 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2415 | } while (n == BATCHSIZE); |
| 2416 | return 0; |
| 2417 | |
| 2418 | error: |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2419 | Py_XDECREF(firstitem); |
| 2420 | Py_XDECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2421 | return -1; |
| 2422 | } |
| 2423 | |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2424 | /* This is a variant of batch_dict() above that specializes for dicts, with no |
| 2425 | * support for dict subclasses. Like batch_dict(), we batch up chunks of |
| 2426 | * MARK key value ... key value SETITEMS |
| 2427 | * opcode sequences. Calling code should have arranged to first create an |
| 2428 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 2429 | * Returns 0 on success, -1 on error. |
| 2430 | * |
| 2431 | * Note that this currently doesn't work for protocol 0. |
| 2432 | */ |
| 2433 | static int |
| 2434 | batch_dict_exact(PicklerObject *self, PyObject *obj) |
| 2435 | { |
| 2436 | PyObject *key = NULL, *value = NULL; |
| 2437 | int i; |
| 2438 | Py_ssize_t dict_size, ppos = 0; |
| 2439 | |
Alexandre Vassalotti | f70b129 | 2009-05-25 18:00:52 +0000 | [diff] [blame] | 2440 | const char mark_op = MARK; |
| 2441 | const char setitem_op = SETITEM; |
| 2442 | const char setitems_op = SETITEMS; |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2443 | |
| 2444 | assert(obj != NULL); |
| 2445 | assert(self->proto > 0); |
| 2446 | |
| 2447 | dict_size = PyDict_Size(obj); |
| 2448 | |
| 2449 | /* Special-case len(d) == 1 to save space. */ |
| 2450 | if (dict_size == 1) { |
| 2451 | PyDict_Next(obj, &ppos, &key, &value); |
| 2452 | if (save(self, key, 0) < 0) |
| 2453 | return -1; |
| 2454 | if (save(self, value, 0) < 0) |
| 2455 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2456 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2457 | return -1; |
| 2458 | return 0; |
| 2459 | } |
| 2460 | |
| 2461 | /* Write in batches of BATCHSIZE. */ |
| 2462 | do { |
| 2463 | i = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2464 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2465 | return -1; |
| 2466 | while (PyDict_Next(obj, &ppos, &key, &value)) { |
| 2467 | if (save(self, key, 0) < 0) |
| 2468 | return -1; |
| 2469 | if (save(self, value, 0) < 0) |
| 2470 | return -1; |
| 2471 | if (++i == BATCHSIZE) |
| 2472 | break; |
| 2473 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2474 | if (_Pickler_Write(self, &setitems_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2475 | return -1; |
| 2476 | if (PyDict_Size(obj) != dict_size) { |
| 2477 | PyErr_Format( |
| 2478 | PyExc_RuntimeError, |
| 2479 | "dictionary changed size during iteration"); |
| 2480 | return -1; |
| 2481 | } |
| 2482 | |
| 2483 | } while (i == BATCHSIZE); |
| 2484 | return 0; |
| 2485 | } |
| 2486 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2487 | static int |
| 2488 | save_dict(PicklerObject *self, PyObject *obj) |
| 2489 | { |
| 2490 | PyObject *items, *iter; |
| 2491 | char header[3]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2492 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2493 | int status = 0; |
| 2494 | |
| 2495 | if (self->fast && !fast_save_enter(self, obj)) |
| 2496 | goto error; |
| 2497 | |
| 2498 | /* Create an empty dict. */ |
| 2499 | if (self->bin) { |
| 2500 | header[0] = EMPTY_DICT; |
| 2501 | len = 1; |
| 2502 | } |
| 2503 | else { |
| 2504 | header[0] = MARK; |
| 2505 | header[1] = DICT; |
| 2506 | len = 2; |
| 2507 | } |
| 2508 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2509 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2510 | goto error; |
| 2511 | |
| 2512 | /* Get dict size, and bow out early if empty. */ |
| 2513 | if ((len = PyDict_Size(obj)) < 0) |
| 2514 | goto error; |
| 2515 | |
| 2516 | if (memo_put(self, obj) < 0) |
| 2517 | goto error; |
| 2518 | |
| 2519 | if (len != 0) { |
| 2520 | /* Save the dict items. */ |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2521 | if (PyDict_CheckExact(obj) && self->proto > 0) { |
| 2522 | /* We can take certain shortcuts if we know this is a dict and |
| 2523 | not a dict subclass. */ |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2524 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 2525 | goto error; |
| 2526 | status = batch_dict_exact(self, obj); |
| 2527 | Py_LeaveRecursiveCall(); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2528 | } else { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2529 | _Py_IDENTIFIER(items); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2530 | |
| 2531 | items = _PyObject_CallMethodId(obj, &PyId_items, "()"); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2532 | if (items == NULL) |
| 2533 | goto error; |
| 2534 | iter = PyObject_GetIter(items); |
| 2535 | Py_DECREF(items); |
| 2536 | if (iter == NULL) |
| 2537 | goto error; |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2538 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 2539 | Py_DECREF(iter); |
| 2540 | goto error; |
| 2541 | } |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2542 | status = batch_dict(self, iter); |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2543 | Py_LeaveRecursiveCall(); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2544 | Py_DECREF(iter); |
| 2545 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2546 | } |
| 2547 | |
| 2548 | if (0) { |
| 2549 | error: |
| 2550 | status = -1; |
| 2551 | } |
| 2552 | |
| 2553 | if (self->fast && !fast_save_leave(self, obj)) |
| 2554 | status = -1; |
| 2555 | |
| 2556 | return status; |
| 2557 | } |
| 2558 | |
| 2559 | static int |
| 2560 | save_global(PicklerObject *self, PyObject *obj, PyObject *name) |
| 2561 | { |
| 2562 | static PyObject *name_str = NULL; |
| 2563 | PyObject *global_name = NULL; |
| 2564 | PyObject *module_name = NULL; |
| 2565 | PyObject *module = NULL; |
| 2566 | PyObject *cls; |
| 2567 | int status = 0; |
| 2568 | |
| 2569 | const char global_op = GLOBAL; |
| 2570 | |
| 2571 | if (name_str == NULL) { |
| 2572 | name_str = PyUnicode_InternFromString("__name__"); |
| 2573 | if (name_str == NULL) |
| 2574 | goto error; |
| 2575 | } |
| 2576 | |
| 2577 | if (name) { |
| 2578 | global_name = name; |
| 2579 | Py_INCREF(global_name); |
| 2580 | } |
| 2581 | else { |
| 2582 | global_name = PyObject_GetAttr(obj, name_str); |
| 2583 | if (global_name == NULL) |
| 2584 | goto error; |
| 2585 | } |
| 2586 | |
| 2587 | module_name = whichmodule(obj, global_name); |
| 2588 | if (module_name == NULL) |
| 2589 | goto error; |
| 2590 | |
| 2591 | /* XXX: Change to use the import C API directly with level=0 to disallow |
| 2592 | relative imports. |
| 2593 | |
| 2594 | XXX: PyImport_ImportModuleLevel could be used. However, this bypasses |
| 2595 | builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore |
| 2596 | custom import functions (IMHO, this would be a nice security |
| 2597 | feature). The import C API would need to be extended to support the |
| 2598 | extra parameters of __import__ to fix that. */ |
| 2599 | module = PyImport_Import(module_name); |
| 2600 | if (module == NULL) { |
| 2601 | PyErr_Format(PicklingError, |
| 2602 | "Can't pickle %R: import of module %R failed", |
| 2603 | obj, module_name); |
| 2604 | goto error; |
| 2605 | } |
| 2606 | cls = PyObject_GetAttr(module, global_name); |
| 2607 | if (cls == NULL) { |
| 2608 | PyErr_Format(PicklingError, |
| 2609 | "Can't pickle %R: attribute lookup %S.%S failed", |
| 2610 | obj, module_name, global_name); |
| 2611 | goto error; |
| 2612 | } |
| 2613 | if (cls != obj) { |
| 2614 | Py_DECREF(cls); |
| 2615 | PyErr_Format(PicklingError, |
| 2616 | "Can't pickle %R: it's not the same object as %S.%S", |
| 2617 | obj, module_name, global_name); |
| 2618 | goto error; |
| 2619 | } |
| 2620 | Py_DECREF(cls); |
| 2621 | |
| 2622 | if (self->proto >= 2) { |
| 2623 | /* See whether this is in the extension registry, and if |
| 2624 | * so generate an EXT opcode. |
| 2625 | */ |
| 2626 | PyObject *code_obj; /* extension code as Python object */ |
| 2627 | long code; /* extension code as C value */ |
| 2628 | char pdata[5]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2629 | Py_ssize_t n; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2630 | |
| 2631 | PyTuple_SET_ITEM(two_tuple, 0, module_name); |
| 2632 | PyTuple_SET_ITEM(two_tuple, 1, global_name); |
| 2633 | code_obj = PyDict_GetItem(extension_registry, two_tuple); |
| 2634 | /* The object is not registered in the extension registry. |
| 2635 | This is the most likely code path. */ |
| 2636 | if (code_obj == NULL) |
| 2637 | goto gen_global; |
| 2638 | |
| 2639 | /* XXX: pickle.py doesn't check neither the type, nor the range |
| 2640 | of the value returned by the extension_registry. It should for |
| 2641 | consistency. */ |
| 2642 | |
| 2643 | /* Verify code_obj has the right type and value. */ |
| 2644 | if (!PyLong_Check(code_obj)) { |
| 2645 | PyErr_Format(PicklingError, |
| 2646 | "Can't pickle %R: extension code %R isn't an integer", |
| 2647 | obj, code_obj); |
| 2648 | goto error; |
| 2649 | } |
| 2650 | code = PyLong_AS_LONG(code_obj); |
| 2651 | if (code <= 0 || code > 0x7fffffffL) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2652 | if (!PyErr_Occurred()) |
| 2653 | PyErr_Format(PicklingError, |
| 2654 | "Can't pickle %R: extension code %ld is out of range", |
| 2655 | obj, code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2656 | goto error; |
| 2657 | } |
| 2658 | |
| 2659 | /* Generate an EXT opcode. */ |
| 2660 | if (code <= 0xff) { |
| 2661 | pdata[0] = EXT1; |
| 2662 | pdata[1] = (unsigned char)code; |
| 2663 | n = 2; |
| 2664 | } |
| 2665 | else if (code <= 0xffff) { |
| 2666 | pdata[0] = EXT2; |
| 2667 | pdata[1] = (unsigned char)(code & 0xff); |
| 2668 | pdata[2] = (unsigned char)((code >> 8) & 0xff); |
| 2669 | n = 3; |
| 2670 | } |
| 2671 | else { |
| 2672 | pdata[0] = EXT4; |
| 2673 | pdata[1] = (unsigned char)(code & 0xff); |
| 2674 | pdata[2] = (unsigned char)((code >> 8) & 0xff); |
| 2675 | pdata[3] = (unsigned char)((code >> 16) & 0xff); |
| 2676 | pdata[4] = (unsigned char)((code >> 24) & 0xff); |
| 2677 | n = 5; |
| 2678 | } |
| 2679 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2680 | if (_Pickler_Write(self, pdata, n) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2681 | goto error; |
| 2682 | } |
| 2683 | else { |
| 2684 | /* Generate a normal global opcode if we are using a pickle |
| 2685 | protocol <= 2, or if the object is not registered in the |
| 2686 | extension registry. */ |
| 2687 | PyObject *encoded; |
| 2688 | PyObject *(*unicode_encoder)(PyObject *); |
| 2689 | |
| 2690 | gen_global: |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2691 | if (_Pickler_Write(self, &global_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2692 | goto error; |
| 2693 | |
| 2694 | /* Since Python 3.0 now supports non-ASCII identifiers, we encode both |
| 2695 | the module name and the global name using UTF-8. We do so only when |
| 2696 | we are using the pickle protocol newer than version 3. This is to |
| 2697 | ensure compatibility with older Unpickler running on Python 2.x. */ |
| 2698 | if (self->proto >= 3) { |
| 2699 | unicode_encoder = PyUnicode_AsUTF8String; |
| 2700 | } |
| 2701 | else { |
| 2702 | unicode_encoder = PyUnicode_AsASCIIString; |
| 2703 | } |
| 2704 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 2705 | /* For protocol < 3 and if the user didn't request against doing so, |
| 2706 | we convert module names to the old 2.x module names. */ |
| 2707 | if (self->fix_imports) { |
| 2708 | PyObject *key; |
| 2709 | PyObject *item; |
| 2710 | |
| 2711 | key = PyTuple_Pack(2, module_name, global_name); |
| 2712 | if (key == NULL) |
| 2713 | goto error; |
| 2714 | item = PyDict_GetItemWithError(name_mapping_3to2, key); |
| 2715 | Py_DECREF(key); |
| 2716 | if (item) { |
| 2717 | if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
| 2718 | PyErr_Format(PyExc_RuntimeError, |
| 2719 | "_compat_pickle.REVERSE_NAME_MAPPING values " |
| 2720 | "should be 2-tuples, not %.200s", |
| 2721 | Py_TYPE(item)->tp_name); |
| 2722 | goto error; |
| 2723 | } |
| 2724 | Py_CLEAR(module_name); |
| 2725 | Py_CLEAR(global_name); |
| 2726 | module_name = PyTuple_GET_ITEM(item, 0); |
| 2727 | global_name = PyTuple_GET_ITEM(item, 1); |
| 2728 | if (!PyUnicode_Check(module_name) || |
| 2729 | !PyUnicode_Check(global_name)) { |
| 2730 | PyErr_Format(PyExc_RuntimeError, |
| 2731 | "_compat_pickle.REVERSE_NAME_MAPPING values " |
| 2732 | "should be pairs of str, not (%.200s, %.200s)", |
| 2733 | Py_TYPE(module_name)->tp_name, |
| 2734 | Py_TYPE(global_name)->tp_name); |
| 2735 | goto error; |
| 2736 | } |
| 2737 | Py_INCREF(module_name); |
| 2738 | Py_INCREF(global_name); |
| 2739 | } |
| 2740 | else if (PyErr_Occurred()) { |
| 2741 | goto error; |
| 2742 | } |
| 2743 | |
| 2744 | item = PyDict_GetItemWithError(import_mapping_3to2, module_name); |
| 2745 | if (item) { |
| 2746 | if (!PyUnicode_Check(item)) { |
| 2747 | PyErr_Format(PyExc_RuntimeError, |
| 2748 | "_compat_pickle.REVERSE_IMPORT_MAPPING values " |
| 2749 | "should be strings, not %.200s", |
| 2750 | Py_TYPE(item)->tp_name); |
| 2751 | goto error; |
| 2752 | } |
| 2753 | Py_CLEAR(module_name); |
| 2754 | module_name = item; |
| 2755 | Py_INCREF(module_name); |
| 2756 | } |
| 2757 | else if (PyErr_Occurred()) { |
| 2758 | goto error; |
| 2759 | } |
| 2760 | } |
| 2761 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2762 | /* Save the name of the module. */ |
| 2763 | encoded = unicode_encoder(module_name); |
| 2764 | if (encoded == NULL) { |
| 2765 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
| 2766 | PyErr_Format(PicklingError, |
| 2767 | "can't pickle module identifier '%S' using " |
| 2768 | "pickle protocol %i", module_name, self->proto); |
| 2769 | goto error; |
| 2770 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2771 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2772 | PyBytes_GET_SIZE(encoded)) < 0) { |
| 2773 | Py_DECREF(encoded); |
| 2774 | goto error; |
| 2775 | } |
| 2776 | Py_DECREF(encoded); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2777 | if(_Pickler_Write(self, "\n", 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2778 | goto error; |
| 2779 | |
| 2780 | /* Save the name of the module. */ |
| 2781 | encoded = unicode_encoder(global_name); |
| 2782 | if (encoded == NULL) { |
| 2783 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
| 2784 | PyErr_Format(PicklingError, |
| 2785 | "can't pickle global identifier '%S' using " |
| 2786 | "pickle protocol %i", global_name, self->proto); |
| 2787 | goto error; |
| 2788 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2789 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2790 | PyBytes_GET_SIZE(encoded)) < 0) { |
| 2791 | Py_DECREF(encoded); |
| 2792 | goto error; |
| 2793 | } |
| 2794 | Py_DECREF(encoded); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2795 | if(_Pickler_Write(self, "\n", 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2796 | goto error; |
| 2797 | |
| 2798 | /* Memoize the object. */ |
| 2799 | if (memo_put(self, obj) < 0) |
| 2800 | goto error; |
| 2801 | } |
| 2802 | |
| 2803 | if (0) { |
| 2804 | error: |
| 2805 | status = -1; |
| 2806 | } |
| 2807 | Py_XDECREF(module_name); |
| 2808 | Py_XDECREF(global_name); |
| 2809 | Py_XDECREF(module); |
| 2810 | |
| 2811 | return status; |
| 2812 | } |
| 2813 | |
| 2814 | static int |
Łukasz Langa | f3078fb | 2012-03-12 19:46:12 +0100 | [diff] [blame] | 2815 | save_ellipsis(PicklerObject *self, PyObject *obj) |
| 2816 | { |
Łukasz Langa | dbd7825 | 2012-03-12 22:59:11 +0100 | [diff] [blame] | 2817 | PyObject *str = PyUnicode_FromString("Ellipsis"); |
Benjamin Peterson | e80b29b | 2012-03-16 18:45:31 -0500 | [diff] [blame] | 2818 | int res; |
Łukasz Langa | dbd7825 | 2012-03-12 22:59:11 +0100 | [diff] [blame] | 2819 | if (str == NULL) |
Łukasz Langa | cad1a07 | 2012-03-12 23:41:07 +0100 | [diff] [blame] | 2820 | return -1; |
Benjamin Peterson | e80b29b | 2012-03-16 18:45:31 -0500 | [diff] [blame] | 2821 | res = save_global(self, Py_Ellipsis, str); |
| 2822 | Py_DECREF(str); |
| 2823 | return res; |
Łukasz Langa | f3078fb | 2012-03-12 19:46:12 +0100 | [diff] [blame] | 2824 | } |
| 2825 | |
| 2826 | static int |
| 2827 | save_notimplemented(PicklerObject *self, PyObject *obj) |
| 2828 | { |
Łukasz Langa | dbd7825 | 2012-03-12 22:59:11 +0100 | [diff] [blame] | 2829 | PyObject *str = PyUnicode_FromString("NotImplemented"); |
Benjamin Peterson | e80b29b | 2012-03-16 18:45:31 -0500 | [diff] [blame] | 2830 | int res; |
Łukasz Langa | dbd7825 | 2012-03-12 22:59:11 +0100 | [diff] [blame] | 2831 | if (str == NULL) |
Łukasz Langa | cad1a07 | 2012-03-12 23:41:07 +0100 | [diff] [blame] | 2832 | return -1; |
Benjamin Peterson | e80b29b | 2012-03-16 18:45:31 -0500 | [diff] [blame] | 2833 | res = save_global(self, Py_NotImplemented, str); |
| 2834 | Py_DECREF(str); |
| 2835 | return res; |
Łukasz Langa | f3078fb | 2012-03-12 19:46:12 +0100 | [diff] [blame] | 2836 | } |
| 2837 | |
| 2838 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2839 | save_pers(PicklerObject *self, PyObject *obj, PyObject *func) |
| 2840 | { |
| 2841 | PyObject *pid = NULL; |
| 2842 | int status = 0; |
| 2843 | |
| 2844 | const char persid_op = PERSID; |
| 2845 | const char binpersid_op = BINPERSID; |
| 2846 | |
| 2847 | Py_INCREF(obj); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2848 | pid = _Pickler_FastCall(self, func, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2849 | if (pid == NULL) |
| 2850 | return -1; |
| 2851 | |
| 2852 | if (pid != Py_None) { |
| 2853 | if (self->bin) { |
| 2854 | if (save(self, pid, 1) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2855 | _Pickler_Write(self, &binpersid_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2856 | goto error; |
| 2857 | } |
| 2858 | else { |
| 2859 | PyObject *pid_str = NULL; |
| 2860 | char *pid_ascii_bytes; |
| 2861 | Py_ssize_t size; |
| 2862 | |
| 2863 | pid_str = PyObject_Str(pid); |
| 2864 | if (pid_str == NULL) |
| 2865 | goto error; |
| 2866 | |
| 2867 | /* XXX: Should it check whether the persistent id only contains |
| 2868 | ASCII characters? And what if the pid contains embedded |
| 2869 | newlines? */ |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 2870 | pid_ascii_bytes = _PyUnicode_AsStringAndSize(pid_str, &size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2871 | Py_DECREF(pid_str); |
| 2872 | if (pid_ascii_bytes == NULL) |
| 2873 | goto error; |
| 2874 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2875 | if (_Pickler_Write(self, &persid_op, 1) < 0 || |
| 2876 | _Pickler_Write(self, pid_ascii_bytes, size) < 0 || |
| 2877 | _Pickler_Write(self, "\n", 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2878 | goto error; |
| 2879 | } |
| 2880 | status = 1; |
| 2881 | } |
| 2882 | |
| 2883 | if (0) { |
| 2884 | error: |
| 2885 | status = -1; |
| 2886 | } |
| 2887 | Py_XDECREF(pid); |
| 2888 | |
| 2889 | return status; |
| 2890 | } |
| 2891 | |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 2892 | static PyObject * |
| 2893 | get_class(PyObject *obj) |
| 2894 | { |
| 2895 | PyObject *cls; |
| 2896 | static PyObject *str_class; |
| 2897 | |
| 2898 | if (str_class == NULL) { |
| 2899 | str_class = PyUnicode_InternFromString("__class__"); |
| 2900 | if (str_class == NULL) |
| 2901 | return NULL; |
| 2902 | } |
| 2903 | cls = PyObject_GetAttr(obj, str_class); |
| 2904 | if (cls == NULL) { |
| 2905 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 2906 | PyErr_Clear(); |
| 2907 | cls = (PyObject *) Py_TYPE(obj); |
| 2908 | Py_INCREF(cls); |
| 2909 | } |
| 2910 | } |
| 2911 | return cls; |
| 2912 | } |
| 2913 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2914 | /* We're saving obj, and args is the 2-thru-5 tuple returned by the |
| 2915 | * appropriate __reduce__ method for obj. |
| 2916 | */ |
| 2917 | static int |
| 2918 | save_reduce(PicklerObject *self, PyObject *args, PyObject *obj) |
| 2919 | { |
| 2920 | PyObject *callable; |
| 2921 | PyObject *argtup; |
| 2922 | PyObject *state = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 2923 | PyObject *listitems = Py_None; |
| 2924 | PyObject *dictitems = Py_None; |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 2925 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2926 | |
| 2927 | int use_newobj = self->proto >= 2; |
| 2928 | |
| 2929 | const char reduce_op = REDUCE; |
| 2930 | const char build_op = BUILD; |
| 2931 | const char newobj_op = NEWOBJ; |
| 2932 | |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 2933 | size = PyTuple_Size(args); |
| 2934 | if (size < 2 || size > 5) { |
| 2935 | PyErr_SetString(PicklingError, "tuple returned by " |
| 2936 | "__reduce__ must contain 2 through 5 elements"); |
| 2937 | return -1; |
| 2938 | } |
| 2939 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2940 | if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5, |
| 2941 | &callable, &argtup, &state, &listitems, &dictitems)) |
| 2942 | return -1; |
| 2943 | |
| 2944 | if (!PyCallable_Check(callable)) { |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 2945 | PyErr_SetString(PicklingError, "first item of the tuple " |
| 2946 | "returned by __reduce__ must be callable"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2947 | return -1; |
| 2948 | } |
| 2949 | if (!PyTuple_Check(argtup)) { |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 2950 | PyErr_SetString(PicklingError, "second item of the tuple " |
| 2951 | "returned by __reduce__ must be a tuple"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2952 | return -1; |
| 2953 | } |
| 2954 | |
| 2955 | if (state == Py_None) |
| 2956 | state = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 2957 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2958 | if (listitems == Py_None) |
| 2959 | listitems = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 2960 | else if (!PyIter_Check(listitems)) { |
| 2961 | PyErr_Format(PicklingError, "Fourth element of tuple" |
| 2962 | "returned by __reduce__ must be an iterator, not %s", |
| 2963 | Py_TYPE(listitems)->tp_name); |
| 2964 | return -1; |
| 2965 | } |
| 2966 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2967 | if (dictitems == Py_None) |
| 2968 | dictitems = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 2969 | else if (!PyIter_Check(dictitems)) { |
| 2970 | PyErr_Format(PicklingError, "Fifth element of tuple" |
| 2971 | "returned by __reduce__ must be an iterator, not %s", |
| 2972 | Py_TYPE(dictitems)->tp_name); |
| 2973 | return -1; |
| 2974 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2975 | |
| 2976 | /* Protocol 2 special case: if callable's name is __newobj__, use |
| 2977 | NEWOBJ. */ |
| 2978 | if (use_newobj) { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 2979 | static PyObject *newobj_str = NULL, *name_str = NULL; |
| 2980 | PyObject *name; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2981 | |
| 2982 | if (newobj_str == NULL) { |
| 2983 | newobj_str = PyUnicode_InternFromString("__newobj__"); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 2984 | name_str = PyUnicode_InternFromString("__name__"); |
| 2985 | if (newobj_str == NULL || name_str == NULL) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2986 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2987 | } |
| 2988 | |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 2989 | name = PyObject_GetAttr(callable, name_str); |
| 2990 | if (name == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2991 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2992 | PyErr_Clear(); |
| 2993 | else |
| 2994 | return -1; |
| 2995 | use_newobj = 0; |
| 2996 | } |
| 2997 | else { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 2998 | use_newobj = PyUnicode_Check(name) && |
| 2999 | PyUnicode_Compare(name, newobj_str) == 0; |
| 3000 | Py_DECREF(name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3001 | } |
| 3002 | } |
| 3003 | if (use_newobj) { |
| 3004 | PyObject *cls; |
| 3005 | PyObject *newargtup; |
| 3006 | PyObject *obj_class; |
| 3007 | int p; |
| 3008 | |
| 3009 | /* Sanity checks. */ |
| 3010 | if (Py_SIZE(argtup) < 1) { |
| 3011 | PyErr_SetString(PicklingError, "__newobj__ arglist is empty"); |
| 3012 | return -1; |
| 3013 | } |
| 3014 | |
| 3015 | cls = PyTuple_GET_ITEM(argtup, 0); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3016 | if (!PyType_Check(cls)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3017 | PyErr_SetString(PicklingError, "args[0] from " |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3018 | "__newobj__ args is not a type"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3019 | return -1; |
| 3020 | } |
| 3021 | |
| 3022 | if (obj != NULL) { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3023 | obj_class = get_class(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3024 | p = obj_class != cls; /* true iff a problem */ |
| 3025 | Py_DECREF(obj_class); |
| 3026 | if (p) { |
| 3027 | PyErr_SetString(PicklingError, "args[0] from " |
| 3028 | "__newobj__ args has the wrong class"); |
| 3029 | return -1; |
| 3030 | } |
| 3031 | } |
| 3032 | /* XXX: These calls save() are prone to infinite recursion. Imagine |
| 3033 | what happen if the value returned by the __reduce__() method of |
| 3034 | some extension type contains another object of the same type. Ouch! |
| 3035 | |
| 3036 | Here is a quick example, that I ran into, to illustrate what I |
| 3037 | mean: |
| 3038 | |
| 3039 | >>> import pickle, copyreg |
| 3040 | >>> copyreg.dispatch_table.pop(complex) |
| 3041 | >>> pickle.dumps(1+2j) |
| 3042 | Traceback (most recent call last): |
| 3043 | ... |
| 3044 | RuntimeError: maximum recursion depth exceeded |
| 3045 | |
| 3046 | Removing the complex class from copyreg.dispatch_table made the |
| 3047 | __reduce_ex__() method emit another complex object: |
| 3048 | |
| 3049 | >>> (1+1j).__reduce_ex__(2) |
| 3050 | (<function __newobj__ at 0xb7b71c3c>, |
| 3051 | (<class 'complex'>, (1+1j)), None, None, None) |
| 3052 | |
| 3053 | Thus when save() was called on newargstup (the 2nd item) recursion |
| 3054 | ensued. Of course, the bug was in the complex class which had a |
| 3055 | broken __getnewargs__() that emitted another complex object. But, |
| 3056 | the point, here, is it is quite easy to end up with a broken reduce |
| 3057 | function. */ |
| 3058 | |
| 3059 | /* Save the class and its __new__ arguments. */ |
| 3060 | if (save(self, cls, 0) < 0) |
| 3061 | return -1; |
| 3062 | |
| 3063 | newargtup = PyTuple_GetSlice(argtup, 1, Py_SIZE(argtup)); |
| 3064 | if (newargtup == NULL) |
| 3065 | return -1; |
| 3066 | |
| 3067 | p = save(self, newargtup, 0); |
| 3068 | Py_DECREF(newargtup); |
| 3069 | if (p < 0) |
| 3070 | return -1; |
| 3071 | |
| 3072 | /* Add NEWOBJ opcode. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3073 | if (_Pickler_Write(self, &newobj_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3074 | return -1; |
| 3075 | } |
| 3076 | else { /* Not using NEWOBJ. */ |
| 3077 | if (save(self, callable, 0) < 0 || |
| 3078 | save(self, argtup, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3079 | _Pickler_Write(self, &reduce_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3080 | return -1; |
| 3081 | } |
| 3082 | |
| 3083 | /* obj can be NULL when save_reduce() is used directly. A NULL obj means |
| 3084 | the caller do not want to memoize the object. Not particularly useful, |
| 3085 | but that is to mimic the behavior save_reduce() in pickle.py when |
| 3086 | obj is None. */ |
| 3087 | if (obj && memo_put(self, obj) < 0) |
| 3088 | return -1; |
| 3089 | |
| 3090 | if (listitems && batch_list(self, listitems) < 0) |
| 3091 | return -1; |
| 3092 | |
| 3093 | if (dictitems && batch_dict(self, dictitems) < 0) |
| 3094 | return -1; |
| 3095 | |
| 3096 | if (state) { |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 3097 | if (save(self, state, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3098 | _Pickler_Write(self, &build_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3099 | return -1; |
| 3100 | } |
| 3101 | |
| 3102 | return 0; |
| 3103 | } |
| 3104 | |
| 3105 | static int |
| 3106 | save(PicklerObject *self, PyObject *obj, int pers_save) |
| 3107 | { |
| 3108 | PyTypeObject *type; |
| 3109 | PyObject *reduce_func = NULL; |
| 3110 | PyObject *reduce_value = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3111 | int status = 0; |
| 3112 | |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 3113 | if (Py_EnterRecursiveCall(" while pickling an object")) |
Alexandre Vassalotti | dff1834 | 2008-07-13 18:48:30 +0000 | [diff] [blame] | 3114 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3115 | |
| 3116 | /* The extra pers_save argument is necessary to avoid calling save_pers() |
| 3117 | on its returned object. */ |
| 3118 | if (!pers_save && self->pers_func) { |
| 3119 | /* save_pers() returns: |
| 3120 | -1 to signal an error; |
| 3121 | 0 if it did nothing successfully; |
| 3122 | 1 if a persistent id was saved. |
| 3123 | */ |
| 3124 | if ((status = save_pers(self, obj, self->pers_func)) != 0) |
| 3125 | goto done; |
| 3126 | } |
| 3127 | |
| 3128 | type = Py_TYPE(obj); |
| 3129 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3130 | /* The old cPickle had an optimization that used switch-case statement |
| 3131 | dispatching on the first letter of the type name. This has was removed |
| 3132 | since benchmarks shown that this optimization was actually slowing |
| 3133 | things down. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3134 | |
| 3135 | /* Atom types; these aren't memoized, so don't check the memo. */ |
| 3136 | |
| 3137 | if (obj == Py_None) { |
| 3138 | status = save_none(self, obj); |
| 3139 | goto done; |
| 3140 | } |
Łukasz Langa | f3078fb | 2012-03-12 19:46:12 +0100 | [diff] [blame] | 3141 | else if (obj == Py_Ellipsis) { |
| 3142 | status = save_ellipsis(self, obj); |
| 3143 | goto done; |
| 3144 | } |
| 3145 | else if (obj == Py_NotImplemented) { |
| 3146 | status = save_notimplemented(self, obj); |
| 3147 | goto done; |
| 3148 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3149 | else if (obj == Py_False || obj == Py_True) { |
| 3150 | status = save_bool(self, obj); |
| 3151 | goto done; |
| 3152 | } |
| 3153 | else if (type == &PyLong_Type) { |
| 3154 | status = save_long(self, obj); |
| 3155 | goto done; |
| 3156 | } |
| 3157 | else if (type == &PyFloat_Type) { |
| 3158 | status = save_float(self, obj); |
| 3159 | goto done; |
| 3160 | } |
| 3161 | |
| 3162 | /* Check the memo to see if it has the object. If so, generate |
| 3163 | a GET (or BINGET) opcode, instead of pickling the object |
| 3164 | once again. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3165 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3166 | if (memo_get(self, obj) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3167 | goto error; |
| 3168 | goto done; |
| 3169 | } |
| 3170 | |
| 3171 | if (type == &PyBytes_Type) { |
| 3172 | status = save_bytes(self, obj); |
| 3173 | goto done; |
| 3174 | } |
| 3175 | else if (type == &PyUnicode_Type) { |
| 3176 | status = save_unicode(self, obj); |
| 3177 | goto done; |
| 3178 | } |
| 3179 | else if (type == &PyDict_Type) { |
| 3180 | status = save_dict(self, obj); |
| 3181 | goto done; |
| 3182 | } |
| 3183 | else if (type == &PyList_Type) { |
| 3184 | status = save_list(self, obj); |
| 3185 | goto done; |
| 3186 | } |
| 3187 | else if (type == &PyTuple_Type) { |
| 3188 | status = save_tuple(self, obj); |
| 3189 | goto done; |
| 3190 | } |
| 3191 | else if (type == &PyType_Type) { |
| 3192 | status = save_global(self, obj, NULL); |
| 3193 | goto done; |
| 3194 | } |
| 3195 | else if (type == &PyFunction_Type) { |
| 3196 | status = save_global(self, obj, NULL); |
| 3197 | if (status < 0 && PyErr_ExceptionMatches(PickleError)) { |
| 3198 | /* fall back to reduce */ |
| 3199 | PyErr_Clear(); |
| 3200 | } |
| 3201 | else { |
| 3202 | goto done; |
| 3203 | } |
| 3204 | } |
| 3205 | else if (type == &PyCFunction_Type) { |
| 3206 | status = save_global(self, obj, NULL); |
| 3207 | goto done; |
| 3208 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3209 | |
| 3210 | /* XXX: This part needs some unit tests. */ |
| 3211 | |
| 3212 | /* Get a reduction callable, and call it. This may come from |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3213 | * self.dispatch_table, copyreg.dispatch_table, the object's |
| 3214 | * __reduce_ex__ method, or the object's __reduce__ method. |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3215 | */ |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3216 | if (self->dispatch_table == NULL) { |
| 3217 | reduce_func = PyDict_GetItem(dispatch_table, (PyObject *)type); |
| 3218 | /* PyDict_GetItem() unlike PyObject_GetItem() and |
| 3219 | PyObject_GetAttr() returns a borrowed ref */ |
| 3220 | Py_XINCREF(reduce_func); |
| 3221 | } else { |
| 3222 | reduce_func = PyObject_GetItem(self->dispatch_table, (PyObject *)type); |
| 3223 | if (reduce_func == NULL) { |
| 3224 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 3225 | PyErr_Clear(); |
| 3226 | else |
| 3227 | goto error; |
| 3228 | } |
| 3229 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3230 | if (reduce_func != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3231 | Py_INCREF(obj); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3232 | reduce_value = _Pickler_FastCall(self, reduce_func, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3233 | } |
Antoine Pitrou | ffd41d9 | 2011-10-04 09:23:04 +0200 | [diff] [blame] | 3234 | else if (PyType_IsSubtype(type, &PyType_Type)) { |
| 3235 | status = save_global(self, obj, NULL); |
| 3236 | goto done; |
| 3237 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3238 | else { |
| 3239 | static PyObject *reduce_str = NULL; |
| 3240 | static PyObject *reduce_ex_str = NULL; |
| 3241 | |
| 3242 | /* Cache the name of the reduce methods. */ |
| 3243 | if (reduce_str == NULL) { |
| 3244 | reduce_str = PyUnicode_InternFromString("__reduce__"); |
| 3245 | if (reduce_str == NULL) |
| 3246 | goto error; |
| 3247 | reduce_ex_str = PyUnicode_InternFromString("__reduce_ex__"); |
| 3248 | if (reduce_ex_str == NULL) |
| 3249 | goto error; |
| 3250 | } |
| 3251 | |
| 3252 | /* XXX: If the __reduce__ method is defined, __reduce_ex__ is |
| 3253 | automatically defined as __reduce__. While this is convenient, this |
| 3254 | make it impossible to know which method was actually called. Of |
| 3255 | course, this is not a big deal. But still, it would be nice to let |
| 3256 | the user know which method was called when something go |
| 3257 | wrong. Incidentally, this means if __reduce_ex__ is not defined, we |
| 3258 | don't actually have to check for a __reduce__ method. */ |
| 3259 | |
| 3260 | /* Check for a __reduce_ex__ method. */ |
| 3261 | reduce_func = PyObject_GetAttr(obj, reduce_ex_str); |
| 3262 | if (reduce_func != NULL) { |
| 3263 | PyObject *proto; |
| 3264 | proto = PyLong_FromLong(self->proto); |
| 3265 | if (proto != NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3266 | reduce_value = _Pickler_FastCall(self, reduce_func, proto); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3267 | } |
| 3268 | } |
| 3269 | else { |
| 3270 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 3271 | PyErr_Clear(); |
| 3272 | else |
| 3273 | goto error; |
| 3274 | /* Check for a __reduce__ method. */ |
| 3275 | reduce_func = PyObject_GetAttr(obj, reduce_str); |
| 3276 | if (reduce_func != NULL) { |
| 3277 | reduce_value = PyObject_Call(reduce_func, empty_tuple, NULL); |
| 3278 | } |
| 3279 | else { |
| 3280 | PyErr_Format(PicklingError, "can't pickle '%.200s' object: %R", |
| 3281 | type->tp_name, obj); |
| 3282 | goto error; |
| 3283 | } |
| 3284 | } |
| 3285 | } |
| 3286 | |
| 3287 | if (reduce_value == NULL) |
| 3288 | goto error; |
| 3289 | |
| 3290 | if (PyUnicode_Check(reduce_value)) { |
| 3291 | status = save_global(self, obj, reduce_value); |
| 3292 | goto done; |
| 3293 | } |
| 3294 | |
| 3295 | if (!PyTuple_Check(reduce_value)) { |
| 3296 | PyErr_SetString(PicklingError, |
| 3297 | "__reduce__ must return a string or tuple"); |
| 3298 | goto error; |
| 3299 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3300 | |
| 3301 | status = save_reduce(self, reduce_value, obj); |
| 3302 | |
| 3303 | if (0) { |
| 3304 | error: |
| 3305 | status = -1; |
| 3306 | } |
| 3307 | done: |
Alexandre Vassalotti | dff1834 | 2008-07-13 18:48:30 +0000 | [diff] [blame] | 3308 | Py_LeaveRecursiveCall(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3309 | Py_XDECREF(reduce_func); |
| 3310 | Py_XDECREF(reduce_value); |
| 3311 | |
| 3312 | return status; |
| 3313 | } |
| 3314 | |
| 3315 | static int |
| 3316 | dump(PicklerObject *self, PyObject *obj) |
| 3317 | { |
| 3318 | const char stop_op = STOP; |
| 3319 | |
| 3320 | if (self->proto >= 2) { |
| 3321 | char header[2]; |
| 3322 | |
| 3323 | header[0] = PROTO; |
| 3324 | assert(self->proto >= 0 && self->proto < 256); |
| 3325 | header[1] = (unsigned char)self->proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3326 | if (_Pickler_Write(self, header, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3327 | return -1; |
| 3328 | } |
| 3329 | |
| 3330 | if (save(self, obj, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3331 | _Pickler_Write(self, &stop_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3332 | return -1; |
| 3333 | |
| 3334 | return 0; |
| 3335 | } |
| 3336 | |
| 3337 | PyDoc_STRVAR(Pickler_clear_memo_doc, |
| 3338 | "clear_memo() -> None. Clears the pickler's \"memo\"." |
| 3339 | "\n" |
| 3340 | "The memo is the data structure that remembers which objects the\n" |
| 3341 | "pickler has already seen, so that shared or recursive objects are\n" |
| 3342 | "pickled by reference and not by value. This method is useful when\n" |
| 3343 | "re-using picklers."); |
| 3344 | |
| 3345 | static PyObject * |
| 3346 | Pickler_clear_memo(PicklerObject *self) |
| 3347 | { |
| 3348 | if (self->memo) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3349 | PyMemoTable_Clear(self->memo); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3350 | |
| 3351 | Py_RETURN_NONE; |
| 3352 | } |
| 3353 | |
| 3354 | PyDoc_STRVAR(Pickler_dump_doc, |
| 3355 | "dump(obj) -> None. Write a pickled representation of obj to the open file."); |
| 3356 | |
| 3357 | static PyObject * |
| 3358 | Pickler_dump(PicklerObject *self, PyObject *args) |
| 3359 | { |
| 3360 | PyObject *obj; |
| 3361 | |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 3362 | /* Check whether the Pickler was initialized correctly (issue3664). |
| 3363 | Developers often forget to call __init__() in their subclasses, which |
| 3364 | would trigger a segfault without this check. */ |
| 3365 | if (self->write == NULL) { |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 3366 | PyErr_Format(PicklingError, |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 3367 | "Pickler.__init__() was not called by %s.__init__()", |
| 3368 | Py_TYPE(self)->tp_name); |
| 3369 | return NULL; |
| 3370 | } |
| 3371 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3372 | if (!PyArg_ParseTuple(args, "O:dump", &obj)) |
| 3373 | return NULL; |
| 3374 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3375 | if (_Pickler_ClearBuffer(self) < 0) |
| 3376 | return NULL; |
| 3377 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3378 | if (dump(self, obj) < 0) |
| 3379 | return NULL; |
| 3380 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3381 | if (_Pickler_FlushToFile(self) < 0) |
| 3382 | return NULL; |
| 3383 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3384 | Py_RETURN_NONE; |
| 3385 | } |
| 3386 | |
| 3387 | static struct PyMethodDef Pickler_methods[] = { |
| 3388 | {"dump", (PyCFunction)Pickler_dump, METH_VARARGS, |
| 3389 | Pickler_dump_doc}, |
| 3390 | {"clear_memo", (PyCFunction)Pickler_clear_memo, METH_NOARGS, |
| 3391 | Pickler_clear_memo_doc}, |
| 3392 | {NULL, NULL} /* sentinel */ |
| 3393 | }; |
| 3394 | |
| 3395 | static void |
| 3396 | Pickler_dealloc(PicklerObject *self) |
| 3397 | { |
| 3398 | PyObject_GC_UnTrack(self); |
| 3399 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3400 | Py_XDECREF(self->output_buffer); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3401 | Py_XDECREF(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3402 | Py_XDECREF(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3403 | Py_XDECREF(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3404 | Py_XDECREF(self->arg); |
| 3405 | Py_XDECREF(self->fast_memo); |
| 3406 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3407 | PyMemoTable_Del(self->memo); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3408 | |
| 3409 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 3410 | } |
| 3411 | |
| 3412 | static int |
| 3413 | Pickler_traverse(PicklerObject *self, visitproc visit, void *arg) |
| 3414 | { |
| 3415 | Py_VISIT(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3416 | Py_VISIT(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3417 | Py_VISIT(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3418 | Py_VISIT(self->arg); |
| 3419 | Py_VISIT(self->fast_memo); |
| 3420 | return 0; |
| 3421 | } |
| 3422 | |
| 3423 | static int |
| 3424 | Pickler_clear(PicklerObject *self) |
| 3425 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3426 | Py_CLEAR(self->output_buffer); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3427 | Py_CLEAR(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3428 | Py_CLEAR(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3429 | Py_CLEAR(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3430 | Py_CLEAR(self->arg); |
| 3431 | Py_CLEAR(self->fast_memo); |
| 3432 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3433 | if (self->memo != NULL) { |
| 3434 | PyMemoTable *memo = self->memo; |
| 3435 | self->memo = NULL; |
| 3436 | PyMemoTable_Del(memo); |
| 3437 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3438 | return 0; |
| 3439 | } |
| 3440 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3441 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3442 | PyDoc_STRVAR(Pickler_doc, |
| 3443 | "Pickler(file, protocol=None)" |
| 3444 | "\n" |
| 3445 | "This takes a binary file for writing a pickle data stream.\n" |
| 3446 | "\n" |
| 3447 | "The optional protocol argument tells the pickler to use the\n" |
| 3448 | "given protocol; supported protocols are 0, 1, 2, 3. The default\n" |
| 3449 | "protocol is 3; a backward-incompatible protocol designed for\n" |
| 3450 | "Python 3.0.\n" |
| 3451 | "\n" |
| 3452 | "Specifying a negative protocol version selects the highest\n" |
| 3453 | "protocol version supported. The higher the protocol used, the\n" |
| 3454 | "more recent the version of Python needed to read the pickle\n" |
| 3455 | "produced.\n" |
| 3456 | "\n" |
| 3457 | "The file argument must have a write() method that accepts a single\n" |
| 3458 | "bytes argument. It can thus be a file object opened for binary\n" |
| 3459 | "writing, a io.BytesIO instance, or any other custom object that\n" |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3460 | "meets this interface.\n" |
| 3461 | "\n" |
| 3462 | "If fix_imports is True and protocol is less than 3, pickle will try to\n" |
| 3463 | "map the new Python 3.x names to the old module names used in Python\n" |
| 3464 | "2.x, so that the pickle data stream is readable with Python 2.x.\n"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3465 | |
| 3466 | static int |
| 3467 | Pickler_init(PicklerObject *self, PyObject *args, PyObject *kwds) |
| 3468 | { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3469 | static char *kwlist[] = {"file", "protocol", "fix_imports", 0}; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3470 | PyObject *file; |
| 3471 | PyObject *proto_obj = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3472 | PyObject *fix_imports = Py_True; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3473 | _Py_IDENTIFIER(persistent_id); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3474 | _Py_IDENTIFIER(dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3475 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3476 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OO:Pickler", |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3477 | kwlist, &file, &proto_obj, &fix_imports)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3478 | return -1; |
| 3479 | |
| 3480 | /* In case of multiple __init__() calls, clear previous content. */ |
| 3481 | if (self->write != NULL) |
| 3482 | (void)Pickler_clear(self); |
| 3483 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3484 | if (_Pickler_SetProtocol(self, proto_obj, fix_imports) < 0) |
| 3485 | return -1; |
| 3486 | |
| 3487 | if (_Pickler_SetOutputStream(self, file) < 0) |
| 3488 | return -1; |
| 3489 | |
| 3490 | /* memo and output_buffer may have already been created in _Pickler_New */ |
| 3491 | if (self->memo == NULL) { |
| 3492 | self->memo = PyMemoTable_New(); |
| 3493 | if (self->memo == NULL) |
| 3494 | return -1; |
| 3495 | } |
| 3496 | self->output_len = 0; |
| 3497 | if (self->output_buffer == NULL) { |
| 3498 | self->max_output_len = WRITE_BUF_SIZE; |
| 3499 | self->output_buffer = PyBytes_FromStringAndSize(NULL, |
| 3500 | self->max_output_len); |
| 3501 | if (self->output_buffer == NULL) |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3502 | return -1; |
| 3503 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3504 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3505 | self->arg = NULL; |
| 3506 | self->fast = 0; |
| 3507 | self->fast_nesting = 0; |
| 3508 | self->fast_memo = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3509 | self->pers_func = NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 3510 | if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) { |
| 3511 | self->pers_func = _PyObject_GetAttrId((PyObject *)self, |
| 3512 | &PyId_persistent_id); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3513 | if (self->pers_func == NULL) |
| 3514 | return -1; |
| 3515 | } |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3516 | self->dispatch_table = NULL; |
| 3517 | if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) { |
| 3518 | self->dispatch_table = _PyObject_GetAttrId((PyObject *)self, |
| 3519 | &PyId_dispatch_table); |
| 3520 | if (self->dispatch_table == NULL) |
| 3521 | return -1; |
| 3522 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3523 | return 0; |
| 3524 | } |
| 3525 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3526 | /* Define a proxy object for the Pickler's internal memo object. This is to |
| 3527 | * avoid breaking code like: |
| 3528 | * pickler.memo.clear() |
| 3529 | * and |
| 3530 | * pickler.memo = saved_memo |
| 3531 | * Is this a good idea? Not really, but we don't want to break code that uses |
| 3532 | * it. Note that we don't implement the entire mapping API here. This is |
| 3533 | * intentional, as these should be treated as black-box implementation details. |
| 3534 | */ |
| 3535 | |
| 3536 | typedef struct { |
| 3537 | PyObject_HEAD |
| 3538 | PicklerObject *pickler; /* Pickler whose memo table we're proxying. */ |
| 3539 | } PicklerMemoProxyObject; |
| 3540 | |
| 3541 | PyDoc_STRVAR(pmp_clear_doc, |
| 3542 | "memo.clear() -> None. Remove all items from memo."); |
| 3543 | |
| 3544 | static PyObject * |
| 3545 | pmp_clear(PicklerMemoProxyObject *self) |
| 3546 | { |
| 3547 | if (self->pickler->memo) |
| 3548 | PyMemoTable_Clear(self->pickler->memo); |
| 3549 | Py_RETURN_NONE; |
| 3550 | } |
| 3551 | |
| 3552 | PyDoc_STRVAR(pmp_copy_doc, |
| 3553 | "memo.copy() -> new_memo. Copy the memo to a new object."); |
| 3554 | |
| 3555 | static PyObject * |
| 3556 | pmp_copy(PicklerMemoProxyObject *self) |
| 3557 | { |
| 3558 | Py_ssize_t i; |
| 3559 | PyMemoTable *memo; |
| 3560 | PyObject *new_memo = PyDict_New(); |
| 3561 | if (new_memo == NULL) |
| 3562 | return NULL; |
| 3563 | |
| 3564 | memo = self->pickler->memo; |
| 3565 | for (i = 0; i < memo->mt_allocated; ++i) { |
| 3566 | PyMemoEntry entry = memo->mt_table[i]; |
| 3567 | if (entry.me_key != NULL) { |
| 3568 | int status; |
| 3569 | PyObject *key, *value; |
| 3570 | |
| 3571 | key = PyLong_FromVoidPtr(entry.me_key); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3572 | value = Py_BuildValue("nO", entry.me_value, entry.me_key); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3573 | |
| 3574 | if (key == NULL || value == NULL) { |
| 3575 | Py_XDECREF(key); |
| 3576 | Py_XDECREF(value); |
| 3577 | goto error; |
| 3578 | } |
| 3579 | status = PyDict_SetItem(new_memo, key, value); |
| 3580 | Py_DECREF(key); |
| 3581 | Py_DECREF(value); |
| 3582 | if (status < 0) |
| 3583 | goto error; |
| 3584 | } |
| 3585 | } |
| 3586 | return new_memo; |
| 3587 | |
| 3588 | error: |
| 3589 | Py_XDECREF(new_memo); |
| 3590 | return NULL; |
| 3591 | } |
| 3592 | |
| 3593 | PyDoc_STRVAR(pmp_reduce_doc, |
| 3594 | "memo.__reduce__(). Pickling support."); |
| 3595 | |
| 3596 | static PyObject * |
| 3597 | pmp_reduce(PicklerMemoProxyObject *self, PyObject *args) |
| 3598 | { |
| 3599 | PyObject *reduce_value, *dict_args; |
| 3600 | PyObject *contents = pmp_copy(self); |
| 3601 | if (contents == NULL) |
| 3602 | return NULL; |
| 3603 | |
| 3604 | reduce_value = PyTuple_New(2); |
| 3605 | if (reduce_value == NULL) { |
| 3606 | Py_DECREF(contents); |
| 3607 | return NULL; |
| 3608 | } |
| 3609 | dict_args = PyTuple_New(1); |
| 3610 | if (dict_args == NULL) { |
| 3611 | Py_DECREF(contents); |
| 3612 | Py_DECREF(reduce_value); |
| 3613 | return NULL; |
| 3614 | } |
| 3615 | PyTuple_SET_ITEM(dict_args, 0, contents); |
| 3616 | Py_INCREF((PyObject *)&PyDict_Type); |
| 3617 | PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type); |
| 3618 | PyTuple_SET_ITEM(reduce_value, 1, dict_args); |
| 3619 | return reduce_value; |
| 3620 | } |
| 3621 | |
| 3622 | static PyMethodDef picklerproxy_methods[] = { |
| 3623 | {"clear", (PyCFunction)pmp_clear, METH_NOARGS, pmp_clear_doc}, |
| 3624 | {"copy", (PyCFunction)pmp_copy, METH_NOARGS, pmp_copy_doc}, |
| 3625 | {"__reduce__", (PyCFunction)pmp_reduce, METH_VARARGS, pmp_reduce_doc}, |
| 3626 | {NULL, NULL} /* sentinel */ |
| 3627 | }; |
| 3628 | |
| 3629 | static void |
| 3630 | PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self) |
| 3631 | { |
| 3632 | PyObject_GC_UnTrack(self); |
| 3633 | Py_XDECREF(self->pickler); |
| 3634 | PyObject_GC_Del((PyObject *)self); |
| 3635 | } |
| 3636 | |
| 3637 | static int |
| 3638 | PicklerMemoProxy_traverse(PicklerMemoProxyObject *self, |
| 3639 | visitproc visit, void *arg) |
| 3640 | { |
| 3641 | Py_VISIT(self->pickler); |
| 3642 | return 0; |
| 3643 | } |
| 3644 | |
| 3645 | static int |
| 3646 | PicklerMemoProxy_clear(PicklerMemoProxyObject *self) |
| 3647 | { |
| 3648 | Py_CLEAR(self->pickler); |
| 3649 | return 0; |
| 3650 | } |
| 3651 | |
| 3652 | static PyTypeObject PicklerMemoProxyType = { |
| 3653 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3654 | "_pickle.PicklerMemoProxy", /*tp_name*/ |
| 3655 | sizeof(PicklerMemoProxyObject), /*tp_basicsize*/ |
| 3656 | 0, |
| 3657 | (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */ |
| 3658 | 0, /* tp_print */ |
| 3659 | 0, /* tp_getattr */ |
| 3660 | 0, /* tp_setattr */ |
| 3661 | 0, /* tp_compare */ |
| 3662 | 0, /* tp_repr */ |
| 3663 | 0, /* tp_as_number */ |
| 3664 | 0, /* tp_as_sequence */ |
| 3665 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 3666 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3667 | 0, /* tp_call */ |
| 3668 | 0, /* tp_str */ |
| 3669 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3670 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 3671 | 0, /* tp_as_buffer */ |
| 3672 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 3673 | 0, /* tp_doc */ |
| 3674 | (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */ |
| 3675 | (inquiry)PicklerMemoProxy_clear, /* tp_clear */ |
| 3676 | 0, /* tp_richcompare */ |
| 3677 | 0, /* tp_weaklistoffset */ |
| 3678 | 0, /* tp_iter */ |
| 3679 | 0, /* tp_iternext */ |
| 3680 | picklerproxy_methods, /* tp_methods */ |
| 3681 | }; |
| 3682 | |
| 3683 | static PyObject * |
| 3684 | PicklerMemoProxy_New(PicklerObject *pickler) |
| 3685 | { |
| 3686 | PicklerMemoProxyObject *self; |
| 3687 | |
| 3688 | self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType); |
| 3689 | if (self == NULL) |
| 3690 | return NULL; |
| 3691 | Py_INCREF(pickler); |
| 3692 | self->pickler = pickler; |
| 3693 | PyObject_GC_Track(self); |
| 3694 | return (PyObject *)self; |
| 3695 | } |
| 3696 | |
| 3697 | /*****************************************************************************/ |
| 3698 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3699 | static PyObject * |
| 3700 | Pickler_get_memo(PicklerObject *self) |
| 3701 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3702 | return PicklerMemoProxy_New(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3703 | } |
| 3704 | |
| 3705 | static int |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3706 | Pickler_set_memo(PicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3707 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3708 | PyMemoTable *new_memo = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3709 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3710 | if (obj == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3711 | PyErr_SetString(PyExc_TypeError, |
| 3712 | "attribute deletion is not supported"); |
| 3713 | return -1; |
| 3714 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3715 | |
| 3716 | if (Py_TYPE(obj) == &PicklerMemoProxyType) { |
| 3717 | PicklerObject *pickler = |
| 3718 | ((PicklerMemoProxyObject *)obj)->pickler; |
| 3719 | |
| 3720 | new_memo = PyMemoTable_Copy(pickler->memo); |
| 3721 | if (new_memo == NULL) |
| 3722 | return -1; |
| 3723 | } |
| 3724 | else if (PyDict_Check(obj)) { |
| 3725 | Py_ssize_t i = 0; |
| 3726 | PyObject *key, *value; |
| 3727 | |
| 3728 | new_memo = PyMemoTable_New(); |
| 3729 | if (new_memo == NULL) |
| 3730 | return -1; |
| 3731 | |
| 3732 | while (PyDict_Next(obj, &i, &key, &value)) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3733 | Py_ssize_t memo_id; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3734 | PyObject *memo_obj; |
| 3735 | |
| 3736 | if (!PyTuple_Check(value) || Py_SIZE(value) != 2) { |
| 3737 | PyErr_SetString(PyExc_TypeError, |
| 3738 | "'memo' values must be 2-item tuples"); |
| 3739 | goto error; |
| 3740 | } |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3741 | memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0)); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3742 | if (memo_id == -1 && PyErr_Occurred()) |
| 3743 | goto error; |
| 3744 | memo_obj = PyTuple_GET_ITEM(value, 1); |
| 3745 | if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0) |
| 3746 | goto error; |
| 3747 | } |
| 3748 | } |
| 3749 | else { |
| 3750 | PyErr_Format(PyExc_TypeError, |
| 3751 | "'memo' attribute must be an PicklerMemoProxy object" |
| 3752 | "or dict, not %.200s", Py_TYPE(obj)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3753 | return -1; |
| 3754 | } |
| 3755 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3756 | PyMemoTable_Del(self->memo); |
| 3757 | self->memo = new_memo; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3758 | |
| 3759 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3760 | |
| 3761 | error: |
| 3762 | if (new_memo) |
| 3763 | PyMemoTable_Del(new_memo); |
| 3764 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3765 | } |
| 3766 | |
| 3767 | static PyObject * |
| 3768 | Pickler_get_persid(PicklerObject *self) |
| 3769 | { |
| 3770 | if (self->pers_func == NULL) |
| 3771 | PyErr_SetString(PyExc_AttributeError, "persistent_id"); |
| 3772 | else |
| 3773 | Py_INCREF(self->pers_func); |
| 3774 | return self->pers_func; |
| 3775 | } |
| 3776 | |
| 3777 | static int |
| 3778 | Pickler_set_persid(PicklerObject *self, PyObject *value) |
| 3779 | { |
| 3780 | PyObject *tmp; |
| 3781 | |
| 3782 | if (value == NULL) { |
| 3783 | PyErr_SetString(PyExc_TypeError, |
| 3784 | "attribute deletion is not supported"); |
| 3785 | return -1; |
| 3786 | } |
| 3787 | if (!PyCallable_Check(value)) { |
| 3788 | PyErr_SetString(PyExc_TypeError, |
| 3789 | "persistent_id must be a callable taking one argument"); |
| 3790 | return -1; |
| 3791 | } |
| 3792 | |
| 3793 | tmp = self->pers_func; |
| 3794 | Py_INCREF(value); |
| 3795 | self->pers_func = value; |
| 3796 | Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */ |
| 3797 | |
| 3798 | return 0; |
| 3799 | } |
| 3800 | |
| 3801 | static PyMemberDef Pickler_members[] = { |
| 3802 | {"bin", T_INT, offsetof(PicklerObject, bin)}, |
| 3803 | {"fast", T_INT, offsetof(PicklerObject, fast)}, |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3804 | {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)}, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3805 | {NULL} |
| 3806 | }; |
| 3807 | |
| 3808 | static PyGetSetDef Pickler_getsets[] = { |
| 3809 | {"memo", (getter)Pickler_get_memo, |
| 3810 | (setter)Pickler_set_memo}, |
| 3811 | {"persistent_id", (getter)Pickler_get_persid, |
| 3812 | (setter)Pickler_set_persid}, |
| 3813 | {NULL} |
| 3814 | }; |
| 3815 | |
| 3816 | static PyTypeObject Pickler_Type = { |
| 3817 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3818 | "_pickle.Pickler" , /*tp_name*/ |
| 3819 | sizeof(PicklerObject), /*tp_basicsize*/ |
| 3820 | 0, /*tp_itemsize*/ |
| 3821 | (destructor)Pickler_dealloc, /*tp_dealloc*/ |
| 3822 | 0, /*tp_print*/ |
| 3823 | 0, /*tp_getattr*/ |
| 3824 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 3825 | 0, /*tp_reserved*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3826 | 0, /*tp_repr*/ |
| 3827 | 0, /*tp_as_number*/ |
| 3828 | 0, /*tp_as_sequence*/ |
| 3829 | 0, /*tp_as_mapping*/ |
| 3830 | 0, /*tp_hash*/ |
| 3831 | 0, /*tp_call*/ |
| 3832 | 0, /*tp_str*/ |
| 3833 | 0, /*tp_getattro*/ |
| 3834 | 0, /*tp_setattro*/ |
| 3835 | 0, /*tp_as_buffer*/ |
| 3836 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 3837 | Pickler_doc, /*tp_doc*/ |
| 3838 | (traverseproc)Pickler_traverse, /*tp_traverse*/ |
| 3839 | (inquiry)Pickler_clear, /*tp_clear*/ |
| 3840 | 0, /*tp_richcompare*/ |
| 3841 | 0, /*tp_weaklistoffset*/ |
| 3842 | 0, /*tp_iter*/ |
| 3843 | 0, /*tp_iternext*/ |
| 3844 | Pickler_methods, /*tp_methods*/ |
| 3845 | Pickler_members, /*tp_members*/ |
| 3846 | Pickler_getsets, /*tp_getset*/ |
| 3847 | 0, /*tp_base*/ |
| 3848 | 0, /*tp_dict*/ |
| 3849 | 0, /*tp_descr_get*/ |
| 3850 | 0, /*tp_descr_set*/ |
| 3851 | 0, /*tp_dictoffset*/ |
| 3852 | (initproc)Pickler_init, /*tp_init*/ |
| 3853 | PyType_GenericAlloc, /*tp_alloc*/ |
| 3854 | PyType_GenericNew, /*tp_new*/ |
| 3855 | PyObject_GC_Del, /*tp_free*/ |
| 3856 | 0, /*tp_is_gc*/ |
| 3857 | }; |
| 3858 | |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 3859 | /* Temporary helper for calling self.find_class(). |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3860 | |
| 3861 | XXX: It would be nice to able to avoid Python function call overhead, by |
| 3862 | using directly the C version of find_class(), when find_class() is not |
| 3863 | overridden by a subclass. Although, this could become rather hackish. A |
| 3864 | simpler optimization would be to call the C function when self is not a |
| 3865 | subclass instance. */ |
| 3866 | static PyObject * |
| 3867 | find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name) |
| 3868 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3869 | _Py_IDENTIFIER(find_class); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3870 | |
| 3871 | return _PyObject_CallMethodId((PyObject *)self, &PyId_find_class, "OO", |
| 3872 | module_name, global_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3873 | } |
| 3874 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3875 | static Py_ssize_t |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3876 | marker(UnpicklerObject *self) |
| 3877 | { |
| 3878 | if (self->num_marks < 1) { |
| 3879 | PyErr_SetString(UnpicklingError, "could not find MARK"); |
| 3880 | return -1; |
| 3881 | } |
| 3882 | |
| 3883 | return self->marks[--self->num_marks]; |
| 3884 | } |
| 3885 | |
| 3886 | static int |
| 3887 | load_none(UnpicklerObject *self) |
| 3888 | { |
| 3889 | PDATA_APPEND(self->stack, Py_None, -1); |
| 3890 | return 0; |
| 3891 | } |
| 3892 | |
| 3893 | static int |
| 3894 | bad_readline(void) |
| 3895 | { |
| 3896 | PyErr_SetString(UnpicklingError, "pickle data was truncated"); |
| 3897 | return -1; |
| 3898 | } |
| 3899 | |
| 3900 | static int |
| 3901 | load_int(UnpicklerObject *self) |
| 3902 | { |
| 3903 | PyObject *value; |
| 3904 | char *endptr, *s; |
| 3905 | Py_ssize_t len; |
| 3906 | long x; |
| 3907 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3908 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3909 | return -1; |
| 3910 | if (len < 2) |
| 3911 | return bad_readline(); |
| 3912 | |
| 3913 | errno = 0; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 3914 | /* XXX: Should the base argument of strtol() be explicitly set to 10? |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3915 | XXX(avassalotti): Should this uses PyOS_strtol()? */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3916 | x = strtol(s, &endptr, 0); |
| 3917 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3918 | if (errno || (*endptr != '\n' && *endptr != '\0')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3919 | /* Hm, maybe we've got something long. Let's try reading |
| 3920 | * it as a Python long object. */ |
| 3921 | errno = 0; |
| 3922 | /* XXX: Same thing about the base here. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3923 | value = PyLong_FromString(s, NULL, 0); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3924 | if (value == NULL) { |
| 3925 | PyErr_SetString(PyExc_ValueError, |
| 3926 | "could not convert string to int"); |
| 3927 | return -1; |
| 3928 | } |
| 3929 | } |
| 3930 | else { |
| 3931 | if (len == 3 && (x == 0 || x == 1)) { |
| 3932 | if ((value = PyBool_FromLong(x)) == NULL) |
| 3933 | return -1; |
| 3934 | } |
| 3935 | else { |
| 3936 | if ((value = PyLong_FromLong(x)) == NULL) |
| 3937 | return -1; |
| 3938 | } |
| 3939 | } |
| 3940 | |
| 3941 | PDATA_PUSH(self->stack, value, -1); |
| 3942 | return 0; |
| 3943 | } |
| 3944 | |
| 3945 | static int |
| 3946 | load_bool(UnpicklerObject *self, PyObject *boolean) |
| 3947 | { |
| 3948 | assert(boolean == Py_True || boolean == Py_False); |
| 3949 | PDATA_APPEND(self->stack, boolean, -1); |
| 3950 | return 0; |
| 3951 | } |
| 3952 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3953 | /* s contains x bytes of an unsigned little-endian integer. Return its value |
| 3954 | * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX. |
| 3955 | */ |
| 3956 | static Py_ssize_t |
| 3957 | calc_binsize(char *bytes, int size) |
| 3958 | { |
| 3959 | unsigned char *s = (unsigned char *)bytes; |
| 3960 | size_t x = 0; |
| 3961 | |
| 3962 | assert(size == 4); |
| 3963 | |
| 3964 | x = (size_t) s[0]; |
| 3965 | x |= (size_t) s[1] << 8; |
| 3966 | x |= (size_t) s[2] << 16; |
| 3967 | x |= (size_t) s[3] << 24; |
| 3968 | |
| 3969 | if (x > PY_SSIZE_T_MAX) |
| 3970 | return -1; |
| 3971 | else |
| 3972 | return (Py_ssize_t) x; |
| 3973 | } |
| 3974 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3975 | /* s contains x bytes of a little-endian integer. Return its value as a |
| 3976 | * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian |
| 3977 | * int, but when x is 4 it's a signed one. This is an historical source |
| 3978 | * of x-platform bugs. |
| 3979 | */ |
| 3980 | static long |
| 3981 | calc_binint(char *bytes, int size) |
| 3982 | { |
| 3983 | unsigned char *s = (unsigned char *)bytes; |
| 3984 | int i = size; |
| 3985 | long x = 0; |
| 3986 | |
| 3987 | for (i = 0; i < size; i++) { |
| 3988 | x |= (long)s[i] << (i * 8); |
| 3989 | } |
| 3990 | |
| 3991 | /* Unlike BININT1 and BININT2, BININT (more accurately BININT4) |
| 3992 | * is signed, so on a box with longs bigger than 4 bytes we need |
| 3993 | * to extend a BININT's sign bit to the full width. |
| 3994 | */ |
| 3995 | if (SIZEOF_LONG > 4 && size == 4) { |
| 3996 | x |= -(x & (1L << 31)); |
| 3997 | } |
| 3998 | |
| 3999 | return x; |
| 4000 | } |
| 4001 | |
| 4002 | static int |
| 4003 | load_binintx(UnpicklerObject *self, char *s, int size) |
| 4004 | { |
| 4005 | PyObject *value; |
| 4006 | long x; |
| 4007 | |
| 4008 | x = calc_binint(s, size); |
| 4009 | |
| 4010 | if ((value = PyLong_FromLong(x)) == NULL) |
| 4011 | return -1; |
| 4012 | |
| 4013 | PDATA_PUSH(self->stack, value, -1); |
| 4014 | return 0; |
| 4015 | } |
| 4016 | |
| 4017 | static int |
| 4018 | load_binint(UnpicklerObject *self) |
| 4019 | { |
| 4020 | char *s; |
| 4021 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4022 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4023 | return -1; |
| 4024 | |
| 4025 | return load_binintx(self, s, 4); |
| 4026 | } |
| 4027 | |
| 4028 | static int |
| 4029 | load_binint1(UnpicklerObject *self) |
| 4030 | { |
| 4031 | char *s; |
| 4032 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4033 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4034 | return -1; |
| 4035 | |
| 4036 | return load_binintx(self, s, 1); |
| 4037 | } |
| 4038 | |
| 4039 | static int |
| 4040 | load_binint2(UnpicklerObject *self) |
| 4041 | { |
| 4042 | char *s; |
| 4043 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4044 | if (_Unpickler_Read(self, &s, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4045 | return -1; |
| 4046 | |
| 4047 | return load_binintx(self, s, 2); |
| 4048 | } |
| 4049 | |
| 4050 | static int |
| 4051 | load_long(UnpicklerObject *self) |
| 4052 | { |
| 4053 | PyObject *value; |
Alexandre Vassalotti | 446f7ff | 2009-01-23 04:43:46 +0000 | [diff] [blame] | 4054 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4055 | Py_ssize_t len; |
| 4056 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4057 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4058 | return -1; |
| 4059 | if (len < 2) |
| 4060 | return bad_readline(); |
| 4061 | |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 4062 | /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove |
| 4063 | the 'L' before calling PyLong_FromString. In order to maintain |
| 4064 | compatibility with Python 3.0.0, we don't actually *require* |
| 4065 | the 'L' to be present. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4066 | if (s[len-2] == 'L') |
Alexandre Vassalotti | 446f7ff | 2009-01-23 04:43:46 +0000 | [diff] [blame] | 4067 | s[len-2] = '\0'; |
Alexandre Vassalotti | e4bccb7 | 2009-01-24 01:47:57 +0000 | [diff] [blame] | 4068 | /* XXX: Should the base argument explicitly set to 10? */ |
| 4069 | value = PyLong_FromString(s, NULL, 0); |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 4070 | if (value == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4071 | return -1; |
| 4072 | |
| 4073 | PDATA_PUSH(self->stack, value, -1); |
| 4074 | return 0; |
| 4075 | } |
| 4076 | |
| 4077 | /* 'size' bytes contain the # of bytes of little-endian 256's-complement |
| 4078 | * data following. |
| 4079 | */ |
| 4080 | static int |
| 4081 | load_counted_long(UnpicklerObject *self, int size) |
| 4082 | { |
| 4083 | PyObject *value; |
| 4084 | char *nbytes; |
| 4085 | char *pdata; |
| 4086 | |
| 4087 | assert(size == 1 || size == 4); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4088 | if (_Unpickler_Read(self, &nbytes, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4089 | return -1; |
| 4090 | |
| 4091 | size = calc_binint(nbytes, size); |
| 4092 | if (size < 0) { |
| 4093 | /* Corrupt or hostile pickle -- we never write one like this */ |
| 4094 | PyErr_SetString(UnpicklingError, |
| 4095 | "LONG pickle has negative byte count"); |
| 4096 | return -1; |
| 4097 | } |
| 4098 | |
| 4099 | if (size == 0) |
| 4100 | value = PyLong_FromLong(0L); |
| 4101 | else { |
| 4102 | /* Read the raw little-endian bytes and convert. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4103 | if (_Unpickler_Read(self, &pdata, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4104 | return -1; |
| 4105 | value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size, |
| 4106 | 1 /* little endian */ , 1 /* signed */ ); |
| 4107 | } |
| 4108 | if (value == NULL) |
| 4109 | return -1; |
| 4110 | PDATA_PUSH(self->stack, value, -1); |
| 4111 | return 0; |
| 4112 | } |
| 4113 | |
| 4114 | static int |
| 4115 | load_float(UnpicklerObject *self) |
| 4116 | { |
| 4117 | PyObject *value; |
| 4118 | char *endptr, *s; |
| 4119 | Py_ssize_t len; |
| 4120 | double d; |
| 4121 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4122 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4123 | return -1; |
| 4124 | if (len < 2) |
| 4125 | return bad_readline(); |
| 4126 | |
| 4127 | errno = 0; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4128 | d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError); |
| 4129 | if (d == -1.0 && PyErr_Occurred()) |
| 4130 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4131 | if ((endptr[0] != '\n') && (endptr[0] != '\0')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4132 | PyErr_SetString(PyExc_ValueError, "could not convert string to float"); |
| 4133 | return -1; |
| 4134 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4135 | value = PyFloat_FromDouble(d); |
| 4136 | if (value == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4137 | return -1; |
| 4138 | |
| 4139 | PDATA_PUSH(self->stack, value, -1); |
| 4140 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4141 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4142 | |
| 4143 | static int |
| 4144 | load_binfloat(UnpicklerObject *self) |
| 4145 | { |
| 4146 | PyObject *value; |
| 4147 | double x; |
| 4148 | char *s; |
| 4149 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4150 | if (_Unpickler_Read(self, &s, 8) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4151 | return -1; |
| 4152 | |
| 4153 | x = _PyFloat_Unpack8((unsigned char *)s, 0); |
| 4154 | if (x == -1.0 && PyErr_Occurred()) |
| 4155 | return -1; |
| 4156 | |
| 4157 | if ((value = PyFloat_FromDouble(x)) == NULL) |
| 4158 | return -1; |
| 4159 | |
| 4160 | PDATA_PUSH(self->stack, value, -1); |
| 4161 | return 0; |
| 4162 | } |
| 4163 | |
| 4164 | static int |
| 4165 | load_string(UnpicklerObject *self) |
| 4166 | { |
| 4167 | PyObject *bytes; |
| 4168 | PyObject *str = NULL; |
| 4169 | Py_ssize_t len; |
| 4170 | char *s, *p; |
| 4171 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4172 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4173 | return -1; |
| 4174 | if (len < 3) |
| 4175 | return bad_readline(); |
| 4176 | if ((s = strdup(s)) == NULL) { |
| 4177 | PyErr_NoMemory(); |
| 4178 | return -1; |
| 4179 | } |
| 4180 | |
| 4181 | /* Strip outermost quotes */ |
| 4182 | while (s[len - 1] <= ' ') |
| 4183 | len--; |
| 4184 | if (s[0] == '"' && s[len - 1] == '"') { |
| 4185 | s[len - 1] = '\0'; |
| 4186 | p = s + 1; |
| 4187 | len -= 2; |
| 4188 | } |
| 4189 | else if (s[0] == '\'' && s[len - 1] == '\'') { |
| 4190 | s[len - 1] = '\0'; |
| 4191 | p = s + 1; |
| 4192 | len -= 2; |
| 4193 | } |
| 4194 | else { |
| 4195 | free(s); |
| 4196 | PyErr_SetString(PyExc_ValueError, "insecure string pickle"); |
| 4197 | return -1; |
| 4198 | } |
| 4199 | |
| 4200 | /* Use the PyBytes API to decode the string, since that is what is used |
| 4201 | to encode, and then coerce the result to Unicode. */ |
| 4202 | bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL); |
| 4203 | free(s); |
| 4204 | if (bytes == NULL) |
| 4205 | return -1; |
| 4206 | str = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors); |
| 4207 | Py_DECREF(bytes); |
| 4208 | if (str == NULL) |
| 4209 | return -1; |
| 4210 | |
| 4211 | PDATA_PUSH(self->stack, str, -1); |
| 4212 | return 0; |
| 4213 | } |
| 4214 | |
| 4215 | static int |
| 4216 | load_binbytes(UnpicklerObject *self) |
| 4217 | { |
| 4218 | PyObject *bytes; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4219 | Py_ssize_t x; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4220 | char *s; |
| 4221 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4222 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4223 | return -1; |
| 4224 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4225 | x = calc_binsize(s, 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4226 | if (x < 0) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4227 | PyErr_Format(PyExc_OverflowError, |
| 4228 | "BINBYTES exceeds system's maximum size of %zd bytes", |
| 4229 | PY_SSIZE_T_MAX |
| 4230 | ); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4231 | return -1; |
| 4232 | } |
| 4233 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4234 | if (_Unpickler_Read(self, &s, x) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4235 | return -1; |
| 4236 | bytes = PyBytes_FromStringAndSize(s, x); |
| 4237 | if (bytes == NULL) |
| 4238 | return -1; |
| 4239 | |
| 4240 | PDATA_PUSH(self->stack, bytes, -1); |
| 4241 | return 0; |
| 4242 | } |
| 4243 | |
| 4244 | static int |
| 4245 | load_short_binbytes(UnpicklerObject *self) |
| 4246 | { |
| 4247 | PyObject *bytes; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4248 | Py_ssize_t x; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4249 | char *s; |
| 4250 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4251 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4252 | return -1; |
| 4253 | |
| 4254 | x = (unsigned char)s[0]; |
| 4255 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4256 | if (_Unpickler_Read(self, &s, x) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4257 | return -1; |
| 4258 | |
| 4259 | bytes = PyBytes_FromStringAndSize(s, x); |
| 4260 | if (bytes == NULL) |
| 4261 | return -1; |
| 4262 | |
| 4263 | PDATA_PUSH(self->stack, bytes, -1); |
| 4264 | return 0; |
| 4265 | } |
| 4266 | |
| 4267 | static int |
| 4268 | load_binstring(UnpicklerObject *self) |
| 4269 | { |
| 4270 | PyObject *str; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4271 | Py_ssize_t x; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4272 | char *s; |
| 4273 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4274 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4275 | return -1; |
| 4276 | |
| 4277 | x = calc_binint(s, 4); |
| 4278 | if (x < 0) { |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 4279 | PyErr_SetString(UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4280 | "BINSTRING pickle has negative byte count"); |
| 4281 | return -1; |
| 4282 | } |
| 4283 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4284 | if (_Unpickler_Read(self, &s, x) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4285 | return -1; |
| 4286 | |
| 4287 | /* Convert Python 2.x strings to unicode. */ |
| 4288 | str = PyUnicode_Decode(s, x, self->encoding, self->errors); |
| 4289 | if (str == NULL) |
| 4290 | return -1; |
| 4291 | |
| 4292 | PDATA_PUSH(self->stack, str, -1); |
| 4293 | return 0; |
| 4294 | } |
| 4295 | |
| 4296 | static int |
| 4297 | load_short_binstring(UnpicklerObject *self) |
| 4298 | { |
| 4299 | PyObject *str; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4300 | Py_ssize_t x; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4301 | char *s; |
| 4302 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4303 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4304 | return -1; |
| 4305 | |
| 4306 | x = (unsigned char)s[0]; |
| 4307 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4308 | if (_Unpickler_Read(self, &s, x) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4309 | return -1; |
| 4310 | |
| 4311 | /* Convert Python 2.x strings to unicode. */ |
| 4312 | str = PyUnicode_Decode(s, x, self->encoding, self->errors); |
| 4313 | if (str == NULL) |
| 4314 | return -1; |
| 4315 | |
| 4316 | PDATA_PUSH(self->stack, str, -1); |
| 4317 | return 0; |
| 4318 | } |
| 4319 | |
| 4320 | static int |
| 4321 | load_unicode(UnpicklerObject *self) |
| 4322 | { |
| 4323 | PyObject *str; |
| 4324 | Py_ssize_t len; |
| 4325 | char *s; |
| 4326 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4327 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4328 | return -1; |
| 4329 | if (len < 1) |
| 4330 | return bad_readline(); |
| 4331 | |
| 4332 | str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL); |
| 4333 | if (str == NULL) |
| 4334 | return -1; |
| 4335 | |
| 4336 | PDATA_PUSH(self->stack, str, -1); |
| 4337 | return 0; |
| 4338 | } |
| 4339 | |
| 4340 | static int |
| 4341 | load_binunicode(UnpicklerObject *self) |
| 4342 | { |
| 4343 | PyObject *str; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4344 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4345 | char *s; |
| 4346 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4347 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4348 | return -1; |
| 4349 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4350 | size = calc_binsize(s, 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4351 | if (size < 0) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4352 | PyErr_Format(PyExc_OverflowError, |
| 4353 | "BINUNICODE exceeds system's maximum size of %zd bytes", |
| 4354 | PY_SSIZE_T_MAX |
| 4355 | ); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4356 | return -1; |
| 4357 | } |
| 4358 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4359 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4360 | if (_Unpickler_Read(self, &s, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4361 | return -1; |
| 4362 | |
Victor Stinner | 485fb56 | 2010-04-13 11:07:24 +0000 | [diff] [blame] | 4363 | str = PyUnicode_DecodeUTF8(s, size, "surrogatepass"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4364 | if (str == NULL) |
| 4365 | return -1; |
| 4366 | |
| 4367 | PDATA_PUSH(self->stack, str, -1); |
| 4368 | return 0; |
| 4369 | } |
| 4370 | |
| 4371 | static int |
| 4372 | load_tuple(UnpicklerObject *self) |
| 4373 | { |
| 4374 | PyObject *tuple; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4375 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4376 | |
| 4377 | if ((i = marker(self)) < 0) |
| 4378 | return -1; |
| 4379 | |
| 4380 | tuple = Pdata_poptuple(self->stack, i); |
| 4381 | if (tuple == NULL) |
| 4382 | return -1; |
| 4383 | PDATA_PUSH(self->stack, tuple, -1); |
| 4384 | return 0; |
| 4385 | } |
| 4386 | |
| 4387 | static int |
| 4388 | load_counted_tuple(UnpicklerObject *self, int len) |
| 4389 | { |
| 4390 | PyObject *tuple; |
| 4391 | |
| 4392 | tuple = PyTuple_New(len); |
| 4393 | if (tuple == NULL) |
| 4394 | return -1; |
| 4395 | |
| 4396 | while (--len >= 0) { |
| 4397 | PyObject *item; |
| 4398 | |
| 4399 | PDATA_POP(self->stack, item); |
| 4400 | if (item == NULL) |
| 4401 | return -1; |
| 4402 | PyTuple_SET_ITEM(tuple, len, item); |
| 4403 | } |
| 4404 | PDATA_PUSH(self->stack, tuple, -1); |
| 4405 | return 0; |
| 4406 | } |
| 4407 | |
| 4408 | static int |
| 4409 | load_empty_list(UnpicklerObject *self) |
| 4410 | { |
| 4411 | PyObject *list; |
| 4412 | |
| 4413 | if ((list = PyList_New(0)) == NULL) |
| 4414 | return -1; |
| 4415 | PDATA_PUSH(self->stack, list, -1); |
| 4416 | return 0; |
| 4417 | } |
| 4418 | |
| 4419 | static int |
| 4420 | load_empty_dict(UnpicklerObject *self) |
| 4421 | { |
| 4422 | PyObject *dict; |
| 4423 | |
| 4424 | if ((dict = PyDict_New()) == NULL) |
| 4425 | return -1; |
| 4426 | PDATA_PUSH(self->stack, dict, -1); |
| 4427 | return 0; |
| 4428 | } |
| 4429 | |
| 4430 | static int |
| 4431 | load_list(UnpicklerObject *self) |
| 4432 | { |
| 4433 | PyObject *list; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4434 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4435 | |
| 4436 | if ((i = marker(self)) < 0) |
| 4437 | return -1; |
| 4438 | |
| 4439 | list = Pdata_poplist(self->stack, i); |
| 4440 | if (list == NULL) |
| 4441 | return -1; |
| 4442 | PDATA_PUSH(self->stack, list, -1); |
| 4443 | return 0; |
| 4444 | } |
| 4445 | |
| 4446 | static int |
| 4447 | load_dict(UnpicklerObject *self) |
| 4448 | { |
| 4449 | PyObject *dict, *key, *value; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4450 | Py_ssize_t i, j, k; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4451 | |
| 4452 | if ((i = marker(self)) < 0) |
| 4453 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4454 | j = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4455 | |
| 4456 | if ((dict = PyDict_New()) == NULL) |
| 4457 | return -1; |
| 4458 | |
| 4459 | for (k = i + 1; k < j; k += 2) { |
| 4460 | key = self->stack->data[k - 1]; |
| 4461 | value = self->stack->data[k]; |
| 4462 | if (PyDict_SetItem(dict, key, value) < 0) { |
| 4463 | Py_DECREF(dict); |
| 4464 | return -1; |
| 4465 | } |
| 4466 | } |
| 4467 | Pdata_clear(self->stack, i); |
| 4468 | PDATA_PUSH(self->stack, dict, -1); |
| 4469 | return 0; |
| 4470 | } |
| 4471 | |
| 4472 | static PyObject * |
| 4473 | instantiate(PyObject *cls, PyObject *args) |
| 4474 | { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 4475 | PyObject *result = NULL; |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 4476 | _Py_IDENTIFIER(__getinitargs__); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 4477 | /* Caller must assure args are a tuple. Normally, args come from |
| 4478 | Pdata_poptuple which packs objects from the top of the stack |
| 4479 | into a newly created tuple. */ |
| 4480 | assert(PyTuple_Check(args)); |
| 4481 | if (Py_SIZE(args) > 0 || !PyType_Check(cls) || |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 4482 | _PyObject_HasAttrId(cls, &PyId___getinitargs__)) { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 4483 | result = PyObject_CallObject(cls, args); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4484 | } |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 4485 | else { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4486 | _Py_IDENTIFIER(__new__); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 4487 | |
| 4488 | result = _PyObject_CallMethodId(cls, &PyId___new__, "O", cls); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 4489 | } |
| 4490 | return result; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4491 | } |
| 4492 | |
| 4493 | static int |
| 4494 | load_obj(UnpicklerObject *self) |
| 4495 | { |
| 4496 | PyObject *cls, *args, *obj = NULL; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4497 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4498 | |
| 4499 | if ((i = marker(self)) < 0) |
| 4500 | return -1; |
| 4501 | |
| 4502 | args = Pdata_poptuple(self->stack, i + 1); |
| 4503 | if (args == NULL) |
| 4504 | return -1; |
| 4505 | |
| 4506 | PDATA_POP(self->stack, cls); |
| 4507 | if (cls) { |
| 4508 | obj = instantiate(cls, args); |
| 4509 | Py_DECREF(cls); |
| 4510 | } |
| 4511 | Py_DECREF(args); |
| 4512 | if (obj == NULL) |
| 4513 | return -1; |
| 4514 | |
| 4515 | PDATA_PUSH(self->stack, obj, -1); |
| 4516 | return 0; |
| 4517 | } |
| 4518 | |
| 4519 | static int |
| 4520 | load_inst(UnpicklerObject *self) |
| 4521 | { |
| 4522 | PyObject *cls = NULL; |
| 4523 | PyObject *args = NULL; |
| 4524 | PyObject *obj = NULL; |
| 4525 | PyObject *module_name; |
| 4526 | PyObject *class_name; |
| 4527 | Py_ssize_t len; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4528 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4529 | char *s; |
| 4530 | |
| 4531 | if ((i = marker(self)) < 0) |
| 4532 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4533 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4534 | return -1; |
| 4535 | if (len < 2) |
| 4536 | return bad_readline(); |
| 4537 | |
| 4538 | /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII |
| 4539 | identifiers are permitted in Python 3.0, since the INST opcode is only |
| 4540 | supported by older protocols on Python 2.x. */ |
| 4541 | module_name = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
| 4542 | if (module_name == NULL) |
| 4543 | return -1; |
| 4544 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4545 | if ((len = _Unpickler_Readline(self, &s)) >= 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4546 | if (len < 2) |
| 4547 | return bad_readline(); |
| 4548 | class_name = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 4549 | if (class_name != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4550 | cls = find_class(self, module_name, class_name); |
| 4551 | Py_DECREF(class_name); |
| 4552 | } |
| 4553 | } |
| 4554 | Py_DECREF(module_name); |
| 4555 | |
| 4556 | if (cls == NULL) |
| 4557 | return -1; |
| 4558 | |
| 4559 | if ((args = Pdata_poptuple(self->stack, i)) != NULL) { |
| 4560 | obj = instantiate(cls, args); |
| 4561 | Py_DECREF(args); |
| 4562 | } |
| 4563 | Py_DECREF(cls); |
| 4564 | |
| 4565 | if (obj == NULL) |
| 4566 | return -1; |
| 4567 | |
| 4568 | PDATA_PUSH(self->stack, obj, -1); |
| 4569 | return 0; |
| 4570 | } |
| 4571 | |
| 4572 | static int |
| 4573 | load_newobj(UnpicklerObject *self) |
| 4574 | { |
| 4575 | PyObject *args = NULL; |
| 4576 | PyObject *clsraw = NULL; |
| 4577 | PyTypeObject *cls; /* clsraw cast to its true type */ |
| 4578 | PyObject *obj; |
| 4579 | |
| 4580 | /* Stack is ... cls argtuple, and we want to call |
| 4581 | * cls.__new__(cls, *argtuple). |
| 4582 | */ |
| 4583 | PDATA_POP(self->stack, args); |
| 4584 | if (args == NULL) |
| 4585 | goto error; |
| 4586 | if (!PyTuple_Check(args)) { |
| 4587 | PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg " "tuple."); |
| 4588 | goto error; |
| 4589 | } |
| 4590 | |
| 4591 | PDATA_POP(self->stack, clsraw); |
| 4592 | cls = (PyTypeObject *)clsraw; |
| 4593 | if (cls == NULL) |
| 4594 | goto error; |
| 4595 | if (!PyType_Check(cls)) { |
| 4596 | PyErr_SetString(UnpicklingError, "NEWOBJ class argument " |
| 4597 | "isn't a type object"); |
| 4598 | goto error; |
| 4599 | } |
| 4600 | if (cls->tp_new == NULL) { |
| 4601 | PyErr_SetString(UnpicklingError, "NEWOBJ class argument " |
| 4602 | "has NULL tp_new"); |
| 4603 | goto error; |
| 4604 | } |
| 4605 | |
| 4606 | /* Call __new__. */ |
| 4607 | obj = cls->tp_new(cls, args, NULL); |
| 4608 | if (obj == NULL) |
| 4609 | goto error; |
| 4610 | |
| 4611 | Py_DECREF(args); |
| 4612 | Py_DECREF(clsraw); |
| 4613 | PDATA_PUSH(self->stack, obj, -1); |
| 4614 | return 0; |
| 4615 | |
| 4616 | error: |
| 4617 | Py_XDECREF(args); |
| 4618 | Py_XDECREF(clsraw); |
| 4619 | return -1; |
| 4620 | } |
| 4621 | |
| 4622 | static int |
| 4623 | load_global(UnpicklerObject *self) |
| 4624 | { |
| 4625 | PyObject *global = NULL; |
| 4626 | PyObject *module_name; |
| 4627 | PyObject *global_name; |
| 4628 | Py_ssize_t len; |
| 4629 | char *s; |
| 4630 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4631 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4632 | return -1; |
| 4633 | if (len < 2) |
| 4634 | return bad_readline(); |
| 4635 | module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict"); |
| 4636 | if (!module_name) |
| 4637 | return -1; |
| 4638 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4639 | if ((len = _Unpickler_Readline(self, &s)) >= 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4640 | if (len < 2) { |
| 4641 | Py_DECREF(module_name); |
| 4642 | return bad_readline(); |
| 4643 | } |
| 4644 | global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict"); |
| 4645 | if (global_name) { |
| 4646 | global = find_class(self, module_name, global_name); |
| 4647 | Py_DECREF(global_name); |
| 4648 | } |
| 4649 | } |
| 4650 | Py_DECREF(module_name); |
| 4651 | |
| 4652 | if (global == NULL) |
| 4653 | return -1; |
| 4654 | PDATA_PUSH(self->stack, global, -1); |
| 4655 | return 0; |
| 4656 | } |
| 4657 | |
| 4658 | static int |
| 4659 | load_persid(UnpicklerObject *self) |
| 4660 | { |
| 4661 | PyObject *pid; |
| 4662 | Py_ssize_t len; |
| 4663 | char *s; |
| 4664 | |
| 4665 | if (self->pers_func) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4666 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4667 | return -1; |
| 4668 | if (len < 2) |
| 4669 | return bad_readline(); |
| 4670 | |
| 4671 | pid = PyBytes_FromStringAndSize(s, len - 1); |
| 4672 | if (pid == NULL) |
| 4673 | return -1; |
| 4674 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4675 | /* Ugh... this does not leak since _Unpickler_FastCall() steals the |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4676 | reference to pid first. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4677 | pid = _Unpickler_FastCall(self, self->pers_func, pid); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4678 | if (pid == NULL) |
| 4679 | return -1; |
| 4680 | |
| 4681 | PDATA_PUSH(self->stack, pid, -1); |
| 4682 | return 0; |
| 4683 | } |
| 4684 | else { |
| 4685 | PyErr_SetString(UnpicklingError, |
| 4686 | "A load persistent id instruction was encountered,\n" |
| 4687 | "but no persistent_load function was specified."); |
| 4688 | return -1; |
| 4689 | } |
| 4690 | } |
| 4691 | |
| 4692 | static int |
| 4693 | load_binpersid(UnpicklerObject *self) |
| 4694 | { |
| 4695 | PyObject *pid; |
| 4696 | |
| 4697 | if (self->pers_func) { |
| 4698 | PDATA_POP(self->stack, pid); |
| 4699 | if (pid == NULL) |
| 4700 | return -1; |
| 4701 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4702 | /* Ugh... this does not leak since _Unpickler_FastCall() steals the |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4703 | reference to pid first. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4704 | pid = _Unpickler_FastCall(self, self->pers_func, pid); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4705 | if (pid == NULL) |
| 4706 | return -1; |
| 4707 | |
| 4708 | PDATA_PUSH(self->stack, pid, -1); |
| 4709 | return 0; |
| 4710 | } |
| 4711 | else { |
| 4712 | PyErr_SetString(UnpicklingError, |
| 4713 | "A load persistent id instruction was encountered,\n" |
| 4714 | "but no persistent_load function was specified."); |
| 4715 | return -1; |
| 4716 | } |
| 4717 | } |
| 4718 | |
| 4719 | static int |
| 4720 | load_pop(UnpicklerObject *self) |
| 4721 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4722 | Py_ssize_t len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4723 | |
| 4724 | /* Note that we split the (pickle.py) stack into two stacks, |
| 4725 | * an object stack and a mark stack. We have to be clever and |
| 4726 | * pop the right one. We do this by looking at the top of the |
Collin Winter | 8ca69de | 2009-05-26 16:53:41 +0000 | [diff] [blame] | 4727 | * mark stack first, and only signalling a stack underflow if |
| 4728 | * the object stack is empty and the mark stack doesn't match |
| 4729 | * our expectations. |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4730 | */ |
Collin Winter | 8ca69de | 2009-05-26 16:53:41 +0000 | [diff] [blame] | 4731 | if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4732 | self->num_marks--; |
Antoine Pitrou | 01a15ea | 2010-01-07 17:57:31 +0000 | [diff] [blame] | 4733 | } else if (len > 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4734 | len--; |
| 4735 | Py_DECREF(self->stack->data[len]); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4736 | Py_SIZE(self->stack) = len; |
Collin Winter | 8ca69de | 2009-05-26 16:53:41 +0000 | [diff] [blame] | 4737 | } else { |
| 4738 | return stack_underflow(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4739 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4740 | return 0; |
| 4741 | } |
| 4742 | |
| 4743 | static int |
| 4744 | load_pop_mark(UnpicklerObject *self) |
| 4745 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4746 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4747 | |
| 4748 | if ((i = marker(self)) < 0) |
| 4749 | return -1; |
| 4750 | |
| 4751 | Pdata_clear(self->stack, i); |
| 4752 | |
| 4753 | return 0; |
| 4754 | } |
| 4755 | |
| 4756 | static int |
| 4757 | load_dup(UnpicklerObject *self) |
| 4758 | { |
| 4759 | PyObject *last; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4760 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4761 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4762 | if ((len = Py_SIZE(self->stack)) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4763 | return stack_underflow(); |
| 4764 | last = self->stack->data[len - 1]; |
| 4765 | PDATA_APPEND(self->stack, last, -1); |
| 4766 | return 0; |
| 4767 | } |
| 4768 | |
| 4769 | static int |
| 4770 | load_get(UnpicklerObject *self) |
| 4771 | { |
| 4772 | PyObject *key, *value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4773 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4774 | Py_ssize_t len; |
| 4775 | char *s; |
| 4776 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4777 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4778 | return -1; |
| 4779 | if (len < 2) |
| 4780 | return bad_readline(); |
| 4781 | |
| 4782 | key = PyLong_FromString(s, NULL, 10); |
| 4783 | if (key == NULL) |
| 4784 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4785 | idx = PyLong_AsSsize_t(key); |
| 4786 | if (idx == -1 && PyErr_Occurred()) { |
| 4787 | Py_DECREF(key); |
| 4788 | return -1; |
| 4789 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4790 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4791 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4792 | if (value == NULL) { |
| 4793 | if (!PyErr_Occurred()) |
| 4794 | PyErr_SetObject(PyExc_KeyError, key); |
| 4795 | Py_DECREF(key); |
| 4796 | return -1; |
| 4797 | } |
| 4798 | Py_DECREF(key); |
| 4799 | |
| 4800 | PDATA_APPEND(self->stack, value, -1); |
| 4801 | return 0; |
| 4802 | } |
| 4803 | |
| 4804 | static int |
| 4805 | load_binget(UnpicklerObject *self) |
| 4806 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4807 | PyObject *value; |
| 4808 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4809 | char *s; |
| 4810 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4811 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4812 | return -1; |
| 4813 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4814 | idx = Py_CHARMASK(s[0]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4815 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4816 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4817 | if (value == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4818 | PyObject *key = PyLong_FromSsize_t(idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4819 | if (!PyErr_Occurred()) |
| 4820 | PyErr_SetObject(PyExc_KeyError, key); |
| 4821 | Py_DECREF(key); |
| 4822 | return -1; |
| 4823 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4824 | |
| 4825 | PDATA_APPEND(self->stack, value, -1); |
| 4826 | return 0; |
| 4827 | } |
| 4828 | |
| 4829 | static int |
| 4830 | load_long_binget(UnpicklerObject *self) |
| 4831 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4832 | PyObject *value; |
| 4833 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4834 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4835 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4836 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4837 | return -1; |
| 4838 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4839 | idx = calc_binsize(s, 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4840 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4841 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4842 | if (value == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4843 | PyObject *key = PyLong_FromSsize_t(idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4844 | if (!PyErr_Occurred()) |
| 4845 | PyErr_SetObject(PyExc_KeyError, key); |
| 4846 | Py_DECREF(key); |
| 4847 | return -1; |
| 4848 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4849 | |
| 4850 | PDATA_APPEND(self->stack, value, -1); |
| 4851 | return 0; |
| 4852 | } |
| 4853 | |
| 4854 | /* Push an object from the extension registry (EXT[124]). nbytes is |
| 4855 | * the number of bytes following the opcode, holding the index (code) value. |
| 4856 | */ |
| 4857 | static int |
| 4858 | load_extension(UnpicklerObject *self, int nbytes) |
| 4859 | { |
| 4860 | char *codebytes; /* the nbytes bytes after the opcode */ |
| 4861 | long code; /* calc_binint returns long */ |
| 4862 | PyObject *py_code; /* code as a Python int */ |
| 4863 | PyObject *obj; /* the object to push */ |
| 4864 | PyObject *pair; /* (module_name, class_name) */ |
| 4865 | PyObject *module_name, *class_name; |
| 4866 | |
| 4867 | assert(nbytes == 1 || nbytes == 2 || nbytes == 4); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4868 | if (_Unpickler_Read(self, &codebytes, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4869 | return -1; |
| 4870 | code = calc_binint(codebytes, nbytes); |
| 4871 | if (code <= 0) { /* note that 0 is forbidden */ |
| 4872 | /* Corrupt or hostile pickle. */ |
| 4873 | PyErr_SetString(UnpicklingError, "EXT specifies code <= 0"); |
| 4874 | return -1; |
| 4875 | } |
| 4876 | |
| 4877 | /* Look for the code in the cache. */ |
| 4878 | py_code = PyLong_FromLong(code); |
| 4879 | if (py_code == NULL) |
| 4880 | return -1; |
| 4881 | obj = PyDict_GetItem(extension_cache, py_code); |
| 4882 | if (obj != NULL) { |
| 4883 | /* Bingo. */ |
| 4884 | Py_DECREF(py_code); |
| 4885 | PDATA_APPEND(self->stack, obj, -1); |
| 4886 | return 0; |
| 4887 | } |
| 4888 | |
| 4889 | /* Look up the (module_name, class_name) pair. */ |
| 4890 | pair = PyDict_GetItem(inverted_registry, py_code); |
| 4891 | if (pair == NULL) { |
| 4892 | Py_DECREF(py_code); |
| 4893 | PyErr_Format(PyExc_ValueError, "unregistered extension " |
| 4894 | "code %ld", code); |
| 4895 | return -1; |
| 4896 | } |
| 4897 | /* Since the extension registry is manipulable via Python code, |
| 4898 | * confirm that pair is really a 2-tuple of strings. |
| 4899 | */ |
| 4900 | if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 || |
| 4901 | !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) || |
| 4902 | !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) { |
| 4903 | Py_DECREF(py_code); |
| 4904 | PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] " |
| 4905 | "isn't a 2-tuple of strings", code); |
| 4906 | return -1; |
| 4907 | } |
| 4908 | /* Load the object. */ |
| 4909 | obj = find_class(self, module_name, class_name); |
| 4910 | if (obj == NULL) { |
| 4911 | Py_DECREF(py_code); |
| 4912 | return -1; |
| 4913 | } |
| 4914 | /* Cache code -> obj. */ |
| 4915 | code = PyDict_SetItem(extension_cache, py_code, obj); |
| 4916 | Py_DECREF(py_code); |
| 4917 | if (code < 0) { |
| 4918 | Py_DECREF(obj); |
| 4919 | return -1; |
| 4920 | } |
| 4921 | PDATA_PUSH(self->stack, obj, -1); |
| 4922 | return 0; |
| 4923 | } |
| 4924 | |
| 4925 | static int |
| 4926 | load_put(UnpicklerObject *self) |
| 4927 | { |
| 4928 | PyObject *key, *value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4929 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4930 | Py_ssize_t len; |
| 4931 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4932 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4933 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4934 | return -1; |
| 4935 | if (len < 2) |
| 4936 | return bad_readline(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4937 | if (Py_SIZE(self->stack) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4938 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4939 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4940 | |
| 4941 | key = PyLong_FromString(s, NULL, 10); |
| 4942 | if (key == NULL) |
| 4943 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4944 | idx = PyLong_AsSsize_t(key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4945 | Py_DECREF(key); |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 4946 | if (idx < 0) { |
| 4947 | if (!PyErr_Occurred()) |
| 4948 | PyErr_SetString(PyExc_ValueError, |
| 4949 | "negative PUT argument"); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4950 | return -1; |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 4951 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4952 | |
| 4953 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4954 | } |
| 4955 | |
| 4956 | static int |
| 4957 | load_binput(UnpicklerObject *self) |
| 4958 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4959 | PyObject *value; |
| 4960 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4961 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4962 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4963 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4964 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4965 | |
| 4966 | if (Py_SIZE(self->stack) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4967 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4968 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4969 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4970 | idx = Py_CHARMASK(s[0]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4971 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4972 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4973 | } |
| 4974 | |
| 4975 | static int |
| 4976 | load_long_binput(UnpicklerObject *self) |
| 4977 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4978 | PyObject *value; |
| 4979 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4980 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4981 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4982 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4983 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4984 | |
| 4985 | if (Py_SIZE(self->stack) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4986 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4987 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4988 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4989 | idx = calc_binsize(s, 4); |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 4990 | if (idx < 0) { |
| 4991 | PyErr_SetString(PyExc_ValueError, |
| 4992 | "negative LONG_BINPUT argument"); |
| 4993 | return -1; |
| 4994 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4995 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4996 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4997 | } |
| 4998 | |
| 4999 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5000 | do_append(UnpicklerObject *self, Py_ssize_t x) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5001 | { |
| 5002 | PyObject *value; |
| 5003 | PyObject *list; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5004 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5005 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5006 | len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5007 | if (x > len || x <= 0) |
| 5008 | return stack_underflow(); |
| 5009 | if (len == x) /* nothing to do */ |
| 5010 | return 0; |
| 5011 | |
| 5012 | list = self->stack->data[x - 1]; |
| 5013 | |
| 5014 | if (PyList_Check(list)) { |
| 5015 | PyObject *slice; |
| 5016 | Py_ssize_t list_len; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5017 | int ret; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5018 | |
| 5019 | slice = Pdata_poplist(self->stack, x); |
| 5020 | if (!slice) |
| 5021 | return -1; |
| 5022 | list_len = PyList_GET_SIZE(list); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5023 | ret = PyList_SetSlice(list, list_len, list_len, slice); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5024 | Py_DECREF(slice); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5025 | return ret; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5026 | } |
| 5027 | else { |
| 5028 | PyObject *append_func; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5029 | _Py_IDENTIFIER(append); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5030 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 5031 | append_func = _PyObject_GetAttrId(list, &PyId_append); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5032 | if (append_func == NULL) |
| 5033 | return -1; |
| 5034 | for (i = x; i < len; i++) { |
| 5035 | PyObject *result; |
| 5036 | |
| 5037 | value = self->stack->data[i]; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5038 | result = _Unpickler_FastCall(self, append_func, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5039 | if (result == NULL) { |
| 5040 | Pdata_clear(self->stack, i + 1); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5041 | Py_SIZE(self->stack) = x; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5042 | return -1; |
| 5043 | } |
| 5044 | Py_DECREF(result); |
| 5045 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5046 | Py_SIZE(self->stack) = x; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5047 | } |
| 5048 | |
| 5049 | return 0; |
| 5050 | } |
| 5051 | |
| 5052 | static int |
| 5053 | load_append(UnpicklerObject *self) |
| 5054 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5055 | return do_append(self, Py_SIZE(self->stack) - 1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5056 | } |
| 5057 | |
| 5058 | static int |
| 5059 | load_appends(UnpicklerObject *self) |
| 5060 | { |
| 5061 | return do_append(self, marker(self)); |
| 5062 | } |
| 5063 | |
| 5064 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5065 | do_setitems(UnpicklerObject *self, Py_ssize_t x) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5066 | { |
| 5067 | PyObject *value, *key; |
| 5068 | PyObject *dict; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5069 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5070 | int status = 0; |
| 5071 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5072 | len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5073 | if (x > len || x <= 0) |
| 5074 | return stack_underflow(); |
| 5075 | if (len == x) /* nothing to do */ |
| 5076 | return 0; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 5077 | if ((len - x) % 2 != 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5078 | /* Currupt or hostile pickle -- we never write one like this. */ |
| 5079 | PyErr_SetString(UnpicklingError, "odd number of items for SETITEMS"); |
| 5080 | return -1; |
| 5081 | } |
| 5082 | |
| 5083 | /* Here, dict does not actually need to be a PyDict; it could be anything |
| 5084 | that supports the __setitem__ attribute. */ |
| 5085 | dict = self->stack->data[x - 1]; |
| 5086 | |
| 5087 | for (i = x + 1; i < len; i += 2) { |
| 5088 | key = self->stack->data[i - 1]; |
| 5089 | value = self->stack->data[i]; |
| 5090 | if (PyObject_SetItem(dict, key, value) < 0) { |
| 5091 | status = -1; |
| 5092 | break; |
| 5093 | } |
| 5094 | } |
| 5095 | |
| 5096 | Pdata_clear(self->stack, x); |
| 5097 | return status; |
| 5098 | } |
| 5099 | |
| 5100 | static int |
| 5101 | load_setitem(UnpicklerObject *self) |
| 5102 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5103 | return do_setitems(self, Py_SIZE(self->stack) - 2); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5104 | } |
| 5105 | |
| 5106 | static int |
| 5107 | load_setitems(UnpicklerObject *self) |
| 5108 | { |
| 5109 | return do_setitems(self, marker(self)); |
| 5110 | } |
| 5111 | |
| 5112 | static int |
| 5113 | load_build(UnpicklerObject *self) |
| 5114 | { |
| 5115 | PyObject *state, *inst, *slotstate; |
| 5116 | PyObject *setstate; |
| 5117 | int status = 0; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5118 | _Py_IDENTIFIER(__setstate__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5119 | |
| 5120 | /* Stack is ... instance, state. We want to leave instance at |
| 5121 | * the stack top, possibly mutated via instance.__setstate__(state). |
| 5122 | */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5123 | if (Py_SIZE(self->stack) < 2) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5124 | return stack_underflow(); |
| 5125 | |
| 5126 | PDATA_POP(self->stack, state); |
| 5127 | if (state == NULL) |
| 5128 | return -1; |
| 5129 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5130 | inst = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5131 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 5132 | setstate = _PyObject_GetAttrId(inst, &PyId___setstate__); |
Alexandre Vassalotti | 1f9d907 | 2008-08-15 03:07:47 +0000 | [diff] [blame] | 5133 | if (setstate == NULL) { |
| 5134 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 5135 | PyErr_Clear(); |
Antoine Pitrou | d79dc62 | 2008-09-05 00:03:33 +0000 | [diff] [blame] | 5136 | else { |
| 5137 | Py_DECREF(state); |
Alexandre Vassalotti | 1f9d907 | 2008-08-15 03:07:47 +0000 | [diff] [blame] | 5138 | return -1; |
Antoine Pitrou | d79dc62 | 2008-09-05 00:03:33 +0000 | [diff] [blame] | 5139 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5140 | } |
| 5141 | else { |
| 5142 | PyObject *result; |
| 5143 | |
| 5144 | /* The explicit __setstate__ is responsible for everything. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5145 | /* Ugh... this does not leak since _Unpickler_FastCall() steals the |
Antoine Pitrou | d79dc62 | 2008-09-05 00:03:33 +0000 | [diff] [blame] | 5146 | reference to state first. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5147 | result = _Unpickler_FastCall(self, setstate, state); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5148 | Py_DECREF(setstate); |
| 5149 | if (result == NULL) |
| 5150 | return -1; |
| 5151 | Py_DECREF(result); |
| 5152 | return 0; |
| 5153 | } |
| 5154 | |
| 5155 | /* A default __setstate__. First see whether state embeds a |
| 5156 | * slot state dict too (a proto 2 addition). |
| 5157 | */ |
| 5158 | if (PyTuple_Check(state) && Py_SIZE(state) == 2) { |
| 5159 | PyObject *tmp = state; |
| 5160 | |
| 5161 | state = PyTuple_GET_ITEM(tmp, 0); |
| 5162 | slotstate = PyTuple_GET_ITEM(tmp, 1); |
| 5163 | Py_INCREF(state); |
| 5164 | Py_INCREF(slotstate); |
| 5165 | Py_DECREF(tmp); |
| 5166 | } |
| 5167 | else |
| 5168 | slotstate = NULL; |
| 5169 | |
| 5170 | /* Set inst.__dict__ from the state dict (if any). */ |
| 5171 | if (state != Py_None) { |
| 5172 | PyObject *dict; |
Antoine Pitrou | a9f48a0 | 2009-05-02 21:41:14 +0000 | [diff] [blame] | 5173 | PyObject *d_key, *d_value; |
| 5174 | Py_ssize_t i; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5175 | _Py_IDENTIFIER(__dict__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5176 | |
| 5177 | if (!PyDict_Check(state)) { |
| 5178 | PyErr_SetString(UnpicklingError, "state is not a dictionary"); |
| 5179 | goto error; |
| 5180 | } |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 5181 | dict = _PyObject_GetAttrId(inst, &PyId___dict__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5182 | if (dict == NULL) |
| 5183 | goto error; |
| 5184 | |
Antoine Pitrou | a9f48a0 | 2009-05-02 21:41:14 +0000 | [diff] [blame] | 5185 | i = 0; |
| 5186 | while (PyDict_Next(state, &i, &d_key, &d_value)) { |
| 5187 | /* normally the keys for instance attributes are |
| 5188 | interned. we should try to do that here. */ |
| 5189 | Py_INCREF(d_key); |
| 5190 | if (PyUnicode_CheckExact(d_key)) |
| 5191 | PyUnicode_InternInPlace(&d_key); |
| 5192 | if (PyObject_SetItem(dict, d_key, d_value) < 0) { |
| 5193 | Py_DECREF(d_key); |
| 5194 | goto error; |
| 5195 | } |
| 5196 | Py_DECREF(d_key); |
| 5197 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5198 | Py_DECREF(dict); |
| 5199 | } |
| 5200 | |
| 5201 | /* Also set instance attributes from the slotstate dict (if any). */ |
| 5202 | if (slotstate != NULL) { |
| 5203 | PyObject *d_key, *d_value; |
| 5204 | Py_ssize_t i; |
| 5205 | |
| 5206 | if (!PyDict_Check(slotstate)) { |
| 5207 | PyErr_SetString(UnpicklingError, |
| 5208 | "slot state is not a dictionary"); |
| 5209 | goto error; |
| 5210 | } |
| 5211 | i = 0; |
| 5212 | while (PyDict_Next(slotstate, &i, &d_key, &d_value)) { |
| 5213 | if (PyObject_SetAttr(inst, d_key, d_value) < 0) |
| 5214 | goto error; |
| 5215 | } |
| 5216 | } |
| 5217 | |
| 5218 | if (0) { |
| 5219 | error: |
| 5220 | status = -1; |
| 5221 | } |
| 5222 | |
| 5223 | Py_DECREF(state); |
| 5224 | Py_XDECREF(slotstate); |
| 5225 | return status; |
| 5226 | } |
| 5227 | |
| 5228 | static int |
| 5229 | load_mark(UnpicklerObject *self) |
| 5230 | { |
| 5231 | |
| 5232 | /* Note that we split the (pickle.py) stack into two stacks, an |
| 5233 | * object stack and a mark stack. Here we push a mark onto the |
| 5234 | * mark stack. |
| 5235 | */ |
| 5236 | |
| 5237 | if ((self->num_marks + 1) >= self->marks_size) { |
| 5238 | size_t alloc; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5239 | Py_ssize_t *marks; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5240 | |
| 5241 | /* Use the size_t type to check for overflow. */ |
| 5242 | alloc = ((size_t)self->num_marks << 1) + 20; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5243 | if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) || |
Alexandre Vassalotti | 7634ff5 | 2008-06-13 02:16:06 +0000 | [diff] [blame] | 5244 | alloc <= ((size_t)self->num_marks + 1)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5245 | PyErr_NoMemory(); |
| 5246 | return -1; |
| 5247 | } |
| 5248 | |
| 5249 | if (self->marks == NULL) |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5250 | marks = (Py_ssize_t *) PyMem_Malloc(alloc * sizeof(Py_ssize_t)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5251 | else |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5252 | marks = (Py_ssize_t *) PyMem_Realloc(self->marks, |
| 5253 | alloc * sizeof(Py_ssize_t)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5254 | if (marks == NULL) { |
| 5255 | PyErr_NoMemory(); |
| 5256 | return -1; |
| 5257 | } |
| 5258 | self->marks = marks; |
| 5259 | self->marks_size = (Py_ssize_t)alloc; |
| 5260 | } |
| 5261 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5262 | self->marks[self->num_marks++] = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5263 | |
| 5264 | return 0; |
| 5265 | } |
| 5266 | |
| 5267 | static int |
| 5268 | load_reduce(UnpicklerObject *self) |
| 5269 | { |
| 5270 | PyObject *callable = NULL; |
| 5271 | PyObject *argtup = NULL; |
| 5272 | PyObject *obj = NULL; |
| 5273 | |
| 5274 | PDATA_POP(self->stack, argtup); |
| 5275 | if (argtup == NULL) |
| 5276 | return -1; |
| 5277 | PDATA_POP(self->stack, callable); |
| 5278 | if (callable) { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5279 | obj = PyObject_CallObject(callable, argtup); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5280 | Py_DECREF(callable); |
| 5281 | } |
| 5282 | Py_DECREF(argtup); |
| 5283 | |
| 5284 | if (obj == NULL) |
| 5285 | return -1; |
| 5286 | |
| 5287 | PDATA_PUSH(self->stack, obj, -1); |
| 5288 | return 0; |
| 5289 | } |
| 5290 | |
| 5291 | /* Just raises an error if we don't know the protocol specified. PROTO |
| 5292 | * is the first opcode for protocols >= 2. |
| 5293 | */ |
| 5294 | static int |
| 5295 | load_proto(UnpicklerObject *self) |
| 5296 | { |
| 5297 | char *s; |
| 5298 | int i; |
| 5299 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5300 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5301 | return -1; |
| 5302 | |
| 5303 | i = (unsigned char)s[0]; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 5304 | if (i <= HIGHEST_PROTOCOL) { |
| 5305 | self->proto = i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5306 | return 0; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 5307 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5308 | |
| 5309 | PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i); |
| 5310 | return -1; |
| 5311 | } |
| 5312 | |
| 5313 | static PyObject * |
| 5314 | load(UnpicklerObject *self) |
| 5315 | { |
| 5316 | PyObject *err; |
| 5317 | PyObject *value = NULL; |
| 5318 | char *s; |
| 5319 | |
| 5320 | self->num_marks = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5321 | if (Py_SIZE(self->stack)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5322 | Pdata_clear(self->stack, 0); |
| 5323 | |
| 5324 | /* Convenient macros for the dispatch while-switch loop just below. */ |
| 5325 | #define OP(opcode, load_func) \ |
| 5326 | case opcode: if (load_func(self) < 0) break; continue; |
| 5327 | |
| 5328 | #define OP_ARG(opcode, load_func, arg) \ |
| 5329 | case opcode: if (load_func(self, (arg)) < 0) break; continue; |
| 5330 | |
| 5331 | while (1) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5332 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5333 | break; |
| 5334 | |
| 5335 | switch ((enum opcode)s[0]) { |
| 5336 | OP(NONE, load_none) |
| 5337 | OP(BININT, load_binint) |
| 5338 | OP(BININT1, load_binint1) |
| 5339 | OP(BININT2, load_binint2) |
| 5340 | OP(INT, load_int) |
| 5341 | OP(LONG, load_long) |
| 5342 | OP_ARG(LONG1, load_counted_long, 1) |
| 5343 | OP_ARG(LONG4, load_counted_long, 4) |
| 5344 | OP(FLOAT, load_float) |
| 5345 | OP(BINFLOAT, load_binfloat) |
| 5346 | OP(BINBYTES, load_binbytes) |
| 5347 | OP(SHORT_BINBYTES, load_short_binbytes) |
| 5348 | OP(BINSTRING, load_binstring) |
| 5349 | OP(SHORT_BINSTRING, load_short_binstring) |
| 5350 | OP(STRING, load_string) |
| 5351 | OP(UNICODE, load_unicode) |
| 5352 | OP(BINUNICODE, load_binunicode) |
| 5353 | OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0) |
| 5354 | OP_ARG(TUPLE1, load_counted_tuple, 1) |
| 5355 | OP_ARG(TUPLE2, load_counted_tuple, 2) |
| 5356 | OP_ARG(TUPLE3, load_counted_tuple, 3) |
| 5357 | OP(TUPLE, load_tuple) |
| 5358 | OP(EMPTY_LIST, load_empty_list) |
| 5359 | OP(LIST, load_list) |
| 5360 | OP(EMPTY_DICT, load_empty_dict) |
| 5361 | OP(DICT, load_dict) |
| 5362 | OP(OBJ, load_obj) |
| 5363 | OP(INST, load_inst) |
| 5364 | OP(NEWOBJ, load_newobj) |
| 5365 | OP(GLOBAL, load_global) |
| 5366 | OP(APPEND, load_append) |
| 5367 | OP(APPENDS, load_appends) |
| 5368 | OP(BUILD, load_build) |
| 5369 | OP(DUP, load_dup) |
| 5370 | OP(BINGET, load_binget) |
| 5371 | OP(LONG_BINGET, load_long_binget) |
| 5372 | OP(GET, load_get) |
| 5373 | OP(MARK, load_mark) |
| 5374 | OP(BINPUT, load_binput) |
| 5375 | OP(LONG_BINPUT, load_long_binput) |
| 5376 | OP(PUT, load_put) |
| 5377 | OP(POP, load_pop) |
| 5378 | OP(POP_MARK, load_pop_mark) |
| 5379 | OP(SETITEM, load_setitem) |
| 5380 | OP(SETITEMS, load_setitems) |
| 5381 | OP(PERSID, load_persid) |
| 5382 | OP(BINPERSID, load_binpersid) |
| 5383 | OP(REDUCE, load_reduce) |
| 5384 | OP(PROTO, load_proto) |
| 5385 | OP_ARG(EXT1, load_extension, 1) |
| 5386 | OP_ARG(EXT2, load_extension, 2) |
| 5387 | OP_ARG(EXT4, load_extension, 4) |
| 5388 | OP_ARG(NEWTRUE, load_bool, Py_True) |
| 5389 | OP_ARG(NEWFALSE, load_bool, Py_False) |
| 5390 | |
| 5391 | case STOP: |
| 5392 | break; |
| 5393 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5394 | default: |
Benjamin Peterson | adde86d | 2011-09-23 13:41:41 -0400 | [diff] [blame] | 5395 | if (s[0] == '\0') |
| 5396 | PyErr_SetNone(PyExc_EOFError); |
| 5397 | else |
| 5398 | PyErr_Format(UnpicklingError, |
| 5399 | "invalid load key, '%c'.", s[0]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5400 | return NULL; |
| 5401 | } |
| 5402 | |
| 5403 | break; /* and we are done! */ |
| 5404 | } |
| 5405 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 5406 | if (_Unpickler_SkipConsumed(self) < 0) |
| 5407 | return NULL; |
| 5408 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5409 | /* XXX: It is not clear what this is actually for. */ |
| 5410 | if ((err = PyErr_Occurred())) { |
| 5411 | if (err == PyExc_EOFError) { |
| 5412 | PyErr_SetNone(PyExc_EOFError); |
| 5413 | } |
| 5414 | return NULL; |
| 5415 | } |
| 5416 | |
| 5417 | PDATA_POP(self->stack, value); |
| 5418 | return value; |
| 5419 | } |
| 5420 | |
| 5421 | PyDoc_STRVAR(Unpickler_load_doc, |
| 5422 | "load() -> object. Load a pickle." |
| 5423 | "\n" |
| 5424 | "Read a pickled object representation from the open file object given in\n" |
| 5425 | "the constructor, and return the reconstituted object hierarchy specified\n" |
| 5426 | "therein.\n"); |
| 5427 | |
| 5428 | static PyObject * |
| 5429 | Unpickler_load(UnpicklerObject *self) |
| 5430 | { |
| 5431 | /* Check whether the Unpickler was initialized correctly. This prevents |
| 5432 | segfaulting if a subclass overridden __init__ with a function that does |
| 5433 | not call Unpickler.__init__(). Here, we simply ensure that self->read |
| 5434 | is not NULL. */ |
| 5435 | if (self->read == NULL) { |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 5436 | PyErr_Format(UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5437 | "Unpickler.__init__() was not called by %s.__init__()", |
| 5438 | Py_TYPE(self)->tp_name); |
| 5439 | return NULL; |
| 5440 | } |
| 5441 | |
| 5442 | return load(self); |
| 5443 | } |
| 5444 | |
| 5445 | /* The name of find_class() is misleading. In newer pickle protocols, this |
| 5446 | function is used for loading any global (i.e., functions), not just |
| 5447 | classes. The name is kept only for backward compatibility. */ |
| 5448 | |
| 5449 | PyDoc_STRVAR(Unpickler_find_class_doc, |
| 5450 | "find_class(module_name, global_name) -> object.\n" |
| 5451 | "\n" |
| 5452 | "Return an object from a specified module, importing the module if\n" |
| 5453 | "necessary. Subclasses may override this method (e.g. to restrict\n" |
| 5454 | "unpickling of arbitrary classes and functions).\n" |
| 5455 | "\n" |
| 5456 | "This method is called whenever a class or a function object is\n" |
| 5457 | "needed. Both arguments passed are str objects.\n"); |
| 5458 | |
| 5459 | static PyObject * |
| 5460 | Unpickler_find_class(UnpicklerObject *self, PyObject *args) |
| 5461 | { |
| 5462 | PyObject *global; |
| 5463 | PyObject *modules_dict; |
| 5464 | PyObject *module; |
| 5465 | PyObject *module_name, *global_name; |
| 5466 | |
| 5467 | if (!PyArg_UnpackTuple(args, "find_class", 2, 2, |
| 5468 | &module_name, &global_name)) |
| 5469 | return NULL; |
| 5470 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 5471 | /* Try to map the old names used in Python 2.x to the new ones used in |
| 5472 | Python 3.x. We do this only with old pickle protocols and when the |
| 5473 | user has not disabled the feature. */ |
| 5474 | if (self->proto < 3 && self->fix_imports) { |
| 5475 | PyObject *key; |
| 5476 | PyObject *item; |
| 5477 | |
| 5478 | /* Check if the global (i.e., a function or a class) was renamed |
| 5479 | or moved to another module. */ |
| 5480 | key = PyTuple_Pack(2, module_name, global_name); |
| 5481 | if (key == NULL) |
| 5482 | return NULL; |
| 5483 | item = PyDict_GetItemWithError(name_mapping_2to3, key); |
| 5484 | Py_DECREF(key); |
| 5485 | if (item) { |
| 5486 | if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
| 5487 | PyErr_Format(PyExc_RuntimeError, |
| 5488 | "_compat_pickle.NAME_MAPPING values should be " |
| 5489 | "2-tuples, not %.200s", Py_TYPE(item)->tp_name); |
| 5490 | return NULL; |
| 5491 | } |
| 5492 | module_name = PyTuple_GET_ITEM(item, 0); |
| 5493 | global_name = PyTuple_GET_ITEM(item, 1); |
| 5494 | if (!PyUnicode_Check(module_name) || |
| 5495 | !PyUnicode_Check(global_name)) { |
| 5496 | PyErr_Format(PyExc_RuntimeError, |
| 5497 | "_compat_pickle.NAME_MAPPING values should be " |
| 5498 | "pairs of str, not (%.200s, %.200s)", |
| 5499 | Py_TYPE(module_name)->tp_name, |
| 5500 | Py_TYPE(global_name)->tp_name); |
| 5501 | return NULL; |
| 5502 | } |
| 5503 | } |
| 5504 | else if (PyErr_Occurred()) { |
| 5505 | return NULL; |
| 5506 | } |
| 5507 | |
| 5508 | /* Check if the module was renamed. */ |
| 5509 | item = PyDict_GetItemWithError(import_mapping_2to3, module_name); |
| 5510 | if (item) { |
| 5511 | if (!PyUnicode_Check(item)) { |
| 5512 | PyErr_Format(PyExc_RuntimeError, |
| 5513 | "_compat_pickle.IMPORT_MAPPING values should be " |
| 5514 | "strings, not %.200s", Py_TYPE(item)->tp_name); |
| 5515 | return NULL; |
| 5516 | } |
| 5517 | module_name = item; |
| 5518 | } |
| 5519 | else if (PyErr_Occurred()) { |
| 5520 | return NULL; |
| 5521 | } |
| 5522 | } |
| 5523 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5524 | modules_dict = PySys_GetObject("modules"); |
| 5525 | if (modules_dict == NULL) |
| 5526 | return NULL; |
| 5527 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 5528 | module = PyDict_GetItemWithError(modules_dict, module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5529 | if (module == NULL) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 5530 | if (PyErr_Occurred()) |
| 5531 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5532 | module = PyImport_Import(module_name); |
| 5533 | if (module == NULL) |
| 5534 | return NULL; |
| 5535 | global = PyObject_GetAttr(module, global_name); |
| 5536 | Py_DECREF(module); |
| 5537 | } |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 5538 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5539 | global = PyObject_GetAttr(module, global_name); |
| 5540 | } |
| 5541 | return global; |
| 5542 | } |
| 5543 | |
| 5544 | static struct PyMethodDef Unpickler_methods[] = { |
| 5545 | {"load", (PyCFunction)Unpickler_load, METH_NOARGS, |
| 5546 | Unpickler_load_doc}, |
| 5547 | {"find_class", (PyCFunction)Unpickler_find_class, METH_VARARGS, |
| 5548 | Unpickler_find_class_doc}, |
| 5549 | {NULL, NULL} /* sentinel */ |
| 5550 | }; |
| 5551 | |
| 5552 | static void |
| 5553 | Unpickler_dealloc(UnpicklerObject *self) |
| 5554 | { |
| 5555 | PyObject_GC_UnTrack((PyObject *)self); |
| 5556 | Py_XDECREF(self->readline); |
| 5557 | Py_XDECREF(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 5558 | Py_XDECREF(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5559 | Py_XDECREF(self->stack); |
| 5560 | Py_XDECREF(self->pers_func); |
| 5561 | Py_XDECREF(self->arg); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5562 | if (self->buffer.buf != NULL) { |
| 5563 | PyBuffer_Release(&self->buffer); |
| 5564 | self->buffer.buf = NULL; |
| 5565 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5566 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5567 | _Unpickler_MemoCleanup(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5568 | PyMem_Free(self->marks); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5569 | PyMem_Free(self->input_line); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5570 | free(self->encoding); |
| 5571 | free(self->errors); |
| 5572 | |
| 5573 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 5574 | } |
| 5575 | |
| 5576 | static int |
| 5577 | Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg) |
| 5578 | { |
| 5579 | Py_VISIT(self->readline); |
| 5580 | Py_VISIT(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 5581 | Py_VISIT(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5582 | Py_VISIT(self->stack); |
| 5583 | Py_VISIT(self->pers_func); |
| 5584 | Py_VISIT(self->arg); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5585 | return 0; |
| 5586 | } |
| 5587 | |
| 5588 | static int |
| 5589 | Unpickler_clear(UnpicklerObject *self) |
| 5590 | { |
| 5591 | Py_CLEAR(self->readline); |
| 5592 | Py_CLEAR(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 5593 | Py_CLEAR(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5594 | Py_CLEAR(self->stack); |
| 5595 | Py_CLEAR(self->pers_func); |
| 5596 | Py_CLEAR(self->arg); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5597 | if (self->buffer.buf != NULL) { |
| 5598 | PyBuffer_Release(&self->buffer); |
| 5599 | self->buffer.buf = NULL; |
| 5600 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5601 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5602 | _Unpickler_MemoCleanup(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5603 | PyMem_Free(self->marks); |
| 5604 | self->marks = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5605 | PyMem_Free(self->input_line); |
| 5606 | self->input_line = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5607 | free(self->encoding); |
| 5608 | self->encoding = NULL; |
| 5609 | free(self->errors); |
| 5610 | self->errors = NULL; |
| 5611 | |
| 5612 | return 0; |
| 5613 | } |
| 5614 | |
| 5615 | PyDoc_STRVAR(Unpickler_doc, |
| 5616 | "Unpickler(file, *, encoding='ASCII', errors='strict')" |
| 5617 | "\n" |
| 5618 | "This takes a binary file for reading a pickle data stream.\n" |
| 5619 | "\n" |
| 5620 | "The protocol version of the pickle is detected automatically, so no\n" |
| 5621 | "proto argument is needed.\n" |
| 5622 | "\n" |
| 5623 | "The file-like object must have two methods, a read() method\n" |
| 5624 | "that takes an integer argument, and a readline() method that\n" |
| 5625 | "requires no arguments. Both methods should return bytes.\n" |
| 5626 | "Thus file-like object can be a binary file object opened for\n" |
| 5627 | "reading, a BytesIO object, or any other custom object that\n" |
| 5628 | "meets this interface.\n" |
| 5629 | "\n" |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 5630 | "Optional keyword arguments are *fix_imports*, *encoding* and *errors*,\n" |
| 5631 | "which are used to control compatiblity support for pickle stream\n" |
| 5632 | "generated by Python 2.x. If *fix_imports* is True, pickle will try to\n" |
| 5633 | "map the old Python 2.x names to the new names used in Python 3.x. The\n" |
| 5634 | "*encoding* and *errors* tell pickle how to decode 8-bit string\n" |
| 5635 | "instances pickled by Python 2.x; these default to 'ASCII' and\n" |
| 5636 | "'strict', respectively.\n"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5637 | |
| 5638 | static int |
| 5639 | Unpickler_init(UnpicklerObject *self, PyObject *args, PyObject *kwds) |
| 5640 | { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 5641 | static char *kwlist[] = {"file", "fix_imports", "encoding", "errors", 0}; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5642 | PyObject *file; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5643 | PyObject *fix_imports = Py_True; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5644 | char *encoding = NULL; |
| 5645 | char *errors = NULL; |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5646 | _Py_IDENTIFIER(persistent_load); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5647 | |
| 5648 | /* XXX: That is an horrible error message. But, I don't know how to do |
| 5649 | better... */ |
| 5650 | if (Py_SIZE(args) != 1) { |
| 5651 | PyErr_Format(PyExc_TypeError, |
| 5652 | "%s takes exactly one positional argument (%zd given)", |
| 5653 | Py_TYPE(self)->tp_name, Py_SIZE(args)); |
| 5654 | return -1; |
| 5655 | } |
| 5656 | |
| 5657 | /* Arguments parsing needs to be done in the __init__() method to allow |
| 5658 | subclasses to define their own __init__() method, which may (or may |
| 5659 | not) support Unpickler arguments. However, this means we need to be |
| 5660 | extra careful in the other Unpickler methods, since a subclass could |
| 5661 | forget to call Unpickler.__init__() thus breaking our internal |
| 5662 | invariants. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5663 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|Oss:Unpickler", kwlist, |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 5664 | &file, &fix_imports, &encoding, &errors)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5665 | return -1; |
| 5666 | |
| 5667 | /* In case of multiple __init__() calls, clear previous content. */ |
| 5668 | if (self->read != NULL) |
| 5669 | (void)Unpickler_clear(self); |
| 5670 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5671 | if (_Unpickler_SetInputStream(self, file) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5672 | return -1; |
| 5673 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5674 | if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5675 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5676 | |
| 5677 | self->fix_imports = PyObject_IsTrue(fix_imports); |
| 5678 | if (self->fix_imports == -1) |
| 5679 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5680 | |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5681 | if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) { |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 5682 | self->pers_func = _PyObject_GetAttrId((PyObject *)self, |
| 5683 | &PyId_persistent_load); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5684 | if (self->pers_func == NULL) |
| 5685 | return -1; |
| 5686 | } |
| 5687 | else { |
| 5688 | self->pers_func = NULL; |
| 5689 | } |
| 5690 | |
| 5691 | self->stack = (Pdata *)Pdata_New(); |
| 5692 | if (self->stack == NULL) |
| 5693 | return -1; |
| 5694 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5695 | self->memo_size = 32; |
| 5696 | self->memo = _Unpickler_NewMemo(self->memo_size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5697 | if (self->memo == NULL) |
| 5698 | return -1; |
| 5699 | |
Alexandre Vassalotti | 0e7aa8c | 2009-04-03 04:17:41 +0000 | [diff] [blame] | 5700 | self->arg = NULL; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 5701 | self->proto = 0; |
Alexandre Vassalotti | 0e7aa8c | 2009-04-03 04:17:41 +0000 | [diff] [blame] | 5702 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5703 | return 0; |
| 5704 | } |
| 5705 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5706 | /* Define a proxy object for the Unpickler's internal memo object. This is to |
| 5707 | * avoid breaking code like: |
| 5708 | * unpickler.memo.clear() |
| 5709 | * and |
| 5710 | * unpickler.memo = saved_memo |
| 5711 | * Is this a good idea? Not really, but we don't want to break code that uses |
| 5712 | * it. Note that we don't implement the entire mapping API here. This is |
| 5713 | * intentional, as these should be treated as black-box implementation details. |
| 5714 | * |
| 5715 | * We do, however, have to implement pickling/unpickling support because of |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 5716 | * real-world code like cvs2svn. |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5717 | */ |
| 5718 | |
| 5719 | typedef struct { |
| 5720 | PyObject_HEAD |
| 5721 | UnpicklerObject *unpickler; |
| 5722 | } UnpicklerMemoProxyObject; |
| 5723 | |
| 5724 | PyDoc_STRVAR(ump_clear_doc, |
| 5725 | "memo.clear() -> None. Remove all items from memo."); |
| 5726 | |
| 5727 | static PyObject * |
| 5728 | ump_clear(UnpicklerMemoProxyObject *self) |
| 5729 | { |
| 5730 | _Unpickler_MemoCleanup(self->unpickler); |
| 5731 | self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size); |
| 5732 | if (self->unpickler->memo == NULL) |
| 5733 | return NULL; |
| 5734 | Py_RETURN_NONE; |
| 5735 | } |
| 5736 | |
| 5737 | PyDoc_STRVAR(ump_copy_doc, |
| 5738 | "memo.copy() -> new_memo. Copy the memo to a new object."); |
| 5739 | |
| 5740 | static PyObject * |
| 5741 | ump_copy(UnpicklerMemoProxyObject *self) |
| 5742 | { |
| 5743 | Py_ssize_t i; |
| 5744 | PyObject *new_memo = PyDict_New(); |
| 5745 | if (new_memo == NULL) |
| 5746 | return NULL; |
| 5747 | |
| 5748 | for (i = 0; i < self->unpickler->memo_size; i++) { |
| 5749 | int status; |
| 5750 | PyObject *key, *value; |
| 5751 | |
| 5752 | value = self->unpickler->memo[i]; |
| 5753 | if (value == NULL) |
| 5754 | continue; |
| 5755 | |
| 5756 | key = PyLong_FromSsize_t(i); |
| 5757 | if (key == NULL) |
| 5758 | goto error; |
| 5759 | status = PyDict_SetItem(new_memo, key, value); |
| 5760 | Py_DECREF(key); |
| 5761 | if (status < 0) |
| 5762 | goto error; |
| 5763 | } |
| 5764 | return new_memo; |
| 5765 | |
| 5766 | error: |
| 5767 | Py_DECREF(new_memo); |
| 5768 | return NULL; |
| 5769 | } |
| 5770 | |
| 5771 | PyDoc_STRVAR(ump_reduce_doc, |
| 5772 | "memo.__reduce__(). Pickling support."); |
| 5773 | |
| 5774 | static PyObject * |
| 5775 | ump_reduce(UnpicklerMemoProxyObject *self, PyObject *args) |
| 5776 | { |
| 5777 | PyObject *reduce_value; |
| 5778 | PyObject *constructor_args; |
| 5779 | PyObject *contents = ump_copy(self); |
| 5780 | if (contents == NULL) |
| 5781 | return NULL; |
| 5782 | |
| 5783 | reduce_value = PyTuple_New(2); |
| 5784 | if (reduce_value == NULL) { |
| 5785 | Py_DECREF(contents); |
| 5786 | return NULL; |
| 5787 | } |
| 5788 | constructor_args = PyTuple_New(1); |
| 5789 | if (constructor_args == NULL) { |
| 5790 | Py_DECREF(contents); |
| 5791 | Py_DECREF(reduce_value); |
| 5792 | return NULL; |
| 5793 | } |
| 5794 | PyTuple_SET_ITEM(constructor_args, 0, contents); |
| 5795 | Py_INCREF((PyObject *)&PyDict_Type); |
| 5796 | PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type); |
| 5797 | PyTuple_SET_ITEM(reduce_value, 1, constructor_args); |
| 5798 | return reduce_value; |
| 5799 | } |
| 5800 | |
| 5801 | static PyMethodDef unpicklerproxy_methods[] = { |
| 5802 | {"clear", (PyCFunction)ump_clear, METH_NOARGS, ump_clear_doc}, |
| 5803 | {"copy", (PyCFunction)ump_copy, METH_NOARGS, ump_copy_doc}, |
| 5804 | {"__reduce__", (PyCFunction)ump_reduce, METH_VARARGS, ump_reduce_doc}, |
| 5805 | {NULL, NULL} /* sentinel */ |
| 5806 | }; |
| 5807 | |
| 5808 | static void |
| 5809 | UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self) |
| 5810 | { |
| 5811 | PyObject_GC_UnTrack(self); |
| 5812 | Py_XDECREF(self->unpickler); |
| 5813 | PyObject_GC_Del((PyObject *)self); |
| 5814 | } |
| 5815 | |
| 5816 | static int |
| 5817 | UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self, |
| 5818 | visitproc visit, void *arg) |
| 5819 | { |
| 5820 | Py_VISIT(self->unpickler); |
| 5821 | return 0; |
| 5822 | } |
| 5823 | |
| 5824 | static int |
| 5825 | UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self) |
| 5826 | { |
| 5827 | Py_CLEAR(self->unpickler); |
| 5828 | return 0; |
| 5829 | } |
| 5830 | |
| 5831 | static PyTypeObject UnpicklerMemoProxyType = { |
| 5832 | PyVarObject_HEAD_INIT(NULL, 0) |
| 5833 | "_pickle.UnpicklerMemoProxy", /*tp_name*/ |
| 5834 | sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/ |
| 5835 | 0, |
| 5836 | (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */ |
| 5837 | 0, /* tp_print */ |
| 5838 | 0, /* tp_getattr */ |
| 5839 | 0, /* tp_setattr */ |
| 5840 | 0, /* tp_compare */ |
| 5841 | 0, /* tp_repr */ |
| 5842 | 0, /* tp_as_number */ |
| 5843 | 0, /* tp_as_sequence */ |
| 5844 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 5845 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5846 | 0, /* tp_call */ |
| 5847 | 0, /* tp_str */ |
| 5848 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 5849 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 5850 | 0, /* tp_as_buffer */ |
| 5851 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 5852 | 0, /* tp_doc */ |
| 5853 | (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */ |
| 5854 | (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */ |
| 5855 | 0, /* tp_richcompare */ |
| 5856 | 0, /* tp_weaklistoffset */ |
| 5857 | 0, /* tp_iter */ |
| 5858 | 0, /* tp_iternext */ |
| 5859 | unpicklerproxy_methods, /* tp_methods */ |
| 5860 | }; |
| 5861 | |
| 5862 | static PyObject * |
| 5863 | UnpicklerMemoProxy_New(UnpicklerObject *unpickler) |
| 5864 | { |
| 5865 | UnpicklerMemoProxyObject *self; |
| 5866 | |
| 5867 | self = PyObject_GC_New(UnpicklerMemoProxyObject, |
| 5868 | &UnpicklerMemoProxyType); |
| 5869 | if (self == NULL) |
| 5870 | return NULL; |
| 5871 | Py_INCREF(unpickler); |
| 5872 | self->unpickler = unpickler; |
| 5873 | PyObject_GC_Track(self); |
| 5874 | return (PyObject *)self; |
| 5875 | } |
| 5876 | |
| 5877 | /*****************************************************************************/ |
| 5878 | |
| 5879 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5880 | static PyObject * |
| 5881 | Unpickler_get_memo(UnpicklerObject *self) |
| 5882 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5883 | return UnpicklerMemoProxy_New(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5884 | } |
| 5885 | |
| 5886 | static int |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5887 | Unpickler_set_memo(UnpicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5888 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5889 | PyObject **new_memo; |
| 5890 | Py_ssize_t new_memo_size = 0; |
| 5891 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5892 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5893 | if (obj == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5894 | PyErr_SetString(PyExc_TypeError, |
| 5895 | "attribute deletion is not supported"); |
| 5896 | return -1; |
| 5897 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5898 | |
| 5899 | if (Py_TYPE(obj) == &UnpicklerMemoProxyType) { |
| 5900 | UnpicklerObject *unpickler = |
| 5901 | ((UnpicklerMemoProxyObject *)obj)->unpickler; |
| 5902 | |
| 5903 | new_memo_size = unpickler->memo_size; |
| 5904 | new_memo = _Unpickler_NewMemo(new_memo_size); |
| 5905 | if (new_memo == NULL) |
| 5906 | return -1; |
| 5907 | |
| 5908 | for (i = 0; i < new_memo_size; i++) { |
| 5909 | Py_XINCREF(unpickler->memo[i]); |
| 5910 | new_memo[i] = unpickler->memo[i]; |
| 5911 | } |
| 5912 | } |
| 5913 | else if (PyDict_Check(obj)) { |
| 5914 | Py_ssize_t i = 0; |
| 5915 | PyObject *key, *value; |
| 5916 | |
| 5917 | new_memo_size = PyDict_Size(obj); |
| 5918 | new_memo = _Unpickler_NewMemo(new_memo_size); |
| 5919 | if (new_memo == NULL) |
| 5920 | return -1; |
| 5921 | |
| 5922 | while (PyDict_Next(obj, &i, &key, &value)) { |
| 5923 | Py_ssize_t idx; |
| 5924 | if (!PyLong_Check(key)) { |
| 5925 | PyErr_SetString(PyExc_TypeError, |
| 5926 | "memo key must be integers"); |
| 5927 | goto error; |
| 5928 | } |
| 5929 | idx = PyLong_AsSsize_t(key); |
| 5930 | if (idx == -1 && PyErr_Occurred()) |
| 5931 | goto error; |
| 5932 | if (_Unpickler_MemoPut(self, idx, value) < 0) |
| 5933 | goto error; |
| 5934 | } |
| 5935 | } |
| 5936 | else { |
| 5937 | PyErr_Format(PyExc_TypeError, |
| 5938 | "'memo' attribute must be an UnpicklerMemoProxy object" |
| 5939 | "or dict, not %.200s", Py_TYPE(obj)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5940 | return -1; |
| 5941 | } |
| 5942 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5943 | _Unpickler_MemoCleanup(self); |
| 5944 | self->memo_size = new_memo_size; |
| 5945 | self->memo = new_memo; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5946 | |
| 5947 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5948 | |
| 5949 | error: |
| 5950 | if (new_memo_size) { |
| 5951 | i = new_memo_size; |
| 5952 | while (--i >= 0) { |
| 5953 | Py_XDECREF(new_memo[i]); |
| 5954 | } |
| 5955 | PyMem_FREE(new_memo); |
| 5956 | } |
| 5957 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5958 | } |
| 5959 | |
| 5960 | static PyObject * |
| 5961 | Unpickler_get_persload(UnpicklerObject *self) |
| 5962 | { |
| 5963 | if (self->pers_func == NULL) |
| 5964 | PyErr_SetString(PyExc_AttributeError, "persistent_load"); |
| 5965 | else |
| 5966 | Py_INCREF(self->pers_func); |
| 5967 | return self->pers_func; |
| 5968 | } |
| 5969 | |
| 5970 | static int |
| 5971 | Unpickler_set_persload(UnpicklerObject *self, PyObject *value) |
| 5972 | { |
| 5973 | PyObject *tmp; |
| 5974 | |
| 5975 | if (value == NULL) { |
| 5976 | PyErr_SetString(PyExc_TypeError, |
| 5977 | "attribute deletion is not supported"); |
| 5978 | return -1; |
| 5979 | } |
| 5980 | if (!PyCallable_Check(value)) { |
| 5981 | PyErr_SetString(PyExc_TypeError, |
| 5982 | "persistent_load must be a callable taking " |
| 5983 | "one argument"); |
| 5984 | return -1; |
| 5985 | } |
| 5986 | |
| 5987 | tmp = self->pers_func; |
| 5988 | Py_INCREF(value); |
| 5989 | self->pers_func = value; |
| 5990 | Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */ |
| 5991 | |
| 5992 | return 0; |
| 5993 | } |
| 5994 | |
| 5995 | static PyGetSetDef Unpickler_getsets[] = { |
| 5996 | {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo}, |
| 5997 | {"persistent_load", (getter)Unpickler_get_persload, |
| 5998 | (setter)Unpickler_set_persload}, |
| 5999 | {NULL} |
| 6000 | }; |
| 6001 | |
| 6002 | static PyTypeObject Unpickler_Type = { |
| 6003 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6004 | "_pickle.Unpickler", /*tp_name*/ |
| 6005 | sizeof(UnpicklerObject), /*tp_basicsize*/ |
| 6006 | 0, /*tp_itemsize*/ |
| 6007 | (destructor)Unpickler_dealloc, /*tp_dealloc*/ |
| 6008 | 0, /*tp_print*/ |
| 6009 | 0, /*tp_getattr*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6010 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 6011 | 0, /*tp_reserved*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6012 | 0, /*tp_repr*/ |
| 6013 | 0, /*tp_as_number*/ |
| 6014 | 0, /*tp_as_sequence*/ |
| 6015 | 0, /*tp_as_mapping*/ |
| 6016 | 0, /*tp_hash*/ |
| 6017 | 0, /*tp_call*/ |
| 6018 | 0, /*tp_str*/ |
| 6019 | 0, /*tp_getattro*/ |
| 6020 | 0, /*tp_setattro*/ |
| 6021 | 0, /*tp_as_buffer*/ |
| 6022 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 6023 | Unpickler_doc, /*tp_doc*/ |
| 6024 | (traverseproc)Unpickler_traverse, /*tp_traverse*/ |
| 6025 | (inquiry)Unpickler_clear, /*tp_clear*/ |
| 6026 | 0, /*tp_richcompare*/ |
| 6027 | 0, /*tp_weaklistoffset*/ |
| 6028 | 0, /*tp_iter*/ |
| 6029 | 0, /*tp_iternext*/ |
| 6030 | Unpickler_methods, /*tp_methods*/ |
| 6031 | 0, /*tp_members*/ |
| 6032 | Unpickler_getsets, /*tp_getset*/ |
| 6033 | 0, /*tp_base*/ |
| 6034 | 0, /*tp_dict*/ |
| 6035 | 0, /*tp_descr_get*/ |
| 6036 | 0, /*tp_descr_set*/ |
| 6037 | 0, /*tp_dictoffset*/ |
| 6038 | (initproc)Unpickler_init, /*tp_init*/ |
| 6039 | PyType_GenericAlloc, /*tp_alloc*/ |
| 6040 | PyType_GenericNew, /*tp_new*/ |
| 6041 | PyObject_GC_Del, /*tp_free*/ |
| 6042 | 0, /*tp_is_gc*/ |
| 6043 | }; |
| 6044 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6045 | PyDoc_STRVAR(pickle_dump_doc, |
| 6046 | "dump(obj, file, protocol=None, *, fix_imports=True) -> None\n" |
| 6047 | "\n" |
| 6048 | "Write a pickled representation of obj to the open file object file. This\n" |
| 6049 | "is equivalent to ``Pickler(file, protocol).dump(obj)``, but may be more\n" |
| 6050 | "efficient.\n" |
| 6051 | "\n" |
| 6052 | "The optional protocol argument tells the pickler to use the given protocol;\n" |
| 6053 | "supported protocols are 0, 1, 2, 3. The default protocol is 3; a\n" |
| 6054 | "backward-incompatible protocol designed for Python 3.0.\n" |
| 6055 | "\n" |
| 6056 | "Specifying a negative protocol version selects the highest protocol version\n" |
| 6057 | "supported. The higher the protocol used, the more recent the version of\n" |
| 6058 | "Python needed to read the pickle produced.\n" |
| 6059 | "\n" |
| 6060 | "The file argument must have a write() method that accepts a single bytes\n" |
| 6061 | "argument. It can thus be a file object opened for binary writing, a\n" |
| 6062 | "io.BytesIO instance, or any other custom object that meets this interface.\n" |
| 6063 | "\n" |
| 6064 | "If fix_imports is True and protocol is less than 3, pickle will try to\n" |
| 6065 | "map the new Python 3.x names to the old module names used in Python 2.x,\n" |
| 6066 | "so that the pickle data stream is readable with Python 2.x.\n"); |
| 6067 | |
| 6068 | static PyObject * |
| 6069 | pickle_dump(PyObject *self, PyObject *args, PyObject *kwds) |
| 6070 | { |
| 6071 | static char *kwlist[] = {"obj", "file", "protocol", "fix_imports", 0}; |
| 6072 | PyObject *obj; |
| 6073 | PyObject *file; |
| 6074 | PyObject *proto = NULL; |
| 6075 | PyObject *fix_imports = Py_True; |
| 6076 | PicklerObject *pickler; |
| 6077 | |
| 6078 | /* fix_imports is a keyword-only argument. */ |
| 6079 | if (Py_SIZE(args) > 3) { |
| 6080 | PyErr_Format(PyExc_TypeError, |
| 6081 | "pickle.dump() takes at most 3 positional " |
| 6082 | "argument (%zd given)", Py_SIZE(args)); |
| 6083 | return NULL; |
| 6084 | } |
| 6085 | |
| 6086 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|OO:dump", kwlist, |
| 6087 | &obj, &file, &proto, &fix_imports)) |
| 6088 | return NULL; |
| 6089 | |
| 6090 | pickler = _Pickler_New(); |
| 6091 | if (pickler == NULL) |
| 6092 | return NULL; |
| 6093 | |
| 6094 | if (_Pickler_SetProtocol(pickler, proto, fix_imports) < 0) |
| 6095 | goto error; |
| 6096 | |
| 6097 | if (_Pickler_SetOutputStream(pickler, file) < 0) |
| 6098 | goto error; |
| 6099 | |
| 6100 | if (dump(pickler, obj) < 0) |
| 6101 | goto error; |
| 6102 | |
| 6103 | if (_Pickler_FlushToFile(pickler) < 0) |
| 6104 | goto error; |
| 6105 | |
| 6106 | Py_DECREF(pickler); |
| 6107 | Py_RETURN_NONE; |
| 6108 | |
| 6109 | error: |
| 6110 | Py_XDECREF(pickler); |
| 6111 | return NULL; |
| 6112 | } |
| 6113 | |
| 6114 | PyDoc_STRVAR(pickle_dumps_doc, |
| 6115 | "dumps(obj, protocol=None, *, fix_imports=True) -> bytes\n" |
| 6116 | "\n" |
| 6117 | "Return the pickled representation of the object as a bytes\n" |
| 6118 | "object, instead of writing it to a file.\n" |
| 6119 | "\n" |
| 6120 | "The optional protocol argument tells the pickler to use the given protocol;\n" |
| 6121 | "supported protocols are 0, 1, 2, 3. The default protocol is 3; a\n" |
| 6122 | "backward-incompatible protocol designed for Python 3.0.\n" |
| 6123 | "\n" |
| 6124 | "Specifying a negative protocol version selects the highest protocol version\n" |
| 6125 | "supported. The higher the protocol used, the more recent the version of\n" |
| 6126 | "Python needed to read the pickle produced.\n" |
| 6127 | "\n" |
| 6128 | "If fix_imports is True and *protocol* is less than 3, pickle will try to\n" |
| 6129 | "map the new Python 3.x names to the old module names used in Python 2.x,\n" |
| 6130 | "so that the pickle data stream is readable with Python 2.x.\n"); |
| 6131 | |
| 6132 | static PyObject * |
| 6133 | pickle_dumps(PyObject *self, PyObject *args, PyObject *kwds) |
| 6134 | { |
| 6135 | static char *kwlist[] = {"obj", "protocol", "fix_imports", 0}; |
| 6136 | PyObject *obj; |
| 6137 | PyObject *proto = NULL; |
| 6138 | PyObject *result; |
| 6139 | PyObject *fix_imports = Py_True; |
| 6140 | PicklerObject *pickler; |
| 6141 | |
| 6142 | /* fix_imports is a keyword-only argument. */ |
| 6143 | if (Py_SIZE(args) > 2) { |
| 6144 | PyErr_Format(PyExc_TypeError, |
| 6145 | "pickle.dumps() takes at most 2 positional " |
| 6146 | "argument (%zd given)", Py_SIZE(args)); |
| 6147 | return NULL; |
| 6148 | } |
| 6149 | |
| 6150 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OO:dumps", kwlist, |
| 6151 | &obj, &proto, &fix_imports)) |
| 6152 | return NULL; |
| 6153 | |
| 6154 | pickler = _Pickler_New(); |
| 6155 | if (pickler == NULL) |
| 6156 | return NULL; |
| 6157 | |
| 6158 | if (_Pickler_SetProtocol(pickler, proto, fix_imports) < 0) |
| 6159 | goto error; |
| 6160 | |
| 6161 | if (dump(pickler, obj) < 0) |
| 6162 | goto error; |
| 6163 | |
| 6164 | result = _Pickler_GetString(pickler); |
| 6165 | Py_DECREF(pickler); |
| 6166 | return result; |
| 6167 | |
| 6168 | error: |
| 6169 | Py_XDECREF(pickler); |
| 6170 | return NULL; |
| 6171 | } |
| 6172 | |
| 6173 | PyDoc_STRVAR(pickle_load_doc, |
| 6174 | "load(file, *, fix_imports=True, encoding='ASCII', errors='strict') -> object\n" |
| 6175 | "\n" |
| 6176 | "Read a pickled object representation from the open file object file and\n" |
| 6177 | "return the reconstituted object hierarchy specified therein. This is\n" |
| 6178 | "equivalent to ``Unpickler(file).load()``, but may be more efficient.\n" |
| 6179 | "\n" |
| 6180 | "The protocol version of the pickle is detected automatically, so no protocol\n" |
| 6181 | "argument is needed. Bytes past the pickled object's representation are\n" |
| 6182 | "ignored.\n" |
| 6183 | "\n" |
| 6184 | "The argument file must have two methods, a read() method that takes an\n" |
| 6185 | "integer argument, and a readline() method that requires no arguments. Both\n" |
| 6186 | "methods should return bytes. Thus *file* can be a binary file object opened\n" |
| 6187 | "for reading, a BytesIO object, or any other custom object that meets this\n" |
| 6188 | "interface.\n" |
| 6189 | "\n" |
| 6190 | "Optional keyword arguments are fix_imports, encoding and errors,\n" |
| 6191 | "which are used to control compatiblity support for pickle stream generated\n" |
| 6192 | "by Python 2.x. If fix_imports is True, pickle will try to map the old\n" |
| 6193 | "Python 2.x names to the new names used in Python 3.x. The encoding and\n" |
| 6194 | "errors tell pickle how to decode 8-bit string instances pickled by Python\n" |
| 6195 | "2.x; these default to 'ASCII' and 'strict', respectively.\n"); |
| 6196 | |
| 6197 | static PyObject * |
| 6198 | pickle_load(PyObject *self, PyObject *args, PyObject *kwds) |
| 6199 | { |
| 6200 | static char *kwlist[] = {"file", "fix_imports", "encoding", "errors", 0}; |
| 6201 | PyObject *file; |
| 6202 | PyObject *fix_imports = Py_True; |
| 6203 | PyObject *result; |
| 6204 | char *encoding = NULL; |
| 6205 | char *errors = NULL; |
| 6206 | UnpicklerObject *unpickler; |
| 6207 | |
| 6208 | /* fix_imports, encoding and errors are a keyword-only argument. */ |
| 6209 | if (Py_SIZE(args) != 1) { |
| 6210 | PyErr_Format(PyExc_TypeError, |
| 6211 | "pickle.load() takes exactly one positional " |
| 6212 | "argument (%zd given)", Py_SIZE(args)); |
| 6213 | return NULL; |
| 6214 | } |
| 6215 | |
| 6216 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|Oss:load", kwlist, |
| 6217 | &file, &fix_imports, &encoding, &errors)) |
| 6218 | return NULL; |
| 6219 | |
| 6220 | unpickler = _Unpickler_New(); |
| 6221 | if (unpickler == NULL) |
| 6222 | return NULL; |
| 6223 | |
| 6224 | if (_Unpickler_SetInputStream(unpickler, file) < 0) |
| 6225 | goto error; |
| 6226 | |
| 6227 | if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0) |
| 6228 | goto error; |
| 6229 | |
| 6230 | unpickler->fix_imports = PyObject_IsTrue(fix_imports); |
| 6231 | if (unpickler->fix_imports == -1) |
| 6232 | goto error; |
| 6233 | |
| 6234 | result = load(unpickler); |
| 6235 | Py_DECREF(unpickler); |
| 6236 | return result; |
| 6237 | |
| 6238 | error: |
| 6239 | Py_XDECREF(unpickler); |
| 6240 | return NULL; |
| 6241 | } |
| 6242 | |
| 6243 | PyDoc_STRVAR(pickle_loads_doc, |
| 6244 | "loads(input, *, fix_imports=True, encoding='ASCII', errors='strict') -> object\n" |
| 6245 | "\n" |
| 6246 | "Read a pickled object hierarchy from a bytes object and return the\n" |
| 6247 | "reconstituted object hierarchy specified therein\n" |
| 6248 | "\n" |
| 6249 | "The protocol version of the pickle is detected automatically, so no protocol\n" |
| 6250 | "argument is needed. Bytes past the pickled object's representation are\n" |
| 6251 | "ignored.\n" |
| 6252 | "\n" |
| 6253 | "Optional keyword arguments are fix_imports, encoding and errors, which\n" |
| 6254 | "are used to control compatiblity support for pickle stream generated\n" |
| 6255 | "by Python 2.x. If fix_imports is True, pickle will try to map the old\n" |
| 6256 | "Python 2.x names to the new names used in Python 3.x. The encoding and\n" |
| 6257 | "errors tell pickle how to decode 8-bit string instances pickled by Python\n" |
| 6258 | "2.x; these default to 'ASCII' and 'strict', respectively.\n"); |
| 6259 | |
| 6260 | static PyObject * |
| 6261 | pickle_loads(PyObject *self, PyObject *args, PyObject *kwds) |
| 6262 | { |
| 6263 | static char *kwlist[] = {"input", "fix_imports", "encoding", "errors", 0}; |
| 6264 | PyObject *input; |
| 6265 | PyObject *fix_imports = Py_True; |
| 6266 | PyObject *result; |
| 6267 | char *encoding = NULL; |
| 6268 | char *errors = NULL; |
| 6269 | UnpicklerObject *unpickler; |
| 6270 | |
| 6271 | /* fix_imports, encoding and errors are a keyword-only argument. */ |
| 6272 | if (Py_SIZE(args) != 1) { |
| 6273 | PyErr_Format(PyExc_TypeError, |
| 6274 | "pickle.loads() takes exactly one positional " |
| 6275 | "argument (%zd given)", Py_SIZE(args)); |
| 6276 | return NULL; |
| 6277 | } |
| 6278 | |
| 6279 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|Oss:loads", kwlist, |
| 6280 | &input, &fix_imports, &encoding, &errors)) |
| 6281 | return NULL; |
| 6282 | |
| 6283 | unpickler = _Unpickler_New(); |
| 6284 | if (unpickler == NULL) |
| 6285 | return NULL; |
| 6286 | |
| 6287 | if (_Unpickler_SetStringInput(unpickler, input) < 0) |
| 6288 | goto error; |
| 6289 | |
| 6290 | if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0) |
| 6291 | goto error; |
| 6292 | |
| 6293 | unpickler->fix_imports = PyObject_IsTrue(fix_imports); |
| 6294 | if (unpickler->fix_imports == -1) |
| 6295 | goto error; |
| 6296 | |
| 6297 | result = load(unpickler); |
| 6298 | Py_DECREF(unpickler); |
| 6299 | return result; |
| 6300 | |
| 6301 | error: |
| 6302 | Py_XDECREF(unpickler); |
| 6303 | return NULL; |
| 6304 | } |
| 6305 | |
| 6306 | |
| 6307 | static struct PyMethodDef pickle_methods[] = { |
| 6308 | {"dump", (PyCFunction)pickle_dump, METH_VARARGS|METH_KEYWORDS, |
| 6309 | pickle_dump_doc}, |
| 6310 | {"dumps", (PyCFunction)pickle_dumps, METH_VARARGS|METH_KEYWORDS, |
| 6311 | pickle_dumps_doc}, |
| 6312 | {"load", (PyCFunction)pickle_load, METH_VARARGS|METH_KEYWORDS, |
| 6313 | pickle_load_doc}, |
| 6314 | {"loads", (PyCFunction)pickle_loads, METH_VARARGS|METH_KEYWORDS, |
| 6315 | pickle_loads_doc}, |
| 6316 | {NULL, NULL} /* sentinel */ |
| 6317 | }; |
| 6318 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6319 | static int |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6320 | initmodule(void) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6321 | { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6322 | PyObject *copyreg = NULL; |
| 6323 | PyObject *compat_pickle = NULL; |
| 6324 | |
| 6325 | /* XXX: We should ensure that the types of the dictionaries imported are |
| 6326 | exactly PyDict objects. Otherwise, it is possible to crash the pickle |
| 6327 | since we use the PyDict API directly to access these dictionaries. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6328 | |
| 6329 | copyreg = PyImport_ImportModule("copyreg"); |
| 6330 | if (!copyreg) |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6331 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6332 | dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table"); |
| 6333 | if (!dispatch_table) |
| 6334 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6335 | extension_registry = \ |
| 6336 | PyObject_GetAttrString(copyreg, "_extension_registry"); |
| 6337 | if (!extension_registry) |
| 6338 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6339 | inverted_registry = PyObject_GetAttrString(copyreg, "_inverted_registry"); |
| 6340 | if (!inverted_registry) |
| 6341 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6342 | extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache"); |
| 6343 | if (!extension_cache) |
| 6344 | goto error; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6345 | Py_CLEAR(copyreg); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6346 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6347 | /* Load the 2.x -> 3.x stdlib module mapping tables */ |
| 6348 | compat_pickle = PyImport_ImportModule("_compat_pickle"); |
| 6349 | if (!compat_pickle) |
| 6350 | goto error; |
| 6351 | name_mapping_2to3 = PyObject_GetAttrString(compat_pickle, "NAME_MAPPING"); |
| 6352 | if (!name_mapping_2to3) |
| 6353 | goto error; |
| 6354 | if (!PyDict_CheckExact(name_mapping_2to3)) { |
| 6355 | PyErr_Format(PyExc_RuntimeError, |
| 6356 | "_compat_pickle.NAME_MAPPING should be a dict, not %.200s", |
| 6357 | Py_TYPE(name_mapping_2to3)->tp_name); |
| 6358 | goto error; |
| 6359 | } |
| 6360 | import_mapping_2to3 = PyObject_GetAttrString(compat_pickle, |
| 6361 | "IMPORT_MAPPING"); |
| 6362 | if (!import_mapping_2to3) |
| 6363 | goto error; |
| 6364 | if (!PyDict_CheckExact(import_mapping_2to3)) { |
| 6365 | PyErr_Format(PyExc_RuntimeError, |
| 6366 | "_compat_pickle.IMPORT_MAPPING should be a dict, " |
| 6367 | "not %.200s", Py_TYPE(import_mapping_2to3)->tp_name); |
| 6368 | goto error; |
| 6369 | } |
| 6370 | /* ... and the 3.x -> 2.x mapping tables */ |
| 6371 | name_mapping_3to2 = PyObject_GetAttrString(compat_pickle, |
| 6372 | "REVERSE_NAME_MAPPING"); |
| 6373 | if (!name_mapping_3to2) |
| 6374 | goto error; |
| 6375 | if (!PyDict_CheckExact(name_mapping_3to2)) { |
| 6376 | PyErr_Format(PyExc_RuntimeError, |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 6377 | "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, " |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6378 | "not %.200s", Py_TYPE(name_mapping_3to2)->tp_name); |
| 6379 | goto error; |
| 6380 | } |
| 6381 | import_mapping_3to2 = PyObject_GetAttrString(compat_pickle, |
| 6382 | "REVERSE_IMPORT_MAPPING"); |
| 6383 | if (!import_mapping_3to2) |
| 6384 | goto error; |
| 6385 | if (!PyDict_CheckExact(import_mapping_3to2)) { |
| 6386 | PyErr_Format(PyExc_RuntimeError, |
| 6387 | "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, " |
| 6388 | "not %.200s", Py_TYPE(import_mapping_3to2)->tp_name); |
| 6389 | goto error; |
| 6390 | } |
| 6391 | Py_CLEAR(compat_pickle); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6392 | |
| 6393 | empty_tuple = PyTuple_New(0); |
| 6394 | if (empty_tuple == NULL) |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6395 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6396 | two_tuple = PyTuple_New(2); |
| 6397 | if (two_tuple == NULL) |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6398 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6399 | /* We use this temp container with no regard to refcounts, or to |
| 6400 | * keeping containees alive. Exempt from GC, because we don't |
| 6401 | * want anything looking at two_tuple() by magic. |
| 6402 | */ |
| 6403 | PyObject_GC_UnTrack(two_tuple); |
| 6404 | |
| 6405 | return 0; |
| 6406 | |
| 6407 | error: |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6408 | Py_CLEAR(copyreg); |
| 6409 | Py_CLEAR(dispatch_table); |
| 6410 | Py_CLEAR(extension_registry); |
| 6411 | Py_CLEAR(inverted_registry); |
| 6412 | Py_CLEAR(extension_cache); |
| 6413 | Py_CLEAR(compat_pickle); |
| 6414 | Py_CLEAR(name_mapping_2to3); |
| 6415 | Py_CLEAR(import_mapping_2to3); |
| 6416 | Py_CLEAR(name_mapping_3to2); |
| 6417 | Py_CLEAR(import_mapping_3to2); |
| 6418 | Py_CLEAR(empty_tuple); |
| 6419 | Py_CLEAR(two_tuple); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6420 | return -1; |
| 6421 | } |
| 6422 | |
| 6423 | static struct PyModuleDef _picklemodule = { |
| 6424 | PyModuleDef_HEAD_INIT, |
| 6425 | "_pickle", |
| 6426 | pickle_module_doc, |
| 6427 | -1, |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6428 | pickle_methods, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6429 | NULL, |
| 6430 | NULL, |
| 6431 | NULL, |
| 6432 | NULL |
| 6433 | }; |
| 6434 | |
| 6435 | PyMODINIT_FUNC |
| 6436 | PyInit__pickle(void) |
| 6437 | { |
| 6438 | PyObject *m; |
| 6439 | |
| 6440 | if (PyType_Ready(&Unpickler_Type) < 0) |
| 6441 | return NULL; |
| 6442 | if (PyType_Ready(&Pickler_Type) < 0) |
| 6443 | return NULL; |
| 6444 | if (PyType_Ready(&Pdata_Type) < 0) |
| 6445 | return NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6446 | if (PyType_Ready(&PicklerMemoProxyType) < 0) |
| 6447 | return NULL; |
| 6448 | if (PyType_Ready(&UnpicklerMemoProxyType) < 0) |
| 6449 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6450 | |
| 6451 | /* Create the module and add the functions. */ |
| 6452 | m = PyModule_Create(&_picklemodule); |
| 6453 | if (m == NULL) |
| 6454 | return NULL; |
| 6455 | |
Antoine Pitrou | 8391cf4 | 2011-07-15 21:01:21 +0200 | [diff] [blame] | 6456 | Py_INCREF(&Pickler_Type); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6457 | if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0) |
| 6458 | return NULL; |
Antoine Pitrou | 8391cf4 | 2011-07-15 21:01:21 +0200 | [diff] [blame] | 6459 | Py_INCREF(&Unpickler_Type); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6460 | if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0) |
| 6461 | return NULL; |
| 6462 | |
| 6463 | /* Initialize the exceptions. */ |
| 6464 | PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL); |
| 6465 | if (PickleError == NULL) |
| 6466 | return NULL; |
| 6467 | PicklingError = \ |
| 6468 | PyErr_NewException("_pickle.PicklingError", PickleError, NULL); |
| 6469 | if (PicklingError == NULL) |
| 6470 | return NULL; |
| 6471 | UnpicklingError = \ |
| 6472 | PyErr_NewException("_pickle.UnpicklingError", PickleError, NULL); |
| 6473 | if (UnpicklingError == NULL) |
| 6474 | return NULL; |
| 6475 | |
| 6476 | if (PyModule_AddObject(m, "PickleError", PickleError) < 0) |
| 6477 | return NULL; |
| 6478 | if (PyModule_AddObject(m, "PicklingError", PicklingError) < 0) |
| 6479 | return NULL; |
| 6480 | if (PyModule_AddObject(m, "UnpicklingError", UnpicklingError) < 0) |
| 6481 | return NULL; |
| 6482 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6483 | if (initmodule() < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6484 | return NULL; |
| 6485 | |
| 6486 | return m; |
| 6487 | } |