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 { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 9 | HIGHEST_PROTOCOL = 4, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 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', |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 74 | SHORT_BINBYTES = 'C', |
| 75 | |
| 76 | /* Protocol 4 */ |
| 77 | SHORT_BINUNICODE = '\x8c', |
| 78 | BINUNICODE8 = '\x8d', |
| 79 | BINBYTES8 = '\x8e', |
| 80 | EMPTY_SET = '\x8f', |
| 81 | ADDITEMS = '\x90', |
| 82 | FROZENSET = '\x91', |
| 83 | NEWOBJ_EX = '\x92', |
| 84 | STACK_GLOBAL = '\x93', |
| 85 | MEMOIZE = '\x94', |
| 86 | FRAME = '\x95' |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | /* These aren't opcodes -- they're ways to pickle bools before protocol 2 |
| 90 | * so that unpicklers written before bools were introduced unpickle them |
| 91 | * as ints, but unpicklers after can recognize that bools were intended. |
| 92 | * Note that protocol 2 added direct ways to pickle bools. |
| 93 | */ |
| 94 | #undef TRUE |
| 95 | #define TRUE "I01\n" |
| 96 | #undef FALSE |
| 97 | #define FALSE "I00\n" |
| 98 | |
| 99 | enum { |
| 100 | /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements |
| 101 | batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will |
| 102 | break if this gets out of synch with pickle.py, but it's unclear that would |
| 103 | help anything either. */ |
| 104 | BATCHSIZE = 1000, |
| 105 | |
| 106 | /* Nesting limit until Pickler, when running in "fast mode", starts |
| 107 | checking for self-referential data-structures. */ |
| 108 | FAST_NESTING_LIMIT = 50, |
| 109 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 110 | /* Initial size of the write buffer of Pickler. */ |
| 111 | WRITE_BUF_SIZE = 4096, |
| 112 | |
| 113 | /* Maximum size of the write buffer of Pickler when pickling to a |
| 114 | stream. This is ignored for in-memory pickling. */ |
| 115 | MAX_WRITE_BUF_SIZE = 64 * 1024, |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 116 | |
| 117 | /* Prefetch size when unpickling (disabled on unpeekable streams) */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 118 | PREFETCH = 8192 * 16, |
| 119 | |
| 120 | FRAME_SIZE_TARGET = 64 * 1024, |
| 121 | |
| 122 | FRAME_HEADER_SIZE = 9 |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | /* Exception classes for pickle. These should override the ones defined in |
| 126 | pickle.py, when the C-optimized Pickler and Unpickler are used. */ |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 127 | static PyObject *PickleError = NULL; |
| 128 | static PyObject *PicklingError = NULL; |
| 129 | static PyObject *UnpicklingError = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 130 | |
| 131 | /* copyreg.dispatch_table, {type_object: pickling_function} */ |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 132 | static PyObject *dispatch_table = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 133 | /* For EXT[124] opcodes. */ |
| 134 | /* copyreg._extension_registry, {(module_name, function_name): code} */ |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 135 | static PyObject *extension_registry = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 136 | /* copyreg._inverted_registry, {code: (module_name, function_name)} */ |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 137 | static PyObject *inverted_registry = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 138 | /* copyreg._extension_cache, {code: object} */ |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 139 | static PyObject *extension_cache = NULL; |
| 140 | |
| 141 | /* _compat_pickle.NAME_MAPPING, {(oldmodule, oldname): (newmodule, newname)} */ |
| 142 | static PyObject *name_mapping_2to3 = NULL; |
| 143 | /* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */ |
| 144 | static PyObject *import_mapping_2to3 = NULL; |
| 145 | /* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */ |
| 146 | static PyObject *name_mapping_3to2 = NULL; |
| 147 | static PyObject *import_mapping_3to2 = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 148 | |
| 149 | /* XXX: Are these really nescessary? */ |
| 150 | /* As the name says, an empty tuple. */ |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 151 | static PyObject *empty_tuple = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 152 | /* For looking up name pairs in copyreg._extension_registry. */ |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 153 | static PyObject *two_tuple = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 154 | |
| 155 | static int |
| 156 | stack_underflow(void) |
| 157 | { |
| 158 | PyErr_SetString(UnpicklingError, "unpickling stack underflow"); |
| 159 | return -1; |
| 160 | } |
| 161 | |
| 162 | /* Internal data type used as the unpickling stack. */ |
| 163 | typedef struct { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 164 | PyObject_VAR_HEAD |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 165 | PyObject **data; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 166 | Py_ssize_t allocated; /* number of slots in data allocated */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 167 | } Pdata; |
| 168 | |
| 169 | static void |
| 170 | Pdata_dealloc(Pdata *self) |
| 171 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 172 | Py_ssize_t i = Py_SIZE(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 173 | while (--i >= 0) { |
| 174 | Py_DECREF(self->data[i]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 175 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 176 | PyMem_FREE(self->data); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 177 | PyObject_Del(self); |
| 178 | } |
| 179 | |
| 180 | static PyTypeObject Pdata_Type = { |
| 181 | PyVarObject_HEAD_INIT(NULL, 0) |
| 182 | "_pickle.Pdata", /*tp_name*/ |
| 183 | sizeof(Pdata), /*tp_basicsize*/ |
| 184 | 0, /*tp_itemsize*/ |
| 185 | (destructor)Pdata_dealloc, /*tp_dealloc*/ |
| 186 | }; |
| 187 | |
| 188 | static PyObject * |
| 189 | Pdata_New(void) |
| 190 | { |
| 191 | Pdata *self; |
| 192 | |
| 193 | if (!(self = PyObject_New(Pdata, &Pdata_Type))) |
| 194 | return NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 195 | Py_SIZE(self) = 0; |
| 196 | self->allocated = 8; |
| 197 | self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 198 | if (self->data) |
| 199 | return (PyObject *)self; |
| 200 | Py_DECREF(self); |
| 201 | return PyErr_NoMemory(); |
| 202 | } |
| 203 | |
| 204 | |
| 205 | /* Retain only the initial clearto items. If clearto >= the current |
| 206 | * number of items, this is a (non-erroneous) NOP. |
| 207 | */ |
| 208 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 209 | Pdata_clear(Pdata *self, Py_ssize_t clearto) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 210 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 211 | Py_ssize_t i = Py_SIZE(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 212 | |
| 213 | if (clearto < 0) |
| 214 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 215 | if (clearto >= i) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 216 | return 0; |
| 217 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 218 | while (--i >= clearto) { |
| 219 | Py_CLEAR(self->data[i]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 220 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 221 | Py_SIZE(self) = clearto; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | static int |
| 226 | Pdata_grow(Pdata *self) |
| 227 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 228 | PyObject **data = self->data; |
| 229 | Py_ssize_t allocated = self->allocated; |
| 230 | Py_ssize_t new_allocated; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 231 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 232 | new_allocated = (allocated >> 3) + 6; |
| 233 | /* check for integer overflow */ |
| 234 | if (new_allocated > PY_SSIZE_T_MAX - allocated) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 235 | goto nomemory; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 236 | new_allocated += allocated; |
| 237 | if (new_allocated > (PY_SSIZE_T_MAX / sizeof(PyObject *))) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 238 | goto nomemory; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 239 | data = PyMem_REALLOC(data, new_allocated * sizeof(PyObject *)); |
| 240 | if (data == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 241 | goto nomemory; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 242 | |
| 243 | self->data = data; |
| 244 | self->allocated = new_allocated; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 245 | return 0; |
| 246 | |
| 247 | nomemory: |
| 248 | PyErr_NoMemory(); |
| 249 | return -1; |
| 250 | } |
| 251 | |
| 252 | /* D is a Pdata*. Pop the topmost element and store it into V, which |
| 253 | * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError |
| 254 | * is raised and V is set to NULL. |
| 255 | */ |
| 256 | static PyObject * |
| 257 | Pdata_pop(Pdata *self) |
| 258 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 259 | if (Py_SIZE(self) == 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 260 | PyErr_SetString(UnpicklingError, "bad pickle data"); |
| 261 | return NULL; |
| 262 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 263 | return self->data[--Py_SIZE(self)]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 264 | } |
| 265 | #define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0) |
| 266 | |
| 267 | static int |
| 268 | Pdata_push(Pdata *self, PyObject *obj) |
| 269 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 270 | if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 271 | return -1; |
| 272 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 273 | self->data[Py_SIZE(self)++] = obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | /* Push an object on stack, transferring its ownership to the stack. */ |
| 278 | #define PDATA_PUSH(D, O, ER) do { \ |
| 279 | if (Pdata_push((D), (O)) < 0) return (ER); } while(0) |
| 280 | |
| 281 | /* Push an object on stack, adding a new reference to the object. */ |
| 282 | #define PDATA_APPEND(D, O, ER) do { \ |
| 283 | Py_INCREF((O)); \ |
| 284 | if (Pdata_push((D), (O)) < 0) return (ER); } while(0) |
| 285 | |
| 286 | static PyObject * |
| 287 | Pdata_poptuple(Pdata *self, Py_ssize_t start) |
| 288 | { |
| 289 | PyObject *tuple; |
| 290 | Py_ssize_t len, i, j; |
| 291 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 292 | len = Py_SIZE(self) - start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 293 | tuple = PyTuple_New(len); |
| 294 | if (tuple == NULL) |
| 295 | return NULL; |
| 296 | for (i = start, j = 0; j < len; i++, j++) |
| 297 | PyTuple_SET_ITEM(tuple, j, self->data[i]); |
| 298 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 299 | Py_SIZE(self) = start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 300 | return tuple; |
| 301 | } |
| 302 | |
| 303 | static PyObject * |
| 304 | Pdata_poplist(Pdata *self, Py_ssize_t start) |
| 305 | { |
| 306 | PyObject *list; |
| 307 | Py_ssize_t len, i, j; |
| 308 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 309 | len = Py_SIZE(self) - start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 310 | list = PyList_New(len); |
| 311 | if (list == NULL) |
| 312 | return NULL; |
| 313 | for (i = start, j = 0; j < len; i++, j++) |
| 314 | PyList_SET_ITEM(list, j, self->data[i]); |
| 315 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 316 | Py_SIZE(self) = start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 317 | return list; |
| 318 | } |
| 319 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 320 | typedef struct { |
| 321 | PyObject *me_key; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 322 | Py_ssize_t me_value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 323 | } PyMemoEntry; |
| 324 | |
| 325 | typedef struct { |
| 326 | Py_ssize_t mt_mask; |
| 327 | Py_ssize_t mt_used; |
| 328 | Py_ssize_t mt_allocated; |
| 329 | PyMemoEntry *mt_table; |
| 330 | } PyMemoTable; |
| 331 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 332 | typedef struct PicklerObject { |
| 333 | PyObject_HEAD |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 334 | PyMemoTable *memo; /* Memo table, keep track of the seen |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 335 | objects to support self-referential objects |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 336 | pickling. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 337 | PyObject *pers_func; /* persistent_id() method, can be NULL */ |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 338 | PyObject *dispatch_table; /* private dispatch_table, can be NULL */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 339 | PyObject *arg; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 340 | |
| 341 | PyObject *write; /* write() method of the output stream. */ |
| 342 | PyObject *output_buffer; /* Write into a local bytearray buffer before |
| 343 | flushing to the stream. */ |
| 344 | Py_ssize_t output_len; /* Length of output_buffer. */ |
| 345 | Py_ssize_t max_output_len; /* Allocation size of output_buffer. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 346 | int proto; /* Pickle protocol number, >= 0 */ |
| 347 | int bin; /* Boolean, true if proto > 0 */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 348 | int framing; /* True when framing is enabled, proto >= 4 */ |
| 349 | Py_ssize_t frame_start; /* Position in output_buffer where the |
| 350 | where the current frame begins. -1 if there |
| 351 | is no frame currently open. */ |
| 352 | |
| 353 | Py_ssize_t buf_size; /* Size of the current buffered pickle data */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 354 | int fast; /* Enable fast mode if set to a true value. |
| 355 | The fast mode disable the usage of memo, |
| 356 | therefore speeding the pickling process by |
| 357 | not generating superfluous PUT opcodes. It |
| 358 | should not be used if with self-referential |
| 359 | objects. */ |
| 360 | int fast_nesting; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 361 | int fix_imports; /* Indicate whether Pickler should fix |
| 362 | the name of globals for Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 363 | PyObject *fast_memo; |
| 364 | } PicklerObject; |
| 365 | |
| 366 | typedef struct UnpicklerObject { |
| 367 | PyObject_HEAD |
| 368 | Pdata *stack; /* Pickle data stack, store unpickled objects. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 369 | |
| 370 | /* The unpickler memo is just an array of PyObject *s. Using a dict |
| 371 | is unnecessary, since the keys are contiguous ints. */ |
| 372 | PyObject **memo; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 373 | Py_ssize_t memo_size; /* Capacity of the memo array */ |
| 374 | Py_ssize_t memo_len; /* Number of objects in the memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 375 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 376 | PyObject *arg; |
| 377 | PyObject *pers_func; /* persistent_load() method, can be NULL. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 378 | |
| 379 | Py_buffer buffer; |
| 380 | char *input_buffer; |
| 381 | char *input_line; |
| 382 | Py_ssize_t input_len; |
| 383 | Py_ssize_t next_read_idx; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 384 | Py_ssize_t frame_end_idx; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 385 | Py_ssize_t prefetched_idx; /* index of first prefetched byte */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 386 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 387 | PyObject *read; /* read() method of the input stream. */ |
| 388 | PyObject *readline; /* readline() method of the input stream. */ |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 389 | PyObject *peek; /* peek() method of the input stream, or NULL */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 390 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 391 | char *encoding; /* Name of the encoding to be used for |
| 392 | decoding strings pickled using Python |
| 393 | 2.x. The default value is "ASCII" */ |
| 394 | char *errors; /* Name of errors handling scheme to used when |
| 395 | decoding strings. The default value is |
| 396 | "strict". */ |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 397 | Py_ssize_t *marks; /* Mark stack, used for unpickling container |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 398 | objects. */ |
| 399 | Py_ssize_t num_marks; /* Number of marks in the mark stack. */ |
| 400 | Py_ssize_t marks_size; /* Current allocated size of the mark stack. */ |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 401 | int proto; /* Protocol of the pickle loaded. */ |
| 402 | int fix_imports; /* Indicate whether Unpickler should fix |
| 403 | the name of globals pickled by Python 2.x. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 404 | int framing; /* True when framing is enabled, proto >= 4 */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 405 | } UnpicklerObject; |
| 406 | |
| 407 | /* Forward declarations */ |
| 408 | static int save(PicklerObject *, PyObject *, int); |
| 409 | static int save_reduce(PicklerObject *, PyObject *, PyObject *); |
| 410 | static PyTypeObject Pickler_Type; |
| 411 | static PyTypeObject Unpickler_Type; |
| 412 | |
| 413 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 414 | /************************************************************************* |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 415 | A custom hashtable mapping void* to Python ints. This is used by the pickler |
| 416 | for memoization. Using a custom hashtable rather than PyDict allows us to skip |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 417 | a bunch of unnecessary object creation. This makes a huge performance |
| 418 | difference. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 419 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 420 | #define MT_MINSIZE 8 |
| 421 | #define PERTURB_SHIFT 5 |
| 422 | |
| 423 | |
| 424 | static PyMemoTable * |
| 425 | PyMemoTable_New(void) |
| 426 | { |
| 427 | PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable)); |
| 428 | if (memo == NULL) { |
| 429 | PyErr_NoMemory(); |
| 430 | return NULL; |
| 431 | } |
| 432 | |
| 433 | memo->mt_used = 0; |
| 434 | memo->mt_allocated = MT_MINSIZE; |
| 435 | memo->mt_mask = MT_MINSIZE - 1; |
| 436 | memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry)); |
| 437 | if (memo->mt_table == NULL) { |
| 438 | PyMem_FREE(memo); |
| 439 | PyErr_NoMemory(); |
| 440 | return NULL; |
| 441 | } |
| 442 | memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry)); |
| 443 | |
| 444 | return memo; |
| 445 | } |
| 446 | |
| 447 | static PyMemoTable * |
| 448 | PyMemoTable_Copy(PyMemoTable *self) |
| 449 | { |
| 450 | Py_ssize_t i; |
| 451 | PyMemoTable *new = PyMemoTable_New(); |
| 452 | if (new == NULL) |
| 453 | return NULL; |
| 454 | |
| 455 | new->mt_used = self->mt_used; |
| 456 | new->mt_allocated = self->mt_allocated; |
| 457 | new->mt_mask = self->mt_mask; |
| 458 | /* The table we get from _New() is probably smaller than we wanted. |
| 459 | Free it and allocate one that's the right size. */ |
| 460 | PyMem_FREE(new->mt_table); |
| 461 | new->mt_table = PyMem_MALLOC(self->mt_allocated * sizeof(PyMemoEntry)); |
| 462 | if (new->mt_table == NULL) { |
| 463 | PyMem_FREE(new); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 464 | PyErr_NoMemory(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 465 | return NULL; |
| 466 | } |
| 467 | for (i = 0; i < self->mt_allocated; i++) { |
| 468 | Py_XINCREF(self->mt_table[i].me_key); |
| 469 | } |
| 470 | memcpy(new->mt_table, self->mt_table, |
| 471 | sizeof(PyMemoEntry) * self->mt_allocated); |
| 472 | |
| 473 | return new; |
| 474 | } |
| 475 | |
| 476 | static Py_ssize_t |
| 477 | PyMemoTable_Size(PyMemoTable *self) |
| 478 | { |
| 479 | return self->mt_used; |
| 480 | } |
| 481 | |
| 482 | static int |
| 483 | PyMemoTable_Clear(PyMemoTable *self) |
| 484 | { |
| 485 | Py_ssize_t i = self->mt_allocated; |
| 486 | |
| 487 | while (--i >= 0) { |
| 488 | Py_XDECREF(self->mt_table[i].me_key); |
| 489 | } |
| 490 | self->mt_used = 0; |
| 491 | memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry)); |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | static void |
| 496 | PyMemoTable_Del(PyMemoTable *self) |
| 497 | { |
| 498 | if (self == NULL) |
| 499 | return; |
| 500 | PyMemoTable_Clear(self); |
| 501 | |
| 502 | PyMem_FREE(self->mt_table); |
| 503 | PyMem_FREE(self); |
| 504 | } |
| 505 | |
| 506 | /* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup() |
| 507 | can be considerably simpler than dictobject.c's lookdict(). */ |
| 508 | static PyMemoEntry * |
| 509 | _PyMemoTable_Lookup(PyMemoTable *self, PyObject *key) |
| 510 | { |
| 511 | size_t i; |
| 512 | size_t perturb; |
| 513 | size_t mask = (size_t)self->mt_mask; |
| 514 | PyMemoEntry *table = self->mt_table; |
| 515 | PyMemoEntry *entry; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 516 | Py_hash_t hash = (Py_hash_t)key >> 3; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 517 | |
| 518 | i = hash & mask; |
| 519 | entry = &table[i]; |
| 520 | if (entry->me_key == NULL || entry->me_key == key) |
| 521 | return entry; |
| 522 | |
| 523 | for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
| 524 | i = (i << 2) + i + perturb + 1; |
| 525 | entry = &table[i & mask]; |
| 526 | if (entry->me_key == NULL || entry->me_key == key) |
| 527 | return entry; |
| 528 | } |
| 529 | assert(0); /* Never reached */ |
| 530 | return NULL; |
| 531 | } |
| 532 | |
| 533 | /* Returns -1 on failure, 0 on success. */ |
| 534 | static int |
| 535 | _PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size) |
| 536 | { |
| 537 | PyMemoEntry *oldtable = NULL; |
| 538 | PyMemoEntry *oldentry, *newentry; |
| 539 | Py_ssize_t new_size = MT_MINSIZE; |
| 540 | Py_ssize_t to_process; |
| 541 | |
| 542 | assert(min_size > 0); |
| 543 | |
| 544 | /* Find the smallest valid table size >= min_size. */ |
| 545 | while (new_size < min_size && new_size > 0) |
| 546 | new_size <<= 1; |
| 547 | if (new_size <= 0) { |
| 548 | PyErr_NoMemory(); |
| 549 | return -1; |
| 550 | } |
| 551 | /* new_size needs to be a power of two. */ |
| 552 | assert((new_size & (new_size - 1)) == 0); |
| 553 | |
| 554 | /* Allocate new table. */ |
| 555 | oldtable = self->mt_table; |
| 556 | self->mt_table = PyMem_MALLOC(new_size * sizeof(PyMemoEntry)); |
| 557 | if (self->mt_table == NULL) { |
Victor Stinner | 8ca72e2 | 2013-07-12 00:53:26 +0200 | [diff] [blame] | 558 | self->mt_table = oldtable; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 559 | PyErr_NoMemory(); |
| 560 | return -1; |
| 561 | } |
| 562 | self->mt_allocated = new_size; |
| 563 | self->mt_mask = new_size - 1; |
| 564 | memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size); |
| 565 | |
| 566 | /* Copy entries from the old table. */ |
| 567 | to_process = self->mt_used; |
| 568 | for (oldentry = oldtable; to_process > 0; oldentry++) { |
| 569 | if (oldentry->me_key != NULL) { |
| 570 | to_process--; |
| 571 | /* newentry is a pointer to a chunk of the new |
| 572 | mt_table, so we're setting the key:value pair |
| 573 | in-place. */ |
| 574 | newentry = _PyMemoTable_Lookup(self, oldentry->me_key); |
| 575 | newentry->me_key = oldentry->me_key; |
| 576 | newentry->me_value = oldentry->me_value; |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | /* Deallocate the old table. */ |
| 581 | PyMem_FREE(oldtable); |
| 582 | return 0; |
| 583 | } |
| 584 | |
| 585 | /* Returns NULL on failure, a pointer to the value otherwise. */ |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 586 | static Py_ssize_t * |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 587 | PyMemoTable_Get(PyMemoTable *self, PyObject *key) |
| 588 | { |
| 589 | PyMemoEntry *entry = _PyMemoTable_Lookup(self, key); |
| 590 | if (entry->me_key == NULL) |
| 591 | return NULL; |
| 592 | return &entry->me_value; |
| 593 | } |
| 594 | |
| 595 | /* Returns -1 on failure, 0 on success. */ |
| 596 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 597 | PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 598 | { |
| 599 | PyMemoEntry *entry; |
| 600 | |
| 601 | assert(key != NULL); |
| 602 | |
| 603 | entry = _PyMemoTable_Lookup(self, key); |
| 604 | if (entry->me_key != NULL) { |
| 605 | entry->me_value = value; |
| 606 | return 0; |
| 607 | } |
| 608 | Py_INCREF(key); |
| 609 | entry->me_key = key; |
| 610 | entry->me_value = value; |
| 611 | self->mt_used++; |
| 612 | |
| 613 | /* If we added a key, we can safely resize. Otherwise just return! |
| 614 | * If used >= 2/3 size, adjust size. Normally, this quaduples the size. |
| 615 | * |
| 616 | * Quadrupling the size improves average table sparseness |
| 617 | * (reducing collisions) at the cost of some memory. It also halves |
| 618 | * the number of expensive resize operations in a growing memo table. |
| 619 | * |
| 620 | * Very large memo tables (over 50K items) use doubling instead. |
| 621 | * This may help applications with severe memory constraints. |
| 622 | */ |
| 623 | if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2)) |
| 624 | return 0; |
| 625 | return _PyMemoTable_ResizeTable(self, |
| 626 | (self->mt_used > 50000 ? 2 : 4) * self->mt_used); |
| 627 | } |
| 628 | |
| 629 | #undef MT_MINSIZE |
| 630 | #undef PERTURB_SHIFT |
| 631 | |
| 632 | /*************************************************************************/ |
| 633 | |
| 634 | /* Helpers for creating the argument tuple passed to functions. This has the |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 635 | performance advantage of calling PyTuple_New() only once. |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 636 | |
| 637 | XXX(avassalotti): Inline directly in _Pickler_FastCall() and |
| 638 | _Unpickler_FastCall(). */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 639 | #define ARG_TUP(self, obj) do { \ |
| 640 | if ((self)->arg || ((self)->arg=PyTuple_New(1))) { \ |
| 641 | Py_XDECREF(PyTuple_GET_ITEM((self)->arg, 0)); \ |
| 642 | PyTuple_SET_ITEM((self)->arg, 0, (obj)); \ |
| 643 | } \ |
| 644 | else { \ |
| 645 | Py_DECREF((obj)); \ |
| 646 | } \ |
| 647 | } while (0) |
| 648 | |
| 649 | #define FREE_ARG_TUP(self) do { \ |
| 650 | if ((self)->arg->ob_refcnt > 1) \ |
| 651 | Py_CLEAR((self)->arg); \ |
| 652 | } while (0) |
| 653 | |
| 654 | /* A temporary cleaner API for fast single argument function call. |
| 655 | |
| 656 | XXX: Does caching the argument tuple provides any real performance benefits? |
| 657 | |
| 658 | A quick benchmark, on a 2.0GHz Athlon64 3200+ running Linux 2.6.24 with |
| 659 | glibc 2.7, tells me that it takes roughly 20,000,000 PyTuple_New(1) calls |
| 660 | when the tuple is retrieved from the freelist (i.e, call PyTuple_New() then |
| 661 | immediately DECREF it) and 1,200,000 calls when allocating brand new tuples |
| 662 | (i.e, call PyTuple_New() and store the returned value in an array), to save |
| 663 | one second (wall clock time). Either ways, the loading time a pickle stream |
| 664 | large enough to generate this number of calls would be massively |
| 665 | overwhelmed by other factors, like I/O throughput, the GC traversal and |
| 666 | object allocation overhead. So, I really doubt these functions provide any |
| 667 | real benefits. |
| 668 | |
| 669 | On the other hand, oprofile reports that pickle spends a lot of time in |
| 670 | these functions. But, that is probably more related to the function call |
| 671 | overhead, than the argument tuple allocation. |
| 672 | |
| 673 | XXX: And, what is the reference behavior of these? Steal, borrow? At first |
| 674 | 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] | 675 | of 'func'. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 676 | static PyObject * |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 677 | _Pickler_FastCall(PicklerObject *self, PyObject *func, PyObject *arg) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 678 | { |
| 679 | PyObject *result = NULL; |
| 680 | |
| 681 | ARG_TUP(self, arg); |
| 682 | if (self->arg) { |
| 683 | result = PyObject_Call(func, self->arg, NULL); |
| 684 | FREE_ARG_TUP(self); |
| 685 | } |
| 686 | return result; |
| 687 | } |
| 688 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 689 | static int |
| 690 | _Pickler_ClearBuffer(PicklerObject *self) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 691 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 692 | Py_CLEAR(self->output_buffer); |
| 693 | self->output_buffer = |
| 694 | PyBytes_FromStringAndSize(NULL, self->max_output_len); |
| 695 | if (self->output_buffer == NULL) |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 696 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 697 | self->output_len = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 698 | self->frame_start = -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 699 | return 0; |
| 700 | } |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 701 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 702 | static void |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 703 | _write_size64(char *out, size_t value) |
| 704 | { |
| 705 | out[0] = (unsigned char)(value & 0xff); |
| 706 | out[1] = (unsigned char)((value >> 8) & 0xff); |
| 707 | out[2] = (unsigned char)((value >> 16) & 0xff); |
| 708 | out[3] = (unsigned char)((value >> 24) & 0xff); |
| 709 | #if SIZEOF_SIZE_T >= 8 |
| 710 | out[4] = (unsigned char)((value >> 32) & 0xff); |
| 711 | out[5] = (unsigned char)((value >> 40) & 0xff); |
| 712 | out[6] = (unsigned char)((value >> 48) & 0xff); |
| 713 | out[7] = (unsigned char)((value >> 56) & 0xff); |
| 714 | #else |
| 715 | out[4] = out[5] = out[6] = out[7] = 0; |
| 716 | #endif |
| 717 | } |
| 718 | |
| 719 | static void |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 720 | _Pickler_WriteFrameHeader(PicklerObject *self, char *qdata, size_t frame_len) |
| 721 | { |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 722 | qdata[0] = FRAME; |
| 723 | _write_size64(qdata + 1, frame_len); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | static int |
| 727 | _Pickler_CommitFrame(PicklerObject *self) |
| 728 | { |
| 729 | size_t frame_len; |
| 730 | char *qdata; |
| 731 | |
| 732 | if (!self->framing || self->frame_start == -1) |
| 733 | return 0; |
| 734 | frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE; |
| 735 | qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start; |
| 736 | _Pickler_WriteFrameHeader(self, qdata, frame_len); |
| 737 | self->frame_start = -1; |
| 738 | return 0; |
| 739 | } |
| 740 | |
| 741 | static int |
| 742 | _Pickler_OpcodeBoundary(PicklerObject *self) |
| 743 | { |
| 744 | Py_ssize_t frame_len; |
| 745 | |
| 746 | if (!self->framing || self->frame_start == -1) |
| 747 | return 0; |
| 748 | frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE; |
| 749 | if (frame_len >= FRAME_SIZE_TARGET) |
| 750 | return _Pickler_CommitFrame(self); |
| 751 | else |
| 752 | return 0; |
| 753 | } |
| 754 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 755 | static PyObject * |
| 756 | _Pickler_GetString(PicklerObject *self) |
| 757 | { |
| 758 | PyObject *output_buffer = self->output_buffer; |
| 759 | |
| 760 | assert(self->output_buffer != NULL); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 761 | |
| 762 | if (_Pickler_CommitFrame(self)) |
| 763 | return NULL; |
| 764 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 765 | self->output_buffer = NULL; |
| 766 | /* Resize down to exact size */ |
| 767 | if (_PyBytes_Resize(&output_buffer, self->output_len) < 0) |
| 768 | return NULL; |
| 769 | return output_buffer; |
| 770 | } |
| 771 | |
| 772 | static int |
| 773 | _Pickler_FlushToFile(PicklerObject *self) |
| 774 | { |
| 775 | PyObject *output, *result; |
| 776 | |
| 777 | assert(self->write != NULL); |
| 778 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 779 | /* This will commit the frame first */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 780 | output = _Pickler_GetString(self); |
| 781 | if (output == NULL) |
| 782 | return -1; |
| 783 | |
| 784 | result = _Pickler_FastCall(self, self->write, output); |
| 785 | Py_XDECREF(result); |
| 786 | return (result == NULL) ? -1 : 0; |
| 787 | } |
| 788 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 789 | static Py_ssize_t |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 790 | _Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 791 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 792 | Py_ssize_t i, n, required; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 793 | char *buffer; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 794 | int need_new_frame; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 795 | |
| 796 | assert(s != NULL); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 797 | need_new_frame = (self->framing && self->frame_start == -1); |
| 798 | |
| 799 | if (need_new_frame) |
| 800 | n = data_len + FRAME_HEADER_SIZE; |
| 801 | else |
| 802 | n = data_len; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 803 | |
| 804 | required = self->output_len + n; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 805 | if (self->write != NULL && required > MAX_WRITE_BUF_SIZE) { |
| 806 | /* XXX This reallocates a new buffer every time, which is a bit |
| 807 | wasteful. */ |
| 808 | if (_Pickler_FlushToFile(self) < 0) |
| 809 | return -1; |
| 810 | if (_Pickler_ClearBuffer(self) < 0) |
| 811 | return -1; |
| 812 | /* The previous frame was just committed by _Pickler_FlushToFile */ |
| 813 | need_new_frame = self->framing; |
| 814 | if (need_new_frame) |
| 815 | n = data_len + FRAME_HEADER_SIZE; |
| 816 | else |
| 817 | n = data_len; |
| 818 | required = self->output_len + n; |
| 819 | } |
| 820 | if (self->write != NULL && n > MAX_WRITE_BUF_SIZE) { |
| 821 | /* For large pickle chunks, we write directly to the output |
| 822 | file instead of buffering. Note the buffer is empty at this |
| 823 | point (it was flushed above, since required >= n). */ |
| 824 | PyObject *output, *result; |
| 825 | if (need_new_frame) { |
| 826 | char frame_header[FRAME_HEADER_SIZE]; |
| 827 | _Pickler_WriteFrameHeader(self, frame_header, (size_t) data_len); |
| 828 | output = PyBytes_FromStringAndSize(frame_header, FRAME_HEADER_SIZE); |
| 829 | if (output == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 830 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 831 | result = _Pickler_FastCall(self, self->write, output); |
| 832 | Py_XDECREF(result); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 833 | if (result == NULL) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 834 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 835 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 836 | /* XXX we could spare an intermediate copy and pass |
| 837 | a memoryview instead */ |
| 838 | output = PyBytes_FromStringAndSize(s, data_len); |
| 839 | if (output == NULL) |
| 840 | return -1; |
| 841 | result = _Pickler_FastCall(self, self->write, output); |
| 842 | Py_XDECREF(result); |
| 843 | return (result == NULL) ? -1 : 0; |
| 844 | } |
| 845 | if (required > self->max_output_len) { |
| 846 | /* Make place in buffer for the pickle chunk */ |
| 847 | if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) { |
| 848 | PyErr_NoMemory(); |
| 849 | return -1; |
| 850 | } |
| 851 | self->max_output_len = (self->output_len + n) / 2 * 3; |
| 852 | if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0) |
| 853 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 854 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 855 | buffer = PyBytes_AS_STRING(self->output_buffer); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 856 | if (need_new_frame) { |
| 857 | /* Setup new frame */ |
| 858 | Py_ssize_t frame_start = self->output_len; |
| 859 | self->frame_start = frame_start; |
| 860 | for (i = 0; i < FRAME_HEADER_SIZE; i++) { |
| 861 | /* Write an invalid value, for debugging */ |
| 862 | buffer[frame_start + i] = 0xFE; |
| 863 | } |
| 864 | self->output_len += FRAME_HEADER_SIZE; |
| 865 | } |
| 866 | if (data_len < 8) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 867 | /* This is faster than memcpy when the string is short. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 868 | for (i = 0; i < data_len; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 869 | buffer[self->output_len + i] = s[i]; |
| 870 | } |
| 871 | } |
| 872 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 873 | memcpy(buffer + self->output_len, s, data_len); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 874 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 875 | self->output_len += data_len; |
| 876 | return data_len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 877 | } |
| 878 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 879 | static PicklerObject * |
| 880 | _Pickler_New(void) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 881 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 882 | PicklerObject *self; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 883 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 884 | self = PyObject_GC_New(PicklerObject, &Pickler_Type); |
| 885 | if (self == NULL) |
| 886 | return NULL; |
| 887 | |
| 888 | self->pers_func = NULL; |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 889 | self->dispatch_table = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 890 | self->arg = NULL; |
| 891 | self->write = NULL; |
| 892 | self->proto = 0; |
| 893 | self->bin = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 894 | self->framing = 0; |
| 895 | self->frame_start = -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 896 | self->fast = 0; |
| 897 | self->fast_nesting = 0; |
| 898 | self->fix_imports = 0; |
| 899 | self->fast_memo = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 900 | self->max_output_len = WRITE_BUF_SIZE; |
| 901 | self->output_len = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 902 | |
| 903 | self->memo = PyMemoTable_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 904 | self->output_buffer = PyBytes_FromStringAndSize(NULL, |
| 905 | self->max_output_len); |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 906 | |
| 907 | if (self->memo == NULL || self->output_buffer == NULL) { |
Victor Stinner | c31df04 | 2013-07-12 00:08:59 +0200 | [diff] [blame] | 908 | Py_DECREF(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 909 | return NULL; |
| 910 | } |
| 911 | return self; |
| 912 | } |
| 913 | |
| 914 | static int |
| 915 | _Pickler_SetProtocol(PicklerObject *self, PyObject *proto_obj, |
| 916 | PyObject *fix_imports_obj) |
| 917 | { |
| 918 | long proto = 0; |
| 919 | int fix_imports; |
| 920 | |
| 921 | if (proto_obj == NULL || proto_obj == Py_None) |
| 922 | proto = DEFAULT_PROTOCOL; |
| 923 | else { |
| 924 | proto = PyLong_AsLong(proto_obj); |
| 925 | if (proto == -1 && PyErr_Occurred()) |
| 926 | return -1; |
| 927 | } |
| 928 | if (proto < 0) |
| 929 | proto = HIGHEST_PROTOCOL; |
| 930 | if (proto > HIGHEST_PROTOCOL) { |
| 931 | PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d", |
| 932 | HIGHEST_PROTOCOL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 933 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 934 | } |
| 935 | fix_imports = PyObject_IsTrue(fix_imports_obj); |
| 936 | if (fix_imports == -1) |
| 937 | return -1; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 938 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 939 | self->proto = proto; |
| 940 | self->bin = proto > 0; |
| 941 | self->fix_imports = fix_imports && proto < 3; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 942 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 943 | return 0; |
| 944 | } |
| 945 | |
| 946 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 947 | be called once on a freshly created Pickler. */ |
| 948 | static int |
| 949 | _Pickler_SetOutputStream(PicklerObject *self, PyObject *file) |
| 950 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 951 | _Py_IDENTIFIER(write); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 952 | assert(file != NULL); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 953 | self->write = _PyObject_GetAttrId(file, &PyId_write); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 954 | if (self->write == NULL) { |
| 955 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 956 | PyErr_SetString(PyExc_TypeError, |
| 957 | "file must have a 'write' attribute"); |
| 958 | return -1; |
| 959 | } |
| 960 | |
| 961 | return 0; |
| 962 | } |
| 963 | |
| 964 | /* See documentation for _Pickler_FastCall(). */ |
| 965 | static PyObject * |
| 966 | _Unpickler_FastCall(UnpicklerObject *self, PyObject *func, PyObject *arg) |
| 967 | { |
| 968 | PyObject *result = NULL; |
| 969 | |
| 970 | ARG_TUP(self, arg); |
| 971 | if (self->arg) { |
| 972 | result = PyObject_Call(func, self->arg, NULL); |
| 973 | FREE_ARG_TUP(self); |
| 974 | } |
| 975 | return result; |
| 976 | } |
| 977 | |
| 978 | /* Returns the size of the input on success, -1 on failure. This takes its |
| 979 | own reference to `input`. */ |
| 980 | static Py_ssize_t |
| 981 | _Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input) |
| 982 | { |
| 983 | if (self->buffer.buf != NULL) |
| 984 | PyBuffer_Release(&self->buffer); |
| 985 | if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0) |
| 986 | return -1; |
| 987 | self->input_buffer = self->buffer.buf; |
| 988 | self->input_len = self->buffer.len; |
| 989 | self->next_read_idx = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 990 | self->frame_end_idx = -1; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 991 | self->prefetched_idx = self->input_len; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 992 | return self->input_len; |
| 993 | } |
| 994 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 995 | static int |
| 996 | _Unpickler_SkipConsumed(UnpicklerObject *self) |
| 997 | { |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 998 | Py_ssize_t consumed; |
| 999 | PyObject *r; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1000 | |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1001 | consumed = self->next_read_idx - self->prefetched_idx; |
| 1002 | if (consumed <= 0) |
| 1003 | return 0; |
| 1004 | |
| 1005 | assert(self->peek); /* otherwise we did something wrong */ |
| 1006 | /* This makes an useless copy... */ |
| 1007 | r = PyObject_CallFunction(self->read, "n", consumed); |
| 1008 | if (r == NULL) |
| 1009 | return -1; |
| 1010 | Py_DECREF(r); |
| 1011 | |
| 1012 | self->prefetched_idx = self->next_read_idx; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1013 | return 0; |
| 1014 | } |
| 1015 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1016 | static const Py_ssize_t READ_WHOLE_LINE = -1; |
| 1017 | |
| 1018 | /* If reading from a file, we need to only pull the bytes we need, since there |
| 1019 | may be multiple pickle objects arranged contiguously in the same input |
| 1020 | buffer. |
| 1021 | |
| 1022 | If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n` |
| 1023 | bytes from the input stream/buffer. |
| 1024 | |
| 1025 | Update the unpickler's input buffer with the newly-read data. Returns -1 on |
| 1026 | failure; on success, returns the number of bytes read from the file. |
| 1027 | |
| 1028 | On success, self->input_len will be 0; this is intentional so that when |
| 1029 | unpickling from a file, the "we've run out of data" code paths will trigger, |
| 1030 | causing the Unpickler to go back to the file for more data. Use the returned |
| 1031 | size to tell you how much data you can process. */ |
| 1032 | static Py_ssize_t |
| 1033 | _Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n) |
| 1034 | { |
| 1035 | PyObject *data; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1036 | Py_ssize_t read_size, prefetched_size = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1037 | |
| 1038 | assert(self->read != NULL); |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1039 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1040 | if (_Unpickler_SkipConsumed(self) < 0) |
| 1041 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1042 | |
| 1043 | if (n == READ_WHOLE_LINE) |
| 1044 | data = PyObject_Call(self->readline, empty_tuple, NULL); |
| 1045 | else { |
| 1046 | PyObject *len = PyLong_FromSsize_t(n); |
| 1047 | if (len == NULL) |
| 1048 | return -1; |
| 1049 | data = _Unpickler_FastCall(self, self->read, len); |
| 1050 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1051 | if (data == NULL) |
| 1052 | return -1; |
| 1053 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1054 | /* Prefetch some data without advancing the file pointer, if possible */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1055 | if (self->peek && !self->framing) { |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1056 | PyObject *len, *prefetched; |
| 1057 | len = PyLong_FromSsize_t(PREFETCH); |
| 1058 | if (len == NULL) { |
| 1059 | Py_DECREF(data); |
| 1060 | return -1; |
| 1061 | } |
| 1062 | prefetched = _Unpickler_FastCall(self, self->peek, len); |
| 1063 | if (prefetched == NULL) { |
| 1064 | if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 1065 | /* peek() is probably not supported by the given file object */ |
| 1066 | PyErr_Clear(); |
| 1067 | Py_CLEAR(self->peek); |
| 1068 | } |
| 1069 | else { |
| 1070 | Py_DECREF(data); |
| 1071 | return -1; |
| 1072 | } |
| 1073 | } |
| 1074 | else { |
| 1075 | assert(PyBytes_Check(prefetched)); |
| 1076 | prefetched_size = PyBytes_GET_SIZE(prefetched); |
| 1077 | PyBytes_ConcatAndDel(&data, prefetched); |
| 1078 | if (data == NULL) |
| 1079 | return -1; |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | read_size = _Unpickler_SetStringInput(self, data) - prefetched_size; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1084 | Py_DECREF(data); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1085 | self->prefetched_idx = read_size; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1086 | return read_size; |
| 1087 | } |
| 1088 | |
| 1089 | /* Read `n` bytes from the unpickler's data source, storing the result in `*s`. |
| 1090 | |
| 1091 | This should be used for all data reads, rather than accessing the unpickler's |
| 1092 | input buffer directly. This method deals correctly with reading from input |
| 1093 | streams, which the input buffer doesn't deal with. |
| 1094 | |
| 1095 | Note that when reading from a file-like object, self->next_read_idx won't |
| 1096 | be updated (it should remain at 0 for the entire unpickling process). You |
| 1097 | should use this function's return value to know how many bytes you can |
| 1098 | consume. |
| 1099 | |
| 1100 | Returns -1 (with an exception set) on failure. On success, return the |
| 1101 | number of chars read. */ |
| 1102 | static Py_ssize_t |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1103 | _Unpickler_ReadUnframed(UnpicklerObject *self, char **s, Py_ssize_t n) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1104 | { |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1105 | Py_ssize_t num_read; |
| 1106 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1107 | if (self->next_read_idx + n <= self->input_len) { |
| 1108 | *s = self->input_buffer + self->next_read_idx; |
| 1109 | self->next_read_idx += n; |
| 1110 | return n; |
| 1111 | } |
| 1112 | if (!self->read) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1113 | PyErr_Format(PyExc_EOFError, "Ran out of input"); |
Amaury Forgeot d'Arc | 3e4e72f | 2008-11-11 20:05:06 +0000 | [diff] [blame] | 1114 | return -1; |
| 1115 | } |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1116 | num_read = _Unpickler_ReadFromFile(self, n); |
| 1117 | if (num_read < 0) |
| 1118 | return -1; |
| 1119 | if (num_read < n) { |
| 1120 | PyErr_Format(PyExc_EOFError, "Ran out of input"); |
| 1121 | return -1; |
| 1122 | } |
| 1123 | *s = self->input_buffer; |
| 1124 | self->next_read_idx = n; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1125 | return n; |
| 1126 | } |
| 1127 | |
| 1128 | static Py_ssize_t |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1129 | _Unpickler_Read(UnpicklerObject *self, char **s, Py_ssize_t n) |
| 1130 | { |
| 1131 | if (self->framing && |
| 1132 | (self->frame_end_idx == -1 || |
| 1133 | self->frame_end_idx <= self->next_read_idx)) { |
| 1134 | /* Need to read new frame */ |
| 1135 | char *dummy; |
| 1136 | unsigned char *frame_start; |
| 1137 | size_t frame_len; |
| 1138 | if (_Unpickler_ReadUnframed(self, &dummy, FRAME_HEADER_SIZE) < 0) |
| 1139 | return -1; |
| 1140 | frame_start = (unsigned char *) dummy; |
| 1141 | if (frame_start[0] != (unsigned char)FRAME) { |
| 1142 | PyErr_Format(UnpicklingError, |
| 1143 | "expected FRAME opcode, got 0x%x instead", |
| 1144 | frame_start[0]); |
| 1145 | return -1; |
| 1146 | } |
| 1147 | frame_len = (size_t) frame_start[1]; |
| 1148 | frame_len |= (size_t) frame_start[2] << 8; |
| 1149 | frame_len |= (size_t) frame_start[3] << 16; |
| 1150 | frame_len |= (size_t) frame_start[4] << 24; |
| 1151 | #if SIZEOF_SIZE_T >= 8 |
| 1152 | frame_len |= (size_t) frame_start[5] << 32; |
| 1153 | frame_len |= (size_t) frame_start[6] << 40; |
| 1154 | frame_len |= (size_t) frame_start[7] << 48; |
| 1155 | frame_len |= (size_t) frame_start[8] << 56; |
| 1156 | #else |
| 1157 | if (frame_start[5] || frame_start[6] || |
| 1158 | frame_start[7] || frame_start[8]) { |
| 1159 | PyErr_Format(PyExc_OverflowError, |
| 1160 | "Frame size too large for 32-bit build"); |
| 1161 | return -1; |
| 1162 | } |
| 1163 | #endif |
| 1164 | if (frame_len > PY_SSIZE_T_MAX) { |
| 1165 | PyErr_Format(UnpicklingError, "Invalid frame length"); |
| 1166 | return -1; |
| 1167 | } |
| 1168 | if (frame_len < n) { |
| 1169 | PyErr_Format(UnpicklingError, "Bad framing"); |
| 1170 | return -1; |
| 1171 | } |
| 1172 | if (_Unpickler_ReadUnframed(self, &dummy /* unused */, |
| 1173 | frame_len) < 0) |
| 1174 | return -1; |
| 1175 | /* Rewind to start of frame */ |
| 1176 | self->frame_end_idx = self->next_read_idx; |
| 1177 | self->next_read_idx -= frame_len; |
| 1178 | } |
| 1179 | if (self->framing) { |
| 1180 | /* Check for bad input */ |
| 1181 | if (n + self->next_read_idx > self->frame_end_idx) { |
| 1182 | PyErr_Format(UnpicklingError, "Bad framing"); |
| 1183 | return -1; |
| 1184 | } |
| 1185 | } |
| 1186 | return _Unpickler_ReadUnframed(self, s, n); |
| 1187 | } |
| 1188 | |
| 1189 | static Py_ssize_t |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1190 | _Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len, |
| 1191 | char **result) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1192 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1193 | char *input_line = PyMem_Realloc(self->input_line, len + 1); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1194 | if (input_line == NULL) { |
| 1195 | PyErr_NoMemory(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1196 | return -1; |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1197 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1198 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1199 | memcpy(input_line, line, len); |
| 1200 | input_line[len] = '\0'; |
| 1201 | self->input_line = input_line; |
| 1202 | *result = self->input_line; |
| 1203 | return len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1206 | /* Read a line from the input stream/buffer. If we run off the end of the input |
| 1207 | before hitting \n, return the data we found. |
| 1208 | |
| 1209 | Returns the number of chars read, or -1 on failure. */ |
| 1210 | static Py_ssize_t |
| 1211 | _Unpickler_Readline(UnpicklerObject *self, char **result) |
| 1212 | { |
| 1213 | Py_ssize_t i, num_read; |
| 1214 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1215 | for (i = self->next_read_idx; i < self->input_len; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1216 | if (self->input_buffer[i] == '\n') { |
| 1217 | char *line_start = self->input_buffer + self->next_read_idx; |
| 1218 | num_read = i - self->next_read_idx + 1; |
| 1219 | self->next_read_idx = i + 1; |
| 1220 | return _Unpickler_CopyLine(self, line_start, num_read, result); |
| 1221 | } |
| 1222 | } |
| 1223 | if (self->read) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1224 | num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE); |
| 1225 | if (num_read < 0) |
| 1226 | return -1; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1227 | self->next_read_idx = num_read; |
Antoine Pitrou | f6c7a85 | 2011-08-11 21:04:02 +0200 | [diff] [blame] | 1228 | return _Unpickler_CopyLine(self, self->input_buffer, num_read, result); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1229 | } |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1230 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1231 | /* If we get here, we've run off the end of the input string. Return the |
| 1232 | remaining string and let the caller figure it out. */ |
| 1233 | *result = self->input_buffer + self->next_read_idx; |
| 1234 | num_read = i - self->next_read_idx; |
| 1235 | self->next_read_idx = i; |
| 1236 | return num_read; |
| 1237 | } |
| 1238 | |
| 1239 | /* Returns -1 (with an exception set) on failure, 0 on success. The memo array |
| 1240 | will be modified in place. */ |
| 1241 | static int |
| 1242 | _Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size) |
| 1243 | { |
| 1244 | Py_ssize_t i; |
| 1245 | PyObject **memo; |
| 1246 | |
| 1247 | assert(new_size > self->memo_size); |
| 1248 | |
| 1249 | memo = PyMem_REALLOC(self->memo, new_size * sizeof(PyObject *)); |
| 1250 | if (memo == NULL) { |
| 1251 | PyErr_NoMemory(); |
| 1252 | return -1; |
| 1253 | } |
| 1254 | self->memo = memo; |
| 1255 | for (i = self->memo_size; i < new_size; i++) |
| 1256 | self->memo[i] = NULL; |
| 1257 | self->memo_size = new_size; |
| 1258 | return 0; |
| 1259 | } |
| 1260 | |
| 1261 | /* Returns NULL if idx is out of bounds. */ |
| 1262 | static PyObject * |
| 1263 | _Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx) |
| 1264 | { |
| 1265 | if (idx < 0 || idx >= self->memo_size) |
| 1266 | return NULL; |
| 1267 | |
| 1268 | return self->memo[idx]; |
| 1269 | } |
| 1270 | |
| 1271 | /* Returns -1 (with an exception set) on failure, 0 on success. |
| 1272 | This takes its own reference to `value`. */ |
| 1273 | static int |
| 1274 | _Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value) |
| 1275 | { |
| 1276 | PyObject *old_item; |
| 1277 | |
| 1278 | if (idx >= self->memo_size) { |
| 1279 | if (_Unpickler_ResizeMemoList(self, idx * 2) < 0) |
| 1280 | return -1; |
| 1281 | assert(idx < self->memo_size); |
| 1282 | } |
| 1283 | Py_INCREF(value); |
| 1284 | old_item = self->memo[idx]; |
| 1285 | self->memo[idx] = value; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1286 | if (old_item != NULL) { |
| 1287 | Py_DECREF(old_item); |
| 1288 | } |
| 1289 | else { |
| 1290 | self->memo_len++; |
| 1291 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1292 | return 0; |
| 1293 | } |
| 1294 | |
| 1295 | static PyObject ** |
| 1296 | _Unpickler_NewMemo(Py_ssize_t new_size) |
| 1297 | { |
| 1298 | PyObject **memo = PyMem_MALLOC(new_size * sizeof(PyObject *)); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1299 | if (memo == NULL) { |
| 1300 | PyErr_NoMemory(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1301 | return NULL; |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1302 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1303 | memset(memo, 0, new_size * sizeof(PyObject *)); |
| 1304 | return memo; |
| 1305 | } |
| 1306 | |
| 1307 | /* Free the unpickler's memo, taking care to decref any items left in it. */ |
| 1308 | static void |
| 1309 | _Unpickler_MemoCleanup(UnpicklerObject *self) |
| 1310 | { |
| 1311 | Py_ssize_t i; |
| 1312 | PyObject **memo = self->memo; |
| 1313 | |
| 1314 | if (self->memo == NULL) |
| 1315 | return; |
| 1316 | self->memo = NULL; |
| 1317 | i = self->memo_size; |
| 1318 | while (--i >= 0) { |
| 1319 | Py_XDECREF(memo[i]); |
| 1320 | } |
| 1321 | PyMem_FREE(memo); |
| 1322 | } |
| 1323 | |
| 1324 | static UnpicklerObject * |
| 1325 | _Unpickler_New(void) |
| 1326 | { |
| 1327 | UnpicklerObject *self; |
| 1328 | |
| 1329 | self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type); |
| 1330 | if (self == NULL) |
| 1331 | return NULL; |
| 1332 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1333 | self->arg = NULL; |
| 1334 | self->pers_func = NULL; |
| 1335 | self->input_buffer = NULL; |
| 1336 | self->input_line = NULL; |
| 1337 | self->input_len = 0; |
| 1338 | self->next_read_idx = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1339 | self->frame_end_idx = -1; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1340 | self->prefetched_idx = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1341 | self->read = NULL; |
| 1342 | self->readline = NULL; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1343 | self->peek = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1344 | self->encoding = NULL; |
| 1345 | self->errors = NULL; |
| 1346 | self->marks = NULL; |
| 1347 | self->num_marks = 0; |
| 1348 | self->marks_size = 0; |
| 1349 | self->proto = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1350 | self->framing = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1351 | self->fix_imports = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1352 | memset(&self->buffer, 0, sizeof(Py_buffer)); |
| 1353 | self->memo_size = 32; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1354 | self->memo_len = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1355 | self->memo = _Unpickler_NewMemo(self->memo_size); |
| 1356 | self->stack = (Pdata *)Pdata_New(); |
| 1357 | |
| 1358 | if (self->memo == NULL || self->stack == NULL) { |
| 1359 | Py_DECREF(self); |
| 1360 | return NULL; |
| 1361 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1362 | |
| 1363 | return self; |
| 1364 | } |
| 1365 | |
| 1366 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1367 | be called once on a freshly created Pickler. */ |
| 1368 | static int |
| 1369 | _Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file) |
| 1370 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1371 | _Py_IDENTIFIER(peek); |
| 1372 | _Py_IDENTIFIER(read); |
| 1373 | _Py_IDENTIFIER(readline); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1374 | |
| 1375 | self->peek = _PyObject_GetAttrId(file, &PyId_peek); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1376 | if (self->peek == NULL) { |
| 1377 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1378 | PyErr_Clear(); |
| 1379 | else |
| 1380 | return -1; |
| 1381 | } |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1382 | self->read = _PyObject_GetAttrId(file, &PyId_read); |
| 1383 | self->readline = _PyObject_GetAttrId(file, &PyId_readline); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1384 | if (self->readline == NULL || self->read == NULL) { |
| 1385 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1386 | PyErr_SetString(PyExc_TypeError, |
| 1387 | "file must have 'read' and 'readline' attributes"); |
| 1388 | Py_CLEAR(self->read); |
| 1389 | Py_CLEAR(self->readline); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1390 | Py_CLEAR(self->peek); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1391 | return -1; |
| 1392 | } |
| 1393 | return 0; |
| 1394 | } |
| 1395 | |
| 1396 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1397 | be called once on a freshly created Pickler. */ |
| 1398 | static int |
| 1399 | _Unpickler_SetInputEncoding(UnpicklerObject *self, |
| 1400 | const char *encoding, |
| 1401 | const char *errors) |
| 1402 | { |
| 1403 | if (encoding == NULL) |
| 1404 | encoding = "ASCII"; |
| 1405 | if (errors == NULL) |
| 1406 | errors = "strict"; |
| 1407 | |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 1408 | self->encoding = _PyMem_Strdup(encoding); |
| 1409 | self->errors = _PyMem_Strdup(errors); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1410 | if (self->encoding == NULL || self->errors == NULL) { |
| 1411 | PyErr_NoMemory(); |
| 1412 | return -1; |
| 1413 | } |
| 1414 | return 0; |
| 1415 | } |
| 1416 | |
| 1417 | /* Generate a GET opcode for an object stored in the memo. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1418 | static int |
| 1419 | memo_get(PicklerObject *self, PyObject *key) |
| 1420 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1421 | Py_ssize_t *value; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1422 | char pdata[30]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1423 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1424 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1425 | value = PyMemoTable_Get(self->memo, key); |
| 1426 | if (value == NULL) { |
| 1427 | PyErr_SetObject(PyExc_KeyError, key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1428 | return -1; |
| 1429 | } |
| 1430 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1431 | if (!self->bin) { |
| 1432 | pdata[0] = GET; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1433 | PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, |
| 1434 | "%" PY_FORMAT_SIZE_T "d\n", *value); |
| 1435 | len = strlen(pdata); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1436 | } |
| 1437 | else { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1438 | if (*value < 256) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1439 | pdata[0] = BINGET; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1440 | pdata[1] = (unsigned char)(*value & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1441 | len = 2; |
| 1442 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1443 | else if (*value <= 0xffffffffL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1444 | pdata[0] = LONG_BINGET; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1445 | pdata[1] = (unsigned char)(*value & 0xff); |
| 1446 | pdata[2] = (unsigned char)((*value >> 8) & 0xff); |
| 1447 | pdata[3] = (unsigned char)((*value >> 16) & 0xff); |
| 1448 | pdata[4] = (unsigned char)((*value >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1449 | len = 5; |
| 1450 | } |
| 1451 | else { /* unlikely */ |
| 1452 | PyErr_SetString(PicklingError, |
| 1453 | "memo id too large for LONG_BINGET"); |
| 1454 | return -1; |
| 1455 | } |
| 1456 | } |
| 1457 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1458 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1459 | return -1; |
| 1460 | |
| 1461 | return 0; |
| 1462 | } |
| 1463 | |
| 1464 | /* Store an object in the memo, assign it a new unique ID based on the number |
| 1465 | of objects currently stored in the memo and generate a PUT opcode. */ |
| 1466 | static int |
| 1467 | memo_put(PicklerObject *self, PyObject *obj) |
| 1468 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1469 | char pdata[30]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1470 | Py_ssize_t len; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1471 | Py_ssize_t idx; |
| 1472 | |
| 1473 | const char memoize_op = MEMOIZE; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1474 | |
| 1475 | if (self->fast) |
| 1476 | return 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1477 | if (_Pickler_OpcodeBoundary(self)) |
| 1478 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1479 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1480 | idx = PyMemoTable_Size(self->memo); |
| 1481 | if (PyMemoTable_Set(self->memo, obj, idx) < 0) |
| 1482 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1483 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1484 | if (self->proto >= 4) { |
| 1485 | if (_Pickler_Write(self, &memoize_op, 1) < 0) |
| 1486 | return -1; |
| 1487 | return 0; |
| 1488 | } |
| 1489 | else if (!self->bin) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1490 | pdata[0] = PUT; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1491 | PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1492 | "%" PY_FORMAT_SIZE_T "d\n", idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1493 | len = strlen(pdata); |
| 1494 | } |
| 1495 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1496 | if (idx < 256) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1497 | pdata[0] = BINPUT; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1498 | pdata[1] = (unsigned char)idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1499 | len = 2; |
| 1500 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1501 | else if (idx <= 0xffffffffL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1502 | pdata[0] = LONG_BINPUT; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1503 | pdata[1] = (unsigned char)(idx & 0xff); |
| 1504 | pdata[2] = (unsigned char)((idx >> 8) & 0xff); |
| 1505 | pdata[3] = (unsigned char)((idx >> 16) & 0xff); |
| 1506 | pdata[4] = (unsigned char)((idx >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1507 | len = 5; |
| 1508 | } |
| 1509 | else { /* unlikely */ |
| 1510 | PyErr_SetString(PicklingError, |
| 1511 | "memo id too large for LONG_BINPUT"); |
| 1512 | return -1; |
| 1513 | } |
| 1514 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1515 | if (_Pickler_Write(self, pdata, len) < 0) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1516 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1517 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1518 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | static PyObject * |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1522 | getattribute(PyObject *obj, PyObject *name, int allow_qualname) { |
| 1523 | PyObject *dotted_path; |
| 1524 | Py_ssize_t i; |
| 1525 | _Py_static_string(PyId_dot, "."); |
| 1526 | _Py_static_string(PyId_locals, "<locals>"); |
| 1527 | |
| 1528 | dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1); |
| 1529 | if (dotted_path == NULL) { |
| 1530 | return NULL; |
| 1531 | } |
| 1532 | assert(Py_SIZE(dotted_path) >= 1); |
| 1533 | if (!allow_qualname && Py_SIZE(dotted_path) > 1) { |
| 1534 | PyErr_Format(PyExc_AttributeError, |
| 1535 | "Can't get qualified attribute %R on %R;" |
| 1536 | "use protocols >= 4 to enable support", |
| 1537 | name, obj); |
| 1538 | Py_DECREF(dotted_path); |
| 1539 | return NULL; |
| 1540 | } |
| 1541 | Py_INCREF(obj); |
| 1542 | for (i = 0; i < Py_SIZE(dotted_path); i++) { |
| 1543 | PyObject *subpath = PyList_GET_ITEM(dotted_path, i); |
| 1544 | PyObject *tmp; |
| 1545 | PyObject *result = PyUnicode_RichCompare( |
| 1546 | subpath, _PyUnicode_FromId(&PyId_locals), Py_EQ); |
| 1547 | int is_equal = (result == Py_True); |
| 1548 | assert(PyBool_Check(result)); |
| 1549 | Py_DECREF(result); |
| 1550 | if (is_equal) { |
| 1551 | PyErr_Format(PyExc_AttributeError, |
| 1552 | "Can't get local attribute %R on %R", name, obj); |
| 1553 | Py_DECREF(dotted_path); |
| 1554 | Py_DECREF(obj); |
| 1555 | return NULL; |
| 1556 | } |
| 1557 | tmp = PyObject_GetAttr(obj, subpath); |
| 1558 | Py_DECREF(obj); |
| 1559 | if (tmp == NULL) { |
| 1560 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 1561 | PyErr_Clear(); |
| 1562 | PyErr_Format(PyExc_AttributeError, |
| 1563 | "Can't get attribute %R on %R", name, obj); |
| 1564 | } |
| 1565 | Py_DECREF(dotted_path); |
| 1566 | return NULL; |
| 1567 | } |
| 1568 | obj = tmp; |
| 1569 | } |
| 1570 | Py_DECREF(dotted_path); |
| 1571 | return obj; |
| 1572 | } |
| 1573 | |
| 1574 | static PyObject * |
| 1575 | whichmodule(PyObject *global, PyObject *global_name, int allow_qualname) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1576 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1577 | PyObject *module_name; |
| 1578 | PyObject *modules_dict; |
| 1579 | PyObject *module; |
| 1580 | PyObject *obj; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1581 | Py_ssize_t i, j; |
| 1582 | _Py_IDENTIFIER(__module__); |
| 1583 | _Py_IDENTIFIER(modules); |
| 1584 | _Py_IDENTIFIER(__main__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1585 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1586 | module_name = _PyObject_GetAttrId(global, &PyId___module__); |
| 1587 | |
| 1588 | if (module_name == NULL) { |
| 1589 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1590 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1591 | PyErr_Clear(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1592 | } |
| 1593 | else { |
| 1594 | /* In some rare cases (e.g., bound methods of extension types), |
| 1595 | __module__ can be None. If it is so, then search sys.modules for |
| 1596 | the module of global. */ |
| 1597 | if (module_name != Py_None) |
| 1598 | return module_name; |
| 1599 | Py_CLEAR(module_name); |
| 1600 | } |
| 1601 | assert(module_name == NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1602 | |
Victor Stinner | bb52020 | 2013-11-06 22:40:41 +0100 | [diff] [blame] | 1603 | modules_dict = _PySys_GetObjectId(&PyId_modules); |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 1604 | if (modules_dict == NULL) { |
| 1605 | PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1606 | return NULL; |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 1607 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1608 | |
| 1609 | i = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1610 | while ((j = PyDict_Next(modules_dict, &i, &module_name, &module))) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1611 | PyObject *result = PyUnicode_RichCompare( |
| 1612 | module_name, _PyUnicode_FromId(&PyId___main__), Py_EQ); |
| 1613 | int is_equal = (result == Py_True); |
| 1614 | assert(PyBool_Check(result)); |
| 1615 | Py_DECREF(result); |
| 1616 | if (is_equal) |
| 1617 | continue; |
| 1618 | if (module == Py_None) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1619 | continue; |
| 1620 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1621 | obj = getattribute(module, global_name, allow_qualname); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1622 | if (obj == NULL) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1623 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1624 | return NULL; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1625 | PyErr_Clear(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1626 | continue; |
| 1627 | } |
| 1628 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1629 | if (obj == global) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1630 | Py_DECREF(obj); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1631 | Py_INCREF(module_name); |
| 1632 | return module_name; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1633 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1634 | Py_DECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
| 1637 | /* If no module is found, use __main__. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1638 | module_name = _PyUnicode_FromId(&PyId___main__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1639 | Py_INCREF(module_name); |
| 1640 | return module_name; |
| 1641 | } |
| 1642 | |
| 1643 | /* fast_save_enter() and fast_save_leave() are guards against recursive |
| 1644 | objects when Pickler is used with the "fast mode" (i.e., with object |
| 1645 | memoization disabled). If the nesting of a list or dict object exceed |
| 1646 | FAST_NESTING_LIMIT, these guards will start keeping an internal |
| 1647 | reference to the seen list or dict objects and check whether these objects |
| 1648 | are recursive. These are not strictly necessary, since save() has a |
| 1649 | hard-coded recursion limit, but they give a nicer error message than the |
| 1650 | typical RuntimeError. */ |
| 1651 | static int |
| 1652 | fast_save_enter(PicklerObject *self, PyObject *obj) |
| 1653 | { |
| 1654 | /* if fast_nesting < 0, we're doing an error exit. */ |
| 1655 | if (++self->fast_nesting >= FAST_NESTING_LIMIT) { |
| 1656 | PyObject *key = NULL; |
| 1657 | if (self->fast_memo == NULL) { |
| 1658 | self->fast_memo = PyDict_New(); |
| 1659 | if (self->fast_memo == NULL) { |
| 1660 | self->fast_nesting = -1; |
| 1661 | return 0; |
| 1662 | } |
| 1663 | } |
| 1664 | key = PyLong_FromVoidPtr(obj); |
| 1665 | if (key == NULL) |
| 1666 | return 0; |
| 1667 | if (PyDict_GetItem(self->fast_memo, key)) { |
| 1668 | Py_DECREF(key); |
| 1669 | PyErr_Format(PyExc_ValueError, |
| 1670 | "fast mode: can't pickle cyclic objects " |
| 1671 | "including object type %.200s at %p", |
| 1672 | obj->ob_type->tp_name, obj); |
| 1673 | self->fast_nesting = -1; |
| 1674 | return 0; |
| 1675 | } |
| 1676 | if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) { |
| 1677 | Py_DECREF(key); |
| 1678 | self->fast_nesting = -1; |
| 1679 | return 0; |
| 1680 | } |
| 1681 | Py_DECREF(key); |
| 1682 | } |
| 1683 | return 1; |
| 1684 | } |
| 1685 | |
| 1686 | static int |
| 1687 | fast_save_leave(PicklerObject *self, PyObject *obj) |
| 1688 | { |
| 1689 | if (self->fast_nesting-- >= FAST_NESTING_LIMIT) { |
| 1690 | PyObject *key = PyLong_FromVoidPtr(obj); |
| 1691 | if (key == NULL) |
| 1692 | return 0; |
| 1693 | if (PyDict_DelItem(self->fast_memo, key) < 0) { |
| 1694 | Py_DECREF(key); |
| 1695 | return 0; |
| 1696 | } |
| 1697 | Py_DECREF(key); |
| 1698 | } |
| 1699 | return 1; |
| 1700 | } |
| 1701 | |
| 1702 | static int |
| 1703 | save_none(PicklerObject *self, PyObject *obj) |
| 1704 | { |
| 1705 | const char none_op = NONE; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1706 | if (_Pickler_Write(self, &none_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1707 | return -1; |
| 1708 | |
| 1709 | return 0; |
| 1710 | } |
| 1711 | |
| 1712 | static int |
| 1713 | save_bool(PicklerObject *self, PyObject *obj) |
| 1714 | { |
| 1715 | static const char *buf[2] = { FALSE, TRUE }; |
| 1716 | const char len[2] = {sizeof(FALSE) - 1, sizeof(TRUE) - 1}; |
| 1717 | int p = (obj == Py_True); |
| 1718 | |
| 1719 | if (self->proto >= 2) { |
| 1720 | const char bool_op = p ? NEWTRUE : NEWFALSE; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1721 | if (_Pickler_Write(self, &bool_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1722 | return -1; |
| 1723 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1724 | else if (_Pickler_Write(self, buf[p], len[p]) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1725 | return -1; |
| 1726 | |
| 1727 | return 0; |
| 1728 | } |
| 1729 | |
| 1730 | static int |
| 1731 | save_int(PicklerObject *self, long x) |
| 1732 | { |
| 1733 | char pdata[32]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1734 | Py_ssize_t len = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1735 | |
| 1736 | if (!self->bin |
| 1737 | #if SIZEOF_LONG > 4 |
| 1738 | || x > 0x7fffffffL || x < -0x80000000L |
| 1739 | #endif |
| 1740 | ) { |
| 1741 | /* Text-mode pickle, or long too big to fit in the 4-byte |
| 1742 | * signed BININT format: store as a string. |
| 1743 | */ |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 1744 | pdata[0] = LONG; /* use LONG for consistency with pickle.py */ |
| 1745 | PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, "%ldL\n", x); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1746 | if (_Pickler_Write(self, pdata, strlen(pdata)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1747 | return -1; |
| 1748 | } |
| 1749 | else { |
| 1750 | /* Binary pickle and x fits in a signed 4-byte int. */ |
| 1751 | pdata[1] = (unsigned char)(x & 0xff); |
| 1752 | pdata[2] = (unsigned char)((x >> 8) & 0xff); |
| 1753 | pdata[3] = (unsigned char)((x >> 16) & 0xff); |
| 1754 | pdata[4] = (unsigned char)((x >> 24) & 0xff); |
| 1755 | |
| 1756 | if ((pdata[4] == 0) && (pdata[3] == 0)) { |
| 1757 | if (pdata[2] == 0) { |
| 1758 | pdata[0] = BININT1; |
| 1759 | len = 2; |
| 1760 | } |
| 1761 | else { |
| 1762 | pdata[0] = BININT2; |
| 1763 | len = 3; |
| 1764 | } |
| 1765 | } |
| 1766 | else { |
| 1767 | pdata[0] = BININT; |
| 1768 | len = 5; |
| 1769 | } |
| 1770 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1771 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1772 | return -1; |
| 1773 | } |
| 1774 | |
| 1775 | return 0; |
| 1776 | } |
| 1777 | |
| 1778 | static int |
| 1779 | save_long(PicklerObject *self, PyObject *obj) |
| 1780 | { |
| 1781 | PyObject *repr = NULL; |
| 1782 | Py_ssize_t size; |
| 1783 | long val = PyLong_AsLong(obj); |
| 1784 | int status = 0; |
| 1785 | |
| 1786 | const char long_op = LONG; |
| 1787 | |
| 1788 | if (val == -1 && PyErr_Occurred()) { |
| 1789 | /* out of range for int pickling */ |
| 1790 | PyErr_Clear(); |
| 1791 | } |
Antoine Pitrou | e58bffb | 2011-08-13 20:40:32 +0200 | [diff] [blame] | 1792 | else |
| 1793 | #if SIZEOF_LONG > 4 |
| 1794 | if (val <= 0x7fffffffL && val >= -0x80000000L) |
| 1795 | #endif |
| 1796 | return save_int(self, val); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1797 | |
| 1798 | if (self->proto >= 2) { |
| 1799 | /* Linear-time pickling. */ |
| 1800 | size_t nbits; |
| 1801 | size_t nbytes; |
| 1802 | unsigned char *pdata; |
| 1803 | char header[5]; |
| 1804 | int i; |
| 1805 | int sign = _PyLong_Sign(obj); |
| 1806 | |
| 1807 | if (sign == 0) { |
| 1808 | header[0] = LONG1; |
| 1809 | header[1] = 0; /* It's 0 -- an empty bytestring. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1810 | if (_Pickler_Write(self, header, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1811 | goto error; |
| 1812 | return 0; |
| 1813 | } |
| 1814 | nbits = _PyLong_NumBits(obj); |
| 1815 | if (nbits == (size_t)-1 && PyErr_Occurred()) |
| 1816 | goto error; |
| 1817 | /* How many bytes do we need? There are nbits >> 3 full |
| 1818 | * bytes of data, and nbits & 7 leftover bits. If there |
| 1819 | * are any leftover bits, then we clearly need another |
| 1820 | * byte. Wnat's not so obvious is that we *probably* |
| 1821 | * need another byte even if there aren't any leftovers: |
| 1822 | * the most-significant bit of the most-significant byte |
| 1823 | * acts like a sign bit, and it's usually got a sense |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1824 | * opposite of the one we need. The exception is ints |
| 1825 | * of the form -(2**(8*j-1)) for j > 0. Such an int is |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1826 | * its own 256's-complement, so has the right sign bit |
| 1827 | * even without the extra byte. That's a pain to check |
| 1828 | * for in advance, though, so we always grab an extra |
| 1829 | * byte at the start, and cut it back later if possible. |
| 1830 | */ |
| 1831 | nbytes = (nbits >> 3) + 1; |
Antoine Pitrou | bf6ecf9 | 2012-11-24 20:40:21 +0100 | [diff] [blame] | 1832 | if (nbytes > 0x7fffffffL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1833 | PyErr_SetString(PyExc_OverflowError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1834 | "int too large to pickle"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1835 | goto error; |
| 1836 | } |
Neal Norwitz | 6ae2eb2 | 2008-08-24 23:50:08 +0000 | [diff] [blame] | 1837 | repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1838 | if (repr == NULL) |
| 1839 | goto error; |
Neal Norwitz | 6ae2eb2 | 2008-08-24 23:50:08 +0000 | [diff] [blame] | 1840 | pdata = (unsigned char *)PyBytes_AS_STRING(repr); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1841 | i = _PyLong_AsByteArray((PyLongObject *)obj, |
| 1842 | pdata, nbytes, |
| 1843 | 1 /* little endian */ , 1 /* signed */ ); |
| 1844 | if (i < 0) |
| 1845 | goto error; |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1846 | /* If the int is negative, this may be a byte more than |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1847 | * needed. This is so iff the MSB is all redundant sign |
| 1848 | * bits. |
| 1849 | */ |
| 1850 | if (sign < 0 && |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1851 | nbytes > 1 && |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1852 | pdata[nbytes - 1] == 0xff && |
| 1853 | (pdata[nbytes - 2] & 0x80) != 0) { |
| 1854 | nbytes--; |
| 1855 | } |
| 1856 | |
| 1857 | if (nbytes < 256) { |
| 1858 | header[0] = LONG1; |
| 1859 | header[1] = (unsigned char)nbytes; |
| 1860 | size = 2; |
| 1861 | } |
| 1862 | else { |
| 1863 | header[0] = LONG4; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1864 | size = (Py_ssize_t) nbytes; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1865 | for (i = 1; i < 5; i++) { |
| 1866 | header[i] = (unsigned char)(size & 0xff); |
| 1867 | size >>= 8; |
| 1868 | } |
| 1869 | size = 5; |
| 1870 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1871 | if (_Pickler_Write(self, header, size) < 0 || |
| 1872 | _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1873 | goto error; |
| 1874 | } |
| 1875 | else { |
| 1876 | char *string; |
| 1877 | |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 1878 | /* proto < 2: write the repr and newline. This is quadratic-time (in |
| 1879 | the number of digits), in both directions. We add a trailing 'L' |
| 1880 | to the repr, for compatibility with Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1881 | |
| 1882 | repr = PyObject_Repr(obj); |
| 1883 | if (repr == NULL) |
| 1884 | goto error; |
| 1885 | |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1886 | string = _PyUnicode_AsStringAndSize(repr, &size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1887 | if (string == NULL) |
| 1888 | goto error; |
| 1889 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1890 | if (_Pickler_Write(self, &long_op, 1) < 0 || |
| 1891 | _Pickler_Write(self, string, size) < 0 || |
| 1892 | _Pickler_Write(self, "L\n", 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1893 | goto error; |
| 1894 | } |
| 1895 | |
| 1896 | if (0) { |
| 1897 | error: |
| 1898 | status = -1; |
| 1899 | } |
| 1900 | Py_XDECREF(repr); |
| 1901 | |
| 1902 | return status; |
| 1903 | } |
| 1904 | |
| 1905 | static int |
| 1906 | save_float(PicklerObject *self, PyObject *obj) |
| 1907 | { |
| 1908 | double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj); |
| 1909 | |
| 1910 | if (self->bin) { |
| 1911 | char pdata[9]; |
| 1912 | pdata[0] = BINFLOAT; |
| 1913 | if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0) |
| 1914 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1915 | if (_Pickler_Write(self, pdata, 9) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1916 | return -1; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1917 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1918 | else { |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1919 | int result = -1; |
| 1920 | char *buf = NULL; |
| 1921 | char op = FLOAT; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1922 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1923 | if (_Pickler_Write(self, &op, 1) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1924 | goto done; |
| 1925 | |
Mark Dickinson | 3e09f43 | 2009-04-17 08:41:23 +0000 | [diff] [blame] | 1926 | buf = PyOS_double_to_string(x, 'g', 17, 0, NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1927 | if (!buf) { |
| 1928 | PyErr_NoMemory(); |
| 1929 | goto done; |
| 1930 | } |
| 1931 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1932 | if (_Pickler_Write(self, buf, strlen(buf)) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1933 | goto done; |
| 1934 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1935 | if (_Pickler_Write(self, "\n", 1) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1936 | goto done; |
| 1937 | |
| 1938 | result = 0; |
| 1939 | done: |
| 1940 | PyMem_Free(buf); |
| 1941 | return result; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1942 | } |
| 1943 | |
| 1944 | return 0; |
| 1945 | } |
| 1946 | |
| 1947 | static int |
| 1948 | save_bytes(PicklerObject *self, PyObject *obj) |
| 1949 | { |
| 1950 | if (self->proto < 3) { |
| 1951 | /* Older pickle protocols do not have an opcode for pickling bytes |
| 1952 | objects. Therefore, we need to fake the copy protocol (i.e., |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 1953 | the __reduce__ method) to permit bytes object unpickling. |
| 1954 | |
| 1955 | Here we use a hack to be compatible with Python 2. Since in Python |
| 1956 | 2 'bytes' is just an alias for 'str' (which has different |
| 1957 | parameters than the actual bytes object), we use codecs.encode |
| 1958 | to create the appropriate 'str' object when unpickled using |
| 1959 | Python 2 *and* the appropriate 'bytes' object when unpickled |
| 1960 | using Python 3. Again this is a hack and we don't need to do this |
| 1961 | with newer protocols. */ |
| 1962 | static PyObject *codecs_encode = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1963 | PyObject *reduce_value = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1964 | int status; |
| 1965 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 1966 | if (codecs_encode == NULL) { |
| 1967 | PyObject *codecs_module = PyImport_ImportModule("codecs"); |
| 1968 | if (codecs_module == NULL) { |
| 1969 | return -1; |
| 1970 | } |
| 1971 | codecs_encode = PyObject_GetAttrString(codecs_module, "encode"); |
| 1972 | Py_DECREF(codecs_module); |
| 1973 | if (codecs_encode == NULL) { |
| 1974 | return -1; |
| 1975 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1976 | } |
| 1977 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 1978 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 1979 | reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type); |
| 1980 | } |
| 1981 | else { |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 1982 | PyObject *unicode_str = |
| 1983 | PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj), |
| 1984 | PyBytes_GET_SIZE(obj), |
| 1985 | "strict"); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1986 | _Py_IDENTIFIER(latin1); |
| 1987 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 1988 | if (unicode_str == NULL) |
| 1989 | return -1; |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 1990 | reduce_value = Py_BuildValue("(O(OO))", |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1991 | codecs_encode, unicode_str, |
| 1992 | _PyUnicode_FromId(&PyId_latin1)); |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 1993 | Py_DECREF(unicode_str); |
| 1994 | } |
| 1995 | |
| 1996 | if (reduce_value == NULL) |
| 1997 | return -1; |
| 1998 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1999 | /* save_reduce() will memoize the object automatically. */ |
| 2000 | status = save_reduce(self, reduce_value, obj); |
| 2001 | Py_DECREF(reduce_value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2002 | return status; |
| 2003 | } |
| 2004 | else { |
| 2005 | Py_ssize_t size; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2006 | char header[9]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2007 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2008 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2009 | size = PyBytes_GET_SIZE(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2010 | if (size < 0) |
| 2011 | return -1; |
| 2012 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2013 | if (size <= 0xff) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2014 | header[0] = SHORT_BINBYTES; |
| 2015 | header[1] = (unsigned char)size; |
| 2016 | len = 2; |
| 2017 | } |
| 2018 | else if (size <= 0xffffffffL) { |
| 2019 | header[0] = BINBYTES; |
| 2020 | header[1] = (unsigned char)(size & 0xff); |
| 2021 | header[2] = (unsigned char)((size >> 8) & 0xff); |
| 2022 | header[3] = (unsigned char)((size >> 16) & 0xff); |
| 2023 | header[4] = (unsigned char)((size >> 24) & 0xff); |
| 2024 | len = 5; |
| 2025 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2026 | else if (self->proto >= 4) { |
| 2027 | int i; |
| 2028 | header[0] = BINBYTES8; |
| 2029 | for (i = 0; i < 8; i++) { |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 2030 | _write_size64(header + 1, size); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2031 | } |
| 2032 | len = 8; |
| 2033 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2034 | else { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2035 | PyErr_SetString(PyExc_OverflowError, |
Serhiy Storchaka | f8def28 | 2013-02-16 17:29:56 +0200 | [diff] [blame] | 2036 | "cannot serialize a bytes object larger than 4 GiB"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2037 | return -1; /* string too large */ |
| 2038 | } |
| 2039 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2040 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2041 | return -1; |
| 2042 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2043 | if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2044 | return -1; |
| 2045 | |
| 2046 | if (memo_put(self, obj) < 0) |
| 2047 | return -1; |
| 2048 | |
| 2049 | return 0; |
| 2050 | } |
| 2051 | } |
| 2052 | |
| 2053 | /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates |
| 2054 | backslash and newline characters to \uXXXX escapes. */ |
| 2055 | static PyObject * |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2056 | raw_unicode_escape(PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2057 | { |
| 2058 | PyObject *repr, *result; |
| 2059 | char *p; |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2060 | Py_ssize_t i, size, expandsize; |
| 2061 | void *data; |
| 2062 | unsigned int kind; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2063 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2064 | if (PyUnicode_READY(obj)) |
| 2065 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2066 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2067 | size = PyUnicode_GET_LENGTH(obj); |
| 2068 | data = PyUnicode_DATA(obj); |
| 2069 | kind = PyUnicode_KIND(obj); |
| 2070 | if (kind == PyUnicode_4BYTE_KIND) |
| 2071 | expandsize = 10; |
| 2072 | else |
| 2073 | expandsize = 6; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 2074 | |
Alexandre Vassalotti | 554d878 | 2008-12-27 07:32:41 +0000 | [diff] [blame] | 2075 | if (size > PY_SSIZE_T_MAX / expandsize) |
| 2076 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 554d878 | 2008-12-27 07:32:41 +0000 | [diff] [blame] | 2077 | repr = PyByteArray_FromStringAndSize(NULL, expandsize * size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2078 | if (repr == NULL) |
| 2079 | return NULL; |
| 2080 | if (size == 0) |
| 2081 | goto done; |
| 2082 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2083 | p = PyByteArray_AS_STRING(repr); |
| 2084 | for (i=0; i < size; i++) { |
| 2085 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2086 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 2087 | if (ch >= 0x10000) { |
| 2088 | *p++ = '\\'; |
| 2089 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 2090 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 2091 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 2092 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 2093 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 2094 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 2095 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 2096 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 2097 | *p++ = Py_hexdigits[ch & 15]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2098 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2099 | /* Map 16-bit characters to '\uxxxx' */ |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2100 | else if (ch >= 256 || ch == '\\' || ch == '\n') { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2101 | *p++ = '\\'; |
| 2102 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 2103 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 2104 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 2105 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 2106 | *p++ = Py_hexdigits[ch & 15]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2107 | } |
Alexandre Vassalotti | 554d878 | 2008-12-27 07:32:41 +0000 | [diff] [blame] | 2108 | /* Copy everything else as-is */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2109 | else |
| 2110 | *p++ = (char) ch; |
| 2111 | } |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2112 | size = p - PyByteArray_AS_STRING(repr); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2113 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2114 | done: |
Alexandre Vassalotti | 554d878 | 2008-12-27 07:32:41 +0000 | [diff] [blame] | 2115 | result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(repr), size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2116 | Py_DECREF(repr); |
| 2117 | return result; |
| 2118 | } |
| 2119 | |
| 2120 | static int |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2121 | write_utf8(PicklerObject *self, char *data, Py_ssize_t size) |
| 2122 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2123 | char header[9]; |
| 2124 | Py_ssize_t len; |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2125 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2126 | if (size <= 0xff && self->proto >= 4) { |
| 2127 | header[0] = SHORT_BINUNICODE; |
| 2128 | header[1] = (unsigned char)(size & 0xff); |
| 2129 | len = 2; |
| 2130 | } |
| 2131 | else if (size <= 0xffffffffUL) { |
| 2132 | header[0] = BINUNICODE; |
| 2133 | header[1] = (unsigned char)(size & 0xff); |
| 2134 | header[2] = (unsigned char)((size >> 8) & 0xff); |
| 2135 | header[3] = (unsigned char)((size >> 16) & 0xff); |
| 2136 | header[4] = (unsigned char)((size >> 24) & 0xff); |
| 2137 | len = 5; |
| 2138 | } |
| 2139 | else if (self->proto >= 4) { |
| 2140 | int i; |
| 2141 | |
| 2142 | header[0] = BINUNICODE8; |
| 2143 | for (i = 0; i < 8; i++) { |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 2144 | _write_size64(header + 1, size); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2145 | } |
| 2146 | len = 9; |
| 2147 | } |
| 2148 | else { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2149 | PyErr_SetString(PyExc_OverflowError, |
Antoine Pitrou | 4b7b0f0 | 2013-04-07 23:46:52 +0200 | [diff] [blame] | 2150 | "cannot serialize a string larger than 4GiB"); |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2151 | return -1; |
| 2152 | } |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2153 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2154 | if (_Pickler_Write(self, header, len) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2155 | return -1; |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2156 | if (_Pickler_Write(self, data, size) < 0) |
| 2157 | return -1; |
| 2158 | |
| 2159 | return 0; |
| 2160 | } |
| 2161 | |
| 2162 | static int |
| 2163 | write_unicode_binary(PicklerObject *self, PyObject *obj) |
| 2164 | { |
| 2165 | PyObject *encoded = NULL; |
| 2166 | Py_ssize_t size; |
| 2167 | char *data; |
| 2168 | int r; |
| 2169 | |
| 2170 | if (PyUnicode_READY(obj)) |
| 2171 | return -1; |
| 2172 | |
| 2173 | data = PyUnicode_AsUTF8AndSize(obj, &size); |
| 2174 | if (data != NULL) |
| 2175 | return write_utf8(self, data, size); |
| 2176 | |
| 2177 | /* Issue #8383: for strings with lone surrogates, fallback on the |
| 2178 | "surrogatepass" error handler. */ |
| 2179 | PyErr_Clear(); |
| 2180 | encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass"); |
| 2181 | if (encoded == NULL) |
| 2182 | return -1; |
| 2183 | |
| 2184 | r = write_utf8(self, PyBytes_AS_STRING(encoded), |
| 2185 | PyBytes_GET_SIZE(encoded)); |
| 2186 | Py_DECREF(encoded); |
| 2187 | return r; |
| 2188 | } |
| 2189 | |
| 2190 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2191 | save_unicode(PicklerObject *self, PyObject *obj) |
| 2192 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2193 | if (self->bin) { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2194 | if (write_unicode_binary(self, obj) < 0) |
| 2195 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2196 | } |
| 2197 | else { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2198 | PyObject *encoded; |
| 2199 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2200 | const char unicode_op = UNICODE; |
| 2201 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2202 | encoded = raw_unicode_escape(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2203 | if (encoded == NULL) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2204 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2205 | |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2206 | if (_Pickler_Write(self, &unicode_op, 1) < 0) { |
| 2207 | Py_DECREF(encoded); |
| 2208 | return -1; |
| 2209 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2210 | |
| 2211 | size = PyBytes_GET_SIZE(encoded); |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2212 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) { |
| 2213 | Py_DECREF(encoded); |
| 2214 | return -1; |
| 2215 | } |
| 2216 | Py_DECREF(encoded); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2217 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2218 | if (_Pickler_Write(self, "\n", 1) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2219 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2220 | } |
| 2221 | if (memo_put(self, obj) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2222 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2223 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2224 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2225 | } |
| 2226 | |
| 2227 | /* A helper for save_tuple. Push the len elements in tuple t on the stack. */ |
| 2228 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2229 | store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2230 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2231 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2232 | |
| 2233 | assert(PyTuple_Size(t) == len); |
| 2234 | |
| 2235 | for (i = 0; i < len; i++) { |
| 2236 | PyObject *element = PyTuple_GET_ITEM(t, i); |
| 2237 | |
| 2238 | if (element == NULL) |
| 2239 | return -1; |
| 2240 | if (save(self, element, 0) < 0) |
| 2241 | return -1; |
| 2242 | } |
| 2243 | |
| 2244 | return 0; |
| 2245 | } |
| 2246 | |
| 2247 | /* Tuples are ubiquitous in the pickle protocols, so many techniques are |
| 2248 | * used across protocols to minimize the space needed to pickle them. |
| 2249 | * Tuples are also the only builtin immutable type that can be recursive |
| 2250 | * (a tuple can be reached from itself), and that requires some subtle |
| 2251 | * magic so that it works in all cases. IOW, this is a long routine. |
| 2252 | */ |
| 2253 | static int |
| 2254 | save_tuple(PicklerObject *self, PyObject *obj) |
| 2255 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2256 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2257 | |
| 2258 | const char mark_op = MARK; |
| 2259 | const char tuple_op = TUPLE; |
| 2260 | const char pop_op = POP; |
| 2261 | const char pop_mark_op = POP_MARK; |
| 2262 | const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3}; |
| 2263 | |
| 2264 | if ((len = PyTuple_Size(obj)) < 0) |
| 2265 | return -1; |
| 2266 | |
| 2267 | if (len == 0) { |
| 2268 | char pdata[2]; |
| 2269 | |
| 2270 | if (self->proto) { |
| 2271 | pdata[0] = EMPTY_TUPLE; |
| 2272 | len = 1; |
| 2273 | } |
| 2274 | else { |
| 2275 | pdata[0] = MARK; |
| 2276 | pdata[1] = TUPLE; |
| 2277 | len = 2; |
| 2278 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2279 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2280 | return -1; |
| 2281 | return 0; |
| 2282 | } |
| 2283 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2284 | /* 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] | 2285 | * saving the tuple elements, the tuple must be recursive, in |
| 2286 | * which case we'll pop everything we put on the stack, and fetch |
| 2287 | * its value from the memo. |
| 2288 | */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2289 | if (len <= 3 && self->proto >= 2) { |
| 2290 | /* Use TUPLE{1,2,3} opcodes. */ |
| 2291 | if (store_tuple_elements(self, obj, len) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2292 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2293 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2294 | if (PyMemoTable_Get(self->memo, obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2295 | /* pop the len elements */ |
| 2296 | for (i = 0; i < len; i++) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2297 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 2298 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2299 | /* fetch from memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2300 | if (memo_get(self, obj) < 0) |
| 2301 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2302 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2303 | return 0; |
| 2304 | } |
| 2305 | else { /* Not recursive. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2306 | if (_Pickler_Write(self, len2opcode + len, 1) < 0) |
| 2307 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2308 | } |
| 2309 | goto memoize; |
| 2310 | } |
| 2311 | |
| 2312 | /* proto < 2 and len > 0, or proto >= 2 and len > 3. |
| 2313 | * Generate MARK e1 e2 ... TUPLE |
| 2314 | */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2315 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2316 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2317 | |
| 2318 | if (store_tuple_elements(self, obj, len) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2319 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2320 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2321 | if (PyMemoTable_Get(self->memo, obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2322 | /* pop the stack stuff we pushed */ |
| 2323 | if (self->bin) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2324 | if (_Pickler_Write(self, &pop_mark_op, 1) < 0) |
| 2325 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2326 | } |
| 2327 | else { |
| 2328 | /* Note that we pop one more than len, to remove |
| 2329 | * the MARK too. |
| 2330 | */ |
| 2331 | for (i = 0; i <= len; i++) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2332 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 2333 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2334 | } |
| 2335 | /* fetch from memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2336 | if (memo_get(self, obj) < 0) |
| 2337 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2338 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2339 | return 0; |
| 2340 | } |
| 2341 | else { /* Not recursive. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2342 | if (_Pickler_Write(self, &tuple_op, 1) < 0) |
| 2343 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2344 | } |
| 2345 | |
| 2346 | memoize: |
| 2347 | if (memo_put(self, obj) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2348 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2349 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2350 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2351 | } |
| 2352 | |
| 2353 | /* iter is an iterator giving items, and we batch up chunks of |
| 2354 | * MARK item item ... item APPENDS |
| 2355 | * opcode sequences. Calling code should have arranged to first create an |
| 2356 | * empty list, or list-like object, for the APPENDS to operate on. |
| 2357 | * Returns 0 on success, <0 on error. |
| 2358 | */ |
| 2359 | static int |
| 2360 | batch_list(PicklerObject *self, PyObject *iter) |
| 2361 | { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2362 | PyObject *obj = NULL; |
| 2363 | PyObject *firstitem = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2364 | int i, n; |
| 2365 | |
| 2366 | const char mark_op = MARK; |
| 2367 | const char append_op = APPEND; |
| 2368 | const char appends_op = APPENDS; |
| 2369 | |
| 2370 | assert(iter != NULL); |
| 2371 | |
| 2372 | /* XXX: I think this function could be made faster by avoiding the |
| 2373 | iterator interface and fetching objects directly from list using |
| 2374 | PyList_GET_ITEM. |
| 2375 | */ |
| 2376 | |
| 2377 | if (self->proto == 0) { |
| 2378 | /* APPENDS isn't available; do one at a time. */ |
| 2379 | for (;;) { |
| 2380 | obj = PyIter_Next(iter); |
| 2381 | if (obj == NULL) { |
| 2382 | if (PyErr_Occurred()) |
| 2383 | return -1; |
| 2384 | break; |
| 2385 | } |
| 2386 | i = save(self, obj, 0); |
| 2387 | Py_DECREF(obj); |
| 2388 | if (i < 0) |
| 2389 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2390 | if (_Pickler_Write(self, &append_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2391 | return -1; |
| 2392 | } |
| 2393 | return 0; |
| 2394 | } |
| 2395 | |
| 2396 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 2397 | do { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2398 | /* Get first item */ |
| 2399 | firstitem = PyIter_Next(iter); |
| 2400 | if (firstitem == NULL) { |
| 2401 | if (PyErr_Occurred()) |
| 2402 | goto error; |
| 2403 | |
| 2404 | /* nothing more to add */ |
| 2405 | break; |
| 2406 | } |
| 2407 | |
| 2408 | /* Try to get a second item */ |
| 2409 | obj = PyIter_Next(iter); |
| 2410 | if (obj == NULL) { |
| 2411 | if (PyErr_Occurred()) |
| 2412 | goto error; |
| 2413 | |
| 2414 | /* Only one item to write */ |
| 2415 | if (save(self, firstitem, 0) < 0) |
| 2416 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2417 | if (_Pickler_Write(self, &append_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2418 | goto error; |
| 2419 | Py_CLEAR(firstitem); |
| 2420 | break; |
| 2421 | } |
| 2422 | |
| 2423 | /* More than one item to write */ |
| 2424 | |
| 2425 | /* Pump out MARK, items, APPENDS. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2426 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2427 | goto error; |
| 2428 | |
| 2429 | if (save(self, firstitem, 0) < 0) |
| 2430 | goto error; |
| 2431 | Py_CLEAR(firstitem); |
| 2432 | n = 1; |
| 2433 | |
| 2434 | /* Fetch and save up to BATCHSIZE items */ |
| 2435 | while (obj) { |
| 2436 | if (save(self, obj, 0) < 0) |
| 2437 | goto error; |
| 2438 | Py_CLEAR(obj); |
| 2439 | n += 1; |
| 2440 | |
| 2441 | if (n == BATCHSIZE) |
| 2442 | break; |
| 2443 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2444 | obj = PyIter_Next(iter); |
| 2445 | if (obj == NULL) { |
| 2446 | if (PyErr_Occurred()) |
| 2447 | goto error; |
| 2448 | break; |
| 2449 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2450 | } |
| 2451 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2452 | if (_Pickler_Write(self, &appends_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2453 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2454 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2455 | } while (n == BATCHSIZE); |
| 2456 | return 0; |
| 2457 | |
| 2458 | error: |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2459 | Py_XDECREF(firstitem); |
| 2460 | Py_XDECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2461 | return -1; |
| 2462 | } |
| 2463 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2464 | /* This is a variant of batch_list() above, specialized for lists (with no |
| 2465 | * support for list subclasses). Like batch_list(), we batch up chunks of |
| 2466 | * MARK item item ... item APPENDS |
| 2467 | * opcode sequences. Calling code should have arranged to first create an |
| 2468 | * empty list, or list-like object, for the APPENDS to operate on. |
| 2469 | * Returns 0 on success, -1 on error. |
| 2470 | * |
| 2471 | * This version is considerably faster than batch_list(), if less general. |
| 2472 | * |
| 2473 | * Note that this only works for protocols > 0. |
| 2474 | */ |
| 2475 | static int |
| 2476 | batch_list_exact(PicklerObject *self, PyObject *obj) |
| 2477 | { |
| 2478 | PyObject *item = NULL; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2479 | Py_ssize_t this_batch, total; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2480 | |
| 2481 | const char append_op = APPEND; |
| 2482 | const char appends_op = APPENDS; |
| 2483 | const char mark_op = MARK; |
| 2484 | |
| 2485 | assert(obj != NULL); |
| 2486 | assert(self->proto > 0); |
| 2487 | assert(PyList_CheckExact(obj)); |
| 2488 | |
| 2489 | if (PyList_GET_SIZE(obj) == 1) { |
| 2490 | item = PyList_GET_ITEM(obj, 0); |
| 2491 | if (save(self, item, 0) < 0) |
| 2492 | return -1; |
| 2493 | if (_Pickler_Write(self, &append_op, 1) < 0) |
| 2494 | return -1; |
| 2495 | return 0; |
| 2496 | } |
| 2497 | |
| 2498 | /* Write in batches of BATCHSIZE. */ |
| 2499 | total = 0; |
| 2500 | do { |
| 2501 | this_batch = 0; |
| 2502 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2503 | return -1; |
| 2504 | while (total < PyList_GET_SIZE(obj)) { |
| 2505 | item = PyList_GET_ITEM(obj, total); |
| 2506 | if (save(self, item, 0) < 0) |
| 2507 | return -1; |
| 2508 | total++; |
| 2509 | if (++this_batch == BATCHSIZE) |
| 2510 | break; |
| 2511 | } |
| 2512 | if (_Pickler_Write(self, &appends_op, 1) < 0) |
| 2513 | return -1; |
| 2514 | |
| 2515 | } while (total < PyList_GET_SIZE(obj)); |
| 2516 | |
| 2517 | return 0; |
| 2518 | } |
| 2519 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2520 | static int |
| 2521 | save_list(PicklerObject *self, PyObject *obj) |
| 2522 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2523 | char header[3]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2524 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2525 | int status = 0; |
| 2526 | |
| 2527 | if (self->fast && !fast_save_enter(self, obj)) |
| 2528 | goto error; |
| 2529 | |
| 2530 | /* Create an empty list. */ |
| 2531 | if (self->bin) { |
| 2532 | header[0] = EMPTY_LIST; |
| 2533 | len = 1; |
| 2534 | } |
| 2535 | else { |
| 2536 | header[0] = MARK; |
| 2537 | header[1] = LIST; |
| 2538 | len = 2; |
| 2539 | } |
| 2540 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2541 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2542 | goto error; |
| 2543 | |
| 2544 | /* Get list length, and bow out early if empty. */ |
| 2545 | if ((len = PyList_Size(obj)) < 0) |
| 2546 | goto error; |
| 2547 | |
| 2548 | if (memo_put(self, obj) < 0) |
| 2549 | goto error; |
| 2550 | |
| 2551 | if (len != 0) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2552 | /* Materialize the list elements. */ |
| 2553 | if (PyList_CheckExact(obj) && self->proto > 0) { |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2554 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 2555 | goto error; |
| 2556 | status = batch_list_exact(self, obj); |
| 2557 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2558 | } else { |
| 2559 | PyObject *iter = PyObject_GetIter(obj); |
| 2560 | if (iter == NULL) |
| 2561 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2562 | |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2563 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 2564 | Py_DECREF(iter); |
| 2565 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2566 | } |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2567 | status = batch_list(self, iter); |
| 2568 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2569 | Py_DECREF(iter); |
| 2570 | } |
| 2571 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2572 | if (0) { |
| 2573 | error: |
| 2574 | status = -1; |
| 2575 | } |
| 2576 | |
| 2577 | if (self->fast && !fast_save_leave(self, obj)) |
| 2578 | status = -1; |
| 2579 | |
| 2580 | return status; |
| 2581 | } |
| 2582 | |
| 2583 | /* iter is an iterator giving (key, value) pairs, and we batch up chunks of |
| 2584 | * MARK key value ... key value SETITEMS |
| 2585 | * opcode sequences. Calling code should have arranged to first create an |
| 2586 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 2587 | * Returns 0 on success, <0 on error. |
| 2588 | * |
| 2589 | * This is very much like batch_list(). The difference between saving |
| 2590 | * elements directly, and picking apart two-tuples, is so long-winded at |
| 2591 | * the C level, though, that attempts to combine these routines were too |
| 2592 | * ugly to bear. |
| 2593 | */ |
| 2594 | static int |
| 2595 | batch_dict(PicklerObject *self, PyObject *iter) |
| 2596 | { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2597 | PyObject *obj = NULL; |
| 2598 | PyObject *firstitem = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2599 | int i, n; |
| 2600 | |
| 2601 | const char mark_op = MARK; |
| 2602 | const char setitem_op = SETITEM; |
| 2603 | const char setitems_op = SETITEMS; |
| 2604 | |
| 2605 | assert(iter != NULL); |
| 2606 | |
| 2607 | if (self->proto == 0) { |
| 2608 | /* SETITEMS isn't available; do one at a time. */ |
| 2609 | for (;;) { |
| 2610 | obj = PyIter_Next(iter); |
| 2611 | if (obj == NULL) { |
| 2612 | if (PyErr_Occurred()) |
| 2613 | return -1; |
| 2614 | break; |
| 2615 | } |
| 2616 | if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) { |
| 2617 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2618 | "iterator must return 2-tuples"); |
| 2619 | return -1; |
| 2620 | } |
| 2621 | i = save(self, PyTuple_GET_ITEM(obj, 0), 0); |
| 2622 | if (i >= 0) |
| 2623 | i = save(self, PyTuple_GET_ITEM(obj, 1), 0); |
| 2624 | Py_DECREF(obj); |
| 2625 | if (i < 0) |
| 2626 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2627 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2628 | return -1; |
| 2629 | } |
| 2630 | return 0; |
| 2631 | } |
| 2632 | |
| 2633 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 2634 | do { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2635 | /* Get first item */ |
| 2636 | firstitem = PyIter_Next(iter); |
| 2637 | if (firstitem == NULL) { |
| 2638 | if (PyErr_Occurred()) |
| 2639 | goto error; |
| 2640 | |
| 2641 | /* nothing more to add */ |
| 2642 | break; |
| 2643 | } |
| 2644 | if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) { |
| 2645 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2646 | "iterator must return 2-tuples"); |
| 2647 | goto error; |
| 2648 | } |
| 2649 | |
| 2650 | /* Try to get a second item */ |
| 2651 | obj = PyIter_Next(iter); |
| 2652 | if (obj == NULL) { |
| 2653 | if (PyErr_Occurred()) |
| 2654 | goto error; |
| 2655 | |
| 2656 | /* Only one item to write */ |
| 2657 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 2658 | goto error; |
| 2659 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 2660 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2661 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2662 | goto error; |
| 2663 | Py_CLEAR(firstitem); |
| 2664 | break; |
| 2665 | } |
| 2666 | |
| 2667 | /* More than one item to write */ |
| 2668 | |
| 2669 | /* Pump out MARK, items, SETITEMS. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2670 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2671 | goto error; |
| 2672 | |
| 2673 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 2674 | goto error; |
| 2675 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 2676 | goto error; |
| 2677 | Py_CLEAR(firstitem); |
| 2678 | n = 1; |
| 2679 | |
| 2680 | /* Fetch and save up to BATCHSIZE items */ |
| 2681 | while (obj) { |
| 2682 | if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) { |
| 2683 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2684 | "iterator must return 2-tuples"); |
| 2685 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2686 | } |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2687 | if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 || |
| 2688 | save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0) |
| 2689 | goto error; |
| 2690 | Py_CLEAR(obj); |
| 2691 | n += 1; |
| 2692 | |
| 2693 | if (n == BATCHSIZE) |
| 2694 | break; |
| 2695 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2696 | obj = PyIter_Next(iter); |
| 2697 | if (obj == NULL) { |
| 2698 | if (PyErr_Occurred()) |
| 2699 | goto error; |
| 2700 | break; |
| 2701 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2702 | } |
| 2703 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2704 | if (_Pickler_Write(self, &setitems_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2705 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2706 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2707 | } while (n == BATCHSIZE); |
| 2708 | return 0; |
| 2709 | |
| 2710 | error: |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2711 | Py_XDECREF(firstitem); |
| 2712 | Py_XDECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2713 | return -1; |
| 2714 | } |
| 2715 | |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2716 | /* This is a variant of batch_dict() above that specializes for dicts, with no |
| 2717 | * support for dict subclasses. Like batch_dict(), we batch up chunks of |
| 2718 | * MARK key value ... key value SETITEMS |
| 2719 | * opcode sequences. Calling code should have arranged to first create an |
| 2720 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 2721 | * Returns 0 on success, -1 on error. |
| 2722 | * |
| 2723 | * Note that this currently doesn't work for protocol 0. |
| 2724 | */ |
| 2725 | static int |
| 2726 | batch_dict_exact(PicklerObject *self, PyObject *obj) |
| 2727 | { |
| 2728 | PyObject *key = NULL, *value = NULL; |
| 2729 | int i; |
| 2730 | Py_ssize_t dict_size, ppos = 0; |
| 2731 | |
Alexandre Vassalotti | f70b129 | 2009-05-25 18:00:52 +0000 | [diff] [blame] | 2732 | const char mark_op = MARK; |
| 2733 | const char setitem_op = SETITEM; |
| 2734 | const char setitems_op = SETITEMS; |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2735 | |
| 2736 | assert(obj != NULL); |
| 2737 | assert(self->proto > 0); |
| 2738 | |
| 2739 | dict_size = PyDict_Size(obj); |
| 2740 | |
| 2741 | /* Special-case len(d) == 1 to save space. */ |
| 2742 | if (dict_size == 1) { |
| 2743 | PyDict_Next(obj, &ppos, &key, &value); |
| 2744 | if (save(self, key, 0) < 0) |
| 2745 | return -1; |
| 2746 | if (save(self, value, 0) < 0) |
| 2747 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2748 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2749 | return -1; |
| 2750 | return 0; |
| 2751 | } |
| 2752 | |
| 2753 | /* Write in batches of BATCHSIZE. */ |
| 2754 | do { |
| 2755 | i = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2756 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2757 | return -1; |
| 2758 | while (PyDict_Next(obj, &ppos, &key, &value)) { |
| 2759 | if (save(self, key, 0) < 0) |
| 2760 | return -1; |
| 2761 | if (save(self, value, 0) < 0) |
| 2762 | return -1; |
| 2763 | if (++i == BATCHSIZE) |
| 2764 | break; |
| 2765 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2766 | if (_Pickler_Write(self, &setitems_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2767 | return -1; |
| 2768 | if (PyDict_Size(obj) != dict_size) { |
| 2769 | PyErr_Format( |
| 2770 | PyExc_RuntimeError, |
| 2771 | "dictionary changed size during iteration"); |
| 2772 | return -1; |
| 2773 | } |
| 2774 | |
| 2775 | } while (i == BATCHSIZE); |
| 2776 | return 0; |
| 2777 | } |
| 2778 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2779 | static int |
| 2780 | save_dict(PicklerObject *self, PyObject *obj) |
| 2781 | { |
| 2782 | PyObject *items, *iter; |
| 2783 | char header[3]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2784 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2785 | int status = 0; |
| 2786 | |
| 2787 | if (self->fast && !fast_save_enter(self, obj)) |
| 2788 | goto error; |
| 2789 | |
| 2790 | /* Create an empty dict. */ |
| 2791 | if (self->bin) { |
| 2792 | header[0] = EMPTY_DICT; |
| 2793 | len = 1; |
| 2794 | } |
| 2795 | else { |
| 2796 | header[0] = MARK; |
| 2797 | header[1] = DICT; |
| 2798 | len = 2; |
| 2799 | } |
| 2800 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2801 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2802 | goto error; |
| 2803 | |
| 2804 | /* Get dict size, and bow out early if empty. */ |
| 2805 | if ((len = PyDict_Size(obj)) < 0) |
| 2806 | goto error; |
| 2807 | |
| 2808 | if (memo_put(self, obj) < 0) |
| 2809 | goto error; |
| 2810 | |
| 2811 | if (len != 0) { |
| 2812 | /* Save the dict items. */ |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2813 | if (PyDict_CheckExact(obj) && self->proto > 0) { |
| 2814 | /* We can take certain shortcuts if we know this is a dict and |
| 2815 | not a dict subclass. */ |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2816 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 2817 | goto error; |
| 2818 | status = batch_dict_exact(self, obj); |
| 2819 | Py_LeaveRecursiveCall(); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2820 | } else { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2821 | _Py_IDENTIFIER(items); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2822 | |
| 2823 | items = _PyObject_CallMethodId(obj, &PyId_items, "()"); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2824 | if (items == NULL) |
| 2825 | goto error; |
| 2826 | iter = PyObject_GetIter(items); |
| 2827 | Py_DECREF(items); |
| 2828 | if (iter == NULL) |
| 2829 | goto error; |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2830 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 2831 | Py_DECREF(iter); |
| 2832 | goto error; |
| 2833 | } |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2834 | status = batch_dict(self, iter); |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2835 | Py_LeaveRecursiveCall(); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2836 | Py_DECREF(iter); |
| 2837 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2838 | } |
| 2839 | |
| 2840 | if (0) { |
| 2841 | error: |
| 2842 | status = -1; |
| 2843 | } |
| 2844 | |
| 2845 | if (self->fast && !fast_save_leave(self, obj)) |
| 2846 | status = -1; |
| 2847 | |
| 2848 | return status; |
| 2849 | } |
| 2850 | |
| 2851 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2852 | save_set(PicklerObject *self, PyObject *obj) |
| 2853 | { |
| 2854 | PyObject *item; |
| 2855 | int i; |
| 2856 | Py_ssize_t set_size, ppos = 0; |
| 2857 | Py_hash_t hash; |
| 2858 | |
| 2859 | const char empty_set_op = EMPTY_SET; |
| 2860 | const char mark_op = MARK; |
| 2861 | const char additems_op = ADDITEMS; |
| 2862 | |
| 2863 | if (self->proto < 4) { |
| 2864 | PyObject *items; |
| 2865 | PyObject *reduce_value; |
| 2866 | int status; |
| 2867 | |
| 2868 | items = PySequence_List(obj); |
| 2869 | if (items == NULL) { |
| 2870 | return -1; |
| 2871 | } |
| 2872 | reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items); |
| 2873 | Py_DECREF(items); |
| 2874 | if (reduce_value == NULL) { |
| 2875 | return -1; |
| 2876 | } |
| 2877 | /* save_reduce() will memoize the object automatically. */ |
| 2878 | status = save_reduce(self, reduce_value, obj); |
| 2879 | Py_DECREF(reduce_value); |
| 2880 | return status; |
| 2881 | } |
| 2882 | |
| 2883 | if (_Pickler_Write(self, &empty_set_op, 1) < 0) |
| 2884 | return -1; |
| 2885 | |
| 2886 | if (memo_put(self, obj) < 0) |
| 2887 | return -1; |
| 2888 | |
| 2889 | set_size = PySet_GET_SIZE(obj); |
| 2890 | if (set_size == 0) |
| 2891 | return 0; /* nothing to do */ |
| 2892 | |
| 2893 | /* Write in batches of BATCHSIZE. */ |
| 2894 | do { |
| 2895 | i = 0; |
| 2896 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2897 | return -1; |
| 2898 | while (_PySet_NextEntry(obj, &ppos, &item, &hash)) { |
| 2899 | if (save(self, item, 0) < 0) |
| 2900 | return -1; |
| 2901 | if (++i == BATCHSIZE) |
| 2902 | break; |
| 2903 | } |
| 2904 | if (_Pickler_Write(self, &additems_op, 1) < 0) |
| 2905 | return -1; |
| 2906 | if (PySet_GET_SIZE(obj) != set_size) { |
| 2907 | PyErr_Format( |
| 2908 | PyExc_RuntimeError, |
| 2909 | "set changed size during iteration"); |
| 2910 | return -1; |
| 2911 | } |
| 2912 | } while (i == BATCHSIZE); |
| 2913 | |
| 2914 | return 0; |
| 2915 | } |
| 2916 | |
| 2917 | static int |
| 2918 | save_frozenset(PicklerObject *self, PyObject *obj) |
| 2919 | { |
| 2920 | PyObject *iter; |
| 2921 | |
| 2922 | const char mark_op = MARK; |
| 2923 | const char frozenset_op = FROZENSET; |
| 2924 | |
| 2925 | if (self->fast && !fast_save_enter(self, obj)) |
| 2926 | return -1; |
| 2927 | |
| 2928 | if (self->proto < 4) { |
| 2929 | PyObject *items; |
| 2930 | PyObject *reduce_value; |
| 2931 | int status; |
| 2932 | |
| 2933 | items = PySequence_List(obj); |
| 2934 | if (items == NULL) { |
| 2935 | return -1; |
| 2936 | } |
| 2937 | reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type, |
| 2938 | items); |
| 2939 | Py_DECREF(items); |
| 2940 | if (reduce_value == NULL) { |
| 2941 | return -1; |
| 2942 | } |
| 2943 | /* save_reduce() will memoize the object automatically. */ |
| 2944 | status = save_reduce(self, reduce_value, obj); |
| 2945 | Py_DECREF(reduce_value); |
| 2946 | return status; |
| 2947 | } |
| 2948 | |
| 2949 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2950 | return -1; |
| 2951 | |
| 2952 | iter = PyObject_GetIter(obj); |
Christian Heimes | b3d3ee4 | 2013-11-23 21:01:40 +0100 | [diff] [blame] | 2953 | if (iter == NULL) { |
Christian Heimes | 74d8d63 | 2013-11-23 21:05:31 +0100 | [diff] [blame] | 2954 | return -1; |
Christian Heimes | b3d3ee4 | 2013-11-23 21:01:40 +0100 | [diff] [blame] | 2955 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2956 | for (;;) { |
| 2957 | PyObject *item; |
| 2958 | |
| 2959 | item = PyIter_Next(iter); |
| 2960 | if (item == NULL) { |
| 2961 | if (PyErr_Occurred()) { |
| 2962 | Py_DECREF(iter); |
| 2963 | return -1; |
| 2964 | } |
| 2965 | break; |
| 2966 | } |
| 2967 | if (save(self, item, 0) < 0) { |
| 2968 | Py_DECREF(item); |
| 2969 | Py_DECREF(iter); |
| 2970 | return -1; |
| 2971 | } |
| 2972 | Py_DECREF(item); |
| 2973 | } |
| 2974 | Py_DECREF(iter); |
| 2975 | |
| 2976 | /* If the object is already in the memo, this means it is |
| 2977 | recursive. In this case, throw away everything we put on the |
| 2978 | stack, and fetch the object back from the memo. */ |
| 2979 | if (PyMemoTable_Get(self->memo, obj)) { |
| 2980 | const char pop_mark_op = POP_MARK; |
| 2981 | |
| 2982 | if (_Pickler_Write(self, &pop_mark_op, 1) < 0) |
| 2983 | return -1; |
| 2984 | if (memo_get(self, obj) < 0) |
| 2985 | return -1; |
| 2986 | return 0; |
| 2987 | } |
| 2988 | |
| 2989 | if (_Pickler_Write(self, &frozenset_op, 1) < 0) |
| 2990 | return -1; |
| 2991 | if (memo_put(self, obj) < 0) |
| 2992 | return -1; |
| 2993 | |
| 2994 | return 0; |
| 2995 | } |
| 2996 | |
| 2997 | static int |
| 2998 | fix_imports(PyObject **module_name, PyObject **global_name) |
| 2999 | { |
| 3000 | PyObject *key; |
| 3001 | PyObject *item; |
| 3002 | |
| 3003 | key = PyTuple_Pack(2, *module_name, *global_name); |
| 3004 | if (key == NULL) |
| 3005 | return -1; |
| 3006 | item = PyDict_GetItemWithError(name_mapping_3to2, key); |
| 3007 | Py_DECREF(key); |
| 3008 | if (item) { |
| 3009 | PyObject *fixed_module_name; |
| 3010 | PyObject *fixed_global_name; |
| 3011 | |
| 3012 | if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
| 3013 | PyErr_Format(PyExc_RuntimeError, |
| 3014 | "_compat_pickle.REVERSE_NAME_MAPPING values " |
| 3015 | "should be 2-tuples, not %.200s", |
| 3016 | Py_TYPE(item)->tp_name); |
| 3017 | return -1; |
| 3018 | } |
| 3019 | fixed_module_name = PyTuple_GET_ITEM(item, 0); |
| 3020 | fixed_global_name = PyTuple_GET_ITEM(item, 1); |
| 3021 | if (!PyUnicode_Check(fixed_module_name) || |
| 3022 | !PyUnicode_Check(fixed_global_name)) { |
| 3023 | PyErr_Format(PyExc_RuntimeError, |
| 3024 | "_compat_pickle.REVERSE_NAME_MAPPING values " |
| 3025 | "should be pairs of str, not (%.200s, %.200s)", |
| 3026 | Py_TYPE(fixed_module_name)->tp_name, |
| 3027 | Py_TYPE(fixed_global_name)->tp_name); |
| 3028 | return -1; |
| 3029 | } |
| 3030 | |
| 3031 | Py_CLEAR(*module_name); |
| 3032 | Py_CLEAR(*global_name); |
| 3033 | Py_INCREF(fixed_module_name); |
| 3034 | Py_INCREF(fixed_global_name); |
| 3035 | *module_name = fixed_module_name; |
| 3036 | *global_name = fixed_global_name; |
| 3037 | } |
| 3038 | else if (PyErr_Occurred()) { |
| 3039 | return -1; |
| 3040 | } |
| 3041 | |
| 3042 | item = PyDict_GetItemWithError(import_mapping_3to2, *module_name); |
| 3043 | if (item) { |
| 3044 | if (!PyUnicode_Check(item)) { |
| 3045 | PyErr_Format(PyExc_RuntimeError, |
| 3046 | "_compat_pickle.REVERSE_IMPORT_MAPPING values " |
| 3047 | "should be strings, not %.200s", |
| 3048 | Py_TYPE(item)->tp_name); |
| 3049 | return -1; |
| 3050 | } |
| 3051 | Py_CLEAR(*module_name); |
| 3052 | Py_INCREF(item); |
| 3053 | *module_name = item; |
| 3054 | } |
| 3055 | else if (PyErr_Occurred()) { |
| 3056 | return -1; |
| 3057 | } |
| 3058 | |
| 3059 | return 0; |
| 3060 | } |
| 3061 | |
| 3062 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3063 | save_global(PicklerObject *self, PyObject *obj, PyObject *name) |
| 3064 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3065 | PyObject *global_name = NULL; |
| 3066 | PyObject *module_name = NULL; |
| 3067 | PyObject *module = NULL; |
| 3068 | PyObject *cls; |
| 3069 | int status = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3070 | _Py_IDENTIFIER(__name__); |
| 3071 | _Py_IDENTIFIER(__qualname__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3072 | |
| 3073 | const char global_op = GLOBAL; |
| 3074 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3075 | if (name) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3076 | Py_INCREF(name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3077 | global_name = name; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3078 | } |
| 3079 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3080 | if (self->proto >= 4) { |
| 3081 | global_name = _PyObject_GetAttrId(obj, &PyId___qualname__); |
| 3082 | if (global_name == NULL) { |
| 3083 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 3084 | goto error; |
| 3085 | PyErr_Clear(); |
| 3086 | } |
| 3087 | } |
| 3088 | if (global_name == NULL) { |
| 3089 | global_name = _PyObject_GetAttrId(obj, &PyId___name__); |
| 3090 | if (global_name == NULL) |
| 3091 | goto error; |
| 3092 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3093 | } |
| 3094 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3095 | module_name = whichmodule(obj, global_name, self->proto >= 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3096 | if (module_name == NULL) |
| 3097 | goto error; |
| 3098 | |
| 3099 | /* XXX: Change to use the import C API directly with level=0 to disallow |
| 3100 | relative imports. |
| 3101 | |
| 3102 | XXX: PyImport_ImportModuleLevel could be used. However, this bypasses |
| 3103 | builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore |
| 3104 | custom import functions (IMHO, this would be a nice security |
| 3105 | feature). The import C API would need to be extended to support the |
| 3106 | extra parameters of __import__ to fix that. */ |
| 3107 | module = PyImport_Import(module_name); |
| 3108 | if (module == NULL) { |
| 3109 | PyErr_Format(PicklingError, |
| 3110 | "Can't pickle %R: import of module %R failed", |
| 3111 | obj, module_name); |
| 3112 | goto error; |
| 3113 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3114 | cls = getattribute(module, global_name, self->proto >= 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3115 | if (cls == NULL) { |
| 3116 | PyErr_Format(PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3117 | "Can't pickle %R: attribute lookup %S on %S failed", |
| 3118 | obj, global_name, module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3119 | goto error; |
| 3120 | } |
| 3121 | if (cls != obj) { |
| 3122 | Py_DECREF(cls); |
| 3123 | PyErr_Format(PicklingError, |
| 3124 | "Can't pickle %R: it's not the same object as %S.%S", |
| 3125 | obj, module_name, global_name); |
| 3126 | goto error; |
| 3127 | } |
| 3128 | Py_DECREF(cls); |
| 3129 | |
| 3130 | if (self->proto >= 2) { |
| 3131 | /* See whether this is in the extension registry, and if |
| 3132 | * so generate an EXT opcode. |
| 3133 | */ |
| 3134 | PyObject *code_obj; /* extension code as Python object */ |
| 3135 | long code; /* extension code as C value */ |
| 3136 | char pdata[5]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3137 | Py_ssize_t n; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3138 | |
| 3139 | PyTuple_SET_ITEM(two_tuple, 0, module_name); |
| 3140 | PyTuple_SET_ITEM(two_tuple, 1, global_name); |
| 3141 | code_obj = PyDict_GetItem(extension_registry, two_tuple); |
| 3142 | /* The object is not registered in the extension registry. |
| 3143 | This is the most likely code path. */ |
| 3144 | if (code_obj == NULL) |
| 3145 | goto gen_global; |
| 3146 | |
| 3147 | /* XXX: pickle.py doesn't check neither the type, nor the range |
| 3148 | of the value returned by the extension_registry. It should for |
| 3149 | consistency. */ |
| 3150 | |
| 3151 | /* Verify code_obj has the right type and value. */ |
| 3152 | if (!PyLong_Check(code_obj)) { |
| 3153 | PyErr_Format(PicklingError, |
| 3154 | "Can't pickle %R: extension code %R isn't an integer", |
| 3155 | obj, code_obj); |
| 3156 | goto error; |
| 3157 | } |
| 3158 | code = PyLong_AS_LONG(code_obj); |
| 3159 | if (code <= 0 || code > 0x7fffffffL) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3160 | if (!PyErr_Occurred()) |
| 3161 | PyErr_Format(PicklingError, |
| 3162 | "Can't pickle %R: extension code %ld is out of range", |
| 3163 | obj, code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3164 | goto error; |
| 3165 | } |
| 3166 | |
| 3167 | /* Generate an EXT opcode. */ |
| 3168 | if (code <= 0xff) { |
| 3169 | pdata[0] = EXT1; |
| 3170 | pdata[1] = (unsigned char)code; |
| 3171 | n = 2; |
| 3172 | } |
| 3173 | else if (code <= 0xffff) { |
| 3174 | pdata[0] = EXT2; |
| 3175 | pdata[1] = (unsigned char)(code & 0xff); |
| 3176 | pdata[2] = (unsigned char)((code >> 8) & 0xff); |
| 3177 | n = 3; |
| 3178 | } |
| 3179 | else { |
| 3180 | pdata[0] = EXT4; |
| 3181 | pdata[1] = (unsigned char)(code & 0xff); |
| 3182 | pdata[2] = (unsigned char)((code >> 8) & 0xff); |
| 3183 | pdata[3] = (unsigned char)((code >> 16) & 0xff); |
| 3184 | pdata[4] = (unsigned char)((code >> 24) & 0xff); |
| 3185 | n = 5; |
| 3186 | } |
| 3187 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3188 | if (_Pickler_Write(self, pdata, n) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3189 | goto error; |
| 3190 | } |
| 3191 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3192 | gen_global: |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3193 | if (self->proto >= 4) { |
| 3194 | const char stack_global_op = STACK_GLOBAL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3195 | |
Christian Heimes | e8b1ba1 | 2013-11-23 21:13:39 +0100 | [diff] [blame^] | 3196 | if (save(self, module_name, 0) < 0) |
| 3197 | goto error; |
| 3198 | if (save(self, global_name, 0) < 0) |
| 3199 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3200 | |
| 3201 | if (_Pickler_Write(self, &stack_global_op, 1) < 0) |
| 3202 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3203 | } |
| 3204 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3205 | /* Generate a normal global opcode if we are using a pickle |
| 3206 | protocol < 4, or if the object is not registered in the |
| 3207 | extension registry. */ |
| 3208 | PyObject *encoded; |
| 3209 | PyObject *(*unicode_encoder)(PyObject *); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3210 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3211 | if (_Pickler_Write(self, &global_op, 1) < 0) |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3212 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3213 | |
| 3214 | /* For protocol < 3 and if the user didn't request against doing |
| 3215 | so, we convert module names to the old 2.x module names. */ |
| 3216 | if (self->proto < 3 && self->fix_imports) { |
| 3217 | if (fix_imports(&module_name, &global_name) < 0) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3218 | goto error; |
| 3219 | } |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3220 | } |
| 3221 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3222 | /* Since Python 3.0 now supports non-ASCII identifiers, we encode |
| 3223 | both the module name and the global name using UTF-8. We do so |
| 3224 | only when we are using the pickle protocol newer than version |
| 3225 | 3. This is to ensure compatibility with older Unpickler running |
| 3226 | on Python 2.x. */ |
| 3227 | if (self->proto == 3) { |
| 3228 | unicode_encoder = PyUnicode_AsUTF8String; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3229 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3230 | else { |
| 3231 | unicode_encoder = PyUnicode_AsASCIIString; |
| 3232 | } |
| 3233 | encoded = unicode_encoder(module_name); |
| 3234 | if (encoded == NULL) { |
| 3235 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
| 3236 | PyErr_Format(PicklingError, |
| 3237 | "can't pickle module identifier '%S' using " |
| 3238 | "pickle protocol %i", |
| 3239 | module_name, self->proto); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3240 | goto error; |
| 3241 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3242 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), |
| 3243 | PyBytes_GET_SIZE(encoded)) < 0) { |
| 3244 | Py_DECREF(encoded); |
| 3245 | goto error; |
| 3246 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3247 | Py_DECREF(encoded); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3248 | if(_Pickler_Write(self, "\n", 1) < 0) |
| 3249 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3250 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3251 | /* Save the name of the module. */ |
| 3252 | encoded = unicode_encoder(global_name); |
| 3253 | if (encoded == NULL) { |
| 3254 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
| 3255 | PyErr_Format(PicklingError, |
| 3256 | "can't pickle global identifier '%S' using " |
| 3257 | "pickle protocol %i", |
| 3258 | global_name, self->proto); |
| 3259 | goto error; |
| 3260 | } |
| 3261 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), |
| 3262 | PyBytes_GET_SIZE(encoded)) < 0) { |
| 3263 | Py_DECREF(encoded); |
| 3264 | goto error; |
| 3265 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3266 | Py_DECREF(encoded); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3267 | if (_Pickler_Write(self, "\n", 1) < 0) |
| 3268 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3269 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3270 | /* Memoize the object. */ |
| 3271 | if (memo_put(self, obj) < 0) |
| 3272 | goto error; |
| 3273 | } |
| 3274 | |
| 3275 | if (0) { |
| 3276 | error: |
| 3277 | status = -1; |
| 3278 | } |
| 3279 | Py_XDECREF(module_name); |
| 3280 | Py_XDECREF(global_name); |
| 3281 | Py_XDECREF(module); |
| 3282 | |
| 3283 | return status; |
| 3284 | } |
| 3285 | |
| 3286 | static int |
Łukasz Langa | f3078fb | 2012-03-12 19:46:12 +0100 | [diff] [blame] | 3287 | save_ellipsis(PicklerObject *self, PyObject *obj) |
| 3288 | { |
Łukasz Langa | dbd7825 | 2012-03-12 22:59:11 +0100 | [diff] [blame] | 3289 | PyObject *str = PyUnicode_FromString("Ellipsis"); |
Benjamin Peterson | e80b29b | 2012-03-16 18:45:31 -0500 | [diff] [blame] | 3290 | int res; |
Łukasz Langa | dbd7825 | 2012-03-12 22:59:11 +0100 | [diff] [blame] | 3291 | if (str == NULL) |
Łukasz Langa | cad1a07 | 2012-03-12 23:41:07 +0100 | [diff] [blame] | 3292 | return -1; |
Benjamin Peterson | e80b29b | 2012-03-16 18:45:31 -0500 | [diff] [blame] | 3293 | res = save_global(self, Py_Ellipsis, str); |
| 3294 | Py_DECREF(str); |
| 3295 | return res; |
Łukasz Langa | f3078fb | 2012-03-12 19:46:12 +0100 | [diff] [blame] | 3296 | } |
| 3297 | |
| 3298 | static int |
| 3299 | save_notimplemented(PicklerObject *self, PyObject *obj) |
| 3300 | { |
Łukasz Langa | dbd7825 | 2012-03-12 22:59:11 +0100 | [diff] [blame] | 3301 | PyObject *str = PyUnicode_FromString("NotImplemented"); |
Benjamin Peterson | e80b29b | 2012-03-16 18:45:31 -0500 | [diff] [blame] | 3302 | int res; |
Łukasz Langa | dbd7825 | 2012-03-12 22:59:11 +0100 | [diff] [blame] | 3303 | if (str == NULL) |
Łukasz Langa | cad1a07 | 2012-03-12 23:41:07 +0100 | [diff] [blame] | 3304 | return -1; |
Benjamin Peterson | e80b29b | 2012-03-16 18:45:31 -0500 | [diff] [blame] | 3305 | res = save_global(self, Py_NotImplemented, str); |
| 3306 | Py_DECREF(str); |
| 3307 | return res; |
Łukasz Langa | f3078fb | 2012-03-12 19:46:12 +0100 | [diff] [blame] | 3308 | } |
| 3309 | |
| 3310 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3311 | save_pers(PicklerObject *self, PyObject *obj, PyObject *func) |
| 3312 | { |
| 3313 | PyObject *pid = NULL; |
| 3314 | int status = 0; |
| 3315 | |
| 3316 | const char persid_op = PERSID; |
| 3317 | const char binpersid_op = BINPERSID; |
| 3318 | |
| 3319 | Py_INCREF(obj); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3320 | pid = _Pickler_FastCall(self, func, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3321 | if (pid == NULL) |
| 3322 | return -1; |
| 3323 | |
| 3324 | if (pid != Py_None) { |
| 3325 | if (self->bin) { |
| 3326 | if (save(self, pid, 1) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3327 | _Pickler_Write(self, &binpersid_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3328 | goto error; |
| 3329 | } |
| 3330 | else { |
| 3331 | PyObject *pid_str = NULL; |
| 3332 | char *pid_ascii_bytes; |
| 3333 | Py_ssize_t size; |
| 3334 | |
| 3335 | pid_str = PyObject_Str(pid); |
| 3336 | if (pid_str == NULL) |
| 3337 | goto error; |
| 3338 | |
| 3339 | /* XXX: Should it check whether the persistent id only contains |
| 3340 | ASCII characters? And what if the pid contains embedded |
| 3341 | newlines? */ |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 3342 | pid_ascii_bytes = _PyUnicode_AsStringAndSize(pid_str, &size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3343 | Py_DECREF(pid_str); |
| 3344 | if (pid_ascii_bytes == NULL) |
| 3345 | goto error; |
| 3346 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3347 | if (_Pickler_Write(self, &persid_op, 1) < 0 || |
| 3348 | _Pickler_Write(self, pid_ascii_bytes, size) < 0 || |
| 3349 | _Pickler_Write(self, "\n", 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3350 | goto error; |
| 3351 | } |
| 3352 | status = 1; |
| 3353 | } |
| 3354 | |
| 3355 | if (0) { |
| 3356 | error: |
| 3357 | status = -1; |
| 3358 | } |
| 3359 | Py_XDECREF(pid); |
| 3360 | |
| 3361 | return status; |
| 3362 | } |
| 3363 | |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3364 | static PyObject * |
| 3365 | get_class(PyObject *obj) |
| 3366 | { |
| 3367 | PyObject *cls; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3368 | _Py_IDENTIFIER(__class__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3369 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3370 | cls = _PyObject_GetAttrId(obj, &PyId___class__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3371 | if (cls == NULL) { |
| 3372 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 3373 | PyErr_Clear(); |
| 3374 | cls = (PyObject *) Py_TYPE(obj); |
| 3375 | Py_INCREF(cls); |
| 3376 | } |
| 3377 | } |
| 3378 | return cls; |
| 3379 | } |
| 3380 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3381 | /* We're saving obj, and args is the 2-thru-5 tuple returned by the |
| 3382 | * appropriate __reduce__ method for obj. |
| 3383 | */ |
| 3384 | static int |
| 3385 | save_reduce(PicklerObject *self, PyObject *args, PyObject *obj) |
| 3386 | { |
| 3387 | PyObject *callable; |
| 3388 | PyObject *argtup; |
| 3389 | PyObject *state = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3390 | PyObject *listitems = Py_None; |
| 3391 | PyObject *dictitems = Py_None; |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3392 | Py_ssize_t size; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3393 | int use_newobj = 0, use_newobj_ex = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3394 | |
| 3395 | const char reduce_op = REDUCE; |
| 3396 | const char build_op = BUILD; |
| 3397 | const char newobj_op = NEWOBJ; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3398 | const char newobj_ex_op = NEWOBJ_EX; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3399 | |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3400 | size = PyTuple_Size(args); |
| 3401 | if (size < 2 || size > 5) { |
| 3402 | PyErr_SetString(PicklingError, "tuple returned by " |
| 3403 | "__reduce__ must contain 2 through 5 elements"); |
| 3404 | return -1; |
| 3405 | } |
| 3406 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3407 | if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5, |
| 3408 | &callable, &argtup, &state, &listitems, &dictitems)) |
| 3409 | return -1; |
| 3410 | |
| 3411 | if (!PyCallable_Check(callable)) { |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3412 | PyErr_SetString(PicklingError, "first item of the tuple " |
| 3413 | "returned by __reduce__ must be callable"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3414 | return -1; |
| 3415 | } |
| 3416 | if (!PyTuple_Check(argtup)) { |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3417 | PyErr_SetString(PicklingError, "second item of the tuple " |
| 3418 | "returned by __reduce__ must be a tuple"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3419 | return -1; |
| 3420 | } |
| 3421 | |
| 3422 | if (state == Py_None) |
| 3423 | state = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3424 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3425 | if (listitems == Py_None) |
| 3426 | listitems = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3427 | else if (!PyIter_Check(listitems)) { |
Alexandre Vassalotti | 00d83f2 | 2013-04-14 01:28:01 -0700 | [diff] [blame] | 3428 | PyErr_Format(PicklingError, "fourth element of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3429 | "returned by __reduce__ must be an iterator, not %s", |
| 3430 | Py_TYPE(listitems)->tp_name); |
| 3431 | return -1; |
| 3432 | } |
| 3433 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3434 | if (dictitems == Py_None) |
| 3435 | dictitems = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3436 | else if (!PyIter_Check(dictitems)) { |
Alexandre Vassalotti | 00d83f2 | 2013-04-14 01:28:01 -0700 | [diff] [blame] | 3437 | PyErr_Format(PicklingError, "fifth element of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3438 | "returned by __reduce__ must be an iterator, not %s", |
| 3439 | Py_TYPE(dictitems)->tp_name); |
| 3440 | return -1; |
| 3441 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3442 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3443 | if (self->proto >= 2) { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3444 | PyObject *name; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3445 | _Py_IDENTIFIER(__name__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3446 | |
Victor Stinner | 804e05e | 2013-11-14 01:26:17 +0100 | [diff] [blame] | 3447 | name = _PyObject_GetAttrId(callable, &PyId___name__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3448 | if (name == NULL) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3449 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3450 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3451 | } |
| 3452 | PyErr_Clear(); |
| 3453 | } |
| 3454 | else if (self->proto >= 4) { |
| 3455 | _Py_IDENTIFIER(__newobj_ex__); |
| 3456 | use_newobj_ex = PyUnicode_Check(name) && |
| 3457 | PyUnicode_Compare( |
| 3458 | name, _PyUnicode_FromId(&PyId___newobj_ex__)) == 0; |
| 3459 | Py_DECREF(name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3460 | } |
| 3461 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3462 | _Py_IDENTIFIER(__newobj__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3463 | use_newobj = PyUnicode_Check(name) && |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3464 | PyUnicode_Compare( |
| 3465 | name, _PyUnicode_FromId(&PyId___newobj__)) == 0; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3466 | Py_DECREF(name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3467 | } |
| 3468 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3469 | |
| 3470 | if (use_newobj_ex) { |
| 3471 | PyObject *cls; |
| 3472 | PyObject *args; |
| 3473 | PyObject *kwargs; |
| 3474 | |
| 3475 | if (Py_SIZE(argtup) != 3) { |
| 3476 | PyErr_Format(PicklingError, |
| 3477 | "length of the NEWOBJ_EX argument tuple must be " |
| 3478 | "exactly 3, not %zd", Py_SIZE(argtup)); |
| 3479 | return -1; |
| 3480 | } |
| 3481 | |
| 3482 | cls = PyTuple_GET_ITEM(argtup, 0); |
| 3483 | if (!PyType_Check(cls)) { |
| 3484 | PyErr_Format(PicklingError, |
| 3485 | "first item from NEWOBJ_EX argument tuple must " |
| 3486 | "be a class, not %.200s", Py_TYPE(cls)->tp_name); |
| 3487 | return -1; |
| 3488 | } |
| 3489 | args = PyTuple_GET_ITEM(argtup, 1); |
| 3490 | if (!PyTuple_Check(args)) { |
| 3491 | PyErr_Format(PicklingError, |
| 3492 | "second item from NEWOBJ_EX argument tuple must " |
| 3493 | "be a tuple, not %.200s", Py_TYPE(args)->tp_name); |
| 3494 | return -1; |
| 3495 | } |
| 3496 | kwargs = PyTuple_GET_ITEM(argtup, 2); |
| 3497 | if (!PyDict_Check(kwargs)) { |
| 3498 | PyErr_Format(PicklingError, |
| 3499 | "third item from NEWOBJ_EX argument tuple must " |
| 3500 | "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name); |
| 3501 | return -1; |
| 3502 | } |
| 3503 | |
| 3504 | if (save(self, cls, 0) < 0 || |
| 3505 | save(self, args, 0) < 0 || |
| 3506 | save(self, kwargs, 0) < 0 || |
| 3507 | _Pickler_Write(self, &newobj_ex_op, 1) < 0) { |
| 3508 | return -1; |
| 3509 | } |
| 3510 | } |
| 3511 | else if (use_newobj) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3512 | PyObject *cls; |
| 3513 | PyObject *newargtup; |
| 3514 | PyObject *obj_class; |
| 3515 | int p; |
| 3516 | |
| 3517 | /* Sanity checks. */ |
| 3518 | if (Py_SIZE(argtup) < 1) { |
| 3519 | PyErr_SetString(PicklingError, "__newobj__ arglist is empty"); |
| 3520 | return -1; |
| 3521 | } |
| 3522 | |
| 3523 | cls = PyTuple_GET_ITEM(argtup, 0); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3524 | if (!PyType_Check(cls)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3525 | PyErr_SetString(PicklingError, "args[0] from " |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3526 | "__newobj__ args is not a type"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3527 | return -1; |
| 3528 | } |
| 3529 | |
| 3530 | if (obj != NULL) { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3531 | obj_class = get_class(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3532 | p = obj_class != cls; /* true iff a problem */ |
| 3533 | Py_DECREF(obj_class); |
| 3534 | if (p) { |
| 3535 | PyErr_SetString(PicklingError, "args[0] from " |
| 3536 | "__newobj__ args has the wrong class"); |
| 3537 | return -1; |
| 3538 | } |
| 3539 | } |
| 3540 | /* XXX: These calls save() are prone to infinite recursion. Imagine |
| 3541 | what happen if the value returned by the __reduce__() method of |
| 3542 | some extension type contains another object of the same type. Ouch! |
| 3543 | |
| 3544 | Here is a quick example, that I ran into, to illustrate what I |
| 3545 | mean: |
| 3546 | |
| 3547 | >>> import pickle, copyreg |
| 3548 | >>> copyreg.dispatch_table.pop(complex) |
| 3549 | >>> pickle.dumps(1+2j) |
| 3550 | Traceback (most recent call last): |
| 3551 | ... |
| 3552 | RuntimeError: maximum recursion depth exceeded |
| 3553 | |
| 3554 | Removing the complex class from copyreg.dispatch_table made the |
| 3555 | __reduce_ex__() method emit another complex object: |
| 3556 | |
| 3557 | >>> (1+1j).__reduce_ex__(2) |
| 3558 | (<function __newobj__ at 0xb7b71c3c>, |
| 3559 | (<class 'complex'>, (1+1j)), None, None, None) |
| 3560 | |
| 3561 | Thus when save() was called on newargstup (the 2nd item) recursion |
| 3562 | ensued. Of course, the bug was in the complex class which had a |
| 3563 | broken __getnewargs__() that emitted another complex object. But, |
| 3564 | the point, here, is it is quite easy to end up with a broken reduce |
| 3565 | function. */ |
| 3566 | |
| 3567 | /* Save the class and its __new__ arguments. */ |
| 3568 | if (save(self, cls, 0) < 0) |
| 3569 | return -1; |
| 3570 | |
| 3571 | newargtup = PyTuple_GetSlice(argtup, 1, Py_SIZE(argtup)); |
| 3572 | if (newargtup == NULL) |
| 3573 | return -1; |
| 3574 | |
| 3575 | p = save(self, newargtup, 0); |
| 3576 | Py_DECREF(newargtup); |
| 3577 | if (p < 0) |
| 3578 | return -1; |
| 3579 | |
| 3580 | /* Add NEWOBJ opcode. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3581 | if (_Pickler_Write(self, &newobj_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3582 | return -1; |
| 3583 | } |
| 3584 | else { /* Not using NEWOBJ. */ |
| 3585 | if (save(self, callable, 0) < 0 || |
| 3586 | save(self, argtup, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3587 | _Pickler_Write(self, &reduce_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3588 | return -1; |
| 3589 | } |
| 3590 | |
| 3591 | /* obj can be NULL when save_reduce() is used directly. A NULL obj means |
| 3592 | the caller do not want to memoize the object. Not particularly useful, |
| 3593 | but that is to mimic the behavior save_reduce() in pickle.py when |
| 3594 | obj is None. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3595 | if (obj != NULL) { |
| 3596 | /* If the object is already in the memo, this means it is |
| 3597 | recursive. In this case, throw away everything we put on the |
| 3598 | stack, and fetch the object back from the memo. */ |
| 3599 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3600 | const char pop_op = POP; |
| 3601 | |
| 3602 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 3603 | return -1; |
| 3604 | if (memo_get(self, obj) < 0) |
| 3605 | return -1; |
| 3606 | |
| 3607 | return 0; |
| 3608 | } |
| 3609 | else if (memo_put(self, obj) < 0) |
| 3610 | return -1; |
| 3611 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3612 | |
| 3613 | if (listitems && batch_list(self, listitems) < 0) |
| 3614 | return -1; |
| 3615 | |
| 3616 | if (dictitems && batch_dict(self, dictitems) < 0) |
| 3617 | return -1; |
| 3618 | |
| 3619 | if (state) { |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 3620 | if (save(self, state, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3621 | _Pickler_Write(self, &build_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3622 | return -1; |
| 3623 | } |
| 3624 | |
| 3625 | return 0; |
| 3626 | } |
| 3627 | |
| 3628 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3629 | save_method(PicklerObject *self, PyObject *obj) |
| 3630 | { |
| 3631 | PyObject *method_self = PyCFunction_GET_SELF(obj); |
| 3632 | |
| 3633 | if (method_self == NULL || PyModule_Check(method_self)) { |
| 3634 | return save_global(self, obj, NULL); |
| 3635 | } |
| 3636 | else { |
| 3637 | PyObject *builtins; |
| 3638 | PyObject *getattr; |
| 3639 | PyObject *reduce_value; |
| 3640 | int status = -1; |
| 3641 | _Py_IDENTIFIER(getattr); |
| 3642 | |
| 3643 | builtins = PyEval_GetBuiltins(); |
| 3644 | getattr = _PyDict_GetItemId(builtins, &PyId_getattr); |
| 3645 | reduce_value = \ |
| 3646 | Py_BuildValue("O(Os)", getattr, method_self, |
| 3647 | ((PyCFunctionObject *)obj)->m_ml->ml_name); |
| 3648 | if (reduce_value != NULL) { |
| 3649 | status = save_reduce(self, reduce_value, obj); |
| 3650 | Py_DECREF(reduce_value); |
| 3651 | } |
| 3652 | return status; |
| 3653 | } |
| 3654 | } |
| 3655 | |
| 3656 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3657 | save(PicklerObject *self, PyObject *obj, int pers_save) |
| 3658 | { |
| 3659 | PyTypeObject *type; |
| 3660 | PyObject *reduce_func = NULL; |
| 3661 | PyObject *reduce_value = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3662 | int status = 0; |
| 3663 | |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 3664 | if (Py_EnterRecursiveCall(" while pickling an object")) |
Alexandre Vassalotti | dff1834 | 2008-07-13 18:48:30 +0000 | [diff] [blame] | 3665 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3666 | |
| 3667 | /* The extra pers_save argument is necessary to avoid calling save_pers() |
| 3668 | on its returned object. */ |
| 3669 | if (!pers_save && self->pers_func) { |
| 3670 | /* save_pers() returns: |
| 3671 | -1 to signal an error; |
| 3672 | 0 if it did nothing successfully; |
| 3673 | 1 if a persistent id was saved. |
| 3674 | */ |
| 3675 | if ((status = save_pers(self, obj, self->pers_func)) != 0) |
| 3676 | goto done; |
| 3677 | } |
| 3678 | |
| 3679 | type = Py_TYPE(obj); |
| 3680 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3681 | /* The old cPickle had an optimization that used switch-case statement |
| 3682 | dispatching on the first letter of the type name. This has was removed |
| 3683 | since benchmarks shown that this optimization was actually slowing |
| 3684 | things down. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3685 | |
| 3686 | /* Atom types; these aren't memoized, so don't check the memo. */ |
| 3687 | |
| 3688 | if (obj == Py_None) { |
| 3689 | status = save_none(self, obj); |
| 3690 | goto done; |
| 3691 | } |
Łukasz Langa | f3078fb | 2012-03-12 19:46:12 +0100 | [diff] [blame] | 3692 | else if (obj == Py_Ellipsis) { |
| 3693 | status = save_ellipsis(self, obj); |
| 3694 | goto done; |
| 3695 | } |
| 3696 | else if (obj == Py_NotImplemented) { |
| 3697 | status = save_notimplemented(self, obj); |
| 3698 | goto done; |
| 3699 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3700 | else if (obj == Py_False || obj == Py_True) { |
| 3701 | status = save_bool(self, obj); |
| 3702 | goto done; |
| 3703 | } |
| 3704 | else if (type == &PyLong_Type) { |
| 3705 | status = save_long(self, obj); |
| 3706 | goto done; |
| 3707 | } |
| 3708 | else if (type == &PyFloat_Type) { |
| 3709 | status = save_float(self, obj); |
| 3710 | goto done; |
| 3711 | } |
| 3712 | |
| 3713 | /* Check the memo to see if it has the object. If so, generate |
| 3714 | a GET (or BINGET) opcode, instead of pickling the object |
| 3715 | once again. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3716 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3717 | if (memo_get(self, obj) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3718 | goto error; |
| 3719 | goto done; |
| 3720 | } |
| 3721 | |
| 3722 | if (type == &PyBytes_Type) { |
| 3723 | status = save_bytes(self, obj); |
| 3724 | goto done; |
| 3725 | } |
| 3726 | else if (type == &PyUnicode_Type) { |
| 3727 | status = save_unicode(self, obj); |
| 3728 | goto done; |
| 3729 | } |
| 3730 | else if (type == &PyDict_Type) { |
| 3731 | status = save_dict(self, obj); |
| 3732 | goto done; |
| 3733 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3734 | else if (type == &PySet_Type) { |
| 3735 | status = save_set(self, obj); |
| 3736 | goto done; |
| 3737 | } |
| 3738 | else if (type == &PyFrozenSet_Type) { |
| 3739 | status = save_frozenset(self, obj); |
| 3740 | goto done; |
| 3741 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3742 | else if (type == &PyList_Type) { |
| 3743 | status = save_list(self, obj); |
| 3744 | goto done; |
| 3745 | } |
| 3746 | else if (type == &PyTuple_Type) { |
| 3747 | status = save_tuple(self, obj); |
| 3748 | goto done; |
| 3749 | } |
| 3750 | else if (type == &PyType_Type) { |
| 3751 | status = save_global(self, obj, NULL); |
| 3752 | goto done; |
| 3753 | } |
| 3754 | else if (type == &PyFunction_Type) { |
| 3755 | status = save_global(self, obj, NULL); |
| 3756 | if (status < 0 && PyErr_ExceptionMatches(PickleError)) { |
| 3757 | /* fall back to reduce */ |
| 3758 | PyErr_Clear(); |
| 3759 | } |
| 3760 | else { |
| 3761 | goto done; |
| 3762 | } |
| 3763 | } |
| 3764 | else if (type == &PyCFunction_Type) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3765 | status = save_method(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3766 | goto done; |
| 3767 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3768 | |
| 3769 | /* XXX: This part needs some unit tests. */ |
| 3770 | |
| 3771 | /* Get a reduction callable, and call it. This may come from |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3772 | * self.dispatch_table, copyreg.dispatch_table, the object's |
| 3773 | * __reduce_ex__ method, or the object's __reduce__ method. |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3774 | */ |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3775 | if (self->dispatch_table == NULL) { |
| 3776 | reduce_func = PyDict_GetItem(dispatch_table, (PyObject *)type); |
| 3777 | /* PyDict_GetItem() unlike PyObject_GetItem() and |
| 3778 | PyObject_GetAttr() returns a borrowed ref */ |
| 3779 | Py_XINCREF(reduce_func); |
| 3780 | } else { |
| 3781 | reduce_func = PyObject_GetItem(self->dispatch_table, (PyObject *)type); |
| 3782 | if (reduce_func == NULL) { |
| 3783 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 3784 | PyErr_Clear(); |
| 3785 | else |
| 3786 | goto error; |
| 3787 | } |
| 3788 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3789 | if (reduce_func != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3790 | Py_INCREF(obj); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3791 | reduce_value = _Pickler_FastCall(self, reduce_func, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3792 | } |
Antoine Pitrou | ffd41d9 | 2011-10-04 09:23:04 +0200 | [diff] [blame] | 3793 | else if (PyType_IsSubtype(type, &PyType_Type)) { |
| 3794 | status = save_global(self, obj, NULL); |
| 3795 | goto done; |
| 3796 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3797 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3798 | _Py_IDENTIFIER(__reduce__); |
| 3799 | _Py_IDENTIFIER(__reduce_ex__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3800 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3801 | |
| 3802 | /* XXX: If the __reduce__ method is defined, __reduce_ex__ is |
| 3803 | automatically defined as __reduce__. While this is convenient, this |
| 3804 | make it impossible to know which method was actually called. Of |
| 3805 | course, this is not a big deal. But still, it would be nice to let |
| 3806 | the user know which method was called when something go |
| 3807 | wrong. Incidentally, this means if __reduce_ex__ is not defined, we |
| 3808 | don't actually have to check for a __reduce__ method. */ |
| 3809 | |
| 3810 | /* Check for a __reduce_ex__ method. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3811 | reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3812 | if (reduce_func != NULL) { |
| 3813 | PyObject *proto; |
| 3814 | proto = PyLong_FromLong(self->proto); |
| 3815 | if (proto != NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3816 | reduce_value = _Pickler_FastCall(self, reduce_func, proto); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3817 | } |
| 3818 | } |
| 3819 | else { |
| 3820 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 3821 | PyErr_Clear(); |
| 3822 | else |
| 3823 | goto error; |
| 3824 | /* Check for a __reduce__ method. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3825 | reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3826 | if (reduce_func != NULL) { |
| 3827 | reduce_value = PyObject_Call(reduce_func, empty_tuple, NULL); |
| 3828 | } |
| 3829 | else { |
| 3830 | PyErr_Format(PicklingError, "can't pickle '%.200s' object: %R", |
| 3831 | type->tp_name, obj); |
| 3832 | goto error; |
| 3833 | } |
| 3834 | } |
| 3835 | } |
| 3836 | |
| 3837 | if (reduce_value == NULL) |
| 3838 | goto error; |
| 3839 | |
| 3840 | if (PyUnicode_Check(reduce_value)) { |
| 3841 | status = save_global(self, obj, reduce_value); |
| 3842 | goto done; |
| 3843 | } |
| 3844 | |
| 3845 | if (!PyTuple_Check(reduce_value)) { |
| 3846 | PyErr_SetString(PicklingError, |
| 3847 | "__reduce__ must return a string or tuple"); |
| 3848 | goto error; |
| 3849 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3850 | |
| 3851 | status = save_reduce(self, reduce_value, obj); |
| 3852 | |
| 3853 | if (0) { |
| 3854 | error: |
| 3855 | status = -1; |
| 3856 | } |
| 3857 | done: |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3858 | if (status == 0) |
| 3859 | status = _Pickler_OpcodeBoundary(self); |
Alexandre Vassalotti | dff1834 | 2008-07-13 18:48:30 +0000 | [diff] [blame] | 3860 | Py_LeaveRecursiveCall(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3861 | Py_XDECREF(reduce_func); |
| 3862 | Py_XDECREF(reduce_value); |
| 3863 | |
| 3864 | return status; |
| 3865 | } |
| 3866 | |
| 3867 | static int |
| 3868 | dump(PicklerObject *self, PyObject *obj) |
| 3869 | { |
| 3870 | const char stop_op = STOP; |
| 3871 | |
| 3872 | if (self->proto >= 2) { |
| 3873 | char header[2]; |
| 3874 | |
| 3875 | header[0] = PROTO; |
| 3876 | assert(self->proto >= 0 && self->proto < 256); |
| 3877 | header[1] = (unsigned char)self->proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3878 | if (_Pickler_Write(self, header, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3879 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3880 | if (self->proto >= 4) |
| 3881 | self->framing = 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3882 | } |
| 3883 | |
| 3884 | if (save(self, obj, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3885 | _Pickler_Write(self, &stop_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3886 | return -1; |
| 3887 | |
| 3888 | return 0; |
| 3889 | } |
| 3890 | |
| 3891 | PyDoc_STRVAR(Pickler_clear_memo_doc, |
| 3892 | "clear_memo() -> None. Clears the pickler's \"memo\"." |
| 3893 | "\n" |
| 3894 | "The memo is the data structure that remembers which objects the\n" |
| 3895 | "pickler has already seen, so that shared or recursive objects are\n" |
| 3896 | "pickled by reference and not by value. This method is useful when\n" |
| 3897 | "re-using picklers."); |
| 3898 | |
| 3899 | static PyObject * |
| 3900 | Pickler_clear_memo(PicklerObject *self) |
| 3901 | { |
| 3902 | if (self->memo) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3903 | PyMemoTable_Clear(self->memo); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3904 | |
| 3905 | Py_RETURN_NONE; |
| 3906 | } |
| 3907 | |
| 3908 | PyDoc_STRVAR(Pickler_dump_doc, |
| 3909 | "dump(obj) -> None. Write a pickled representation of obj to the open file."); |
| 3910 | |
| 3911 | static PyObject * |
| 3912 | Pickler_dump(PicklerObject *self, PyObject *args) |
| 3913 | { |
| 3914 | PyObject *obj; |
| 3915 | |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 3916 | /* Check whether the Pickler was initialized correctly (issue3664). |
| 3917 | Developers often forget to call __init__() in their subclasses, which |
| 3918 | would trigger a segfault without this check. */ |
| 3919 | if (self->write == NULL) { |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 3920 | PyErr_Format(PicklingError, |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 3921 | "Pickler.__init__() was not called by %s.__init__()", |
| 3922 | Py_TYPE(self)->tp_name); |
| 3923 | return NULL; |
| 3924 | } |
| 3925 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3926 | if (!PyArg_ParseTuple(args, "O:dump", &obj)) |
| 3927 | return NULL; |
| 3928 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3929 | if (_Pickler_ClearBuffer(self) < 0) |
| 3930 | return NULL; |
| 3931 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3932 | if (dump(self, obj) < 0) |
| 3933 | return NULL; |
| 3934 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3935 | if (_Pickler_FlushToFile(self) < 0) |
| 3936 | return NULL; |
| 3937 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3938 | Py_RETURN_NONE; |
| 3939 | } |
| 3940 | |
| 3941 | static struct PyMethodDef Pickler_methods[] = { |
| 3942 | {"dump", (PyCFunction)Pickler_dump, METH_VARARGS, |
| 3943 | Pickler_dump_doc}, |
| 3944 | {"clear_memo", (PyCFunction)Pickler_clear_memo, METH_NOARGS, |
| 3945 | Pickler_clear_memo_doc}, |
| 3946 | {NULL, NULL} /* sentinel */ |
| 3947 | }; |
| 3948 | |
| 3949 | static void |
| 3950 | Pickler_dealloc(PicklerObject *self) |
| 3951 | { |
| 3952 | PyObject_GC_UnTrack(self); |
| 3953 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3954 | Py_XDECREF(self->output_buffer); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3955 | Py_XDECREF(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3956 | Py_XDECREF(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3957 | Py_XDECREF(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3958 | Py_XDECREF(self->arg); |
| 3959 | Py_XDECREF(self->fast_memo); |
| 3960 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3961 | PyMemoTable_Del(self->memo); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3962 | |
| 3963 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 3964 | } |
| 3965 | |
| 3966 | static int |
| 3967 | Pickler_traverse(PicklerObject *self, visitproc visit, void *arg) |
| 3968 | { |
| 3969 | Py_VISIT(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3970 | Py_VISIT(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3971 | Py_VISIT(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3972 | Py_VISIT(self->arg); |
| 3973 | Py_VISIT(self->fast_memo); |
| 3974 | return 0; |
| 3975 | } |
| 3976 | |
| 3977 | static int |
| 3978 | Pickler_clear(PicklerObject *self) |
| 3979 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3980 | Py_CLEAR(self->output_buffer); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3981 | Py_CLEAR(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3982 | Py_CLEAR(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3983 | Py_CLEAR(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3984 | Py_CLEAR(self->arg); |
| 3985 | Py_CLEAR(self->fast_memo); |
| 3986 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3987 | if (self->memo != NULL) { |
| 3988 | PyMemoTable *memo = self->memo; |
| 3989 | self->memo = NULL; |
| 3990 | PyMemoTable_Del(memo); |
| 3991 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3992 | return 0; |
| 3993 | } |
| 3994 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3995 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3996 | PyDoc_STRVAR(Pickler_doc, |
| 3997 | "Pickler(file, protocol=None)" |
| 3998 | "\n" |
| 3999 | "This takes a binary file for writing a pickle data stream.\n" |
| 4000 | "\n" |
| 4001 | "The optional protocol argument tells the pickler to use the\n" |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4002 | "given protocol; supported protocols are 0, 1, 2, 3 and 4. The\n" |
| 4003 | "default protocol is 3; a backward-incompatible protocol designed for\n" |
| 4004 | "Python 3.\n" |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4005 | "\n" |
| 4006 | "Specifying a negative protocol version selects the highest\n" |
| 4007 | "protocol version supported. The higher the protocol used, the\n" |
| 4008 | "more recent the version of Python needed to read the pickle\n" |
| 4009 | "produced.\n" |
| 4010 | "\n" |
| 4011 | "The file argument must have a write() method that accepts a single\n" |
| 4012 | "bytes argument. It can thus be a file object opened for binary\n" |
| 4013 | "writing, a io.BytesIO instance, or any other custom object that\n" |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 4014 | "meets this interface.\n" |
| 4015 | "\n" |
| 4016 | "If fix_imports is True and protocol is less than 3, pickle will try to\n" |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4017 | "map the new Python 3 names to the old module names used in Python 2,\n" |
| 4018 | "so that the pickle data stream is readable with Python 2.\n"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4019 | |
| 4020 | static int |
| 4021 | Pickler_init(PicklerObject *self, PyObject *args, PyObject *kwds) |
| 4022 | { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 4023 | static char *kwlist[] = {"file", "protocol", "fix_imports", 0}; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4024 | PyObject *file; |
| 4025 | PyObject *proto_obj = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4026 | PyObject *fix_imports = Py_True; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4027 | _Py_IDENTIFIER(persistent_id); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4028 | _Py_IDENTIFIER(dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4029 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4030 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OO:Pickler", |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 4031 | kwlist, &file, &proto_obj, &fix_imports)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4032 | return -1; |
| 4033 | |
| 4034 | /* In case of multiple __init__() calls, clear previous content. */ |
| 4035 | if (self->write != NULL) |
| 4036 | (void)Pickler_clear(self); |
| 4037 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4038 | if (_Pickler_SetProtocol(self, proto_obj, fix_imports) < 0) |
| 4039 | return -1; |
| 4040 | |
| 4041 | if (_Pickler_SetOutputStream(self, file) < 0) |
| 4042 | return -1; |
| 4043 | |
| 4044 | /* memo and output_buffer may have already been created in _Pickler_New */ |
| 4045 | if (self->memo == NULL) { |
| 4046 | self->memo = PyMemoTable_New(); |
| 4047 | if (self->memo == NULL) |
| 4048 | return -1; |
| 4049 | } |
| 4050 | self->output_len = 0; |
| 4051 | if (self->output_buffer == NULL) { |
| 4052 | self->max_output_len = WRITE_BUF_SIZE; |
| 4053 | self->output_buffer = PyBytes_FromStringAndSize(NULL, |
| 4054 | self->max_output_len); |
| 4055 | if (self->output_buffer == NULL) |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 4056 | return -1; |
| 4057 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4058 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 4059 | self->arg = NULL; |
| 4060 | self->fast = 0; |
| 4061 | self->fast_nesting = 0; |
| 4062 | self->fast_memo = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4063 | self->pers_func = NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 4064 | if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) { |
| 4065 | self->pers_func = _PyObject_GetAttrId((PyObject *)self, |
| 4066 | &PyId_persistent_id); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4067 | if (self->pers_func == NULL) |
| 4068 | return -1; |
| 4069 | } |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4070 | self->dispatch_table = NULL; |
| 4071 | if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) { |
| 4072 | self->dispatch_table = _PyObject_GetAttrId((PyObject *)self, |
| 4073 | &PyId_dispatch_table); |
| 4074 | if (self->dispatch_table == NULL) |
| 4075 | return -1; |
| 4076 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4077 | return 0; |
| 4078 | } |
| 4079 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4080 | /* Define a proxy object for the Pickler's internal memo object. This is to |
| 4081 | * avoid breaking code like: |
| 4082 | * pickler.memo.clear() |
| 4083 | * and |
| 4084 | * pickler.memo = saved_memo |
| 4085 | * Is this a good idea? Not really, but we don't want to break code that uses |
| 4086 | * it. Note that we don't implement the entire mapping API here. This is |
| 4087 | * intentional, as these should be treated as black-box implementation details. |
| 4088 | */ |
| 4089 | |
| 4090 | typedef struct { |
| 4091 | PyObject_HEAD |
| 4092 | PicklerObject *pickler; /* Pickler whose memo table we're proxying. */ |
| 4093 | } PicklerMemoProxyObject; |
| 4094 | |
| 4095 | PyDoc_STRVAR(pmp_clear_doc, |
| 4096 | "memo.clear() -> None. Remove all items from memo."); |
| 4097 | |
| 4098 | static PyObject * |
| 4099 | pmp_clear(PicklerMemoProxyObject *self) |
| 4100 | { |
| 4101 | if (self->pickler->memo) |
| 4102 | PyMemoTable_Clear(self->pickler->memo); |
| 4103 | Py_RETURN_NONE; |
| 4104 | } |
| 4105 | |
| 4106 | PyDoc_STRVAR(pmp_copy_doc, |
| 4107 | "memo.copy() -> new_memo. Copy the memo to a new object."); |
| 4108 | |
| 4109 | static PyObject * |
| 4110 | pmp_copy(PicklerMemoProxyObject *self) |
| 4111 | { |
| 4112 | Py_ssize_t i; |
| 4113 | PyMemoTable *memo; |
| 4114 | PyObject *new_memo = PyDict_New(); |
| 4115 | if (new_memo == NULL) |
| 4116 | return NULL; |
| 4117 | |
| 4118 | memo = self->pickler->memo; |
| 4119 | for (i = 0; i < memo->mt_allocated; ++i) { |
| 4120 | PyMemoEntry entry = memo->mt_table[i]; |
| 4121 | if (entry.me_key != NULL) { |
| 4122 | int status; |
| 4123 | PyObject *key, *value; |
| 4124 | |
| 4125 | key = PyLong_FromVoidPtr(entry.me_key); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4126 | value = Py_BuildValue("nO", entry.me_value, entry.me_key); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4127 | |
| 4128 | if (key == NULL || value == NULL) { |
| 4129 | Py_XDECREF(key); |
| 4130 | Py_XDECREF(value); |
| 4131 | goto error; |
| 4132 | } |
| 4133 | status = PyDict_SetItem(new_memo, key, value); |
| 4134 | Py_DECREF(key); |
| 4135 | Py_DECREF(value); |
| 4136 | if (status < 0) |
| 4137 | goto error; |
| 4138 | } |
| 4139 | } |
| 4140 | return new_memo; |
| 4141 | |
| 4142 | error: |
| 4143 | Py_XDECREF(new_memo); |
| 4144 | return NULL; |
| 4145 | } |
| 4146 | |
| 4147 | PyDoc_STRVAR(pmp_reduce_doc, |
| 4148 | "memo.__reduce__(). Pickling support."); |
| 4149 | |
| 4150 | static PyObject * |
| 4151 | pmp_reduce(PicklerMemoProxyObject *self, PyObject *args) |
| 4152 | { |
| 4153 | PyObject *reduce_value, *dict_args; |
| 4154 | PyObject *contents = pmp_copy(self); |
| 4155 | if (contents == NULL) |
| 4156 | return NULL; |
| 4157 | |
| 4158 | reduce_value = PyTuple_New(2); |
| 4159 | if (reduce_value == NULL) { |
| 4160 | Py_DECREF(contents); |
| 4161 | return NULL; |
| 4162 | } |
| 4163 | dict_args = PyTuple_New(1); |
| 4164 | if (dict_args == NULL) { |
| 4165 | Py_DECREF(contents); |
| 4166 | Py_DECREF(reduce_value); |
| 4167 | return NULL; |
| 4168 | } |
| 4169 | PyTuple_SET_ITEM(dict_args, 0, contents); |
| 4170 | Py_INCREF((PyObject *)&PyDict_Type); |
| 4171 | PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type); |
| 4172 | PyTuple_SET_ITEM(reduce_value, 1, dict_args); |
| 4173 | return reduce_value; |
| 4174 | } |
| 4175 | |
| 4176 | static PyMethodDef picklerproxy_methods[] = { |
| 4177 | {"clear", (PyCFunction)pmp_clear, METH_NOARGS, pmp_clear_doc}, |
| 4178 | {"copy", (PyCFunction)pmp_copy, METH_NOARGS, pmp_copy_doc}, |
| 4179 | {"__reduce__", (PyCFunction)pmp_reduce, METH_VARARGS, pmp_reduce_doc}, |
| 4180 | {NULL, NULL} /* sentinel */ |
| 4181 | }; |
| 4182 | |
| 4183 | static void |
| 4184 | PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self) |
| 4185 | { |
| 4186 | PyObject_GC_UnTrack(self); |
| 4187 | Py_XDECREF(self->pickler); |
| 4188 | PyObject_GC_Del((PyObject *)self); |
| 4189 | } |
| 4190 | |
| 4191 | static int |
| 4192 | PicklerMemoProxy_traverse(PicklerMemoProxyObject *self, |
| 4193 | visitproc visit, void *arg) |
| 4194 | { |
| 4195 | Py_VISIT(self->pickler); |
| 4196 | return 0; |
| 4197 | } |
| 4198 | |
| 4199 | static int |
| 4200 | PicklerMemoProxy_clear(PicklerMemoProxyObject *self) |
| 4201 | { |
| 4202 | Py_CLEAR(self->pickler); |
| 4203 | return 0; |
| 4204 | } |
| 4205 | |
| 4206 | static PyTypeObject PicklerMemoProxyType = { |
| 4207 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4208 | "_pickle.PicklerMemoProxy", /*tp_name*/ |
| 4209 | sizeof(PicklerMemoProxyObject), /*tp_basicsize*/ |
| 4210 | 0, |
| 4211 | (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */ |
| 4212 | 0, /* tp_print */ |
| 4213 | 0, /* tp_getattr */ |
| 4214 | 0, /* tp_setattr */ |
| 4215 | 0, /* tp_compare */ |
| 4216 | 0, /* tp_repr */ |
| 4217 | 0, /* tp_as_number */ |
| 4218 | 0, /* tp_as_sequence */ |
| 4219 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 4220 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4221 | 0, /* tp_call */ |
| 4222 | 0, /* tp_str */ |
| 4223 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4224 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 4225 | 0, /* tp_as_buffer */ |
| 4226 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 4227 | 0, /* tp_doc */ |
| 4228 | (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */ |
| 4229 | (inquiry)PicklerMemoProxy_clear, /* tp_clear */ |
| 4230 | 0, /* tp_richcompare */ |
| 4231 | 0, /* tp_weaklistoffset */ |
| 4232 | 0, /* tp_iter */ |
| 4233 | 0, /* tp_iternext */ |
| 4234 | picklerproxy_methods, /* tp_methods */ |
| 4235 | }; |
| 4236 | |
| 4237 | static PyObject * |
| 4238 | PicklerMemoProxy_New(PicklerObject *pickler) |
| 4239 | { |
| 4240 | PicklerMemoProxyObject *self; |
| 4241 | |
| 4242 | self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType); |
| 4243 | if (self == NULL) |
| 4244 | return NULL; |
| 4245 | Py_INCREF(pickler); |
| 4246 | self->pickler = pickler; |
| 4247 | PyObject_GC_Track(self); |
| 4248 | return (PyObject *)self; |
| 4249 | } |
| 4250 | |
| 4251 | /*****************************************************************************/ |
| 4252 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4253 | static PyObject * |
| 4254 | Pickler_get_memo(PicklerObject *self) |
| 4255 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4256 | return PicklerMemoProxy_New(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4257 | } |
| 4258 | |
| 4259 | static int |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4260 | Pickler_set_memo(PicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4261 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4262 | PyMemoTable *new_memo = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4263 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4264 | if (obj == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4265 | PyErr_SetString(PyExc_TypeError, |
| 4266 | "attribute deletion is not supported"); |
| 4267 | return -1; |
| 4268 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4269 | |
| 4270 | if (Py_TYPE(obj) == &PicklerMemoProxyType) { |
| 4271 | PicklerObject *pickler = |
| 4272 | ((PicklerMemoProxyObject *)obj)->pickler; |
| 4273 | |
| 4274 | new_memo = PyMemoTable_Copy(pickler->memo); |
| 4275 | if (new_memo == NULL) |
| 4276 | return -1; |
| 4277 | } |
| 4278 | else if (PyDict_Check(obj)) { |
| 4279 | Py_ssize_t i = 0; |
| 4280 | PyObject *key, *value; |
| 4281 | |
| 4282 | new_memo = PyMemoTable_New(); |
| 4283 | if (new_memo == NULL) |
| 4284 | return -1; |
| 4285 | |
| 4286 | while (PyDict_Next(obj, &i, &key, &value)) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4287 | Py_ssize_t memo_id; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4288 | PyObject *memo_obj; |
| 4289 | |
| 4290 | if (!PyTuple_Check(value) || Py_SIZE(value) != 2) { |
| 4291 | PyErr_SetString(PyExc_TypeError, |
| 4292 | "'memo' values must be 2-item tuples"); |
| 4293 | goto error; |
| 4294 | } |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4295 | memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0)); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4296 | if (memo_id == -1 && PyErr_Occurred()) |
| 4297 | goto error; |
| 4298 | memo_obj = PyTuple_GET_ITEM(value, 1); |
| 4299 | if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0) |
| 4300 | goto error; |
| 4301 | } |
| 4302 | } |
| 4303 | else { |
| 4304 | PyErr_Format(PyExc_TypeError, |
| 4305 | "'memo' attribute must be an PicklerMemoProxy object" |
| 4306 | "or dict, not %.200s", Py_TYPE(obj)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4307 | return -1; |
| 4308 | } |
| 4309 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4310 | PyMemoTable_Del(self->memo); |
| 4311 | self->memo = new_memo; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4312 | |
| 4313 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4314 | |
| 4315 | error: |
| 4316 | if (new_memo) |
| 4317 | PyMemoTable_Del(new_memo); |
| 4318 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4319 | } |
| 4320 | |
| 4321 | static PyObject * |
| 4322 | Pickler_get_persid(PicklerObject *self) |
| 4323 | { |
| 4324 | if (self->pers_func == NULL) |
| 4325 | PyErr_SetString(PyExc_AttributeError, "persistent_id"); |
| 4326 | else |
| 4327 | Py_INCREF(self->pers_func); |
| 4328 | return self->pers_func; |
| 4329 | } |
| 4330 | |
| 4331 | static int |
| 4332 | Pickler_set_persid(PicklerObject *self, PyObject *value) |
| 4333 | { |
| 4334 | PyObject *tmp; |
| 4335 | |
| 4336 | if (value == NULL) { |
| 4337 | PyErr_SetString(PyExc_TypeError, |
| 4338 | "attribute deletion is not supported"); |
| 4339 | return -1; |
| 4340 | } |
| 4341 | if (!PyCallable_Check(value)) { |
| 4342 | PyErr_SetString(PyExc_TypeError, |
| 4343 | "persistent_id must be a callable taking one argument"); |
| 4344 | return -1; |
| 4345 | } |
| 4346 | |
| 4347 | tmp = self->pers_func; |
| 4348 | Py_INCREF(value); |
| 4349 | self->pers_func = value; |
| 4350 | Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */ |
| 4351 | |
| 4352 | return 0; |
| 4353 | } |
| 4354 | |
| 4355 | static PyMemberDef Pickler_members[] = { |
| 4356 | {"bin", T_INT, offsetof(PicklerObject, bin)}, |
| 4357 | {"fast", T_INT, offsetof(PicklerObject, fast)}, |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4358 | {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)}, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4359 | {NULL} |
| 4360 | }; |
| 4361 | |
| 4362 | static PyGetSetDef Pickler_getsets[] = { |
| 4363 | {"memo", (getter)Pickler_get_memo, |
| 4364 | (setter)Pickler_set_memo}, |
| 4365 | {"persistent_id", (getter)Pickler_get_persid, |
| 4366 | (setter)Pickler_set_persid}, |
| 4367 | {NULL} |
| 4368 | }; |
| 4369 | |
| 4370 | static PyTypeObject Pickler_Type = { |
| 4371 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4372 | "_pickle.Pickler" , /*tp_name*/ |
| 4373 | sizeof(PicklerObject), /*tp_basicsize*/ |
| 4374 | 0, /*tp_itemsize*/ |
| 4375 | (destructor)Pickler_dealloc, /*tp_dealloc*/ |
| 4376 | 0, /*tp_print*/ |
| 4377 | 0, /*tp_getattr*/ |
| 4378 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 4379 | 0, /*tp_reserved*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4380 | 0, /*tp_repr*/ |
| 4381 | 0, /*tp_as_number*/ |
| 4382 | 0, /*tp_as_sequence*/ |
| 4383 | 0, /*tp_as_mapping*/ |
| 4384 | 0, /*tp_hash*/ |
| 4385 | 0, /*tp_call*/ |
| 4386 | 0, /*tp_str*/ |
| 4387 | 0, /*tp_getattro*/ |
| 4388 | 0, /*tp_setattro*/ |
| 4389 | 0, /*tp_as_buffer*/ |
| 4390 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 4391 | Pickler_doc, /*tp_doc*/ |
| 4392 | (traverseproc)Pickler_traverse, /*tp_traverse*/ |
| 4393 | (inquiry)Pickler_clear, /*tp_clear*/ |
| 4394 | 0, /*tp_richcompare*/ |
| 4395 | 0, /*tp_weaklistoffset*/ |
| 4396 | 0, /*tp_iter*/ |
| 4397 | 0, /*tp_iternext*/ |
| 4398 | Pickler_methods, /*tp_methods*/ |
| 4399 | Pickler_members, /*tp_members*/ |
| 4400 | Pickler_getsets, /*tp_getset*/ |
| 4401 | 0, /*tp_base*/ |
| 4402 | 0, /*tp_dict*/ |
| 4403 | 0, /*tp_descr_get*/ |
| 4404 | 0, /*tp_descr_set*/ |
| 4405 | 0, /*tp_dictoffset*/ |
| 4406 | (initproc)Pickler_init, /*tp_init*/ |
| 4407 | PyType_GenericAlloc, /*tp_alloc*/ |
| 4408 | PyType_GenericNew, /*tp_new*/ |
| 4409 | PyObject_GC_Del, /*tp_free*/ |
| 4410 | 0, /*tp_is_gc*/ |
| 4411 | }; |
| 4412 | |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 4413 | /* Temporary helper for calling self.find_class(). |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4414 | |
| 4415 | XXX: It would be nice to able to avoid Python function call overhead, by |
| 4416 | using directly the C version of find_class(), when find_class() is not |
| 4417 | overridden by a subclass. Although, this could become rather hackish. A |
| 4418 | simpler optimization would be to call the C function when self is not a |
| 4419 | subclass instance. */ |
| 4420 | static PyObject * |
| 4421 | find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name) |
| 4422 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4423 | _Py_IDENTIFIER(find_class); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 4424 | |
| 4425 | return _PyObject_CallMethodId((PyObject *)self, &PyId_find_class, "OO", |
| 4426 | module_name, global_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4427 | } |
| 4428 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4429 | static Py_ssize_t |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4430 | marker(UnpicklerObject *self) |
| 4431 | { |
| 4432 | if (self->num_marks < 1) { |
| 4433 | PyErr_SetString(UnpicklingError, "could not find MARK"); |
| 4434 | return -1; |
| 4435 | } |
| 4436 | |
| 4437 | return self->marks[--self->num_marks]; |
| 4438 | } |
| 4439 | |
| 4440 | static int |
| 4441 | load_none(UnpicklerObject *self) |
| 4442 | { |
| 4443 | PDATA_APPEND(self->stack, Py_None, -1); |
| 4444 | return 0; |
| 4445 | } |
| 4446 | |
| 4447 | static int |
| 4448 | bad_readline(void) |
| 4449 | { |
| 4450 | PyErr_SetString(UnpicklingError, "pickle data was truncated"); |
| 4451 | return -1; |
| 4452 | } |
| 4453 | |
| 4454 | static int |
| 4455 | load_int(UnpicklerObject *self) |
| 4456 | { |
| 4457 | PyObject *value; |
| 4458 | char *endptr, *s; |
| 4459 | Py_ssize_t len; |
| 4460 | long x; |
| 4461 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4462 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4463 | return -1; |
| 4464 | if (len < 2) |
| 4465 | return bad_readline(); |
| 4466 | |
| 4467 | errno = 0; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 4468 | /* XXX: Should the base argument of strtol() be explicitly set to 10? |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4469 | XXX(avassalotti): Should this uses PyOS_strtol()? */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4470 | x = strtol(s, &endptr, 0); |
| 4471 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4472 | if (errno || (*endptr != '\n' && *endptr != '\0')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4473 | /* Hm, maybe we've got something long. Let's try reading |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4474 | * it as a Python int object. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4475 | errno = 0; |
| 4476 | /* XXX: Same thing about the base here. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4477 | value = PyLong_FromString(s, NULL, 0); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4478 | if (value == NULL) { |
| 4479 | PyErr_SetString(PyExc_ValueError, |
| 4480 | "could not convert string to int"); |
| 4481 | return -1; |
| 4482 | } |
| 4483 | } |
| 4484 | else { |
| 4485 | if (len == 3 && (x == 0 || x == 1)) { |
| 4486 | if ((value = PyBool_FromLong(x)) == NULL) |
| 4487 | return -1; |
| 4488 | } |
| 4489 | else { |
| 4490 | if ((value = PyLong_FromLong(x)) == NULL) |
| 4491 | return -1; |
| 4492 | } |
| 4493 | } |
| 4494 | |
| 4495 | PDATA_PUSH(self->stack, value, -1); |
| 4496 | return 0; |
| 4497 | } |
| 4498 | |
| 4499 | static int |
| 4500 | load_bool(UnpicklerObject *self, PyObject *boolean) |
| 4501 | { |
| 4502 | assert(boolean == Py_True || boolean == Py_False); |
| 4503 | PDATA_APPEND(self->stack, boolean, -1); |
| 4504 | return 0; |
| 4505 | } |
| 4506 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4507 | /* s contains x bytes of an unsigned little-endian integer. Return its value |
| 4508 | * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX. |
| 4509 | */ |
| 4510 | static Py_ssize_t |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4511 | calc_binsize(char *bytes, int nbytes) |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4512 | { |
| 4513 | unsigned char *s = (unsigned char *)bytes; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4514 | int i; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4515 | size_t x = 0; |
| 4516 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4517 | for (i = 0; i < nbytes; i++) { |
| 4518 | x |= (size_t) s[i] << (8 * i); |
| 4519 | } |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4520 | |
| 4521 | if (x > PY_SSIZE_T_MAX) |
| 4522 | return -1; |
| 4523 | else |
| 4524 | return (Py_ssize_t) x; |
| 4525 | } |
| 4526 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4527 | /* s contains x bytes of a little-endian integer. Return its value as a |
| 4528 | * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian |
| 4529 | * int, but when x is 4 it's a signed one. This is an historical source |
| 4530 | * of x-platform bugs. |
| 4531 | */ |
| 4532 | static long |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4533 | calc_binint(char *bytes, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4534 | { |
| 4535 | unsigned char *s = (unsigned char *)bytes; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4536 | int i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4537 | long x = 0; |
| 4538 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4539 | for (i = 0; i < nbytes; i++) { |
| 4540 | x |= (long)s[i] << (8 * i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4541 | } |
| 4542 | |
| 4543 | /* Unlike BININT1 and BININT2, BININT (more accurately BININT4) |
| 4544 | * is signed, so on a box with longs bigger than 4 bytes we need |
| 4545 | * to extend a BININT's sign bit to the full width. |
| 4546 | */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4547 | if (SIZEOF_LONG > 4 && nbytes == 4) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4548 | x |= -(x & (1L << 31)); |
| 4549 | } |
| 4550 | |
| 4551 | return x; |
| 4552 | } |
| 4553 | |
| 4554 | static int |
| 4555 | load_binintx(UnpicklerObject *self, char *s, int size) |
| 4556 | { |
| 4557 | PyObject *value; |
| 4558 | long x; |
| 4559 | |
| 4560 | x = calc_binint(s, size); |
| 4561 | |
| 4562 | if ((value = PyLong_FromLong(x)) == NULL) |
| 4563 | return -1; |
| 4564 | |
| 4565 | PDATA_PUSH(self->stack, value, -1); |
| 4566 | return 0; |
| 4567 | } |
| 4568 | |
| 4569 | static int |
| 4570 | load_binint(UnpicklerObject *self) |
| 4571 | { |
| 4572 | char *s; |
| 4573 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4574 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4575 | return -1; |
| 4576 | |
| 4577 | return load_binintx(self, s, 4); |
| 4578 | } |
| 4579 | |
| 4580 | static int |
| 4581 | load_binint1(UnpicklerObject *self) |
| 4582 | { |
| 4583 | char *s; |
| 4584 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4585 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4586 | return -1; |
| 4587 | |
| 4588 | return load_binintx(self, s, 1); |
| 4589 | } |
| 4590 | |
| 4591 | static int |
| 4592 | load_binint2(UnpicklerObject *self) |
| 4593 | { |
| 4594 | char *s; |
| 4595 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4596 | if (_Unpickler_Read(self, &s, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4597 | return -1; |
| 4598 | |
| 4599 | return load_binintx(self, s, 2); |
| 4600 | } |
| 4601 | |
| 4602 | static int |
| 4603 | load_long(UnpicklerObject *self) |
| 4604 | { |
| 4605 | PyObject *value; |
Alexandre Vassalotti | 446f7ff | 2009-01-23 04:43:46 +0000 | [diff] [blame] | 4606 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4607 | Py_ssize_t len; |
| 4608 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4609 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4610 | return -1; |
| 4611 | if (len < 2) |
| 4612 | return bad_readline(); |
| 4613 | |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 4614 | /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove |
| 4615 | the 'L' before calling PyLong_FromString. In order to maintain |
| 4616 | compatibility with Python 3.0.0, we don't actually *require* |
| 4617 | the 'L' to be present. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4618 | if (s[len-2] == 'L') |
Alexandre Vassalotti | 446f7ff | 2009-01-23 04:43:46 +0000 | [diff] [blame] | 4619 | s[len-2] = '\0'; |
Alexandre Vassalotti | e4bccb7 | 2009-01-24 01:47:57 +0000 | [diff] [blame] | 4620 | /* XXX: Should the base argument explicitly set to 10? */ |
| 4621 | value = PyLong_FromString(s, NULL, 0); |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 4622 | if (value == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4623 | return -1; |
| 4624 | |
| 4625 | PDATA_PUSH(self->stack, value, -1); |
| 4626 | return 0; |
| 4627 | } |
| 4628 | |
| 4629 | /* 'size' bytes contain the # of bytes of little-endian 256's-complement |
| 4630 | * data following. |
| 4631 | */ |
| 4632 | static int |
| 4633 | load_counted_long(UnpicklerObject *self, int size) |
| 4634 | { |
| 4635 | PyObject *value; |
| 4636 | char *nbytes; |
| 4637 | char *pdata; |
| 4638 | |
| 4639 | assert(size == 1 || size == 4); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4640 | if (_Unpickler_Read(self, &nbytes, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4641 | return -1; |
| 4642 | |
| 4643 | size = calc_binint(nbytes, size); |
| 4644 | if (size < 0) { |
| 4645 | /* Corrupt or hostile pickle -- we never write one like this */ |
| 4646 | PyErr_SetString(UnpicklingError, |
| 4647 | "LONG pickle has negative byte count"); |
| 4648 | return -1; |
| 4649 | } |
| 4650 | |
| 4651 | if (size == 0) |
| 4652 | value = PyLong_FromLong(0L); |
| 4653 | else { |
| 4654 | /* Read the raw little-endian bytes and convert. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4655 | if (_Unpickler_Read(self, &pdata, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4656 | return -1; |
| 4657 | value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size, |
| 4658 | 1 /* little endian */ , 1 /* signed */ ); |
| 4659 | } |
| 4660 | if (value == NULL) |
| 4661 | return -1; |
| 4662 | PDATA_PUSH(self->stack, value, -1); |
| 4663 | return 0; |
| 4664 | } |
| 4665 | |
| 4666 | static int |
| 4667 | load_float(UnpicklerObject *self) |
| 4668 | { |
| 4669 | PyObject *value; |
| 4670 | char *endptr, *s; |
| 4671 | Py_ssize_t len; |
| 4672 | double d; |
| 4673 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4674 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4675 | return -1; |
| 4676 | if (len < 2) |
| 4677 | return bad_readline(); |
| 4678 | |
| 4679 | errno = 0; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4680 | d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError); |
| 4681 | if (d == -1.0 && PyErr_Occurred()) |
| 4682 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4683 | if ((endptr[0] != '\n') && (endptr[0] != '\0')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4684 | PyErr_SetString(PyExc_ValueError, "could not convert string to float"); |
| 4685 | return -1; |
| 4686 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4687 | value = PyFloat_FromDouble(d); |
| 4688 | if (value == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4689 | return -1; |
| 4690 | |
| 4691 | PDATA_PUSH(self->stack, value, -1); |
| 4692 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4693 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4694 | |
| 4695 | static int |
| 4696 | load_binfloat(UnpicklerObject *self) |
| 4697 | { |
| 4698 | PyObject *value; |
| 4699 | double x; |
| 4700 | char *s; |
| 4701 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4702 | if (_Unpickler_Read(self, &s, 8) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4703 | return -1; |
| 4704 | |
| 4705 | x = _PyFloat_Unpack8((unsigned char *)s, 0); |
| 4706 | if (x == -1.0 && PyErr_Occurred()) |
| 4707 | return -1; |
| 4708 | |
| 4709 | if ((value = PyFloat_FromDouble(x)) == NULL) |
| 4710 | return -1; |
| 4711 | |
| 4712 | PDATA_PUSH(self->stack, value, -1); |
| 4713 | return 0; |
| 4714 | } |
| 4715 | |
| 4716 | static int |
| 4717 | load_string(UnpicklerObject *self) |
| 4718 | { |
| 4719 | PyObject *bytes; |
| 4720 | PyObject *str = NULL; |
| 4721 | Py_ssize_t len; |
| 4722 | char *s, *p; |
| 4723 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4724 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4725 | return -1; |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4726 | /* Strip the newline */ |
| 4727 | len--; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4728 | /* Strip outermost quotes */ |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4729 | if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4730 | p = s + 1; |
| 4731 | len -= 2; |
| 4732 | } |
| 4733 | else { |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4734 | PyErr_SetString(UnpicklingError, |
| 4735 | "the STRING opcode argument must be quoted"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4736 | return -1; |
| 4737 | } |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4738 | assert(len >= 0); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4739 | |
| 4740 | /* Use the PyBytes API to decode the string, since that is what is used |
| 4741 | to encode, and then coerce the result to Unicode. */ |
| 4742 | bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4743 | if (bytes == NULL) |
| 4744 | return -1; |
| 4745 | str = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors); |
| 4746 | Py_DECREF(bytes); |
| 4747 | if (str == NULL) |
| 4748 | return -1; |
| 4749 | |
| 4750 | PDATA_PUSH(self->stack, str, -1); |
| 4751 | return 0; |
| 4752 | } |
| 4753 | |
| 4754 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4755 | load_counted_binbytes(UnpicklerObject *self, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4756 | { |
| 4757 | PyObject *bytes; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4758 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4759 | char *s; |
| 4760 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4761 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4762 | return -1; |
| 4763 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4764 | size = calc_binsize(s, nbytes); |
| 4765 | if (size < 0) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4766 | PyErr_Format(PyExc_OverflowError, |
| 4767 | "BINBYTES exceeds system's maximum size of %zd bytes", |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 4768 | PY_SSIZE_T_MAX); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4769 | return -1; |
| 4770 | } |
| 4771 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4772 | if (_Unpickler_Read(self, &s, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4773 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4774 | |
| 4775 | bytes = PyBytes_FromStringAndSize(s, size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4776 | if (bytes == NULL) |
| 4777 | return -1; |
| 4778 | |
| 4779 | PDATA_PUSH(self->stack, bytes, -1); |
| 4780 | return 0; |
| 4781 | } |
| 4782 | |
| 4783 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4784 | load_counted_binstring(UnpicklerObject *self, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4785 | { |
| 4786 | PyObject *str; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4787 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4788 | char *s; |
| 4789 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4790 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4791 | return -1; |
| 4792 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4793 | size = calc_binsize(s, nbytes); |
| 4794 | if (size < 0) { |
| 4795 | PyErr_Format(UnpicklingError, |
| 4796 | "BINSTRING exceeds system's maximum size of %zd bytes", |
| 4797 | PY_SSIZE_T_MAX); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4798 | return -1; |
| 4799 | } |
| 4800 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4801 | if (_Unpickler_Read(self, &s, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4802 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4803 | /* Convert Python 2.x strings to unicode. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4804 | str = PyUnicode_Decode(s, size, self->encoding, self->errors); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4805 | if (str == NULL) |
| 4806 | return -1; |
| 4807 | |
| 4808 | PDATA_PUSH(self->stack, str, -1); |
| 4809 | return 0; |
| 4810 | } |
| 4811 | |
| 4812 | static int |
| 4813 | load_unicode(UnpicklerObject *self) |
| 4814 | { |
| 4815 | PyObject *str; |
| 4816 | Py_ssize_t len; |
| 4817 | char *s; |
| 4818 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4819 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4820 | return -1; |
| 4821 | if (len < 1) |
| 4822 | return bad_readline(); |
| 4823 | |
| 4824 | str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL); |
| 4825 | if (str == NULL) |
| 4826 | return -1; |
| 4827 | |
| 4828 | PDATA_PUSH(self->stack, str, -1); |
| 4829 | return 0; |
| 4830 | } |
| 4831 | |
| 4832 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4833 | load_counted_binunicode(UnpicklerObject *self, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4834 | { |
| 4835 | PyObject *str; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4836 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4837 | char *s; |
| 4838 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4839 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4840 | return -1; |
| 4841 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4842 | size = calc_binsize(s, nbytes); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4843 | if (size < 0) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4844 | PyErr_Format(PyExc_OverflowError, |
| 4845 | "BINUNICODE exceeds system's maximum size of %zd bytes", |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 4846 | PY_SSIZE_T_MAX); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4847 | return -1; |
| 4848 | } |
| 4849 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4850 | if (_Unpickler_Read(self, &s, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4851 | return -1; |
| 4852 | |
Victor Stinner | 485fb56 | 2010-04-13 11:07:24 +0000 | [diff] [blame] | 4853 | str = PyUnicode_DecodeUTF8(s, size, "surrogatepass"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4854 | if (str == NULL) |
| 4855 | return -1; |
| 4856 | |
| 4857 | PDATA_PUSH(self->stack, str, -1); |
| 4858 | return 0; |
| 4859 | } |
| 4860 | |
| 4861 | static int |
| 4862 | load_tuple(UnpicklerObject *self) |
| 4863 | { |
| 4864 | PyObject *tuple; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4865 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4866 | |
| 4867 | if ((i = marker(self)) < 0) |
| 4868 | return -1; |
| 4869 | |
| 4870 | tuple = Pdata_poptuple(self->stack, i); |
| 4871 | if (tuple == NULL) |
| 4872 | return -1; |
| 4873 | PDATA_PUSH(self->stack, tuple, -1); |
| 4874 | return 0; |
| 4875 | } |
| 4876 | |
| 4877 | static int |
| 4878 | load_counted_tuple(UnpicklerObject *self, int len) |
| 4879 | { |
| 4880 | PyObject *tuple; |
| 4881 | |
| 4882 | tuple = PyTuple_New(len); |
| 4883 | if (tuple == NULL) |
| 4884 | return -1; |
| 4885 | |
| 4886 | while (--len >= 0) { |
| 4887 | PyObject *item; |
| 4888 | |
| 4889 | PDATA_POP(self->stack, item); |
| 4890 | if (item == NULL) |
| 4891 | return -1; |
| 4892 | PyTuple_SET_ITEM(tuple, len, item); |
| 4893 | } |
| 4894 | PDATA_PUSH(self->stack, tuple, -1); |
| 4895 | return 0; |
| 4896 | } |
| 4897 | |
| 4898 | static int |
| 4899 | load_empty_list(UnpicklerObject *self) |
| 4900 | { |
| 4901 | PyObject *list; |
| 4902 | |
| 4903 | if ((list = PyList_New(0)) == NULL) |
| 4904 | return -1; |
| 4905 | PDATA_PUSH(self->stack, list, -1); |
| 4906 | return 0; |
| 4907 | } |
| 4908 | |
| 4909 | static int |
| 4910 | load_empty_dict(UnpicklerObject *self) |
| 4911 | { |
| 4912 | PyObject *dict; |
| 4913 | |
| 4914 | if ((dict = PyDict_New()) == NULL) |
| 4915 | return -1; |
| 4916 | PDATA_PUSH(self->stack, dict, -1); |
| 4917 | return 0; |
| 4918 | } |
| 4919 | |
| 4920 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4921 | load_empty_set(UnpicklerObject *self) |
| 4922 | { |
| 4923 | PyObject *set; |
| 4924 | |
| 4925 | if ((set = PySet_New(NULL)) == NULL) |
| 4926 | return -1; |
| 4927 | PDATA_PUSH(self->stack, set, -1); |
| 4928 | return 0; |
| 4929 | } |
| 4930 | |
| 4931 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4932 | load_list(UnpicklerObject *self) |
| 4933 | { |
| 4934 | PyObject *list; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4935 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4936 | |
| 4937 | if ((i = marker(self)) < 0) |
| 4938 | return -1; |
| 4939 | |
| 4940 | list = Pdata_poplist(self->stack, i); |
| 4941 | if (list == NULL) |
| 4942 | return -1; |
| 4943 | PDATA_PUSH(self->stack, list, -1); |
| 4944 | return 0; |
| 4945 | } |
| 4946 | |
| 4947 | static int |
| 4948 | load_dict(UnpicklerObject *self) |
| 4949 | { |
| 4950 | PyObject *dict, *key, *value; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4951 | Py_ssize_t i, j, k; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4952 | |
| 4953 | if ((i = marker(self)) < 0) |
| 4954 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4955 | j = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4956 | |
| 4957 | if ((dict = PyDict_New()) == NULL) |
| 4958 | return -1; |
| 4959 | |
| 4960 | for (k = i + 1; k < j; k += 2) { |
| 4961 | key = self->stack->data[k - 1]; |
| 4962 | value = self->stack->data[k]; |
| 4963 | if (PyDict_SetItem(dict, key, value) < 0) { |
| 4964 | Py_DECREF(dict); |
| 4965 | return -1; |
| 4966 | } |
| 4967 | } |
| 4968 | Pdata_clear(self->stack, i); |
| 4969 | PDATA_PUSH(self->stack, dict, -1); |
| 4970 | return 0; |
| 4971 | } |
| 4972 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4973 | static int |
| 4974 | load_frozenset(UnpicklerObject *self) |
| 4975 | { |
| 4976 | PyObject *items; |
| 4977 | PyObject *frozenset; |
| 4978 | Py_ssize_t i; |
| 4979 | |
| 4980 | if ((i = marker(self)) < 0) |
| 4981 | return -1; |
| 4982 | |
| 4983 | items = Pdata_poptuple(self->stack, i); |
| 4984 | if (items == NULL) |
| 4985 | return -1; |
| 4986 | |
| 4987 | frozenset = PyFrozenSet_New(items); |
| 4988 | Py_DECREF(items); |
| 4989 | if (frozenset == NULL) |
| 4990 | return -1; |
| 4991 | |
| 4992 | PDATA_PUSH(self->stack, frozenset, -1); |
| 4993 | return 0; |
| 4994 | } |
| 4995 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4996 | static PyObject * |
| 4997 | instantiate(PyObject *cls, PyObject *args) |
| 4998 | { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 4999 | PyObject *result = NULL; |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5000 | _Py_IDENTIFIER(__getinitargs__); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5001 | /* Caller must assure args are a tuple. Normally, args come from |
| 5002 | Pdata_poptuple which packs objects from the top of the stack |
| 5003 | into a newly created tuple. */ |
| 5004 | assert(PyTuple_Check(args)); |
| 5005 | if (Py_SIZE(args) > 0 || !PyType_Check(cls) || |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5006 | _PyObject_HasAttrId(cls, &PyId___getinitargs__)) { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5007 | result = PyObject_CallObject(cls, args); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5008 | } |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5009 | else { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5010 | _Py_IDENTIFIER(__new__); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 5011 | |
| 5012 | result = _PyObject_CallMethodId(cls, &PyId___new__, "O", cls); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5013 | } |
| 5014 | return result; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5015 | } |
| 5016 | |
| 5017 | static int |
| 5018 | load_obj(UnpicklerObject *self) |
| 5019 | { |
| 5020 | PyObject *cls, *args, *obj = NULL; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5021 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5022 | |
| 5023 | if ((i = marker(self)) < 0) |
| 5024 | return -1; |
| 5025 | |
| 5026 | args = Pdata_poptuple(self->stack, i + 1); |
| 5027 | if (args == NULL) |
| 5028 | return -1; |
| 5029 | |
| 5030 | PDATA_POP(self->stack, cls); |
| 5031 | if (cls) { |
| 5032 | obj = instantiate(cls, args); |
| 5033 | Py_DECREF(cls); |
| 5034 | } |
| 5035 | Py_DECREF(args); |
| 5036 | if (obj == NULL) |
| 5037 | return -1; |
| 5038 | |
| 5039 | PDATA_PUSH(self->stack, obj, -1); |
| 5040 | return 0; |
| 5041 | } |
| 5042 | |
| 5043 | static int |
| 5044 | load_inst(UnpicklerObject *self) |
| 5045 | { |
| 5046 | PyObject *cls = NULL; |
| 5047 | PyObject *args = NULL; |
| 5048 | PyObject *obj = NULL; |
| 5049 | PyObject *module_name; |
| 5050 | PyObject *class_name; |
| 5051 | Py_ssize_t len; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5052 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5053 | char *s; |
| 5054 | |
| 5055 | if ((i = marker(self)) < 0) |
| 5056 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5057 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5058 | return -1; |
| 5059 | if (len < 2) |
| 5060 | return bad_readline(); |
| 5061 | |
| 5062 | /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII |
| 5063 | identifiers are permitted in Python 3.0, since the INST opcode is only |
| 5064 | supported by older protocols on Python 2.x. */ |
| 5065 | module_name = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
| 5066 | if (module_name == NULL) |
| 5067 | return -1; |
| 5068 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5069 | if ((len = _Unpickler_Readline(self, &s)) >= 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5070 | if (len < 2) |
| 5071 | return bad_readline(); |
| 5072 | class_name = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5073 | if (class_name != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5074 | cls = find_class(self, module_name, class_name); |
| 5075 | Py_DECREF(class_name); |
| 5076 | } |
| 5077 | } |
| 5078 | Py_DECREF(module_name); |
| 5079 | |
| 5080 | if (cls == NULL) |
| 5081 | return -1; |
| 5082 | |
| 5083 | if ((args = Pdata_poptuple(self->stack, i)) != NULL) { |
| 5084 | obj = instantiate(cls, args); |
| 5085 | Py_DECREF(args); |
| 5086 | } |
| 5087 | Py_DECREF(cls); |
| 5088 | |
| 5089 | if (obj == NULL) |
| 5090 | return -1; |
| 5091 | |
| 5092 | PDATA_PUSH(self->stack, obj, -1); |
| 5093 | return 0; |
| 5094 | } |
| 5095 | |
| 5096 | static int |
| 5097 | load_newobj(UnpicklerObject *self) |
| 5098 | { |
| 5099 | PyObject *args = NULL; |
| 5100 | PyObject *clsraw = NULL; |
| 5101 | PyTypeObject *cls; /* clsraw cast to its true type */ |
| 5102 | PyObject *obj; |
| 5103 | |
| 5104 | /* Stack is ... cls argtuple, and we want to call |
| 5105 | * cls.__new__(cls, *argtuple). |
| 5106 | */ |
| 5107 | PDATA_POP(self->stack, args); |
| 5108 | if (args == NULL) |
| 5109 | goto error; |
| 5110 | if (!PyTuple_Check(args)) { |
| 5111 | PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg " "tuple."); |
| 5112 | goto error; |
| 5113 | } |
| 5114 | |
| 5115 | PDATA_POP(self->stack, clsraw); |
| 5116 | cls = (PyTypeObject *)clsraw; |
| 5117 | if (cls == NULL) |
| 5118 | goto error; |
| 5119 | if (!PyType_Check(cls)) { |
| 5120 | PyErr_SetString(UnpicklingError, "NEWOBJ class argument " |
| 5121 | "isn't a type object"); |
| 5122 | goto error; |
| 5123 | } |
| 5124 | if (cls->tp_new == NULL) { |
| 5125 | PyErr_SetString(UnpicklingError, "NEWOBJ class argument " |
| 5126 | "has NULL tp_new"); |
| 5127 | goto error; |
| 5128 | } |
| 5129 | |
| 5130 | /* Call __new__. */ |
| 5131 | obj = cls->tp_new(cls, args, NULL); |
| 5132 | if (obj == NULL) |
| 5133 | goto error; |
| 5134 | |
| 5135 | Py_DECREF(args); |
| 5136 | Py_DECREF(clsraw); |
| 5137 | PDATA_PUSH(self->stack, obj, -1); |
| 5138 | return 0; |
| 5139 | |
| 5140 | error: |
| 5141 | Py_XDECREF(args); |
| 5142 | Py_XDECREF(clsraw); |
| 5143 | return -1; |
| 5144 | } |
| 5145 | |
| 5146 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5147 | load_newobj_ex(UnpicklerObject *self) |
| 5148 | { |
| 5149 | PyObject *cls, *args, *kwargs; |
| 5150 | PyObject *obj; |
| 5151 | |
| 5152 | PDATA_POP(self->stack, kwargs); |
| 5153 | if (kwargs == NULL) { |
| 5154 | return -1; |
| 5155 | } |
| 5156 | PDATA_POP(self->stack, args); |
| 5157 | if (args == NULL) { |
| 5158 | Py_DECREF(kwargs); |
| 5159 | return -1; |
| 5160 | } |
| 5161 | PDATA_POP(self->stack, cls); |
| 5162 | if (cls == NULL) { |
| 5163 | Py_DECREF(kwargs); |
| 5164 | Py_DECREF(args); |
| 5165 | return -1; |
| 5166 | } |
| 5167 | |
| 5168 | if (!PyType_Check(cls)) { |
| 5169 | Py_DECREF(kwargs); |
| 5170 | Py_DECREF(args); |
| 5171 | Py_DECREF(cls); |
| 5172 | PyErr_Format(UnpicklingError, |
| 5173 | "NEWOBJ_EX class argument must be a type, not %.200s", |
| 5174 | Py_TYPE(cls)->tp_name); |
| 5175 | return -1; |
| 5176 | } |
| 5177 | |
| 5178 | if (((PyTypeObject *)cls)->tp_new == NULL) { |
| 5179 | Py_DECREF(kwargs); |
| 5180 | Py_DECREF(args); |
| 5181 | Py_DECREF(cls); |
| 5182 | PyErr_SetString(UnpicklingError, |
| 5183 | "NEWOBJ_EX class argument doesn't have __new__"); |
| 5184 | return -1; |
| 5185 | } |
| 5186 | obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs); |
| 5187 | Py_DECREF(kwargs); |
| 5188 | Py_DECREF(args); |
| 5189 | Py_DECREF(cls); |
| 5190 | if (obj == NULL) { |
| 5191 | return -1; |
| 5192 | } |
| 5193 | PDATA_PUSH(self->stack, obj, -1); |
| 5194 | return 0; |
| 5195 | } |
| 5196 | |
| 5197 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5198 | load_global(UnpicklerObject *self) |
| 5199 | { |
| 5200 | PyObject *global = NULL; |
| 5201 | PyObject *module_name; |
| 5202 | PyObject *global_name; |
| 5203 | Py_ssize_t len; |
| 5204 | char *s; |
| 5205 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5206 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5207 | return -1; |
| 5208 | if (len < 2) |
| 5209 | return bad_readline(); |
| 5210 | module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict"); |
| 5211 | if (!module_name) |
| 5212 | return -1; |
| 5213 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5214 | if ((len = _Unpickler_Readline(self, &s)) >= 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5215 | if (len < 2) { |
| 5216 | Py_DECREF(module_name); |
| 5217 | return bad_readline(); |
| 5218 | } |
| 5219 | global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict"); |
| 5220 | if (global_name) { |
| 5221 | global = find_class(self, module_name, global_name); |
| 5222 | Py_DECREF(global_name); |
| 5223 | } |
| 5224 | } |
| 5225 | Py_DECREF(module_name); |
| 5226 | |
| 5227 | if (global == NULL) |
| 5228 | return -1; |
| 5229 | PDATA_PUSH(self->stack, global, -1); |
| 5230 | return 0; |
| 5231 | } |
| 5232 | |
| 5233 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5234 | load_stack_global(UnpicklerObject *self) |
| 5235 | { |
| 5236 | PyObject *global; |
| 5237 | PyObject *module_name; |
| 5238 | PyObject *global_name; |
| 5239 | |
| 5240 | PDATA_POP(self->stack, global_name); |
| 5241 | PDATA_POP(self->stack, module_name); |
| 5242 | if (module_name == NULL || !PyUnicode_CheckExact(module_name) || |
| 5243 | global_name == NULL || !PyUnicode_CheckExact(global_name)) { |
| 5244 | PyErr_SetString(UnpicklingError, "STACK_GLOBAL requires str"); |
| 5245 | Py_XDECREF(global_name); |
| 5246 | Py_XDECREF(module_name); |
| 5247 | return -1; |
| 5248 | } |
| 5249 | global = find_class(self, module_name, global_name); |
| 5250 | Py_DECREF(global_name); |
| 5251 | Py_DECREF(module_name); |
| 5252 | if (global == NULL) |
| 5253 | return -1; |
| 5254 | PDATA_PUSH(self->stack, global, -1); |
| 5255 | return 0; |
| 5256 | } |
| 5257 | |
| 5258 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5259 | load_persid(UnpicklerObject *self) |
| 5260 | { |
| 5261 | PyObject *pid; |
| 5262 | Py_ssize_t len; |
| 5263 | char *s; |
| 5264 | |
| 5265 | if (self->pers_func) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5266 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5267 | return -1; |
| 5268 | if (len < 2) |
| 5269 | return bad_readline(); |
| 5270 | |
| 5271 | pid = PyBytes_FromStringAndSize(s, len - 1); |
| 5272 | if (pid == NULL) |
| 5273 | return -1; |
| 5274 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5275 | /* Ugh... this does not leak since _Unpickler_FastCall() steals the |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5276 | reference to pid first. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5277 | pid = _Unpickler_FastCall(self, self->pers_func, pid); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5278 | if (pid == NULL) |
| 5279 | return -1; |
| 5280 | |
| 5281 | PDATA_PUSH(self->stack, pid, -1); |
| 5282 | return 0; |
| 5283 | } |
| 5284 | else { |
| 5285 | PyErr_SetString(UnpicklingError, |
| 5286 | "A load persistent id instruction was encountered,\n" |
| 5287 | "but no persistent_load function was specified."); |
| 5288 | return -1; |
| 5289 | } |
| 5290 | } |
| 5291 | |
| 5292 | static int |
| 5293 | load_binpersid(UnpicklerObject *self) |
| 5294 | { |
| 5295 | PyObject *pid; |
| 5296 | |
| 5297 | if (self->pers_func) { |
| 5298 | PDATA_POP(self->stack, pid); |
| 5299 | if (pid == NULL) |
| 5300 | return -1; |
| 5301 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5302 | /* Ugh... this does not leak since _Unpickler_FastCall() steals the |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5303 | reference to pid first. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5304 | pid = _Unpickler_FastCall(self, self->pers_func, pid); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5305 | if (pid == NULL) |
| 5306 | return -1; |
| 5307 | |
| 5308 | PDATA_PUSH(self->stack, pid, -1); |
| 5309 | return 0; |
| 5310 | } |
| 5311 | else { |
| 5312 | PyErr_SetString(UnpicklingError, |
| 5313 | "A load persistent id instruction was encountered,\n" |
| 5314 | "but no persistent_load function was specified."); |
| 5315 | return -1; |
| 5316 | } |
| 5317 | } |
| 5318 | |
| 5319 | static int |
| 5320 | load_pop(UnpicklerObject *self) |
| 5321 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5322 | Py_ssize_t len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5323 | |
| 5324 | /* Note that we split the (pickle.py) stack into two stacks, |
| 5325 | * an object stack and a mark stack. We have to be clever and |
| 5326 | * 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] | 5327 | * mark stack first, and only signalling a stack underflow if |
| 5328 | * the object stack is empty and the mark stack doesn't match |
| 5329 | * our expectations. |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5330 | */ |
Collin Winter | 8ca69de | 2009-05-26 16:53:41 +0000 | [diff] [blame] | 5331 | if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5332 | self->num_marks--; |
Antoine Pitrou | 01a15ea | 2010-01-07 17:57:31 +0000 | [diff] [blame] | 5333 | } else if (len > 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5334 | len--; |
| 5335 | Py_DECREF(self->stack->data[len]); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5336 | Py_SIZE(self->stack) = len; |
Collin Winter | 8ca69de | 2009-05-26 16:53:41 +0000 | [diff] [blame] | 5337 | } else { |
| 5338 | return stack_underflow(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5339 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5340 | return 0; |
| 5341 | } |
| 5342 | |
| 5343 | static int |
| 5344 | load_pop_mark(UnpicklerObject *self) |
| 5345 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5346 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5347 | |
| 5348 | if ((i = marker(self)) < 0) |
| 5349 | return -1; |
| 5350 | |
| 5351 | Pdata_clear(self->stack, i); |
| 5352 | |
| 5353 | return 0; |
| 5354 | } |
| 5355 | |
| 5356 | static int |
| 5357 | load_dup(UnpicklerObject *self) |
| 5358 | { |
| 5359 | PyObject *last; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5360 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5361 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5362 | if ((len = Py_SIZE(self->stack)) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5363 | return stack_underflow(); |
| 5364 | last = self->stack->data[len - 1]; |
| 5365 | PDATA_APPEND(self->stack, last, -1); |
| 5366 | return 0; |
| 5367 | } |
| 5368 | |
| 5369 | static int |
| 5370 | load_get(UnpicklerObject *self) |
| 5371 | { |
| 5372 | PyObject *key, *value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5373 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5374 | Py_ssize_t len; |
| 5375 | char *s; |
| 5376 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5377 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5378 | return -1; |
| 5379 | if (len < 2) |
| 5380 | return bad_readline(); |
| 5381 | |
| 5382 | key = PyLong_FromString(s, NULL, 10); |
| 5383 | if (key == NULL) |
| 5384 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5385 | idx = PyLong_AsSsize_t(key); |
| 5386 | if (idx == -1 && PyErr_Occurred()) { |
| 5387 | Py_DECREF(key); |
| 5388 | return -1; |
| 5389 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5390 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5391 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5392 | if (value == NULL) { |
| 5393 | if (!PyErr_Occurred()) |
| 5394 | PyErr_SetObject(PyExc_KeyError, key); |
| 5395 | Py_DECREF(key); |
| 5396 | return -1; |
| 5397 | } |
| 5398 | Py_DECREF(key); |
| 5399 | |
| 5400 | PDATA_APPEND(self->stack, value, -1); |
| 5401 | return 0; |
| 5402 | } |
| 5403 | |
| 5404 | static int |
| 5405 | load_binget(UnpicklerObject *self) |
| 5406 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5407 | PyObject *value; |
| 5408 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5409 | char *s; |
| 5410 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5411 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5412 | return -1; |
| 5413 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5414 | idx = Py_CHARMASK(s[0]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5415 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5416 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5417 | if (value == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5418 | PyObject *key = PyLong_FromSsize_t(idx); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5419 | if (key != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5420 | PyErr_SetObject(PyExc_KeyError, key); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5421 | Py_DECREF(key); |
| 5422 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5423 | return -1; |
| 5424 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5425 | |
| 5426 | PDATA_APPEND(self->stack, value, -1); |
| 5427 | return 0; |
| 5428 | } |
| 5429 | |
| 5430 | static int |
| 5431 | load_long_binget(UnpicklerObject *self) |
| 5432 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5433 | PyObject *value; |
| 5434 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5435 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5436 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5437 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5438 | return -1; |
| 5439 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5440 | idx = calc_binsize(s, 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5441 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5442 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5443 | if (value == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5444 | PyObject *key = PyLong_FromSsize_t(idx); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5445 | if (key != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5446 | PyErr_SetObject(PyExc_KeyError, key); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5447 | Py_DECREF(key); |
| 5448 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5449 | return -1; |
| 5450 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5451 | |
| 5452 | PDATA_APPEND(self->stack, value, -1); |
| 5453 | return 0; |
| 5454 | } |
| 5455 | |
| 5456 | /* Push an object from the extension registry (EXT[124]). nbytes is |
| 5457 | * the number of bytes following the opcode, holding the index (code) value. |
| 5458 | */ |
| 5459 | static int |
| 5460 | load_extension(UnpicklerObject *self, int nbytes) |
| 5461 | { |
| 5462 | char *codebytes; /* the nbytes bytes after the opcode */ |
| 5463 | long code; /* calc_binint returns long */ |
| 5464 | PyObject *py_code; /* code as a Python int */ |
| 5465 | PyObject *obj; /* the object to push */ |
| 5466 | PyObject *pair; /* (module_name, class_name) */ |
| 5467 | PyObject *module_name, *class_name; |
| 5468 | |
| 5469 | assert(nbytes == 1 || nbytes == 2 || nbytes == 4); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5470 | if (_Unpickler_Read(self, &codebytes, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5471 | return -1; |
| 5472 | code = calc_binint(codebytes, nbytes); |
| 5473 | if (code <= 0) { /* note that 0 is forbidden */ |
| 5474 | /* Corrupt or hostile pickle. */ |
| 5475 | PyErr_SetString(UnpicklingError, "EXT specifies code <= 0"); |
| 5476 | return -1; |
| 5477 | } |
| 5478 | |
| 5479 | /* Look for the code in the cache. */ |
| 5480 | py_code = PyLong_FromLong(code); |
| 5481 | if (py_code == NULL) |
| 5482 | return -1; |
| 5483 | obj = PyDict_GetItem(extension_cache, py_code); |
| 5484 | if (obj != NULL) { |
| 5485 | /* Bingo. */ |
| 5486 | Py_DECREF(py_code); |
| 5487 | PDATA_APPEND(self->stack, obj, -1); |
| 5488 | return 0; |
| 5489 | } |
| 5490 | |
| 5491 | /* Look up the (module_name, class_name) pair. */ |
| 5492 | pair = PyDict_GetItem(inverted_registry, py_code); |
| 5493 | if (pair == NULL) { |
| 5494 | Py_DECREF(py_code); |
| 5495 | PyErr_Format(PyExc_ValueError, "unregistered extension " |
| 5496 | "code %ld", code); |
| 5497 | return -1; |
| 5498 | } |
| 5499 | /* Since the extension registry is manipulable via Python code, |
| 5500 | * confirm that pair is really a 2-tuple of strings. |
| 5501 | */ |
| 5502 | if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 || |
| 5503 | !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) || |
| 5504 | !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) { |
| 5505 | Py_DECREF(py_code); |
| 5506 | PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] " |
| 5507 | "isn't a 2-tuple of strings", code); |
| 5508 | return -1; |
| 5509 | } |
| 5510 | /* Load the object. */ |
| 5511 | obj = find_class(self, module_name, class_name); |
| 5512 | if (obj == NULL) { |
| 5513 | Py_DECREF(py_code); |
| 5514 | return -1; |
| 5515 | } |
| 5516 | /* Cache code -> obj. */ |
| 5517 | code = PyDict_SetItem(extension_cache, py_code, obj); |
| 5518 | Py_DECREF(py_code); |
| 5519 | if (code < 0) { |
| 5520 | Py_DECREF(obj); |
| 5521 | return -1; |
| 5522 | } |
| 5523 | PDATA_PUSH(self->stack, obj, -1); |
| 5524 | return 0; |
| 5525 | } |
| 5526 | |
| 5527 | static int |
| 5528 | load_put(UnpicklerObject *self) |
| 5529 | { |
| 5530 | PyObject *key, *value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5531 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5532 | Py_ssize_t len; |
| 5533 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5534 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5535 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5536 | return -1; |
| 5537 | if (len < 2) |
| 5538 | return bad_readline(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5539 | if (Py_SIZE(self->stack) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5540 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5541 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5542 | |
| 5543 | key = PyLong_FromString(s, NULL, 10); |
| 5544 | if (key == NULL) |
| 5545 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5546 | idx = PyLong_AsSsize_t(key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5547 | Py_DECREF(key); |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5548 | if (idx < 0) { |
| 5549 | if (!PyErr_Occurred()) |
| 5550 | PyErr_SetString(PyExc_ValueError, |
| 5551 | "negative PUT argument"); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5552 | return -1; |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5553 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5554 | |
| 5555 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5556 | } |
| 5557 | |
| 5558 | static int |
| 5559 | load_binput(UnpicklerObject *self) |
| 5560 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5561 | PyObject *value; |
| 5562 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5563 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5564 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5565 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5566 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5567 | |
| 5568 | if (Py_SIZE(self->stack) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5569 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5570 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5571 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5572 | idx = Py_CHARMASK(s[0]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5573 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5574 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5575 | } |
| 5576 | |
| 5577 | static int |
| 5578 | load_long_binput(UnpicklerObject *self) |
| 5579 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5580 | PyObject *value; |
| 5581 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5582 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5583 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5584 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5585 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5586 | |
| 5587 | if (Py_SIZE(self->stack) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5588 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5589 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5590 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5591 | idx = calc_binsize(s, 4); |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5592 | if (idx < 0) { |
| 5593 | PyErr_SetString(PyExc_ValueError, |
| 5594 | "negative LONG_BINPUT argument"); |
| 5595 | return -1; |
| 5596 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5597 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5598 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5599 | } |
| 5600 | |
| 5601 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5602 | load_memoize(UnpicklerObject *self) |
| 5603 | { |
| 5604 | PyObject *value; |
| 5605 | |
| 5606 | if (Py_SIZE(self->stack) <= 0) |
| 5607 | return stack_underflow(); |
| 5608 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
| 5609 | |
| 5610 | return _Unpickler_MemoPut(self, self->memo_len, value); |
| 5611 | } |
| 5612 | |
| 5613 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5614 | do_append(UnpicklerObject *self, Py_ssize_t x) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5615 | { |
| 5616 | PyObject *value; |
| 5617 | PyObject *list; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5618 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5619 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5620 | len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5621 | if (x > len || x <= 0) |
| 5622 | return stack_underflow(); |
| 5623 | if (len == x) /* nothing to do */ |
| 5624 | return 0; |
| 5625 | |
| 5626 | list = self->stack->data[x - 1]; |
| 5627 | |
| 5628 | if (PyList_Check(list)) { |
| 5629 | PyObject *slice; |
| 5630 | Py_ssize_t list_len; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5631 | int ret; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5632 | |
| 5633 | slice = Pdata_poplist(self->stack, x); |
| 5634 | if (!slice) |
| 5635 | return -1; |
| 5636 | list_len = PyList_GET_SIZE(list); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5637 | ret = PyList_SetSlice(list, list_len, list_len, slice); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5638 | Py_DECREF(slice); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5639 | return ret; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5640 | } |
| 5641 | else { |
| 5642 | PyObject *append_func; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5643 | _Py_IDENTIFIER(append); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5644 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 5645 | append_func = _PyObject_GetAttrId(list, &PyId_append); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5646 | if (append_func == NULL) |
| 5647 | return -1; |
| 5648 | for (i = x; i < len; i++) { |
| 5649 | PyObject *result; |
| 5650 | |
| 5651 | value = self->stack->data[i]; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5652 | result = _Unpickler_FastCall(self, append_func, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5653 | if (result == NULL) { |
| 5654 | Pdata_clear(self->stack, i + 1); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5655 | Py_SIZE(self->stack) = x; |
Alexandre Vassalotti | 637c7c4 | 2013-04-20 21:28:21 -0700 | [diff] [blame] | 5656 | Py_DECREF(append_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5657 | return -1; |
| 5658 | } |
| 5659 | Py_DECREF(result); |
| 5660 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5661 | Py_SIZE(self->stack) = x; |
Alexandre Vassalotti | 637c7c4 | 2013-04-20 21:28:21 -0700 | [diff] [blame] | 5662 | Py_DECREF(append_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5663 | } |
| 5664 | |
| 5665 | return 0; |
| 5666 | } |
| 5667 | |
| 5668 | static int |
| 5669 | load_append(UnpicklerObject *self) |
| 5670 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5671 | return do_append(self, Py_SIZE(self->stack) - 1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5672 | } |
| 5673 | |
| 5674 | static int |
| 5675 | load_appends(UnpicklerObject *self) |
| 5676 | { |
| 5677 | return do_append(self, marker(self)); |
| 5678 | } |
| 5679 | |
| 5680 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5681 | do_setitems(UnpicklerObject *self, Py_ssize_t x) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5682 | { |
| 5683 | PyObject *value, *key; |
| 5684 | PyObject *dict; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5685 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5686 | int status = 0; |
| 5687 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5688 | len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5689 | if (x > len || x <= 0) |
| 5690 | return stack_underflow(); |
| 5691 | if (len == x) /* nothing to do */ |
| 5692 | return 0; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 5693 | if ((len - x) % 2 != 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5694 | /* Currupt or hostile pickle -- we never write one like this. */ |
| 5695 | PyErr_SetString(UnpicklingError, "odd number of items for SETITEMS"); |
| 5696 | return -1; |
| 5697 | } |
| 5698 | |
| 5699 | /* Here, dict does not actually need to be a PyDict; it could be anything |
| 5700 | that supports the __setitem__ attribute. */ |
| 5701 | dict = self->stack->data[x - 1]; |
| 5702 | |
| 5703 | for (i = x + 1; i < len; i += 2) { |
| 5704 | key = self->stack->data[i - 1]; |
| 5705 | value = self->stack->data[i]; |
| 5706 | if (PyObject_SetItem(dict, key, value) < 0) { |
| 5707 | status = -1; |
| 5708 | break; |
| 5709 | } |
| 5710 | } |
| 5711 | |
| 5712 | Pdata_clear(self->stack, x); |
| 5713 | return status; |
| 5714 | } |
| 5715 | |
| 5716 | static int |
| 5717 | load_setitem(UnpicklerObject *self) |
| 5718 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5719 | return do_setitems(self, Py_SIZE(self->stack) - 2); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5720 | } |
| 5721 | |
| 5722 | static int |
| 5723 | load_setitems(UnpicklerObject *self) |
| 5724 | { |
| 5725 | return do_setitems(self, marker(self)); |
| 5726 | } |
| 5727 | |
| 5728 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5729 | load_additems(UnpicklerObject *self) |
| 5730 | { |
| 5731 | PyObject *set; |
| 5732 | Py_ssize_t mark, len, i; |
| 5733 | |
| 5734 | mark = marker(self); |
| 5735 | len = Py_SIZE(self->stack); |
| 5736 | if (mark > len || mark <= 0) |
| 5737 | return stack_underflow(); |
| 5738 | if (len == mark) /* nothing to do */ |
| 5739 | return 0; |
| 5740 | |
| 5741 | set = self->stack->data[mark - 1]; |
| 5742 | |
| 5743 | if (PySet_Check(set)) { |
| 5744 | PyObject *items; |
| 5745 | int status; |
| 5746 | |
| 5747 | items = Pdata_poptuple(self->stack, mark); |
| 5748 | if (items == NULL) |
| 5749 | return -1; |
| 5750 | |
| 5751 | status = _PySet_Update(set, items); |
| 5752 | Py_DECREF(items); |
| 5753 | return status; |
| 5754 | } |
| 5755 | else { |
| 5756 | PyObject *add_func; |
| 5757 | _Py_IDENTIFIER(add); |
| 5758 | |
| 5759 | add_func = _PyObject_GetAttrId(set, &PyId_add); |
| 5760 | if (add_func == NULL) |
| 5761 | return -1; |
| 5762 | for (i = mark; i < len; i++) { |
| 5763 | PyObject *result; |
| 5764 | PyObject *item; |
| 5765 | |
| 5766 | item = self->stack->data[i]; |
| 5767 | result = _Unpickler_FastCall(self, add_func, item); |
| 5768 | if (result == NULL) { |
| 5769 | Pdata_clear(self->stack, i + 1); |
| 5770 | Py_SIZE(self->stack) = mark; |
| 5771 | return -1; |
| 5772 | } |
| 5773 | Py_DECREF(result); |
| 5774 | } |
| 5775 | Py_SIZE(self->stack) = mark; |
| 5776 | } |
| 5777 | |
| 5778 | return 0; |
| 5779 | } |
| 5780 | |
| 5781 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5782 | load_build(UnpicklerObject *self) |
| 5783 | { |
| 5784 | PyObject *state, *inst, *slotstate; |
| 5785 | PyObject *setstate; |
| 5786 | int status = 0; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5787 | _Py_IDENTIFIER(__setstate__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5788 | |
| 5789 | /* Stack is ... instance, state. We want to leave instance at |
| 5790 | * the stack top, possibly mutated via instance.__setstate__(state). |
| 5791 | */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5792 | if (Py_SIZE(self->stack) < 2) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5793 | return stack_underflow(); |
| 5794 | |
| 5795 | PDATA_POP(self->stack, state); |
| 5796 | if (state == NULL) |
| 5797 | return -1; |
| 5798 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5799 | inst = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5800 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 5801 | setstate = _PyObject_GetAttrId(inst, &PyId___setstate__); |
Alexandre Vassalotti | 1f9d907 | 2008-08-15 03:07:47 +0000 | [diff] [blame] | 5802 | if (setstate == NULL) { |
| 5803 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 5804 | PyErr_Clear(); |
Antoine Pitrou | d79dc62 | 2008-09-05 00:03:33 +0000 | [diff] [blame] | 5805 | else { |
| 5806 | Py_DECREF(state); |
Alexandre Vassalotti | 1f9d907 | 2008-08-15 03:07:47 +0000 | [diff] [blame] | 5807 | return -1; |
Antoine Pitrou | d79dc62 | 2008-09-05 00:03:33 +0000 | [diff] [blame] | 5808 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5809 | } |
| 5810 | else { |
| 5811 | PyObject *result; |
| 5812 | |
| 5813 | /* The explicit __setstate__ is responsible for everything. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5814 | /* Ugh... this does not leak since _Unpickler_FastCall() steals the |
Antoine Pitrou | d79dc62 | 2008-09-05 00:03:33 +0000 | [diff] [blame] | 5815 | reference to state first. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5816 | result = _Unpickler_FastCall(self, setstate, state); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5817 | Py_DECREF(setstate); |
| 5818 | if (result == NULL) |
| 5819 | return -1; |
| 5820 | Py_DECREF(result); |
| 5821 | return 0; |
| 5822 | } |
| 5823 | |
| 5824 | /* A default __setstate__. First see whether state embeds a |
| 5825 | * slot state dict too (a proto 2 addition). |
| 5826 | */ |
| 5827 | if (PyTuple_Check(state) && Py_SIZE(state) == 2) { |
| 5828 | PyObject *tmp = state; |
| 5829 | |
| 5830 | state = PyTuple_GET_ITEM(tmp, 0); |
| 5831 | slotstate = PyTuple_GET_ITEM(tmp, 1); |
| 5832 | Py_INCREF(state); |
| 5833 | Py_INCREF(slotstate); |
| 5834 | Py_DECREF(tmp); |
| 5835 | } |
| 5836 | else |
| 5837 | slotstate = NULL; |
| 5838 | |
| 5839 | /* Set inst.__dict__ from the state dict (if any). */ |
| 5840 | if (state != Py_None) { |
| 5841 | PyObject *dict; |
Antoine Pitrou | a9f48a0 | 2009-05-02 21:41:14 +0000 | [diff] [blame] | 5842 | PyObject *d_key, *d_value; |
| 5843 | Py_ssize_t i; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5844 | _Py_IDENTIFIER(__dict__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5845 | |
| 5846 | if (!PyDict_Check(state)) { |
| 5847 | PyErr_SetString(UnpicklingError, "state is not a dictionary"); |
| 5848 | goto error; |
| 5849 | } |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 5850 | dict = _PyObject_GetAttrId(inst, &PyId___dict__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5851 | if (dict == NULL) |
| 5852 | goto error; |
| 5853 | |
Antoine Pitrou | a9f48a0 | 2009-05-02 21:41:14 +0000 | [diff] [blame] | 5854 | i = 0; |
| 5855 | while (PyDict_Next(state, &i, &d_key, &d_value)) { |
| 5856 | /* normally the keys for instance attributes are |
| 5857 | interned. we should try to do that here. */ |
| 5858 | Py_INCREF(d_key); |
| 5859 | if (PyUnicode_CheckExact(d_key)) |
| 5860 | PyUnicode_InternInPlace(&d_key); |
| 5861 | if (PyObject_SetItem(dict, d_key, d_value) < 0) { |
| 5862 | Py_DECREF(d_key); |
| 5863 | goto error; |
| 5864 | } |
| 5865 | Py_DECREF(d_key); |
| 5866 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5867 | Py_DECREF(dict); |
| 5868 | } |
| 5869 | |
| 5870 | /* Also set instance attributes from the slotstate dict (if any). */ |
| 5871 | if (slotstate != NULL) { |
| 5872 | PyObject *d_key, *d_value; |
| 5873 | Py_ssize_t i; |
| 5874 | |
| 5875 | if (!PyDict_Check(slotstate)) { |
| 5876 | PyErr_SetString(UnpicklingError, |
| 5877 | "slot state is not a dictionary"); |
| 5878 | goto error; |
| 5879 | } |
| 5880 | i = 0; |
| 5881 | while (PyDict_Next(slotstate, &i, &d_key, &d_value)) { |
| 5882 | if (PyObject_SetAttr(inst, d_key, d_value) < 0) |
| 5883 | goto error; |
| 5884 | } |
| 5885 | } |
| 5886 | |
| 5887 | if (0) { |
| 5888 | error: |
| 5889 | status = -1; |
| 5890 | } |
| 5891 | |
| 5892 | Py_DECREF(state); |
| 5893 | Py_XDECREF(slotstate); |
| 5894 | return status; |
| 5895 | } |
| 5896 | |
| 5897 | static int |
| 5898 | load_mark(UnpicklerObject *self) |
| 5899 | { |
| 5900 | |
| 5901 | /* Note that we split the (pickle.py) stack into two stacks, an |
| 5902 | * object stack and a mark stack. Here we push a mark onto the |
| 5903 | * mark stack. |
| 5904 | */ |
| 5905 | |
| 5906 | if ((self->num_marks + 1) >= self->marks_size) { |
| 5907 | size_t alloc; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5908 | Py_ssize_t *marks; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5909 | |
| 5910 | /* Use the size_t type to check for overflow. */ |
| 5911 | alloc = ((size_t)self->num_marks << 1) + 20; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5912 | if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) || |
Alexandre Vassalotti | 7634ff5 | 2008-06-13 02:16:06 +0000 | [diff] [blame] | 5913 | alloc <= ((size_t)self->num_marks + 1)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5914 | PyErr_NoMemory(); |
| 5915 | return -1; |
| 5916 | } |
| 5917 | |
| 5918 | if (self->marks == NULL) |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5919 | marks = (Py_ssize_t *) PyMem_Malloc(alloc * sizeof(Py_ssize_t)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5920 | else |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5921 | marks = (Py_ssize_t *) PyMem_Realloc(self->marks, |
| 5922 | alloc * sizeof(Py_ssize_t)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5923 | if (marks == NULL) { |
| 5924 | PyErr_NoMemory(); |
| 5925 | return -1; |
| 5926 | } |
| 5927 | self->marks = marks; |
| 5928 | self->marks_size = (Py_ssize_t)alloc; |
| 5929 | } |
| 5930 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5931 | self->marks[self->num_marks++] = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5932 | |
| 5933 | return 0; |
| 5934 | } |
| 5935 | |
| 5936 | static int |
| 5937 | load_reduce(UnpicklerObject *self) |
| 5938 | { |
| 5939 | PyObject *callable = NULL; |
| 5940 | PyObject *argtup = NULL; |
| 5941 | PyObject *obj = NULL; |
| 5942 | |
| 5943 | PDATA_POP(self->stack, argtup); |
| 5944 | if (argtup == NULL) |
| 5945 | return -1; |
| 5946 | PDATA_POP(self->stack, callable); |
| 5947 | if (callable) { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5948 | obj = PyObject_CallObject(callable, argtup); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5949 | Py_DECREF(callable); |
| 5950 | } |
| 5951 | Py_DECREF(argtup); |
| 5952 | |
| 5953 | if (obj == NULL) |
| 5954 | return -1; |
| 5955 | |
| 5956 | PDATA_PUSH(self->stack, obj, -1); |
| 5957 | return 0; |
| 5958 | } |
| 5959 | |
| 5960 | /* Just raises an error if we don't know the protocol specified. PROTO |
| 5961 | * is the first opcode for protocols >= 2. |
| 5962 | */ |
| 5963 | static int |
| 5964 | load_proto(UnpicklerObject *self) |
| 5965 | { |
| 5966 | char *s; |
| 5967 | int i; |
| 5968 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5969 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5970 | return -1; |
| 5971 | |
| 5972 | i = (unsigned char)s[0]; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 5973 | if (i <= HIGHEST_PROTOCOL) { |
| 5974 | self->proto = i; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5975 | self->framing = (self->proto >= 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5976 | return 0; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 5977 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5978 | |
| 5979 | PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i); |
| 5980 | return -1; |
| 5981 | } |
| 5982 | |
| 5983 | static PyObject * |
| 5984 | load(UnpicklerObject *self) |
| 5985 | { |
| 5986 | PyObject *err; |
| 5987 | PyObject *value = NULL; |
| 5988 | char *s; |
| 5989 | |
| 5990 | self->num_marks = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5991 | self->proto = 0; |
| 5992 | self->framing = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5993 | if (Py_SIZE(self->stack)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5994 | Pdata_clear(self->stack, 0); |
| 5995 | |
| 5996 | /* Convenient macros for the dispatch while-switch loop just below. */ |
| 5997 | #define OP(opcode, load_func) \ |
| 5998 | case opcode: if (load_func(self) < 0) break; continue; |
| 5999 | |
| 6000 | #define OP_ARG(opcode, load_func, arg) \ |
| 6001 | case opcode: if (load_func(self, (arg)) < 0) break; continue; |
| 6002 | |
| 6003 | while (1) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6004 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6005 | break; |
| 6006 | |
| 6007 | switch ((enum opcode)s[0]) { |
| 6008 | OP(NONE, load_none) |
| 6009 | OP(BININT, load_binint) |
| 6010 | OP(BININT1, load_binint1) |
| 6011 | OP(BININT2, load_binint2) |
| 6012 | OP(INT, load_int) |
| 6013 | OP(LONG, load_long) |
| 6014 | OP_ARG(LONG1, load_counted_long, 1) |
| 6015 | OP_ARG(LONG4, load_counted_long, 4) |
| 6016 | OP(FLOAT, load_float) |
| 6017 | OP(BINFLOAT, load_binfloat) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6018 | OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1) |
| 6019 | OP_ARG(BINBYTES, load_counted_binbytes, 4) |
| 6020 | OP_ARG(BINBYTES8, load_counted_binbytes, 8) |
| 6021 | OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1) |
| 6022 | OP_ARG(BINSTRING, load_counted_binstring, 4) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6023 | OP(STRING, load_string) |
| 6024 | OP(UNICODE, load_unicode) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6025 | OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1) |
| 6026 | OP_ARG(BINUNICODE, load_counted_binunicode, 4) |
| 6027 | OP_ARG(BINUNICODE8, load_counted_binunicode, 8) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6028 | OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0) |
| 6029 | OP_ARG(TUPLE1, load_counted_tuple, 1) |
| 6030 | OP_ARG(TUPLE2, load_counted_tuple, 2) |
| 6031 | OP_ARG(TUPLE3, load_counted_tuple, 3) |
| 6032 | OP(TUPLE, load_tuple) |
| 6033 | OP(EMPTY_LIST, load_empty_list) |
| 6034 | OP(LIST, load_list) |
| 6035 | OP(EMPTY_DICT, load_empty_dict) |
| 6036 | OP(DICT, load_dict) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6037 | OP(EMPTY_SET, load_empty_set) |
| 6038 | OP(ADDITEMS, load_additems) |
| 6039 | OP(FROZENSET, load_frozenset) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6040 | OP(OBJ, load_obj) |
| 6041 | OP(INST, load_inst) |
| 6042 | OP(NEWOBJ, load_newobj) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6043 | OP(NEWOBJ_EX, load_newobj_ex) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6044 | OP(GLOBAL, load_global) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6045 | OP(STACK_GLOBAL, load_stack_global) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6046 | OP(APPEND, load_append) |
| 6047 | OP(APPENDS, load_appends) |
| 6048 | OP(BUILD, load_build) |
| 6049 | OP(DUP, load_dup) |
| 6050 | OP(BINGET, load_binget) |
| 6051 | OP(LONG_BINGET, load_long_binget) |
| 6052 | OP(GET, load_get) |
| 6053 | OP(MARK, load_mark) |
| 6054 | OP(BINPUT, load_binput) |
| 6055 | OP(LONG_BINPUT, load_long_binput) |
| 6056 | OP(PUT, load_put) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6057 | OP(MEMOIZE, load_memoize) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6058 | OP(POP, load_pop) |
| 6059 | OP(POP_MARK, load_pop_mark) |
| 6060 | OP(SETITEM, load_setitem) |
| 6061 | OP(SETITEMS, load_setitems) |
| 6062 | OP(PERSID, load_persid) |
| 6063 | OP(BINPERSID, load_binpersid) |
| 6064 | OP(REDUCE, load_reduce) |
| 6065 | OP(PROTO, load_proto) |
| 6066 | OP_ARG(EXT1, load_extension, 1) |
| 6067 | OP_ARG(EXT2, load_extension, 2) |
| 6068 | OP_ARG(EXT4, load_extension, 4) |
| 6069 | OP_ARG(NEWTRUE, load_bool, Py_True) |
| 6070 | OP_ARG(NEWFALSE, load_bool, Py_False) |
| 6071 | |
| 6072 | case STOP: |
| 6073 | break; |
| 6074 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6075 | default: |
Benjamin Peterson | adde86d | 2011-09-23 13:41:41 -0400 | [diff] [blame] | 6076 | if (s[0] == '\0') |
| 6077 | PyErr_SetNone(PyExc_EOFError); |
| 6078 | else |
| 6079 | PyErr_Format(UnpicklingError, |
| 6080 | "invalid load key, '%c'.", s[0]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6081 | return NULL; |
| 6082 | } |
| 6083 | |
| 6084 | break; /* and we are done! */ |
| 6085 | } |
| 6086 | |
| 6087 | /* XXX: It is not clear what this is actually for. */ |
| 6088 | if ((err = PyErr_Occurred())) { |
| 6089 | if (err == PyExc_EOFError) { |
| 6090 | PyErr_SetNone(PyExc_EOFError); |
| 6091 | } |
| 6092 | return NULL; |
| 6093 | } |
| 6094 | |
Victor Stinner | 2ae57e3 | 2013-10-31 13:39:23 +0100 | [diff] [blame] | 6095 | if (_Unpickler_SkipConsumed(self) < 0) |
| 6096 | return NULL; |
| 6097 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6098 | PDATA_POP(self->stack, value); |
| 6099 | return value; |
| 6100 | } |
| 6101 | |
| 6102 | PyDoc_STRVAR(Unpickler_load_doc, |
| 6103 | "load() -> object. Load a pickle." |
| 6104 | "\n" |
| 6105 | "Read a pickled object representation from the open file object given in\n" |
| 6106 | "the constructor, and return the reconstituted object hierarchy specified\n" |
| 6107 | "therein.\n"); |
| 6108 | |
| 6109 | static PyObject * |
| 6110 | Unpickler_load(UnpicklerObject *self) |
| 6111 | { |
| 6112 | /* Check whether the Unpickler was initialized correctly. This prevents |
| 6113 | segfaulting if a subclass overridden __init__ with a function that does |
| 6114 | not call Unpickler.__init__(). Here, we simply ensure that self->read |
| 6115 | is not NULL. */ |
| 6116 | if (self->read == NULL) { |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 6117 | PyErr_Format(UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6118 | "Unpickler.__init__() was not called by %s.__init__()", |
| 6119 | Py_TYPE(self)->tp_name); |
| 6120 | return NULL; |
| 6121 | } |
| 6122 | |
| 6123 | return load(self); |
| 6124 | } |
| 6125 | |
| 6126 | /* The name of find_class() is misleading. In newer pickle protocols, this |
| 6127 | function is used for loading any global (i.e., functions), not just |
| 6128 | classes. The name is kept only for backward compatibility. */ |
| 6129 | |
| 6130 | PyDoc_STRVAR(Unpickler_find_class_doc, |
| 6131 | "find_class(module_name, global_name) -> object.\n" |
| 6132 | "\n" |
| 6133 | "Return an object from a specified module, importing the module if\n" |
| 6134 | "necessary. Subclasses may override this method (e.g. to restrict\n" |
| 6135 | "unpickling of arbitrary classes and functions).\n" |
| 6136 | "\n" |
| 6137 | "This method is called whenever a class or a function object is\n" |
| 6138 | "needed. Both arguments passed are str objects.\n"); |
| 6139 | |
| 6140 | static PyObject * |
| 6141 | Unpickler_find_class(UnpicklerObject *self, PyObject *args) |
| 6142 | { |
| 6143 | PyObject *global; |
| 6144 | PyObject *modules_dict; |
| 6145 | PyObject *module; |
| 6146 | PyObject *module_name, *global_name; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6147 | _Py_IDENTIFIER(modules); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6148 | |
| 6149 | if (!PyArg_UnpackTuple(args, "find_class", 2, 2, |
| 6150 | &module_name, &global_name)) |
| 6151 | return NULL; |
| 6152 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6153 | /* Try to map the old names used in Python 2.x to the new ones used in |
| 6154 | Python 3.x. We do this only with old pickle protocols and when the |
| 6155 | user has not disabled the feature. */ |
| 6156 | if (self->proto < 3 && self->fix_imports) { |
| 6157 | PyObject *key; |
| 6158 | PyObject *item; |
| 6159 | |
| 6160 | /* Check if the global (i.e., a function or a class) was renamed |
| 6161 | or moved to another module. */ |
| 6162 | key = PyTuple_Pack(2, module_name, global_name); |
| 6163 | if (key == NULL) |
| 6164 | return NULL; |
| 6165 | item = PyDict_GetItemWithError(name_mapping_2to3, key); |
| 6166 | Py_DECREF(key); |
| 6167 | if (item) { |
| 6168 | if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
| 6169 | PyErr_Format(PyExc_RuntimeError, |
| 6170 | "_compat_pickle.NAME_MAPPING values should be " |
| 6171 | "2-tuples, not %.200s", Py_TYPE(item)->tp_name); |
| 6172 | return NULL; |
| 6173 | } |
| 6174 | module_name = PyTuple_GET_ITEM(item, 0); |
| 6175 | global_name = PyTuple_GET_ITEM(item, 1); |
| 6176 | if (!PyUnicode_Check(module_name) || |
| 6177 | !PyUnicode_Check(global_name)) { |
| 6178 | PyErr_Format(PyExc_RuntimeError, |
| 6179 | "_compat_pickle.NAME_MAPPING values should be " |
| 6180 | "pairs of str, not (%.200s, %.200s)", |
| 6181 | Py_TYPE(module_name)->tp_name, |
| 6182 | Py_TYPE(global_name)->tp_name); |
| 6183 | return NULL; |
| 6184 | } |
| 6185 | } |
| 6186 | else if (PyErr_Occurred()) { |
| 6187 | return NULL; |
| 6188 | } |
| 6189 | |
| 6190 | /* Check if the module was renamed. */ |
| 6191 | item = PyDict_GetItemWithError(import_mapping_2to3, module_name); |
| 6192 | if (item) { |
| 6193 | if (!PyUnicode_Check(item)) { |
| 6194 | PyErr_Format(PyExc_RuntimeError, |
| 6195 | "_compat_pickle.IMPORT_MAPPING values should be " |
| 6196 | "strings, not %.200s", Py_TYPE(item)->tp_name); |
| 6197 | return NULL; |
| 6198 | } |
| 6199 | module_name = item; |
| 6200 | } |
| 6201 | else if (PyErr_Occurred()) { |
| 6202 | return NULL; |
| 6203 | } |
| 6204 | } |
| 6205 | |
Victor Stinner | bb52020 | 2013-11-06 22:40:41 +0100 | [diff] [blame] | 6206 | modules_dict = _PySys_GetObjectId(&PyId_modules); |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 6207 | if (modules_dict == NULL) { |
| 6208 | PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6209 | return NULL; |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 6210 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6211 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6212 | module = PyDict_GetItemWithError(modules_dict, module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6213 | if (module == NULL) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6214 | if (PyErr_Occurred()) |
| 6215 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6216 | module = PyImport_Import(module_name); |
| 6217 | if (module == NULL) |
| 6218 | return NULL; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6219 | global = getattribute(module, global_name, self->proto >= 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6220 | Py_DECREF(module); |
| 6221 | } |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 6222 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6223 | global = getattribute(module, global_name, self->proto >= 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6224 | } |
| 6225 | return global; |
| 6226 | } |
| 6227 | |
| 6228 | static struct PyMethodDef Unpickler_methods[] = { |
| 6229 | {"load", (PyCFunction)Unpickler_load, METH_NOARGS, |
| 6230 | Unpickler_load_doc}, |
| 6231 | {"find_class", (PyCFunction)Unpickler_find_class, METH_VARARGS, |
| 6232 | Unpickler_find_class_doc}, |
| 6233 | {NULL, NULL} /* sentinel */ |
| 6234 | }; |
| 6235 | |
| 6236 | static void |
| 6237 | Unpickler_dealloc(UnpicklerObject *self) |
| 6238 | { |
| 6239 | PyObject_GC_UnTrack((PyObject *)self); |
| 6240 | Py_XDECREF(self->readline); |
| 6241 | Py_XDECREF(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6242 | Py_XDECREF(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6243 | Py_XDECREF(self->stack); |
| 6244 | Py_XDECREF(self->pers_func); |
| 6245 | Py_XDECREF(self->arg); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6246 | if (self->buffer.buf != NULL) { |
| 6247 | PyBuffer_Release(&self->buffer); |
| 6248 | self->buffer.buf = NULL; |
| 6249 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6250 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6251 | _Unpickler_MemoCleanup(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6252 | PyMem_Free(self->marks); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6253 | PyMem_Free(self->input_line); |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6254 | PyMem_Free(self->encoding); |
| 6255 | PyMem_Free(self->errors); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6256 | |
| 6257 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 6258 | } |
| 6259 | |
| 6260 | static int |
| 6261 | Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg) |
| 6262 | { |
| 6263 | Py_VISIT(self->readline); |
| 6264 | Py_VISIT(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6265 | Py_VISIT(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6266 | Py_VISIT(self->stack); |
| 6267 | Py_VISIT(self->pers_func); |
| 6268 | Py_VISIT(self->arg); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6269 | return 0; |
| 6270 | } |
| 6271 | |
| 6272 | static int |
| 6273 | Unpickler_clear(UnpicklerObject *self) |
| 6274 | { |
| 6275 | Py_CLEAR(self->readline); |
| 6276 | Py_CLEAR(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6277 | Py_CLEAR(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6278 | Py_CLEAR(self->stack); |
| 6279 | Py_CLEAR(self->pers_func); |
| 6280 | Py_CLEAR(self->arg); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6281 | if (self->buffer.buf != NULL) { |
| 6282 | PyBuffer_Release(&self->buffer); |
| 6283 | self->buffer.buf = NULL; |
| 6284 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6285 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6286 | _Unpickler_MemoCleanup(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6287 | PyMem_Free(self->marks); |
| 6288 | self->marks = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6289 | PyMem_Free(self->input_line); |
| 6290 | self->input_line = NULL; |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6291 | PyMem_Free(self->encoding); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6292 | self->encoding = NULL; |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6293 | PyMem_Free(self->errors); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6294 | self->errors = NULL; |
| 6295 | |
| 6296 | return 0; |
| 6297 | } |
| 6298 | |
| 6299 | PyDoc_STRVAR(Unpickler_doc, |
| 6300 | "Unpickler(file, *, encoding='ASCII', errors='strict')" |
| 6301 | "\n" |
| 6302 | "This takes a binary file for reading a pickle data stream.\n" |
| 6303 | "\n" |
| 6304 | "The protocol version of the pickle is detected automatically, so no\n" |
| 6305 | "proto argument is needed.\n" |
| 6306 | "\n" |
| 6307 | "The file-like object must have two methods, a read() method\n" |
| 6308 | "that takes an integer argument, and a readline() method that\n" |
| 6309 | "requires no arguments. Both methods should return bytes.\n" |
| 6310 | "Thus file-like object can be a binary file object opened for\n" |
| 6311 | "reading, a BytesIO object, or any other custom object that\n" |
| 6312 | "meets this interface.\n" |
| 6313 | "\n" |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6314 | "Optional keyword arguments are *fix_imports*, *encoding* and *errors*,\n" |
| 6315 | "which are used to control compatiblity support for pickle stream\n" |
| 6316 | "generated by Python 2.x. If *fix_imports* is True, pickle will try to\n" |
| 6317 | "map the old Python 2.x names to the new names used in Python 3.x. The\n" |
| 6318 | "*encoding* and *errors* tell pickle how to decode 8-bit string\n" |
| 6319 | "instances pickled by Python 2.x; these default to 'ASCII' and\n" |
| 6320 | "'strict', respectively.\n"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6321 | |
| 6322 | static int |
| 6323 | Unpickler_init(UnpicklerObject *self, PyObject *args, PyObject *kwds) |
| 6324 | { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6325 | static char *kwlist[] = {"file", "fix_imports", "encoding", "errors", 0}; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6326 | PyObject *file; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6327 | PyObject *fix_imports = Py_True; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6328 | char *encoding = NULL; |
| 6329 | char *errors = NULL; |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 6330 | _Py_IDENTIFIER(persistent_load); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6331 | |
| 6332 | /* XXX: That is an horrible error message. But, I don't know how to do |
| 6333 | better... */ |
| 6334 | if (Py_SIZE(args) != 1) { |
| 6335 | PyErr_Format(PyExc_TypeError, |
| 6336 | "%s takes exactly one positional argument (%zd given)", |
| 6337 | Py_TYPE(self)->tp_name, Py_SIZE(args)); |
| 6338 | return -1; |
| 6339 | } |
| 6340 | |
| 6341 | /* Arguments parsing needs to be done in the __init__() method to allow |
| 6342 | subclasses to define their own __init__() method, which may (or may |
| 6343 | not) support Unpickler arguments. However, this means we need to be |
| 6344 | extra careful in the other Unpickler methods, since a subclass could |
| 6345 | forget to call Unpickler.__init__() thus breaking our internal |
| 6346 | invariants. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6347 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|Oss:Unpickler", kwlist, |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6348 | &file, &fix_imports, &encoding, &errors)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6349 | return -1; |
| 6350 | |
| 6351 | /* In case of multiple __init__() calls, clear previous content. */ |
| 6352 | if (self->read != NULL) |
| 6353 | (void)Unpickler_clear(self); |
| 6354 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6355 | if (_Unpickler_SetInputStream(self, file) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6356 | return -1; |
| 6357 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6358 | if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6359 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6360 | |
| 6361 | self->fix_imports = PyObject_IsTrue(fix_imports); |
| 6362 | if (self->fix_imports == -1) |
| 6363 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6364 | |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 6365 | if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) { |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6366 | self->pers_func = _PyObject_GetAttrId((PyObject *)self, |
| 6367 | &PyId_persistent_load); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6368 | if (self->pers_func == NULL) |
| 6369 | return -1; |
| 6370 | } |
| 6371 | else { |
| 6372 | self->pers_func = NULL; |
| 6373 | } |
| 6374 | |
| 6375 | self->stack = (Pdata *)Pdata_New(); |
| 6376 | if (self->stack == NULL) |
| 6377 | return -1; |
| 6378 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6379 | self->memo_size = 32; |
| 6380 | self->memo = _Unpickler_NewMemo(self->memo_size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6381 | if (self->memo == NULL) |
| 6382 | return -1; |
| 6383 | |
Alexandre Vassalotti | 0e7aa8c | 2009-04-03 04:17:41 +0000 | [diff] [blame] | 6384 | self->arg = NULL; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6385 | self->proto = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6386 | self->framing = 0; |
Alexandre Vassalotti | 0e7aa8c | 2009-04-03 04:17:41 +0000 | [diff] [blame] | 6387 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6388 | return 0; |
| 6389 | } |
| 6390 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6391 | /* Define a proxy object for the Unpickler's internal memo object. This is to |
| 6392 | * avoid breaking code like: |
| 6393 | * unpickler.memo.clear() |
| 6394 | * and |
| 6395 | * unpickler.memo = saved_memo |
| 6396 | * Is this a good idea? Not really, but we don't want to break code that uses |
| 6397 | * it. Note that we don't implement the entire mapping API here. This is |
| 6398 | * intentional, as these should be treated as black-box implementation details. |
| 6399 | * |
| 6400 | * We do, however, have to implement pickling/unpickling support because of |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 6401 | * real-world code like cvs2svn. |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6402 | */ |
| 6403 | |
| 6404 | typedef struct { |
| 6405 | PyObject_HEAD |
| 6406 | UnpicklerObject *unpickler; |
| 6407 | } UnpicklerMemoProxyObject; |
| 6408 | |
| 6409 | PyDoc_STRVAR(ump_clear_doc, |
| 6410 | "memo.clear() -> None. Remove all items from memo."); |
| 6411 | |
| 6412 | static PyObject * |
| 6413 | ump_clear(UnpicklerMemoProxyObject *self) |
| 6414 | { |
| 6415 | _Unpickler_MemoCleanup(self->unpickler); |
| 6416 | self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size); |
| 6417 | if (self->unpickler->memo == NULL) |
| 6418 | return NULL; |
| 6419 | Py_RETURN_NONE; |
| 6420 | } |
| 6421 | |
| 6422 | PyDoc_STRVAR(ump_copy_doc, |
| 6423 | "memo.copy() -> new_memo. Copy the memo to a new object."); |
| 6424 | |
| 6425 | static PyObject * |
| 6426 | ump_copy(UnpicklerMemoProxyObject *self) |
| 6427 | { |
| 6428 | Py_ssize_t i; |
| 6429 | PyObject *new_memo = PyDict_New(); |
| 6430 | if (new_memo == NULL) |
| 6431 | return NULL; |
| 6432 | |
| 6433 | for (i = 0; i < self->unpickler->memo_size; i++) { |
| 6434 | int status; |
| 6435 | PyObject *key, *value; |
| 6436 | |
| 6437 | value = self->unpickler->memo[i]; |
| 6438 | if (value == NULL) |
| 6439 | continue; |
| 6440 | |
| 6441 | key = PyLong_FromSsize_t(i); |
| 6442 | if (key == NULL) |
| 6443 | goto error; |
| 6444 | status = PyDict_SetItem(new_memo, key, value); |
| 6445 | Py_DECREF(key); |
| 6446 | if (status < 0) |
| 6447 | goto error; |
| 6448 | } |
| 6449 | return new_memo; |
| 6450 | |
| 6451 | error: |
| 6452 | Py_DECREF(new_memo); |
| 6453 | return NULL; |
| 6454 | } |
| 6455 | |
| 6456 | PyDoc_STRVAR(ump_reduce_doc, |
| 6457 | "memo.__reduce__(). Pickling support."); |
| 6458 | |
| 6459 | static PyObject * |
| 6460 | ump_reduce(UnpicklerMemoProxyObject *self, PyObject *args) |
| 6461 | { |
| 6462 | PyObject *reduce_value; |
| 6463 | PyObject *constructor_args; |
| 6464 | PyObject *contents = ump_copy(self); |
| 6465 | if (contents == NULL) |
| 6466 | return NULL; |
| 6467 | |
| 6468 | reduce_value = PyTuple_New(2); |
| 6469 | if (reduce_value == NULL) { |
| 6470 | Py_DECREF(contents); |
| 6471 | return NULL; |
| 6472 | } |
| 6473 | constructor_args = PyTuple_New(1); |
| 6474 | if (constructor_args == NULL) { |
| 6475 | Py_DECREF(contents); |
| 6476 | Py_DECREF(reduce_value); |
| 6477 | return NULL; |
| 6478 | } |
| 6479 | PyTuple_SET_ITEM(constructor_args, 0, contents); |
| 6480 | Py_INCREF((PyObject *)&PyDict_Type); |
| 6481 | PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type); |
| 6482 | PyTuple_SET_ITEM(reduce_value, 1, constructor_args); |
| 6483 | return reduce_value; |
| 6484 | } |
| 6485 | |
| 6486 | static PyMethodDef unpicklerproxy_methods[] = { |
| 6487 | {"clear", (PyCFunction)ump_clear, METH_NOARGS, ump_clear_doc}, |
| 6488 | {"copy", (PyCFunction)ump_copy, METH_NOARGS, ump_copy_doc}, |
| 6489 | {"__reduce__", (PyCFunction)ump_reduce, METH_VARARGS, ump_reduce_doc}, |
| 6490 | {NULL, NULL} /* sentinel */ |
| 6491 | }; |
| 6492 | |
| 6493 | static void |
| 6494 | UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self) |
| 6495 | { |
| 6496 | PyObject_GC_UnTrack(self); |
| 6497 | Py_XDECREF(self->unpickler); |
| 6498 | PyObject_GC_Del((PyObject *)self); |
| 6499 | } |
| 6500 | |
| 6501 | static int |
| 6502 | UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self, |
| 6503 | visitproc visit, void *arg) |
| 6504 | { |
| 6505 | Py_VISIT(self->unpickler); |
| 6506 | return 0; |
| 6507 | } |
| 6508 | |
| 6509 | static int |
| 6510 | UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self) |
| 6511 | { |
| 6512 | Py_CLEAR(self->unpickler); |
| 6513 | return 0; |
| 6514 | } |
| 6515 | |
| 6516 | static PyTypeObject UnpicklerMemoProxyType = { |
| 6517 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6518 | "_pickle.UnpicklerMemoProxy", /*tp_name*/ |
| 6519 | sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/ |
| 6520 | 0, |
| 6521 | (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */ |
| 6522 | 0, /* tp_print */ |
| 6523 | 0, /* tp_getattr */ |
| 6524 | 0, /* tp_setattr */ |
| 6525 | 0, /* tp_compare */ |
| 6526 | 0, /* tp_repr */ |
| 6527 | 0, /* tp_as_number */ |
| 6528 | 0, /* tp_as_sequence */ |
| 6529 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 6530 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6531 | 0, /* tp_call */ |
| 6532 | 0, /* tp_str */ |
| 6533 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 6534 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 6535 | 0, /* tp_as_buffer */ |
| 6536 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 6537 | 0, /* tp_doc */ |
| 6538 | (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */ |
| 6539 | (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */ |
| 6540 | 0, /* tp_richcompare */ |
| 6541 | 0, /* tp_weaklistoffset */ |
| 6542 | 0, /* tp_iter */ |
| 6543 | 0, /* tp_iternext */ |
| 6544 | unpicklerproxy_methods, /* tp_methods */ |
| 6545 | }; |
| 6546 | |
| 6547 | static PyObject * |
| 6548 | UnpicklerMemoProxy_New(UnpicklerObject *unpickler) |
| 6549 | { |
| 6550 | UnpicklerMemoProxyObject *self; |
| 6551 | |
| 6552 | self = PyObject_GC_New(UnpicklerMemoProxyObject, |
| 6553 | &UnpicklerMemoProxyType); |
| 6554 | if (self == NULL) |
| 6555 | return NULL; |
| 6556 | Py_INCREF(unpickler); |
| 6557 | self->unpickler = unpickler; |
| 6558 | PyObject_GC_Track(self); |
| 6559 | return (PyObject *)self; |
| 6560 | } |
| 6561 | |
| 6562 | /*****************************************************************************/ |
| 6563 | |
| 6564 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6565 | static PyObject * |
| 6566 | Unpickler_get_memo(UnpicklerObject *self) |
| 6567 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6568 | return UnpicklerMemoProxy_New(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6569 | } |
| 6570 | |
| 6571 | static int |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6572 | Unpickler_set_memo(UnpicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6573 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6574 | PyObject **new_memo; |
| 6575 | Py_ssize_t new_memo_size = 0; |
| 6576 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6577 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6578 | if (obj == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6579 | PyErr_SetString(PyExc_TypeError, |
| 6580 | "attribute deletion is not supported"); |
| 6581 | return -1; |
| 6582 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6583 | |
| 6584 | if (Py_TYPE(obj) == &UnpicklerMemoProxyType) { |
| 6585 | UnpicklerObject *unpickler = |
| 6586 | ((UnpicklerMemoProxyObject *)obj)->unpickler; |
| 6587 | |
| 6588 | new_memo_size = unpickler->memo_size; |
| 6589 | new_memo = _Unpickler_NewMemo(new_memo_size); |
| 6590 | if (new_memo == NULL) |
| 6591 | return -1; |
| 6592 | |
| 6593 | for (i = 0; i < new_memo_size; i++) { |
| 6594 | Py_XINCREF(unpickler->memo[i]); |
| 6595 | new_memo[i] = unpickler->memo[i]; |
| 6596 | } |
| 6597 | } |
| 6598 | else if (PyDict_Check(obj)) { |
| 6599 | Py_ssize_t i = 0; |
| 6600 | PyObject *key, *value; |
| 6601 | |
| 6602 | new_memo_size = PyDict_Size(obj); |
| 6603 | new_memo = _Unpickler_NewMemo(new_memo_size); |
| 6604 | if (new_memo == NULL) |
| 6605 | return -1; |
| 6606 | |
| 6607 | while (PyDict_Next(obj, &i, &key, &value)) { |
| 6608 | Py_ssize_t idx; |
| 6609 | if (!PyLong_Check(key)) { |
| 6610 | PyErr_SetString(PyExc_TypeError, |
| 6611 | "memo key must be integers"); |
| 6612 | goto error; |
| 6613 | } |
| 6614 | idx = PyLong_AsSsize_t(key); |
| 6615 | if (idx == -1 && PyErr_Occurred()) |
| 6616 | goto error; |
Christian Heimes | a24b4d2 | 2013-07-01 15:17:45 +0200 | [diff] [blame] | 6617 | if (idx < 0) { |
| 6618 | PyErr_SetString(PyExc_ValueError, |
Christian Heimes | 8087879 | 2013-07-01 15:23:39 +0200 | [diff] [blame] | 6619 | "memo key must be positive integers."); |
Christian Heimes | a24b4d2 | 2013-07-01 15:17:45 +0200 | [diff] [blame] | 6620 | goto error; |
| 6621 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6622 | if (_Unpickler_MemoPut(self, idx, value) < 0) |
| 6623 | goto error; |
| 6624 | } |
| 6625 | } |
| 6626 | else { |
| 6627 | PyErr_Format(PyExc_TypeError, |
| 6628 | "'memo' attribute must be an UnpicklerMemoProxy object" |
| 6629 | "or dict, not %.200s", Py_TYPE(obj)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6630 | return -1; |
| 6631 | } |
| 6632 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6633 | _Unpickler_MemoCleanup(self); |
| 6634 | self->memo_size = new_memo_size; |
| 6635 | self->memo = new_memo; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6636 | |
| 6637 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6638 | |
| 6639 | error: |
| 6640 | if (new_memo_size) { |
| 6641 | i = new_memo_size; |
| 6642 | while (--i >= 0) { |
| 6643 | Py_XDECREF(new_memo[i]); |
| 6644 | } |
| 6645 | PyMem_FREE(new_memo); |
| 6646 | } |
| 6647 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6648 | } |
| 6649 | |
| 6650 | static PyObject * |
| 6651 | Unpickler_get_persload(UnpicklerObject *self) |
| 6652 | { |
| 6653 | if (self->pers_func == NULL) |
| 6654 | PyErr_SetString(PyExc_AttributeError, "persistent_load"); |
| 6655 | else |
| 6656 | Py_INCREF(self->pers_func); |
| 6657 | return self->pers_func; |
| 6658 | } |
| 6659 | |
| 6660 | static int |
| 6661 | Unpickler_set_persload(UnpicklerObject *self, PyObject *value) |
| 6662 | { |
| 6663 | PyObject *tmp; |
| 6664 | |
| 6665 | if (value == NULL) { |
| 6666 | PyErr_SetString(PyExc_TypeError, |
| 6667 | "attribute deletion is not supported"); |
| 6668 | return -1; |
| 6669 | } |
| 6670 | if (!PyCallable_Check(value)) { |
| 6671 | PyErr_SetString(PyExc_TypeError, |
| 6672 | "persistent_load must be a callable taking " |
| 6673 | "one argument"); |
| 6674 | return -1; |
| 6675 | } |
| 6676 | |
| 6677 | tmp = self->pers_func; |
| 6678 | Py_INCREF(value); |
| 6679 | self->pers_func = value; |
| 6680 | Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */ |
| 6681 | |
| 6682 | return 0; |
| 6683 | } |
| 6684 | |
| 6685 | static PyGetSetDef Unpickler_getsets[] = { |
| 6686 | {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo}, |
| 6687 | {"persistent_load", (getter)Unpickler_get_persload, |
| 6688 | (setter)Unpickler_set_persload}, |
| 6689 | {NULL} |
| 6690 | }; |
| 6691 | |
| 6692 | static PyTypeObject Unpickler_Type = { |
| 6693 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6694 | "_pickle.Unpickler", /*tp_name*/ |
| 6695 | sizeof(UnpicklerObject), /*tp_basicsize*/ |
| 6696 | 0, /*tp_itemsize*/ |
| 6697 | (destructor)Unpickler_dealloc, /*tp_dealloc*/ |
| 6698 | 0, /*tp_print*/ |
| 6699 | 0, /*tp_getattr*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6700 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 6701 | 0, /*tp_reserved*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6702 | 0, /*tp_repr*/ |
| 6703 | 0, /*tp_as_number*/ |
| 6704 | 0, /*tp_as_sequence*/ |
| 6705 | 0, /*tp_as_mapping*/ |
| 6706 | 0, /*tp_hash*/ |
| 6707 | 0, /*tp_call*/ |
| 6708 | 0, /*tp_str*/ |
| 6709 | 0, /*tp_getattro*/ |
| 6710 | 0, /*tp_setattro*/ |
| 6711 | 0, /*tp_as_buffer*/ |
| 6712 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 6713 | Unpickler_doc, /*tp_doc*/ |
| 6714 | (traverseproc)Unpickler_traverse, /*tp_traverse*/ |
| 6715 | (inquiry)Unpickler_clear, /*tp_clear*/ |
| 6716 | 0, /*tp_richcompare*/ |
| 6717 | 0, /*tp_weaklistoffset*/ |
| 6718 | 0, /*tp_iter*/ |
| 6719 | 0, /*tp_iternext*/ |
| 6720 | Unpickler_methods, /*tp_methods*/ |
| 6721 | 0, /*tp_members*/ |
| 6722 | Unpickler_getsets, /*tp_getset*/ |
| 6723 | 0, /*tp_base*/ |
| 6724 | 0, /*tp_dict*/ |
| 6725 | 0, /*tp_descr_get*/ |
| 6726 | 0, /*tp_descr_set*/ |
| 6727 | 0, /*tp_dictoffset*/ |
| 6728 | (initproc)Unpickler_init, /*tp_init*/ |
| 6729 | PyType_GenericAlloc, /*tp_alloc*/ |
| 6730 | PyType_GenericNew, /*tp_new*/ |
| 6731 | PyObject_GC_Del, /*tp_free*/ |
| 6732 | 0, /*tp_is_gc*/ |
| 6733 | }; |
| 6734 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6735 | PyDoc_STRVAR(pickle_dump_doc, |
| 6736 | "dump(obj, file, protocol=None, *, fix_imports=True) -> None\n" |
| 6737 | "\n" |
| 6738 | "Write a pickled representation of obj to the open file object file. This\n" |
| 6739 | "is equivalent to ``Pickler(file, protocol).dump(obj)``, but may be more\n" |
| 6740 | "efficient.\n" |
| 6741 | "\n" |
| 6742 | "The optional protocol argument tells the pickler to use the given protocol;\n" |
| 6743 | "supported protocols are 0, 1, 2, 3. The default protocol is 3; a\n" |
| 6744 | "backward-incompatible protocol designed for Python 3.0.\n" |
| 6745 | "\n" |
| 6746 | "Specifying a negative protocol version selects the highest protocol version\n" |
| 6747 | "supported. The higher the protocol used, the more recent the version of\n" |
| 6748 | "Python needed to read the pickle produced.\n" |
| 6749 | "\n" |
| 6750 | "The file argument must have a write() method that accepts a single bytes\n" |
| 6751 | "argument. It can thus be a file object opened for binary writing, a\n" |
| 6752 | "io.BytesIO instance, or any other custom object that meets this interface.\n" |
| 6753 | "\n" |
| 6754 | "If fix_imports is True and protocol is less than 3, pickle will try to\n" |
| 6755 | "map the new Python 3.x names to the old module names used in Python 2.x,\n" |
| 6756 | "so that the pickle data stream is readable with Python 2.x.\n"); |
| 6757 | |
| 6758 | static PyObject * |
| 6759 | pickle_dump(PyObject *self, PyObject *args, PyObject *kwds) |
| 6760 | { |
| 6761 | static char *kwlist[] = {"obj", "file", "protocol", "fix_imports", 0}; |
| 6762 | PyObject *obj; |
| 6763 | PyObject *file; |
| 6764 | PyObject *proto = NULL; |
| 6765 | PyObject *fix_imports = Py_True; |
| 6766 | PicklerObject *pickler; |
| 6767 | |
| 6768 | /* fix_imports is a keyword-only argument. */ |
| 6769 | if (Py_SIZE(args) > 3) { |
| 6770 | PyErr_Format(PyExc_TypeError, |
| 6771 | "pickle.dump() takes at most 3 positional " |
| 6772 | "argument (%zd given)", Py_SIZE(args)); |
| 6773 | return NULL; |
| 6774 | } |
| 6775 | |
| 6776 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|OO:dump", kwlist, |
| 6777 | &obj, &file, &proto, &fix_imports)) |
| 6778 | return NULL; |
| 6779 | |
| 6780 | pickler = _Pickler_New(); |
| 6781 | if (pickler == NULL) |
| 6782 | return NULL; |
| 6783 | |
| 6784 | if (_Pickler_SetProtocol(pickler, proto, fix_imports) < 0) |
| 6785 | goto error; |
| 6786 | |
| 6787 | if (_Pickler_SetOutputStream(pickler, file) < 0) |
| 6788 | goto error; |
| 6789 | |
| 6790 | if (dump(pickler, obj) < 0) |
| 6791 | goto error; |
| 6792 | |
| 6793 | if (_Pickler_FlushToFile(pickler) < 0) |
| 6794 | goto error; |
| 6795 | |
| 6796 | Py_DECREF(pickler); |
| 6797 | Py_RETURN_NONE; |
| 6798 | |
| 6799 | error: |
| 6800 | Py_XDECREF(pickler); |
| 6801 | return NULL; |
| 6802 | } |
| 6803 | |
| 6804 | PyDoc_STRVAR(pickle_dumps_doc, |
| 6805 | "dumps(obj, protocol=None, *, fix_imports=True) -> bytes\n" |
| 6806 | "\n" |
| 6807 | "Return the pickled representation of the object as a bytes\n" |
| 6808 | "object, instead of writing it to a file.\n" |
| 6809 | "\n" |
| 6810 | "The optional protocol argument tells the pickler to use the given protocol;\n" |
| 6811 | "supported protocols are 0, 1, 2, 3. The default protocol is 3; a\n" |
| 6812 | "backward-incompatible protocol designed for Python 3.0.\n" |
| 6813 | "\n" |
| 6814 | "Specifying a negative protocol version selects the highest protocol version\n" |
| 6815 | "supported. The higher the protocol used, the more recent the version of\n" |
| 6816 | "Python needed to read the pickle produced.\n" |
| 6817 | "\n" |
| 6818 | "If fix_imports is True and *protocol* is less than 3, pickle will try to\n" |
| 6819 | "map the new Python 3.x names to the old module names used in Python 2.x,\n" |
| 6820 | "so that the pickle data stream is readable with Python 2.x.\n"); |
| 6821 | |
| 6822 | static PyObject * |
| 6823 | pickle_dumps(PyObject *self, PyObject *args, PyObject *kwds) |
| 6824 | { |
| 6825 | static char *kwlist[] = {"obj", "protocol", "fix_imports", 0}; |
| 6826 | PyObject *obj; |
| 6827 | PyObject *proto = NULL; |
| 6828 | PyObject *result; |
| 6829 | PyObject *fix_imports = Py_True; |
| 6830 | PicklerObject *pickler; |
| 6831 | |
| 6832 | /* fix_imports is a keyword-only argument. */ |
| 6833 | if (Py_SIZE(args) > 2) { |
| 6834 | PyErr_Format(PyExc_TypeError, |
| 6835 | "pickle.dumps() takes at most 2 positional " |
| 6836 | "argument (%zd given)", Py_SIZE(args)); |
| 6837 | return NULL; |
| 6838 | } |
| 6839 | |
| 6840 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OO:dumps", kwlist, |
| 6841 | &obj, &proto, &fix_imports)) |
| 6842 | return NULL; |
| 6843 | |
| 6844 | pickler = _Pickler_New(); |
| 6845 | if (pickler == NULL) |
| 6846 | return NULL; |
| 6847 | |
| 6848 | if (_Pickler_SetProtocol(pickler, proto, fix_imports) < 0) |
| 6849 | goto error; |
| 6850 | |
| 6851 | if (dump(pickler, obj) < 0) |
| 6852 | goto error; |
| 6853 | |
| 6854 | result = _Pickler_GetString(pickler); |
| 6855 | Py_DECREF(pickler); |
| 6856 | return result; |
| 6857 | |
| 6858 | error: |
| 6859 | Py_XDECREF(pickler); |
| 6860 | return NULL; |
| 6861 | } |
| 6862 | |
| 6863 | PyDoc_STRVAR(pickle_load_doc, |
| 6864 | "load(file, *, fix_imports=True, encoding='ASCII', errors='strict') -> object\n" |
| 6865 | "\n" |
| 6866 | "Read a pickled object representation from the open file object file and\n" |
| 6867 | "return the reconstituted object hierarchy specified therein. This is\n" |
| 6868 | "equivalent to ``Unpickler(file).load()``, but may be more efficient.\n" |
| 6869 | "\n" |
| 6870 | "The protocol version of the pickle is detected automatically, so no protocol\n" |
| 6871 | "argument is needed. Bytes past the pickled object's representation are\n" |
| 6872 | "ignored.\n" |
| 6873 | "\n" |
| 6874 | "The argument file must have two methods, a read() method that takes an\n" |
| 6875 | "integer argument, and a readline() method that requires no arguments. Both\n" |
| 6876 | "methods should return bytes. Thus *file* can be a binary file object opened\n" |
| 6877 | "for reading, a BytesIO object, or any other custom object that meets this\n" |
| 6878 | "interface.\n" |
| 6879 | "\n" |
| 6880 | "Optional keyword arguments are fix_imports, encoding and errors,\n" |
| 6881 | "which are used to control compatiblity support for pickle stream generated\n" |
| 6882 | "by Python 2.x. If fix_imports is True, pickle will try to map the old\n" |
| 6883 | "Python 2.x names to the new names used in Python 3.x. The encoding and\n" |
| 6884 | "errors tell pickle how to decode 8-bit string instances pickled by Python\n" |
| 6885 | "2.x; these default to 'ASCII' and 'strict', respectively.\n"); |
| 6886 | |
| 6887 | static PyObject * |
| 6888 | pickle_load(PyObject *self, PyObject *args, PyObject *kwds) |
| 6889 | { |
| 6890 | static char *kwlist[] = {"file", "fix_imports", "encoding", "errors", 0}; |
| 6891 | PyObject *file; |
| 6892 | PyObject *fix_imports = Py_True; |
| 6893 | PyObject *result; |
| 6894 | char *encoding = NULL; |
| 6895 | char *errors = NULL; |
| 6896 | UnpicklerObject *unpickler; |
| 6897 | |
| 6898 | /* fix_imports, encoding and errors are a keyword-only argument. */ |
| 6899 | if (Py_SIZE(args) != 1) { |
| 6900 | PyErr_Format(PyExc_TypeError, |
| 6901 | "pickle.load() takes exactly one positional " |
| 6902 | "argument (%zd given)", Py_SIZE(args)); |
| 6903 | return NULL; |
| 6904 | } |
| 6905 | |
| 6906 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|Oss:load", kwlist, |
| 6907 | &file, &fix_imports, &encoding, &errors)) |
| 6908 | return NULL; |
| 6909 | |
| 6910 | unpickler = _Unpickler_New(); |
| 6911 | if (unpickler == NULL) |
| 6912 | return NULL; |
| 6913 | |
| 6914 | if (_Unpickler_SetInputStream(unpickler, file) < 0) |
| 6915 | goto error; |
| 6916 | |
| 6917 | if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0) |
| 6918 | goto error; |
| 6919 | |
| 6920 | unpickler->fix_imports = PyObject_IsTrue(fix_imports); |
| 6921 | if (unpickler->fix_imports == -1) |
| 6922 | goto error; |
| 6923 | |
| 6924 | result = load(unpickler); |
| 6925 | Py_DECREF(unpickler); |
| 6926 | return result; |
| 6927 | |
| 6928 | error: |
| 6929 | Py_XDECREF(unpickler); |
| 6930 | return NULL; |
| 6931 | } |
| 6932 | |
| 6933 | PyDoc_STRVAR(pickle_loads_doc, |
| 6934 | "loads(input, *, fix_imports=True, encoding='ASCII', errors='strict') -> object\n" |
| 6935 | "\n" |
| 6936 | "Read a pickled object hierarchy from a bytes object and return the\n" |
| 6937 | "reconstituted object hierarchy specified therein\n" |
| 6938 | "\n" |
| 6939 | "The protocol version of the pickle is detected automatically, so no protocol\n" |
| 6940 | "argument is needed. Bytes past the pickled object's representation are\n" |
| 6941 | "ignored.\n" |
| 6942 | "\n" |
| 6943 | "Optional keyword arguments are fix_imports, encoding and errors, which\n" |
| 6944 | "are used to control compatiblity support for pickle stream generated\n" |
| 6945 | "by Python 2.x. If fix_imports is True, pickle will try to map the old\n" |
| 6946 | "Python 2.x names to the new names used in Python 3.x. The encoding and\n" |
| 6947 | "errors tell pickle how to decode 8-bit string instances pickled by Python\n" |
| 6948 | "2.x; these default to 'ASCII' and 'strict', respectively.\n"); |
| 6949 | |
| 6950 | static PyObject * |
| 6951 | pickle_loads(PyObject *self, PyObject *args, PyObject *kwds) |
| 6952 | { |
| 6953 | static char *kwlist[] = {"input", "fix_imports", "encoding", "errors", 0}; |
| 6954 | PyObject *input; |
| 6955 | PyObject *fix_imports = Py_True; |
| 6956 | PyObject *result; |
| 6957 | char *encoding = NULL; |
| 6958 | char *errors = NULL; |
| 6959 | UnpicklerObject *unpickler; |
| 6960 | |
| 6961 | /* fix_imports, encoding and errors are a keyword-only argument. */ |
| 6962 | if (Py_SIZE(args) != 1) { |
| 6963 | PyErr_Format(PyExc_TypeError, |
| 6964 | "pickle.loads() takes exactly one positional " |
| 6965 | "argument (%zd given)", Py_SIZE(args)); |
| 6966 | return NULL; |
| 6967 | } |
| 6968 | |
| 6969 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|Oss:loads", kwlist, |
| 6970 | &input, &fix_imports, &encoding, &errors)) |
| 6971 | return NULL; |
| 6972 | |
| 6973 | unpickler = _Unpickler_New(); |
| 6974 | if (unpickler == NULL) |
| 6975 | return NULL; |
| 6976 | |
| 6977 | if (_Unpickler_SetStringInput(unpickler, input) < 0) |
| 6978 | goto error; |
| 6979 | |
| 6980 | if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0) |
| 6981 | goto error; |
| 6982 | |
| 6983 | unpickler->fix_imports = PyObject_IsTrue(fix_imports); |
| 6984 | if (unpickler->fix_imports == -1) |
| 6985 | goto error; |
| 6986 | |
| 6987 | result = load(unpickler); |
| 6988 | Py_DECREF(unpickler); |
| 6989 | return result; |
| 6990 | |
| 6991 | error: |
| 6992 | Py_XDECREF(unpickler); |
| 6993 | return NULL; |
| 6994 | } |
| 6995 | |
| 6996 | |
| 6997 | static struct PyMethodDef pickle_methods[] = { |
| 6998 | {"dump", (PyCFunction)pickle_dump, METH_VARARGS|METH_KEYWORDS, |
| 6999 | pickle_dump_doc}, |
| 7000 | {"dumps", (PyCFunction)pickle_dumps, METH_VARARGS|METH_KEYWORDS, |
| 7001 | pickle_dumps_doc}, |
| 7002 | {"load", (PyCFunction)pickle_load, METH_VARARGS|METH_KEYWORDS, |
| 7003 | pickle_load_doc}, |
| 7004 | {"loads", (PyCFunction)pickle_loads, METH_VARARGS|METH_KEYWORDS, |
| 7005 | pickle_loads_doc}, |
| 7006 | {NULL, NULL} /* sentinel */ |
| 7007 | }; |
| 7008 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7009 | static int |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 7010 | initmodule(void) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7011 | { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 7012 | PyObject *copyreg = NULL; |
| 7013 | PyObject *compat_pickle = NULL; |
| 7014 | |
| 7015 | /* XXX: We should ensure that the types of the dictionaries imported are |
| 7016 | exactly PyDict objects. Otherwise, it is possible to crash the pickle |
| 7017 | since we use the PyDict API directly to access these dictionaries. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7018 | |
| 7019 | copyreg = PyImport_ImportModule("copyreg"); |
| 7020 | if (!copyreg) |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 7021 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7022 | dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table"); |
| 7023 | if (!dispatch_table) |
| 7024 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7025 | extension_registry = \ |
| 7026 | PyObject_GetAttrString(copyreg, "_extension_registry"); |
| 7027 | if (!extension_registry) |
| 7028 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7029 | inverted_registry = PyObject_GetAttrString(copyreg, "_inverted_registry"); |
| 7030 | if (!inverted_registry) |
| 7031 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7032 | extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache"); |
| 7033 | if (!extension_cache) |
| 7034 | goto error; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 7035 | Py_CLEAR(copyreg); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7036 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 7037 | /* Load the 2.x -> 3.x stdlib module mapping tables */ |
| 7038 | compat_pickle = PyImport_ImportModule("_compat_pickle"); |
| 7039 | if (!compat_pickle) |
| 7040 | goto error; |
| 7041 | name_mapping_2to3 = PyObject_GetAttrString(compat_pickle, "NAME_MAPPING"); |
| 7042 | if (!name_mapping_2to3) |
| 7043 | goto error; |
| 7044 | if (!PyDict_CheckExact(name_mapping_2to3)) { |
| 7045 | PyErr_Format(PyExc_RuntimeError, |
| 7046 | "_compat_pickle.NAME_MAPPING should be a dict, not %.200s", |
| 7047 | Py_TYPE(name_mapping_2to3)->tp_name); |
| 7048 | goto error; |
| 7049 | } |
| 7050 | import_mapping_2to3 = PyObject_GetAttrString(compat_pickle, |
| 7051 | "IMPORT_MAPPING"); |
| 7052 | if (!import_mapping_2to3) |
| 7053 | goto error; |
| 7054 | if (!PyDict_CheckExact(import_mapping_2to3)) { |
| 7055 | PyErr_Format(PyExc_RuntimeError, |
| 7056 | "_compat_pickle.IMPORT_MAPPING should be a dict, " |
| 7057 | "not %.200s", Py_TYPE(import_mapping_2to3)->tp_name); |
| 7058 | goto error; |
| 7059 | } |
| 7060 | /* ... and the 3.x -> 2.x mapping tables */ |
| 7061 | name_mapping_3to2 = PyObject_GetAttrString(compat_pickle, |
| 7062 | "REVERSE_NAME_MAPPING"); |
| 7063 | if (!name_mapping_3to2) |
| 7064 | goto error; |
| 7065 | if (!PyDict_CheckExact(name_mapping_3to2)) { |
| 7066 | PyErr_Format(PyExc_RuntimeError, |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 7067 | "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, " |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 7068 | "not %.200s", Py_TYPE(name_mapping_3to2)->tp_name); |
| 7069 | goto error; |
| 7070 | } |
| 7071 | import_mapping_3to2 = PyObject_GetAttrString(compat_pickle, |
| 7072 | "REVERSE_IMPORT_MAPPING"); |
| 7073 | if (!import_mapping_3to2) |
| 7074 | goto error; |
| 7075 | if (!PyDict_CheckExact(import_mapping_3to2)) { |
| 7076 | PyErr_Format(PyExc_RuntimeError, |
| 7077 | "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, " |
| 7078 | "not %.200s", Py_TYPE(import_mapping_3to2)->tp_name); |
| 7079 | goto error; |
| 7080 | } |
| 7081 | Py_CLEAR(compat_pickle); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7082 | |
| 7083 | empty_tuple = PyTuple_New(0); |
| 7084 | if (empty_tuple == NULL) |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 7085 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7086 | two_tuple = PyTuple_New(2); |
| 7087 | if (two_tuple == NULL) |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 7088 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7089 | /* We use this temp container with no regard to refcounts, or to |
| 7090 | * keeping containees alive. Exempt from GC, because we don't |
| 7091 | * want anything looking at two_tuple() by magic. |
| 7092 | */ |
| 7093 | PyObject_GC_UnTrack(two_tuple); |
| 7094 | |
| 7095 | return 0; |
| 7096 | |
| 7097 | error: |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 7098 | Py_CLEAR(copyreg); |
| 7099 | Py_CLEAR(dispatch_table); |
| 7100 | Py_CLEAR(extension_registry); |
| 7101 | Py_CLEAR(inverted_registry); |
| 7102 | Py_CLEAR(extension_cache); |
| 7103 | Py_CLEAR(compat_pickle); |
| 7104 | Py_CLEAR(name_mapping_2to3); |
| 7105 | Py_CLEAR(import_mapping_2to3); |
| 7106 | Py_CLEAR(name_mapping_3to2); |
| 7107 | Py_CLEAR(import_mapping_3to2); |
| 7108 | Py_CLEAR(empty_tuple); |
| 7109 | Py_CLEAR(two_tuple); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7110 | return -1; |
| 7111 | } |
| 7112 | |
| 7113 | static struct PyModuleDef _picklemodule = { |
| 7114 | PyModuleDef_HEAD_INIT, |
| 7115 | "_pickle", |
| 7116 | pickle_module_doc, |
| 7117 | -1, |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7118 | pickle_methods, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7119 | NULL, |
| 7120 | NULL, |
| 7121 | NULL, |
| 7122 | NULL |
| 7123 | }; |
| 7124 | |
| 7125 | PyMODINIT_FUNC |
| 7126 | PyInit__pickle(void) |
| 7127 | { |
| 7128 | PyObject *m; |
| 7129 | |
| 7130 | if (PyType_Ready(&Unpickler_Type) < 0) |
| 7131 | return NULL; |
| 7132 | if (PyType_Ready(&Pickler_Type) < 0) |
| 7133 | return NULL; |
| 7134 | if (PyType_Ready(&Pdata_Type) < 0) |
| 7135 | return NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7136 | if (PyType_Ready(&PicklerMemoProxyType) < 0) |
| 7137 | return NULL; |
| 7138 | if (PyType_Ready(&UnpicklerMemoProxyType) < 0) |
| 7139 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7140 | |
| 7141 | /* Create the module and add the functions. */ |
| 7142 | m = PyModule_Create(&_picklemodule); |
| 7143 | if (m == NULL) |
| 7144 | return NULL; |
| 7145 | |
Antoine Pitrou | 8391cf4 | 2011-07-15 21:01:21 +0200 | [diff] [blame] | 7146 | Py_INCREF(&Pickler_Type); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7147 | if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0) |
| 7148 | return NULL; |
Antoine Pitrou | 8391cf4 | 2011-07-15 21:01:21 +0200 | [diff] [blame] | 7149 | Py_INCREF(&Unpickler_Type); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7150 | if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0) |
| 7151 | return NULL; |
| 7152 | |
| 7153 | /* Initialize the exceptions. */ |
| 7154 | PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL); |
| 7155 | if (PickleError == NULL) |
| 7156 | return NULL; |
| 7157 | PicklingError = \ |
| 7158 | PyErr_NewException("_pickle.PicklingError", PickleError, NULL); |
| 7159 | if (PicklingError == NULL) |
| 7160 | return NULL; |
| 7161 | UnpicklingError = \ |
| 7162 | PyErr_NewException("_pickle.UnpicklingError", PickleError, NULL); |
| 7163 | if (UnpicklingError == NULL) |
| 7164 | return NULL; |
| 7165 | |
| 7166 | if (PyModule_AddObject(m, "PickleError", PickleError) < 0) |
| 7167 | return NULL; |
| 7168 | if (PyModule_AddObject(m, "PicklingError", PicklingError) < 0) |
| 7169 | return NULL; |
| 7170 | if (PyModule_AddObject(m, "UnpicklingError", UnpicklingError) < 0) |
| 7171 | return NULL; |
| 7172 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 7173 | if (initmodule() < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7174 | return NULL; |
| 7175 | |
| 7176 | return m; |
| 7177 | } |