Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | #include "structmember.h" |
| 3 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4 | PyDoc_STRVAR(pickle_module_doc, |
| 5 | "Optimized C implementation for the Python pickle module."); |
| 6 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7 | /*[clinic input] |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 8 | output preset file |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 9 | module _pickle |
Larry Hastings | c204726 | 2014-01-25 20:43:29 -0800 | [diff] [blame] | 10 | class _pickle.Pickler "PicklerObject *" "&Pickler_Type" |
| 11 | class _pickle.PicklerMemoProxy "PicklerMemoProxyObject *" "&PicklerMemoProxyType" |
| 12 | class _pickle.Unpickler "UnpicklerObject *" "&Unpickler_Type" |
| 13 | class _pickle.UnpicklerMemoProxy "UnpicklerMemoProxyObject *" "&UnpicklerMemoProxyType" |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 14 | [clinic start generated code]*/ |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 15 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=11c45248a41dd3fc]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 16 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 17 | /* Bump this when new opcodes are added to the pickle protocol. */ |
| 18 | enum { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 19 | HIGHEST_PROTOCOL = 4, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 20 | DEFAULT_PROTOCOL = 3 |
| 21 | }; |
| 22 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 23 | /* Pickle opcodes. These must be kept updated with pickle.py. |
| 24 | Extensive docs are in pickletools.py. */ |
| 25 | enum opcode { |
| 26 | MARK = '(', |
| 27 | STOP = '.', |
| 28 | POP = '0', |
| 29 | POP_MARK = '1', |
| 30 | DUP = '2', |
| 31 | FLOAT = 'F', |
| 32 | INT = 'I', |
| 33 | BININT = 'J', |
| 34 | BININT1 = 'K', |
| 35 | LONG = 'L', |
| 36 | BININT2 = 'M', |
| 37 | NONE = 'N', |
| 38 | PERSID = 'P', |
| 39 | BINPERSID = 'Q', |
| 40 | REDUCE = 'R', |
| 41 | STRING = 'S', |
| 42 | BINSTRING = 'T', |
| 43 | SHORT_BINSTRING = 'U', |
| 44 | UNICODE = 'V', |
| 45 | BINUNICODE = 'X', |
| 46 | APPEND = 'a', |
| 47 | BUILD = 'b', |
| 48 | GLOBAL = 'c', |
| 49 | DICT = 'd', |
| 50 | EMPTY_DICT = '}', |
| 51 | APPENDS = 'e', |
| 52 | GET = 'g', |
| 53 | BINGET = 'h', |
| 54 | INST = 'i', |
| 55 | LONG_BINGET = 'j', |
| 56 | LIST = 'l', |
| 57 | EMPTY_LIST = ']', |
| 58 | OBJ = 'o', |
| 59 | PUT = 'p', |
| 60 | BINPUT = 'q', |
| 61 | LONG_BINPUT = 'r', |
| 62 | SETITEM = 's', |
| 63 | TUPLE = 't', |
| 64 | EMPTY_TUPLE = ')', |
| 65 | SETITEMS = 'u', |
| 66 | BINFLOAT = 'G', |
| 67 | |
| 68 | /* Protocol 2. */ |
| 69 | PROTO = '\x80', |
| 70 | NEWOBJ = '\x81', |
| 71 | EXT1 = '\x82', |
| 72 | EXT2 = '\x83', |
| 73 | EXT4 = '\x84', |
| 74 | TUPLE1 = '\x85', |
| 75 | TUPLE2 = '\x86', |
| 76 | TUPLE3 = '\x87', |
| 77 | NEWTRUE = '\x88', |
| 78 | NEWFALSE = '\x89', |
| 79 | LONG1 = '\x8a', |
| 80 | LONG4 = '\x8b', |
| 81 | |
| 82 | /* Protocol 3 (Python 3.x) */ |
| 83 | BINBYTES = 'B', |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 84 | SHORT_BINBYTES = 'C', |
| 85 | |
| 86 | /* Protocol 4 */ |
| 87 | SHORT_BINUNICODE = '\x8c', |
| 88 | BINUNICODE8 = '\x8d', |
| 89 | BINBYTES8 = '\x8e', |
| 90 | EMPTY_SET = '\x8f', |
| 91 | ADDITEMS = '\x90', |
| 92 | FROZENSET = '\x91', |
| 93 | NEWOBJ_EX = '\x92', |
| 94 | STACK_GLOBAL = '\x93', |
| 95 | MEMOIZE = '\x94', |
| 96 | FRAME = '\x95' |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 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 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 113 | /* Prefetch size when unpickling (disabled on unpeekable streams) */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 114 | PREFETCH = 8192 * 16, |
| 115 | |
| 116 | FRAME_SIZE_TARGET = 64 * 1024, |
| 117 | |
| 118 | FRAME_HEADER_SIZE = 9 |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 121 | /*************************************************************************/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 122 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 123 | /* State of the pickle module, per PEP 3121. */ |
| 124 | typedef struct { |
| 125 | /* Exception classes for pickle. */ |
| 126 | PyObject *PickleError; |
| 127 | PyObject *PicklingError; |
| 128 | PyObject *UnpicklingError; |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 129 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 130 | /* copyreg.dispatch_table, {type_object: pickling_function} */ |
| 131 | PyObject *dispatch_table; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 132 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 133 | /* For the extension opcodes EXT1, EXT2 and EXT4. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 134 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 135 | /* copyreg._extension_registry, {(module_name, function_name): code} */ |
| 136 | PyObject *extension_registry; |
| 137 | /* copyreg._extension_cache, {code: object} */ |
| 138 | PyObject *extension_cache; |
| 139 | /* copyreg._inverted_registry, {code: (module_name, function_name)} */ |
| 140 | PyObject *inverted_registry; |
| 141 | |
| 142 | /* Import mappings for compatibility with Python 2.x */ |
| 143 | |
| 144 | /* _compat_pickle.NAME_MAPPING, |
| 145 | {(oldmodule, oldname): (newmodule, newname)} */ |
| 146 | PyObject *name_mapping_2to3; |
| 147 | /* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */ |
| 148 | PyObject *import_mapping_2to3; |
| 149 | /* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */ |
| 150 | PyObject *name_mapping_3to2; |
| 151 | PyObject *import_mapping_3to2; |
| 152 | |
| 153 | /* codecs.encode, used for saving bytes in older protocols */ |
| 154 | PyObject *codecs_encode; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 155 | } PickleState; |
| 156 | |
| 157 | /* Forward declaration of the _pickle module definition. */ |
| 158 | static struct PyModuleDef _picklemodule; |
| 159 | |
| 160 | /* Given a module object, get its per-module state. */ |
| 161 | static PickleState * |
| 162 | _Pickle_GetState(PyObject *module) |
| 163 | { |
| 164 | return (PickleState *)PyModule_GetState(module); |
| 165 | } |
| 166 | |
| 167 | /* Find the module instance imported in the currently running sub-interpreter |
| 168 | and get its state. */ |
| 169 | static PickleState * |
| 170 | _Pickle_GetGlobalState(void) |
| 171 | { |
| 172 | return _Pickle_GetState(PyState_FindModule(&_picklemodule)); |
| 173 | } |
| 174 | |
| 175 | /* Clear the given pickle module state. */ |
| 176 | static void |
| 177 | _Pickle_ClearState(PickleState *st) |
| 178 | { |
| 179 | Py_CLEAR(st->PickleError); |
| 180 | Py_CLEAR(st->PicklingError); |
| 181 | Py_CLEAR(st->UnpicklingError); |
| 182 | Py_CLEAR(st->dispatch_table); |
| 183 | Py_CLEAR(st->extension_registry); |
| 184 | Py_CLEAR(st->extension_cache); |
| 185 | Py_CLEAR(st->inverted_registry); |
| 186 | Py_CLEAR(st->name_mapping_2to3); |
| 187 | Py_CLEAR(st->import_mapping_2to3); |
| 188 | Py_CLEAR(st->name_mapping_3to2); |
| 189 | Py_CLEAR(st->import_mapping_3to2); |
| 190 | Py_CLEAR(st->codecs_encode); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /* Initialize the given pickle module state. */ |
| 194 | static int |
| 195 | _Pickle_InitState(PickleState *st) |
| 196 | { |
| 197 | PyObject *copyreg = NULL; |
| 198 | PyObject *compat_pickle = NULL; |
| 199 | PyObject *codecs = NULL; |
| 200 | |
| 201 | copyreg = PyImport_ImportModule("copyreg"); |
| 202 | if (!copyreg) |
| 203 | goto error; |
| 204 | st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table"); |
| 205 | if (!st->dispatch_table) |
| 206 | goto error; |
| 207 | if (!PyDict_CheckExact(st->dispatch_table)) { |
| 208 | PyErr_Format(PyExc_RuntimeError, |
| 209 | "copyreg.dispatch_table should be a dict, not %.200s", |
| 210 | Py_TYPE(st->dispatch_table)->tp_name); |
| 211 | goto error; |
| 212 | } |
| 213 | st->extension_registry = \ |
| 214 | PyObject_GetAttrString(copyreg, "_extension_registry"); |
| 215 | if (!st->extension_registry) |
| 216 | goto error; |
| 217 | if (!PyDict_CheckExact(st->extension_registry)) { |
| 218 | PyErr_Format(PyExc_RuntimeError, |
| 219 | "copyreg._extension_registry should be a dict, " |
| 220 | "not %.200s", Py_TYPE(st->extension_registry)->tp_name); |
| 221 | goto error; |
| 222 | } |
| 223 | st->inverted_registry = \ |
| 224 | PyObject_GetAttrString(copyreg, "_inverted_registry"); |
| 225 | if (!st->inverted_registry) |
| 226 | goto error; |
| 227 | if (!PyDict_CheckExact(st->inverted_registry)) { |
| 228 | PyErr_Format(PyExc_RuntimeError, |
| 229 | "copyreg._inverted_registry should be a dict, " |
| 230 | "not %.200s", Py_TYPE(st->inverted_registry)->tp_name); |
| 231 | goto error; |
| 232 | } |
| 233 | st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache"); |
| 234 | if (!st->extension_cache) |
| 235 | goto error; |
| 236 | if (!PyDict_CheckExact(st->extension_cache)) { |
| 237 | PyErr_Format(PyExc_RuntimeError, |
| 238 | "copyreg._extension_cache should be a dict, " |
| 239 | "not %.200s", Py_TYPE(st->extension_cache)->tp_name); |
| 240 | goto error; |
| 241 | } |
| 242 | Py_CLEAR(copyreg); |
| 243 | |
| 244 | /* Load the 2.x -> 3.x stdlib module mapping tables */ |
| 245 | compat_pickle = PyImport_ImportModule("_compat_pickle"); |
| 246 | if (!compat_pickle) |
| 247 | goto error; |
| 248 | st->name_mapping_2to3 = \ |
| 249 | PyObject_GetAttrString(compat_pickle, "NAME_MAPPING"); |
| 250 | if (!st->name_mapping_2to3) |
| 251 | goto error; |
| 252 | if (!PyDict_CheckExact(st->name_mapping_2to3)) { |
| 253 | PyErr_Format(PyExc_RuntimeError, |
| 254 | "_compat_pickle.NAME_MAPPING should be a dict, not %.200s", |
| 255 | Py_TYPE(st->name_mapping_2to3)->tp_name); |
| 256 | goto error; |
| 257 | } |
| 258 | st->import_mapping_2to3 = \ |
| 259 | PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING"); |
| 260 | if (!st->import_mapping_2to3) |
| 261 | goto error; |
| 262 | if (!PyDict_CheckExact(st->import_mapping_2to3)) { |
| 263 | PyErr_Format(PyExc_RuntimeError, |
| 264 | "_compat_pickle.IMPORT_MAPPING should be a dict, " |
| 265 | "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name); |
| 266 | goto error; |
| 267 | } |
| 268 | /* ... and the 3.x -> 2.x mapping tables */ |
| 269 | st->name_mapping_3to2 = \ |
| 270 | PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING"); |
| 271 | if (!st->name_mapping_3to2) |
| 272 | goto error; |
| 273 | if (!PyDict_CheckExact(st->name_mapping_3to2)) { |
| 274 | PyErr_Format(PyExc_RuntimeError, |
| 275 | "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, " |
| 276 | "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name); |
| 277 | goto error; |
| 278 | } |
| 279 | st->import_mapping_3to2 = \ |
| 280 | PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING"); |
| 281 | if (!st->import_mapping_3to2) |
| 282 | goto error; |
| 283 | if (!PyDict_CheckExact(st->import_mapping_3to2)) { |
| 284 | PyErr_Format(PyExc_RuntimeError, |
| 285 | "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, " |
| 286 | "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name); |
| 287 | goto error; |
| 288 | } |
| 289 | Py_CLEAR(compat_pickle); |
| 290 | |
| 291 | codecs = PyImport_ImportModule("codecs"); |
| 292 | if (codecs == NULL) |
| 293 | goto error; |
| 294 | st->codecs_encode = PyObject_GetAttrString(codecs, "encode"); |
| 295 | if (st->codecs_encode == NULL) { |
| 296 | goto error; |
| 297 | } |
| 298 | if (!PyCallable_Check(st->codecs_encode)) { |
| 299 | PyErr_Format(PyExc_RuntimeError, |
| 300 | "codecs.encode should be a callable, not %.200s", |
| 301 | Py_TYPE(st->codecs_encode)->tp_name); |
| 302 | goto error; |
| 303 | } |
| 304 | Py_CLEAR(codecs); |
| 305 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 306 | return 0; |
| 307 | |
| 308 | error: |
| 309 | Py_CLEAR(copyreg); |
| 310 | Py_CLEAR(compat_pickle); |
| 311 | Py_CLEAR(codecs); |
| 312 | _Pickle_ClearState(st); |
| 313 | return -1; |
| 314 | } |
| 315 | |
| 316 | /* Helper for calling a function with a single argument quickly. |
| 317 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 318 | This function steals the reference of the given argument. */ |
| 319 | static PyObject * |
| 320 | _Pickle_FastCall(PyObject *func, PyObject *obj) |
| 321 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 322 | PyObject *result; |
Alexandre Vassalotti | b13e6bc | 2013-11-28 14:56:09 -0800 | [diff] [blame] | 323 | PyObject *arg_tuple = PyTuple_New(1); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 324 | |
Alexandre Vassalotti | b13e6bc | 2013-11-28 14:56:09 -0800 | [diff] [blame] | 325 | /* Note: this function used to reuse the argument tuple. This used to give |
| 326 | a slight performance boost with older pickle implementations where many |
| 327 | unbuffered reads occurred (thus needing many function calls). |
| 328 | |
| 329 | However, this optimization was removed because it was too complicated |
| 330 | to get right. It abused the C API for tuples to mutate them which led |
| 331 | to subtle reference counting and concurrency bugs. Furthermore, the |
| 332 | introduction of protocol 4 and the prefetching optimization via peek() |
| 333 | significantly reduced the number of function calls we do. Thus, the |
| 334 | benefits became marginal at best. */ |
| 335 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 336 | if (arg_tuple == NULL) { |
Alexandre Vassalotti | b13e6bc | 2013-11-28 14:56:09 -0800 | [diff] [blame] | 337 | Py_DECREF(obj); |
| 338 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 339 | } |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 340 | PyTuple_SET_ITEM(arg_tuple, 0, obj); |
| 341 | result = PyObject_Call(func, arg_tuple, NULL); |
Alexandre Vassalotti | b13e6bc | 2013-11-28 14:56:09 -0800 | [diff] [blame] | 342 | Py_CLEAR(arg_tuple); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 343 | return result; |
| 344 | } |
| 345 | |
| 346 | /*************************************************************************/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 347 | |
| 348 | static int |
| 349 | stack_underflow(void) |
| 350 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 351 | PickleState *st = _Pickle_GetGlobalState(); |
| 352 | PyErr_SetString(st->UnpicklingError, "unpickling stack underflow"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 353 | return -1; |
| 354 | } |
| 355 | |
| 356 | /* Internal data type used as the unpickling stack. */ |
| 357 | typedef struct { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 358 | PyObject_VAR_HEAD |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 359 | PyObject **data; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 360 | Py_ssize_t allocated; /* number of slots in data allocated */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 361 | } Pdata; |
| 362 | |
| 363 | static void |
| 364 | Pdata_dealloc(Pdata *self) |
| 365 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 366 | Py_ssize_t i = Py_SIZE(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 367 | while (--i >= 0) { |
| 368 | Py_DECREF(self->data[i]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 369 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 370 | PyMem_FREE(self->data); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 371 | PyObject_Del(self); |
| 372 | } |
| 373 | |
| 374 | static PyTypeObject Pdata_Type = { |
| 375 | PyVarObject_HEAD_INIT(NULL, 0) |
| 376 | "_pickle.Pdata", /*tp_name*/ |
| 377 | sizeof(Pdata), /*tp_basicsize*/ |
| 378 | 0, /*tp_itemsize*/ |
| 379 | (destructor)Pdata_dealloc, /*tp_dealloc*/ |
| 380 | }; |
| 381 | |
| 382 | static PyObject * |
| 383 | Pdata_New(void) |
| 384 | { |
| 385 | Pdata *self; |
| 386 | |
| 387 | if (!(self = PyObject_New(Pdata, &Pdata_Type))) |
| 388 | return NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 389 | Py_SIZE(self) = 0; |
| 390 | self->allocated = 8; |
| 391 | self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 392 | if (self->data) |
| 393 | return (PyObject *)self; |
| 394 | Py_DECREF(self); |
| 395 | return PyErr_NoMemory(); |
| 396 | } |
| 397 | |
| 398 | |
| 399 | /* Retain only the initial clearto items. If clearto >= the current |
| 400 | * number of items, this is a (non-erroneous) NOP. |
| 401 | */ |
| 402 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 403 | Pdata_clear(Pdata *self, Py_ssize_t clearto) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 404 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 405 | Py_ssize_t i = Py_SIZE(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 406 | |
| 407 | if (clearto < 0) |
| 408 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 409 | if (clearto >= i) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 410 | return 0; |
| 411 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 412 | while (--i >= clearto) { |
| 413 | Py_CLEAR(self->data[i]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 414 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 415 | Py_SIZE(self) = clearto; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | static int |
| 420 | Pdata_grow(Pdata *self) |
| 421 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 422 | PyObject **data = self->data; |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 423 | size_t allocated = (size_t)self->allocated; |
| 424 | size_t new_allocated; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 425 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 426 | new_allocated = (allocated >> 3) + 6; |
| 427 | /* check for integer overflow */ |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 428 | if (new_allocated > (size_t)PY_SSIZE_T_MAX - allocated) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 429 | goto nomemory; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 430 | new_allocated += allocated; |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 431 | if (new_allocated > ((size_t)PY_SSIZE_T_MAX / sizeof(PyObject *))) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 432 | goto nomemory; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 433 | data = PyMem_REALLOC(data, new_allocated * sizeof(PyObject *)); |
| 434 | if (data == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 435 | goto nomemory; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 436 | |
| 437 | self->data = data; |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 438 | self->allocated = (Py_ssize_t)new_allocated; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 439 | return 0; |
| 440 | |
| 441 | nomemory: |
| 442 | PyErr_NoMemory(); |
| 443 | return -1; |
| 444 | } |
| 445 | |
| 446 | /* D is a Pdata*. Pop the topmost element and store it into V, which |
| 447 | * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError |
| 448 | * is raised and V is set to NULL. |
| 449 | */ |
| 450 | static PyObject * |
| 451 | Pdata_pop(Pdata *self) |
| 452 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 453 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 454 | if (Py_SIZE(self) == 0) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 455 | PyErr_SetString(st->UnpicklingError, "bad pickle data"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 456 | return NULL; |
| 457 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 458 | return self->data[--Py_SIZE(self)]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 459 | } |
| 460 | #define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0) |
| 461 | |
| 462 | static int |
| 463 | Pdata_push(Pdata *self, PyObject *obj) |
| 464 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 465 | if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 466 | return -1; |
| 467 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 468 | self->data[Py_SIZE(self)++] = obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | /* Push an object on stack, transferring its ownership to the stack. */ |
| 473 | #define PDATA_PUSH(D, O, ER) do { \ |
| 474 | if (Pdata_push((D), (O)) < 0) return (ER); } while(0) |
| 475 | |
| 476 | /* Push an object on stack, adding a new reference to the object. */ |
| 477 | #define PDATA_APPEND(D, O, ER) do { \ |
| 478 | Py_INCREF((O)); \ |
| 479 | if (Pdata_push((D), (O)) < 0) return (ER); } while(0) |
| 480 | |
| 481 | static PyObject * |
| 482 | Pdata_poptuple(Pdata *self, Py_ssize_t start) |
| 483 | { |
| 484 | PyObject *tuple; |
| 485 | Py_ssize_t len, i, j; |
| 486 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 487 | len = Py_SIZE(self) - start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 488 | tuple = PyTuple_New(len); |
| 489 | if (tuple == NULL) |
| 490 | return NULL; |
| 491 | for (i = start, j = 0; j < len; i++, j++) |
| 492 | PyTuple_SET_ITEM(tuple, j, self->data[i]); |
| 493 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 494 | Py_SIZE(self) = start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 495 | return tuple; |
| 496 | } |
| 497 | |
| 498 | static PyObject * |
| 499 | Pdata_poplist(Pdata *self, Py_ssize_t start) |
| 500 | { |
| 501 | PyObject *list; |
| 502 | Py_ssize_t len, i, j; |
| 503 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 504 | len = Py_SIZE(self) - start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 505 | list = PyList_New(len); |
| 506 | if (list == NULL) |
| 507 | return NULL; |
| 508 | for (i = start, j = 0; j < len; i++, j++) |
| 509 | PyList_SET_ITEM(list, j, self->data[i]); |
| 510 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 511 | Py_SIZE(self) = start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 512 | return list; |
| 513 | } |
| 514 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 515 | typedef struct { |
| 516 | PyObject *me_key; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 517 | Py_ssize_t me_value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 518 | } PyMemoEntry; |
| 519 | |
| 520 | typedef struct { |
| 521 | Py_ssize_t mt_mask; |
| 522 | Py_ssize_t mt_used; |
| 523 | Py_ssize_t mt_allocated; |
| 524 | PyMemoEntry *mt_table; |
| 525 | } PyMemoTable; |
| 526 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 527 | typedef struct PicklerObject { |
| 528 | PyObject_HEAD |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 529 | PyMemoTable *memo; /* Memo table, keep track of the seen |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 530 | objects to support self-referential objects |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 531 | pickling. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 532 | PyObject *pers_func; /* persistent_id() method, can be NULL */ |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 533 | PyObject *dispatch_table; /* private dispatch_table, can be NULL */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 534 | |
| 535 | PyObject *write; /* write() method of the output stream. */ |
| 536 | PyObject *output_buffer; /* Write into a local bytearray buffer before |
| 537 | flushing to the stream. */ |
| 538 | Py_ssize_t output_len; /* Length of output_buffer. */ |
| 539 | Py_ssize_t max_output_len; /* Allocation size of output_buffer. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 540 | int proto; /* Pickle protocol number, >= 0 */ |
| 541 | int bin; /* Boolean, true if proto > 0 */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 542 | int framing; /* True when framing is enabled, proto >= 4 */ |
| 543 | Py_ssize_t frame_start; /* Position in output_buffer where the |
| 544 | where the current frame begins. -1 if there |
| 545 | is no frame currently open. */ |
| 546 | |
| 547 | Py_ssize_t buf_size; /* Size of the current buffered pickle data */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 548 | int fast; /* Enable fast mode if set to a true value. |
| 549 | The fast mode disable the usage of memo, |
| 550 | therefore speeding the pickling process by |
| 551 | not generating superfluous PUT opcodes. It |
| 552 | should not be used if with self-referential |
| 553 | objects. */ |
| 554 | int fast_nesting; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 555 | int fix_imports; /* Indicate whether Pickler should fix |
| 556 | the name of globals for Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 557 | PyObject *fast_memo; |
| 558 | } PicklerObject; |
| 559 | |
| 560 | typedef struct UnpicklerObject { |
| 561 | PyObject_HEAD |
| 562 | Pdata *stack; /* Pickle data stack, store unpickled objects. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 563 | |
| 564 | /* The unpickler memo is just an array of PyObject *s. Using a dict |
| 565 | is unnecessary, since the keys are contiguous ints. */ |
| 566 | PyObject **memo; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 567 | Py_ssize_t memo_size; /* Capacity of the memo array */ |
| 568 | Py_ssize_t memo_len; /* Number of objects in the memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 569 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 570 | PyObject *pers_func; /* persistent_load() method, can be NULL. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 571 | |
| 572 | Py_buffer buffer; |
| 573 | char *input_buffer; |
| 574 | char *input_line; |
| 575 | Py_ssize_t input_len; |
| 576 | Py_ssize_t next_read_idx; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 577 | Py_ssize_t prefetched_idx; /* index of first prefetched byte */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 578 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 579 | PyObject *read; /* read() method of the input stream. */ |
| 580 | PyObject *readline; /* readline() method of the input stream. */ |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 581 | PyObject *peek; /* peek() method of the input stream, or NULL */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 582 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 583 | char *encoding; /* Name of the encoding to be used for |
| 584 | decoding strings pickled using Python |
| 585 | 2.x. The default value is "ASCII" */ |
| 586 | char *errors; /* Name of errors handling scheme to used when |
| 587 | decoding strings. The default value is |
| 588 | "strict". */ |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 589 | Py_ssize_t *marks; /* Mark stack, used for unpickling container |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 590 | objects. */ |
| 591 | Py_ssize_t num_marks; /* Number of marks in the mark stack. */ |
| 592 | Py_ssize_t marks_size; /* Current allocated size of the mark stack. */ |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 593 | int proto; /* Protocol of the pickle loaded. */ |
| 594 | int fix_imports; /* Indicate whether Unpickler should fix |
| 595 | the name of globals pickled by Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 596 | } UnpicklerObject; |
| 597 | |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 598 | typedef struct { |
| 599 | PyObject_HEAD |
| 600 | PicklerObject *pickler; /* Pickler whose memo table we're proxying. */ |
| 601 | } PicklerMemoProxyObject; |
| 602 | |
| 603 | typedef struct { |
| 604 | PyObject_HEAD |
| 605 | UnpicklerObject *unpickler; |
| 606 | } UnpicklerMemoProxyObject; |
| 607 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 608 | /* Forward declarations */ |
| 609 | static int save(PicklerObject *, PyObject *, int); |
| 610 | static int save_reduce(PicklerObject *, PyObject *, PyObject *); |
| 611 | static PyTypeObject Pickler_Type; |
| 612 | static PyTypeObject Unpickler_Type; |
| 613 | |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 614 | #include "clinic/_pickle.c.h" |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 615 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 616 | /************************************************************************* |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 617 | A custom hashtable mapping void* to Python ints. This is used by the pickler |
| 618 | 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] | 619 | a bunch of unnecessary object creation. This makes a huge performance |
| 620 | difference. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 621 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 622 | #define MT_MINSIZE 8 |
| 623 | #define PERTURB_SHIFT 5 |
| 624 | |
| 625 | |
| 626 | static PyMemoTable * |
| 627 | PyMemoTable_New(void) |
| 628 | { |
| 629 | PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable)); |
| 630 | if (memo == NULL) { |
| 631 | PyErr_NoMemory(); |
| 632 | return NULL; |
| 633 | } |
| 634 | |
| 635 | memo->mt_used = 0; |
| 636 | memo->mt_allocated = MT_MINSIZE; |
| 637 | memo->mt_mask = MT_MINSIZE - 1; |
| 638 | memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry)); |
| 639 | if (memo->mt_table == NULL) { |
| 640 | PyMem_FREE(memo); |
| 641 | PyErr_NoMemory(); |
| 642 | return NULL; |
| 643 | } |
| 644 | memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry)); |
| 645 | |
| 646 | return memo; |
| 647 | } |
| 648 | |
| 649 | static PyMemoTable * |
| 650 | PyMemoTable_Copy(PyMemoTable *self) |
| 651 | { |
| 652 | Py_ssize_t i; |
| 653 | PyMemoTable *new = PyMemoTable_New(); |
| 654 | if (new == NULL) |
| 655 | return NULL; |
| 656 | |
| 657 | new->mt_used = self->mt_used; |
| 658 | new->mt_allocated = self->mt_allocated; |
| 659 | new->mt_mask = self->mt_mask; |
| 660 | /* The table we get from _New() is probably smaller than we wanted. |
| 661 | Free it and allocate one that's the right size. */ |
| 662 | PyMem_FREE(new->mt_table); |
| 663 | new->mt_table = PyMem_MALLOC(self->mt_allocated * sizeof(PyMemoEntry)); |
| 664 | if (new->mt_table == NULL) { |
| 665 | PyMem_FREE(new); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 666 | PyErr_NoMemory(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 667 | return NULL; |
| 668 | } |
| 669 | for (i = 0; i < self->mt_allocated; i++) { |
| 670 | Py_XINCREF(self->mt_table[i].me_key); |
| 671 | } |
| 672 | memcpy(new->mt_table, self->mt_table, |
| 673 | sizeof(PyMemoEntry) * self->mt_allocated); |
| 674 | |
| 675 | return new; |
| 676 | } |
| 677 | |
| 678 | static Py_ssize_t |
| 679 | PyMemoTable_Size(PyMemoTable *self) |
| 680 | { |
| 681 | return self->mt_used; |
| 682 | } |
| 683 | |
| 684 | static int |
| 685 | PyMemoTable_Clear(PyMemoTable *self) |
| 686 | { |
| 687 | Py_ssize_t i = self->mt_allocated; |
| 688 | |
| 689 | while (--i >= 0) { |
| 690 | Py_XDECREF(self->mt_table[i].me_key); |
| 691 | } |
| 692 | self->mt_used = 0; |
| 693 | memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry)); |
| 694 | return 0; |
| 695 | } |
| 696 | |
| 697 | static void |
| 698 | PyMemoTable_Del(PyMemoTable *self) |
| 699 | { |
| 700 | if (self == NULL) |
| 701 | return; |
| 702 | PyMemoTable_Clear(self); |
| 703 | |
| 704 | PyMem_FREE(self->mt_table); |
| 705 | PyMem_FREE(self); |
| 706 | } |
| 707 | |
| 708 | /* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup() |
| 709 | can be considerably simpler than dictobject.c's lookdict(). */ |
| 710 | static PyMemoEntry * |
| 711 | _PyMemoTable_Lookup(PyMemoTable *self, PyObject *key) |
| 712 | { |
| 713 | size_t i; |
| 714 | size_t perturb; |
| 715 | size_t mask = (size_t)self->mt_mask; |
| 716 | PyMemoEntry *table = self->mt_table; |
| 717 | PyMemoEntry *entry; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 718 | Py_hash_t hash = (Py_hash_t)key >> 3; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 719 | |
| 720 | i = hash & mask; |
| 721 | entry = &table[i]; |
| 722 | if (entry->me_key == NULL || entry->me_key == key) |
| 723 | return entry; |
| 724 | |
| 725 | for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
| 726 | i = (i << 2) + i + perturb + 1; |
| 727 | entry = &table[i & mask]; |
| 728 | if (entry->me_key == NULL || entry->me_key == key) |
| 729 | return entry; |
| 730 | } |
| 731 | assert(0); /* Never reached */ |
| 732 | return NULL; |
| 733 | } |
| 734 | |
| 735 | /* Returns -1 on failure, 0 on success. */ |
| 736 | static int |
| 737 | _PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size) |
| 738 | { |
| 739 | PyMemoEntry *oldtable = NULL; |
| 740 | PyMemoEntry *oldentry, *newentry; |
| 741 | Py_ssize_t new_size = MT_MINSIZE; |
| 742 | Py_ssize_t to_process; |
| 743 | |
| 744 | assert(min_size > 0); |
| 745 | |
| 746 | /* Find the smallest valid table size >= min_size. */ |
| 747 | while (new_size < min_size && new_size > 0) |
| 748 | new_size <<= 1; |
| 749 | if (new_size <= 0) { |
| 750 | PyErr_NoMemory(); |
| 751 | return -1; |
| 752 | } |
| 753 | /* new_size needs to be a power of two. */ |
| 754 | assert((new_size & (new_size - 1)) == 0); |
| 755 | |
| 756 | /* Allocate new table. */ |
| 757 | oldtable = self->mt_table; |
| 758 | self->mt_table = PyMem_MALLOC(new_size * sizeof(PyMemoEntry)); |
| 759 | if (self->mt_table == NULL) { |
Victor Stinner | 8ca72e2 | 2013-07-12 00:53:26 +0200 | [diff] [blame] | 760 | self->mt_table = oldtable; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 761 | PyErr_NoMemory(); |
| 762 | return -1; |
| 763 | } |
| 764 | self->mt_allocated = new_size; |
| 765 | self->mt_mask = new_size - 1; |
| 766 | memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size); |
| 767 | |
| 768 | /* Copy entries from the old table. */ |
| 769 | to_process = self->mt_used; |
| 770 | for (oldentry = oldtable; to_process > 0; oldentry++) { |
| 771 | if (oldentry->me_key != NULL) { |
| 772 | to_process--; |
| 773 | /* newentry is a pointer to a chunk of the new |
| 774 | mt_table, so we're setting the key:value pair |
| 775 | in-place. */ |
| 776 | newentry = _PyMemoTable_Lookup(self, oldentry->me_key); |
| 777 | newentry->me_key = oldentry->me_key; |
| 778 | newentry->me_value = oldentry->me_value; |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | /* Deallocate the old table. */ |
| 783 | PyMem_FREE(oldtable); |
| 784 | return 0; |
| 785 | } |
| 786 | |
| 787 | /* Returns NULL on failure, a pointer to the value otherwise. */ |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 788 | static Py_ssize_t * |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 789 | PyMemoTable_Get(PyMemoTable *self, PyObject *key) |
| 790 | { |
| 791 | PyMemoEntry *entry = _PyMemoTable_Lookup(self, key); |
| 792 | if (entry->me_key == NULL) |
| 793 | return NULL; |
| 794 | return &entry->me_value; |
| 795 | } |
| 796 | |
| 797 | /* Returns -1 on failure, 0 on success. */ |
| 798 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 799 | PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 800 | { |
| 801 | PyMemoEntry *entry; |
| 802 | |
| 803 | assert(key != NULL); |
| 804 | |
| 805 | entry = _PyMemoTable_Lookup(self, key); |
| 806 | if (entry->me_key != NULL) { |
| 807 | entry->me_value = value; |
| 808 | return 0; |
| 809 | } |
| 810 | Py_INCREF(key); |
| 811 | entry->me_key = key; |
| 812 | entry->me_value = value; |
| 813 | self->mt_used++; |
| 814 | |
| 815 | /* If we added a key, we can safely resize. Otherwise just return! |
| 816 | * If used >= 2/3 size, adjust size. Normally, this quaduples the size. |
| 817 | * |
| 818 | * Quadrupling the size improves average table sparseness |
| 819 | * (reducing collisions) at the cost of some memory. It also halves |
| 820 | * the number of expensive resize operations in a growing memo table. |
| 821 | * |
| 822 | * Very large memo tables (over 50K items) use doubling instead. |
| 823 | * This may help applications with severe memory constraints. |
| 824 | */ |
| 825 | if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2)) |
| 826 | return 0; |
| 827 | return _PyMemoTable_ResizeTable(self, |
| 828 | (self->mt_used > 50000 ? 2 : 4) * self->mt_used); |
| 829 | } |
| 830 | |
| 831 | #undef MT_MINSIZE |
| 832 | #undef PERTURB_SHIFT |
| 833 | |
| 834 | /*************************************************************************/ |
| 835 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 836 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 837 | static int |
| 838 | _Pickler_ClearBuffer(PicklerObject *self) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 839 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 840 | Py_CLEAR(self->output_buffer); |
| 841 | self->output_buffer = |
| 842 | PyBytes_FromStringAndSize(NULL, self->max_output_len); |
| 843 | if (self->output_buffer == NULL) |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 844 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 845 | self->output_len = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 846 | self->frame_start = -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 847 | return 0; |
| 848 | } |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 849 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 850 | static void |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 851 | _write_size64(char *out, size_t value) |
| 852 | { |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 853 | size_t i; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 854 | |
| 855 | assert(sizeof(size_t) <= 8); |
| 856 | |
| 857 | for (i = 0; i < sizeof(size_t); i++) { |
| 858 | out[i] = (unsigned char)((value >> (8 * i)) & 0xff); |
| 859 | } |
| 860 | for (i = sizeof(size_t); i < 8; i++) { |
| 861 | out[i] = 0; |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 862 | } |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | static void |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 866 | _Pickler_WriteFrameHeader(PicklerObject *self, char *qdata, size_t frame_len) |
| 867 | { |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 868 | qdata[0] = FRAME; |
| 869 | _write_size64(qdata + 1, frame_len); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | static int |
| 873 | _Pickler_CommitFrame(PicklerObject *self) |
| 874 | { |
| 875 | size_t frame_len; |
| 876 | char *qdata; |
| 877 | |
| 878 | if (!self->framing || self->frame_start == -1) |
| 879 | return 0; |
| 880 | frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE; |
| 881 | qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start; |
| 882 | _Pickler_WriteFrameHeader(self, qdata, frame_len); |
| 883 | self->frame_start = -1; |
| 884 | return 0; |
| 885 | } |
| 886 | |
| 887 | static int |
| 888 | _Pickler_OpcodeBoundary(PicklerObject *self) |
| 889 | { |
| 890 | Py_ssize_t frame_len; |
| 891 | |
| 892 | if (!self->framing || self->frame_start == -1) |
| 893 | return 0; |
| 894 | frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE; |
| 895 | if (frame_len >= FRAME_SIZE_TARGET) |
| 896 | return _Pickler_CommitFrame(self); |
| 897 | else |
| 898 | return 0; |
| 899 | } |
| 900 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 901 | static PyObject * |
| 902 | _Pickler_GetString(PicklerObject *self) |
| 903 | { |
| 904 | PyObject *output_buffer = self->output_buffer; |
| 905 | |
| 906 | assert(self->output_buffer != NULL); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 907 | |
| 908 | if (_Pickler_CommitFrame(self)) |
| 909 | return NULL; |
| 910 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 911 | self->output_buffer = NULL; |
| 912 | /* Resize down to exact size */ |
| 913 | if (_PyBytes_Resize(&output_buffer, self->output_len) < 0) |
| 914 | return NULL; |
| 915 | return output_buffer; |
| 916 | } |
| 917 | |
| 918 | static int |
| 919 | _Pickler_FlushToFile(PicklerObject *self) |
| 920 | { |
| 921 | PyObject *output, *result; |
| 922 | |
| 923 | assert(self->write != NULL); |
| 924 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 925 | /* This will commit the frame first */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 926 | output = _Pickler_GetString(self); |
| 927 | if (output == NULL) |
| 928 | return -1; |
| 929 | |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 930 | result = _Pickle_FastCall(self->write, output); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 931 | Py_XDECREF(result); |
| 932 | return (result == NULL) ? -1 : 0; |
| 933 | } |
| 934 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 935 | static Py_ssize_t |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 936 | _Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 937 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 938 | Py_ssize_t i, n, required; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 939 | char *buffer; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 940 | int need_new_frame; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 941 | |
| 942 | assert(s != NULL); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 943 | need_new_frame = (self->framing && self->frame_start == -1); |
| 944 | |
| 945 | if (need_new_frame) |
| 946 | n = data_len + FRAME_HEADER_SIZE; |
| 947 | else |
| 948 | n = data_len; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 949 | |
| 950 | required = self->output_len + n; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 951 | if (required > self->max_output_len) { |
| 952 | /* Make place in buffer for the pickle chunk */ |
| 953 | if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) { |
| 954 | PyErr_NoMemory(); |
| 955 | return -1; |
| 956 | } |
| 957 | self->max_output_len = (self->output_len + n) / 2 * 3; |
| 958 | if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0) |
| 959 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 960 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 961 | buffer = PyBytes_AS_STRING(self->output_buffer); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 962 | if (need_new_frame) { |
| 963 | /* Setup new frame */ |
| 964 | Py_ssize_t frame_start = self->output_len; |
| 965 | self->frame_start = frame_start; |
| 966 | for (i = 0; i < FRAME_HEADER_SIZE; i++) { |
| 967 | /* Write an invalid value, for debugging */ |
| 968 | buffer[frame_start + i] = 0xFE; |
| 969 | } |
| 970 | self->output_len += FRAME_HEADER_SIZE; |
| 971 | } |
| 972 | if (data_len < 8) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 973 | /* This is faster than memcpy when the string is short. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 974 | for (i = 0; i < data_len; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 975 | buffer[self->output_len + i] = s[i]; |
| 976 | } |
| 977 | } |
| 978 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 979 | memcpy(buffer + self->output_len, s, data_len); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 980 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 981 | self->output_len += data_len; |
| 982 | return data_len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 983 | } |
| 984 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 985 | static PicklerObject * |
| 986 | _Pickler_New(void) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 987 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 988 | PicklerObject *self; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 989 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 990 | self = PyObject_GC_New(PicklerObject, &Pickler_Type); |
| 991 | if (self == NULL) |
| 992 | return NULL; |
| 993 | |
| 994 | self->pers_func = NULL; |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 995 | self->dispatch_table = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 996 | self->write = NULL; |
| 997 | self->proto = 0; |
| 998 | self->bin = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 999 | self->framing = 0; |
| 1000 | self->frame_start = -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1001 | self->fast = 0; |
| 1002 | self->fast_nesting = 0; |
| 1003 | self->fix_imports = 0; |
| 1004 | self->fast_memo = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1005 | self->max_output_len = WRITE_BUF_SIZE; |
| 1006 | self->output_len = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1007 | |
| 1008 | self->memo = PyMemoTable_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1009 | self->output_buffer = PyBytes_FromStringAndSize(NULL, |
| 1010 | self->max_output_len); |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1011 | |
| 1012 | if (self->memo == NULL || self->output_buffer == NULL) { |
Victor Stinner | c31df04 | 2013-07-12 00:08:59 +0200 | [diff] [blame] | 1013 | Py_DECREF(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1014 | return NULL; |
| 1015 | } |
| 1016 | return self; |
| 1017 | } |
| 1018 | |
| 1019 | static int |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1020 | _Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1021 | { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1022 | long proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1023 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1024 | if (protocol == NULL || protocol == Py_None) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1025 | proto = DEFAULT_PROTOCOL; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1026 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1027 | else { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1028 | proto = PyLong_AsLong(protocol); |
| 1029 | if (proto < 0) { |
| 1030 | if (proto == -1 && PyErr_Occurred()) |
| 1031 | return -1; |
| 1032 | proto = HIGHEST_PROTOCOL; |
| 1033 | } |
| 1034 | else if (proto > HIGHEST_PROTOCOL) { |
| 1035 | PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d", |
| 1036 | HIGHEST_PROTOCOL); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1037 | return -1; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1038 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1039 | } |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1040 | self->proto = (int)proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1041 | self->bin = proto > 0; |
| 1042 | self->fix_imports = fix_imports && proto < 3; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1043 | return 0; |
| 1044 | } |
| 1045 | |
| 1046 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1047 | be called once on a freshly created Pickler. */ |
| 1048 | static int |
| 1049 | _Pickler_SetOutputStream(PicklerObject *self, PyObject *file) |
| 1050 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1051 | _Py_IDENTIFIER(write); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1052 | assert(file != NULL); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1053 | self->write = _PyObject_GetAttrId(file, &PyId_write); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1054 | if (self->write == NULL) { |
| 1055 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1056 | PyErr_SetString(PyExc_TypeError, |
| 1057 | "file must have a 'write' attribute"); |
| 1058 | return -1; |
| 1059 | } |
| 1060 | |
| 1061 | return 0; |
| 1062 | } |
| 1063 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1064 | /* Returns the size of the input on success, -1 on failure. This takes its |
| 1065 | own reference to `input`. */ |
| 1066 | static Py_ssize_t |
| 1067 | _Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input) |
| 1068 | { |
| 1069 | if (self->buffer.buf != NULL) |
| 1070 | PyBuffer_Release(&self->buffer); |
| 1071 | if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0) |
| 1072 | return -1; |
| 1073 | self->input_buffer = self->buffer.buf; |
| 1074 | self->input_len = self->buffer.len; |
| 1075 | self->next_read_idx = 0; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1076 | self->prefetched_idx = self->input_len; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1077 | return self->input_len; |
| 1078 | } |
| 1079 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1080 | static int |
| 1081 | _Unpickler_SkipConsumed(UnpicklerObject *self) |
| 1082 | { |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1083 | Py_ssize_t consumed; |
| 1084 | PyObject *r; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1085 | |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1086 | consumed = self->next_read_idx - self->prefetched_idx; |
| 1087 | if (consumed <= 0) |
| 1088 | return 0; |
| 1089 | |
| 1090 | assert(self->peek); /* otherwise we did something wrong */ |
| 1091 | /* This makes an useless copy... */ |
| 1092 | r = PyObject_CallFunction(self->read, "n", consumed); |
| 1093 | if (r == NULL) |
| 1094 | return -1; |
| 1095 | Py_DECREF(r); |
| 1096 | |
| 1097 | self->prefetched_idx = self->next_read_idx; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1098 | return 0; |
| 1099 | } |
| 1100 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1101 | static const Py_ssize_t READ_WHOLE_LINE = -1; |
| 1102 | |
| 1103 | /* If reading from a file, we need to only pull the bytes we need, since there |
| 1104 | may be multiple pickle objects arranged contiguously in the same input |
| 1105 | buffer. |
| 1106 | |
| 1107 | If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n` |
| 1108 | bytes from the input stream/buffer. |
| 1109 | |
| 1110 | Update the unpickler's input buffer with the newly-read data. Returns -1 on |
| 1111 | failure; on success, returns the number of bytes read from the file. |
| 1112 | |
| 1113 | On success, self->input_len will be 0; this is intentional so that when |
| 1114 | unpickling from a file, the "we've run out of data" code paths will trigger, |
| 1115 | causing the Unpickler to go back to the file for more data. Use the returned |
| 1116 | size to tell you how much data you can process. */ |
| 1117 | static Py_ssize_t |
| 1118 | _Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n) |
| 1119 | { |
| 1120 | PyObject *data; |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1121 | Py_ssize_t read_size; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1122 | |
| 1123 | assert(self->read != NULL); |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1124 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1125 | if (_Unpickler_SkipConsumed(self) < 0) |
| 1126 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1127 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1128 | if (n == READ_WHOLE_LINE) { |
Alexandre Vassalotti | 6bf41e5 | 2013-11-28 15:17:29 -0800 | [diff] [blame] | 1129 | PyObject *empty_tuple = PyTuple_New(0); |
| 1130 | data = PyObject_Call(self->readline, empty_tuple, NULL); |
| 1131 | Py_DECREF(empty_tuple); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1132 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1133 | else { |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1134 | PyObject *len; |
| 1135 | /* Prefetch some data without advancing the file pointer, if possible */ |
| 1136 | if (self->peek && n < PREFETCH) { |
| 1137 | len = PyLong_FromSsize_t(PREFETCH); |
| 1138 | if (len == NULL) |
| 1139 | return -1; |
| 1140 | data = _Pickle_FastCall(self->peek, len); |
| 1141 | if (data == NULL) { |
| 1142 | if (!PyErr_ExceptionMatches(PyExc_NotImplementedError)) |
| 1143 | return -1; |
| 1144 | /* peek() is probably not supported by the given file object */ |
| 1145 | PyErr_Clear(); |
| 1146 | Py_CLEAR(self->peek); |
| 1147 | } |
| 1148 | else { |
| 1149 | read_size = _Unpickler_SetStringInput(self, data); |
| 1150 | Py_DECREF(data); |
| 1151 | self->prefetched_idx = 0; |
| 1152 | if (n <= read_size) |
| 1153 | return n; |
| 1154 | } |
| 1155 | } |
| 1156 | len = PyLong_FromSsize_t(n); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1157 | if (len == NULL) |
| 1158 | return -1; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 1159 | data = _Pickle_FastCall(self->read, len); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1160 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1161 | if (data == NULL) |
| 1162 | return -1; |
| 1163 | |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1164 | read_size = _Unpickler_SetStringInput(self, data); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1165 | Py_DECREF(data); |
| 1166 | return read_size; |
| 1167 | } |
| 1168 | |
| 1169 | /* Read `n` bytes from the unpickler's data source, storing the result in `*s`. |
| 1170 | |
| 1171 | This should be used for all data reads, rather than accessing the unpickler's |
| 1172 | input buffer directly. This method deals correctly with reading from input |
| 1173 | streams, which the input buffer doesn't deal with. |
| 1174 | |
| 1175 | Note that when reading from a file-like object, self->next_read_idx won't |
| 1176 | be updated (it should remain at 0 for the entire unpickling process). You |
| 1177 | should use this function's return value to know how many bytes you can |
| 1178 | consume. |
| 1179 | |
| 1180 | Returns -1 (with an exception set) on failure. On success, return the |
| 1181 | number of chars read. */ |
| 1182 | static Py_ssize_t |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 1183 | _Unpickler_Read(UnpicklerObject *self, char **s, Py_ssize_t n) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1184 | { |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1185 | Py_ssize_t num_read; |
| 1186 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1187 | if (self->next_read_idx + n <= self->input_len) { |
| 1188 | *s = self->input_buffer + self->next_read_idx; |
| 1189 | self->next_read_idx += n; |
| 1190 | return n; |
| 1191 | } |
| 1192 | if (!self->read) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1193 | PyErr_Format(PyExc_EOFError, "Ran out of input"); |
Amaury Forgeot d'Arc | 3e4e72f | 2008-11-11 20:05:06 +0000 | [diff] [blame] | 1194 | return -1; |
| 1195 | } |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1196 | num_read = _Unpickler_ReadFromFile(self, n); |
| 1197 | if (num_read < 0) |
| 1198 | return -1; |
| 1199 | if (num_read < n) { |
| 1200 | PyErr_Format(PyExc_EOFError, "Ran out of input"); |
| 1201 | return -1; |
| 1202 | } |
| 1203 | *s = self->input_buffer; |
| 1204 | self->next_read_idx = n; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1205 | return n; |
| 1206 | } |
| 1207 | |
| 1208 | static Py_ssize_t |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1209 | _Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len, |
| 1210 | char **result) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1211 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1212 | char *input_line = PyMem_Realloc(self->input_line, len + 1); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1213 | if (input_line == NULL) { |
| 1214 | PyErr_NoMemory(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1215 | return -1; |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1216 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1217 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1218 | memcpy(input_line, line, len); |
| 1219 | input_line[len] = '\0'; |
| 1220 | self->input_line = input_line; |
| 1221 | *result = self->input_line; |
| 1222 | return len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1225 | /* Read a line from the input stream/buffer. If we run off the end of the input |
| 1226 | before hitting \n, return the data we found. |
| 1227 | |
| 1228 | Returns the number of chars read, or -1 on failure. */ |
| 1229 | static Py_ssize_t |
| 1230 | _Unpickler_Readline(UnpicklerObject *self, char **result) |
| 1231 | { |
| 1232 | Py_ssize_t i, num_read; |
| 1233 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1234 | for (i = self->next_read_idx; i < self->input_len; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1235 | if (self->input_buffer[i] == '\n') { |
| 1236 | char *line_start = self->input_buffer + self->next_read_idx; |
| 1237 | num_read = i - self->next_read_idx + 1; |
| 1238 | self->next_read_idx = i + 1; |
| 1239 | return _Unpickler_CopyLine(self, line_start, num_read, result); |
| 1240 | } |
| 1241 | } |
| 1242 | if (self->read) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1243 | num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE); |
| 1244 | if (num_read < 0) |
| 1245 | return -1; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1246 | self->next_read_idx = num_read; |
Antoine Pitrou | f6c7a85 | 2011-08-11 21:04:02 +0200 | [diff] [blame] | 1247 | return _Unpickler_CopyLine(self, self->input_buffer, num_read, result); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1248 | } |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1249 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1250 | /* If we get here, we've run off the end of the input string. Return the |
| 1251 | remaining string and let the caller figure it out. */ |
| 1252 | *result = self->input_buffer + self->next_read_idx; |
| 1253 | num_read = i - self->next_read_idx; |
| 1254 | self->next_read_idx = i; |
| 1255 | return num_read; |
| 1256 | } |
| 1257 | |
| 1258 | /* Returns -1 (with an exception set) on failure, 0 on success. The memo array |
| 1259 | will be modified in place. */ |
| 1260 | static int |
| 1261 | _Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size) |
| 1262 | { |
| 1263 | Py_ssize_t i; |
| 1264 | PyObject **memo; |
| 1265 | |
| 1266 | assert(new_size > self->memo_size); |
| 1267 | |
| 1268 | memo = PyMem_REALLOC(self->memo, new_size * sizeof(PyObject *)); |
| 1269 | if (memo == NULL) { |
| 1270 | PyErr_NoMemory(); |
| 1271 | return -1; |
| 1272 | } |
| 1273 | self->memo = memo; |
| 1274 | for (i = self->memo_size; i < new_size; i++) |
| 1275 | self->memo[i] = NULL; |
| 1276 | self->memo_size = new_size; |
| 1277 | return 0; |
| 1278 | } |
| 1279 | |
| 1280 | /* Returns NULL if idx is out of bounds. */ |
| 1281 | static PyObject * |
| 1282 | _Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx) |
| 1283 | { |
| 1284 | if (idx < 0 || idx >= self->memo_size) |
| 1285 | return NULL; |
| 1286 | |
| 1287 | return self->memo[idx]; |
| 1288 | } |
| 1289 | |
| 1290 | /* Returns -1 (with an exception set) on failure, 0 on success. |
| 1291 | This takes its own reference to `value`. */ |
| 1292 | static int |
| 1293 | _Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value) |
| 1294 | { |
| 1295 | PyObject *old_item; |
| 1296 | |
| 1297 | if (idx >= self->memo_size) { |
| 1298 | if (_Unpickler_ResizeMemoList(self, idx * 2) < 0) |
| 1299 | return -1; |
| 1300 | assert(idx < self->memo_size); |
| 1301 | } |
| 1302 | Py_INCREF(value); |
| 1303 | old_item = self->memo[idx]; |
| 1304 | self->memo[idx] = value; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1305 | if (old_item != NULL) { |
| 1306 | Py_DECREF(old_item); |
| 1307 | } |
| 1308 | else { |
| 1309 | self->memo_len++; |
| 1310 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1311 | return 0; |
| 1312 | } |
| 1313 | |
| 1314 | static PyObject ** |
| 1315 | _Unpickler_NewMemo(Py_ssize_t new_size) |
| 1316 | { |
| 1317 | PyObject **memo = PyMem_MALLOC(new_size * sizeof(PyObject *)); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1318 | if (memo == NULL) { |
| 1319 | PyErr_NoMemory(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1320 | return NULL; |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1321 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1322 | memset(memo, 0, new_size * sizeof(PyObject *)); |
| 1323 | return memo; |
| 1324 | } |
| 1325 | |
| 1326 | /* Free the unpickler's memo, taking care to decref any items left in it. */ |
| 1327 | static void |
| 1328 | _Unpickler_MemoCleanup(UnpicklerObject *self) |
| 1329 | { |
| 1330 | Py_ssize_t i; |
| 1331 | PyObject **memo = self->memo; |
| 1332 | |
| 1333 | if (self->memo == NULL) |
| 1334 | return; |
| 1335 | self->memo = NULL; |
| 1336 | i = self->memo_size; |
| 1337 | while (--i >= 0) { |
| 1338 | Py_XDECREF(memo[i]); |
| 1339 | } |
| 1340 | PyMem_FREE(memo); |
| 1341 | } |
| 1342 | |
| 1343 | static UnpicklerObject * |
| 1344 | _Unpickler_New(void) |
| 1345 | { |
| 1346 | UnpicklerObject *self; |
| 1347 | |
| 1348 | self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type); |
| 1349 | if (self == NULL) |
| 1350 | return NULL; |
| 1351 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1352 | self->pers_func = NULL; |
| 1353 | self->input_buffer = NULL; |
| 1354 | self->input_line = NULL; |
| 1355 | self->input_len = 0; |
| 1356 | self->next_read_idx = 0; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1357 | self->prefetched_idx = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1358 | self->read = NULL; |
| 1359 | self->readline = NULL; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1360 | self->peek = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1361 | self->encoding = NULL; |
| 1362 | self->errors = NULL; |
| 1363 | self->marks = NULL; |
| 1364 | self->num_marks = 0; |
| 1365 | self->marks_size = 0; |
| 1366 | self->proto = 0; |
| 1367 | self->fix_imports = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1368 | memset(&self->buffer, 0, sizeof(Py_buffer)); |
| 1369 | self->memo_size = 32; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1370 | self->memo_len = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1371 | self->memo = _Unpickler_NewMemo(self->memo_size); |
| 1372 | self->stack = (Pdata *)Pdata_New(); |
| 1373 | |
| 1374 | if (self->memo == NULL || self->stack == NULL) { |
| 1375 | Py_DECREF(self); |
| 1376 | return NULL; |
| 1377 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1378 | |
| 1379 | return self; |
| 1380 | } |
| 1381 | |
| 1382 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1383 | be called once on a freshly created Pickler. */ |
| 1384 | static int |
| 1385 | _Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file) |
| 1386 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1387 | _Py_IDENTIFIER(peek); |
| 1388 | _Py_IDENTIFIER(read); |
| 1389 | _Py_IDENTIFIER(readline); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1390 | |
| 1391 | self->peek = _PyObject_GetAttrId(file, &PyId_peek); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1392 | if (self->peek == NULL) { |
| 1393 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1394 | PyErr_Clear(); |
| 1395 | else |
| 1396 | return -1; |
| 1397 | } |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1398 | self->read = _PyObject_GetAttrId(file, &PyId_read); |
| 1399 | self->readline = _PyObject_GetAttrId(file, &PyId_readline); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1400 | if (self->readline == NULL || self->read == NULL) { |
| 1401 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1402 | PyErr_SetString(PyExc_TypeError, |
| 1403 | "file must have 'read' and 'readline' attributes"); |
| 1404 | Py_CLEAR(self->read); |
| 1405 | Py_CLEAR(self->readline); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1406 | Py_CLEAR(self->peek); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1407 | return -1; |
| 1408 | } |
| 1409 | return 0; |
| 1410 | } |
| 1411 | |
| 1412 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1413 | be called once on a freshly created Pickler. */ |
| 1414 | static int |
| 1415 | _Unpickler_SetInputEncoding(UnpicklerObject *self, |
| 1416 | const char *encoding, |
| 1417 | const char *errors) |
| 1418 | { |
| 1419 | if (encoding == NULL) |
| 1420 | encoding = "ASCII"; |
| 1421 | if (errors == NULL) |
| 1422 | errors = "strict"; |
| 1423 | |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 1424 | self->encoding = _PyMem_Strdup(encoding); |
| 1425 | self->errors = _PyMem_Strdup(errors); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1426 | if (self->encoding == NULL || self->errors == NULL) { |
| 1427 | PyErr_NoMemory(); |
| 1428 | return -1; |
| 1429 | } |
| 1430 | return 0; |
| 1431 | } |
| 1432 | |
| 1433 | /* Generate a GET opcode for an object stored in the memo. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1434 | static int |
| 1435 | memo_get(PicklerObject *self, PyObject *key) |
| 1436 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1437 | Py_ssize_t *value; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1438 | char pdata[30]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1439 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1440 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1441 | value = PyMemoTable_Get(self->memo, key); |
| 1442 | if (value == NULL) { |
| 1443 | PyErr_SetObject(PyExc_KeyError, key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1444 | return -1; |
| 1445 | } |
| 1446 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1447 | if (!self->bin) { |
| 1448 | pdata[0] = GET; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1449 | PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, |
| 1450 | "%" PY_FORMAT_SIZE_T "d\n", *value); |
| 1451 | len = strlen(pdata); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1452 | } |
| 1453 | else { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1454 | if (*value < 256) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1455 | pdata[0] = BINGET; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1456 | pdata[1] = (unsigned char)(*value & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1457 | len = 2; |
| 1458 | } |
Serhiy Storchaka | 67c719b | 2014-09-05 10:10:23 +0300 | [diff] [blame] | 1459 | else if ((size_t)*value <= 0xffffffffUL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1460 | pdata[0] = LONG_BINGET; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1461 | pdata[1] = (unsigned char)(*value & 0xff); |
| 1462 | pdata[2] = (unsigned char)((*value >> 8) & 0xff); |
| 1463 | pdata[3] = (unsigned char)((*value >> 16) & 0xff); |
| 1464 | pdata[4] = (unsigned char)((*value >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1465 | len = 5; |
| 1466 | } |
| 1467 | else { /* unlikely */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1468 | PickleState *st = _Pickle_GetGlobalState(); |
| 1469 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1470 | "memo id too large for LONG_BINGET"); |
| 1471 | return -1; |
| 1472 | } |
| 1473 | } |
| 1474 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1475 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1476 | return -1; |
| 1477 | |
| 1478 | return 0; |
| 1479 | } |
| 1480 | |
| 1481 | /* Store an object in the memo, assign it a new unique ID based on the number |
| 1482 | of objects currently stored in the memo and generate a PUT opcode. */ |
| 1483 | static int |
| 1484 | memo_put(PicklerObject *self, PyObject *obj) |
| 1485 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1486 | char pdata[30]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1487 | Py_ssize_t len; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1488 | Py_ssize_t idx; |
| 1489 | |
| 1490 | const char memoize_op = MEMOIZE; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1491 | |
| 1492 | if (self->fast) |
| 1493 | return 0; |
| 1494 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1495 | idx = PyMemoTable_Size(self->memo); |
| 1496 | if (PyMemoTable_Set(self->memo, obj, idx) < 0) |
| 1497 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1498 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1499 | if (self->proto >= 4) { |
| 1500 | if (_Pickler_Write(self, &memoize_op, 1) < 0) |
| 1501 | return -1; |
| 1502 | return 0; |
| 1503 | } |
| 1504 | else if (!self->bin) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1505 | pdata[0] = PUT; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1506 | PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1507 | "%" PY_FORMAT_SIZE_T "d\n", idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1508 | len = strlen(pdata); |
| 1509 | } |
| 1510 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1511 | if (idx < 256) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1512 | pdata[0] = BINPUT; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1513 | pdata[1] = (unsigned char)idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1514 | len = 2; |
| 1515 | } |
Serhiy Storchaka | 67c719b | 2014-09-05 10:10:23 +0300 | [diff] [blame] | 1516 | else if ((size_t)idx <= 0xffffffffUL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1517 | pdata[0] = LONG_BINPUT; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1518 | pdata[1] = (unsigned char)(idx & 0xff); |
| 1519 | pdata[2] = (unsigned char)((idx >> 8) & 0xff); |
| 1520 | pdata[3] = (unsigned char)((idx >> 16) & 0xff); |
| 1521 | pdata[4] = (unsigned char)((idx >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1522 | len = 5; |
| 1523 | } |
| 1524 | else { /* unlikely */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1525 | PickleState *st = _Pickle_GetGlobalState(); |
| 1526 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1527 | "memo id too large for LONG_BINPUT"); |
| 1528 | return -1; |
| 1529 | } |
| 1530 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1531 | if (_Pickler_Write(self, pdata, len) < 0) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1532 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1533 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1534 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1535 | } |
| 1536 | |
| 1537 | static PyObject * |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1538 | get_dotted_path(PyObject *obj, PyObject *name, int allow_qualname) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1539 | _Py_static_string(PyId_dot, "."); |
| 1540 | _Py_static_string(PyId_locals, "<locals>"); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1541 | PyObject *dotted_path; |
| 1542 | Py_ssize_t i, n; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1543 | |
| 1544 | dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1545 | if (dotted_path == NULL) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1546 | return NULL; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1547 | n = PyList_GET_SIZE(dotted_path); |
| 1548 | assert(n >= 1); |
| 1549 | if (!allow_qualname && n > 1) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1550 | PyErr_Format(PyExc_AttributeError, |
| 1551 | "Can't get qualified attribute %R on %R;" |
| 1552 | "use protocols >= 4 to enable support", |
| 1553 | name, obj); |
| 1554 | Py_DECREF(dotted_path); |
| 1555 | return NULL; |
| 1556 | } |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1557 | for (i = 0; i < n; i++) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1558 | PyObject *subpath = PyList_GET_ITEM(dotted_path, i); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1559 | PyObject *result = PyUnicode_RichCompare( |
| 1560 | subpath, _PyUnicode_FromId(&PyId_locals), Py_EQ); |
| 1561 | int is_equal = (result == Py_True); |
| 1562 | assert(PyBool_Check(result)); |
| 1563 | Py_DECREF(result); |
| 1564 | if (is_equal) { |
| 1565 | PyErr_Format(PyExc_AttributeError, |
| 1566 | "Can't get local attribute %R on %R", name, obj); |
| 1567 | Py_DECREF(dotted_path); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1568 | return NULL; |
| 1569 | } |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1570 | } |
| 1571 | return dotted_path; |
| 1572 | } |
| 1573 | |
| 1574 | static PyObject * |
| 1575 | get_deep_attribute(PyObject *obj, PyObject *names) |
| 1576 | { |
| 1577 | Py_ssize_t i, n; |
| 1578 | |
| 1579 | assert(PyList_CheckExact(names)); |
| 1580 | Py_INCREF(obj); |
| 1581 | n = PyList_GET_SIZE(names); |
| 1582 | for (i = 0; i < n; i++) { |
| 1583 | PyObject *name = PyList_GET_ITEM(names, i); |
| 1584 | PyObject *tmp; |
| 1585 | tmp = PyObject_GetAttr(obj, name); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1586 | Py_DECREF(obj); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1587 | if (tmp == NULL) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1588 | return NULL; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1589 | obj = tmp; |
| 1590 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1591 | return obj; |
| 1592 | } |
| 1593 | |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1594 | static void |
| 1595 | reformat_attribute_error(PyObject *obj, PyObject *name) |
| 1596 | { |
| 1597 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 1598 | PyErr_Clear(); |
| 1599 | PyErr_Format(PyExc_AttributeError, |
| 1600 | "Can't get attribute %R on %R", name, obj); |
| 1601 | } |
| 1602 | } |
| 1603 | |
| 1604 | |
| 1605 | static PyObject * |
| 1606 | getattribute(PyObject *obj, PyObject *name, int allow_qualname) |
| 1607 | { |
| 1608 | PyObject *dotted_path, *attr; |
| 1609 | |
| 1610 | dotted_path = get_dotted_path(obj, name, allow_qualname); |
| 1611 | if (dotted_path == NULL) |
| 1612 | return NULL; |
| 1613 | attr = get_deep_attribute(obj, dotted_path); |
| 1614 | Py_DECREF(dotted_path); |
| 1615 | if (attr == NULL) |
| 1616 | reformat_attribute_error(obj, name); |
| 1617 | return attr; |
| 1618 | } |
| 1619 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1620 | static PyObject * |
| 1621 | whichmodule(PyObject *global, PyObject *global_name, int allow_qualname) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1622 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1623 | PyObject *module_name; |
| 1624 | PyObject *modules_dict; |
| 1625 | PyObject *module; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1626 | Py_ssize_t i; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1627 | _Py_IDENTIFIER(__module__); |
| 1628 | _Py_IDENTIFIER(modules); |
| 1629 | _Py_IDENTIFIER(__main__); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1630 | PyObject *dotted_path; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1631 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1632 | module_name = _PyObject_GetAttrId(global, &PyId___module__); |
| 1633 | |
| 1634 | if (module_name == NULL) { |
| 1635 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1636 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1637 | PyErr_Clear(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1638 | } |
| 1639 | else { |
| 1640 | /* In some rare cases (e.g., bound methods of extension types), |
| 1641 | __module__ can be None. If it is so, then search sys.modules for |
| 1642 | the module of global. */ |
| 1643 | if (module_name != Py_None) |
| 1644 | return module_name; |
| 1645 | Py_CLEAR(module_name); |
| 1646 | } |
| 1647 | assert(module_name == NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1648 | |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1649 | /* Fallback on walking sys.modules */ |
Victor Stinner | bb52020 | 2013-11-06 22:40:41 +0100 | [diff] [blame] | 1650 | modules_dict = _PySys_GetObjectId(&PyId_modules); |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 1651 | if (modules_dict == NULL) { |
| 1652 | PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1653 | return NULL; |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 1654 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1655 | |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1656 | dotted_path = get_dotted_path(module, global_name, allow_qualname); |
| 1657 | if (dotted_path == NULL) |
| 1658 | return NULL; |
| 1659 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1660 | i = 0; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1661 | while (PyDict_Next(modules_dict, &i, &module_name, &module)) { |
| 1662 | PyObject *candidate; |
| 1663 | if (PyUnicode_Check(module_name) && |
| 1664 | !PyUnicode_CompareWithASCIIString(module_name, "__main__")) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1665 | continue; |
| 1666 | if (module == Py_None) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1667 | continue; |
| 1668 | |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1669 | candidate = get_deep_attribute(module, dotted_path); |
| 1670 | if (candidate == NULL) { |
| 1671 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 1672 | Py_DECREF(dotted_path); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1673 | return NULL; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1674 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1675 | PyErr_Clear(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1676 | continue; |
| 1677 | } |
| 1678 | |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1679 | if (candidate == global) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1680 | Py_INCREF(module_name); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1681 | Py_DECREF(dotted_path); |
| 1682 | Py_DECREF(candidate); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1683 | return module_name; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1684 | } |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1685 | Py_DECREF(candidate); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1686 | } |
| 1687 | |
| 1688 | /* If no module is found, use __main__. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1689 | module_name = _PyUnicode_FromId(&PyId___main__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1690 | Py_INCREF(module_name); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1691 | Py_DECREF(dotted_path); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1692 | return module_name; |
| 1693 | } |
| 1694 | |
| 1695 | /* fast_save_enter() and fast_save_leave() are guards against recursive |
| 1696 | objects when Pickler is used with the "fast mode" (i.e., with object |
| 1697 | memoization disabled). If the nesting of a list or dict object exceed |
| 1698 | FAST_NESTING_LIMIT, these guards will start keeping an internal |
| 1699 | reference to the seen list or dict objects and check whether these objects |
| 1700 | are recursive. These are not strictly necessary, since save() has a |
| 1701 | hard-coded recursion limit, but they give a nicer error message than the |
| 1702 | typical RuntimeError. */ |
| 1703 | static int |
| 1704 | fast_save_enter(PicklerObject *self, PyObject *obj) |
| 1705 | { |
| 1706 | /* if fast_nesting < 0, we're doing an error exit. */ |
| 1707 | if (++self->fast_nesting >= FAST_NESTING_LIMIT) { |
| 1708 | PyObject *key = NULL; |
| 1709 | if (self->fast_memo == NULL) { |
| 1710 | self->fast_memo = PyDict_New(); |
| 1711 | if (self->fast_memo == NULL) { |
| 1712 | self->fast_nesting = -1; |
| 1713 | return 0; |
| 1714 | } |
| 1715 | } |
| 1716 | key = PyLong_FromVoidPtr(obj); |
| 1717 | if (key == NULL) |
| 1718 | return 0; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 1719 | if (PyDict_GetItemWithError(self->fast_memo, key)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1720 | Py_DECREF(key); |
| 1721 | PyErr_Format(PyExc_ValueError, |
| 1722 | "fast mode: can't pickle cyclic objects " |
| 1723 | "including object type %.200s at %p", |
| 1724 | obj->ob_type->tp_name, obj); |
| 1725 | self->fast_nesting = -1; |
| 1726 | return 0; |
| 1727 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 1728 | if (PyErr_Occurred()) { |
| 1729 | return 0; |
| 1730 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1731 | if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) { |
| 1732 | Py_DECREF(key); |
| 1733 | self->fast_nesting = -1; |
| 1734 | return 0; |
| 1735 | } |
| 1736 | Py_DECREF(key); |
| 1737 | } |
| 1738 | return 1; |
| 1739 | } |
| 1740 | |
| 1741 | static int |
| 1742 | fast_save_leave(PicklerObject *self, PyObject *obj) |
| 1743 | { |
| 1744 | if (self->fast_nesting-- >= FAST_NESTING_LIMIT) { |
| 1745 | PyObject *key = PyLong_FromVoidPtr(obj); |
| 1746 | if (key == NULL) |
| 1747 | return 0; |
| 1748 | if (PyDict_DelItem(self->fast_memo, key) < 0) { |
| 1749 | Py_DECREF(key); |
| 1750 | return 0; |
| 1751 | } |
| 1752 | Py_DECREF(key); |
| 1753 | } |
| 1754 | return 1; |
| 1755 | } |
| 1756 | |
| 1757 | static int |
| 1758 | save_none(PicklerObject *self, PyObject *obj) |
| 1759 | { |
| 1760 | const char none_op = NONE; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1761 | if (_Pickler_Write(self, &none_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1762 | return -1; |
| 1763 | |
| 1764 | return 0; |
| 1765 | } |
| 1766 | |
| 1767 | static int |
| 1768 | save_bool(PicklerObject *self, PyObject *obj) |
| 1769 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1770 | if (self->proto >= 2) { |
Alexandre Vassalotti | 8a67f52 | 2013-11-24 21:40:18 -0800 | [diff] [blame] | 1771 | const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1772 | if (_Pickler_Write(self, &bool_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1773 | return -1; |
| 1774 | } |
Alexandre Vassalotti | 8a67f52 | 2013-11-24 21:40:18 -0800 | [diff] [blame] | 1775 | else { |
| 1776 | /* These aren't opcodes -- they're ways to pickle bools before protocol 2 |
| 1777 | * so that unpicklers written before bools were introduced unpickle them |
| 1778 | * as ints, but unpicklers after can recognize that bools were intended. |
| 1779 | * Note that protocol 2 added direct ways to pickle bools. |
| 1780 | */ |
| 1781 | const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n"; |
| 1782 | if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0) |
| 1783 | return -1; |
| 1784 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1785 | return 0; |
| 1786 | } |
| 1787 | |
| 1788 | static int |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1789 | save_long(PicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1790 | { |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1791 | PyObject *repr = NULL; |
| 1792 | Py_ssize_t size; |
| 1793 | long val; |
| 1794 | int status = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1795 | |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1796 | const char long_op = LONG; |
| 1797 | |
| 1798 | val= PyLong_AsLong(obj); |
| 1799 | if (val == -1 && PyErr_Occurred()) { |
| 1800 | /* out of range for int pickling */ |
| 1801 | PyErr_Clear(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1802 | } |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1803 | else if (self->bin && |
| 1804 | (sizeof(long) <= 4 || |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 1805 | (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 1806 | /* result fits in a signed 4-byte integer. |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 1807 | |
| 1808 | Note: we can't use -0x80000000L in the above condition because some |
| 1809 | compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type |
| 1810 | before applying the unary minus when sizeof(long) <= 4. The |
| 1811 | resulting value stays unsigned which is commonly not what we want, |
| 1812 | so MSVC happily warns us about it. However, that result would have |
| 1813 | been fine because we guard for sizeof(long) <= 4 which turns the |
| 1814 | condition true in that particular case. */ |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1815 | char pdata[32]; |
| 1816 | Py_ssize_t len = 0; |
| 1817 | |
| 1818 | pdata[1] = (unsigned char)(val & 0xff); |
| 1819 | pdata[2] = (unsigned char)((val >> 8) & 0xff); |
| 1820 | pdata[3] = (unsigned char)((val >> 16) & 0xff); |
| 1821 | pdata[4] = (unsigned char)((val >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1822 | |
| 1823 | if ((pdata[4] == 0) && (pdata[3] == 0)) { |
| 1824 | if (pdata[2] == 0) { |
| 1825 | pdata[0] = BININT1; |
| 1826 | len = 2; |
| 1827 | } |
| 1828 | else { |
| 1829 | pdata[0] = BININT2; |
| 1830 | len = 3; |
| 1831 | } |
| 1832 | } |
| 1833 | else { |
| 1834 | pdata[0] = BININT; |
| 1835 | len = 5; |
| 1836 | } |
| 1837 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1838 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1839 | return -1; |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1840 | |
| 1841 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1844 | if (self->proto >= 2) { |
| 1845 | /* Linear-time pickling. */ |
| 1846 | size_t nbits; |
| 1847 | size_t nbytes; |
| 1848 | unsigned char *pdata; |
| 1849 | char header[5]; |
| 1850 | int i; |
| 1851 | int sign = _PyLong_Sign(obj); |
| 1852 | |
| 1853 | if (sign == 0) { |
| 1854 | header[0] = LONG1; |
| 1855 | header[1] = 0; /* It's 0 -- an empty bytestring. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1856 | if (_Pickler_Write(self, header, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1857 | goto error; |
| 1858 | return 0; |
| 1859 | } |
| 1860 | nbits = _PyLong_NumBits(obj); |
| 1861 | if (nbits == (size_t)-1 && PyErr_Occurred()) |
| 1862 | goto error; |
| 1863 | /* How many bytes do we need? There are nbits >> 3 full |
| 1864 | * bytes of data, and nbits & 7 leftover bits. If there |
| 1865 | * are any leftover bits, then we clearly need another |
| 1866 | * byte. Wnat's not so obvious is that we *probably* |
| 1867 | * need another byte even if there aren't any leftovers: |
| 1868 | * the most-significant bit of the most-significant byte |
| 1869 | * acts like a sign bit, and it's usually got a sense |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1870 | * opposite of the one we need. The exception is ints |
| 1871 | * 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] | 1872 | * its own 256's-complement, so has the right sign bit |
| 1873 | * even without the extra byte. That's a pain to check |
| 1874 | * for in advance, though, so we always grab an extra |
| 1875 | * byte at the start, and cut it back later if possible. |
| 1876 | */ |
| 1877 | nbytes = (nbits >> 3) + 1; |
Antoine Pitrou | bf6ecf9 | 2012-11-24 20:40:21 +0100 | [diff] [blame] | 1878 | if (nbytes > 0x7fffffffL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1879 | PyErr_SetString(PyExc_OverflowError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1880 | "int too large to pickle"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1881 | goto error; |
| 1882 | } |
Neal Norwitz | 6ae2eb2 | 2008-08-24 23:50:08 +0000 | [diff] [blame] | 1883 | repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1884 | if (repr == NULL) |
| 1885 | goto error; |
Neal Norwitz | 6ae2eb2 | 2008-08-24 23:50:08 +0000 | [diff] [blame] | 1886 | pdata = (unsigned char *)PyBytes_AS_STRING(repr); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1887 | i = _PyLong_AsByteArray((PyLongObject *)obj, |
| 1888 | pdata, nbytes, |
| 1889 | 1 /* little endian */ , 1 /* signed */ ); |
| 1890 | if (i < 0) |
| 1891 | goto error; |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1892 | /* If the int is negative, this may be a byte more than |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1893 | * needed. This is so iff the MSB is all redundant sign |
| 1894 | * bits. |
| 1895 | */ |
| 1896 | if (sign < 0 && |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1897 | nbytes > 1 && |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1898 | pdata[nbytes - 1] == 0xff && |
| 1899 | (pdata[nbytes - 2] & 0x80) != 0) { |
| 1900 | nbytes--; |
| 1901 | } |
| 1902 | |
| 1903 | if (nbytes < 256) { |
| 1904 | header[0] = LONG1; |
| 1905 | header[1] = (unsigned char)nbytes; |
| 1906 | size = 2; |
| 1907 | } |
| 1908 | else { |
| 1909 | header[0] = LONG4; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1910 | size = (Py_ssize_t) nbytes; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1911 | for (i = 1; i < 5; i++) { |
| 1912 | header[i] = (unsigned char)(size & 0xff); |
| 1913 | size >>= 8; |
| 1914 | } |
| 1915 | size = 5; |
| 1916 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1917 | if (_Pickler_Write(self, header, size) < 0 || |
| 1918 | _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1919 | goto error; |
| 1920 | } |
| 1921 | else { |
| 1922 | char *string; |
| 1923 | |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 1924 | /* proto < 2: write the repr and newline. This is quadratic-time (in |
| 1925 | the number of digits), in both directions. We add a trailing 'L' |
| 1926 | to the repr, for compatibility with Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1927 | |
| 1928 | repr = PyObject_Repr(obj); |
| 1929 | if (repr == NULL) |
| 1930 | goto error; |
| 1931 | |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1932 | string = _PyUnicode_AsStringAndSize(repr, &size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1933 | if (string == NULL) |
| 1934 | goto error; |
| 1935 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1936 | if (_Pickler_Write(self, &long_op, 1) < 0 || |
| 1937 | _Pickler_Write(self, string, size) < 0 || |
| 1938 | _Pickler_Write(self, "L\n", 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1939 | goto error; |
| 1940 | } |
| 1941 | |
| 1942 | if (0) { |
| 1943 | error: |
| 1944 | status = -1; |
| 1945 | } |
| 1946 | Py_XDECREF(repr); |
| 1947 | |
| 1948 | return status; |
| 1949 | } |
| 1950 | |
| 1951 | static int |
| 1952 | save_float(PicklerObject *self, PyObject *obj) |
| 1953 | { |
| 1954 | double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj); |
| 1955 | |
| 1956 | if (self->bin) { |
| 1957 | char pdata[9]; |
| 1958 | pdata[0] = BINFLOAT; |
| 1959 | if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0) |
| 1960 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1961 | if (_Pickler_Write(self, pdata, 9) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1962 | return -1; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1963 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1964 | else { |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1965 | int result = -1; |
| 1966 | char *buf = NULL; |
| 1967 | char op = FLOAT; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1968 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1969 | if (_Pickler_Write(self, &op, 1) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1970 | goto done; |
| 1971 | |
Mark Dickinson | 3e09f43 | 2009-04-17 08:41:23 +0000 | [diff] [blame] | 1972 | buf = PyOS_double_to_string(x, 'g', 17, 0, NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1973 | if (!buf) { |
| 1974 | PyErr_NoMemory(); |
| 1975 | goto done; |
| 1976 | } |
| 1977 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1978 | if (_Pickler_Write(self, buf, strlen(buf)) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1979 | goto done; |
| 1980 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1981 | if (_Pickler_Write(self, "\n", 1) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1982 | goto done; |
| 1983 | |
| 1984 | result = 0; |
| 1985 | done: |
| 1986 | PyMem_Free(buf); |
| 1987 | return result; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1988 | } |
| 1989 | |
| 1990 | return 0; |
| 1991 | } |
| 1992 | |
| 1993 | static int |
| 1994 | save_bytes(PicklerObject *self, PyObject *obj) |
| 1995 | { |
| 1996 | if (self->proto < 3) { |
| 1997 | /* Older pickle protocols do not have an opcode for pickling bytes |
| 1998 | objects. Therefore, we need to fake the copy protocol (i.e., |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 1999 | the __reduce__ method) to permit bytes object unpickling. |
| 2000 | |
| 2001 | Here we use a hack to be compatible with Python 2. Since in Python |
| 2002 | 2 'bytes' is just an alias for 'str' (which has different |
| 2003 | parameters than the actual bytes object), we use codecs.encode |
| 2004 | to create the appropriate 'str' object when unpickled using |
| 2005 | Python 2 *and* the appropriate 'bytes' object when unpickled |
| 2006 | using Python 3. Again this is a hack and we don't need to do this |
| 2007 | with newer protocols. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2008 | PyObject *reduce_value = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2009 | int status; |
| 2010 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2011 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2012 | reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type); |
| 2013 | } |
| 2014 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 2015 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2016 | PyObject *unicode_str = |
| 2017 | PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj), |
| 2018 | PyBytes_GET_SIZE(obj), |
| 2019 | "strict"); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2020 | _Py_IDENTIFIER(latin1); |
| 2021 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2022 | if (unicode_str == NULL) |
| 2023 | return -1; |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2024 | reduce_value = Py_BuildValue("(O(OO))", |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 2025 | st->codecs_encode, unicode_str, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2026 | _PyUnicode_FromId(&PyId_latin1)); |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2027 | Py_DECREF(unicode_str); |
| 2028 | } |
| 2029 | |
| 2030 | if (reduce_value == NULL) |
| 2031 | return -1; |
| 2032 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2033 | /* save_reduce() will memoize the object automatically. */ |
| 2034 | status = save_reduce(self, reduce_value, obj); |
| 2035 | Py_DECREF(reduce_value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2036 | return status; |
| 2037 | } |
| 2038 | else { |
| 2039 | Py_ssize_t size; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2040 | char header[9]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2041 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2042 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2043 | size = PyBytes_GET_SIZE(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2044 | if (size < 0) |
| 2045 | return -1; |
| 2046 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2047 | if (size <= 0xff) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2048 | header[0] = SHORT_BINBYTES; |
| 2049 | header[1] = (unsigned char)size; |
| 2050 | len = 2; |
| 2051 | } |
Serhiy Storchaka | 67c719b | 2014-09-05 10:10:23 +0300 | [diff] [blame] | 2052 | else if ((size_t)size <= 0xffffffffUL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2053 | header[0] = BINBYTES; |
| 2054 | header[1] = (unsigned char)(size & 0xff); |
| 2055 | header[2] = (unsigned char)((size >> 8) & 0xff); |
| 2056 | header[3] = (unsigned char)((size >> 16) & 0xff); |
| 2057 | header[4] = (unsigned char)((size >> 24) & 0xff); |
| 2058 | len = 5; |
| 2059 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2060 | else if (self->proto >= 4) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2061 | header[0] = BINBYTES8; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 2062 | _write_size64(header + 1, size); |
Alexandre Vassalotti | 6e73ff1 | 2013-12-05 19:29:32 -0800 | [diff] [blame] | 2063 | len = 9; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2064 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2065 | else { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2066 | PyErr_SetString(PyExc_OverflowError, |
Serhiy Storchaka | f8def28 | 2013-02-16 17:29:56 +0200 | [diff] [blame] | 2067 | "cannot serialize a bytes object larger than 4 GiB"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2068 | return -1; /* string too large */ |
| 2069 | } |
| 2070 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2071 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2072 | return -1; |
| 2073 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2074 | if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2075 | return -1; |
| 2076 | |
| 2077 | if (memo_put(self, obj) < 0) |
| 2078 | return -1; |
| 2079 | |
| 2080 | return 0; |
| 2081 | } |
| 2082 | } |
| 2083 | |
| 2084 | /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates |
| 2085 | backslash and newline characters to \uXXXX escapes. */ |
| 2086 | static PyObject * |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2087 | raw_unicode_escape(PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2088 | { |
Victor Stinner | 7270b7f | 2014-08-17 21:14:46 +0200 | [diff] [blame] | 2089 | PyObject *repr; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2090 | char *p; |
Victor Stinner | 049e509 | 2014-08-17 22:20:00 +0200 | [diff] [blame] | 2091 | Py_ssize_t i, size; |
| 2092 | size_t expandsize; |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2093 | void *data; |
| 2094 | unsigned int kind; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2095 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2096 | if (PyUnicode_READY(obj)) |
| 2097 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2098 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2099 | size = PyUnicode_GET_LENGTH(obj); |
| 2100 | data = PyUnicode_DATA(obj); |
| 2101 | kind = PyUnicode_KIND(obj); |
| 2102 | if (kind == PyUnicode_4BYTE_KIND) |
| 2103 | expandsize = 10; |
| 2104 | else |
| 2105 | expandsize = 6; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 2106 | |
Victor Stinner | 049e509 | 2014-08-17 22:20:00 +0200 | [diff] [blame] | 2107 | if ((size_t)size > (size_t)PY_SSIZE_T_MAX / expandsize) |
Alexandre Vassalotti | 554d878 | 2008-12-27 07:32:41 +0000 | [diff] [blame] | 2108 | return PyErr_NoMemory(); |
Victor Stinner | 7270b7f | 2014-08-17 21:14:46 +0200 | [diff] [blame] | 2109 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2110 | if (repr == NULL) |
| 2111 | return NULL; |
| 2112 | if (size == 0) |
Victor Stinner | 7270b7f | 2014-08-17 21:14:46 +0200 | [diff] [blame] | 2113 | return repr; |
| 2114 | assert(Py_REFCNT(repr) == 1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2115 | |
Victor Stinner | 7270b7f | 2014-08-17 21:14:46 +0200 | [diff] [blame] | 2116 | p = PyBytes_AS_STRING(repr); |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2117 | for (i=0; i < size; i++) { |
| 2118 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2119 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 2120 | if (ch >= 0x10000) { |
| 2121 | *p++ = '\\'; |
| 2122 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 2123 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 2124 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 2125 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 2126 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 2127 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 2128 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 2129 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 2130 | *p++ = Py_hexdigits[ch & 15]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2131 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2132 | /* Map 16-bit characters to '\uxxxx' */ |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2133 | else if (ch >= 256 || ch == '\\' || ch == '\n') { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2134 | *p++ = '\\'; |
| 2135 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 2136 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 2137 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 2138 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 2139 | *p++ = Py_hexdigits[ch & 15]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2140 | } |
Alexandre Vassalotti | 554d878 | 2008-12-27 07:32:41 +0000 | [diff] [blame] | 2141 | /* Copy everything else as-is */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2142 | else |
| 2143 | *p++ = (char) ch; |
| 2144 | } |
Victor Stinner | 7270b7f | 2014-08-17 21:14:46 +0200 | [diff] [blame] | 2145 | size = p - PyBytes_AS_STRING(repr); |
| 2146 | if (_PyBytes_Resize(&repr, size) < 0) |
| 2147 | return NULL; |
| 2148 | return repr; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2149 | } |
| 2150 | |
| 2151 | static int |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2152 | write_utf8(PicklerObject *self, char *data, Py_ssize_t size) |
| 2153 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2154 | char header[9]; |
| 2155 | Py_ssize_t len; |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2156 | |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 2157 | assert(size >= 0); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2158 | if (size <= 0xff && self->proto >= 4) { |
| 2159 | header[0] = SHORT_BINUNICODE; |
| 2160 | header[1] = (unsigned char)(size & 0xff); |
| 2161 | len = 2; |
| 2162 | } |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 2163 | else if ((size_t)size <= 0xffffffffUL) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2164 | header[0] = BINUNICODE; |
| 2165 | header[1] = (unsigned char)(size & 0xff); |
| 2166 | header[2] = (unsigned char)((size >> 8) & 0xff); |
| 2167 | header[3] = (unsigned char)((size >> 16) & 0xff); |
| 2168 | header[4] = (unsigned char)((size >> 24) & 0xff); |
| 2169 | len = 5; |
| 2170 | } |
| 2171 | else if (self->proto >= 4) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2172 | header[0] = BINUNICODE8; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 2173 | _write_size64(header + 1, size); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2174 | len = 9; |
| 2175 | } |
| 2176 | else { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2177 | PyErr_SetString(PyExc_OverflowError, |
Antoine Pitrou | 4b7b0f0 | 2013-04-07 23:46:52 +0200 | [diff] [blame] | 2178 | "cannot serialize a string larger than 4GiB"); |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2179 | return -1; |
| 2180 | } |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2181 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2182 | if (_Pickler_Write(self, header, len) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2183 | return -1; |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2184 | if (_Pickler_Write(self, data, size) < 0) |
| 2185 | return -1; |
| 2186 | |
| 2187 | return 0; |
| 2188 | } |
| 2189 | |
| 2190 | static int |
| 2191 | write_unicode_binary(PicklerObject *self, PyObject *obj) |
| 2192 | { |
| 2193 | PyObject *encoded = NULL; |
| 2194 | Py_ssize_t size; |
| 2195 | char *data; |
| 2196 | int r; |
| 2197 | |
| 2198 | if (PyUnicode_READY(obj)) |
| 2199 | return -1; |
| 2200 | |
| 2201 | data = PyUnicode_AsUTF8AndSize(obj, &size); |
| 2202 | if (data != NULL) |
| 2203 | return write_utf8(self, data, size); |
| 2204 | |
| 2205 | /* Issue #8383: for strings with lone surrogates, fallback on the |
| 2206 | "surrogatepass" error handler. */ |
| 2207 | PyErr_Clear(); |
| 2208 | encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass"); |
| 2209 | if (encoded == NULL) |
| 2210 | return -1; |
| 2211 | |
| 2212 | r = write_utf8(self, PyBytes_AS_STRING(encoded), |
| 2213 | PyBytes_GET_SIZE(encoded)); |
| 2214 | Py_DECREF(encoded); |
| 2215 | return r; |
| 2216 | } |
| 2217 | |
| 2218 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2219 | save_unicode(PicklerObject *self, PyObject *obj) |
| 2220 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2221 | if (self->bin) { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2222 | if (write_unicode_binary(self, obj) < 0) |
| 2223 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2224 | } |
| 2225 | else { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2226 | PyObject *encoded; |
| 2227 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2228 | const char unicode_op = UNICODE; |
| 2229 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2230 | encoded = raw_unicode_escape(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2231 | if (encoded == NULL) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2232 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2233 | |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2234 | if (_Pickler_Write(self, &unicode_op, 1) < 0) { |
| 2235 | Py_DECREF(encoded); |
| 2236 | return -1; |
| 2237 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2238 | |
| 2239 | size = PyBytes_GET_SIZE(encoded); |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2240 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) { |
| 2241 | Py_DECREF(encoded); |
| 2242 | return -1; |
| 2243 | } |
| 2244 | Py_DECREF(encoded); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2245 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2246 | if (_Pickler_Write(self, "\n", 1) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2247 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2248 | } |
| 2249 | if (memo_put(self, obj) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2250 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2251 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2252 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
| 2255 | /* A helper for save_tuple. Push the len elements in tuple t on the stack. */ |
| 2256 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2257 | store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2258 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2259 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2260 | |
| 2261 | assert(PyTuple_Size(t) == len); |
| 2262 | |
| 2263 | for (i = 0; i < len; i++) { |
| 2264 | PyObject *element = PyTuple_GET_ITEM(t, i); |
| 2265 | |
| 2266 | if (element == NULL) |
| 2267 | return -1; |
| 2268 | if (save(self, element, 0) < 0) |
| 2269 | return -1; |
| 2270 | } |
| 2271 | |
| 2272 | return 0; |
| 2273 | } |
| 2274 | |
| 2275 | /* Tuples are ubiquitous in the pickle protocols, so many techniques are |
| 2276 | * used across protocols to minimize the space needed to pickle them. |
| 2277 | * Tuples are also the only builtin immutable type that can be recursive |
| 2278 | * (a tuple can be reached from itself), and that requires some subtle |
| 2279 | * magic so that it works in all cases. IOW, this is a long routine. |
| 2280 | */ |
| 2281 | static int |
| 2282 | save_tuple(PicklerObject *self, PyObject *obj) |
| 2283 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2284 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2285 | |
| 2286 | const char mark_op = MARK; |
| 2287 | const char tuple_op = TUPLE; |
| 2288 | const char pop_op = POP; |
| 2289 | const char pop_mark_op = POP_MARK; |
| 2290 | const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3}; |
| 2291 | |
| 2292 | if ((len = PyTuple_Size(obj)) < 0) |
| 2293 | return -1; |
| 2294 | |
| 2295 | if (len == 0) { |
| 2296 | char pdata[2]; |
| 2297 | |
| 2298 | if (self->proto) { |
| 2299 | pdata[0] = EMPTY_TUPLE; |
| 2300 | len = 1; |
| 2301 | } |
| 2302 | else { |
| 2303 | pdata[0] = MARK; |
| 2304 | pdata[1] = TUPLE; |
| 2305 | len = 2; |
| 2306 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2307 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2308 | return -1; |
| 2309 | return 0; |
| 2310 | } |
| 2311 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2312 | /* 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] | 2313 | * saving the tuple elements, the tuple must be recursive, in |
| 2314 | * which case we'll pop everything we put on the stack, and fetch |
| 2315 | * its value from the memo. |
| 2316 | */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2317 | if (len <= 3 && self->proto >= 2) { |
| 2318 | /* Use TUPLE{1,2,3} opcodes. */ |
| 2319 | if (store_tuple_elements(self, obj, len) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2320 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2321 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2322 | if (PyMemoTable_Get(self->memo, obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2323 | /* pop the len elements */ |
| 2324 | for (i = 0; i < len; i++) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2325 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 2326 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2327 | /* fetch from memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2328 | if (memo_get(self, obj) < 0) |
| 2329 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2330 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2331 | return 0; |
| 2332 | } |
| 2333 | else { /* Not recursive. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2334 | if (_Pickler_Write(self, len2opcode + len, 1) < 0) |
| 2335 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2336 | } |
| 2337 | goto memoize; |
| 2338 | } |
| 2339 | |
| 2340 | /* proto < 2 and len > 0, or proto >= 2 and len > 3. |
| 2341 | * Generate MARK e1 e2 ... TUPLE |
| 2342 | */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2343 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2344 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2345 | |
| 2346 | if (store_tuple_elements(self, obj, len) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2347 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2348 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2349 | if (PyMemoTable_Get(self->memo, obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2350 | /* pop the stack stuff we pushed */ |
| 2351 | if (self->bin) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2352 | if (_Pickler_Write(self, &pop_mark_op, 1) < 0) |
| 2353 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2354 | } |
| 2355 | else { |
| 2356 | /* Note that we pop one more than len, to remove |
| 2357 | * the MARK too. |
| 2358 | */ |
| 2359 | for (i = 0; i <= len; i++) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2360 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 2361 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2362 | } |
| 2363 | /* fetch from memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2364 | if (memo_get(self, obj) < 0) |
| 2365 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2366 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2367 | return 0; |
| 2368 | } |
| 2369 | else { /* Not recursive. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2370 | if (_Pickler_Write(self, &tuple_op, 1) < 0) |
| 2371 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2372 | } |
| 2373 | |
| 2374 | memoize: |
| 2375 | if (memo_put(self, obj) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2376 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2377 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2378 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2379 | } |
| 2380 | |
| 2381 | /* iter is an iterator giving items, and we batch up chunks of |
| 2382 | * MARK item item ... item APPENDS |
| 2383 | * opcode sequences. Calling code should have arranged to first create an |
| 2384 | * empty list, or list-like object, for the APPENDS to operate on. |
| 2385 | * Returns 0 on success, <0 on error. |
| 2386 | */ |
| 2387 | static int |
| 2388 | batch_list(PicklerObject *self, PyObject *iter) |
| 2389 | { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2390 | PyObject *obj = NULL; |
| 2391 | PyObject *firstitem = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2392 | int i, n; |
| 2393 | |
| 2394 | const char mark_op = MARK; |
| 2395 | const char append_op = APPEND; |
| 2396 | const char appends_op = APPENDS; |
| 2397 | |
| 2398 | assert(iter != NULL); |
| 2399 | |
| 2400 | /* XXX: I think this function could be made faster by avoiding the |
| 2401 | iterator interface and fetching objects directly from list using |
| 2402 | PyList_GET_ITEM. |
| 2403 | */ |
| 2404 | |
| 2405 | if (self->proto == 0) { |
| 2406 | /* APPENDS isn't available; do one at a time. */ |
| 2407 | for (;;) { |
| 2408 | obj = PyIter_Next(iter); |
| 2409 | if (obj == NULL) { |
| 2410 | if (PyErr_Occurred()) |
| 2411 | return -1; |
| 2412 | break; |
| 2413 | } |
| 2414 | i = save(self, obj, 0); |
| 2415 | Py_DECREF(obj); |
| 2416 | if (i < 0) |
| 2417 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2418 | if (_Pickler_Write(self, &append_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2419 | return -1; |
| 2420 | } |
| 2421 | return 0; |
| 2422 | } |
| 2423 | |
| 2424 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 2425 | do { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2426 | /* Get first item */ |
| 2427 | firstitem = PyIter_Next(iter); |
| 2428 | if (firstitem == NULL) { |
| 2429 | if (PyErr_Occurred()) |
| 2430 | goto error; |
| 2431 | |
| 2432 | /* nothing more to add */ |
| 2433 | break; |
| 2434 | } |
| 2435 | |
| 2436 | /* Try to get a second item */ |
| 2437 | obj = PyIter_Next(iter); |
| 2438 | if (obj == NULL) { |
| 2439 | if (PyErr_Occurred()) |
| 2440 | goto error; |
| 2441 | |
| 2442 | /* Only one item to write */ |
| 2443 | if (save(self, firstitem, 0) < 0) |
| 2444 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2445 | if (_Pickler_Write(self, &append_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2446 | goto error; |
| 2447 | Py_CLEAR(firstitem); |
| 2448 | break; |
| 2449 | } |
| 2450 | |
| 2451 | /* More than one item to write */ |
| 2452 | |
| 2453 | /* Pump out MARK, items, APPENDS. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2454 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2455 | goto error; |
| 2456 | |
| 2457 | if (save(self, firstitem, 0) < 0) |
| 2458 | goto error; |
| 2459 | Py_CLEAR(firstitem); |
| 2460 | n = 1; |
| 2461 | |
| 2462 | /* Fetch and save up to BATCHSIZE items */ |
| 2463 | while (obj) { |
| 2464 | if (save(self, obj, 0) < 0) |
| 2465 | goto error; |
| 2466 | Py_CLEAR(obj); |
| 2467 | n += 1; |
| 2468 | |
| 2469 | if (n == BATCHSIZE) |
| 2470 | break; |
| 2471 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2472 | obj = PyIter_Next(iter); |
| 2473 | if (obj == NULL) { |
| 2474 | if (PyErr_Occurred()) |
| 2475 | goto error; |
| 2476 | break; |
| 2477 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2478 | } |
| 2479 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2480 | if (_Pickler_Write(self, &appends_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2481 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2482 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2483 | } while (n == BATCHSIZE); |
| 2484 | return 0; |
| 2485 | |
| 2486 | error: |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2487 | Py_XDECREF(firstitem); |
| 2488 | Py_XDECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2489 | return -1; |
| 2490 | } |
| 2491 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2492 | /* This is a variant of batch_list() above, specialized for lists (with no |
| 2493 | * support for list subclasses). Like batch_list(), we batch up chunks of |
| 2494 | * MARK item item ... item APPENDS |
| 2495 | * opcode sequences. Calling code should have arranged to first create an |
| 2496 | * empty list, or list-like object, for the APPENDS to operate on. |
| 2497 | * Returns 0 on success, -1 on error. |
| 2498 | * |
| 2499 | * This version is considerably faster than batch_list(), if less general. |
| 2500 | * |
| 2501 | * Note that this only works for protocols > 0. |
| 2502 | */ |
| 2503 | static int |
| 2504 | batch_list_exact(PicklerObject *self, PyObject *obj) |
| 2505 | { |
| 2506 | PyObject *item = NULL; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2507 | Py_ssize_t this_batch, total; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2508 | |
| 2509 | const char append_op = APPEND; |
| 2510 | const char appends_op = APPENDS; |
| 2511 | const char mark_op = MARK; |
| 2512 | |
| 2513 | assert(obj != NULL); |
| 2514 | assert(self->proto > 0); |
| 2515 | assert(PyList_CheckExact(obj)); |
| 2516 | |
| 2517 | if (PyList_GET_SIZE(obj) == 1) { |
| 2518 | item = PyList_GET_ITEM(obj, 0); |
| 2519 | if (save(self, item, 0) < 0) |
| 2520 | return -1; |
| 2521 | if (_Pickler_Write(self, &append_op, 1) < 0) |
| 2522 | return -1; |
| 2523 | return 0; |
| 2524 | } |
| 2525 | |
| 2526 | /* Write in batches of BATCHSIZE. */ |
| 2527 | total = 0; |
| 2528 | do { |
| 2529 | this_batch = 0; |
| 2530 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2531 | return -1; |
| 2532 | while (total < PyList_GET_SIZE(obj)) { |
| 2533 | item = PyList_GET_ITEM(obj, total); |
| 2534 | if (save(self, item, 0) < 0) |
| 2535 | return -1; |
| 2536 | total++; |
| 2537 | if (++this_batch == BATCHSIZE) |
| 2538 | break; |
| 2539 | } |
| 2540 | if (_Pickler_Write(self, &appends_op, 1) < 0) |
| 2541 | return -1; |
| 2542 | |
| 2543 | } while (total < PyList_GET_SIZE(obj)); |
| 2544 | |
| 2545 | return 0; |
| 2546 | } |
| 2547 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2548 | static int |
| 2549 | save_list(PicklerObject *self, PyObject *obj) |
| 2550 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2551 | char header[3]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2552 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2553 | int status = 0; |
| 2554 | |
| 2555 | if (self->fast && !fast_save_enter(self, obj)) |
| 2556 | goto error; |
| 2557 | |
| 2558 | /* Create an empty list. */ |
| 2559 | if (self->bin) { |
| 2560 | header[0] = EMPTY_LIST; |
| 2561 | len = 1; |
| 2562 | } |
| 2563 | else { |
| 2564 | header[0] = MARK; |
| 2565 | header[1] = LIST; |
| 2566 | len = 2; |
| 2567 | } |
| 2568 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2569 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2570 | goto error; |
| 2571 | |
| 2572 | /* Get list length, and bow out early if empty. */ |
| 2573 | if ((len = PyList_Size(obj)) < 0) |
| 2574 | goto error; |
| 2575 | |
| 2576 | if (memo_put(self, obj) < 0) |
| 2577 | goto error; |
| 2578 | |
| 2579 | if (len != 0) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2580 | /* Materialize the list elements. */ |
| 2581 | if (PyList_CheckExact(obj) && self->proto > 0) { |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2582 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 2583 | goto error; |
| 2584 | status = batch_list_exact(self, obj); |
| 2585 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2586 | } else { |
| 2587 | PyObject *iter = PyObject_GetIter(obj); |
| 2588 | if (iter == NULL) |
| 2589 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2590 | |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2591 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 2592 | Py_DECREF(iter); |
| 2593 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2594 | } |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2595 | status = batch_list(self, iter); |
| 2596 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2597 | Py_DECREF(iter); |
| 2598 | } |
| 2599 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2600 | if (0) { |
| 2601 | error: |
| 2602 | status = -1; |
| 2603 | } |
| 2604 | |
| 2605 | if (self->fast && !fast_save_leave(self, obj)) |
| 2606 | status = -1; |
| 2607 | |
| 2608 | return status; |
| 2609 | } |
| 2610 | |
| 2611 | /* iter is an iterator giving (key, value) pairs, and we batch up chunks of |
| 2612 | * MARK key value ... key value SETITEMS |
| 2613 | * opcode sequences. Calling code should have arranged to first create an |
| 2614 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 2615 | * Returns 0 on success, <0 on error. |
| 2616 | * |
| 2617 | * This is very much like batch_list(). The difference between saving |
| 2618 | * elements directly, and picking apart two-tuples, is so long-winded at |
| 2619 | * the C level, though, that attempts to combine these routines were too |
| 2620 | * ugly to bear. |
| 2621 | */ |
| 2622 | static int |
| 2623 | batch_dict(PicklerObject *self, PyObject *iter) |
| 2624 | { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2625 | PyObject *obj = NULL; |
| 2626 | PyObject *firstitem = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2627 | int i, n; |
| 2628 | |
| 2629 | const char mark_op = MARK; |
| 2630 | const char setitem_op = SETITEM; |
| 2631 | const char setitems_op = SETITEMS; |
| 2632 | |
| 2633 | assert(iter != NULL); |
| 2634 | |
| 2635 | if (self->proto == 0) { |
| 2636 | /* SETITEMS isn't available; do one at a time. */ |
| 2637 | for (;;) { |
| 2638 | obj = PyIter_Next(iter); |
| 2639 | if (obj == NULL) { |
| 2640 | if (PyErr_Occurred()) |
| 2641 | return -1; |
| 2642 | break; |
| 2643 | } |
| 2644 | if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) { |
| 2645 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2646 | "iterator must return 2-tuples"); |
| 2647 | return -1; |
| 2648 | } |
| 2649 | i = save(self, PyTuple_GET_ITEM(obj, 0), 0); |
| 2650 | if (i >= 0) |
| 2651 | i = save(self, PyTuple_GET_ITEM(obj, 1), 0); |
| 2652 | Py_DECREF(obj); |
| 2653 | if (i < 0) |
| 2654 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2655 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2656 | return -1; |
| 2657 | } |
| 2658 | return 0; |
| 2659 | } |
| 2660 | |
| 2661 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 2662 | do { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2663 | /* Get first item */ |
| 2664 | firstitem = PyIter_Next(iter); |
| 2665 | if (firstitem == NULL) { |
| 2666 | if (PyErr_Occurred()) |
| 2667 | goto error; |
| 2668 | |
| 2669 | /* nothing more to add */ |
| 2670 | break; |
| 2671 | } |
| 2672 | if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) { |
| 2673 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2674 | "iterator must return 2-tuples"); |
| 2675 | goto error; |
| 2676 | } |
| 2677 | |
| 2678 | /* Try to get a second item */ |
| 2679 | obj = PyIter_Next(iter); |
| 2680 | if (obj == NULL) { |
| 2681 | if (PyErr_Occurred()) |
| 2682 | goto error; |
| 2683 | |
| 2684 | /* Only one item to write */ |
| 2685 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 2686 | goto error; |
| 2687 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 2688 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2689 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2690 | goto error; |
| 2691 | Py_CLEAR(firstitem); |
| 2692 | break; |
| 2693 | } |
| 2694 | |
| 2695 | /* More than one item to write */ |
| 2696 | |
| 2697 | /* Pump out MARK, items, SETITEMS. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2698 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2699 | goto error; |
| 2700 | |
| 2701 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 2702 | goto error; |
| 2703 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 2704 | goto error; |
| 2705 | Py_CLEAR(firstitem); |
| 2706 | n = 1; |
| 2707 | |
| 2708 | /* Fetch and save up to BATCHSIZE items */ |
| 2709 | while (obj) { |
| 2710 | if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) { |
| 2711 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2712 | "iterator must return 2-tuples"); |
| 2713 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2714 | } |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2715 | if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 || |
| 2716 | save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0) |
| 2717 | goto error; |
| 2718 | Py_CLEAR(obj); |
| 2719 | n += 1; |
| 2720 | |
| 2721 | if (n == BATCHSIZE) |
| 2722 | break; |
| 2723 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2724 | obj = PyIter_Next(iter); |
| 2725 | if (obj == NULL) { |
| 2726 | if (PyErr_Occurred()) |
| 2727 | goto error; |
| 2728 | break; |
| 2729 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2730 | } |
| 2731 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2732 | if (_Pickler_Write(self, &setitems_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2733 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2734 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2735 | } while (n == BATCHSIZE); |
| 2736 | return 0; |
| 2737 | |
| 2738 | error: |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2739 | Py_XDECREF(firstitem); |
| 2740 | Py_XDECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2741 | return -1; |
| 2742 | } |
| 2743 | |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2744 | /* This is a variant of batch_dict() above that specializes for dicts, with no |
| 2745 | * support for dict subclasses. Like batch_dict(), we batch up chunks of |
| 2746 | * MARK key value ... key value SETITEMS |
| 2747 | * opcode sequences. Calling code should have arranged to first create an |
| 2748 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 2749 | * Returns 0 on success, -1 on error. |
| 2750 | * |
| 2751 | * Note that this currently doesn't work for protocol 0. |
| 2752 | */ |
| 2753 | static int |
| 2754 | batch_dict_exact(PicklerObject *self, PyObject *obj) |
| 2755 | { |
| 2756 | PyObject *key = NULL, *value = NULL; |
| 2757 | int i; |
| 2758 | Py_ssize_t dict_size, ppos = 0; |
| 2759 | |
Alexandre Vassalotti | f70b129 | 2009-05-25 18:00:52 +0000 | [diff] [blame] | 2760 | const char mark_op = MARK; |
| 2761 | const char setitem_op = SETITEM; |
| 2762 | const char setitems_op = SETITEMS; |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2763 | |
| 2764 | assert(obj != NULL); |
| 2765 | assert(self->proto > 0); |
| 2766 | |
| 2767 | dict_size = PyDict_Size(obj); |
| 2768 | |
| 2769 | /* Special-case len(d) == 1 to save space. */ |
| 2770 | if (dict_size == 1) { |
| 2771 | PyDict_Next(obj, &ppos, &key, &value); |
| 2772 | if (save(self, key, 0) < 0) |
| 2773 | return -1; |
| 2774 | if (save(self, value, 0) < 0) |
| 2775 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2776 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2777 | return -1; |
| 2778 | return 0; |
| 2779 | } |
| 2780 | |
| 2781 | /* Write in batches of BATCHSIZE. */ |
| 2782 | do { |
| 2783 | i = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2784 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2785 | return -1; |
| 2786 | while (PyDict_Next(obj, &ppos, &key, &value)) { |
| 2787 | if (save(self, key, 0) < 0) |
| 2788 | return -1; |
| 2789 | if (save(self, value, 0) < 0) |
| 2790 | return -1; |
| 2791 | if (++i == BATCHSIZE) |
| 2792 | break; |
| 2793 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2794 | if (_Pickler_Write(self, &setitems_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2795 | return -1; |
| 2796 | if (PyDict_Size(obj) != dict_size) { |
| 2797 | PyErr_Format( |
| 2798 | PyExc_RuntimeError, |
| 2799 | "dictionary changed size during iteration"); |
| 2800 | return -1; |
| 2801 | } |
| 2802 | |
| 2803 | } while (i == BATCHSIZE); |
| 2804 | return 0; |
| 2805 | } |
| 2806 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2807 | static int |
| 2808 | save_dict(PicklerObject *self, PyObject *obj) |
| 2809 | { |
| 2810 | PyObject *items, *iter; |
| 2811 | char header[3]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2812 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2813 | int status = 0; |
| 2814 | |
| 2815 | if (self->fast && !fast_save_enter(self, obj)) |
| 2816 | goto error; |
| 2817 | |
| 2818 | /* Create an empty dict. */ |
| 2819 | if (self->bin) { |
| 2820 | header[0] = EMPTY_DICT; |
| 2821 | len = 1; |
| 2822 | } |
| 2823 | else { |
| 2824 | header[0] = MARK; |
| 2825 | header[1] = DICT; |
| 2826 | len = 2; |
| 2827 | } |
| 2828 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2829 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2830 | goto error; |
| 2831 | |
| 2832 | /* Get dict size, and bow out early if empty. */ |
| 2833 | if ((len = PyDict_Size(obj)) < 0) |
| 2834 | goto error; |
| 2835 | |
| 2836 | if (memo_put(self, obj) < 0) |
| 2837 | goto error; |
| 2838 | |
| 2839 | if (len != 0) { |
| 2840 | /* Save the dict items. */ |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2841 | if (PyDict_CheckExact(obj) && self->proto > 0) { |
| 2842 | /* We can take certain shortcuts if we know this is a dict and |
| 2843 | not a dict subclass. */ |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2844 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 2845 | goto error; |
| 2846 | status = batch_dict_exact(self, obj); |
| 2847 | Py_LeaveRecursiveCall(); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2848 | } else { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2849 | _Py_IDENTIFIER(items); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2850 | |
| 2851 | items = _PyObject_CallMethodId(obj, &PyId_items, "()"); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2852 | if (items == NULL) |
| 2853 | goto error; |
| 2854 | iter = PyObject_GetIter(items); |
| 2855 | Py_DECREF(items); |
| 2856 | if (iter == NULL) |
| 2857 | goto error; |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2858 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 2859 | Py_DECREF(iter); |
| 2860 | goto error; |
| 2861 | } |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2862 | status = batch_dict(self, iter); |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2863 | Py_LeaveRecursiveCall(); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2864 | Py_DECREF(iter); |
| 2865 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2866 | } |
| 2867 | |
| 2868 | if (0) { |
| 2869 | error: |
| 2870 | status = -1; |
| 2871 | } |
| 2872 | |
| 2873 | if (self->fast && !fast_save_leave(self, obj)) |
| 2874 | status = -1; |
| 2875 | |
| 2876 | return status; |
| 2877 | } |
| 2878 | |
| 2879 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2880 | save_set(PicklerObject *self, PyObject *obj) |
| 2881 | { |
| 2882 | PyObject *item; |
| 2883 | int i; |
| 2884 | Py_ssize_t set_size, ppos = 0; |
| 2885 | Py_hash_t hash; |
| 2886 | |
| 2887 | const char empty_set_op = EMPTY_SET; |
| 2888 | const char mark_op = MARK; |
| 2889 | const char additems_op = ADDITEMS; |
| 2890 | |
| 2891 | if (self->proto < 4) { |
| 2892 | PyObject *items; |
| 2893 | PyObject *reduce_value; |
| 2894 | int status; |
| 2895 | |
| 2896 | items = PySequence_List(obj); |
| 2897 | if (items == NULL) { |
| 2898 | return -1; |
| 2899 | } |
| 2900 | reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items); |
| 2901 | Py_DECREF(items); |
| 2902 | if (reduce_value == NULL) { |
| 2903 | return -1; |
| 2904 | } |
| 2905 | /* save_reduce() will memoize the object automatically. */ |
| 2906 | status = save_reduce(self, reduce_value, obj); |
| 2907 | Py_DECREF(reduce_value); |
| 2908 | return status; |
| 2909 | } |
| 2910 | |
| 2911 | if (_Pickler_Write(self, &empty_set_op, 1) < 0) |
| 2912 | return -1; |
| 2913 | |
| 2914 | if (memo_put(self, obj) < 0) |
| 2915 | return -1; |
| 2916 | |
| 2917 | set_size = PySet_GET_SIZE(obj); |
| 2918 | if (set_size == 0) |
| 2919 | return 0; /* nothing to do */ |
| 2920 | |
| 2921 | /* Write in batches of BATCHSIZE. */ |
| 2922 | do { |
| 2923 | i = 0; |
| 2924 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2925 | return -1; |
| 2926 | while (_PySet_NextEntry(obj, &ppos, &item, &hash)) { |
| 2927 | if (save(self, item, 0) < 0) |
| 2928 | return -1; |
| 2929 | if (++i == BATCHSIZE) |
| 2930 | break; |
| 2931 | } |
| 2932 | if (_Pickler_Write(self, &additems_op, 1) < 0) |
| 2933 | return -1; |
| 2934 | if (PySet_GET_SIZE(obj) != set_size) { |
| 2935 | PyErr_Format( |
| 2936 | PyExc_RuntimeError, |
| 2937 | "set changed size during iteration"); |
| 2938 | return -1; |
| 2939 | } |
| 2940 | } while (i == BATCHSIZE); |
| 2941 | |
| 2942 | return 0; |
| 2943 | } |
| 2944 | |
| 2945 | static int |
| 2946 | save_frozenset(PicklerObject *self, PyObject *obj) |
| 2947 | { |
| 2948 | PyObject *iter; |
| 2949 | |
| 2950 | const char mark_op = MARK; |
| 2951 | const char frozenset_op = FROZENSET; |
| 2952 | |
| 2953 | if (self->fast && !fast_save_enter(self, obj)) |
| 2954 | return -1; |
| 2955 | |
| 2956 | if (self->proto < 4) { |
| 2957 | PyObject *items; |
| 2958 | PyObject *reduce_value; |
| 2959 | int status; |
| 2960 | |
| 2961 | items = PySequence_List(obj); |
| 2962 | if (items == NULL) { |
| 2963 | return -1; |
| 2964 | } |
| 2965 | reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type, |
| 2966 | items); |
| 2967 | Py_DECREF(items); |
| 2968 | if (reduce_value == NULL) { |
| 2969 | return -1; |
| 2970 | } |
| 2971 | /* save_reduce() will memoize the object automatically. */ |
| 2972 | status = save_reduce(self, reduce_value, obj); |
| 2973 | Py_DECREF(reduce_value); |
| 2974 | return status; |
| 2975 | } |
| 2976 | |
| 2977 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2978 | return -1; |
| 2979 | |
| 2980 | iter = PyObject_GetIter(obj); |
Christian Heimes | b3d3ee4 | 2013-11-23 21:01:40 +0100 | [diff] [blame] | 2981 | if (iter == NULL) { |
Christian Heimes | 74d8d63 | 2013-11-23 21:05:31 +0100 | [diff] [blame] | 2982 | return -1; |
Christian Heimes | b3d3ee4 | 2013-11-23 21:01:40 +0100 | [diff] [blame] | 2983 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2984 | for (;;) { |
| 2985 | PyObject *item; |
| 2986 | |
| 2987 | item = PyIter_Next(iter); |
| 2988 | if (item == NULL) { |
| 2989 | if (PyErr_Occurred()) { |
| 2990 | Py_DECREF(iter); |
| 2991 | return -1; |
| 2992 | } |
| 2993 | break; |
| 2994 | } |
| 2995 | if (save(self, item, 0) < 0) { |
| 2996 | Py_DECREF(item); |
| 2997 | Py_DECREF(iter); |
| 2998 | return -1; |
| 2999 | } |
| 3000 | Py_DECREF(item); |
| 3001 | } |
| 3002 | Py_DECREF(iter); |
| 3003 | |
| 3004 | /* If the object is already in the memo, this means it is |
| 3005 | recursive. In this case, throw away everything we put on the |
| 3006 | stack, and fetch the object back from the memo. */ |
| 3007 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3008 | const char pop_mark_op = POP_MARK; |
| 3009 | |
| 3010 | if (_Pickler_Write(self, &pop_mark_op, 1) < 0) |
| 3011 | return -1; |
| 3012 | if (memo_get(self, obj) < 0) |
| 3013 | return -1; |
| 3014 | return 0; |
| 3015 | } |
| 3016 | |
| 3017 | if (_Pickler_Write(self, &frozenset_op, 1) < 0) |
| 3018 | return -1; |
| 3019 | if (memo_put(self, obj) < 0) |
| 3020 | return -1; |
| 3021 | |
| 3022 | return 0; |
| 3023 | } |
| 3024 | |
| 3025 | static int |
| 3026 | fix_imports(PyObject **module_name, PyObject **global_name) |
| 3027 | { |
| 3028 | PyObject *key; |
| 3029 | PyObject *item; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3030 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3031 | |
| 3032 | key = PyTuple_Pack(2, *module_name, *global_name); |
| 3033 | if (key == NULL) |
| 3034 | return -1; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3035 | item = PyDict_GetItemWithError(st->name_mapping_3to2, key); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3036 | Py_DECREF(key); |
| 3037 | if (item) { |
| 3038 | PyObject *fixed_module_name; |
| 3039 | PyObject *fixed_global_name; |
| 3040 | |
| 3041 | if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
| 3042 | PyErr_Format(PyExc_RuntimeError, |
| 3043 | "_compat_pickle.REVERSE_NAME_MAPPING values " |
| 3044 | "should be 2-tuples, not %.200s", |
| 3045 | Py_TYPE(item)->tp_name); |
| 3046 | return -1; |
| 3047 | } |
| 3048 | fixed_module_name = PyTuple_GET_ITEM(item, 0); |
| 3049 | fixed_global_name = PyTuple_GET_ITEM(item, 1); |
| 3050 | if (!PyUnicode_Check(fixed_module_name) || |
| 3051 | !PyUnicode_Check(fixed_global_name)) { |
| 3052 | PyErr_Format(PyExc_RuntimeError, |
| 3053 | "_compat_pickle.REVERSE_NAME_MAPPING values " |
| 3054 | "should be pairs of str, not (%.200s, %.200s)", |
| 3055 | Py_TYPE(fixed_module_name)->tp_name, |
| 3056 | Py_TYPE(fixed_global_name)->tp_name); |
| 3057 | return -1; |
| 3058 | } |
| 3059 | |
| 3060 | Py_CLEAR(*module_name); |
| 3061 | Py_CLEAR(*global_name); |
| 3062 | Py_INCREF(fixed_module_name); |
| 3063 | Py_INCREF(fixed_global_name); |
| 3064 | *module_name = fixed_module_name; |
| 3065 | *global_name = fixed_global_name; |
| 3066 | } |
| 3067 | else if (PyErr_Occurred()) { |
| 3068 | return -1; |
| 3069 | } |
| 3070 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3071 | item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3072 | if (item) { |
| 3073 | if (!PyUnicode_Check(item)) { |
| 3074 | PyErr_Format(PyExc_RuntimeError, |
| 3075 | "_compat_pickle.REVERSE_IMPORT_MAPPING values " |
| 3076 | "should be strings, not %.200s", |
| 3077 | Py_TYPE(item)->tp_name); |
| 3078 | return -1; |
| 3079 | } |
| 3080 | Py_CLEAR(*module_name); |
| 3081 | Py_INCREF(item); |
| 3082 | *module_name = item; |
| 3083 | } |
| 3084 | else if (PyErr_Occurred()) { |
| 3085 | return -1; |
| 3086 | } |
| 3087 | |
| 3088 | return 0; |
| 3089 | } |
| 3090 | |
| 3091 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3092 | save_global(PicklerObject *self, PyObject *obj, PyObject *name) |
| 3093 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3094 | PyObject *global_name = NULL; |
| 3095 | PyObject *module_name = NULL; |
| 3096 | PyObject *module = NULL; |
| 3097 | PyObject *cls; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3098 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3099 | int status = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3100 | _Py_IDENTIFIER(__name__); |
| 3101 | _Py_IDENTIFIER(__qualname__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3102 | |
| 3103 | const char global_op = GLOBAL; |
| 3104 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3105 | if (name) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3106 | Py_INCREF(name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3107 | global_name = name; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3108 | } |
| 3109 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3110 | if (self->proto >= 4) { |
| 3111 | global_name = _PyObject_GetAttrId(obj, &PyId___qualname__); |
| 3112 | if (global_name == NULL) { |
| 3113 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 3114 | goto error; |
| 3115 | PyErr_Clear(); |
| 3116 | } |
| 3117 | } |
| 3118 | if (global_name == NULL) { |
| 3119 | global_name = _PyObject_GetAttrId(obj, &PyId___name__); |
| 3120 | if (global_name == NULL) |
| 3121 | goto error; |
| 3122 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3123 | } |
| 3124 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3125 | module_name = whichmodule(obj, global_name, self->proto >= 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3126 | if (module_name == NULL) |
| 3127 | goto error; |
| 3128 | |
| 3129 | /* XXX: Change to use the import C API directly with level=0 to disallow |
| 3130 | relative imports. |
| 3131 | |
| 3132 | XXX: PyImport_ImportModuleLevel could be used. However, this bypasses |
| 3133 | builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore |
| 3134 | custom import functions (IMHO, this would be a nice security |
| 3135 | feature). The import C API would need to be extended to support the |
| 3136 | extra parameters of __import__ to fix that. */ |
| 3137 | module = PyImport_Import(module_name); |
| 3138 | if (module == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3139 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3140 | "Can't pickle %R: import of module %R failed", |
| 3141 | obj, module_name); |
| 3142 | goto error; |
| 3143 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3144 | cls = getattribute(module, global_name, self->proto >= 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3145 | if (cls == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3146 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3147 | "Can't pickle %R: attribute lookup %S on %S failed", |
| 3148 | obj, global_name, module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3149 | goto error; |
| 3150 | } |
| 3151 | if (cls != obj) { |
| 3152 | Py_DECREF(cls); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3153 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3154 | "Can't pickle %R: it's not the same object as %S.%S", |
| 3155 | obj, module_name, global_name); |
| 3156 | goto error; |
| 3157 | } |
| 3158 | Py_DECREF(cls); |
| 3159 | |
| 3160 | if (self->proto >= 2) { |
| 3161 | /* See whether this is in the extension registry, and if |
| 3162 | * so generate an EXT opcode. |
| 3163 | */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3164 | PyObject *extension_key; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3165 | PyObject *code_obj; /* extension code as Python object */ |
| 3166 | long code; /* extension code as C value */ |
| 3167 | char pdata[5]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3168 | Py_ssize_t n; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3169 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3170 | extension_key = PyTuple_Pack(2, module_name, global_name); |
| 3171 | if (extension_key == NULL) { |
| 3172 | goto error; |
| 3173 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3174 | code_obj = PyDict_GetItemWithError(st->extension_registry, |
| 3175 | extension_key); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3176 | Py_DECREF(extension_key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3177 | /* The object is not registered in the extension registry. |
| 3178 | This is the most likely code path. */ |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3179 | if (code_obj == NULL) { |
| 3180 | if (PyErr_Occurred()) { |
| 3181 | goto error; |
| 3182 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3183 | goto gen_global; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3184 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3185 | |
| 3186 | /* XXX: pickle.py doesn't check neither the type, nor the range |
| 3187 | of the value returned by the extension_registry. It should for |
| 3188 | consistency. */ |
| 3189 | |
| 3190 | /* Verify code_obj has the right type and value. */ |
| 3191 | if (!PyLong_Check(code_obj)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3192 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3193 | "Can't pickle %R: extension code %R isn't an integer", |
| 3194 | obj, code_obj); |
| 3195 | goto error; |
| 3196 | } |
| 3197 | code = PyLong_AS_LONG(code_obj); |
| 3198 | if (code <= 0 || code > 0x7fffffffL) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3199 | if (!PyErr_Occurred()) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3200 | PyErr_Format(st->PicklingError, "Can't pickle %R: extension " |
| 3201 | "code %ld is out of range", obj, code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3202 | goto error; |
| 3203 | } |
| 3204 | |
| 3205 | /* Generate an EXT opcode. */ |
| 3206 | if (code <= 0xff) { |
| 3207 | pdata[0] = EXT1; |
| 3208 | pdata[1] = (unsigned char)code; |
| 3209 | n = 2; |
| 3210 | } |
| 3211 | else if (code <= 0xffff) { |
| 3212 | pdata[0] = EXT2; |
| 3213 | pdata[1] = (unsigned char)(code & 0xff); |
| 3214 | pdata[2] = (unsigned char)((code >> 8) & 0xff); |
| 3215 | n = 3; |
| 3216 | } |
| 3217 | else { |
| 3218 | pdata[0] = EXT4; |
| 3219 | pdata[1] = (unsigned char)(code & 0xff); |
| 3220 | pdata[2] = (unsigned char)((code >> 8) & 0xff); |
| 3221 | pdata[3] = (unsigned char)((code >> 16) & 0xff); |
| 3222 | pdata[4] = (unsigned char)((code >> 24) & 0xff); |
| 3223 | n = 5; |
| 3224 | } |
| 3225 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3226 | if (_Pickler_Write(self, pdata, n) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3227 | goto error; |
| 3228 | } |
| 3229 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3230 | gen_global: |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3231 | if (self->proto >= 4) { |
| 3232 | const char stack_global_op = STACK_GLOBAL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3233 | |
Christian Heimes | e8b1ba1 | 2013-11-23 21:13:39 +0100 | [diff] [blame] | 3234 | if (save(self, module_name, 0) < 0) |
| 3235 | goto error; |
| 3236 | if (save(self, global_name, 0) < 0) |
| 3237 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3238 | |
| 3239 | if (_Pickler_Write(self, &stack_global_op, 1) < 0) |
| 3240 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3241 | } |
| 3242 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3243 | /* Generate a normal global opcode if we are using a pickle |
| 3244 | protocol < 4, or if the object is not registered in the |
| 3245 | extension registry. */ |
| 3246 | PyObject *encoded; |
| 3247 | PyObject *(*unicode_encoder)(PyObject *); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3248 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3249 | if (_Pickler_Write(self, &global_op, 1) < 0) |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3250 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3251 | |
| 3252 | /* For protocol < 3 and if the user didn't request against doing |
| 3253 | so, we convert module names to the old 2.x module names. */ |
| 3254 | if (self->proto < 3 && self->fix_imports) { |
| 3255 | if (fix_imports(&module_name, &global_name) < 0) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3256 | goto error; |
| 3257 | } |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3258 | } |
| 3259 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3260 | /* Since Python 3.0 now supports non-ASCII identifiers, we encode |
| 3261 | both the module name and the global name using UTF-8. We do so |
| 3262 | only when we are using the pickle protocol newer than version |
| 3263 | 3. This is to ensure compatibility with older Unpickler running |
| 3264 | on Python 2.x. */ |
| 3265 | if (self->proto == 3) { |
| 3266 | unicode_encoder = PyUnicode_AsUTF8String; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3267 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3268 | else { |
| 3269 | unicode_encoder = PyUnicode_AsASCIIString; |
| 3270 | } |
| 3271 | encoded = unicode_encoder(module_name); |
| 3272 | if (encoded == NULL) { |
| 3273 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3274 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3275 | "can't pickle module identifier '%S' using " |
| 3276 | "pickle protocol %i", |
| 3277 | module_name, self->proto); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3278 | goto error; |
| 3279 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3280 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), |
| 3281 | PyBytes_GET_SIZE(encoded)) < 0) { |
| 3282 | Py_DECREF(encoded); |
| 3283 | goto error; |
| 3284 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3285 | Py_DECREF(encoded); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3286 | if(_Pickler_Write(self, "\n", 1) < 0) |
| 3287 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3288 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3289 | /* Save the name of the module. */ |
| 3290 | encoded = unicode_encoder(global_name); |
| 3291 | if (encoded == NULL) { |
| 3292 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3293 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3294 | "can't pickle global identifier '%S' using " |
| 3295 | "pickle protocol %i", |
| 3296 | global_name, self->proto); |
| 3297 | goto error; |
| 3298 | } |
| 3299 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), |
| 3300 | PyBytes_GET_SIZE(encoded)) < 0) { |
| 3301 | Py_DECREF(encoded); |
| 3302 | goto error; |
| 3303 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3304 | Py_DECREF(encoded); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3305 | if (_Pickler_Write(self, "\n", 1) < 0) |
| 3306 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3307 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3308 | /* Memoize the object. */ |
| 3309 | if (memo_put(self, obj) < 0) |
| 3310 | goto error; |
| 3311 | } |
| 3312 | |
| 3313 | if (0) { |
| 3314 | error: |
| 3315 | status = -1; |
| 3316 | } |
| 3317 | Py_XDECREF(module_name); |
| 3318 | Py_XDECREF(global_name); |
| 3319 | Py_XDECREF(module); |
| 3320 | |
| 3321 | return status; |
| 3322 | } |
| 3323 | |
| 3324 | static int |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3325 | save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton) |
| 3326 | { |
| 3327 | PyObject *reduce_value; |
| 3328 | int status; |
| 3329 | |
| 3330 | reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton); |
| 3331 | if (reduce_value == NULL) { |
| 3332 | return -1; |
| 3333 | } |
| 3334 | status = save_reduce(self, reduce_value, obj); |
| 3335 | Py_DECREF(reduce_value); |
| 3336 | return status; |
| 3337 | } |
| 3338 | |
| 3339 | static int |
| 3340 | save_type(PicklerObject *self, PyObject *obj) |
| 3341 | { |
Alexandre Vassalotti | 65846c6 | 2013-11-30 17:55:48 -0800 | [diff] [blame] | 3342 | if (obj == (PyObject *)&_PyNone_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3343 | return save_singleton_type(self, obj, Py_None); |
| 3344 | } |
| 3345 | else if (obj == (PyObject *)&PyEllipsis_Type) { |
| 3346 | return save_singleton_type(self, obj, Py_Ellipsis); |
| 3347 | } |
Alexandre Vassalotti | 65846c6 | 2013-11-30 17:55:48 -0800 | [diff] [blame] | 3348 | else if (obj == (PyObject *)&_PyNotImplemented_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3349 | return save_singleton_type(self, obj, Py_NotImplemented); |
| 3350 | } |
| 3351 | return save_global(self, obj, NULL); |
| 3352 | } |
| 3353 | |
| 3354 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3355 | save_pers(PicklerObject *self, PyObject *obj, PyObject *func) |
| 3356 | { |
| 3357 | PyObject *pid = NULL; |
| 3358 | int status = 0; |
| 3359 | |
| 3360 | const char persid_op = PERSID; |
| 3361 | const char binpersid_op = BINPERSID; |
| 3362 | |
| 3363 | Py_INCREF(obj); |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 3364 | pid = _Pickle_FastCall(func, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3365 | if (pid == NULL) |
| 3366 | return -1; |
| 3367 | |
| 3368 | if (pid != Py_None) { |
| 3369 | if (self->bin) { |
| 3370 | if (save(self, pid, 1) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3371 | _Pickler_Write(self, &binpersid_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3372 | goto error; |
| 3373 | } |
| 3374 | else { |
| 3375 | PyObject *pid_str = NULL; |
| 3376 | char *pid_ascii_bytes; |
| 3377 | Py_ssize_t size; |
| 3378 | |
| 3379 | pid_str = PyObject_Str(pid); |
| 3380 | if (pid_str == NULL) |
| 3381 | goto error; |
| 3382 | |
| 3383 | /* XXX: Should it check whether the persistent id only contains |
| 3384 | ASCII characters? And what if the pid contains embedded |
| 3385 | newlines? */ |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 3386 | pid_ascii_bytes = _PyUnicode_AsStringAndSize(pid_str, &size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3387 | Py_DECREF(pid_str); |
| 3388 | if (pid_ascii_bytes == NULL) |
| 3389 | goto error; |
| 3390 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3391 | if (_Pickler_Write(self, &persid_op, 1) < 0 || |
| 3392 | _Pickler_Write(self, pid_ascii_bytes, size) < 0 || |
| 3393 | _Pickler_Write(self, "\n", 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3394 | goto error; |
| 3395 | } |
| 3396 | status = 1; |
| 3397 | } |
| 3398 | |
| 3399 | if (0) { |
| 3400 | error: |
| 3401 | status = -1; |
| 3402 | } |
| 3403 | Py_XDECREF(pid); |
| 3404 | |
| 3405 | return status; |
| 3406 | } |
| 3407 | |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3408 | static PyObject * |
| 3409 | get_class(PyObject *obj) |
| 3410 | { |
| 3411 | PyObject *cls; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3412 | _Py_IDENTIFIER(__class__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3413 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3414 | cls = _PyObject_GetAttrId(obj, &PyId___class__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3415 | if (cls == NULL) { |
| 3416 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 3417 | PyErr_Clear(); |
| 3418 | cls = (PyObject *) Py_TYPE(obj); |
| 3419 | Py_INCREF(cls); |
| 3420 | } |
| 3421 | } |
| 3422 | return cls; |
| 3423 | } |
| 3424 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3425 | /* We're saving obj, and args is the 2-thru-5 tuple returned by the |
| 3426 | * appropriate __reduce__ method for obj. |
| 3427 | */ |
| 3428 | static int |
| 3429 | save_reduce(PicklerObject *self, PyObject *args, PyObject *obj) |
| 3430 | { |
| 3431 | PyObject *callable; |
| 3432 | PyObject *argtup; |
| 3433 | PyObject *state = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3434 | PyObject *listitems = Py_None; |
| 3435 | PyObject *dictitems = Py_None; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3436 | PickleState *st = _Pickle_GetGlobalState(); |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3437 | Py_ssize_t size; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3438 | int use_newobj = 0, use_newobj_ex = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3439 | |
| 3440 | const char reduce_op = REDUCE; |
| 3441 | const char build_op = BUILD; |
| 3442 | const char newobj_op = NEWOBJ; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3443 | const char newobj_ex_op = NEWOBJ_EX; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3444 | |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3445 | size = PyTuple_Size(args); |
| 3446 | if (size < 2 || size > 5) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3447 | PyErr_SetString(st->PicklingError, "tuple returned by " |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3448 | "__reduce__ must contain 2 through 5 elements"); |
| 3449 | return -1; |
| 3450 | } |
| 3451 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3452 | if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5, |
| 3453 | &callable, &argtup, &state, &listitems, &dictitems)) |
| 3454 | return -1; |
| 3455 | |
| 3456 | if (!PyCallable_Check(callable)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3457 | PyErr_SetString(st->PicklingError, "first item of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3458 | "returned by __reduce__ must be callable"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3459 | return -1; |
| 3460 | } |
| 3461 | if (!PyTuple_Check(argtup)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3462 | PyErr_SetString(st->PicklingError, "second item of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3463 | "returned by __reduce__ must be a tuple"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3464 | return -1; |
| 3465 | } |
| 3466 | |
| 3467 | if (state == Py_None) |
| 3468 | state = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3469 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3470 | if (listitems == Py_None) |
| 3471 | listitems = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3472 | else if (!PyIter_Check(listitems)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3473 | PyErr_Format(st->PicklingError, "fourth element of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3474 | "returned by __reduce__ must be an iterator, not %s", |
| 3475 | Py_TYPE(listitems)->tp_name); |
| 3476 | return -1; |
| 3477 | } |
| 3478 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3479 | if (dictitems == Py_None) |
| 3480 | dictitems = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3481 | else if (!PyIter_Check(dictitems)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3482 | PyErr_Format(st->PicklingError, "fifth element of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3483 | "returned by __reduce__ must be an iterator, not %s", |
| 3484 | Py_TYPE(dictitems)->tp_name); |
| 3485 | return -1; |
| 3486 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3487 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3488 | if (self->proto >= 2) { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3489 | PyObject *name; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3490 | _Py_IDENTIFIER(__name__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3491 | |
Victor Stinner | 804e05e | 2013-11-14 01:26:17 +0100 | [diff] [blame] | 3492 | name = _PyObject_GetAttrId(callable, &PyId___name__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3493 | if (name == NULL) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3494 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3495 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3496 | } |
| 3497 | PyErr_Clear(); |
| 3498 | } |
| 3499 | else if (self->proto >= 4) { |
| 3500 | _Py_IDENTIFIER(__newobj_ex__); |
| 3501 | use_newobj_ex = PyUnicode_Check(name) && |
| 3502 | PyUnicode_Compare( |
| 3503 | name, _PyUnicode_FromId(&PyId___newobj_ex__)) == 0; |
| 3504 | Py_DECREF(name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3505 | } |
| 3506 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3507 | _Py_IDENTIFIER(__newobj__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3508 | use_newobj = PyUnicode_Check(name) && |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3509 | PyUnicode_Compare( |
| 3510 | name, _PyUnicode_FromId(&PyId___newobj__)) == 0; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3511 | Py_DECREF(name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3512 | } |
| 3513 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3514 | |
| 3515 | if (use_newobj_ex) { |
| 3516 | PyObject *cls; |
| 3517 | PyObject *args; |
| 3518 | PyObject *kwargs; |
| 3519 | |
| 3520 | if (Py_SIZE(argtup) != 3) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3521 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3522 | "length of the NEWOBJ_EX argument tuple must be " |
| 3523 | "exactly 3, not %zd", Py_SIZE(argtup)); |
| 3524 | return -1; |
| 3525 | } |
| 3526 | |
| 3527 | cls = PyTuple_GET_ITEM(argtup, 0); |
| 3528 | if (!PyType_Check(cls)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3529 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3530 | "first item from NEWOBJ_EX argument tuple must " |
| 3531 | "be a class, not %.200s", Py_TYPE(cls)->tp_name); |
| 3532 | return -1; |
| 3533 | } |
| 3534 | args = PyTuple_GET_ITEM(argtup, 1); |
| 3535 | if (!PyTuple_Check(args)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3536 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3537 | "second item from NEWOBJ_EX argument tuple must " |
| 3538 | "be a tuple, not %.200s", Py_TYPE(args)->tp_name); |
| 3539 | return -1; |
| 3540 | } |
| 3541 | kwargs = PyTuple_GET_ITEM(argtup, 2); |
| 3542 | if (!PyDict_Check(kwargs)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3543 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3544 | "third item from NEWOBJ_EX argument tuple must " |
| 3545 | "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name); |
| 3546 | return -1; |
| 3547 | } |
| 3548 | |
| 3549 | if (save(self, cls, 0) < 0 || |
| 3550 | save(self, args, 0) < 0 || |
| 3551 | save(self, kwargs, 0) < 0 || |
| 3552 | _Pickler_Write(self, &newobj_ex_op, 1) < 0) { |
| 3553 | return -1; |
| 3554 | } |
| 3555 | } |
| 3556 | else if (use_newobj) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3557 | PyObject *cls; |
| 3558 | PyObject *newargtup; |
| 3559 | PyObject *obj_class; |
| 3560 | int p; |
| 3561 | |
| 3562 | /* Sanity checks. */ |
| 3563 | if (Py_SIZE(argtup) < 1) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3564 | PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3565 | return -1; |
| 3566 | } |
| 3567 | |
| 3568 | cls = PyTuple_GET_ITEM(argtup, 0); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3569 | if (!PyType_Check(cls)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3570 | PyErr_SetString(st->PicklingError, "args[0] from " |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3571 | "__newobj__ args is not a type"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3572 | return -1; |
| 3573 | } |
| 3574 | |
| 3575 | if (obj != NULL) { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3576 | obj_class = get_class(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3577 | p = obj_class != cls; /* true iff a problem */ |
| 3578 | Py_DECREF(obj_class); |
| 3579 | if (p) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3580 | PyErr_SetString(st->PicklingError, "args[0] from " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3581 | "__newobj__ args has the wrong class"); |
| 3582 | return -1; |
| 3583 | } |
| 3584 | } |
| 3585 | /* XXX: These calls save() are prone to infinite recursion. Imagine |
| 3586 | what happen if the value returned by the __reduce__() method of |
| 3587 | some extension type contains another object of the same type. Ouch! |
| 3588 | |
| 3589 | Here is a quick example, that I ran into, to illustrate what I |
| 3590 | mean: |
| 3591 | |
| 3592 | >>> import pickle, copyreg |
| 3593 | >>> copyreg.dispatch_table.pop(complex) |
| 3594 | >>> pickle.dumps(1+2j) |
| 3595 | Traceback (most recent call last): |
| 3596 | ... |
| 3597 | RuntimeError: maximum recursion depth exceeded |
| 3598 | |
| 3599 | Removing the complex class from copyreg.dispatch_table made the |
| 3600 | __reduce_ex__() method emit another complex object: |
| 3601 | |
| 3602 | >>> (1+1j).__reduce_ex__(2) |
| 3603 | (<function __newobj__ at 0xb7b71c3c>, |
| 3604 | (<class 'complex'>, (1+1j)), None, None, None) |
| 3605 | |
| 3606 | Thus when save() was called on newargstup (the 2nd item) recursion |
| 3607 | ensued. Of course, the bug was in the complex class which had a |
| 3608 | broken __getnewargs__() that emitted another complex object. But, |
| 3609 | the point, here, is it is quite easy to end up with a broken reduce |
| 3610 | function. */ |
| 3611 | |
| 3612 | /* Save the class and its __new__ arguments. */ |
| 3613 | if (save(self, cls, 0) < 0) |
| 3614 | return -1; |
| 3615 | |
| 3616 | newargtup = PyTuple_GetSlice(argtup, 1, Py_SIZE(argtup)); |
| 3617 | if (newargtup == NULL) |
| 3618 | return -1; |
| 3619 | |
| 3620 | p = save(self, newargtup, 0); |
| 3621 | Py_DECREF(newargtup); |
| 3622 | if (p < 0) |
| 3623 | return -1; |
| 3624 | |
| 3625 | /* Add NEWOBJ opcode. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3626 | if (_Pickler_Write(self, &newobj_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3627 | return -1; |
| 3628 | } |
| 3629 | else { /* Not using NEWOBJ. */ |
| 3630 | if (save(self, callable, 0) < 0 || |
| 3631 | save(self, argtup, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3632 | _Pickler_Write(self, &reduce_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3633 | return -1; |
| 3634 | } |
| 3635 | |
| 3636 | /* obj can be NULL when save_reduce() is used directly. A NULL obj means |
| 3637 | the caller do not want to memoize the object. Not particularly useful, |
| 3638 | but that is to mimic the behavior save_reduce() in pickle.py when |
| 3639 | obj is None. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3640 | if (obj != NULL) { |
| 3641 | /* If the object is already in the memo, this means it is |
| 3642 | recursive. In this case, throw away everything we put on the |
| 3643 | stack, and fetch the object back from the memo. */ |
| 3644 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3645 | const char pop_op = POP; |
| 3646 | |
| 3647 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 3648 | return -1; |
| 3649 | if (memo_get(self, obj) < 0) |
| 3650 | return -1; |
| 3651 | |
| 3652 | return 0; |
| 3653 | } |
| 3654 | else if (memo_put(self, obj) < 0) |
| 3655 | return -1; |
| 3656 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3657 | |
| 3658 | if (listitems && batch_list(self, listitems) < 0) |
| 3659 | return -1; |
| 3660 | |
| 3661 | if (dictitems && batch_dict(self, dictitems) < 0) |
| 3662 | return -1; |
| 3663 | |
| 3664 | if (state) { |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 3665 | if (save(self, state, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3666 | _Pickler_Write(self, &build_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3667 | return -1; |
| 3668 | } |
| 3669 | |
| 3670 | return 0; |
| 3671 | } |
| 3672 | |
| 3673 | static int |
| 3674 | save(PicklerObject *self, PyObject *obj, int pers_save) |
| 3675 | { |
| 3676 | PyTypeObject *type; |
| 3677 | PyObject *reduce_func = NULL; |
| 3678 | PyObject *reduce_value = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3679 | int status = 0; |
| 3680 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 3681 | if (_Pickler_OpcodeBoundary(self) < 0) |
| 3682 | return -1; |
| 3683 | |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 3684 | if (Py_EnterRecursiveCall(" while pickling an object")) |
Alexandre Vassalotti | dff1834 | 2008-07-13 18:48:30 +0000 | [diff] [blame] | 3685 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3686 | |
| 3687 | /* The extra pers_save argument is necessary to avoid calling save_pers() |
| 3688 | on its returned object. */ |
| 3689 | if (!pers_save && self->pers_func) { |
| 3690 | /* save_pers() returns: |
| 3691 | -1 to signal an error; |
| 3692 | 0 if it did nothing successfully; |
| 3693 | 1 if a persistent id was saved. |
| 3694 | */ |
| 3695 | if ((status = save_pers(self, obj, self->pers_func)) != 0) |
| 3696 | goto done; |
| 3697 | } |
| 3698 | |
| 3699 | type = Py_TYPE(obj); |
| 3700 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3701 | /* The old cPickle had an optimization that used switch-case statement |
| 3702 | dispatching on the first letter of the type name. This has was removed |
| 3703 | since benchmarks shown that this optimization was actually slowing |
| 3704 | things down. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3705 | |
| 3706 | /* Atom types; these aren't memoized, so don't check the memo. */ |
| 3707 | |
| 3708 | if (obj == Py_None) { |
| 3709 | status = save_none(self, obj); |
| 3710 | goto done; |
| 3711 | } |
| 3712 | else if (obj == Py_False || obj == Py_True) { |
| 3713 | status = save_bool(self, obj); |
| 3714 | goto done; |
| 3715 | } |
| 3716 | else if (type == &PyLong_Type) { |
| 3717 | status = save_long(self, obj); |
| 3718 | goto done; |
| 3719 | } |
| 3720 | else if (type == &PyFloat_Type) { |
| 3721 | status = save_float(self, obj); |
| 3722 | goto done; |
| 3723 | } |
| 3724 | |
| 3725 | /* Check the memo to see if it has the object. If so, generate |
| 3726 | a GET (or BINGET) opcode, instead of pickling the object |
| 3727 | once again. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3728 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3729 | if (memo_get(self, obj) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3730 | goto error; |
| 3731 | goto done; |
| 3732 | } |
| 3733 | |
| 3734 | if (type == &PyBytes_Type) { |
| 3735 | status = save_bytes(self, obj); |
| 3736 | goto done; |
| 3737 | } |
| 3738 | else if (type == &PyUnicode_Type) { |
| 3739 | status = save_unicode(self, obj); |
| 3740 | goto done; |
| 3741 | } |
| 3742 | else if (type == &PyDict_Type) { |
| 3743 | status = save_dict(self, obj); |
| 3744 | goto done; |
| 3745 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3746 | else if (type == &PySet_Type) { |
| 3747 | status = save_set(self, obj); |
| 3748 | goto done; |
| 3749 | } |
| 3750 | else if (type == &PyFrozenSet_Type) { |
| 3751 | status = save_frozenset(self, obj); |
| 3752 | goto done; |
| 3753 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3754 | else if (type == &PyList_Type) { |
| 3755 | status = save_list(self, obj); |
| 3756 | goto done; |
| 3757 | } |
| 3758 | else if (type == &PyTuple_Type) { |
| 3759 | status = save_tuple(self, obj); |
| 3760 | goto done; |
| 3761 | } |
| 3762 | else if (type == &PyType_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3763 | status = save_type(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3764 | goto done; |
| 3765 | } |
| 3766 | else if (type == &PyFunction_Type) { |
| 3767 | status = save_global(self, obj, NULL); |
Alexandre Vassalotti | fc91285 | 2013-11-24 03:07:35 -0800 | [diff] [blame] | 3768 | goto done; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3769 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3770 | |
| 3771 | /* XXX: This part needs some unit tests. */ |
| 3772 | |
| 3773 | /* Get a reduction callable, and call it. This may come from |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3774 | * self.dispatch_table, copyreg.dispatch_table, the object's |
| 3775 | * __reduce_ex__ method, or the object's __reduce__ method. |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3776 | */ |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3777 | if (self->dispatch_table == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3778 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3779 | reduce_func = PyDict_GetItemWithError(st->dispatch_table, |
| 3780 | (PyObject *)type); |
| 3781 | if (reduce_func == NULL) { |
| 3782 | if (PyErr_Occurred()) { |
| 3783 | goto error; |
| 3784 | } |
| 3785 | } else { |
| 3786 | /* PyDict_GetItemWithError() returns a borrowed reference. |
| 3787 | Increase the reference count to be consistent with |
| 3788 | PyObject_GetItem and _PyObject_GetAttrId used below. */ |
| 3789 | Py_INCREF(reduce_func); |
| 3790 | } |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3791 | } else { |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3792 | reduce_func = PyObject_GetItem(self->dispatch_table, |
| 3793 | (PyObject *)type); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3794 | if (reduce_func == NULL) { |
| 3795 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 3796 | PyErr_Clear(); |
| 3797 | else |
| 3798 | goto error; |
| 3799 | } |
| 3800 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3801 | if (reduce_func != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3802 | Py_INCREF(obj); |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 3803 | reduce_value = _Pickle_FastCall(reduce_func, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3804 | } |
Antoine Pitrou | ffd41d9 | 2011-10-04 09:23:04 +0200 | [diff] [blame] | 3805 | else if (PyType_IsSubtype(type, &PyType_Type)) { |
| 3806 | status = save_global(self, obj, NULL); |
| 3807 | goto done; |
| 3808 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3809 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3810 | _Py_IDENTIFIER(__reduce__); |
| 3811 | _Py_IDENTIFIER(__reduce_ex__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3812 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3813 | |
| 3814 | /* XXX: If the __reduce__ method is defined, __reduce_ex__ is |
| 3815 | automatically defined as __reduce__. While this is convenient, this |
| 3816 | make it impossible to know which method was actually called. Of |
| 3817 | course, this is not a big deal. But still, it would be nice to let |
| 3818 | the user know which method was called when something go |
| 3819 | wrong. Incidentally, this means if __reduce_ex__ is not defined, we |
| 3820 | don't actually have to check for a __reduce__ method. */ |
| 3821 | |
| 3822 | /* Check for a __reduce_ex__ method. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3823 | reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3824 | if (reduce_func != NULL) { |
| 3825 | PyObject *proto; |
| 3826 | proto = PyLong_FromLong(self->proto); |
| 3827 | if (proto != NULL) { |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 3828 | reduce_value = _Pickle_FastCall(reduce_func, proto); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3829 | } |
| 3830 | } |
| 3831 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3832 | PickleState *st = _Pickle_GetGlobalState(); |
| 3833 | |
| 3834 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3835 | PyErr_Clear(); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3836 | } |
| 3837 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3838 | goto error; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3839 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3840 | /* Check for a __reduce__ method. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3841 | reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3842 | if (reduce_func != NULL) { |
Alexandre Vassalotti | 6bf41e5 | 2013-11-28 15:17:29 -0800 | [diff] [blame] | 3843 | PyObject *empty_tuple = PyTuple_New(0); |
| 3844 | reduce_value = PyObject_Call(reduce_func, empty_tuple, |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3845 | NULL); |
Alexandre Vassalotti | 6bf41e5 | 2013-11-28 15:17:29 -0800 | [diff] [blame] | 3846 | Py_DECREF(empty_tuple); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3847 | } |
| 3848 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3849 | PyErr_Format(st->PicklingError, |
| 3850 | "can't pickle '%.200s' object: %R", |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3851 | type->tp_name, obj); |
| 3852 | goto error; |
| 3853 | } |
| 3854 | } |
| 3855 | } |
| 3856 | |
| 3857 | if (reduce_value == NULL) |
| 3858 | goto error; |
| 3859 | |
| 3860 | if (PyUnicode_Check(reduce_value)) { |
| 3861 | status = save_global(self, obj, reduce_value); |
| 3862 | goto done; |
| 3863 | } |
| 3864 | |
| 3865 | if (!PyTuple_Check(reduce_value)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3866 | PickleState *st = _Pickle_GetGlobalState(); |
| 3867 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3868 | "__reduce__ must return a string or tuple"); |
| 3869 | goto error; |
| 3870 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3871 | |
| 3872 | status = save_reduce(self, reduce_value, obj); |
| 3873 | |
| 3874 | if (0) { |
| 3875 | error: |
| 3876 | status = -1; |
| 3877 | } |
| 3878 | done: |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 3879 | |
Alexandre Vassalotti | dff1834 | 2008-07-13 18:48:30 +0000 | [diff] [blame] | 3880 | Py_LeaveRecursiveCall(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3881 | Py_XDECREF(reduce_func); |
| 3882 | Py_XDECREF(reduce_value); |
| 3883 | |
| 3884 | return status; |
| 3885 | } |
| 3886 | |
| 3887 | static int |
| 3888 | dump(PicklerObject *self, PyObject *obj) |
| 3889 | { |
| 3890 | const char stop_op = STOP; |
| 3891 | |
| 3892 | if (self->proto >= 2) { |
| 3893 | char header[2]; |
| 3894 | |
| 3895 | header[0] = PROTO; |
| 3896 | assert(self->proto >= 0 && self->proto < 256); |
| 3897 | header[1] = (unsigned char)self->proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3898 | if (_Pickler_Write(self, header, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3899 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3900 | if (self->proto >= 4) |
| 3901 | self->framing = 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3902 | } |
| 3903 | |
| 3904 | if (save(self, obj, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3905 | _Pickler_Write(self, &stop_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3906 | return -1; |
| 3907 | |
| 3908 | return 0; |
| 3909 | } |
| 3910 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3911 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3912 | |
| 3913 | _pickle.Pickler.clear_memo |
| 3914 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3915 | Clears the pickler's "memo". |
| 3916 | |
| 3917 | The memo is the data structure that remembers which objects the |
| 3918 | pickler has already seen, so that shared or recursive objects are |
| 3919 | pickled by reference and not by value. This method is useful when |
| 3920 | re-using picklers. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3921 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3922 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 3923 | static PyObject * |
| 3924 | _pickle_Pickler_clear_memo_impl(PicklerObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 3925 | /*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3926 | { |
| 3927 | if (self->memo) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3928 | PyMemoTable_Clear(self->memo); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3929 | |
| 3930 | Py_RETURN_NONE; |
| 3931 | } |
| 3932 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3933 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3934 | |
| 3935 | _pickle.Pickler.dump |
| 3936 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3937 | obj: object |
| 3938 | / |
| 3939 | |
| 3940 | Write a pickled representation of the given object to the open file. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3941 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3942 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3943 | static PyObject * |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3944 | _pickle_Pickler_dump(PicklerObject *self, PyObject *obj) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 3945 | /*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3946 | { |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 3947 | /* Check whether the Pickler was initialized correctly (issue3664). |
| 3948 | Developers often forget to call __init__() in their subclasses, which |
| 3949 | would trigger a segfault without this check. */ |
| 3950 | if (self->write == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3951 | PickleState *st = _Pickle_GetGlobalState(); |
| 3952 | PyErr_Format(st->PicklingError, |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 3953 | "Pickler.__init__() was not called by %s.__init__()", |
| 3954 | Py_TYPE(self)->tp_name); |
| 3955 | return NULL; |
| 3956 | } |
| 3957 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3958 | if (_Pickler_ClearBuffer(self) < 0) |
| 3959 | return NULL; |
| 3960 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3961 | if (dump(self, obj) < 0) |
| 3962 | return NULL; |
| 3963 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3964 | if (_Pickler_FlushToFile(self) < 0) |
| 3965 | return NULL; |
| 3966 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3967 | Py_RETURN_NONE; |
| 3968 | } |
| 3969 | |
| 3970 | static struct PyMethodDef Pickler_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3971 | _PICKLE_PICKLER_DUMP_METHODDEF |
| 3972 | _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3973 | {NULL, NULL} /* sentinel */ |
| 3974 | }; |
| 3975 | |
| 3976 | static void |
| 3977 | Pickler_dealloc(PicklerObject *self) |
| 3978 | { |
| 3979 | PyObject_GC_UnTrack(self); |
| 3980 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3981 | Py_XDECREF(self->output_buffer); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3982 | Py_XDECREF(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3983 | Py_XDECREF(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3984 | Py_XDECREF(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3985 | Py_XDECREF(self->fast_memo); |
| 3986 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3987 | PyMemoTable_Del(self->memo); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3988 | |
| 3989 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 3990 | } |
| 3991 | |
| 3992 | static int |
| 3993 | Pickler_traverse(PicklerObject *self, visitproc visit, void *arg) |
| 3994 | { |
| 3995 | Py_VISIT(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3996 | Py_VISIT(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3997 | Py_VISIT(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3998 | Py_VISIT(self->fast_memo); |
| 3999 | return 0; |
| 4000 | } |
| 4001 | |
| 4002 | static int |
| 4003 | Pickler_clear(PicklerObject *self) |
| 4004 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4005 | Py_CLEAR(self->output_buffer); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4006 | Py_CLEAR(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4007 | Py_CLEAR(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4008 | Py_CLEAR(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4009 | Py_CLEAR(self->fast_memo); |
| 4010 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4011 | if (self->memo != NULL) { |
| 4012 | PyMemoTable *memo = self->memo; |
| 4013 | self->memo = NULL; |
| 4014 | PyMemoTable_Del(memo); |
| 4015 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4016 | return 0; |
| 4017 | } |
| 4018 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4019 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4020 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4021 | |
| 4022 | _pickle.Pickler.__init__ |
| 4023 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4024 | file: object |
| 4025 | protocol: object = NULL |
| 4026 | fix_imports: bool = True |
| 4027 | |
| 4028 | This takes a binary file for writing a pickle data stream. |
| 4029 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4030 | The optional *protocol* argument tells the pickler to use the given |
| 4031 | protocol; supported protocols are 0, 1, 2, 3 and 4. The default |
| 4032 | protocol is 3; a backward-incompatible protocol designed for Python 3. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4033 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4034 | Specifying a negative protocol version selects the highest protocol |
| 4035 | version supported. The higher the protocol used, the more recent the |
| 4036 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4037 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4038 | The *file* argument must have a write() method that accepts a single |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4039 | bytes argument. It can thus be a file object opened for binary |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4040 | writing, a io.BytesIO instance, or any other custom object that meets |
| 4041 | this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4042 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4043 | If *fix_imports* is True and protocol is less than 3, pickle will try |
| 4044 | to map the new Python 3 names to the old module names used in Python |
| 4045 | 2, so that the pickle data stream is readable with Python 2. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4046 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4047 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4048 | static int |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4049 | _pickle_Pickler___init___impl(PicklerObject *self, PyObject *file, PyObject *protocol, int fix_imports) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4050 | /*[clinic end generated code: output=56e229f3b1f4332f input=b8cdeb7e3f5ee674]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4051 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4052 | _Py_IDENTIFIER(persistent_id); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4053 | _Py_IDENTIFIER(dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4054 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4055 | /* In case of multiple __init__() calls, clear previous content. */ |
| 4056 | if (self->write != NULL) |
| 4057 | (void)Pickler_clear(self); |
| 4058 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4059 | if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4060 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4061 | |
| 4062 | if (_Pickler_SetOutputStream(self, file) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4063 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4064 | |
| 4065 | /* memo and output_buffer may have already been created in _Pickler_New */ |
| 4066 | if (self->memo == NULL) { |
| 4067 | self->memo = PyMemoTable_New(); |
| 4068 | if (self->memo == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4069 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4070 | } |
| 4071 | self->output_len = 0; |
| 4072 | if (self->output_buffer == NULL) { |
| 4073 | self->max_output_len = WRITE_BUF_SIZE; |
| 4074 | self->output_buffer = PyBytes_FromStringAndSize(NULL, |
| 4075 | self->max_output_len); |
| 4076 | if (self->output_buffer == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4077 | return -1; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 4078 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4079 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 4080 | self->fast = 0; |
| 4081 | self->fast_nesting = 0; |
| 4082 | self->fast_memo = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4083 | self->pers_func = NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 4084 | if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) { |
| 4085 | self->pers_func = _PyObject_GetAttrId((PyObject *)self, |
| 4086 | &PyId_persistent_id); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4087 | if (self->pers_func == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4088 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4089 | } |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4090 | self->dispatch_table = NULL; |
| 4091 | if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) { |
| 4092 | self->dispatch_table = _PyObject_GetAttrId((PyObject *)self, |
| 4093 | &PyId_dispatch_table); |
| 4094 | if (self->dispatch_table == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4095 | return -1; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4096 | } |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4097 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4098 | return 0; |
| 4099 | } |
| 4100 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4101 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4102 | /* Define a proxy object for the Pickler's internal memo object. This is to |
| 4103 | * avoid breaking code like: |
| 4104 | * pickler.memo.clear() |
| 4105 | * and |
| 4106 | * pickler.memo = saved_memo |
| 4107 | * Is this a good idea? Not really, but we don't want to break code that uses |
| 4108 | * it. Note that we don't implement the entire mapping API here. This is |
| 4109 | * intentional, as these should be treated as black-box implementation details. |
| 4110 | */ |
| 4111 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4112 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4113 | _pickle.PicklerMemoProxy.clear |
| 4114 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4115 | Remove all items from memo. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4116 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4117 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4118 | static PyObject * |
| 4119 | _pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4120 | /*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4121 | { |
| 4122 | if (self->pickler->memo) |
| 4123 | PyMemoTable_Clear(self->pickler->memo); |
| 4124 | Py_RETURN_NONE; |
| 4125 | } |
| 4126 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4127 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4128 | _pickle.PicklerMemoProxy.copy |
| 4129 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4130 | Copy the memo to a new object. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4131 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4132 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4133 | static PyObject * |
| 4134 | _pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4135 | /*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4136 | { |
| 4137 | Py_ssize_t i; |
| 4138 | PyMemoTable *memo; |
| 4139 | PyObject *new_memo = PyDict_New(); |
| 4140 | if (new_memo == NULL) |
| 4141 | return NULL; |
| 4142 | |
| 4143 | memo = self->pickler->memo; |
| 4144 | for (i = 0; i < memo->mt_allocated; ++i) { |
| 4145 | PyMemoEntry entry = memo->mt_table[i]; |
| 4146 | if (entry.me_key != NULL) { |
| 4147 | int status; |
| 4148 | PyObject *key, *value; |
| 4149 | |
| 4150 | key = PyLong_FromVoidPtr(entry.me_key); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4151 | value = Py_BuildValue("nO", entry.me_value, entry.me_key); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4152 | |
| 4153 | if (key == NULL || value == NULL) { |
| 4154 | Py_XDECREF(key); |
| 4155 | Py_XDECREF(value); |
| 4156 | goto error; |
| 4157 | } |
| 4158 | status = PyDict_SetItem(new_memo, key, value); |
| 4159 | Py_DECREF(key); |
| 4160 | Py_DECREF(value); |
| 4161 | if (status < 0) |
| 4162 | goto error; |
| 4163 | } |
| 4164 | } |
| 4165 | return new_memo; |
| 4166 | |
| 4167 | error: |
| 4168 | Py_XDECREF(new_memo); |
| 4169 | return NULL; |
| 4170 | } |
| 4171 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4172 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4173 | _pickle.PicklerMemoProxy.__reduce__ |
| 4174 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4175 | Implement pickle support. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4176 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4177 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4178 | static PyObject * |
| 4179 | _pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4180 | /*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4181 | { |
| 4182 | PyObject *reduce_value, *dict_args; |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4183 | PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4184 | if (contents == NULL) |
| 4185 | return NULL; |
| 4186 | |
| 4187 | reduce_value = PyTuple_New(2); |
| 4188 | if (reduce_value == NULL) { |
| 4189 | Py_DECREF(contents); |
| 4190 | return NULL; |
| 4191 | } |
| 4192 | dict_args = PyTuple_New(1); |
| 4193 | if (dict_args == NULL) { |
| 4194 | Py_DECREF(contents); |
| 4195 | Py_DECREF(reduce_value); |
| 4196 | return NULL; |
| 4197 | } |
| 4198 | PyTuple_SET_ITEM(dict_args, 0, contents); |
| 4199 | Py_INCREF((PyObject *)&PyDict_Type); |
| 4200 | PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type); |
| 4201 | PyTuple_SET_ITEM(reduce_value, 1, dict_args); |
| 4202 | return reduce_value; |
| 4203 | } |
| 4204 | |
| 4205 | static PyMethodDef picklerproxy_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4206 | _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF |
| 4207 | _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF |
| 4208 | _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4209 | {NULL, NULL} /* sentinel */ |
| 4210 | }; |
| 4211 | |
| 4212 | static void |
| 4213 | PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self) |
| 4214 | { |
| 4215 | PyObject_GC_UnTrack(self); |
| 4216 | Py_XDECREF(self->pickler); |
| 4217 | PyObject_GC_Del((PyObject *)self); |
| 4218 | } |
| 4219 | |
| 4220 | static int |
| 4221 | PicklerMemoProxy_traverse(PicklerMemoProxyObject *self, |
| 4222 | visitproc visit, void *arg) |
| 4223 | { |
| 4224 | Py_VISIT(self->pickler); |
| 4225 | return 0; |
| 4226 | } |
| 4227 | |
| 4228 | static int |
| 4229 | PicklerMemoProxy_clear(PicklerMemoProxyObject *self) |
| 4230 | { |
| 4231 | Py_CLEAR(self->pickler); |
| 4232 | return 0; |
| 4233 | } |
| 4234 | |
| 4235 | static PyTypeObject PicklerMemoProxyType = { |
| 4236 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4237 | "_pickle.PicklerMemoProxy", /*tp_name*/ |
| 4238 | sizeof(PicklerMemoProxyObject), /*tp_basicsize*/ |
| 4239 | 0, |
| 4240 | (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */ |
| 4241 | 0, /* tp_print */ |
| 4242 | 0, /* tp_getattr */ |
| 4243 | 0, /* tp_setattr */ |
| 4244 | 0, /* tp_compare */ |
| 4245 | 0, /* tp_repr */ |
| 4246 | 0, /* tp_as_number */ |
| 4247 | 0, /* tp_as_sequence */ |
| 4248 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 4249 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4250 | 0, /* tp_call */ |
| 4251 | 0, /* tp_str */ |
| 4252 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4253 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 4254 | 0, /* tp_as_buffer */ |
| 4255 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 4256 | 0, /* tp_doc */ |
| 4257 | (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */ |
| 4258 | (inquiry)PicklerMemoProxy_clear, /* tp_clear */ |
| 4259 | 0, /* tp_richcompare */ |
| 4260 | 0, /* tp_weaklistoffset */ |
| 4261 | 0, /* tp_iter */ |
| 4262 | 0, /* tp_iternext */ |
| 4263 | picklerproxy_methods, /* tp_methods */ |
| 4264 | }; |
| 4265 | |
| 4266 | static PyObject * |
| 4267 | PicklerMemoProxy_New(PicklerObject *pickler) |
| 4268 | { |
| 4269 | PicklerMemoProxyObject *self; |
| 4270 | |
| 4271 | self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType); |
| 4272 | if (self == NULL) |
| 4273 | return NULL; |
| 4274 | Py_INCREF(pickler); |
| 4275 | self->pickler = pickler; |
| 4276 | PyObject_GC_Track(self); |
| 4277 | return (PyObject *)self; |
| 4278 | } |
| 4279 | |
| 4280 | /*****************************************************************************/ |
| 4281 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4282 | static PyObject * |
| 4283 | Pickler_get_memo(PicklerObject *self) |
| 4284 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4285 | return PicklerMemoProxy_New(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4286 | } |
| 4287 | |
| 4288 | static int |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4289 | Pickler_set_memo(PicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4290 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4291 | PyMemoTable *new_memo = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4292 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4293 | if (obj == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4294 | PyErr_SetString(PyExc_TypeError, |
| 4295 | "attribute deletion is not supported"); |
| 4296 | return -1; |
| 4297 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4298 | |
| 4299 | if (Py_TYPE(obj) == &PicklerMemoProxyType) { |
| 4300 | PicklerObject *pickler = |
| 4301 | ((PicklerMemoProxyObject *)obj)->pickler; |
| 4302 | |
| 4303 | new_memo = PyMemoTable_Copy(pickler->memo); |
| 4304 | if (new_memo == NULL) |
| 4305 | return -1; |
| 4306 | } |
| 4307 | else if (PyDict_Check(obj)) { |
| 4308 | Py_ssize_t i = 0; |
| 4309 | PyObject *key, *value; |
| 4310 | |
| 4311 | new_memo = PyMemoTable_New(); |
| 4312 | if (new_memo == NULL) |
| 4313 | return -1; |
| 4314 | |
| 4315 | while (PyDict_Next(obj, &i, &key, &value)) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4316 | Py_ssize_t memo_id; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4317 | PyObject *memo_obj; |
| 4318 | |
| 4319 | if (!PyTuple_Check(value) || Py_SIZE(value) != 2) { |
| 4320 | PyErr_SetString(PyExc_TypeError, |
| 4321 | "'memo' values must be 2-item tuples"); |
| 4322 | goto error; |
| 4323 | } |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4324 | memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0)); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4325 | if (memo_id == -1 && PyErr_Occurred()) |
| 4326 | goto error; |
| 4327 | memo_obj = PyTuple_GET_ITEM(value, 1); |
| 4328 | if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0) |
| 4329 | goto error; |
| 4330 | } |
| 4331 | } |
| 4332 | else { |
| 4333 | PyErr_Format(PyExc_TypeError, |
| 4334 | "'memo' attribute must be an PicklerMemoProxy object" |
| 4335 | "or dict, not %.200s", Py_TYPE(obj)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4336 | return -1; |
| 4337 | } |
| 4338 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4339 | PyMemoTable_Del(self->memo); |
| 4340 | self->memo = new_memo; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4341 | |
| 4342 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4343 | |
| 4344 | error: |
| 4345 | if (new_memo) |
| 4346 | PyMemoTable_Del(new_memo); |
| 4347 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4348 | } |
| 4349 | |
| 4350 | static PyObject * |
| 4351 | Pickler_get_persid(PicklerObject *self) |
| 4352 | { |
| 4353 | if (self->pers_func == NULL) |
| 4354 | PyErr_SetString(PyExc_AttributeError, "persistent_id"); |
| 4355 | else |
| 4356 | Py_INCREF(self->pers_func); |
| 4357 | return self->pers_func; |
| 4358 | } |
| 4359 | |
| 4360 | static int |
| 4361 | Pickler_set_persid(PicklerObject *self, PyObject *value) |
| 4362 | { |
| 4363 | PyObject *tmp; |
| 4364 | |
| 4365 | if (value == NULL) { |
| 4366 | PyErr_SetString(PyExc_TypeError, |
| 4367 | "attribute deletion is not supported"); |
| 4368 | return -1; |
| 4369 | } |
| 4370 | if (!PyCallable_Check(value)) { |
| 4371 | PyErr_SetString(PyExc_TypeError, |
| 4372 | "persistent_id must be a callable taking one argument"); |
| 4373 | return -1; |
| 4374 | } |
| 4375 | |
| 4376 | tmp = self->pers_func; |
| 4377 | Py_INCREF(value); |
| 4378 | self->pers_func = value; |
| 4379 | Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */ |
| 4380 | |
| 4381 | return 0; |
| 4382 | } |
| 4383 | |
| 4384 | static PyMemberDef Pickler_members[] = { |
| 4385 | {"bin", T_INT, offsetof(PicklerObject, bin)}, |
| 4386 | {"fast", T_INT, offsetof(PicklerObject, fast)}, |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4387 | {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)}, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4388 | {NULL} |
| 4389 | }; |
| 4390 | |
| 4391 | static PyGetSetDef Pickler_getsets[] = { |
| 4392 | {"memo", (getter)Pickler_get_memo, |
| 4393 | (setter)Pickler_set_memo}, |
| 4394 | {"persistent_id", (getter)Pickler_get_persid, |
| 4395 | (setter)Pickler_set_persid}, |
| 4396 | {NULL} |
| 4397 | }; |
| 4398 | |
| 4399 | static PyTypeObject Pickler_Type = { |
| 4400 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4401 | "_pickle.Pickler" , /*tp_name*/ |
| 4402 | sizeof(PicklerObject), /*tp_basicsize*/ |
| 4403 | 0, /*tp_itemsize*/ |
| 4404 | (destructor)Pickler_dealloc, /*tp_dealloc*/ |
| 4405 | 0, /*tp_print*/ |
| 4406 | 0, /*tp_getattr*/ |
| 4407 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 4408 | 0, /*tp_reserved*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4409 | 0, /*tp_repr*/ |
| 4410 | 0, /*tp_as_number*/ |
| 4411 | 0, /*tp_as_sequence*/ |
| 4412 | 0, /*tp_as_mapping*/ |
| 4413 | 0, /*tp_hash*/ |
| 4414 | 0, /*tp_call*/ |
| 4415 | 0, /*tp_str*/ |
| 4416 | 0, /*tp_getattro*/ |
| 4417 | 0, /*tp_setattro*/ |
| 4418 | 0, /*tp_as_buffer*/ |
| 4419 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4420 | _pickle_Pickler___init____doc__, /*tp_doc*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4421 | (traverseproc)Pickler_traverse, /*tp_traverse*/ |
| 4422 | (inquiry)Pickler_clear, /*tp_clear*/ |
| 4423 | 0, /*tp_richcompare*/ |
| 4424 | 0, /*tp_weaklistoffset*/ |
| 4425 | 0, /*tp_iter*/ |
| 4426 | 0, /*tp_iternext*/ |
| 4427 | Pickler_methods, /*tp_methods*/ |
| 4428 | Pickler_members, /*tp_members*/ |
| 4429 | Pickler_getsets, /*tp_getset*/ |
| 4430 | 0, /*tp_base*/ |
| 4431 | 0, /*tp_dict*/ |
| 4432 | 0, /*tp_descr_get*/ |
| 4433 | 0, /*tp_descr_set*/ |
| 4434 | 0, /*tp_dictoffset*/ |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4435 | _pickle_Pickler___init__, /*tp_init*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4436 | PyType_GenericAlloc, /*tp_alloc*/ |
| 4437 | PyType_GenericNew, /*tp_new*/ |
| 4438 | PyObject_GC_Del, /*tp_free*/ |
| 4439 | 0, /*tp_is_gc*/ |
| 4440 | }; |
| 4441 | |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 4442 | /* Temporary helper for calling self.find_class(). |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4443 | |
| 4444 | XXX: It would be nice to able to avoid Python function call overhead, by |
| 4445 | using directly the C version of find_class(), when find_class() is not |
| 4446 | overridden by a subclass. Although, this could become rather hackish. A |
| 4447 | simpler optimization would be to call the C function when self is not a |
| 4448 | subclass instance. */ |
| 4449 | static PyObject * |
| 4450 | find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name) |
| 4451 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4452 | _Py_IDENTIFIER(find_class); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 4453 | |
| 4454 | return _PyObject_CallMethodId((PyObject *)self, &PyId_find_class, "OO", |
| 4455 | module_name, global_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4456 | } |
| 4457 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4458 | static Py_ssize_t |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4459 | marker(UnpicklerObject *self) |
| 4460 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4461 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4462 | if (self->num_marks < 1) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4463 | PyErr_SetString(st->UnpicklingError, "could not find MARK"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4464 | return -1; |
| 4465 | } |
| 4466 | |
| 4467 | return self->marks[--self->num_marks]; |
| 4468 | } |
| 4469 | |
| 4470 | static int |
| 4471 | load_none(UnpicklerObject *self) |
| 4472 | { |
| 4473 | PDATA_APPEND(self->stack, Py_None, -1); |
| 4474 | return 0; |
| 4475 | } |
| 4476 | |
| 4477 | static int |
| 4478 | bad_readline(void) |
| 4479 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4480 | PickleState *st = _Pickle_GetGlobalState(); |
| 4481 | PyErr_SetString(st->UnpicklingError, "pickle data was truncated"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4482 | return -1; |
| 4483 | } |
| 4484 | |
| 4485 | static int |
| 4486 | load_int(UnpicklerObject *self) |
| 4487 | { |
| 4488 | PyObject *value; |
| 4489 | char *endptr, *s; |
| 4490 | Py_ssize_t len; |
| 4491 | long x; |
| 4492 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4493 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4494 | return -1; |
| 4495 | if (len < 2) |
| 4496 | return bad_readline(); |
| 4497 | |
| 4498 | errno = 0; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 4499 | /* XXX: Should the base argument of strtol() be explicitly set to 10? |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4500 | XXX(avassalotti): Should this uses PyOS_strtol()? */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4501 | x = strtol(s, &endptr, 0); |
| 4502 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4503 | if (errno || (*endptr != '\n' && *endptr != '\0')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4504 | /* Hm, maybe we've got something long. Let's try reading |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4505 | * it as a Python int object. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4506 | errno = 0; |
| 4507 | /* XXX: Same thing about the base here. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4508 | value = PyLong_FromString(s, NULL, 0); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4509 | if (value == NULL) { |
| 4510 | PyErr_SetString(PyExc_ValueError, |
| 4511 | "could not convert string to int"); |
| 4512 | return -1; |
| 4513 | } |
| 4514 | } |
| 4515 | else { |
| 4516 | if (len == 3 && (x == 0 || x == 1)) { |
| 4517 | if ((value = PyBool_FromLong(x)) == NULL) |
| 4518 | return -1; |
| 4519 | } |
| 4520 | else { |
| 4521 | if ((value = PyLong_FromLong(x)) == NULL) |
| 4522 | return -1; |
| 4523 | } |
| 4524 | } |
| 4525 | |
| 4526 | PDATA_PUSH(self->stack, value, -1); |
| 4527 | return 0; |
| 4528 | } |
| 4529 | |
| 4530 | static int |
| 4531 | load_bool(UnpicklerObject *self, PyObject *boolean) |
| 4532 | { |
| 4533 | assert(boolean == Py_True || boolean == Py_False); |
| 4534 | PDATA_APPEND(self->stack, boolean, -1); |
| 4535 | return 0; |
| 4536 | } |
| 4537 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4538 | /* s contains x bytes of an unsigned little-endian integer. Return its value |
| 4539 | * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX. |
| 4540 | */ |
| 4541 | static Py_ssize_t |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4542 | calc_binsize(char *bytes, int nbytes) |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4543 | { |
| 4544 | unsigned char *s = (unsigned char *)bytes; |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 4545 | Py_ssize_t i; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4546 | size_t x = 0; |
| 4547 | |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 4548 | for (i = 0; i < nbytes && (size_t)i < sizeof(size_t); i++) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4549 | x |= (size_t) s[i] << (8 * i); |
| 4550 | } |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4551 | |
| 4552 | if (x > PY_SSIZE_T_MAX) |
| 4553 | return -1; |
| 4554 | else |
| 4555 | return (Py_ssize_t) x; |
| 4556 | } |
| 4557 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4558 | /* s contains x bytes of a little-endian integer. Return its value as a |
| 4559 | * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian |
| 4560 | * int, but when x is 4 it's a signed one. This is an historical source |
| 4561 | * of x-platform bugs. |
| 4562 | */ |
| 4563 | static long |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4564 | calc_binint(char *bytes, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4565 | { |
| 4566 | unsigned char *s = (unsigned char *)bytes; |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 4567 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4568 | long x = 0; |
| 4569 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4570 | for (i = 0; i < nbytes; i++) { |
| 4571 | x |= (long)s[i] << (8 * i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4572 | } |
| 4573 | |
| 4574 | /* Unlike BININT1 and BININT2, BININT (more accurately BININT4) |
| 4575 | * is signed, so on a box with longs bigger than 4 bytes we need |
| 4576 | * to extend a BININT's sign bit to the full width. |
| 4577 | */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4578 | if (SIZEOF_LONG > 4 && nbytes == 4) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4579 | x |= -(x & (1L << 31)); |
| 4580 | } |
| 4581 | |
| 4582 | return x; |
| 4583 | } |
| 4584 | |
| 4585 | static int |
| 4586 | load_binintx(UnpicklerObject *self, char *s, int size) |
| 4587 | { |
| 4588 | PyObject *value; |
| 4589 | long x; |
| 4590 | |
| 4591 | x = calc_binint(s, size); |
| 4592 | |
| 4593 | if ((value = PyLong_FromLong(x)) == NULL) |
| 4594 | return -1; |
| 4595 | |
| 4596 | PDATA_PUSH(self->stack, value, -1); |
| 4597 | return 0; |
| 4598 | } |
| 4599 | |
| 4600 | static int |
| 4601 | load_binint(UnpicklerObject *self) |
| 4602 | { |
| 4603 | char *s; |
| 4604 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4605 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4606 | return -1; |
| 4607 | |
| 4608 | return load_binintx(self, s, 4); |
| 4609 | } |
| 4610 | |
| 4611 | static int |
| 4612 | load_binint1(UnpicklerObject *self) |
| 4613 | { |
| 4614 | char *s; |
| 4615 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4616 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4617 | return -1; |
| 4618 | |
| 4619 | return load_binintx(self, s, 1); |
| 4620 | } |
| 4621 | |
| 4622 | static int |
| 4623 | load_binint2(UnpicklerObject *self) |
| 4624 | { |
| 4625 | char *s; |
| 4626 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4627 | if (_Unpickler_Read(self, &s, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4628 | return -1; |
| 4629 | |
| 4630 | return load_binintx(self, s, 2); |
| 4631 | } |
| 4632 | |
| 4633 | static int |
| 4634 | load_long(UnpicklerObject *self) |
| 4635 | { |
| 4636 | PyObject *value; |
Alexandre Vassalotti | 446f7ff | 2009-01-23 04:43:46 +0000 | [diff] [blame] | 4637 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4638 | Py_ssize_t len; |
| 4639 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4640 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4641 | return -1; |
| 4642 | if (len < 2) |
| 4643 | return bad_readline(); |
| 4644 | |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 4645 | /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove |
| 4646 | the 'L' before calling PyLong_FromString. In order to maintain |
| 4647 | compatibility with Python 3.0.0, we don't actually *require* |
| 4648 | the 'L' to be present. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4649 | if (s[len-2] == 'L') |
Alexandre Vassalotti | 446f7ff | 2009-01-23 04:43:46 +0000 | [diff] [blame] | 4650 | s[len-2] = '\0'; |
Alexandre Vassalotti | e4bccb7 | 2009-01-24 01:47:57 +0000 | [diff] [blame] | 4651 | /* XXX: Should the base argument explicitly set to 10? */ |
| 4652 | value = PyLong_FromString(s, NULL, 0); |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 4653 | if (value == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4654 | return -1; |
| 4655 | |
| 4656 | PDATA_PUSH(self->stack, value, -1); |
| 4657 | return 0; |
| 4658 | } |
| 4659 | |
| 4660 | /* 'size' bytes contain the # of bytes of little-endian 256's-complement |
| 4661 | * data following. |
| 4662 | */ |
| 4663 | static int |
| 4664 | load_counted_long(UnpicklerObject *self, int size) |
| 4665 | { |
| 4666 | PyObject *value; |
| 4667 | char *nbytes; |
| 4668 | char *pdata; |
| 4669 | |
| 4670 | assert(size == 1 || size == 4); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4671 | if (_Unpickler_Read(self, &nbytes, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4672 | return -1; |
| 4673 | |
| 4674 | size = calc_binint(nbytes, size); |
| 4675 | if (size < 0) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4676 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4677 | /* Corrupt or hostile pickle -- we never write one like this */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4678 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4679 | "LONG pickle has negative byte count"); |
| 4680 | return -1; |
| 4681 | } |
| 4682 | |
| 4683 | if (size == 0) |
| 4684 | value = PyLong_FromLong(0L); |
| 4685 | else { |
| 4686 | /* Read the raw little-endian bytes and convert. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4687 | if (_Unpickler_Read(self, &pdata, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4688 | return -1; |
| 4689 | value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size, |
| 4690 | 1 /* little endian */ , 1 /* signed */ ); |
| 4691 | } |
| 4692 | if (value == NULL) |
| 4693 | return -1; |
| 4694 | PDATA_PUSH(self->stack, value, -1); |
| 4695 | return 0; |
| 4696 | } |
| 4697 | |
| 4698 | static int |
| 4699 | load_float(UnpicklerObject *self) |
| 4700 | { |
| 4701 | PyObject *value; |
| 4702 | char *endptr, *s; |
| 4703 | Py_ssize_t len; |
| 4704 | double d; |
| 4705 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4706 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4707 | return -1; |
| 4708 | if (len < 2) |
| 4709 | return bad_readline(); |
| 4710 | |
| 4711 | errno = 0; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4712 | d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError); |
| 4713 | if (d == -1.0 && PyErr_Occurred()) |
| 4714 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4715 | if ((endptr[0] != '\n') && (endptr[0] != '\0')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4716 | PyErr_SetString(PyExc_ValueError, "could not convert string to float"); |
| 4717 | return -1; |
| 4718 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4719 | value = PyFloat_FromDouble(d); |
| 4720 | if (value == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4721 | return -1; |
| 4722 | |
| 4723 | PDATA_PUSH(self->stack, value, -1); |
| 4724 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4725 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4726 | |
| 4727 | static int |
| 4728 | load_binfloat(UnpicklerObject *self) |
| 4729 | { |
| 4730 | PyObject *value; |
| 4731 | double x; |
| 4732 | char *s; |
| 4733 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4734 | if (_Unpickler_Read(self, &s, 8) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4735 | return -1; |
| 4736 | |
| 4737 | x = _PyFloat_Unpack8((unsigned char *)s, 0); |
| 4738 | if (x == -1.0 && PyErr_Occurred()) |
| 4739 | return -1; |
| 4740 | |
| 4741 | if ((value = PyFloat_FromDouble(x)) == NULL) |
| 4742 | return -1; |
| 4743 | |
| 4744 | PDATA_PUSH(self->stack, value, -1); |
| 4745 | return 0; |
| 4746 | } |
| 4747 | |
| 4748 | static int |
| 4749 | load_string(UnpicklerObject *self) |
| 4750 | { |
| 4751 | PyObject *bytes; |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4752 | PyObject *obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4753 | Py_ssize_t len; |
| 4754 | char *s, *p; |
| 4755 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4756 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4757 | return -1; |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4758 | /* Strip the newline */ |
| 4759 | len--; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4760 | /* Strip outermost quotes */ |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4761 | if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4762 | p = s + 1; |
| 4763 | len -= 2; |
| 4764 | } |
| 4765 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4766 | PickleState *st = _Pickle_GetGlobalState(); |
| 4767 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4768 | "the STRING opcode argument must be quoted"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4769 | return -1; |
| 4770 | } |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4771 | assert(len >= 0); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4772 | |
| 4773 | /* Use the PyBytes API to decode the string, since that is what is used |
| 4774 | to encode, and then coerce the result to Unicode. */ |
| 4775 | bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4776 | if (bytes == NULL) |
| 4777 | return -1; |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4778 | |
| 4779 | /* Leave the Python 2.x strings as bytes if the *encoding* given to the |
| 4780 | Unpickler was 'bytes'. Otherwise, convert them to unicode. */ |
| 4781 | if (strcmp(self->encoding, "bytes") == 0) { |
| 4782 | obj = bytes; |
| 4783 | } |
| 4784 | else { |
| 4785 | obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors); |
| 4786 | Py_DECREF(bytes); |
| 4787 | if (obj == NULL) { |
| 4788 | return -1; |
| 4789 | } |
| 4790 | } |
| 4791 | |
| 4792 | PDATA_PUSH(self->stack, obj, -1); |
| 4793 | return 0; |
| 4794 | } |
| 4795 | |
| 4796 | static int |
| 4797 | load_counted_binstring(UnpicklerObject *self, int nbytes) |
| 4798 | { |
| 4799 | PyObject *obj; |
| 4800 | Py_ssize_t size; |
| 4801 | char *s; |
| 4802 | |
| 4803 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4804 | return -1; |
| 4805 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4806 | size = calc_binsize(s, nbytes); |
| 4807 | if (size < 0) { |
| 4808 | PickleState *st = _Pickle_GetGlobalState(); |
| 4809 | PyErr_Format(st->UnpicklingError, |
| 4810 | "BINSTRING exceeds system's maximum size of %zd bytes", |
| 4811 | PY_SSIZE_T_MAX); |
| 4812 | return -1; |
| 4813 | } |
| 4814 | |
| 4815 | if (_Unpickler_Read(self, &s, size) < 0) |
| 4816 | return -1; |
| 4817 | |
| 4818 | /* Convert Python 2.x strings to bytes if the *encoding* given to the |
| 4819 | Unpickler was 'bytes'. Otherwise, convert them to unicode. */ |
| 4820 | if (strcmp(self->encoding, "bytes") == 0) { |
| 4821 | obj = PyBytes_FromStringAndSize(s, size); |
| 4822 | } |
| 4823 | else { |
| 4824 | obj = PyUnicode_Decode(s, size, self->encoding, self->errors); |
| 4825 | } |
| 4826 | if (obj == NULL) { |
| 4827 | return -1; |
| 4828 | } |
| 4829 | |
| 4830 | PDATA_PUSH(self->stack, obj, -1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4831 | return 0; |
| 4832 | } |
| 4833 | |
| 4834 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4835 | load_counted_binbytes(UnpicklerObject *self, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4836 | { |
| 4837 | PyObject *bytes; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4838 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4839 | char *s; |
| 4840 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4841 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4842 | return -1; |
| 4843 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4844 | size = calc_binsize(s, nbytes); |
| 4845 | if (size < 0) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4846 | PyErr_Format(PyExc_OverflowError, |
| 4847 | "BINBYTES exceeds system's maximum size of %zd bytes", |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 4848 | PY_SSIZE_T_MAX); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4849 | return -1; |
| 4850 | } |
| 4851 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4852 | if (_Unpickler_Read(self, &s, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4853 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4854 | |
| 4855 | bytes = PyBytes_FromStringAndSize(s, size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4856 | if (bytes == NULL) |
| 4857 | return -1; |
| 4858 | |
| 4859 | PDATA_PUSH(self->stack, bytes, -1); |
| 4860 | return 0; |
| 4861 | } |
| 4862 | |
| 4863 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4864 | load_unicode(UnpicklerObject *self) |
| 4865 | { |
| 4866 | PyObject *str; |
| 4867 | Py_ssize_t len; |
| 4868 | char *s; |
| 4869 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4870 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4871 | return -1; |
| 4872 | if (len < 1) |
| 4873 | return bad_readline(); |
| 4874 | |
| 4875 | str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL); |
| 4876 | if (str == NULL) |
| 4877 | return -1; |
| 4878 | |
| 4879 | PDATA_PUSH(self->stack, str, -1); |
| 4880 | return 0; |
| 4881 | } |
| 4882 | |
| 4883 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4884 | load_counted_binunicode(UnpicklerObject *self, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4885 | { |
| 4886 | PyObject *str; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4887 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4888 | char *s; |
| 4889 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4890 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4891 | return -1; |
| 4892 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4893 | size = calc_binsize(s, nbytes); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4894 | if (size < 0) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4895 | PyErr_Format(PyExc_OverflowError, |
| 4896 | "BINUNICODE exceeds system's maximum size of %zd bytes", |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 4897 | PY_SSIZE_T_MAX); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4898 | return -1; |
| 4899 | } |
| 4900 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4901 | if (_Unpickler_Read(self, &s, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4902 | return -1; |
| 4903 | |
Victor Stinner | 485fb56 | 2010-04-13 11:07:24 +0000 | [diff] [blame] | 4904 | str = PyUnicode_DecodeUTF8(s, size, "surrogatepass"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4905 | if (str == NULL) |
| 4906 | return -1; |
| 4907 | |
| 4908 | PDATA_PUSH(self->stack, str, -1); |
| 4909 | return 0; |
| 4910 | } |
| 4911 | |
| 4912 | static int |
| 4913 | load_tuple(UnpicklerObject *self) |
| 4914 | { |
| 4915 | PyObject *tuple; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4916 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4917 | |
| 4918 | if ((i = marker(self)) < 0) |
| 4919 | return -1; |
| 4920 | |
| 4921 | tuple = Pdata_poptuple(self->stack, i); |
| 4922 | if (tuple == NULL) |
| 4923 | return -1; |
| 4924 | PDATA_PUSH(self->stack, tuple, -1); |
| 4925 | return 0; |
| 4926 | } |
| 4927 | |
| 4928 | static int |
| 4929 | load_counted_tuple(UnpicklerObject *self, int len) |
| 4930 | { |
| 4931 | PyObject *tuple; |
| 4932 | |
| 4933 | tuple = PyTuple_New(len); |
| 4934 | if (tuple == NULL) |
| 4935 | return -1; |
| 4936 | |
| 4937 | while (--len >= 0) { |
| 4938 | PyObject *item; |
| 4939 | |
| 4940 | PDATA_POP(self->stack, item); |
| 4941 | if (item == NULL) |
| 4942 | return -1; |
| 4943 | PyTuple_SET_ITEM(tuple, len, item); |
| 4944 | } |
| 4945 | PDATA_PUSH(self->stack, tuple, -1); |
| 4946 | return 0; |
| 4947 | } |
| 4948 | |
| 4949 | static int |
| 4950 | load_empty_list(UnpicklerObject *self) |
| 4951 | { |
| 4952 | PyObject *list; |
| 4953 | |
| 4954 | if ((list = PyList_New(0)) == NULL) |
| 4955 | return -1; |
| 4956 | PDATA_PUSH(self->stack, list, -1); |
| 4957 | return 0; |
| 4958 | } |
| 4959 | |
| 4960 | static int |
| 4961 | load_empty_dict(UnpicklerObject *self) |
| 4962 | { |
| 4963 | PyObject *dict; |
| 4964 | |
| 4965 | if ((dict = PyDict_New()) == NULL) |
| 4966 | return -1; |
| 4967 | PDATA_PUSH(self->stack, dict, -1); |
| 4968 | return 0; |
| 4969 | } |
| 4970 | |
| 4971 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4972 | load_empty_set(UnpicklerObject *self) |
| 4973 | { |
| 4974 | PyObject *set; |
| 4975 | |
| 4976 | if ((set = PySet_New(NULL)) == NULL) |
| 4977 | return -1; |
| 4978 | PDATA_PUSH(self->stack, set, -1); |
| 4979 | return 0; |
| 4980 | } |
| 4981 | |
| 4982 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4983 | load_list(UnpicklerObject *self) |
| 4984 | { |
| 4985 | PyObject *list; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4986 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4987 | |
| 4988 | if ((i = marker(self)) < 0) |
| 4989 | return -1; |
| 4990 | |
| 4991 | list = Pdata_poplist(self->stack, i); |
| 4992 | if (list == NULL) |
| 4993 | return -1; |
| 4994 | PDATA_PUSH(self->stack, list, -1); |
| 4995 | return 0; |
| 4996 | } |
| 4997 | |
| 4998 | static int |
| 4999 | load_dict(UnpicklerObject *self) |
| 5000 | { |
| 5001 | PyObject *dict, *key, *value; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5002 | Py_ssize_t i, j, k; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5003 | |
| 5004 | if ((i = marker(self)) < 0) |
| 5005 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5006 | j = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5007 | |
| 5008 | if ((dict = PyDict_New()) == NULL) |
| 5009 | return -1; |
| 5010 | |
| 5011 | for (k = i + 1; k < j; k += 2) { |
| 5012 | key = self->stack->data[k - 1]; |
| 5013 | value = self->stack->data[k]; |
| 5014 | if (PyDict_SetItem(dict, key, value) < 0) { |
| 5015 | Py_DECREF(dict); |
| 5016 | return -1; |
| 5017 | } |
| 5018 | } |
| 5019 | Pdata_clear(self->stack, i); |
| 5020 | PDATA_PUSH(self->stack, dict, -1); |
| 5021 | return 0; |
| 5022 | } |
| 5023 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5024 | static int |
| 5025 | load_frozenset(UnpicklerObject *self) |
| 5026 | { |
| 5027 | PyObject *items; |
| 5028 | PyObject *frozenset; |
| 5029 | Py_ssize_t i; |
| 5030 | |
| 5031 | if ((i = marker(self)) < 0) |
| 5032 | return -1; |
| 5033 | |
| 5034 | items = Pdata_poptuple(self->stack, i); |
| 5035 | if (items == NULL) |
| 5036 | return -1; |
| 5037 | |
| 5038 | frozenset = PyFrozenSet_New(items); |
| 5039 | Py_DECREF(items); |
| 5040 | if (frozenset == NULL) |
| 5041 | return -1; |
| 5042 | |
| 5043 | PDATA_PUSH(self->stack, frozenset, -1); |
| 5044 | return 0; |
| 5045 | } |
| 5046 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5047 | static PyObject * |
| 5048 | instantiate(PyObject *cls, PyObject *args) |
| 5049 | { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5050 | PyObject *result = NULL; |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5051 | _Py_IDENTIFIER(__getinitargs__); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5052 | /* Caller must assure args are a tuple. Normally, args come from |
| 5053 | Pdata_poptuple which packs objects from the top of the stack |
| 5054 | into a newly created tuple. */ |
| 5055 | assert(PyTuple_Check(args)); |
| 5056 | if (Py_SIZE(args) > 0 || !PyType_Check(cls) || |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5057 | _PyObject_HasAttrId(cls, &PyId___getinitargs__)) { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5058 | result = PyObject_CallObject(cls, args); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5059 | } |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5060 | else { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5061 | _Py_IDENTIFIER(__new__); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 5062 | |
| 5063 | result = _PyObject_CallMethodId(cls, &PyId___new__, "O", cls); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5064 | } |
| 5065 | return result; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5066 | } |
| 5067 | |
| 5068 | static int |
| 5069 | load_obj(UnpicklerObject *self) |
| 5070 | { |
| 5071 | PyObject *cls, *args, *obj = NULL; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5072 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5073 | |
| 5074 | if ((i = marker(self)) < 0) |
| 5075 | return -1; |
| 5076 | |
| 5077 | args = Pdata_poptuple(self->stack, i + 1); |
| 5078 | if (args == NULL) |
| 5079 | return -1; |
| 5080 | |
| 5081 | PDATA_POP(self->stack, cls); |
| 5082 | if (cls) { |
| 5083 | obj = instantiate(cls, args); |
| 5084 | Py_DECREF(cls); |
| 5085 | } |
| 5086 | Py_DECREF(args); |
| 5087 | if (obj == NULL) |
| 5088 | return -1; |
| 5089 | |
| 5090 | PDATA_PUSH(self->stack, obj, -1); |
| 5091 | return 0; |
| 5092 | } |
| 5093 | |
| 5094 | static int |
| 5095 | load_inst(UnpicklerObject *self) |
| 5096 | { |
| 5097 | PyObject *cls = NULL; |
| 5098 | PyObject *args = NULL; |
| 5099 | PyObject *obj = NULL; |
| 5100 | PyObject *module_name; |
| 5101 | PyObject *class_name; |
| 5102 | Py_ssize_t len; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5103 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5104 | char *s; |
| 5105 | |
| 5106 | if ((i = marker(self)) < 0) |
| 5107 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5108 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5109 | return -1; |
| 5110 | if (len < 2) |
| 5111 | return bad_readline(); |
| 5112 | |
| 5113 | /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII |
| 5114 | identifiers are permitted in Python 3.0, since the INST opcode is only |
| 5115 | supported by older protocols on Python 2.x. */ |
| 5116 | module_name = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
| 5117 | if (module_name == NULL) |
| 5118 | return -1; |
| 5119 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5120 | if ((len = _Unpickler_Readline(self, &s)) >= 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5121 | if (len < 2) |
| 5122 | return bad_readline(); |
| 5123 | class_name = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5124 | if (class_name != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5125 | cls = find_class(self, module_name, class_name); |
| 5126 | Py_DECREF(class_name); |
| 5127 | } |
| 5128 | } |
| 5129 | Py_DECREF(module_name); |
| 5130 | |
| 5131 | if (cls == NULL) |
| 5132 | return -1; |
| 5133 | |
| 5134 | if ((args = Pdata_poptuple(self->stack, i)) != NULL) { |
| 5135 | obj = instantiate(cls, args); |
| 5136 | Py_DECREF(args); |
| 5137 | } |
| 5138 | Py_DECREF(cls); |
| 5139 | |
| 5140 | if (obj == NULL) |
| 5141 | return -1; |
| 5142 | |
| 5143 | PDATA_PUSH(self->stack, obj, -1); |
| 5144 | return 0; |
| 5145 | } |
| 5146 | |
| 5147 | static int |
| 5148 | load_newobj(UnpicklerObject *self) |
| 5149 | { |
| 5150 | PyObject *args = NULL; |
| 5151 | PyObject *clsraw = NULL; |
| 5152 | PyTypeObject *cls; /* clsraw cast to its true type */ |
| 5153 | PyObject *obj; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5154 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5155 | |
| 5156 | /* Stack is ... cls argtuple, and we want to call |
| 5157 | * cls.__new__(cls, *argtuple). |
| 5158 | */ |
| 5159 | PDATA_POP(self->stack, args); |
| 5160 | if (args == NULL) |
| 5161 | goto error; |
| 5162 | if (!PyTuple_Check(args)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5163 | PyErr_SetString(st->UnpicklingError, |
| 5164 | "NEWOBJ expected an arg " "tuple."); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5165 | goto error; |
| 5166 | } |
| 5167 | |
| 5168 | PDATA_POP(self->stack, clsraw); |
| 5169 | cls = (PyTypeObject *)clsraw; |
| 5170 | if (cls == NULL) |
| 5171 | goto error; |
| 5172 | if (!PyType_Check(cls)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5173 | PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5174 | "isn't a type object"); |
| 5175 | goto error; |
| 5176 | } |
| 5177 | if (cls->tp_new == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5178 | PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5179 | "has NULL tp_new"); |
| 5180 | goto error; |
| 5181 | } |
| 5182 | |
| 5183 | /* Call __new__. */ |
| 5184 | obj = cls->tp_new(cls, args, NULL); |
| 5185 | if (obj == NULL) |
| 5186 | goto error; |
| 5187 | |
| 5188 | Py_DECREF(args); |
| 5189 | Py_DECREF(clsraw); |
| 5190 | PDATA_PUSH(self->stack, obj, -1); |
| 5191 | return 0; |
| 5192 | |
| 5193 | error: |
| 5194 | Py_XDECREF(args); |
| 5195 | Py_XDECREF(clsraw); |
| 5196 | return -1; |
| 5197 | } |
| 5198 | |
| 5199 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5200 | load_newobj_ex(UnpicklerObject *self) |
| 5201 | { |
| 5202 | PyObject *cls, *args, *kwargs; |
| 5203 | PyObject *obj; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5204 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5205 | |
| 5206 | PDATA_POP(self->stack, kwargs); |
| 5207 | if (kwargs == NULL) { |
| 5208 | return -1; |
| 5209 | } |
| 5210 | PDATA_POP(self->stack, args); |
| 5211 | if (args == NULL) { |
| 5212 | Py_DECREF(kwargs); |
| 5213 | return -1; |
| 5214 | } |
| 5215 | PDATA_POP(self->stack, cls); |
| 5216 | if (cls == NULL) { |
| 5217 | Py_DECREF(kwargs); |
| 5218 | Py_DECREF(args); |
| 5219 | return -1; |
| 5220 | } |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 5221 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5222 | if (!PyType_Check(cls)) { |
| 5223 | Py_DECREF(kwargs); |
| 5224 | Py_DECREF(args); |
| 5225 | Py_DECREF(cls); |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 5226 | PyErr_Format(st->UnpicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5227 | "NEWOBJ_EX class argument must be a type, not %.200s", |
| 5228 | Py_TYPE(cls)->tp_name); |
| 5229 | return -1; |
| 5230 | } |
| 5231 | |
| 5232 | if (((PyTypeObject *)cls)->tp_new == NULL) { |
| 5233 | Py_DECREF(kwargs); |
| 5234 | Py_DECREF(args); |
| 5235 | Py_DECREF(cls); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5236 | PyErr_SetString(st->UnpicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5237 | "NEWOBJ_EX class argument doesn't have __new__"); |
| 5238 | return -1; |
| 5239 | } |
| 5240 | obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs); |
| 5241 | Py_DECREF(kwargs); |
| 5242 | Py_DECREF(args); |
| 5243 | Py_DECREF(cls); |
| 5244 | if (obj == NULL) { |
| 5245 | return -1; |
| 5246 | } |
| 5247 | PDATA_PUSH(self->stack, obj, -1); |
| 5248 | return 0; |
| 5249 | } |
| 5250 | |
| 5251 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5252 | load_global(UnpicklerObject *self) |
| 5253 | { |
| 5254 | PyObject *global = NULL; |
| 5255 | PyObject *module_name; |
| 5256 | PyObject *global_name; |
| 5257 | Py_ssize_t len; |
| 5258 | char *s; |
| 5259 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5260 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5261 | return -1; |
| 5262 | if (len < 2) |
| 5263 | return bad_readline(); |
| 5264 | module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict"); |
| 5265 | if (!module_name) |
| 5266 | return -1; |
| 5267 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5268 | if ((len = _Unpickler_Readline(self, &s)) >= 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5269 | if (len < 2) { |
| 5270 | Py_DECREF(module_name); |
| 5271 | return bad_readline(); |
| 5272 | } |
| 5273 | global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict"); |
| 5274 | if (global_name) { |
| 5275 | global = find_class(self, module_name, global_name); |
| 5276 | Py_DECREF(global_name); |
| 5277 | } |
| 5278 | } |
| 5279 | Py_DECREF(module_name); |
| 5280 | |
| 5281 | if (global == NULL) |
| 5282 | return -1; |
| 5283 | PDATA_PUSH(self->stack, global, -1); |
| 5284 | return 0; |
| 5285 | } |
| 5286 | |
| 5287 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5288 | load_stack_global(UnpicklerObject *self) |
| 5289 | { |
| 5290 | PyObject *global; |
| 5291 | PyObject *module_name; |
| 5292 | PyObject *global_name; |
| 5293 | |
| 5294 | PDATA_POP(self->stack, global_name); |
| 5295 | PDATA_POP(self->stack, module_name); |
| 5296 | if (module_name == NULL || !PyUnicode_CheckExact(module_name) || |
| 5297 | global_name == NULL || !PyUnicode_CheckExact(global_name)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5298 | PickleState *st = _Pickle_GetGlobalState(); |
| 5299 | PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str"); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5300 | Py_XDECREF(global_name); |
| 5301 | Py_XDECREF(module_name); |
| 5302 | return -1; |
| 5303 | } |
| 5304 | global = find_class(self, module_name, global_name); |
| 5305 | Py_DECREF(global_name); |
| 5306 | Py_DECREF(module_name); |
| 5307 | if (global == NULL) |
| 5308 | return -1; |
| 5309 | PDATA_PUSH(self->stack, global, -1); |
| 5310 | return 0; |
| 5311 | } |
| 5312 | |
| 5313 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5314 | load_persid(UnpicklerObject *self) |
| 5315 | { |
| 5316 | PyObject *pid; |
| 5317 | Py_ssize_t len; |
| 5318 | char *s; |
| 5319 | |
| 5320 | if (self->pers_func) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5321 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5322 | return -1; |
Alexandre Vassalotti | 896414f | 2013-11-30 13:52:35 -0800 | [diff] [blame] | 5323 | if (len < 1) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5324 | return bad_readline(); |
| 5325 | |
| 5326 | pid = PyBytes_FromStringAndSize(s, len - 1); |
| 5327 | if (pid == NULL) |
| 5328 | return -1; |
| 5329 | |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5330 | /* This does not leak since _Pickle_FastCall() steals the reference |
| 5331 | to pid first. */ |
| 5332 | pid = _Pickle_FastCall(self->pers_func, pid); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5333 | if (pid == NULL) |
| 5334 | return -1; |
| 5335 | |
| 5336 | PDATA_PUSH(self->stack, pid, -1); |
| 5337 | return 0; |
| 5338 | } |
| 5339 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5340 | PickleState *st = _Pickle_GetGlobalState(); |
| 5341 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5342 | "A load persistent id instruction was encountered,\n" |
| 5343 | "but no persistent_load function was specified."); |
| 5344 | return -1; |
| 5345 | } |
| 5346 | } |
| 5347 | |
| 5348 | static int |
| 5349 | load_binpersid(UnpicklerObject *self) |
| 5350 | { |
| 5351 | PyObject *pid; |
| 5352 | |
| 5353 | if (self->pers_func) { |
| 5354 | PDATA_POP(self->stack, pid); |
| 5355 | if (pid == NULL) |
| 5356 | return -1; |
| 5357 | |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5358 | /* This does not leak since _Pickle_FastCall() steals the |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5359 | reference to pid first. */ |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5360 | pid = _Pickle_FastCall(self->pers_func, pid); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5361 | if (pid == NULL) |
| 5362 | return -1; |
| 5363 | |
| 5364 | PDATA_PUSH(self->stack, pid, -1); |
| 5365 | return 0; |
| 5366 | } |
| 5367 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5368 | PickleState *st = _Pickle_GetGlobalState(); |
| 5369 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5370 | "A load persistent id instruction was encountered,\n" |
| 5371 | "but no persistent_load function was specified."); |
| 5372 | return -1; |
| 5373 | } |
| 5374 | } |
| 5375 | |
| 5376 | static int |
| 5377 | load_pop(UnpicklerObject *self) |
| 5378 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5379 | Py_ssize_t len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5380 | |
| 5381 | /* Note that we split the (pickle.py) stack into two stacks, |
| 5382 | * an object stack and a mark stack. We have to be clever and |
| 5383 | * 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] | 5384 | * mark stack first, and only signalling a stack underflow if |
| 5385 | * the object stack is empty and the mark stack doesn't match |
| 5386 | * our expectations. |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5387 | */ |
Collin Winter | 8ca69de | 2009-05-26 16:53:41 +0000 | [diff] [blame] | 5388 | if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5389 | self->num_marks--; |
Antoine Pitrou | 01a15ea | 2010-01-07 17:57:31 +0000 | [diff] [blame] | 5390 | } else if (len > 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5391 | len--; |
| 5392 | Py_DECREF(self->stack->data[len]); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5393 | Py_SIZE(self->stack) = len; |
Collin Winter | 8ca69de | 2009-05-26 16:53:41 +0000 | [diff] [blame] | 5394 | } else { |
| 5395 | return stack_underflow(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5396 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5397 | return 0; |
| 5398 | } |
| 5399 | |
| 5400 | static int |
| 5401 | load_pop_mark(UnpicklerObject *self) |
| 5402 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5403 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5404 | |
| 5405 | if ((i = marker(self)) < 0) |
| 5406 | return -1; |
| 5407 | |
| 5408 | Pdata_clear(self->stack, i); |
| 5409 | |
| 5410 | return 0; |
| 5411 | } |
| 5412 | |
| 5413 | static int |
| 5414 | load_dup(UnpicklerObject *self) |
| 5415 | { |
| 5416 | PyObject *last; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5417 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5418 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5419 | if ((len = Py_SIZE(self->stack)) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5420 | return stack_underflow(); |
| 5421 | last = self->stack->data[len - 1]; |
| 5422 | PDATA_APPEND(self->stack, last, -1); |
| 5423 | return 0; |
| 5424 | } |
| 5425 | |
| 5426 | static int |
| 5427 | load_get(UnpicklerObject *self) |
| 5428 | { |
| 5429 | PyObject *key, *value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5430 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5431 | Py_ssize_t len; |
| 5432 | char *s; |
| 5433 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5434 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5435 | return -1; |
| 5436 | if (len < 2) |
| 5437 | return bad_readline(); |
| 5438 | |
| 5439 | key = PyLong_FromString(s, NULL, 10); |
| 5440 | if (key == NULL) |
| 5441 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5442 | idx = PyLong_AsSsize_t(key); |
| 5443 | if (idx == -1 && PyErr_Occurred()) { |
| 5444 | Py_DECREF(key); |
| 5445 | return -1; |
| 5446 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5447 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5448 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5449 | if (value == NULL) { |
| 5450 | if (!PyErr_Occurred()) |
| 5451 | PyErr_SetObject(PyExc_KeyError, key); |
| 5452 | Py_DECREF(key); |
| 5453 | return -1; |
| 5454 | } |
| 5455 | Py_DECREF(key); |
| 5456 | |
| 5457 | PDATA_APPEND(self->stack, value, -1); |
| 5458 | return 0; |
| 5459 | } |
| 5460 | |
| 5461 | static int |
| 5462 | load_binget(UnpicklerObject *self) |
| 5463 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5464 | PyObject *value; |
| 5465 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5466 | char *s; |
| 5467 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5468 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5469 | return -1; |
| 5470 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5471 | idx = Py_CHARMASK(s[0]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5472 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5473 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5474 | if (value == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5475 | PyObject *key = PyLong_FromSsize_t(idx); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5476 | if (key != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5477 | PyErr_SetObject(PyExc_KeyError, key); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5478 | Py_DECREF(key); |
| 5479 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5480 | return -1; |
| 5481 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5482 | |
| 5483 | PDATA_APPEND(self->stack, value, -1); |
| 5484 | return 0; |
| 5485 | } |
| 5486 | |
| 5487 | static int |
| 5488 | load_long_binget(UnpicklerObject *self) |
| 5489 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5490 | PyObject *value; |
| 5491 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5492 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5493 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5494 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5495 | return -1; |
| 5496 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5497 | idx = calc_binsize(s, 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5498 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5499 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5500 | if (value == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5501 | PyObject *key = PyLong_FromSsize_t(idx); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5502 | if (key != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5503 | PyErr_SetObject(PyExc_KeyError, key); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5504 | Py_DECREF(key); |
| 5505 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5506 | return -1; |
| 5507 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5508 | |
| 5509 | PDATA_APPEND(self->stack, value, -1); |
| 5510 | return 0; |
| 5511 | } |
| 5512 | |
| 5513 | /* Push an object from the extension registry (EXT[124]). nbytes is |
| 5514 | * the number of bytes following the opcode, holding the index (code) value. |
| 5515 | */ |
| 5516 | static int |
| 5517 | load_extension(UnpicklerObject *self, int nbytes) |
| 5518 | { |
| 5519 | char *codebytes; /* the nbytes bytes after the opcode */ |
| 5520 | long code; /* calc_binint returns long */ |
| 5521 | PyObject *py_code; /* code as a Python int */ |
| 5522 | PyObject *obj; /* the object to push */ |
| 5523 | PyObject *pair; /* (module_name, class_name) */ |
| 5524 | PyObject *module_name, *class_name; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5525 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5526 | |
| 5527 | assert(nbytes == 1 || nbytes == 2 || nbytes == 4); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5528 | if (_Unpickler_Read(self, &codebytes, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5529 | return -1; |
| 5530 | code = calc_binint(codebytes, nbytes); |
| 5531 | if (code <= 0) { /* note that 0 is forbidden */ |
| 5532 | /* Corrupt or hostile pickle. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5533 | PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5534 | return -1; |
| 5535 | } |
| 5536 | |
| 5537 | /* Look for the code in the cache. */ |
| 5538 | py_code = PyLong_FromLong(code); |
| 5539 | if (py_code == NULL) |
| 5540 | return -1; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5541 | obj = PyDict_GetItemWithError(st->extension_cache, py_code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5542 | if (obj != NULL) { |
| 5543 | /* Bingo. */ |
| 5544 | Py_DECREF(py_code); |
| 5545 | PDATA_APPEND(self->stack, obj, -1); |
| 5546 | return 0; |
| 5547 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5548 | if (PyErr_Occurred()) { |
| 5549 | Py_DECREF(py_code); |
| 5550 | return -1; |
| 5551 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5552 | |
| 5553 | /* Look up the (module_name, class_name) pair. */ |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5554 | pair = PyDict_GetItemWithError(st->inverted_registry, py_code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5555 | if (pair == NULL) { |
| 5556 | Py_DECREF(py_code); |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5557 | if (!PyErr_Occurred()) { |
| 5558 | PyErr_Format(PyExc_ValueError, "unregistered extension " |
| 5559 | "code %ld", code); |
| 5560 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5561 | return -1; |
| 5562 | } |
| 5563 | /* Since the extension registry is manipulable via Python code, |
| 5564 | * confirm that pair is really a 2-tuple of strings. |
| 5565 | */ |
| 5566 | if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 || |
| 5567 | !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) || |
| 5568 | !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) { |
| 5569 | Py_DECREF(py_code); |
| 5570 | PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] " |
| 5571 | "isn't a 2-tuple of strings", code); |
| 5572 | return -1; |
| 5573 | } |
| 5574 | /* Load the object. */ |
| 5575 | obj = find_class(self, module_name, class_name); |
| 5576 | if (obj == NULL) { |
| 5577 | Py_DECREF(py_code); |
| 5578 | return -1; |
| 5579 | } |
| 5580 | /* Cache code -> obj. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5581 | code = PyDict_SetItem(st->extension_cache, py_code, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5582 | Py_DECREF(py_code); |
| 5583 | if (code < 0) { |
| 5584 | Py_DECREF(obj); |
| 5585 | return -1; |
| 5586 | } |
| 5587 | PDATA_PUSH(self->stack, obj, -1); |
| 5588 | return 0; |
| 5589 | } |
| 5590 | |
| 5591 | static int |
| 5592 | load_put(UnpicklerObject *self) |
| 5593 | { |
| 5594 | PyObject *key, *value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5595 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5596 | Py_ssize_t len; |
| 5597 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5598 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5599 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5600 | return -1; |
| 5601 | if (len < 2) |
| 5602 | return bad_readline(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5603 | if (Py_SIZE(self->stack) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5604 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5605 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5606 | |
| 5607 | key = PyLong_FromString(s, NULL, 10); |
| 5608 | if (key == NULL) |
| 5609 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5610 | idx = PyLong_AsSsize_t(key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5611 | Py_DECREF(key); |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5612 | if (idx < 0) { |
| 5613 | if (!PyErr_Occurred()) |
| 5614 | PyErr_SetString(PyExc_ValueError, |
| 5615 | "negative PUT argument"); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5616 | return -1; |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5617 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5618 | |
| 5619 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5620 | } |
| 5621 | |
| 5622 | static int |
| 5623 | load_binput(UnpicklerObject *self) |
| 5624 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5625 | PyObject *value; |
| 5626 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5627 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5628 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5629 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5630 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5631 | |
| 5632 | if (Py_SIZE(self->stack) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5633 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5634 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5635 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5636 | idx = Py_CHARMASK(s[0]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5637 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5638 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5639 | } |
| 5640 | |
| 5641 | static int |
| 5642 | load_long_binput(UnpicklerObject *self) |
| 5643 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5644 | PyObject *value; |
| 5645 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5646 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5647 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5648 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5649 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5650 | |
| 5651 | if (Py_SIZE(self->stack) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5652 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5653 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5654 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5655 | idx = calc_binsize(s, 4); |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5656 | if (idx < 0) { |
| 5657 | PyErr_SetString(PyExc_ValueError, |
| 5658 | "negative LONG_BINPUT argument"); |
| 5659 | return -1; |
| 5660 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5661 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5662 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5663 | } |
| 5664 | |
| 5665 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5666 | load_memoize(UnpicklerObject *self) |
| 5667 | { |
| 5668 | PyObject *value; |
| 5669 | |
| 5670 | if (Py_SIZE(self->stack) <= 0) |
| 5671 | return stack_underflow(); |
| 5672 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
| 5673 | |
| 5674 | return _Unpickler_MemoPut(self, self->memo_len, value); |
| 5675 | } |
| 5676 | |
| 5677 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5678 | do_append(UnpicklerObject *self, Py_ssize_t x) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5679 | { |
| 5680 | PyObject *value; |
| 5681 | PyObject *list; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5682 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5683 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5684 | len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5685 | if (x > len || x <= 0) |
| 5686 | return stack_underflow(); |
| 5687 | if (len == x) /* nothing to do */ |
| 5688 | return 0; |
| 5689 | |
| 5690 | list = self->stack->data[x - 1]; |
| 5691 | |
| 5692 | if (PyList_Check(list)) { |
| 5693 | PyObject *slice; |
| 5694 | Py_ssize_t list_len; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5695 | int ret; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5696 | |
| 5697 | slice = Pdata_poplist(self->stack, x); |
| 5698 | if (!slice) |
| 5699 | return -1; |
| 5700 | list_len = PyList_GET_SIZE(list); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5701 | ret = PyList_SetSlice(list, list_len, list_len, slice); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5702 | Py_DECREF(slice); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5703 | return ret; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5704 | } |
| 5705 | else { |
| 5706 | PyObject *append_func; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5707 | _Py_IDENTIFIER(append); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5708 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 5709 | append_func = _PyObject_GetAttrId(list, &PyId_append); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5710 | if (append_func == NULL) |
| 5711 | return -1; |
| 5712 | for (i = x; i < len; i++) { |
| 5713 | PyObject *result; |
| 5714 | |
| 5715 | value = self->stack->data[i]; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5716 | result = _Pickle_FastCall(append_func, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5717 | if (result == NULL) { |
| 5718 | Pdata_clear(self->stack, i + 1); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5719 | Py_SIZE(self->stack) = x; |
Alexandre Vassalotti | 637c7c4 | 2013-04-20 21:28:21 -0700 | [diff] [blame] | 5720 | Py_DECREF(append_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5721 | return -1; |
| 5722 | } |
| 5723 | Py_DECREF(result); |
| 5724 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5725 | Py_SIZE(self->stack) = x; |
Alexandre Vassalotti | 637c7c4 | 2013-04-20 21:28:21 -0700 | [diff] [blame] | 5726 | Py_DECREF(append_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5727 | } |
| 5728 | |
| 5729 | return 0; |
| 5730 | } |
| 5731 | |
| 5732 | static int |
| 5733 | load_append(UnpicklerObject *self) |
| 5734 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5735 | return do_append(self, Py_SIZE(self->stack) - 1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5736 | } |
| 5737 | |
| 5738 | static int |
| 5739 | load_appends(UnpicklerObject *self) |
| 5740 | { |
| 5741 | return do_append(self, marker(self)); |
| 5742 | } |
| 5743 | |
| 5744 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5745 | do_setitems(UnpicklerObject *self, Py_ssize_t x) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5746 | { |
| 5747 | PyObject *value, *key; |
| 5748 | PyObject *dict; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5749 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5750 | int status = 0; |
| 5751 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5752 | len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5753 | if (x > len || x <= 0) |
| 5754 | return stack_underflow(); |
| 5755 | if (len == x) /* nothing to do */ |
| 5756 | return 0; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 5757 | if ((len - x) % 2 != 0) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5758 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5759 | /* Currupt or hostile pickle -- we never write one like this. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5760 | PyErr_SetString(st->UnpicklingError, |
| 5761 | "odd number of items for SETITEMS"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5762 | return -1; |
| 5763 | } |
| 5764 | |
| 5765 | /* Here, dict does not actually need to be a PyDict; it could be anything |
| 5766 | that supports the __setitem__ attribute. */ |
| 5767 | dict = self->stack->data[x - 1]; |
| 5768 | |
| 5769 | for (i = x + 1; i < len; i += 2) { |
| 5770 | key = self->stack->data[i - 1]; |
| 5771 | value = self->stack->data[i]; |
| 5772 | if (PyObject_SetItem(dict, key, value) < 0) { |
| 5773 | status = -1; |
| 5774 | break; |
| 5775 | } |
| 5776 | } |
| 5777 | |
| 5778 | Pdata_clear(self->stack, x); |
| 5779 | return status; |
| 5780 | } |
| 5781 | |
| 5782 | static int |
| 5783 | load_setitem(UnpicklerObject *self) |
| 5784 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5785 | return do_setitems(self, Py_SIZE(self->stack) - 2); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5786 | } |
| 5787 | |
| 5788 | static int |
| 5789 | load_setitems(UnpicklerObject *self) |
| 5790 | { |
| 5791 | return do_setitems(self, marker(self)); |
| 5792 | } |
| 5793 | |
| 5794 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5795 | load_additems(UnpicklerObject *self) |
| 5796 | { |
| 5797 | PyObject *set; |
| 5798 | Py_ssize_t mark, len, i; |
| 5799 | |
| 5800 | mark = marker(self); |
| 5801 | len = Py_SIZE(self->stack); |
| 5802 | if (mark > len || mark <= 0) |
| 5803 | return stack_underflow(); |
| 5804 | if (len == mark) /* nothing to do */ |
| 5805 | return 0; |
| 5806 | |
| 5807 | set = self->stack->data[mark - 1]; |
| 5808 | |
| 5809 | if (PySet_Check(set)) { |
| 5810 | PyObject *items; |
| 5811 | int status; |
| 5812 | |
| 5813 | items = Pdata_poptuple(self->stack, mark); |
| 5814 | if (items == NULL) |
| 5815 | return -1; |
| 5816 | |
| 5817 | status = _PySet_Update(set, items); |
| 5818 | Py_DECREF(items); |
| 5819 | return status; |
| 5820 | } |
| 5821 | else { |
| 5822 | PyObject *add_func; |
| 5823 | _Py_IDENTIFIER(add); |
| 5824 | |
| 5825 | add_func = _PyObject_GetAttrId(set, &PyId_add); |
| 5826 | if (add_func == NULL) |
| 5827 | return -1; |
| 5828 | for (i = mark; i < len; i++) { |
| 5829 | PyObject *result; |
| 5830 | PyObject *item; |
| 5831 | |
| 5832 | item = self->stack->data[i]; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5833 | result = _Pickle_FastCall(add_func, item); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5834 | if (result == NULL) { |
| 5835 | Pdata_clear(self->stack, i + 1); |
| 5836 | Py_SIZE(self->stack) = mark; |
| 5837 | return -1; |
| 5838 | } |
| 5839 | Py_DECREF(result); |
| 5840 | } |
| 5841 | Py_SIZE(self->stack) = mark; |
| 5842 | } |
| 5843 | |
| 5844 | return 0; |
| 5845 | } |
| 5846 | |
| 5847 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5848 | load_build(UnpicklerObject *self) |
| 5849 | { |
| 5850 | PyObject *state, *inst, *slotstate; |
| 5851 | PyObject *setstate; |
| 5852 | int status = 0; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5853 | _Py_IDENTIFIER(__setstate__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5854 | |
| 5855 | /* Stack is ... instance, state. We want to leave instance at |
| 5856 | * the stack top, possibly mutated via instance.__setstate__(state). |
| 5857 | */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5858 | if (Py_SIZE(self->stack) < 2) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5859 | return stack_underflow(); |
| 5860 | |
| 5861 | PDATA_POP(self->stack, state); |
| 5862 | if (state == NULL) |
| 5863 | return -1; |
| 5864 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5865 | inst = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5866 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 5867 | setstate = _PyObject_GetAttrId(inst, &PyId___setstate__); |
Alexandre Vassalotti | 1f9d907 | 2008-08-15 03:07:47 +0000 | [diff] [blame] | 5868 | if (setstate == NULL) { |
| 5869 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 5870 | PyErr_Clear(); |
Antoine Pitrou | d79dc62 | 2008-09-05 00:03:33 +0000 | [diff] [blame] | 5871 | else { |
| 5872 | Py_DECREF(state); |
Alexandre Vassalotti | 1f9d907 | 2008-08-15 03:07:47 +0000 | [diff] [blame] | 5873 | return -1; |
Antoine Pitrou | d79dc62 | 2008-09-05 00:03:33 +0000 | [diff] [blame] | 5874 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5875 | } |
| 5876 | else { |
| 5877 | PyObject *result; |
| 5878 | |
| 5879 | /* The explicit __setstate__ is responsible for everything. */ |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5880 | result = _Pickle_FastCall(setstate, state); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5881 | Py_DECREF(setstate); |
| 5882 | if (result == NULL) |
| 5883 | return -1; |
| 5884 | Py_DECREF(result); |
| 5885 | return 0; |
| 5886 | } |
| 5887 | |
| 5888 | /* A default __setstate__. First see whether state embeds a |
| 5889 | * slot state dict too (a proto 2 addition). |
| 5890 | */ |
| 5891 | if (PyTuple_Check(state) && Py_SIZE(state) == 2) { |
| 5892 | PyObject *tmp = state; |
| 5893 | |
| 5894 | state = PyTuple_GET_ITEM(tmp, 0); |
| 5895 | slotstate = PyTuple_GET_ITEM(tmp, 1); |
| 5896 | Py_INCREF(state); |
| 5897 | Py_INCREF(slotstate); |
| 5898 | Py_DECREF(tmp); |
| 5899 | } |
| 5900 | else |
| 5901 | slotstate = NULL; |
| 5902 | |
| 5903 | /* Set inst.__dict__ from the state dict (if any). */ |
| 5904 | if (state != Py_None) { |
| 5905 | PyObject *dict; |
Antoine Pitrou | a9f48a0 | 2009-05-02 21:41:14 +0000 | [diff] [blame] | 5906 | PyObject *d_key, *d_value; |
| 5907 | Py_ssize_t i; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5908 | _Py_IDENTIFIER(__dict__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5909 | |
| 5910 | if (!PyDict_Check(state)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5911 | PickleState *st = _Pickle_GetGlobalState(); |
| 5912 | PyErr_SetString(st->UnpicklingError, "state is not a dictionary"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5913 | goto error; |
| 5914 | } |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 5915 | dict = _PyObject_GetAttrId(inst, &PyId___dict__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5916 | if (dict == NULL) |
| 5917 | goto error; |
| 5918 | |
Antoine Pitrou | a9f48a0 | 2009-05-02 21:41:14 +0000 | [diff] [blame] | 5919 | i = 0; |
| 5920 | while (PyDict_Next(state, &i, &d_key, &d_value)) { |
| 5921 | /* normally the keys for instance attributes are |
| 5922 | interned. we should try to do that here. */ |
| 5923 | Py_INCREF(d_key); |
| 5924 | if (PyUnicode_CheckExact(d_key)) |
| 5925 | PyUnicode_InternInPlace(&d_key); |
| 5926 | if (PyObject_SetItem(dict, d_key, d_value) < 0) { |
| 5927 | Py_DECREF(d_key); |
| 5928 | goto error; |
| 5929 | } |
| 5930 | Py_DECREF(d_key); |
| 5931 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5932 | Py_DECREF(dict); |
| 5933 | } |
| 5934 | |
| 5935 | /* Also set instance attributes from the slotstate dict (if any). */ |
| 5936 | if (slotstate != NULL) { |
| 5937 | PyObject *d_key, *d_value; |
| 5938 | Py_ssize_t i; |
| 5939 | |
| 5940 | if (!PyDict_Check(slotstate)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5941 | PickleState *st = _Pickle_GetGlobalState(); |
| 5942 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5943 | "slot state is not a dictionary"); |
| 5944 | goto error; |
| 5945 | } |
| 5946 | i = 0; |
| 5947 | while (PyDict_Next(slotstate, &i, &d_key, &d_value)) { |
| 5948 | if (PyObject_SetAttr(inst, d_key, d_value) < 0) |
| 5949 | goto error; |
| 5950 | } |
| 5951 | } |
| 5952 | |
| 5953 | if (0) { |
| 5954 | error: |
| 5955 | status = -1; |
| 5956 | } |
| 5957 | |
| 5958 | Py_DECREF(state); |
| 5959 | Py_XDECREF(slotstate); |
| 5960 | return status; |
| 5961 | } |
| 5962 | |
| 5963 | static int |
| 5964 | load_mark(UnpicklerObject *self) |
| 5965 | { |
| 5966 | |
| 5967 | /* Note that we split the (pickle.py) stack into two stacks, an |
| 5968 | * object stack and a mark stack. Here we push a mark onto the |
| 5969 | * mark stack. |
| 5970 | */ |
| 5971 | |
| 5972 | if ((self->num_marks + 1) >= self->marks_size) { |
| 5973 | size_t alloc; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5974 | Py_ssize_t *marks; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5975 | |
| 5976 | /* Use the size_t type to check for overflow. */ |
| 5977 | alloc = ((size_t)self->num_marks << 1) + 20; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5978 | if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) || |
Alexandre Vassalotti | 7634ff5 | 2008-06-13 02:16:06 +0000 | [diff] [blame] | 5979 | alloc <= ((size_t)self->num_marks + 1)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5980 | PyErr_NoMemory(); |
| 5981 | return -1; |
| 5982 | } |
| 5983 | |
| 5984 | if (self->marks == NULL) |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5985 | marks = (Py_ssize_t *) PyMem_Malloc(alloc * sizeof(Py_ssize_t)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5986 | else |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5987 | marks = (Py_ssize_t *) PyMem_Realloc(self->marks, |
| 5988 | alloc * sizeof(Py_ssize_t)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5989 | if (marks == NULL) { |
| 5990 | PyErr_NoMemory(); |
| 5991 | return -1; |
| 5992 | } |
| 5993 | self->marks = marks; |
| 5994 | self->marks_size = (Py_ssize_t)alloc; |
| 5995 | } |
| 5996 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5997 | self->marks[self->num_marks++] = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5998 | |
| 5999 | return 0; |
| 6000 | } |
| 6001 | |
| 6002 | static int |
| 6003 | load_reduce(UnpicklerObject *self) |
| 6004 | { |
| 6005 | PyObject *callable = NULL; |
| 6006 | PyObject *argtup = NULL; |
| 6007 | PyObject *obj = NULL; |
| 6008 | |
| 6009 | PDATA_POP(self->stack, argtup); |
| 6010 | if (argtup == NULL) |
| 6011 | return -1; |
| 6012 | PDATA_POP(self->stack, callable); |
| 6013 | if (callable) { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 6014 | obj = PyObject_CallObject(callable, argtup); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6015 | Py_DECREF(callable); |
| 6016 | } |
| 6017 | Py_DECREF(argtup); |
| 6018 | |
| 6019 | if (obj == NULL) |
| 6020 | return -1; |
| 6021 | |
| 6022 | PDATA_PUSH(self->stack, obj, -1); |
| 6023 | return 0; |
| 6024 | } |
| 6025 | |
| 6026 | /* Just raises an error if we don't know the protocol specified. PROTO |
| 6027 | * is the first opcode for protocols >= 2. |
| 6028 | */ |
| 6029 | static int |
| 6030 | load_proto(UnpicklerObject *self) |
| 6031 | { |
| 6032 | char *s; |
| 6033 | int i; |
| 6034 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6035 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6036 | return -1; |
| 6037 | |
| 6038 | i = (unsigned char)s[0]; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6039 | if (i <= HIGHEST_PROTOCOL) { |
| 6040 | self->proto = i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6041 | return 0; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6042 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6043 | |
| 6044 | PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i); |
| 6045 | return -1; |
| 6046 | } |
| 6047 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6048 | static int |
| 6049 | load_frame(UnpicklerObject *self) |
| 6050 | { |
| 6051 | char *s; |
| 6052 | Py_ssize_t frame_len; |
| 6053 | |
| 6054 | if (_Unpickler_Read(self, &s, 8) < 0) |
| 6055 | return -1; |
| 6056 | |
| 6057 | frame_len = calc_binsize(s, 8); |
| 6058 | if (frame_len < 0) { |
| 6059 | PyErr_Format(PyExc_OverflowError, |
| 6060 | "FRAME length exceeds system's maximum of %zd bytes", |
| 6061 | PY_SSIZE_T_MAX); |
| 6062 | return -1; |
| 6063 | } |
| 6064 | |
| 6065 | if (_Unpickler_Read(self, &s, frame_len) < 0) |
| 6066 | return -1; |
| 6067 | |
| 6068 | /* Rewind to start of frame */ |
| 6069 | self->next_read_idx -= frame_len; |
| 6070 | return 0; |
| 6071 | } |
| 6072 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6073 | static PyObject * |
| 6074 | load(UnpicklerObject *self) |
| 6075 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6076 | PyObject *value = NULL; |
Christian Heimes | 27ea78b | 2014-01-27 01:03:53 +0100 | [diff] [blame] | 6077 | char *s = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6078 | |
| 6079 | self->num_marks = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6080 | self->proto = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6081 | if (Py_SIZE(self->stack)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6082 | Pdata_clear(self->stack, 0); |
| 6083 | |
| 6084 | /* Convenient macros for the dispatch while-switch loop just below. */ |
| 6085 | #define OP(opcode, load_func) \ |
| 6086 | case opcode: if (load_func(self) < 0) break; continue; |
| 6087 | |
| 6088 | #define OP_ARG(opcode, load_func, arg) \ |
| 6089 | case opcode: if (load_func(self, (arg)) < 0) break; continue; |
| 6090 | |
| 6091 | while (1) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6092 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6093 | break; |
| 6094 | |
| 6095 | switch ((enum opcode)s[0]) { |
| 6096 | OP(NONE, load_none) |
| 6097 | OP(BININT, load_binint) |
| 6098 | OP(BININT1, load_binint1) |
| 6099 | OP(BININT2, load_binint2) |
| 6100 | OP(INT, load_int) |
| 6101 | OP(LONG, load_long) |
| 6102 | OP_ARG(LONG1, load_counted_long, 1) |
| 6103 | OP_ARG(LONG4, load_counted_long, 4) |
| 6104 | OP(FLOAT, load_float) |
| 6105 | OP(BINFLOAT, load_binfloat) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6106 | OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1) |
| 6107 | OP_ARG(BINBYTES, load_counted_binbytes, 4) |
| 6108 | OP_ARG(BINBYTES8, load_counted_binbytes, 8) |
| 6109 | OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1) |
| 6110 | OP_ARG(BINSTRING, load_counted_binstring, 4) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6111 | OP(STRING, load_string) |
| 6112 | OP(UNICODE, load_unicode) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6113 | OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1) |
| 6114 | OP_ARG(BINUNICODE, load_counted_binunicode, 4) |
| 6115 | OP_ARG(BINUNICODE8, load_counted_binunicode, 8) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6116 | OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0) |
| 6117 | OP_ARG(TUPLE1, load_counted_tuple, 1) |
| 6118 | OP_ARG(TUPLE2, load_counted_tuple, 2) |
| 6119 | OP_ARG(TUPLE3, load_counted_tuple, 3) |
| 6120 | OP(TUPLE, load_tuple) |
| 6121 | OP(EMPTY_LIST, load_empty_list) |
| 6122 | OP(LIST, load_list) |
| 6123 | OP(EMPTY_DICT, load_empty_dict) |
| 6124 | OP(DICT, load_dict) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6125 | OP(EMPTY_SET, load_empty_set) |
| 6126 | OP(ADDITEMS, load_additems) |
| 6127 | OP(FROZENSET, load_frozenset) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6128 | OP(OBJ, load_obj) |
| 6129 | OP(INST, load_inst) |
| 6130 | OP(NEWOBJ, load_newobj) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6131 | OP(NEWOBJ_EX, load_newobj_ex) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6132 | OP(GLOBAL, load_global) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6133 | OP(STACK_GLOBAL, load_stack_global) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6134 | OP(APPEND, load_append) |
| 6135 | OP(APPENDS, load_appends) |
| 6136 | OP(BUILD, load_build) |
| 6137 | OP(DUP, load_dup) |
| 6138 | OP(BINGET, load_binget) |
| 6139 | OP(LONG_BINGET, load_long_binget) |
| 6140 | OP(GET, load_get) |
| 6141 | OP(MARK, load_mark) |
| 6142 | OP(BINPUT, load_binput) |
| 6143 | OP(LONG_BINPUT, load_long_binput) |
| 6144 | OP(PUT, load_put) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6145 | OP(MEMOIZE, load_memoize) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6146 | OP(POP, load_pop) |
| 6147 | OP(POP_MARK, load_pop_mark) |
| 6148 | OP(SETITEM, load_setitem) |
| 6149 | OP(SETITEMS, load_setitems) |
| 6150 | OP(PERSID, load_persid) |
| 6151 | OP(BINPERSID, load_binpersid) |
| 6152 | OP(REDUCE, load_reduce) |
| 6153 | OP(PROTO, load_proto) |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6154 | OP(FRAME, load_frame) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6155 | OP_ARG(EXT1, load_extension, 1) |
| 6156 | OP_ARG(EXT2, load_extension, 2) |
| 6157 | OP_ARG(EXT4, load_extension, 4) |
| 6158 | OP_ARG(NEWTRUE, load_bool, Py_True) |
| 6159 | OP_ARG(NEWFALSE, load_bool, Py_False) |
| 6160 | |
| 6161 | case STOP: |
| 6162 | break; |
| 6163 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6164 | default: |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6165 | if (s[0] == '\0') { |
Benjamin Peterson | adde86d | 2011-09-23 13:41:41 -0400 | [diff] [blame] | 6166 | PyErr_SetNone(PyExc_EOFError); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6167 | } |
| 6168 | else { |
| 6169 | PickleState *st = _Pickle_GetGlobalState(); |
| 6170 | PyErr_Format(st->UnpicklingError, |
Benjamin Peterson | adde86d | 2011-09-23 13:41:41 -0400 | [diff] [blame] | 6171 | "invalid load key, '%c'.", s[0]); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6172 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6173 | return NULL; |
| 6174 | } |
| 6175 | |
| 6176 | break; /* and we are done! */ |
| 6177 | } |
| 6178 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6179 | if (PyErr_Occurred()) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6180 | return NULL; |
| 6181 | } |
| 6182 | |
Victor Stinner | 2ae57e3 | 2013-10-31 13:39:23 +0100 | [diff] [blame] | 6183 | if (_Unpickler_SkipConsumed(self) < 0) |
| 6184 | return NULL; |
| 6185 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6186 | PDATA_POP(self->stack, value); |
| 6187 | return value; |
| 6188 | } |
| 6189 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6190 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6191 | |
| 6192 | _pickle.Unpickler.load |
| 6193 | |
| 6194 | Load a pickle. |
| 6195 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6196 | Read a pickled object representation from the open file object given |
| 6197 | in the constructor, and return the reconstituted object hierarchy |
| 6198 | specified therein. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6199 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6200 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6201 | static PyObject * |
Larry Hastings | c204726 | 2014-01-25 20:43:29 -0800 | [diff] [blame] | 6202 | _pickle_Unpickler_load_impl(UnpicklerObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6203 | /*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6204 | { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6205 | UnpicklerObject *unpickler = (UnpicklerObject*)self; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6206 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6207 | /* Check whether the Unpickler was initialized correctly. This prevents |
| 6208 | segfaulting if a subclass overridden __init__ with a function that does |
| 6209 | not call Unpickler.__init__(). Here, we simply ensure that self->read |
| 6210 | is not NULL. */ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6211 | if (unpickler->read == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6212 | PickleState *st = _Pickle_GetGlobalState(); |
| 6213 | PyErr_Format(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6214 | "Unpickler.__init__() was not called by %s.__init__()", |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6215 | Py_TYPE(unpickler)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6216 | return NULL; |
| 6217 | } |
| 6218 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6219 | return load(unpickler); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6220 | } |
| 6221 | |
| 6222 | /* The name of find_class() is misleading. In newer pickle protocols, this |
| 6223 | function is used for loading any global (i.e., functions), not just |
| 6224 | classes. The name is kept only for backward compatibility. */ |
| 6225 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6226 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6227 | |
| 6228 | _pickle.Unpickler.find_class |
| 6229 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6230 | module_name: object |
| 6231 | global_name: object |
| 6232 | / |
| 6233 | |
| 6234 | Return an object from a specified module. |
| 6235 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6236 | If necessary, the module will be imported. Subclasses may override |
| 6237 | this method (e.g. to restrict unpickling of arbitrary classes and |
| 6238 | functions). |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6239 | |
| 6240 | This method is called whenever a class or a function object is |
| 6241 | needed. Both arguments passed are str objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6242 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6243 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6244 | static PyObject * |
| 6245 | _pickle_Unpickler_find_class_impl(UnpicklerObject *self, PyObject *module_name, PyObject *global_name) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6246 | /*[clinic end generated code: output=64c77437e088e188 input=e2e6a865de093ef4]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6247 | { |
| 6248 | PyObject *global; |
| 6249 | PyObject *modules_dict; |
| 6250 | PyObject *module; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6251 | _Py_IDENTIFIER(modules); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6252 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6253 | /* Try to map the old names used in Python 2.x to the new ones used in |
| 6254 | Python 3.x. We do this only with old pickle protocols and when the |
| 6255 | user has not disabled the feature. */ |
| 6256 | if (self->proto < 3 && self->fix_imports) { |
| 6257 | PyObject *key; |
| 6258 | PyObject *item; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6259 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6260 | |
| 6261 | /* Check if the global (i.e., a function or a class) was renamed |
| 6262 | or moved to another module. */ |
| 6263 | key = PyTuple_Pack(2, module_name, global_name); |
| 6264 | if (key == NULL) |
| 6265 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6266 | item = PyDict_GetItemWithError(st->name_mapping_2to3, key); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6267 | Py_DECREF(key); |
| 6268 | if (item) { |
| 6269 | if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
| 6270 | PyErr_Format(PyExc_RuntimeError, |
| 6271 | "_compat_pickle.NAME_MAPPING values should be " |
| 6272 | "2-tuples, not %.200s", Py_TYPE(item)->tp_name); |
| 6273 | return NULL; |
| 6274 | } |
| 6275 | module_name = PyTuple_GET_ITEM(item, 0); |
| 6276 | global_name = PyTuple_GET_ITEM(item, 1); |
| 6277 | if (!PyUnicode_Check(module_name) || |
| 6278 | !PyUnicode_Check(global_name)) { |
| 6279 | PyErr_Format(PyExc_RuntimeError, |
| 6280 | "_compat_pickle.NAME_MAPPING values should be " |
| 6281 | "pairs of str, not (%.200s, %.200s)", |
| 6282 | Py_TYPE(module_name)->tp_name, |
| 6283 | Py_TYPE(global_name)->tp_name); |
| 6284 | return NULL; |
| 6285 | } |
| 6286 | } |
| 6287 | else if (PyErr_Occurred()) { |
| 6288 | return NULL; |
| 6289 | } |
| 6290 | |
| 6291 | /* Check if the module was renamed. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6292 | item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6293 | if (item) { |
| 6294 | if (!PyUnicode_Check(item)) { |
| 6295 | PyErr_Format(PyExc_RuntimeError, |
| 6296 | "_compat_pickle.IMPORT_MAPPING values should be " |
| 6297 | "strings, not %.200s", Py_TYPE(item)->tp_name); |
| 6298 | return NULL; |
| 6299 | } |
| 6300 | module_name = item; |
| 6301 | } |
| 6302 | else if (PyErr_Occurred()) { |
| 6303 | return NULL; |
| 6304 | } |
| 6305 | } |
| 6306 | |
Victor Stinner | bb52020 | 2013-11-06 22:40:41 +0100 | [diff] [blame] | 6307 | modules_dict = _PySys_GetObjectId(&PyId_modules); |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 6308 | if (modules_dict == NULL) { |
| 6309 | PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6310 | return NULL; |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 6311 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6312 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6313 | module = PyDict_GetItemWithError(modules_dict, module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6314 | if (module == NULL) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6315 | if (PyErr_Occurred()) |
| 6316 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6317 | module = PyImport_Import(module_name); |
| 6318 | if (module == NULL) |
| 6319 | return NULL; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6320 | global = getattribute(module, global_name, self->proto >= 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6321 | Py_DECREF(module); |
| 6322 | } |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 6323 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6324 | global = getattribute(module, global_name, self->proto >= 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6325 | } |
| 6326 | return global; |
| 6327 | } |
| 6328 | |
| 6329 | static struct PyMethodDef Unpickler_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6330 | _PICKLE_UNPICKLER_LOAD_METHODDEF |
| 6331 | _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6332 | {NULL, NULL} /* sentinel */ |
| 6333 | }; |
| 6334 | |
| 6335 | static void |
| 6336 | Unpickler_dealloc(UnpicklerObject *self) |
| 6337 | { |
| 6338 | PyObject_GC_UnTrack((PyObject *)self); |
| 6339 | Py_XDECREF(self->readline); |
| 6340 | Py_XDECREF(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6341 | Py_XDECREF(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6342 | Py_XDECREF(self->stack); |
| 6343 | Py_XDECREF(self->pers_func); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6344 | if (self->buffer.buf != NULL) { |
| 6345 | PyBuffer_Release(&self->buffer); |
| 6346 | self->buffer.buf = NULL; |
| 6347 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6348 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6349 | _Unpickler_MemoCleanup(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6350 | PyMem_Free(self->marks); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6351 | PyMem_Free(self->input_line); |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6352 | PyMem_Free(self->encoding); |
| 6353 | PyMem_Free(self->errors); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6354 | |
| 6355 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 6356 | } |
| 6357 | |
| 6358 | static int |
| 6359 | Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg) |
| 6360 | { |
| 6361 | Py_VISIT(self->readline); |
| 6362 | Py_VISIT(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6363 | Py_VISIT(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6364 | Py_VISIT(self->stack); |
| 6365 | Py_VISIT(self->pers_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6366 | return 0; |
| 6367 | } |
| 6368 | |
| 6369 | static int |
| 6370 | Unpickler_clear(UnpicklerObject *self) |
| 6371 | { |
| 6372 | Py_CLEAR(self->readline); |
| 6373 | Py_CLEAR(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6374 | Py_CLEAR(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6375 | Py_CLEAR(self->stack); |
| 6376 | Py_CLEAR(self->pers_func); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6377 | if (self->buffer.buf != NULL) { |
| 6378 | PyBuffer_Release(&self->buffer); |
| 6379 | self->buffer.buf = NULL; |
| 6380 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6381 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6382 | _Unpickler_MemoCleanup(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6383 | PyMem_Free(self->marks); |
| 6384 | self->marks = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6385 | PyMem_Free(self->input_line); |
| 6386 | self->input_line = NULL; |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6387 | PyMem_Free(self->encoding); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6388 | self->encoding = NULL; |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6389 | PyMem_Free(self->errors); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6390 | self->errors = NULL; |
| 6391 | |
| 6392 | return 0; |
| 6393 | } |
| 6394 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6395 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6396 | |
| 6397 | _pickle.Unpickler.__init__ |
| 6398 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6399 | file: object |
| 6400 | * |
| 6401 | fix_imports: bool = True |
| 6402 | encoding: str = 'ASCII' |
| 6403 | errors: str = 'strict' |
| 6404 | |
| 6405 | This takes a binary file for reading a pickle data stream. |
| 6406 | |
| 6407 | The protocol version of the pickle is detected automatically, so no |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6408 | protocol argument is needed. Bytes past the pickled object's |
| 6409 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6410 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6411 | The argument *file* must have two methods, a read() method that takes |
| 6412 | an integer argument, and a readline() method that requires no |
| 6413 | arguments. Both methods should return bytes. Thus *file* can be a |
| 6414 | binary file object opened for reading, a io.BytesIO object, or any |
| 6415 | other custom object that meets this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6416 | |
| 6417 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
| 6418 | which are used to control compatiblity support for pickle stream |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6419 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 6420 | map the old Python 2 names to the new names used in Python 3. The |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6421 | *encoding* and *errors* tell pickle how to decode 8-bit string |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6422 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 6423 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 6424 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6425 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6426 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6427 | static int |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6428 | _pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file, int fix_imports, const char *encoding, const char *errors) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6429 | /*[clinic end generated code: output=b9ed1d84d315f3b5 input=30b4dc9e976b890c]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6430 | { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 6431 | _Py_IDENTIFIER(persistent_load); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6432 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6433 | /* In case of multiple __init__() calls, clear previous content. */ |
| 6434 | if (self->read != NULL) |
| 6435 | (void)Unpickler_clear(self); |
| 6436 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6437 | if (_Unpickler_SetInputStream(self, file) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6438 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6439 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6440 | if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6441 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6442 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6443 | self->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6444 | if (self->fix_imports == -1) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6445 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6446 | |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 6447 | if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) { |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6448 | self->pers_func = _PyObject_GetAttrId((PyObject *)self, |
| 6449 | &PyId_persistent_load); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6450 | if (self->pers_func == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6451 | return 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6452 | } |
| 6453 | else { |
| 6454 | self->pers_func = NULL; |
| 6455 | } |
| 6456 | |
| 6457 | self->stack = (Pdata *)Pdata_New(); |
| 6458 | if (self->stack == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6459 | return 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6460 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6461 | self->memo_size = 32; |
| 6462 | self->memo = _Unpickler_NewMemo(self->memo_size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6463 | if (self->memo == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6464 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6465 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6466 | self->proto = 0; |
Alexandre Vassalotti | 0e7aa8c | 2009-04-03 04:17:41 +0000 | [diff] [blame] | 6467 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6468 | return 0; |
| 6469 | } |
| 6470 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6471 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6472 | /* Define a proxy object for the Unpickler's internal memo object. This is to |
| 6473 | * avoid breaking code like: |
| 6474 | * unpickler.memo.clear() |
| 6475 | * and |
| 6476 | * unpickler.memo = saved_memo |
| 6477 | * Is this a good idea? Not really, but we don't want to break code that uses |
| 6478 | * it. Note that we don't implement the entire mapping API here. This is |
| 6479 | * intentional, as these should be treated as black-box implementation details. |
| 6480 | * |
| 6481 | * We do, however, have to implement pickling/unpickling support because of |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 6482 | * real-world code like cvs2svn. |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6483 | */ |
| 6484 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6485 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6486 | _pickle.UnpicklerMemoProxy.clear |
| 6487 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6488 | Remove all items from memo. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6489 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6490 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6491 | static PyObject * |
| 6492 | _pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6493 | /*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6494 | { |
| 6495 | _Unpickler_MemoCleanup(self->unpickler); |
| 6496 | self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size); |
| 6497 | if (self->unpickler->memo == NULL) |
| 6498 | return NULL; |
| 6499 | Py_RETURN_NONE; |
| 6500 | } |
| 6501 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6502 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6503 | _pickle.UnpicklerMemoProxy.copy |
| 6504 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6505 | Copy the memo to a new object. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6506 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6507 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6508 | static PyObject * |
| 6509 | _pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6510 | /*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6511 | { |
| 6512 | Py_ssize_t i; |
| 6513 | PyObject *new_memo = PyDict_New(); |
| 6514 | if (new_memo == NULL) |
| 6515 | return NULL; |
| 6516 | |
| 6517 | for (i = 0; i < self->unpickler->memo_size; i++) { |
| 6518 | int status; |
| 6519 | PyObject *key, *value; |
| 6520 | |
| 6521 | value = self->unpickler->memo[i]; |
| 6522 | if (value == NULL) |
| 6523 | continue; |
| 6524 | |
| 6525 | key = PyLong_FromSsize_t(i); |
| 6526 | if (key == NULL) |
| 6527 | goto error; |
| 6528 | status = PyDict_SetItem(new_memo, key, value); |
| 6529 | Py_DECREF(key); |
| 6530 | if (status < 0) |
| 6531 | goto error; |
| 6532 | } |
| 6533 | return new_memo; |
| 6534 | |
| 6535 | error: |
| 6536 | Py_DECREF(new_memo); |
| 6537 | return NULL; |
| 6538 | } |
| 6539 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6540 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6541 | _pickle.UnpicklerMemoProxy.__reduce__ |
| 6542 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6543 | Implement pickling support. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6544 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6545 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6546 | static PyObject * |
| 6547 | _pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6548 | /*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6549 | { |
| 6550 | PyObject *reduce_value; |
| 6551 | PyObject *constructor_args; |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6552 | PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6553 | if (contents == NULL) |
| 6554 | return NULL; |
| 6555 | |
| 6556 | reduce_value = PyTuple_New(2); |
| 6557 | if (reduce_value == NULL) { |
| 6558 | Py_DECREF(contents); |
| 6559 | return NULL; |
| 6560 | } |
| 6561 | constructor_args = PyTuple_New(1); |
| 6562 | if (constructor_args == NULL) { |
| 6563 | Py_DECREF(contents); |
| 6564 | Py_DECREF(reduce_value); |
| 6565 | return NULL; |
| 6566 | } |
| 6567 | PyTuple_SET_ITEM(constructor_args, 0, contents); |
| 6568 | Py_INCREF((PyObject *)&PyDict_Type); |
| 6569 | PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type); |
| 6570 | PyTuple_SET_ITEM(reduce_value, 1, constructor_args); |
| 6571 | return reduce_value; |
| 6572 | } |
| 6573 | |
| 6574 | static PyMethodDef unpicklerproxy_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6575 | _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF |
| 6576 | _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF |
| 6577 | _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6578 | {NULL, NULL} /* sentinel */ |
| 6579 | }; |
| 6580 | |
| 6581 | static void |
| 6582 | UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self) |
| 6583 | { |
| 6584 | PyObject_GC_UnTrack(self); |
| 6585 | Py_XDECREF(self->unpickler); |
| 6586 | PyObject_GC_Del((PyObject *)self); |
| 6587 | } |
| 6588 | |
| 6589 | static int |
| 6590 | UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self, |
| 6591 | visitproc visit, void *arg) |
| 6592 | { |
| 6593 | Py_VISIT(self->unpickler); |
| 6594 | return 0; |
| 6595 | } |
| 6596 | |
| 6597 | static int |
| 6598 | UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self) |
| 6599 | { |
| 6600 | Py_CLEAR(self->unpickler); |
| 6601 | return 0; |
| 6602 | } |
| 6603 | |
| 6604 | static PyTypeObject UnpicklerMemoProxyType = { |
| 6605 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6606 | "_pickle.UnpicklerMemoProxy", /*tp_name*/ |
| 6607 | sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/ |
| 6608 | 0, |
| 6609 | (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */ |
| 6610 | 0, /* tp_print */ |
| 6611 | 0, /* tp_getattr */ |
| 6612 | 0, /* tp_setattr */ |
| 6613 | 0, /* tp_compare */ |
| 6614 | 0, /* tp_repr */ |
| 6615 | 0, /* tp_as_number */ |
| 6616 | 0, /* tp_as_sequence */ |
| 6617 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 6618 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6619 | 0, /* tp_call */ |
| 6620 | 0, /* tp_str */ |
| 6621 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 6622 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 6623 | 0, /* tp_as_buffer */ |
| 6624 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 6625 | 0, /* tp_doc */ |
| 6626 | (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */ |
| 6627 | (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */ |
| 6628 | 0, /* tp_richcompare */ |
| 6629 | 0, /* tp_weaklistoffset */ |
| 6630 | 0, /* tp_iter */ |
| 6631 | 0, /* tp_iternext */ |
| 6632 | unpicklerproxy_methods, /* tp_methods */ |
| 6633 | }; |
| 6634 | |
| 6635 | static PyObject * |
| 6636 | UnpicklerMemoProxy_New(UnpicklerObject *unpickler) |
| 6637 | { |
| 6638 | UnpicklerMemoProxyObject *self; |
| 6639 | |
| 6640 | self = PyObject_GC_New(UnpicklerMemoProxyObject, |
| 6641 | &UnpicklerMemoProxyType); |
| 6642 | if (self == NULL) |
| 6643 | return NULL; |
| 6644 | Py_INCREF(unpickler); |
| 6645 | self->unpickler = unpickler; |
| 6646 | PyObject_GC_Track(self); |
| 6647 | return (PyObject *)self; |
| 6648 | } |
| 6649 | |
| 6650 | /*****************************************************************************/ |
| 6651 | |
| 6652 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6653 | static PyObject * |
| 6654 | Unpickler_get_memo(UnpicklerObject *self) |
| 6655 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6656 | return UnpicklerMemoProxy_New(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6657 | } |
| 6658 | |
| 6659 | static int |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6660 | Unpickler_set_memo(UnpicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6661 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6662 | PyObject **new_memo; |
| 6663 | Py_ssize_t new_memo_size = 0; |
| 6664 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6665 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6666 | if (obj == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6667 | PyErr_SetString(PyExc_TypeError, |
| 6668 | "attribute deletion is not supported"); |
| 6669 | return -1; |
| 6670 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6671 | |
| 6672 | if (Py_TYPE(obj) == &UnpicklerMemoProxyType) { |
| 6673 | UnpicklerObject *unpickler = |
| 6674 | ((UnpicklerMemoProxyObject *)obj)->unpickler; |
| 6675 | |
| 6676 | new_memo_size = unpickler->memo_size; |
| 6677 | new_memo = _Unpickler_NewMemo(new_memo_size); |
| 6678 | if (new_memo == NULL) |
| 6679 | return -1; |
| 6680 | |
| 6681 | for (i = 0; i < new_memo_size; i++) { |
| 6682 | Py_XINCREF(unpickler->memo[i]); |
| 6683 | new_memo[i] = unpickler->memo[i]; |
| 6684 | } |
| 6685 | } |
| 6686 | else if (PyDict_Check(obj)) { |
| 6687 | Py_ssize_t i = 0; |
| 6688 | PyObject *key, *value; |
| 6689 | |
| 6690 | new_memo_size = PyDict_Size(obj); |
| 6691 | new_memo = _Unpickler_NewMemo(new_memo_size); |
| 6692 | if (new_memo == NULL) |
| 6693 | return -1; |
| 6694 | |
| 6695 | while (PyDict_Next(obj, &i, &key, &value)) { |
| 6696 | Py_ssize_t idx; |
| 6697 | if (!PyLong_Check(key)) { |
| 6698 | PyErr_SetString(PyExc_TypeError, |
| 6699 | "memo key must be integers"); |
| 6700 | goto error; |
| 6701 | } |
| 6702 | idx = PyLong_AsSsize_t(key); |
| 6703 | if (idx == -1 && PyErr_Occurred()) |
| 6704 | goto error; |
Christian Heimes | a24b4d2 | 2013-07-01 15:17:45 +0200 | [diff] [blame] | 6705 | if (idx < 0) { |
| 6706 | PyErr_SetString(PyExc_ValueError, |
Christian Heimes | 8087879 | 2013-07-01 15:23:39 +0200 | [diff] [blame] | 6707 | "memo key must be positive integers."); |
Christian Heimes | a24b4d2 | 2013-07-01 15:17:45 +0200 | [diff] [blame] | 6708 | goto error; |
| 6709 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6710 | if (_Unpickler_MemoPut(self, idx, value) < 0) |
| 6711 | goto error; |
| 6712 | } |
| 6713 | } |
| 6714 | else { |
| 6715 | PyErr_Format(PyExc_TypeError, |
| 6716 | "'memo' attribute must be an UnpicklerMemoProxy object" |
| 6717 | "or dict, not %.200s", Py_TYPE(obj)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6718 | return -1; |
| 6719 | } |
| 6720 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6721 | _Unpickler_MemoCleanup(self); |
| 6722 | self->memo_size = new_memo_size; |
| 6723 | self->memo = new_memo; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6724 | |
| 6725 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6726 | |
| 6727 | error: |
| 6728 | if (new_memo_size) { |
| 6729 | i = new_memo_size; |
| 6730 | while (--i >= 0) { |
| 6731 | Py_XDECREF(new_memo[i]); |
| 6732 | } |
| 6733 | PyMem_FREE(new_memo); |
| 6734 | } |
| 6735 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6736 | } |
| 6737 | |
| 6738 | static PyObject * |
| 6739 | Unpickler_get_persload(UnpicklerObject *self) |
| 6740 | { |
| 6741 | if (self->pers_func == NULL) |
| 6742 | PyErr_SetString(PyExc_AttributeError, "persistent_load"); |
| 6743 | else |
| 6744 | Py_INCREF(self->pers_func); |
| 6745 | return self->pers_func; |
| 6746 | } |
| 6747 | |
| 6748 | static int |
| 6749 | Unpickler_set_persload(UnpicklerObject *self, PyObject *value) |
| 6750 | { |
| 6751 | PyObject *tmp; |
| 6752 | |
| 6753 | if (value == NULL) { |
| 6754 | PyErr_SetString(PyExc_TypeError, |
| 6755 | "attribute deletion is not supported"); |
| 6756 | return -1; |
| 6757 | } |
| 6758 | if (!PyCallable_Check(value)) { |
| 6759 | PyErr_SetString(PyExc_TypeError, |
| 6760 | "persistent_load must be a callable taking " |
| 6761 | "one argument"); |
| 6762 | return -1; |
| 6763 | } |
| 6764 | |
| 6765 | tmp = self->pers_func; |
| 6766 | Py_INCREF(value); |
| 6767 | self->pers_func = value; |
| 6768 | Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */ |
| 6769 | |
| 6770 | return 0; |
| 6771 | } |
| 6772 | |
| 6773 | static PyGetSetDef Unpickler_getsets[] = { |
| 6774 | {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo}, |
| 6775 | {"persistent_load", (getter)Unpickler_get_persload, |
| 6776 | (setter)Unpickler_set_persload}, |
| 6777 | {NULL} |
| 6778 | }; |
| 6779 | |
| 6780 | static PyTypeObject Unpickler_Type = { |
| 6781 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6782 | "_pickle.Unpickler", /*tp_name*/ |
| 6783 | sizeof(UnpicklerObject), /*tp_basicsize*/ |
| 6784 | 0, /*tp_itemsize*/ |
| 6785 | (destructor)Unpickler_dealloc, /*tp_dealloc*/ |
| 6786 | 0, /*tp_print*/ |
| 6787 | 0, /*tp_getattr*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6788 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 6789 | 0, /*tp_reserved*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6790 | 0, /*tp_repr*/ |
| 6791 | 0, /*tp_as_number*/ |
| 6792 | 0, /*tp_as_sequence*/ |
| 6793 | 0, /*tp_as_mapping*/ |
| 6794 | 0, /*tp_hash*/ |
| 6795 | 0, /*tp_call*/ |
| 6796 | 0, /*tp_str*/ |
| 6797 | 0, /*tp_getattro*/ |
| 6798 | 0, /*tp_setattro*/ |
| 6799 | 0, /*tp_as_buffer*/ |
| 6800 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6801 | _pickle_Unpickler___init____doc__, /*tp_doc*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6802 | (traverseproc)Unpickler_traverse, /*tp_traverse*/ |
| 6803 | (inquiry)Unpickler_clear, /*tp_clear*/ |
| 6804 | 0, /*tp_richcompare*/ |
| 6805 | 0, /*tp_weaklistoffset*/ |
| 6806 | 0, /*tp_iter*/ |
| 6807 | 0, /*tp_iternext*/ |
| 6808 | Unpickler_methods, /*tp_methods*/ |
| 6809 | 0, /*tp_members*/ |
| 6810 | Unpickler_getsets, /*tp_getset*/ |
| 6811 | 0, /*tp_base*/ |
| 6812 | 0, /*tp_dict*/ |
| 6813 | 0, /*tp_descr_get*/ |
| 6814 | 0, /*tp_descr_set*/ |
| 6815 | 0, /*tp_dictoffset*/ |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6816 | _pickle_Unpickler___init__, /*tp_init*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6817 | PyType_GenericAlloc, /*tp_alloc*/ |
| 6818 | PyType_GenericNew, /*tp_new*/ |
| 6819 | PyObject_GC_Del, /*tp_free*/ |
| 6820 | 0, /*tp_is_gc*/ |
| 6821 | }; |
| 6822 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6823 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6824 | |
| 6825 | _pickle.dump |
| 6826 | |
| 6827 | obj: object |
| 6828 | file: object |
| 6829 | protocol: object = NULL |
| 6830 | * |
| 6831 | fix_imports: bool = True |
| 6832 | |
| 6833 | Write a pickled representation of obj to the open file object file. |
| 6834 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6835 | This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may |
| 6836 | be more efficient. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6837 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6838 | The optional *protocol* argument tells the pickler to use the given |
| 6839 | protocol supported protocols are 0, 1, 2, 3 and 4. The default |
| 6840 | protocol is 3; a backward-incompatible protocol designed for Python 3. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6841 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6842 | Specifying a negative protocol version selects the highest protocol |
| 6843 | version supported. The higher the protocol used, the more recent the |
| 6844 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6845 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6846 | The *file* argument must have a write() method that accepts a single |
| 6847 | bytes argument. It can thus be a file object opened for binary |
| 6848 | writing, a io.BytesIO instance, or any other custom object that meets |
| 6849 | this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6850 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6851 | If *fix_imports* is True and protocol is less than 3, pickle will try |
| 6852 | to map the new Python 3 names to the old module names used in Python |
| 6853 | 2, so that the pickle data stream is readable with Python 2. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6854 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6855 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6856 | static PyObject * |
| 6857 | _pickle_dump_impl(PyModuleDef *module, PyObject *obj, PyObject *file, PyObject *protocol, int fix_imports) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6858 | /*[clinic end generated code: output=a606e626d553850d input=e9e5fdd48de92eae]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6859 | { |
| 6860 | PicklerObject *pickler = _Pickler_New(); |
| 6861 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6862 | if (pickler == NULL) |
| 6863 | return NULL; |
| 6864 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6865 | if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6866 | goto error; |
| 6867 | |
| 6868 | if (_Pickler_SetOutputStream(pickler, file) < 0) |
| 6869 | goto error; |
| 6870 | |
| 6871 | if (dump(pickler, obj) < 0) |
| 6872 | goto error; |
| 6873 | |
| 6874 | if (_Pickler_FlushToFile(pickler) < 0) |
| 6875 | goto error; |
| 6876 | |
| 6877 | Py_DECREF(pickler); |
| 6878 | Py_RETURN_NONE; |
| 6879 | |
| 6880 | error: |
| 6881 | Py_XDECREF(pickler); |
| 6882 | return NULL; |
| 6883 | } |
| 6884 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6885 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6886 | |
| 6887 | _pickle.dumps |
| 6888 | |
| 6889 | obj: object |
| 6890 | protocol: object = NULL |
| 6891 | * |
| 6892 | fix_imports: bool = True |
| 6893 | |
| 6894 | Return the pickled representation of the object as a bytes object. |
| 6895 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6896 | The optional *protocol* argument tells the pickler to use the given |
| 6897 | protocol; supported protocols are 0, 1, 2, 3 and 4. The default |
| 6898 | protocol is 3; a backward-incompatible protocol designed for Python 3. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6899 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6900 | Specifying a negative protocol version selects the highest protocol |
| 6901 | version supported. The higher the protocol used, the more recent the |
| 6902 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6903 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6904 | If *fix_imports* is True and *protocol* is less than 3, pickle will |
| 6905 | try to map the new Python 3 names to the old module names used in |
| 6906 | Python 2, so that the pickle data stream is readable with Python 2. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6907 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6908 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6909 | static PyObject * |
| 6910 | _pickle_dumps_impl(PyModuleDef *module, PyObject *obj, PyObject *protocol, int fix_imports) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6911 | /*[clinic end generated code: output=777f0deefe5b88ee input=293dbeda181580b7]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6912 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6913 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6914 | PicklerObject *pickler = _Pickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6915 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6916 | if (pickler == NULL) |
| 6917 | return NULL; |
| 6918 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6919 | if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6920 | goto error; |
| 6921 | |
| 6922 | if (dump(pickler, obj) < 0) |
| 6923 | goto error; |
| 6924 | |
| 6925 | result = _Pickler_GetString(pickler); |
| 6926 | Py_DECREF(pickler); |
| 6927 | return result; |
| 6928 | |
| 6929 | error: |
| 6930 | Py_XDECREF(pickler); |
| 6931 | return NULL; |
| 6932 | } |
| 6933 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6934 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6935 | |
| 6936 | _pickle.load |
| 6937 | |
| 6938 | file: object |
| 6939 | * |
| 6940 | fix_imports: bool = True |
| 6941 | encoding: str = 'ASCII' |
| 6942 | errors: str = 'strict' |
| 6943 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6944 | Read and return an object from the pickle data stored in a file. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6945 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6946 | This is equivalent to ``Unpickler(file).load()``, but may be more |
| 6947 | efficient. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6948 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6949 | The protocol version of the pickle is detected automatically, so no |
| 6950 | protocol argument is needed. Bytes past the pickled object's |
| 6951 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6952 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6953 | The argument *file* must have two methods, a read() method that takes |
| 6954 | an integer argument, and a readline() method that requires no |
| 6955 | arguments. Both methods should return bytes. Thus *file* can be a |
| 6956 | binary file object opened for reading, a io.BytesIO object, or any |
| 6957 | other custom object that meets this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6958 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6959 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
| 6960 | which are used to control compatiblity support for pickle stream |
| 6961 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 6962 | map the old Python 2 names to the new names used in Python 3. The |
| 6963 | *encoding* and *errors* tell pickle how to decode 8-bit string |
| 6964 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 6965 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 6966 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6967 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6968 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6969 | static PyObject * |
| 6970 | _pickle_load_impl(PyModuleDef *module, PyObject *file, int fix_imports, const char *encoding, const char *errors) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6971 | /*[clinic end generated code: output=568c61356c172654 input=da97372e38e510a6]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6972 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6973 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6974 | UnpicklerObject *unpickler = _Unpickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6975 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6976 | if (unpickler == NULL) |
| 6977 | return NULL; |
| 6978 | |
| 6979 | if (_Unpickler_SetInputStream(unpickler, file) < 0) |
| 6980 | goto error; |
| 6981 | |
| 6982 | if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0) |
| 6983 | goto error; |
| 6984 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6985 | unpickler->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6986 | |
| 6987 | result = load(unpickler); |
| 6988 | Py_DECREF(unpickler); |
| 6989 | return result; |
| 6990 | |
| 6991 | error: |
| 6992 | Py_XDECREF(unpickler); |
| 6993 | return NULL; |
| 6994 | } |
| 6995 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6996 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6997 | |
| 6998 | _pickle.loads |
| 6999 | |
| 7000 | data: object |
| 7001 | * |
| 7002 | fix_imports: bool = True |
| 7003 | encoding: str = 'ASCII' |
| 7004 | errors: str = 'strict' |
| 7005 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7006 | Read and return an object from the given pickle data. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7007 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7008 | The protocol version of the pickle is detected automatically, so no |
| 7009 | protocol argument is needed. Bytes past the pickled object's |
| 7010 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7011 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7012 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
| 7013 | which are used to control compatiblity support for pickle stream |
| 7014 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 7015 | map the old Python 2 names to the new names used in Python 3. The |
| 7016 | *encoding* and *errors* tell pickle how to decode 8-bit string |
| 7017 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 7018 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 7019 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7020 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7021 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7022 | static PyObject * |
| 7023 | _pickle_loads_impl(PyModuleDef *module, PyObject *data, int fix_imports, const char *encoding, const char *errors) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 7024 | /*[clinic end generated code: output=0b3845ad110b2522 input=f57f0fdaa2b4cb8b]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7025 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7026 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7027 | UnpicklerObject *unpickler = _Unpickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7028 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7029 | if (unpickler == NULL) |
| 7030 | return NULL; |
| 7031 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7032 | if (_Unpickler_SetStringInput(unpickler, data) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7033 | goto error; |
| 7034 | |
| 7035 | if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0) |
| 7036 | goto error; |
| 7037 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7038 | unpickler->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7039 | |
| 7040 | result = load(unpickler); |
| 7041 | Py_DECREF(unpickler); |
| 7042 | return result; |
| 7043 | |
| 7044 | error: |
| 7045 | Py_XDECREF(unpickler); |
| 7046 | return NULL; |
| 7047 | } |
| 7048 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7049 | static struct PyMethodDef pickle_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7050 | _PICKLE_DUMP_METHODDEF |
| 7051 | _PICKLE_DUMPS_METHODDEF |
| 7052 | _PICKLE_LOAD_METHODDEF |
| 7053 | _PICKLE_LOADS_METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7054 | {NULL, NULL} /* sentinel */ |
| 7055 | }; |
| 7056 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7057 | static int |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7058 | pickle_clear(PyObject *m) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7059 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7060 | _Pickle_ClearState(_Pickle_GetState(m)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7061 | return 0; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7062 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7063 | |
Stefan Krah | f483b0f | 2013-12-14 13:43:10 +0100 | [diff] [blame] | 7064 | static void |
| 7065 | pickle_free(PyObject *m) |
| 7066 | { |
| 7067 | _Pickle_ClearState(_Pickle_GetState(m)); |
| 7068 | } |
| 7069 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7070 | static int |
| 7071 | pickle_traverse(PyObject *m, visitproc visit, void *arg) |
| 7072 | { |
| 7073 | PickleState *st = _Pickle_GetState(m); |
| 7074 | Py_VISIT(st->PickleError); |
| 7075 | Py_VISIT(st->PicklingError); |
| 7076 | Py_VISIT(st->UnpicklingError); |
| 7077 | Py_VISIT(st->dispatch_table); |
| 7078 | Py_VISIT(st->extension_registry); |
| 7079 | Py_VISIT(st->extension_cache); |
| 7080 | Py_VISIT(st->inverted_registry); |
| 7081 | Py_VISIT(st->name_mapping_2to3); |
| 7082 | Py_VISIT(st->import_mapping_2to3); |
| 7083 | Py_VISIT(st->name_mapping_3to2); |
| 7084 | Py_VISIT(st->import_mapping_3to2); |
| 7085 | Py_VISIT(st->codecs_encode); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7086 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7087 | } |
| 7088 | |
| 7089 | static struct PyModuleDef _picklemodule = { |
| 7090 | PyModuleDef_HEAD_INIT, |
Stefan Krah | f483b0f | 2013-12-14 13:43:10 +0100 | [diff] [blame] | 7091 | "_pickle", /* m_name */ |
| 7092 | pickle_module_doc, /* m_doc */ |
| 7093 | sizeof(PickleState), /* m_size */ |
| 7094 | pickle_methods, /* m_methods */ |
| 7095 | NULL, /* m_reload */ |
| 7096 | pickle_traverse, /* m_traverse */ |
| 7097 | pickle_clear, /* m_clear */ |
| 7098 | (freefunc)pickle_free /* m_free */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7099 | }; |
| 7100 | |
| 7101 | PyMODINIT_FUNC |
| 7102 | PyInit__pickle(void) |
| 7103 | { |
| 7104 | PyObject *m; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7105 | PickleState *st; |
| 7106 | |
| 7107 | m = PyState_FindModule(&_picklemodule); |
| 7108 | if (m) { |
| 7109 | Py_INCREF(m); |
| 7110 | return m; |
| 7111 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7112 | |
| 7113 | if (PyType_Ready(&Unpickler_Type) < 0) |
| 7114 | return NULL; |
| 7115 | if (PyType_Ready(&Pickler_Type) < 0) |
| 7116 | return NULL; |
| 7117 | if (PyType_Ready(&Pdata_Type) < 0) |
| 7118 | return NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7119 | if (PyType_Ready(&PicklerMemoProxyType) < 0) |
| 7120 | return NULL; |
| 7121 | if (PyType_Ready(&UnpicklerMemoProxyType) < 0) |
| 7122 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7123 | |
| 7124 | /* Create the module and add the functions. */ |
| 7125 | m = PyModule_Create(&_picklemodule); |
| 7126 | if (m == NULL) |
| 7127 | return NULL; |
| 7128 | |
Antoine Pitrou | 8391cf4 | 2011-07-15 21:01:21 +0200 | [diff] [blame] | 7129 | Py_INCREF(&Pickler_Type); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7130 | if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0) |
| 7131 | return NULL; |
Antoine Pitrou | 8391cf4 | 2011-07-15 21:01:21 +0200 | [diff] [blame] | 7132 | Py_INCREF(&Unpickler_Type); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7133 | if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0) |
| 7134 | return NULL; |
| 7135 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7136 | st = _Pickle_GetState(m); |
| 7137 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7138 | /* Initialize the exceptions. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7139 | st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL); |
| 7140 | if (st->PickleError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7141 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7142 | st->PicklingError = \ |
| 7143 | PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL); |
| 7144 | if (st->PicklingError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7145 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7146 | st->UnpicklingError = \ |
| 7147 | PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL); |
| 7148 | if (st->UnpicklingError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7149 | return NULL; |
| 7150 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7151 | Py_INCREF(st->PickleError); |
| 7152 | if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7153 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7154 | Py_INCREF(st->PicklingError); |
| 7155 | if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7156 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7157 | Py_INCREF(st->UnpicklingError); |
| 7158 | if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7159 | return NULL; |
| 7160 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7161 | if (_Pickle_InitState(st) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7162 | return NULL; |
| 7163 | |
| 7164 | return m; |
| 7165 | } |