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; |
| 423 | Py_ssize_t allocated = self->allocated; |
| 424 | Py_ssize_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 */ |
| 428 | if (new_allocated > 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; |
| 431 | if (new_allocated > (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; |
| 438 | self->allocated = 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 | { |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 853 | int i; |
| 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 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1459 | else if (*value <= 0xffffffffL) { |
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 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1516 | else if (idx <= 0xffffffffL) { |
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 | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1538 | getattribute(PyObject *obj, PyObject *name, int allow_qualname) { |
| 1539 | PyObject *dotted_path; |
| 1540 | Py_ssize_t i; |
| 1541 | _Py_static_string(PyId_dot, "."); |
| 1542 | _Py_static_string(PyId_locals, "<locals>"); |
| 1543 | |
| 1544 | dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1); |
| 1545 | if (dotted_path == NULL) { |
| 1546 | return NULL; |
| 1547 | } |
| 1548 | assert(Py_SIZE(dotted_path) >= 1); |
| 1549 | if (!allow_qualname && Py_SIZE(dotted_path) > 1) { |
| 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 | } |
| 1557 | Py_INCREF(obj); |
| 1558 | for (i = 0; i < Py_SIZE(dotted_path); i++) { |
| 1559 | PyObject *subpath = PyList_GET_ITEM(dotted_path, i); |
| 1560 | PyObject *tmp; |
| 1561 | PyObject *result = PyUnicode_RichCompare( |
| 1562 | subpath, _PyUnicode_FromId(&PyId_locals), Py_EQ); |
| 1563 | int is_equal = (result == Py_True); |
| 1564 | assert(PyBool_Check(result)); |
| 1565 | Py_DECREF(result); |
| 1566 | if (is_equal) { |
| 1567 | PyErr_Format(PyExc_AttributeError, |
| 1568 | "Can't get local attribute %R on %R", name, obj); |
| 1569 | Py_DECREF(dotted_path); |
| 1570 | Py_DECREF(obj); |
| 1571 | return NULL; |
| 1572 | } |
| 1573 | tmp = PyObject_GetAttr(obj, subpath); |
| 1574 | Py_DECREF(obj); |
| 1575 | if (tmp == NULL) { |
| 1576 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 1577 | PyErr_Clear(); |
| 1578 | PyErr_Format(PyExc_AttributeError, |
| 1579 | "Can't get attribute %R on %R", name, obj); |
| 1580 | } |
| 1581 | Py_DECREF(dotted_path); |
| 1582 | return NULL; |
| 1583 | } |
| 1584 | obj = tmp; |
| 1585 | } |
| 1586 | Py_DECREF(dotted_path); |
| 1587 | return obj; |
| 1588 | } |
| 1589 | |
| 1590 | static PyObject * |
| 1591 | whichmodule(PyObject *global, PyObject *global_name, int allow_qualname) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1592 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1593 | PyObject *module_name; |
| 1594 | PyObject *modules_dict; |
| 1595 | PyObject *module; |
| 1596 | PyObject *obj; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1597 | Py_ssize_t i, j; |
| 1598 | _Py_IDENTIFIER(__module__); |
| 1599 | _Py_IDENTIFIER(modules); |
| 1600 | _Py_IDENTIFIER(__main__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1601 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1602 | module_name = _PyObject_GetAttrId(global, &PyId___module__); |
| 1603 | |
| 1604 | if (module_name == NULL) { |
| 1605 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1606 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1607 | PyErr_Clear(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1608 | } |
| 1609 | else { |
| 1610 | /* In some rare cases (e.g., bound methods of extension types), |
| 1611 | __module__ can be None. If it is so, then search sys.modules for |
| 1612 | the module of global. */ |
| 1613 | if (module_name != Py_None) |
| 1614 | return module_name; |
| 1615 | Py_CLEAR(module_name); |
| 1616 | } |
| 1617 | assert(module_name == NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1618 | |
Victor Stinner | bb52020 | 2013-11-06 22:40:41 +0100 | [diff] [blame] | 1619 | modules_dict = _PySys_GetObjectId(&PyId_modules); |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 1620 | if (modules_dict == NULL) { |
| 1621 | PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1622 | return NULL; |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 1623 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1624 | |
| 1625 | i = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1626 | while ((j = PyDict_Next(modules_dict, &i, &module_name, &module))) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1627 | PyObject *result = PyUnicode_RichCompare( |
| 1628 | module_name, _PyUnicode_FromId(&PyId___main__), Py_EQ); |
| 1629 | int is_equal = (result == Py_True); |
| 1630 | assert(PyBool_Check(result)); |
| 1631 | Py_DECREF(result); |
| 1632 | if (is_equal) |
| 1633 | continue; |
| 1634 | if (module == Py_None) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1635 | continue; |
| 1636 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1637 | obj = getattribute(module, global_name, allow_qualname); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1638 | if (obj == NULL) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1639 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1640 | return NULL; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1641 | PyErr_Clear(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1642 | continue; |
| 1643 | } |
| 1644 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1645 | if (obj == global) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1646 | Py_DECREF(obj); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1647 | Py_INCREF(module_name); |
| 1648 | return module_name; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1649 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1650 | Py_DECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
| 1653 | /* If no module is found, use __main__. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1654 | module_name = _PyUnicode_FromId(&PyId___main__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1655 | Py_INCREF(module_name); |
| 1656 | return module_name; |
| 1657 | } |
| 1658 | |
| 1659 | /* fast_save_enter() and fast_save_leave() are guards against recursive |
| 1660 | objects when Pickler is used with the "fast mode" (i.e., with object |
| 1661 | memoization disabled). If the nesting of a list or dict object exceed |
| 1662 | FAST_NESTING_LIMIT, these guards will start keeping an internal |
| 1663 | reference to the seen list or dict objects and check whether these objects |
| 1664 | are recursive. These are not strictly necessary, since save() has a |
| 1665 | hard-coded recursion limit, but they give a nicer error message than the |
| 1666 | typical RuntimeError. */ |
| 1667 | static int |
| 1668 | fast_save_enter(PicklerObject *self, PyObject *obj) |
| 1669 | { |
| 1670 | /* if fast_nesting < 0, we're doing an error exit. */ |
| 1671 | if (++self->fast_nesting >= FAST_NESTING_LIMIT) { |
| 1672 | PyObject *key = NULL; |
| 1673 | if (self->fast_memo == NULL) { |
| 1674 | self->fast_memo = PyDict_New(); |
| 1675 | if (self->fast_memo == NULL) { |
| 1676 | self->fast_nesting = -1; |
| 1677 | return 0; |
| 1678 | } |
| 1679 | } |
| 1680 | key = PyLong_FromVoidPtr(obj); |
| 1681 | if (key == NULL) |
| 1682 | return 0; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 1683 | if (PyDict_GetItemWithError(self->fast_memo, key)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1684 | Py_DECREF(key); |
| 1685 | PyErr_Format(PyExc_ValueError, |
| 1686 | "fast mode: can't pickle cyclic objects " |
| 1687 | "including object type %.200s at %p", |
| 1688 | obj->ob_type->tp_name, obj); |
| 1689 | self->fast_nesting = -1; |
| 1690 | return 0; |
| 1691 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 1692 | if (PyErr_Occurred()) { |
| 1693 | return 0; |
| 1694 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1695 | if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) { |
| 1696 | Py_DECREF(key); |
| 1697 | self->fast_nesting = -1; |
| 1698 | return 0; |
| 1699 | } |
| 1700 | Py_DECREF(key); |
| 1701 | } |
| 1702 | return 1; |
| 1703 | } |
| 1704 | |
| 1705 | static int |
| 1706 | fast_save_leave(PicklerObject *self, PyObject *obj) |
| 1707 | { |
| 1708 | if (self->fast_nesting-- >= FAST_NESTING_LIMIT) { |
| 1709 | PyObject *key = PyLong_FromVoidPtr(obj); |
| 1710 | if (key == NULL) |
| 1711 | return 0; |
| 1712 | if (PyDict_DelItem(self->fast_memo, key) < 0) { |
| 1713 | Py_DECREF(key); |
| 1714 | return 0; |
| 1715 | } |
| 1716 | Py_DECREF(key); |
| 1717 | } |
| 1718 | return 1; |
| 1719 | } |
| 1720 | |
| 1721 | static int |
| 1722 | save_none(PicklerObject *self, PyObject *obj) |
| 1723 | { |
| 1724 | const char none_op = NONE; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1725 | if (_Pickler_Write(self, &none_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1726 | return -1; |
| 1727 | |
| 1728 | return 0; |
| 1729 | } |
| 1730 | |
| 1731 | static int |
| 1732 | save_bool(PicklerObject *self, PyObject *obj) |
| 1733 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1734 | if (self->proto >= 2) { |
Alexandre Vassalotti | 8a67f52 | 2013-11-24 21:40:18 -0800 | [diff] [blame] | 1735 | const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1736 | if (_Pickler_Write(self, &bool_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1737 | return -1; |
| 1738 | } |
Alexandre Vassalotti | 8a67f52 | 2013-11-24 21:40:18 -0800 | [diff] [blame] | 1739 | else { |
| 1740 | /* These aren't opcodes -- they're ways to pickle bools before protocol 2 |
| 1741 | * so that unpicklers written before bools were introduced unpickle them |
| 1742 | * as ints, but unpicklers after can recognize that bools were intended. |
| 1743 | * Note that protocol 2 added direct ways to pickle bools. |
| 1744 | */ |
| 1745 | const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n"; |
| 1746 | if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0) |
| 1747 | return -1; |
| 1748 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1749 | return 0; |
| 1750 | } |
| 1751 | |
| 1752 | static int |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1753 | save_long(PicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1754 | { |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1755 | PyObject *repr = NULL; |
| 1756 | Py_ssize_t size; |
| 1757 | long val; |
| 1758 | int status = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1759 | |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1760 | const char long_op = LONG; |
| 1761 | |
| 1762 | val= PyLong_AsLong(obj); |
| 1763 | if (val == -1 && PyErr_Occurred()) { |
| 1764 | /* out of range for int pickling */ |
| 1765 | PyErr_Clear(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1766 | } |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1767 | else if (self->bin && |
| 1768 | (sizeof(long) <= 4 || |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 1769 | (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 1770 | /* result fits in a signed 4-byte integer. |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 1771 | |
| 1772 | Note: we can't use -0x80000000L in the above condition because some |
| 1773 | compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type |
| 1774 | before applying the unary minus when sizeof(long) <= 4. The |
| 1775 | resulting value stays unsigned which is commonly not what we want, |
| 1776 | so MSVC happily warns us about it. However, that result would have |
| 1777 | been fine because we guard for sizeof(long) <= 4 which turns the |
| 1778 | condition true in that particular case. */ |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1779 | char pdata[32]; |
| 1780 | Py_ssize_t len = 0; |
| 1781 | |
| 1782 | pdata[1] = (unsigned char)(val & 0xff); |
| 1783 | pdata[2] = (unsigned char)((val >> 8) & 0xff); |
| 1784 | pdata[3] = (unsigned char)((val >> 16) & 0xff); |
| 1785 | pdata[4] = (unsigned char)((val >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1786 | |
| 1787 | if ((pdata[4] == 0) && (pdata[3] == 0)) { |
| 1788 | if (pdata[2] == 0) { |
| 1789 | pdata[0] = BININT1; |
| 1790 | len = 2; |
| 1791 | } |
| 1792 | else { |
| 1793 | pdata[0] = BININT2; |
| 1794 | len = 3; |
| 1795 | } |
| 1796 | } |
| 1797 | else { |
| 1798 | pdata[0] = BININT; |
| 1799 | len = 5; |
| 1800 | } |
| 1801 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1802 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1803 | return -1; |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1804 | |
| 1805 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1806 | } |
| 1807 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1808 | if (self->proto >= 2) { |
| 1809 | /* Linear-time pickling. */ |
| 1810 | size_t nbits; |
| 1811 | size_t nbytes; |
| 1812 | unsigned char *pdata; |
| 1813 | char header[5]; |
| 1814 | int i; |
| 1815 | int sign = _PyLong_Sign(obj); |
| 1816 | |
| 1817 | if (sign == 0) { |
| 1818 | header[0] = LONG1; |
| 1819 | header[1] = 0; /* It's 0 -- an empty bytestring. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1820 | if (_Pickler_Write(self, header, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1821 | goto error; |
| 1822 | return 0; |
| 1823 | } |
| 1824 | nbits = _PyLong_NumBits(obj); |
| 1825 | if (nbits == (size_t)-1 && PyErr_Occurred()) |
| 1826 | goto error; |
| 1827 | /* How many bytes do we need? There are nbits >> 3 full |
| 1828 | * bytes of data, and nbits & 7 leftover bits. If there |
| 1829 | * are any leftover bits, then we clearly need another |
| 1830 | * byte. Wnat's not so obvious is that we *probably* |
| 1831 | * need another byte even if there aren't any leftovers: |
| 1832 | * the most-significant bit of the most-significant byte |
| 1833 | * acts like a sign bit, and it's usually got a sense |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1834 | * opposite of the one we need. The exception is ints |
| 1835 | * 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] | 1836 | * its own 256's-complement, so has the right sign bit |
| 1837 | * even without the extra byte. That's a pain to check |
| 1838 | * for in advance, though, so we always grab an extra |
| 1839 | * byte at the start, and cut it back later if possible. |
| 1840 | */ |
| 1841 | nbytes = (nbits >> 3) + 1; |
Antoine Pitrou | bf6ecf9 | 2012-11-24 20:40:21 +0100 | [diff] [blame] | 1842 | if (nbytes > 0x7fffffffL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1843 | PyErr_SetString(PyExc_OverflowError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1844 | "int too large to pickle"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1845 | goto error; |
| 1846 | } |
Neal Norwitz | 6ae2eb2 | 2008-08-24 23:50:08 +0000 | [diff] [blame] | 1847 | repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1848 | if (repr == NULL) |
| 1849 | goto error; |
Neal Norwitz | 6ae2eb2 | 2008-08-24 23:50:08 +0000 | [diff] [blame] | 1850 | pdata = (unsigned char *)PyBytes_AS_STRING(repr); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1851 | i = _PyLong_AsByteArray((PyLongObject *)obj, |
| 1852 | pdata, nbytes, |
| 1853 | 1 /* little endian */ , 1 /* signed */ ); |
| 1854 | if (i < 0) |
| 1855 | goto error; |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1856 | /* If the int is negative, this may be a byte more than |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1857 | * needed. This is so iff the MSB is all redundant sign |
| 1858 | * bits. |
| 1859 | */ |
| 1860 | if (sign < 0 && |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1861 | nbytes > 1 && |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1862 | pdata[nbytes - 1] == 0xff && |
| 1863 | (pdata[nbytes - 2] & 0x80) != 0) { |
| 1864 | nbytes--; |
| 1865 | } |
| 1866 | |
| 1867 | if (nbytes < 256) { |
| 1868 | header[0] = LONG1; |
| 1869 | header[1] = (unsigned char)nbytes; |
| 1870 | size = 2; |
| 1871 | } |
| 1872 | else { |
| 1873 | header[0] = LONG4; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1874 | size = (Py_ssize_t) nbytes; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1875 | for (i = 1; i < 5; i++) { |
| 1876 | header[i] = (unsigned char)(size & 0xff); |
| 1877 | size >>= 8; |
| 1878 | } |
| 1879 | size = 5; |
| 1880 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1881 | if (_Pickler_Write(self, header, size) < 0 || |
| 1882 | _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1883 | goto error; |
| 1884 | } |
| 1885 | else { |
| 1886 | char *string; |
| 1887 | |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 1888 | /* proto < 2: write the repr and newline. This is quadratic-time (in |
| 1889 | the number of digits), in both directions. We add a trailing 'L' |
| 1890 | to the repr, for compatibility with Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1891 | |
| 1892 | repr = PyObject_Repr(obj); |
| 1893 | if (repr == NULL) |
| 1894 | goto error; |
| 1895 | |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1896 | string = _PyUnicode_AsStringAndSize(repr, &size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1897 | if (string == NULL) |
| 1898 | goto error; |
| 1899 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1900 | if (_Pickler_Write(self, &long_op, 1) < 0 || |
| 1901 | _Pickler_Write(self, string, size) < 0 || |
| 1902 | _Pickler_Write(self, "L\n", 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1903 | goto error; |
| 1904 | } |
| 1905 | |
| 1906 | if (0) { |
| 1907 | error: |
| 1908 | status = -1; |
| 1909 | } |
| 1910 | Py_XDECREF(repr); |
| 1911 | |
| 1912 | return status; |
| 1913 | } |
| 1914 | |
| 1915 | static int |
| 1916 | save_float(PicklerObject *self, PyObject *obj) |
| 1917 | { |
| 1918 | double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj); |
| 1919 | |
| 1920 | if (self->bin) { |
| 1921 | char pdata[9]; |
| 1922 | pdata[0] = BINFLOAT; |
| 1923 | if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0) |
| 1924 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1925 | if (_Pickler_Write(self, pdata, 9) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1926 | return -1; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1927 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1928 | else { |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1929 | int result = -1; |
| 1930 | char *buf = NULL; |
| 1931 | char op = FLOAT; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1932 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1933 | if (_Pickler_Write(self, &op, 1) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1934 | goto done; |
| 1935 | |
Mark Dickinson | 3e09f43 | 2009-04-17 08:41:23 +0000 | [diff] [blame] | 1936 | buf = PyOS_double_to_string(x, 'g', 17, 0, NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1937 | if (!buf) { |
| 1938 | PyErr_NoMemory(); |
| 1939 | goto done; |
| 1940 | } |
| 1941 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1942 | if (_Pickler_Write(self, buf, strlen(buf)) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1943 | goto done; |
| 1944 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1945 | if (_Pickler_Write(self, "\n", 1) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1946 | goto done; |
| 1947 | |
| 1948 | result = 0; |
| 1949 | done: |
| 1950 | PyMem_Free(buf); |
| 1951 | return result; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | return 0; |
| 1955 | } |
| 1956 | |
| 1957 | static int |
| 1958 | save_bytes(PicklerObject *self, PyObject *obj) |
| 1959 | { |
| 1960 | if (self->proto < 3) { |
| 1961 | /* Older pickle protocols do not have an opcode for pickling bytes |
| 1962 | objects. Therefore, we need to fake the copy protocol (i.e., |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 1963 | the __reduce__ method) to permit bytes object unpickling. |
| 1964 | |
| 1965 | Here we use a hack to be compatible with Python 2. Since in Python |
| 1966 | 2 'bytes' is just an alias for 'str' (which has different |
| 1967 | parameters than the actual bytes object), we use codecs.encode |
| 1968 | to create the appropriate 'str' object when unpickled using |
| 1969 | Python 2 *and* the appropriate 'bytes' object when unpickled |
| 1970 | using Python 3. Again this is a hack and we don't need to do this |
| 1971 | with newer protocols. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1972 | PyObject *reduce_value = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1973 | int status; |
| 1974 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 1975 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 1976 | reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type); |
| 1977 | } |
| 1978 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1979 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 1980 | PyObject *unicode_str = |
| 1981 | PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj), |
| 1982 | PyBytes_GET_SIZE(obj), |
| 1983 | "strict"); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1984 | _Py_IDENTIFIER(latin1); |
| 1985 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 1986 | if (unicode_str == NULL) |
| 1987 | return -1; |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 1988 | reduce_value = Py_BuildValue("(O(OO))", |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1989 | st->codecs_encode, unicode_str, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1990 | _PyUnicode_FromId(&PyId_latin1)); |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 1991 | Py_DECREF(unicode_str); |
| 1992 | } |
| 1993 | |
| 1994 | if (reduce_value == NULL) |
| 1995 | return -1; |
| 1996 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1997 | /* save_reduce() will memoize the object automatically. */ |
| 1998 | status = save_reduce(self, reduce_value, obj); |
| 1999 | Py_DECREF(reduce_value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2000 | return status; |
| 2001 | } |
| 2002 | else { |
| 2003 | Py_ssize_t size; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2004 | char header[9]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2005 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2006 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2007 | size = PyBytes_GET_SIZE(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2008 | if (size < 0) |
| 2009 | return -1; |
| 2010 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2011 | if (size <= 0xff) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2012 | header[0] = SHORT_BINBYTES; |
| 2013 | header[1] = (unsigned char)size; |
| 2014 | len = 2; |
| 2015 | } |
| 2016 | else if (size <= 0xffffffffL) { |
| 2017 | header[0] = BINBYTES; |
| 2018 | header[1] = (unsigned char)(size & 0xff); |
| 2019 | header[2] = (unsigned char)((size >> 8) & 0xff); |
| 2020 | header[3] = (unsigned char)((size >> 16) & 0xff); |
| 2021 | header[4] = (unsigned char)((size >> 24) & 0xff); |
| 2022 | len = 5; |
| 2023 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2024 | else if (self->proto >= 4) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2025 | header[0] = BINBYTES8; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 2026 | _write_size64(header + 1, size); |
Alexandre Vassalotti | 6e73ff1 | 2013-12-05 19:29:32 -0800 | [diff] [blame] | 2027 | len = 9; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2028 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2029 | else { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2030 | PyErr_SetString(PyExc_OverflowError, |
Serhiy Storchaka | f8def28 | 2013-02-16 17:29:56 +0200 | [diff] [blame] | 2031 | "cannot serialize a bytes object larger than 4 GiB"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2032 | return -1; /* string too large */ |
| 2033 | } |
| 2034 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2035 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2036 | return -1; |
| 2037 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2038 | if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2039 | return -1; |
| 2040 | |
| 2041 | if (memo_put(self, obj) < 0) |
| 2042 | return -1; |
| 2043 | |
| 2044 | return 0; |
| 2045 | } |
| 2046 | } |
| 2047 | |
| 2048 | /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates |
| 2049 | backslash and newline characters to \uXXXX escapes. */ |
| 2050 | static PyObject * |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2051 | raw_unicode_escape(PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2052 | { |
| 2053 | PyObject *repr, *result; |
| 2054 | char *p; |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2055 | Py_ssize_t i, size, expandsize; |
| 2056 | void *data; |
| 2057 | unsigned int kind; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2058 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2059 | if (PyUnicode_READY(obj)) |
| 2060 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2061 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2062 | size = PyUnicode_GET_LENGTH(obj); |
| 2063 | data = PyUnicode_DATA(obj); |
| 2064 | kind = PyUnicode_KIND(obj); |
| 2065 | if (kind == PyUnicode_4BYTE_KIND) |
| 2066 | expandsize = 10; |
| 2067 | else |
| 2068 | expandsize = 6; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 2069 | |
Alexandre Vassalotti | 554d878 | 2008-12-27 07:32:41 +0000 | [diff] [blame] | 2070 | if (size > PY_SSIZE_T_MAX / expandsize) |
| 2071 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 554d878 | 2008-12-27 07:32:41 +0000 | [diff] [blame] | 2072 | repr = PyByteArray_FromStringAndSize(NULL, expandsize * size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2073 | if (repr == NULL) |
| 2074 | return NULL; |
| 2075 | if (size == 0) |
| 2076 | goto done; |
| 2077 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2078 | p = PyByteArray_AS_STRING(repr); |
| 2079 | for (i=0; i < size; i++) { |
| 2080 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2081 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 2082 | if (ch >= 0x10000) { |
| 2083 | *p++ = '\\'; |
| 2084 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 2085 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 2086 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 2087 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 2088 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 2089 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 2090 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 2091 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 2092 | *p++ = Py_hexdigits[ch & 15]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2093 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2094 | /* Map 16-bit characters to '\uxxxx' */ |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2095 | else if (ch >= 256 || ch == '\\' || ch == '\n') { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2096 | *p++ = '\\'; |
| 2097 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 2098 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 2099 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 2100 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 2101 | *p++ = Py_hexdigits[ch & 15]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2102 | } |
Alexandre Vassalotti | 554d878 | 2008-12-27 07:32:41 +0000 | [diff] [blame] | 2103 | /* Copy everything else as-is */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2104 | else |
| 2105 | *p++ = (char) ch; |
| 2106 | } |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2107 | size = p - PyByteArray_AS_STRING(repr); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2108 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2109 | done: |
Alexandre Vassalotti | 554d878 | 2008-12-27 07:32:41 +0000 | [diff] [blame] | 2110 | result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(repr), size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2111 | Py_DECREF(repr); |
| 2112 | return result; |
| 2113 | } |
| 2114 | |
| 2115 | static int |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2116 | write_utf8(PicklerObject *self, char *data, Py_ssize_t size) |
| 2117 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2118 | char header[9]; |
| 2119 | Py_ssize_t len; |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2120 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2121 | if (size <= 0xff && self->proto >= 4) { |
| 2122 | header[0] = SHORT_BINUNICODE; |
| 2123 | header[1] = (unsigned char)(size & 0xff); |
| 2124 | len = 2; |
| 2125 | } |
| 2126 | else if (size <= 0xffffffffUL) { |
| 2127 | header[0] = BINUNICODE; |
| 2128 | header[1] = (unsigned char)(size & 0xff); |
| 2129 | header[2] = (unsigned char)((size >> 8) & 0xff); |
| 2130 | header[3] = (unsigned char)((size >> 16) & 0xff); |
| 2131 | header[4] = (unsigned char)((size >> 24) & 0xff); |
| 2132 | len = 5; |
| 2133 | } |
| 2134 | else if (self->proto >= 4) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2135 | header[0] = BINUNICODE8; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 2136 | _write_size64(header + 1, size); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2137 | len = 9; |
| 2138 | } |
| 2139 | else { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2140 | PyErr_SetString(PyExc_OverflowError, |
Antoine Pitrou | 4b7b0f0 | 2013-04-07 23:46:52 +0200 | [diff] [blame] | 2141 | "cannot serialize a string larger than 4GiB"); |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2142 | return -1; |
| 2143 | } |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2144 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2145 | if (_Pickler_Write(self, header, len) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2146 | return -1; |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2147 | if (_Pickler_Write(self, data, size) < 0) |
| 2148 | return -1; |
| 2149 | |
| 2150 | return 0; |
| 2151 | } |
| 2152 | |
| 2153 | static int |
| 2154 | write_unicode_binary(PicklerObject *self, PyObject *obj) |
| 2155 | { |
| 2156 | PyObject *encoded = NULL; |
| 2157 | Py_ssize_t size; |
| 2158 | char *data; |
| 2159 | int r; |
| 2160 | |
| 2161 | if (PyUnicode_READY(obj)) |
| 2162 | return -1; |
| 2163 | |
| 2164 | data = PyUnicode_AsUTF8AndSize(obj, &size); |
| 2165 | if (data != NULL) |
| 2166 | return write_utf8(self, data, size); |
| 2167 | |
| 2168 | /* Issue #8383: for strings with lone surrogates, fallback on the |
| 2169 | "surrogatepass" error handler. */ |
| 2170 | PyErr_Clear(); |
| 2171 | encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass"); |
| 2172 | if (encoded == NULL) |
| 2173 | return -1; |
| 2174 | |
| 2175 | r = write_utf8(self, PyBytes_AS_STRING(encoded), |
| 2176 | PyBytes_GET_SIZE(encoded)); |
| 2177 | Py_DECREF(encoded); |
| 2178 | return r; |
| 2179 | } |
| 2180 | |
| 2181 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2182 | save_unicode(PicklerObject *self, PyObject *obj) |
| 2183 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2184 | if (self->bin) { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2185 | if (write_unicode_binary(self, obj) < 0) |
| 2186 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2187 | } |
| 2188 | else { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2189 | PyObject *encoded; |
| 2190 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2191 | const char unicode_op = UNICODE; |
| 2192 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2193 | encoded = raw_unicode_escape(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2194 | if (encoded == NULL) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2195 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2196 | |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2197 | if (_Pickler_Write(self, &unicode_op, 1) < 0) { |
| 2198 | Py_DECREF(encoded); |
| 2199 | return -1; |
| 2200 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2201 | |
| 2202 | size = PyBytes_GET_SIZE(encoded); |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2203 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) { |
| 2204 | Py_DECREF(encoded); |
| 2205 | return -1; |
| 2206 | } |
| 2207 | Py_DECREF(encoded); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2208 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2209 | if (_Pickler_Write(self, "\n", 1) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2210 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2211 | } |
| 2212 | if (memo_put(self, obj) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2213 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2214 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2215 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2216 | } |
| 2217 | |
| 2218 | /* A helper for save_tuple. Push the len elements in tuple t on the stack. */ |
| 2219 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2220 | store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2221 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2222 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2223 | |
| 2224 | assert(PyTuple_Size(t) == len); |
| 2225 | |
| 2226 | for (i = 0; i < len; i++) { |
| 2227 | PyObject *element = PyTuple_GET_ITEM(t, i); |
| 2228 | |
| 2229 | if (element == NULL) |
| 2230 | return -1; |
| 2231 | if (save(self, element, 0) < 0) |
| 2232 | return -1; |
| 2233 | } |
| 2234 | |
| 2235 | return 0; |
| 2236 | } |
| 2237 | |
| 2238 | /* Tuples are ubiquitous in the pickle protocols, so many techniques are |
| 2239 | * used across protocols to minimize the space needed to pickle them. |
| 2240 | * Tuples are also the only builtin immutable type that can be recursive |
| 2241 | * (a tuple can be reached from itself), and that requires some subtle |
| 2242 | * magic so that it works in all cases. IOW, this is a long routine. |
| 2243 | */ |
| 2244 | static int |
| 2245 | save_tuple(PicklerObject *self, PyObject *obj) |
| 2246 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2247 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2248 | |
| 2249 | const char mark_op = MARK; |
| 2250 | const char tuple_op = TUPLE; |
| 2251 | const char pop_op = POP; |
| 2252 | const char pop_mark_op = POP_MARK; |
| 2253 | const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3}; |
| 2254 | |
| 2255 | if ((len = PyTuple_Size(obj)) < 0) |
| 2256 | return -1; |
| 2257 | |
| 2258 | if (len == 0) { |
| 2259 | char pdata[2]; |
| 2260 | |
| 2261 | if (self->proto) { |
| 2262 | pdata[0] = EMPTY_TUPLE; |
| 2263 | len = 1; |
| 2264 | } |
| 2265 | else { |
| 2266 | pdata[0] = MARK; |
| 2267 | pdata[1] = TUPLE; |
| 2268 | len = 2; |
| 2269 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2270 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2271 | return -1; |
| 2272 | return 0; |
| 2273 | } |
| 2274 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2275 | /* 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] | 2276 | * saving the tuple elements, the tuple must be recursive, in |
| 2277 | * which case we'll pop everything we put on the stack, and fetch |
| 2278 | * its value from the memo. |
| 2279 | */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2280 | if (len <= 3 && self->proto >= 2) { |
| 2281 | /* Use TUPLE{1,2,3} opcodes. */ |
| 2282 | if (store_tuple_elements(self, obj, len) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2283 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2284 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2285 | if (PyMemoTable_Get(self->memo, obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2286 | /* pop the len elements */ |
| 2287 | for (i = 0; i < len; i++) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2288 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 2289 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2290 | /* fetch from memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2291 | if (memo_get(self, obj) < 0) |
| 2292 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2293 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2294 | return 0; |
| 2295 | } |
| 2296 | else { /* Not recursive. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2297 | if (_Pickler_Write(self, len2opcode + len, 1) < 0) |
| 2298 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2299 | } |
| 2300 | goto memoize; |
| 2301 | } |
| 2302 | |
| 2303 | /* proto < 2 and len > 0, or proto >= 2 and len > 3. |
| 2304 | * Generate MARK e1 e2 ... TUPLE |
| 2305 | */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2306 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2307 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2308 | |
| 2309 | if (store_tuple_elements(self, obj, len) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2310 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2311 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2312 | if (PyMemoTable_Get(self->memo, obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2313 | /* pop the stack stuff we pushed */ |
| 2314 | if (self->bin) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2315 | if (_Pickler_Write(self, &pop_mark_op, 1) < 0) |
| 2316 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2317 | } |
| 2318 | else { |
| 2319 | /* Note that we pop one more than len, to remove |
| 2320 | * the MARK too. |
| 2321 | */ |
| 2322 | for (i = 0; i <= len; i++) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2323 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 2324 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2325 | } |
| 2326 | /* fetch from memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2327 | if (memo_get(self, obj) < 0) |
| 2328 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2329 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2330 | return 0; |
| 2331 | } |
| 2332 | else { /* Not recursive. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2333 | if (_Pickler_Write(self, &tuple_op, 1) < 0) |
| 2334 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2335 | } |
| 2336 | |
| 2337 | memoize: |
| 2338 | if (memo_put(self, obj) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2339 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2340 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2341 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2342 | } |
| 2343 | |
| 2344 | /* iter is an iterator giving items, and we batch up chunks of |
| 2345 | * MARK item item ... item APPENDS |
| 2346 | * opcode sequences. Calling code should have arranged to first create an |
| 2347 | * empty list, or list-like object, for the APPENDS to operate on. |
| 2348 | * Returns 0 on success, <0 on error. |
| 2349 | */ |
| 2350 | static int |
| 2351 | batch_list(PicklerObject *self, PyObject *iter) |
| 2352 | { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2353 | PyObject *obj = NULL; |
| 2354 | PyObject *firstitem = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2355 | int i, n; |
| 2356 | |
| 2357 | const char mark_op = MARK; |
| 2358 | const char append_op = APPEND; |
| 2359 | const char appends_op = APPENDS; |
| 2360 | |
| 2361 | assert(iter != NULL); |
| 2362 | |
| 2363 | /* XXX: I think this function could be made faster by avoiding the |
| 2364 | iterator interface and fetching objects directly from list using |
| 2365 | PyList_GET_ITEM. |
| 2366 | */ |
| 2367 | |
| 2368 | if (self->proto == 0) { |
| 2369 | /* APPENDS isn't available; do one at a time. */ |
| 2370 | for (;;) { |
| 2371 | obj = PyIter_Next(iter); |
| 2372 | if (obj == NULL) { |
| 2373 | if (PyErr_Occurred()) |
| 2374 | return -1; |
| 2375 | break; |
| 2376 | } |
| 2377 | i = save(self, obj, 0); |
| 2378 | Py_DECREF(obj); |
| 2379 | if (i < 0) |
| 2380 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2381 | if (_Pickler_Write(self, &append_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2382 | return -1; |
| 2383 | } |
| 2384 | return 0; |
| 2385 | } |
| 2386 | |
| 2387 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 2388 | do { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2389 | /* Get first item */ |
| 2390 | firstitem = PyIter_Next(iter); |
| 2391 | if (firstitem == NULL) { |
| 2392 | if (PyErr_Occurred()) |
| 2393 | goto error; |
| 2394 | |
| 2395 | /* nothing more to add */ |
| 2396 | break; |
| 2397 | } |
| 2398 | |
| 2399 | /* Try to get a second item */ |
| 2400 | obj = PyIter_Next(iter); |
| 2401 | if (obj == NULL) { |
| 2402 | if (PyErr_Occurred()) |
| 2403 | goto error; |
| 2404 | |
| 2405 | /* Only one item to write */ |
| 2406 | if (save(self, firstitem, 0) < 0) |
| 2407 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2408 | if (_Pickler_Write(self, &append_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2409 | goto error; |
| 2410 | Py_CLEAR(firstitem); |
| 2411 | break; |
| 2412 | } |
| 2413 | |
| 2414 | /* More than one item to write */ |
| 2415 | |
| 2416 | /* Pump out MARK, items, APPENDS. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2417 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2418 | goto error; |
| 2419 | |
| 2420 | if (save(self, firstitem, 0) < 0) |
| 2421 | goto error; |
| 2422 | Py_CLEAR(firstitem); |
| 2423 | n = 1; |
| 2424 | |
| 2425 | /* Fetch and save up to BATCHSIZE items */ |
| 2426 | while (obj) { |
| 2427 | if (save(self, obj, 0) < 0) |
| 2428 | goto error; |
| 2429 | Py_CLEAR(obj); |
| 2430 | n += 1; |
| 2431 | |
| 2432 | if (n == BATCHSIZE) |
| 2433 | break; |
| 2434 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2435 | obj = PyIter_Next(iter); |
| 2436 | if (obj == NULL) { |
| 2437 | if (PyErr_Occurred()) |
| 2438 | goto error; |
| 2439 | break; |
| 2440 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2441 | } |
| 2442 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2443 | if (_Pickler_Write(self, &appends_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2444 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2445 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2446 | } while (n == BATCHSIZE); |
| 2447 | return 0; |
| 2448 | |
| 2449 | error: |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2450 | Py_XDECREF(firstitem); |
| 2451 | Py_XDECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2452 | return -1; |
| 2453 | } |
| 2454 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2455 | /* This is a variant of batch_list() above, specialized for lists (with no |
| 2456 | * support for list subclasses). Like batch_list(), we batch up chunks of |
| 2457 | * MARK item item ... item APPENDS |
| 2458 | * opcode sequences. Calling code should have arranged to first create an |
| 2459 | * empty list, or list-like object, for the APPENDS to operate on. |
| 2460 | * Returns 0 on success, -1 on error. |
| 2461 | * |
| 2462 | * This version is considerably faster than batch_list(), if less general. |
| 2463 | * |
| 2464 | * Note that this only works for protocols > 0. |
| 2465 | */ |
| 2466 | static int |
| 2467 | batch_list_exact(PicklerObject *self, PyObject *obj) |
| 2468 | { |
| 2469 | PyObject *item = NULL; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2470 | Py_ssize_t this_batch, total; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2471 | |
| 2472 | const char append_op = APPEND; |
| 2473 | const char appends_op = APPENDS; |
| 2474 | const char mark_op = MARK; |
| 2475 | |
| 2476 | assert(obj != NULL); |
| 2477 | assert(self->proto > 0); |
| 2478 | assert(PyList_CheckExact(obj)); |
| 2479 | |
| 2480 | if (PyList_GET_SIZE(obj) == 1) { |
| 2481 | item = PyList_GET_ITEM(obj, 0); |
| 2482 | if (save(self, item, 0) < 0) |
| 2483 | return -1; |
| 2484 | if (_Pickler_Write(self, &append_op, 1) < 0) |
| 2485 | return -1; |
| 2486 | return 0; |
| 2487 | } |
| 2488 | |
| 2489 | /* Write in batches of BATCHSIZE. */ |
| 2490 | total = 0; |
| 2491 | do { |
| 2492 | this_batch = 0; |
| 2493 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2494 | return -1; |
| 2495 | while (total < PyList_GET_SIZE(obj)) { |
| 2496 | item = PyList_GET_ITEM(obj, total); |
| 2497 | if (save(self, item, 0) < 0) |
| 2498 | return -1; |
| 2499 | total++; |
| 2500 | if (++this_batch == BATCHSIZE) |
| 2501 | break; |
| 2502 | } |
| 2503 | if (_Pickler_Write(self, &appends_op, 1) < 0) |
| 2504 | return -1; |
| 2505 | |
| 2506 | } while (total < PyList_GET_SIZE(obj)); |
| 2507 | |
| 2508 | return 0; |
| 2509 | } |
| 2510 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2511 | static int |
| 2512 | save_list(PicklerObject *self, PyObject *obj) |
| 2513 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2514 | char header[3]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2515 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2516 | int status = 0; |
| 2517 | |
| 2518 | if (self->fast && !fast_save_enter(self, obj)) |
| 2519 | goto error; |
| 2520 | |
| 2521 | /* Create an empty list. */ |
| 2522 | if (self->bin) { |
| 2523 | header[0] = EMPTY_LIST; |
| 2524 | len = 1; |
| 2525 | } |
| 2526 | else { |
| 2527 | header[0] = MARK; |
| 2528 | header[1] = LIST; |
| 2529 | len = 2; |
| 2530 | } |
| 2531 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2532 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2533 | goto error; |
| 2534 | |
| 2535 | /* Get list length, and bow out early if empty. */ |
| 2536 | if ((len = PyList_Size(obj)) < 0) |
| 2537 | goto error; |
| 2538 | |
| 2539 | if (memo_put(self, obj) < 0) |
| 2540 | goto error; |
| 2541 | |
| 2542 | if (len != 0) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2543 | /* Materialize the list elements. */ |
| 2544 | if (PyList_CheckExact(obj) && self->proto > 0) { |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2545 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 2546 | goto error; |
| 2547 | status = batch_list_exact(self, obj); |
| 2548 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2549 | } else { |
| 2550 | PyObject *iter = PyObject_GetIter(obj); |
| 2551 | if (iter == NULL) |
| 2552 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2553 | |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2554 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 2555 | Py_DECREF(iter); |
| 2556 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2557 | } |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2558 | status = batch_list(self, iter); |
| 2559 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2560 | Py_DECREF(iter); |
| 2561 | } |
| 2562 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2563 | if (0) { |
| 2564 | error: |
| 2565 | status = -1; |
| 2566 | } |
| 2567 | |
| 2568 | if (self->fast && !fast_save_leave(self, obj)) |
| 2569 | status = -1; |
| 2570 | |
| 2571 | return status; |
| 2572 | } |
| 2573 | |
| 2574 | /* iter is an iterator giving (key, value) pairs, and we batch up chunks of |
| 2575 | * MARK key value ... key value SETITEMS |
| 2576 | * opcode sequences. Calling code should have arranged to first create an |
| 2577 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 2578 | * Returns 0 on success, <0 on error. |
| 2579 | * |
| 2580 | * This is very much like batch_list(). The difference between saving |
| 2581 | * elements directly, and picking apart two-tuples, is so long-winded at |
| 2582 | * the C level, though, that attempts to combine these routines were too |
| 2583 | * ugly to bear. |
| 2584 | */ |
| 2585 | static int |
| 2586 | batch_dict(PicklerObject *self, PyObject *iter) |
| 2587 | { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2588 | PyObject *obj = NULL; |
| 2589 | PyObject *firstitem = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2590 | int i, n; |
| 2591 | |
| 2592 | const char mark_op = MARK; |
| 2593 | const char setitem_op = SETITEM; |
| 2594 | const char setitems_op = SETITEMS; |
| 2595 | |
| 2596 | assert(iter != NULL); |
| 2597 | |
| 2598 | if (self->proto == 0) { |
| 2599 | /* SETITEMS isn't available; do one at a time. */ |
| 2600 | for (;;) { |
| 2601 | obj = PyIter_Next(iter); |
| 2602 | if (obj == NULL) { |
| 2603 | if (PyErr_Occurred()) |
| 2604 | return -1; |
| 2605 | break; |
| 2606 | } |
| 2607 | if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) { |
| 2608 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2609 | "iterator must return 2-tuples"); |
| 2610 | return -1; |
| 2611 | } |
| 2612 | i = save(self, PyTuple_GET_ITEM(obj, 0), 0); |
| 2613 | if (i >= 0) |
| 2614 | i = save(self, PyTuple_GET_ITEM(obj, 1), 0); |
| 2615 | Py_DECREF(obj); |
| 2616 | if (i < 0) |
| 2617 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2618 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2619 | return -1; |
| 2620 | } |
| 2621 | return 0; |
| 2622 | } |
| 2623 | |
| 2624 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 2625 | do { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2626 | /* Get first item */ |
| 2627 | firstitem = PyIter_Next(iter); |
| 2628 | if (firstitem == NULL) { |
| 2629 | if (PyErr_Occurred()) |
| 2630 | goto error; |
| 2631 | |
| 2632 | /* nothing more to add */ |
| 2633 | break; |
| 2634 | } |
| 2635 | if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) { |
| 2636 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2637 | "iterator must return 2-tuples"); |
| 2638 | goto error; |
| 2639 | } |
| 2640 | |
| 2641 | /* Try to get a second item */ |
| 2642 | obj = PyIter_Next(iter); |
| 2643 | if (obj == NULL) { |
| 2644 | if (PyErr_Occurred()) |
| 2645 | goto error; |
| 2646 | |
| 2647 | /* Only one item to write */ |
| 2648 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 2649 | goto error; |
| 2650 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 2651 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2652 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2653 | goto error; |
| 2654 | Py_CLEAR(firstitem); |
| 2655 | break; |
| 2656 | } |
| 2657 | |
| 2658 | /* More than one item to write */ |
| 2659 | |
| 2660 | /* Pump out MARK, items, SETITEMS. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2661 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2662 | goto error; |
| 2663 | |
| 2664 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 2665 | goto error; |
| 2666 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 2667 | goto error; |
| 2668 | Py_CLEAR(firstitem); |
| 2669 | n = 1; |
| 2670 | |
| 2671 | /* Fetch and save up to BATCHSIZE items */ |
| 2672 | while (obj) { |
| 2673 | if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) { |
| 2674 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2675 | "iterator must return 2-tuples"); |
| 2676 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2677 | } |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2678 | if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 || |
| 2679 | save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0) |
| 2680 | goto error; |
| 2681 | Py_CLEAR(obj); |
| 2682 | n += 1; |
| 2683 | |
| 2684 | if (n == BATCHSIZE) |
| 2685 | break; |
| 2686 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2687 | obj = PyIter_Next(iter); |
| 2688 | if (obj == NULL) { |
| 2689 | if (PyErr_Occurred()) |
| 2690 | goto error; |
| 2691 | break; |
| 2692 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2693 | } |
| 2694 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2695 | if (_Pickler_Write(self, &setitems_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2696 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2697 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2698 | } while (n == BATCHSIZE); |
| 2699 | return 0; |
| 2700 | |
| 2701 | error: |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2702 | Py_XDECREF(firstitem); |
| 2703 | Py_XDECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2704 | return -1; |
| 2705 | } |
| 2706 | |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2707 | /* This is a variant of batch_dict() above that specializes for dicts, with no |
| 2708 | * support for dict subclasses. Like batch_dict(), we batch up chunks of |
| 2709 | * MARK key value ... key value SETITEMS |
| 2710 | * opcode sequences. Calling code should have arranged to first create an |
| 2711 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 2712 | * Returns 0 on success, -1 on error. |
| 2713 | * |
| 2714 | * Note that this currently doesn't work for protocol 0. |
| 2715 | */ |
| 2716 | static int |
| 2717 | batch_dict_exact(PicklerObject *self, PyObject *obj) |
| 2718 | { |
| 2719 | PyObject *key = NULL, *value = NULL; |
| 2720 | int i; |
| 2721 | Py_ssize_t dict_size, ppos = 0; |
| 2722 | |
Alexandre Vassalotti | f70b129 | 2009-05-25 18:00:52 +0000 | [diff] [blame] | 2723 | const char mark_op = MARK; |
| 2724 | const char setitem_op = SETITEM; |
| 2725 | const char setitems_op = SETITEMS; |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2726 | |
| 2727 | assert(obj != NULL); |
| 2728 | assert(self->proto > 0); |
| 2729 | |
| 2730 | dict_size = PyDict_Size(obj); |
| 2731 | |
| 2732 | /* Special-case len(d) == 1 to save space. */ |
| 2733 | if (dict_size == 1) { |
| 2734 | PyDict_Next(obj, &ppos, &key, &value); |
| 2735 | if (save(self, key, 0) < 0) |
| 2736 | return -1; |
| 2737 | if (save(self, value, 0) < 0) |
| 2738 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2739 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2740 | return -1; |
| 2741 | return 0; |
| 2742 | } |
| 2743 | |
| 2744 | /* Write in batches of BATCHSIZE. */ |
| 2745 | do { |
| 2746 | i = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2747 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2748 | return -1; |
| 2749 | while (PyDict_Next(obj, &ppos, &key, &value)) { |
| 2750 | if (save(self, key, 0) < 0) |
| 2751 | return -1; |
| 2752 | if (save(self, value, 0) < 0) |
| 2753 | return -1; |
| 2754 | if (++i == BATCHSIZE) |
| 2755 | break; |
| 2756 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2757 | if (_Pickler_Write(self, &setitems_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2758 | return -1; |
| 2759 | if (PyDict_Size(obj) != dict_size) { |
| 2760 | PyErr_Format( |
| 2761 | PyExc_RuntimeError, |
| 2762 | "dictionary changed size during iteration"); |
| 2763 | return -1; |
| 2764 | } |
| 2765 | |
| 2766 | } while (i == BATCHSIZE); |
| 2767 | return 0; |
| 2768 | } |
| 2769 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2770 | static int |
| 2771 | save_dict(PicklerObject *self, PyObject *obj) |
| 2772 | { |
| 2773 | PyObject *items, *iter; |
| 2774 | char header[3]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2775 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2776 | int status = 0; |
| 2777 | |
| 2778 | if (self->fast && !fast_save_enter(self, obj)) |
| 2779 | goto error; |
| 2780 | |
| 2781 | /* Create an empty dict. */ |
| 2782 | if (self->bin) { |
| 2783 | header[0] = EMPTY_DICT; |
| 2784 | len = 1; |
| 2785 | } |
| 2786 | else { |
| 2787 | header[0] = MARK; |
| 2788 | header[1] = DICT; |
| 2789 | len = 2; |
| 2790 | } |
| 2791 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2792 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2793 | goto error; |
| 2794 | |
| 2795 | /* Get dict size, and bow out early if empty. */ |
| 2796 | if ((len = PyDict_Size(obj)) < 0) |
| 2797 | goto error; |
| 2798 | |
| 2799 | if (memo_put(self, obj) < 0) |
| 2800 | goto error; |
| 2801 | |
| 2802 | if (len != 0) { |
| 2803 | /* Save the dict items. */ |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2804 | if (PyDict_CheckExact(obj) && self->proto > 0) { |
| 2805 | /* We can take certain shortcuts if we know this is a dict and |
| 2806 | not a dict subclass. */ |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2807 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 2808 | goto error; |
| 2809 | status = batch_dict_exact(self, obj); |
| 2810 | Py_LeaveRecursiveCall(); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2811 | } else { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2812 | _Py_IDENTIFIER(items); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2813 | |
| 2814 | items = _PyObject_CallMethodId(obj, &PyId_items, "()"); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2815 | if (items == NULL) |
| 2816 | goto error; |
| 2817 | iter = PyObject_GetIter(items); |
| 2818 | Py_DECREF(items); |
| 2819 | if (iter == NULL) |
| 2820 | goto error; |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2821 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 2822 | Py_DECREF(iter); |
| 2823 | goto error; |
| 2824 | } |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2825 | status = batch_dict(self, iter); |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2826 | Py_LeaveRecursiveCall(); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2827 | Py_DECREF(iter); |
| 2828 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2829 | } |
| 2830 | |
| 2831 | if (0) { |
| 2832 | error: |
| 2833 | status = -1; |
| 2834 | } |
| 2835 | |
| 2836 | if (self->fast && !fast_save_leave(self, obj)) |
| 2837 | status = -1; |
| 2838 | |
| 2839 | return status; |
| 2840 | } |
| 2841 | |
| 2842 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2843 | save_set(PicklerObject *self, PyObject *obj) |
| 2844 | { |
| 2845 | PyObject *item; |
| 2846 | int i; |
| 2847 | Py_ssize_t set_size, ppos = 0; |
| 2848 | Py_hash_t hash; |
| 2849 | |
| 2850 | const char empty_set_op = EMPTY_SET; |
| 2851 | const char mark_op = MARK; |
| 2852 | const char additems_op = ADDITEMS; |
| 2853 | |
| 2854 | if (self->proto < 4) { |
| 2855 | PyObject *items; |
| 2856 | PyObject *reduce_value; |
| 2857 | int status; |
| 2858 | |
| 2859 | items = PySequence_List(obj); |
| 2860 | if (items == NULL) { |
| 2861 | return -1; |
| 2862 | } |
| 2863 | reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items); |
| 2864 | Py_DECREF(items); |
| 2865 | if (reduce_value == NULL) { |
| 2866 | return -1; |
| 2867 | } |
| 2868 | /* save_reduce() will memoize the object automatically. */ |
| 2869 | status = save_reduce(self, reduce_value, obj); |
| 2870 | Py_DECREF(reduce_value); |
| 2871 | return status; |
| 2872 | } |
| 2873 | |
| 2874 | if (_Pickler_Write(self, &empty_set_op, 1) < 0) |
| 2875 | return -1; |
| 2876 | |
| 2877 | if (memo_put(self, obj) < 0) |
| 2878 | return -1; |
| 2879 | |
| 2880 | set_size = PySet_GET_SIZE(obj); |
| 2881 | if (set_size == 0) |
| 2882 | return 0; /* nothing to do */ |
| 2883 | |
| 2884 | /* Write in batches of BATCHSIZE. */ |
| 2885 | do { |
| 2886 | i = 0; |
| 2887 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2888 | return -1; |
| 2889 | while (_PySet_NextEntry(obj, &ppos, &item, &hash)) { |
| 2890 | if (save(self, item, 0) < 0) |
| 2891 | return -1; |
| 2892 | if (++i == BATCHSIZE) |
| 2893 | break; |
| 2894 | } |
| 2895 | if (_Pickler_Write(self, &additems_op, 1) < 0) |
| 2896 | return -1; |
| 2897 | if (PySet_GET_SIZE(obj) != set_size) { |
| 2898 | PyErr_Format( |
| 2899 | PyExc_RuntimeError, |
| 2900 | "set changed size during iteration"); |
| 2901 | return -1; |
| 2902 | } |
| 2903 | } while (i == BATCHSIZE); |
| 2904 | |
| 2905 | return 0; |
| 2906 | } |
| 2907 | |
| 2908 | static int |
| 2909 | save_frozenset(PicklerObject *self, PyObject *obj) |
| 2910 | { |
| 2911 | PyObject *iter; |
| 2912 | |
| 2913 | const char mark_op = MARK; |
| 2914 | const char frozenset_op = FROZENSET; |
| 2915 | |
| 2916 | if (self->fast && !fast_save_enter(self, obj)) |
| 2917 | return -1; |
| 2918 | |
| 2919 | if (self->proto < 4) { |
| 2920 | PyObject *items; |
| 2921 | PyObject *reduce_value; |
| 2922 | int status; |
| 2923 | |
| 2924 | items = PySequence_List(obj); |
| 2925 | if (items == NULL) { |
| 2926 | return -1; |
| 2927 | } |
| 2928 | reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type, |
| 2929 | items); |
| 2930 | Py_DECREF(items); |
| 2931 | if (reduce_value == NULL) { |
| 2932 | return -1; |
| 2933 | } |
| 2934 | /* save_reduce() will memoize the object automatically. */ |
| 2935 | status = save_reduce(self, reduce_value, obj); |
| 2936 | Py_DECREF(reduce_value); |
| 2937 | return status; |
| 2938 | } |
| 2939 | |
| 2940 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2941 | return -1; |
| 2942 | |
| 2943 | iter = PyObject_GetIter(obj); |
Christian Heimes | b3d3ee4 | 2013-11-23 21:01:40 +0100 | [diff] [blame] | 2944 | if (iter == NULL) { |
Christian Heimes | 74d8d63 | 2013-11-23 21:05:31 +0100 | [diff] [blame] | 2945 | return -1; |
Christian Heimes | b3d3ee4 | 2013-11-23 21:01:40 +0100 | [diff] [blame] | 2946 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2947 | for (;;) { |
| 2948 | PyObject *item; |
| 2949 | |
| 2950 | item = PyIter_Next(iter); |
| 2951 | if (item == NULL) { |
| 2952 | if (PyErr_Occurred()) { |
| 2953 | Py_DECREF(iter); |
| 2954 | return -1; |
| 2955 | } |
| 2956 | break; |
| 2957 | } |
| 2958 | if (save(self, item, 0) < 0) { |
| 2959 | Py_DECREF(item); |
| 2960 | Py_DECREF(iter); |
| 2961 | return -1; |
| 2962 | } |
| 2963 | Py_DECREF(item); |
| 2964 | } |
| 2965 | Py_DECREF(iter); |
| 2966 | |
| 2967 | /* If the object is already in the memo, this means it is |
| 2968 | recursive. In this case, throw away everything we put on the |
| 2969 | stack, and fetch the object back from the memo. */ |
| 2970 | if (PyMemoTable_Get(self->memo, obj)) { |
| 2971 | const char pop_mark_op = POP_MARK; |
| 2972 | |
| 2973 | if (_Pickler_Write(self, &pop_mark_op, 1) < 0) |
| 2974 | return -1; |
| 2975 | if (memo_get(self, obj) < 0) |
| 2976 | return -1; |
| 2977 | return 0; |
| 2978 | } |
| 2979 | |
| 2980 | if (_Pickler_Write(self, &frozenset_op, 1) < 0) |
| 2981 | return -1; |
| 2982 | if (memo_put(self, obj) < 0) |
| 2983 | return -1; |
| 2984 | |
| 2985 | return 0; |
| 2986 | } |
| 2987 | |
| 2988 | static int |
| 2989 | fix_imports(PyObject **module_name, PyObject **global_name) |
| 2990 | { |
| 2991 | PyObject *key; |
| 2992 | PyObject *item; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 2993 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2994 | |
| 2995 | key = PyTuple_Pack(2, *module_name, *global_name); |
| 2996 | if (key == NULL) |
| 2997 | return -1; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 2998 | item = PyDict_GetItemWithError(st->name_mapping_3to2, key); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2999 | Py_DECREF(key); |
| 3000 | if (item) { |
| 3001 | PyObject *fixed_module_name; |
| 3002 | PyObject *fixed_global_name; |
| 3003 | |
| 3004 | if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
| 3005 | PyErr_Format(PyExc_RuntimeError, |
| 3006 | "_compat_pickle.REVERSE_NAME_MAPPING values " |
| 3007 | "should be 2-tuples, not %.200s", |
| 3008 | Py_TYPE(item)->tp_name); |
| 3009 | return -1; |
| 3010 | } |
| 3011 | fixed_module_name = PyTuple_GET_ITEM(item, 0); |
| 3012 | fixed_global_name = PyTuple_GET_ITEM(item, 1); |
| 3013 | if (!PyUnicode_Check(fixed_module_name) || |
| 3014 | !PyUnicode_Check(fixed_global_name)) { |
| 3015 | PyErr_Format(PyExc_RuntimeError, |
| 3016 | "_compat_pickle.REVERSE_NAME_MAPPING values " |
| 3017 | "should be pairs of str, not (%.200s, %.200s)", |
| 3018 | Py_TYPE(fixed_module_name)->tp_name, |
| 3019 | Py_TYPE(fixed_global_name)->tp_name); |
| 3020 | return -1; |
| 3021 | } |
| 3022 | |
| 3023 | Py_CLEAR(*module_name); |
| 3024 | Py_CLEAR(*global_name); |
| 3025 | Py_INCREF(fixed_module_name); |
| 3026 | Py_INCREF(fixed_global_name); |
| 3027 | *module_name = fixed_module_name; |
| 3028 | *global_name = fixed_global_name; |
| 3029 | } |
| 3030 | else if (PyErr_Occurred()) { |
| 3031 | return -1; |
| 3032 | } |
| 3033 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3034 | item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3035 | if (item) { |
| 3036 | if (!PyUnicode_Check(item)) { |
| 3037 | PyErr_Format(PyExc_RuntimeError, |
| 3038 | "_compat_pickle.REVERSE_IMPORT_MAPPING values " |
| 3039 | "should be strings, not %.200s", |
| 3040 | Py_TYPE(item)->tp_name); |
| 3041 | return -1; |
| 3042 | } |
| 3043 | Py_CLEAR(*module_name); |
| 3044 | Py_INCREF(item); |
| 3045 | *module_name = item; |
| 3046 | } |
| 3047 | else if (PyErr_Occurred()) { |
| 3048 | return -1; |
| 3049 | } |
| 3050 | |
| 3051 | return 0; |
| 3052 | } |
| 3053 | |
| 3054 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3055 | save_global(PicklerObject *self, PyObject *obj, PyObject *name) |
| 3056 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3057 | PyObject *global_name = NULL; |
| 3058 | PyObject *module_name = NULL; |
| 3059 | PyObject *module = NULL; |
| 3060 | PyObject *cls; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3061 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3062 | int status = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3063 | _Py_IDENTIFIER(__name__); |
| 3064 | _Py_IDENTIFIER(__qualname__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3065 | |
| 3066 | const char global_op = GLOBAL; |
| 3067 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3068 | if (name) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3069 | Py_INCREF(name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3070 | global_name = name; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3071 | } |
| 3072 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3073 | if (self->proto >= 4) { |
| 3074 | global_name = _PyObject_GetAttrId(obj, &PyId___qualname__); |
| 3075 | if (global_name == NULL) { |
| 3076 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 3077 | goto error; |
| 3078 | PyErr_Clear(); |
| 3079 | } |
| 3080 | } |
| 3081 | if (global_name == NULL) { |
| 3082 | global_name = _PyObject_GetAttrId(obj, &PyId___name__); |
| 3083 | if (global_name == NULL) |
| 3084 | goto error; |
| 3085 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3086 | } |
| 3087 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3088 | module_name = whichmodule(obj, global_name, self->proto >= 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3089 | if (module_name == NULL) |
| 3090 | goto error; |
| 3091 | |
| 3092 | /* XXX: Change to use the import C API directly with level=0 to disallow |
| 3093 | relative imports. |
| 3094 | |
| 3095 | XXX: PyImport_ImportModuleLevel could be used. However, this bypasses |
| 3096 | builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore |
| 3097 | custom import functions (IMHO, this would be a nice security |
| 3098 | feature). The import C API would need to be extended to support the |
| 3099 | extra parameters of __import__ to fix that. */ |
| 3100 | module = PyImport_Import(module_name); |
| 3101 | if (module == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3102 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3103 | "Can't pickle %R: import of module %R failed", |
| 3104 | obj, module_name); |
| 3105 | goto error; |
| 3106 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3107 | cls = getattribute(module, global_name, self->proto >= 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3108 | if (cls == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3109 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3110 | "Can't pickle %R: attribute lookup %S on %S failed", |
| 3111 | obj, global_name, module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3112 | goto error; |
| 3113 | } |
| 3114 | if (cls != obj) { |
| 3115 | Py_DECREF(cls); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3116 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3117 | "Can't pickle %R: it's not the same object as %S.%S", |
| 3118 | obj, module_name, global_name); |
| 3119 | goto error; |
| 3120 | } |
| 3121 | Py_DECREF(cls); |
| 3122 | |
| 3123 | if (self->proto >= 2) { |
| 3124 | /* See whether this is in the extension registry, and if |
| 3125 | * so generate an EXT opcode. |
| 3126 | */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3127 | PyObject *extension_key; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3128 | PyObject *code_obj; /* extension code as Python object */ |
| 3129 | long code; /* extension code as C value */ |
| 3130 | char pdata[5]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3131 | Py_ssize_t n; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3132 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3133 | extension_key = PyTuple_Pack(2, module_name, global_name); |
| 3134 | if (extension_key == NULL) { |
| 3135 | goto error; |
| 3136 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3137 | code_obj = PyDict_GetItemWithError(st->extension_registry, |
| 3138 | extension_key); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3139 | Py_DECREF(extension_key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3140 | /* The object is not registered in the extension registry. |
| 3141 | This is the most likely code path. */ |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3142 | if (code_obj == NULL) { |
| 3143 | if (PyErr_Occurred()) { |
| 3144 | goto error; |
| 3145 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3146 | goto gen_global; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3147 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3148 | |
| 3149 | /* XXX: pickle.py doesn't check neither the type, nor the range |
| 3150 | of the value returned by the extension_registry. It should for |
| 3151 | consistency. */ |
| 3152 | |
| 3153 | /* Verify code_obj has the right type and value. */ |
| 3154 | if (!PyLong_Check(code_obj)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3155 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3156 | "Can't pickle %R: extension code %R isn't an integer", |
| 3157 | obj, code_obj); |
| 3158 | goto error; |
| 3159 | } |
| 3160 | code = PyLong_AS_LONG(code_obj); |
| 3161 | if (code <= 0 || code > 0x7fffffffL) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3162 | if (!PyErr_Occurred()) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3163 | PyErr_Format(st->PicklingError, "Can't pickle %R: extension " |
| 3164 | "code %ld is out of range", obj, code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3165 | goto error; |
| 3166 | } |
| 3167 | |
| 3168 | /* Generate an EXT opcode. */ |
| 3169 | if (code <= 0xff) { |
| 3170 | pdata[0] = EXT1; |
| 3171 | pdata[1] = (unsigned char)code; |
| 3172 | n = 2; |
| 3173 | } |
| 3174 | else if (code <= 0xffff) { |
| 3175 | pdata[0] = EXT2; |
| 3176 | pdata[1] = (unsigned char)(code & 0xff); |
| 3177 | pdata[2] = (unsigned char)((code >> 8) & 0xff); |
| 3178 | n = 3; |
| 3179 | } |
| 3180 | else { |
| 3181 | pdata[0] = EXT4; |
| 3182 | pdata[1] = (unsigned char)(code & 0xff); |
| 3183 | pdata[2] = (unsigned char)((code >> 8) & 0xff); |
| 3184 | pdata[3] = (unsigned char)((code >> 16) & 0xff); |
| 3185 | pdata[4] = (unsigned char)((code >> 24) & 0xff); |
| 3186 | n = 5; |
| 3187 | } |
| 3188 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3189 | if (_Pickler_Write(self, pdata, n) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3190 | goto error; |
| 3191 | } |
| 3192 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3193 | gen_global: |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3194 | if (self->proto >= 4) { |
| 3195 | const char stack_global_op = STACK_GLOBAL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3196 | |
Christian Heimes | e8b1ba1 | 2013-11-23 21:13:39 +0100 | [diff] [blame] | 3197 | if (save(self, module_name, 0) < 0) |
| 3198 | goto error; |
| 3199 | if (save(self, global_name, 0) < 0) |
| 3200 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3201 | |
| 3202 | if (_Pickler_Write(self, &stack_global_op, 1) < 0) |
| 3203 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3204 | } |
| 3205 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3206 | /* Generate a normal global opcode if we are using a pickle |
| 3207 | protocol < 4, or if the object is not registered in the |
| 3208 | extension registry. */ |
| 3209 | PyObject *encoded; |
| 3210 | PyObject *(*unicode_encoder)(PyObject *); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3211 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3212 | if (_Pickler_Write(self, &global_op, 1) < 0) |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3213 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3214 | |
| 3215 | /* For protocol < 3 and if the user didn't request against doing |
| 3216 | so, we convert module names to the old 2.x module names. */ |
| 3217 | if (self->proto < 3 && self->fix_imports) { |
| 3218 | if (fix_imports(&module_name, &global_name) < 0) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3219 | goto error; |
| 3220 | } |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3221 | } |
| 3222 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3223 | /* Since Python 3.0 now supports non-ASCII identifiers, we encode |
| 3224 | both the module name and the global name using UTF-8. We do so |
| 3225 | only when we are using the pickle protocol newer than version |
| 3226 | 3. This is to ensure compatibility with older Unpickler running |
| 3227 | on Python 2.x. */ |
| 3228 | if (self->proto == 3) { |
| 3229 | unicode_encoder = PyUnicode_AsUTF8String; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3230 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3231 | else { |
| 3232 | unicode_encoder = PyUnicode_AsASCIIString; |
| 3233 | } |
| 3234 | encoded = unicode_encoder(module_name); |
| 3235 | if (encoded == NULL) { |
| 3236 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3237 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3238 | "can't pickle module identifier '%S' using " |
| 3239 | "pickle protocol %i", |
| 3240 | module_name, self->proto); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3241 | goto error; |
| 3242 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3243 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), |
| 3244 | PyBytes_GET_SIZE(encoded)) < 0) { |
| 3245 | Py_DECREF(encoded); |
| 3246 | goto error; |
| 3247 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3248 | Py_DECREF(encoded); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3249 | if(_Pickler_Write(self, "\n", 1) < 0) |
| 3250 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3251 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3252 | /* Save the name of the module. */ |
| 3253 | encoded = unicode_encoder(global_name); |
| 3254 | if (encoded == NULL) { |
| 3255 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3256 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3257 | "can't pickle global identifier '%S' using " |
| 3258 | "pickle protocol %i", |
| 3259 | global_name, self->proto); |
| 3260 | goto error; |
| 3261 | } |
| 3262 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), |
| 3263 | PyBytes_GET_SIZE(encoded)) < 0) { |
| 3264 | Py_DECREF(encoded); |
| 3265 | goto error; |
| 3266 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3267 | Py_DECREF(encoded); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3268 | if (_Pickler_Write(self, "\n", 1) < 0) |
| 3269 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3270 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3271 | /* Memoize the object. */ |
| 3272 | if (memo_put(self, obj) < 0) |
| 3273 | goto error; |
| 3274 | } |
| 3275 | |
| 3276 | if (0) { |
| 3277 | error: |
| 3278 | status = -1; |
| 3279 | } |
| 3280 | Py_XDECREF(module_name); |
| 3281 | Py_XDECREF(global_name); |
| 3282 | Py_XDECREF(module); |
| 3283 | |
| 3284 | return status; |
| 3285 | } |
| 3286 | |
| 3287 | static int |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3288 | save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton) |
| 3289 | { |
| 3290 | PyObject *reduce_value; |
| 3291 | int status; |
| 3292 | |
| 3293 | reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton); |
| 3294 | if (reduce_value == NULL) { |
| 3295 | return -1; |
| 3296 | } |
| 3297 | status = save_reduce(self, reduce_value, obj); |
| 3298 | Py_DECREF(reduce_value); |
| 3299 | return status; |
| 3300 | } |
| 3301 | |
| 3302 | static int |
| 3303 | save_type(PicklerObject *self, PyObject *obj) |
| 3304 | { |
Alexandre Vassalotti | 65846c6 | 2013-11-30 17:55:48 -0800 | [diff] [blame] | 3305 | if (obj == (PyObject *)&_PyNone_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3306 | return save_singleton_type(self, obj, Py_None); |
| 3307 | } |
| 3308 | else if (obj == (PyObject *)&PyEllipsis_Type) { |
| 3309 | return save_singleton_type(self, obj, Py_Ellipsis); |
| 3310 | } |
Alexandre Vassalotti | 65846c6 | 2013-11-30 17:55:48 -0800 | [diff] [blame] | 3311 | else if (obj == (PyObject *)&_PyNotImplemented_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3312 | return save_singleton_type(self, obj, Py_NotImplemented); |
| 3313 | } |
| 3314 | return save_global(self, obj, NULL); |
| 3315 | } |
| 3316 | |
| 3317 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3318 | save_pers(PicklerObject *self, PyObject *obj, PyObject *func) |
| 3319 | { |
| 3320 | PyObject *pid = NULL; |
| 3321 | int status = 0; |
| 3322 | |
| 3323 | const char persid_op = PERSID; |
| 3324 | const char binpersid_op = BINPERSID; |
| 3325 | |
| 3326 | Py_INCREF(obj); |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 3327 | pid = _Pickle_FastCall(func, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3328 | if (pid == NULL) |
| 3329 | return -1; |
| 3330 | |
| 3331 | if (pid != Py_None) { |
| 3332 | if (self->bin) { |
| 3333 | if (save(self, pid, 1) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3334 | _Pickler_Write(self, &binpersid_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3335 | goto error; |
| 3336 | } |
| 3337 | else { |
| 3338 | PyObject *pid_str = NULL; |
| 3339 | char *pid_ascii_bytes; |
| 3340 | Py_ssize_t size; |
| 3341 | |
| 3342 | pid_str = PyObject_Str(pid); |
| 3343 | if (pid_str == NULL) |
| 3344 | goto error; |
| 3345 | |
| 3346 | /* XXX: Should it check whether the persistent id only contains |
| 3347 | ASCII characters? And what if the pid contains embedded |
| 3348 | newlines? */ |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 3349 | pid_ascii_bytes = _PyUnicode_AsStringAndSize(pid_str, &size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3350 | Py_DECREF(pid_str); |
| 3351 | if (pid_ascii_bytes == NULL) |
| 3352 | goto error; |
| 3353 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3354 | if (_Pickler_Write(self, &persid_op, 1) < 0 || |
| 3355 | _Pickler_Write(self, pid_ascii_bytes, size) < 0 || |
| 3356 | _Pickler_Write(self, "\n", 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3357 | goto error; |
| 3358 | } |
| 3359 | status = 1; |
| 3360 | } |
| 3361 | |
| 3362 | if (0) { |
| 3363 | error: |
| 3364 | status = -1; |
| 3365 | } |
| 3366 | Py_XDECREF(pid); |
| 3367 | |
| 3368 | return status; |
| 3369 | } |
| 3370 | |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3371 | static PyObject * |
| 3372 | get_class(PyObject *obj) |
| 3373 | { |
| 3374 | PyObject *cls; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3375 | _Py_IDENTIFIER(__class__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3376 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3377 | cls = _PyObject_GetAttrId(obj, &PyId___class__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3378 | if (cls == NULL) { |
| 3379 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 3380 | PyErr_Clear(); |
| 3381 | cls = (PyObject *) Py_TYPE(obj); |
| 3382 | Py_INCREF(cls); |
| 3383 | } |
| 3384 | } |
| 3385 | return cls; |
| 3386 | } |
| 3387 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3388 | /* We're saving obj, and args is the 2-thru-5 tuple returned by the |
| 3389 | * appropriate __reduce__ method for obj. |
| 3390 | */ |
| 3391 | static int |
| 3392 | save_reduce(PicklerObject *self, PyObject *args, PyObject *obj) |
| 3393 | { |
| 3394 | PyObject *callable; |
| 3395 | PyObject *argtup; |
| 3396 | PyObject *state = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3397 | PyObject *listitems = Py_None; |
| 3398 | PyObject *dictitems = Py_None; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3399 | PickleState *st = _Pickle_GetGlobalState(); |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3400 | Py_ssize_t size; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3401 | int use_newobj = 0, use_newobj_ex = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3402 | |
| 3403 | const char reduce_op = REDUCE; |
| 3404 | const char build_op = BUILD; |
| 3405 | const char newobj_op = NEWOBJ; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3406 | const char newobj_ex_op = NEWOBJ_EX; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3407 | |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3408 | size = PyTuple_Size(args); |
| 3409 | if (size < 2 || size > 5) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3410 | PyErr_SetString(st->PicklingError, "tuple returned by " |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3411 | "__reduce__ must contain 2 through 5 elements"); |
| 3412 | return -1; |
| 3413 | } |
| 3414 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3415 | if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5, |
| 3416 | &callable, &argtup, &state, &listitems, &dictitems)) |
| 3417 | return -1; |
| 3418 | |
| 3419 | if (!PyCallable_Check(callable)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3420 | PyErr_SetString(st->PicklingError, "first item of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3421 | "returned by __reduce__ must be callable"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3422 | return -1; |
| 3423 | } |
| 3424 | if (!PyTuple_Check(argtup)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3425 | PyErr_SetString(st->PicklingError, "second item of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3426 | "returned by __reduce__ must be a tuple"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3427 | return -1; |
| 3428 | } |
| 3429 | |
| 3430 | if (state == Py_None) |
| 3431 | state = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3432 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3433 | if (listitems == Py_None) |
| 3434 | listitems = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3435 | else if (!PyIter_Check(listitems)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3436 | PyErr_Format(st->PicklingError, "fourth element of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3437 | "returned by __reduce__ must be an iterator, not %s", |
| 3438 | Py_TYPE(listitems)->tp_name); |
| 3439 | return -1; |
| 3440 | } |
| 3441 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3442 | if (dictitems == Py_None) |
| 3443 | dictitems = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3444 | else if (!PyIter_Check(dictitems)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3445 | PyErr_Format(st->PicklingError, "fifth element of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3446 | "returned by __reduce__ must be an iterator, not %s", |
| 3447 | Py_TYPE(dictitems)->tp_name); |
| 3448 | return -1; |
| 3449 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3450 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3451 | if (self->proto >= 2) { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3452 | PyObject *name; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3453 | _Py_IDENTIFIER(__name__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3454 | |
Victor Stinner | 804e05e | 2013-11-14 01:26:17 +0100 | [diff] [blame] | 3455 | name = _PyObject_GetAttrId(callable, &PyId___name__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3456 | if (name == NULL) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3457 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3458 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3459 | } |
| 3460 | PyErr_Clear(); |
| 3461 | } |
| 3462 | else if (self->proto >= 4) { |
| 3463 | _Py_IDENTIFIER(__newobj_ex__); |
| 3464 | use_newobj_ex = PyUnicode_Check(name) && |
| 3465 | PyUnicode_Compare( |
| 3466 | name, _PyUnicode_FromId(&PyId___newobj_ex__)) == 0; |
| 3467 | Py_DECREF(name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3468 | } |
| 3469 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3470 | _Py_IDENTIFIER(__newobj__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3471 | use_newobj = PyUnicode_Check(name) && |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3472 | PyUnicode_Compare( |
| 3473 | name, _PyUnicode_FromId(&PyId___newobj__)) == 0; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3474 | Py_DECREF(name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3475 | } |
| 3476 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3477 | |
| 3478 | if (use_newobj_ex) { |
| 3479 | PyObject *cls; |
| 3480 | PyObject *args; |
| 3481 | PyObject *kwargs; |
| 3482 | |
| 3483 | if (Py_SIZE(argtup) != 3) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3484 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3485 | "length of the NEWOBJ_EX argument tuple must be " |
| 3486 | "exactly 3, not %zd", Py_SIZE(argtup)); |
| 3487 | return -1; |
| 3488 | } |
| 3489 | |
| 3490 | cls = PyTuple_GET_ITEM(argtup, 0); |
| 3491 | if (!PyType_Check(cls)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3492 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3493 | "first item from NEWOBJ_EX argument tuple must " |
| 3494 | "be a class, not %.200s", Py_TYPE(cls)->tp_name); |
| 3495 | return -1; |
| 3496 | } |
| 3497 | args = PyTuple_GET_ITEM(argtup, 1); |
| 3498 | if (!PyTuple_Check(args)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3499 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3500 | "second item from NEWOBJ_EX argument tuple must " |
| 3501 | "be a tuple, not %.200s", Py_TYPE(args)->tp_name); |
| 3502 | return -1; |
| 3503 | } |
| 3504 | kwargs = PyTuple_GET_ITEM(argtup, 2); |
| 3505 | if (!PyDict_Check(kwargs)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3506 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3507 | "third item from NEWOBJ_EX argument tuple must " |
| 3508 | "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name); |
| 3509 | return -1; |
| 3510 | } |
| 3511 | |
| 3512 | if (save(self, cls, 0) < 0 || |
| 3513 | save(self, args, 0) < 0 || |
| 3514 | save(self, kwargs, 0) < 0 || |
| 3515 | _Pickler_Write(self, &newobj_ex_op, 1) < 0) { |
| 3516 | return -1; |
| 3517 | } |
| 3518 | } |
| 3519 | else if (use_newobj) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3520 | PyObject *cls; |
| 3521 | PyObject *newargtup; |
| 3522 | PyObject *obj_class; |
| 3523 | int p; |
| 3524 | |
| 3525 | /* Sanity checks. */ |
| 3526 | if (Py_SIZE(argtup) < 1) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3527 | PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3528 | return -1; |
| 3529 | } |
| 3530 | |
| 3531 | cls = PyTuple_GET_ITEM(argtup, 0); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3532 | if (!PyType_Check(cls)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3533 | PyErr_SetString(st->PicklingError, "args[0] from " |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3534 | "__newobj__ args is not a type"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3535 | return -1; |
| 3536 | } |
| 3537 | |
| 3538 | if (obj != NULL) { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3539 | obj_class = get_class(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3540 | p = obj_class != cls; /* true iff a problem */ |
| 3541 | Py_DECREF(obj_class); |
| 3542 | if (p) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3543 | PyErr_SetString(st->PicklingError, "args[0] from " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3544 | "__newobj__ args has the wrong class"); |
| 3545 | return -1; |
| 3546 | } |
| 3547 | } |
| 3548 | /* XXX: These calls save() are prone to infinite recursion. Imagine |
| 3549 | what happen if the value returned by the __reduce__() method of |
| 3550 | some extension type contains another object of the same type. Ouch! |
| 3551 | |
| 3552 | Here is a quick example, that I ran into, to illustrate what I |
| 3553 | mean: |
| 3554 | |
| 3555 | >>> import pickle, copyreg |
| 3556 | >>> copyreg.dispatch_table.pop(complex) |
| 3557 | >>> pickle.dumps(1+2j) |
| 3558 | Traceback (most recent call last): |
| 3559 | ... |
| 3560 | RuntimeError: maximum recursion depth exceeded |
| 3561 | |
| 3562 | Removing the complex class from copyreg.dispatch_table made the |
| 3563 | __reduce_ex__() method emit another complex object: |
| 3564 | |
| 3565 | >>> (1+1j).__reduce_ex__(2) |
| 3566 | (<function __newobj__ at 0xb7b71c3c>, |
| 3567 | (<class 'complex'>, (1+1j)), None, None, None) |
| 3568 | |
| 3569 | Thus when save() was called on newargstup (the 2nd item) recursion |
| 3570 | ensued. Of course, the bug was in the complex class which had a |
| 3571 | broken __getnewargs__() that emitted another complex object. But, |
| 3572 | the point, here, is it is quite easy to end up with a broken reduce |
| 3573 | function. */ |
| 3574 | |
| 3575 | /* Save the class and its __new__ arguments. */ |
| 3576 | if (save(self, cls, 0) < 0) |
| 3577 | return -1; |
| 3578 | |
| 3579 | newargtup = PyTuple_GetSlice(argtup, 1, Py_SIZE(argtup)); |
| 3580 | if (newargtup == NULL) |
| 3581 | return -1; |
| 3582 | |
| 3583 | p = save(self, newargtup, 0); |
| 3584 | Py_DECREF(newargtup); |
| 3585 | if (p < 0) |
| 3586 | return -1; |
| 3587 | |
| 3588 | /* Add NEWOBJ opcode. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3589 | if (_Pickler_Write(self, &newobj_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3590 | return -1; |
| 3591 | } |
| 3592 | else { /* Not using NEWOBJ. */ |
| 3593 | if (save(self, callable, 0) < 0 || |
| 3594 | save(self, argtup, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3595 | _Pickler_Write(self, &reduce_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3596 | return -1; |
| 3597 | } |
| 3598 | |
| 3599 | /* obj can be NULL when save_reduce() is used directly. A NULL obj means |
| 3600 | the caller do not want to memoize the object. Not particularly useful, |
| 3601 | but that is to mimic the behavior save_reduce() in pickle.py when |
| 3602 | obj is None. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3603 | if (obj != NULL) { |
| 3604 | /* If the object is already in the memo, this means it is |
| 3605 | recursive. In this case, throw away everything we put on the |
| 3606 | stack, and fetch the object back from the memo. */ |
| 3607 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3608 | const char pop_op = POP; |
| 3609 | |
| 3610 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 3611 | return -1; |
| 3612 | if (memo_get(self, obj) < 0) |
| 3613 | return -1; |
| 3614 | |
| 3615 | return 0; |
| 3616 | } |
| 3617 | else if (memo_put(self, obj) < 0) |
| 3618 | return -1; |
| 3619 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3620 | |
| 3621 | if (listitems && batch_list(self, listitems) < 0) |
| 3622 | return -1; |
| 3623 | |
| 3624 | if (dictitems && batch_dict(self, dictitems) < 0) |
| 3625 | return -1; |
| 3626 | |
| 3627 | if (state) { |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 3628 | if (save(self, state, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3629 | _Pickler_Write(self, &build_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3630 | return -1; |
| 3631 | } |
| 3632 | |
| 3633 | return 0; |
| 3634 | } |
| 3635 | |
| 3636 | static int |
| 3637 | save(PicklerObject *self, PyObject *obj, int pers_save) |
| 3638 | { |
| 3639 | PyTypeObject *type; |
| 3640 | PyObject *reduce_func = NULL; |
| 3641 | PyObject *reduce_value = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3642 | int status = 0; |
| 3643 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 3644 | if (_Pickler_OpcodeBoundary(self) < 0) |
| 3645 | return -1; |
| 3646 | |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 3647 | if (Py_EnterRecursiveCall(" while pickling an object")) |
Alexandre Vassalotti | dff1834 | 2008-07-13 18:48:30 +0000 | [diff] [blame] | 3648 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3649 | |
| 3650 | /* The extra pers_save argument is necessary to avoid calling save_pers() |
| 3651 | on its returned object. */ |
| 3652 | if (!pers_save && self->pers_func) { |
| 3653 | /* save_pers() returns: |
| 3654 | -1 to signal an error; |
| 3655 | 0 if it did nothing successfully; |
| 3656 | 1 if a persistent id was saved. |
| 3657 | */ |
| 3658 | if ((status = save_pers(self, obj, self->pers_func)) != 0) |
| 3659 | goto done; |
| 3660 | } |
| 3661 | |
| 3662 | type = Py_TYPE(obj); |
| 3663 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3664 | /* The old cPickle had an optimization that used switch-case statement |
| 3665 | dispatching on the first letter of the type name. This has was removed |
| 3666 | since benchmarks shown that this optimization was actually slowing |
| 3667 | things down. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3668 | |
| 3669 | /* Atom types; these aren't memoized, so don't check the memo. */ |
| 3670 | |
| 3671 | if (obj == Py_None) { |
| 3672 | status = save_none(self, obj); |
| 3673 | goto done; |
| 3674 | } |
| 3675 | else if (obj == Py_False || obj == Py_True) { |
| 3676 | status = save_bool(self, obj); |
| 3677 | goto done; |
| 3678 | } |
| 3679 | else if (type == &PyLong_Type) { |
| 3680 | status = save_long(self, obj); |
| 3681 | goto done; |
| 3682 | } |
| 3683 | else if (type == &PyFloat_Type) { |
| 3684 | status = save_float(self, obj); |
| 3685 | goto done; |
| 3686 | } |
| 3687 | |
| 3688 | /* Check the memo to see if it has the object. If so, generate |
| 3689 | a GET (or BINGET) opcode, instead of pickling the object |
| 3690 | once again. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3691 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3692 | if (memo_get(self, obj) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3693 | goto error; |
| 3694 | goto done; |
| 3695 | } |
| 3696 | |
| 3697 | if (type == &PyBytes_Type) { |
| 3698 | status = save_bytes(self, obj); |
| 3699 | goto done; |
| 3700 | } |
| 3701 | else if (type == &PyUnicode_Type) { |
| 3702 | status = save_unicode(self, obj); |
| 3703 | goto done; |
| 3704 | } |
| 3705 | else if (type == &PyDict_Type) { |
| 3706 | status = save_dict(self, obj); |
| 3707 | goto done; |
| 3708 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3709 | else if (type == &PySet_Type) { |
| 3710 | status = save_set(self, obj); |
| 3711 | goto done; |
| 3712 | } |
| 3713 | else if (type == &PyFrozenSet_Type) { |
| 3714 | status = save_frozenset(self, obj); |
| 3715 | goto done; |
| 3716 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3717 | else if (type == &PyList_Type) { |
| 3718 | status = save_list(self, obj); |
| 3719 | goto done; |
| 3720 | } |
| 3721 | else if (type == &PyTuple_Type) { |
| 3722 | status = save_tuple(self, obj); |
| 3723 | goto done; |
| 3724 | } |
| 3725 | else if (type == &PyType_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3726 | status = save_type(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3727 | goto done; |
| 3728 | } |
| 3729 | else if (type == &PyFunction_Type) { |
| 3730 | status = save_global(self, obj, NULL); |
Alexandre Vassalotti | fc91285 | 2013-11-24 03:07:35 -0800 | [diff] [blame] | 3731 | goto done; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3732 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3733 | |
| 3734 | /* XXX: This part needs some unit tests. */ |
| 3735 | |
| 3736 | /* Get a reduction callable, and call it. This may come from |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3737 | * self.dispatch_table, copyreg.dispatch_table, the object's |
| 3738 | * __reduce_ex__ method, or the object's __reduce__ method. |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3739 | */ |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3740 | if (self->dispatch_table == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3741 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3742 | reduce_func = PyDict_GetItemWithError(st->dispatch_table, |
| 3743 | (PyObject *)type); |
| 3744 | if (reduce_func == NULL) { |
| 3745 | if (PyErr_Occurred()) { |
| 3746 | goto error; |
| 3747 | } |
| 3748 | } else { |
| 3749 | /* PyDict_GetItemWithError() returns a borrowed reference. |
| 3750 | Increase the reference count to be consistent with |
| 3751 | PyObject_GetItem and _PyObject_GetAttrId used below. */ |
| 3752 | Py_INCREF(reduce_func); |
| 3753 | } |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3754 | } else { |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3755 | reduce_func = PyObject_GetItem(self->dispatch_table, |
| 3756 | (PyObject *)type); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3757 | if (reduce_func == NULL) { |
| 3758 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 3759 | PyErr_Clear(); |
| 3760 | else |
| 3761 | goto error; |
| 3762 | } |
| 3763 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3764 | if (reduce_func != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3765 | Py_INCREF(obj); |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 3766 | reduce_value = _Pickle_FastCall(reduce_func, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3767 | } |
Antoine Pitrou | ffd41d9 | 2011-10-04 09:23:04 +0200 | [diff] [blame] | 3768 | else if (PyType_IsSubtype(type, &PyType_Type)) { |
| 3769 | status = save_global(self, obj, NULL); |
| 3770 | goto done; |
| 3771 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3772 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3773 | _Py_IDENTIFIER(__reduce__); |
| 3774 | _Py_IDENTIFIER(__reduce_ex__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3775 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3776 | |
| 3777 | /* XXX: If the __reduce__ method is defined, __reduce_ex__ is |
| 3778 | automatically defined as __reduce__. While this is convenient, this |
| 3779 | make it impossible to know which method was actually called. Of |
| 3780 | course, this is not a big deal. But still, it would be nice to let |
| 3781 | the user know which method was called when something go |
| 3782 | wrong. Incidentally, this means if __reduce_ex__ is not defined, we |
| 3783 | don't actually have to check for a __reduce__ method. */ |
| 3784 | |
| 3785 | /* Check for a __reduce_ex__ method. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3786 | reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3787 | if (reduce_func != NULL) { |
| 3788 | PyObject *proto; |
| 3789 | proto = PyLong_FromLong(self->proto); |
| 3790 | if (proto != NULL) { |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 3791 | reduce_value = _Pickle_FastCall(reduce_func, proto); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3792 | } |
| 3793 | } |
| 3794 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3795 | PickleState *st = _Pickle_GetGlobalState(); |
| 3796 | |
| 3797 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3798 | PyErr_Clear(); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3799 | } |
| 3800 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3801 | goto error; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3802 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3803 | /* Check for a __reduce__ method. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3804 | reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3805 | if (reduce_func != NULL) { |
Alexandre Vassalotti | 6bf41e5 | 2013-11-28 15:17:29 -0800 | [diff] [blame] | 3806 | PyObject *empty_tuple = PyTuple_New(0); |
| 3807 | reduce_value = PyObject_Call(reduce_func, empty_tuple, |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3808 | NULL); |
Alexandre Vassalotti | 6bf41e5 | 2013-11-28 15:17:29 -0800 | [diff] [blame] | 3809 | Py_DECREF(empty_tuple); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3810 | } |
| 3811 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3812 | PyErr_Format(st->PicklingError, |
| 3813 | "can't pickle '%.200s' object: %R", |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3814 | type->tp_name, obj); |
| 3815 | goto error; |
| 3816 | } |
| 3817 | } |
| 3818 | } |
| 3819 | |
| 3820 | if (reduce_value == NULL) |
| 3821 | goto error; |
| 3822 | |
| 3823 | if (PyUnicode_Check(reduce_value)) { |
| 3824 | status = save_global(self, obj, reduce_value); |
| 3825 | goto done; |
| 3826 | } |
| 3827 | |
| 3828 | if (!PyTuple_Check(reduce_value)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3829 | PickleState *st = _Pickle_GetGlobalState(); |
| 3830 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3831 | "__reduce__ must return a string or tuple"); |
| 3832 | goto error; |
| 3833 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3834 | |
| 3835 | status = save_reduce(self, reduce_value, obj); |
| 3836 | |
| 3837 | if (0) { |
| 3838 | error: |
| 3839 | status = -1; |
| 3840 | } |
| 3841 | done: |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 3842 | |
Alexandre Vassalotti | dff1834 | 2008-07-13 18:48:30 +0000 | [diff] [blame] | 3843 | Py_LeaveRecursiveCall(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3844 | Py_XDECREF(reduce_func); |
| 3845 | Py_XDECREF(reduce_value); |
| 3846 | |
| 3847 | return status; |
| 3848 | } |
| 3849 | |
| 3850 | static int |
| 3851 | dump(PicklerObject *self, PyObject *obj) |
| 3852 | { |
| 3853 | const char stop_op = STOP; |
| 3854 | |
| 3855 | if (self->proto >= 2) { |
| 3856 | char header[2]; |
| 3857 | |
| 3858 | header[0] = PROTO; |
| 3859 | assert(self->proto >= 0 && self->proto < 256); |
| 3860 | header[1] = (unsigned char)self->proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3861 | if (_Pickler_Write(self, header, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3862 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3863 | if (self->proto >= 4) |
| 3864 | self->framing = 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3865 | } |
| 3866 | |
| 3867 | if (save(self, obj, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3868 | _Pickler_Write(self, &stop_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3869 | return -1; |
| 3870 | |
| 3871 | return 0; |
| 3872 | } |
| 3873 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3874 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3875 | |
| 3876 | _pickle.Pickler.clear_memo |
| 3877 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3878 | Clears the pickler's "memo". |
| 3879 | |
| 3880 | The memo is the data structure that remembers which objects the |
| 3881 | pickler has already seen, so that shared or recursive objects are |
| 3882 | pickled by reference and not by value. This method is useful when |
| 3883 | re-using picklers. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3884 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3885 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 3886 | static PyObject * |
| 3887 | _pickle_Pickler_clear_memo_impl(PicklerObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 3888 | /*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3889 | { |
| 3890 | if (self->memo) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3891 | PyMemoTable_Clear(self->memo); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3892 | |
| 3893 | Py_RETURN_NONE; |
| 3894 | } |
| 3895 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3896 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3897 | |
| 3898 | _pickle.Pickler.dump |
| 3899 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3900 | obj: object |
| 3901 | / |
| 3902 | |
| 3903 | Write a pickled representation of the given object to the open file. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3904 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3905 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3906 | static PyObject * |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3907 | _pickle_Pickler_dump(PicklerObject *self, PyObject *obj) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 3908 | /*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3909 | { |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 3910 | /* Check whether the Pickler was initialized correctly (issue3664). |
| 3911 | Developers often forget to call __init__() in their subclasses, which |
| 3912 | would trigger a segfault without this check. */ |
| 3913 | if (self->write == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3914 | PickleState *st = _Pickle_GetGlobalState(); |
| 3915 | PyErr_Format(st->PicklingError, |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 3916 | "Pickler.__init__() was not called by %s.__init__()", |
| 3917 | Py_TYPE(self)->tp_name); |
| 3918 | return NULL; |
| 3919 | } |
| 3920 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3921 | if (_Pickler_ClearBuffer(self) < 0) |
| 3922 | return NULL; |
| 3923 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3924 | if (dump(self, obj) < 0) |
| 3925 | return NULL; |
| 3926 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3927 | if (_Pickler_FlushToFile(self) < 0) |
| 3928 | return NULL; |
| 3929 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3930 | Py_RETURN_NONE; |
| 3931 | } |
| 3932 | |
| 3933 | static struct PyMethodDef Pickler_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3934 | _PICKLE_PICKLER_DUMP_METHODDEF |
| 3935 | _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3936 | {NULL, NULL} /* sentinel */ |
| 3937 | }; |
| 3938 | |
| 3939 | static void |
| 3940 | Pickler_dealloc(PicklerObject *self) |
| 3941 | { |
| 3942 | PyObject_GC_UnTrack(self); |
| 3943 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3944 | Py_XDECREF(self->output_buffer); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3945 | Py_XDECREF(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3946 | Py_XDECREF(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3947 | Py_XDECREF(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3948 | Py_XDECREF(self->fast_memo); |
| 3949 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3950 | PyMemoTable_Del(self->memo); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3951 | |
| 3952 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 3953 | } |
| 3954 | |
| 3955 | static int |
| 3956 | Pickler_traverse(PicklerObject *self, visitproc visit, void *arg) |
| 3957 | { |
| 3958 | Py_VISIT(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3959 | Py_VISIT(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3960 | Py_VISIT(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3961 | Py_VISIT(self->fast_memo); |
| 3962 | return 0; |
| 3963 | } |
| 3964 | |
| 3965 | static int |
| 3966 | Pickler_clear(PicklerObject *self) |
| 3967 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3968 | Py_CLEAR(self->output_buffer); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3969 | Py_CLEAR(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3970 | Py_CLEAR(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3971 | Py_CLEAR(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3972 | Py_CLEAR(self->fast_memo); |
| 3973 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3974 | if (self->memo != NULL) { |
| 3975 | PyMemoTable *memo = self->memo; |
| 3976 | self->memo = NULL; |
| 3977 | PyMemoTable_Del(memo); |
| 3978 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3979 | return 0; |
| 3980 | } |
| 3981 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3982 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3983 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3984 | |
| 3985 | _pickle.Pickler.__init__ |
| 3986 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3987 | file: object |
| 3988 | protocol: object = NULL |
| 3989 | fix_imports: bool = True |
| 3990 | |
| 3991 | This takes a binary file for writing a pickle data stream. |
| 3992 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 3993 | The optional *protocol* argument tells the pickler to use the given |
| 3994 | protocol; supported protocols are 0, 1, 2, 3 and 4. The default |
| 3995 | protocol is 3; a backward-incompatible protocol designed for Python 3. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3996 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 3997 | Specifying a negative protocol version selects the highest protocol |
| 3998 | version supported. The higher the protocol used, the more recent the |
| 3999 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4000 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4001 | The *file* argument must have a write() method that accepts a single |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4002 | bytes argument. It can thus be a file object opened for binary |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4003 | writing, a io.BytesIO instance, or any other custom object that meets |
| 4004 | this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4005 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4006 | If *fix_imports* is True and protocol is less than 3, pickle will try |
| 4007 | to map the new Python 3 names to the old module names used in Python |
| 4008 | 2, so that the pickle data stream is readable with Python 2. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4009 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4010 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4011 | static int |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4012 | _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] | 4013 | /*[clinic end generated code: output=56e229f3b1f4332f input=b8cdeb7e3f5ee674]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4014 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4015 | _Py_IDENTIFIER(persistent_id); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4016 | _Py_IDENTIFIER(dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4017 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4018 | /* In case of multiple __init__() calls, clear previous content. */ |
| 4019 | if (self->write != NULL) |
| 4020 | (void)Pickler_clear(self); |
| 4021 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4022 | if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4023 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4024 | |
| 4025 | if (_Pickler_SetOutputStream(self, file) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4026 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4027 | |
| 4028 | /* memo and output_buffer may have already been created in _Pickler_New */ |
| 4029 | if (self->memo == NULL) { |
| 4030 | self->memo = PyMemoTable_New(); |
| 4031 | if (self->memo == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4032 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4033 | } |
| 4034 | self->output_len = 0; |
| 4035 | if (self->output_buffer == NULL) { |
| 4036 | self->max_output_len = WRITE_BUF_SIZE; |
| 4037 | self->output_buffer = PyBytes_FromStringAndSize(NULL, |
| 4038 | self->max_output_len); |
| 4039 | if (self->output_buffer == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4040 | return -1; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 4041 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4042 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 4043 | self->fast = 0; |
| 4044 | self->fast_nesting = 0; |
| 4045 | self->fast_memo = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4046 | self->pers_func = NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 4047 | if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) { |
| 4048 | self->pers_func = _PyObject_GetAttrId((PyObject *)self, |
| 4049 | &PyId_persistent_id); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4050 | if (self->pers_func == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4051 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4052 | } |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4053 | self->dispatch_table = NULL; |
| 4054 | if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) { |
| 4055 | self->dispatch_table = _PyObject_GetAttrId((PyObject *)self, |
| 4056 | &PyId_dispatch_table); |
| 4057 | if (self->dispatch_table == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4058 | return -1; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4059 | } |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4060 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4061 | return 0; |
| 4062 | } |
| 4063 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4064 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4065 | /* Define a proxy object for the Pickler's internal memo object. This is to |
| 4066 | * avoid breaking code like: |
| 4067 | * pickler.memo.clear() |
| 4068 | * and |
| 4069 | * pickler.memo = saved_memo |
| 4070 | * Is this a good idea? Not really, but we don't want to break code that uses |
| 4071 | * it. Note that we don't implement the entire mapping API here. This is |
| 4072 | * intentional, as these should be treated as black-box implementation details. |
| 4073 | */ |
| 4074 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4075 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4076 | _pickle.PicklerMemoProxy.clear |
| 4077 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4078 | Remove all items from memo. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4079 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4080 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4081 | static PyObject * |
| 4082 | _pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4083 | /*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4084 | { |
| 4085 | if (self->pickler->memo) |
| 4086 | PyMemoTable_Clear(self->pickler->memo); |
| 4087 | Py_RETURN_NONE; |
| 4088 | } |
| 4089 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4090 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4091 | _pickle.PicklerMemoProxy.copy |
| 4092 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4093 | Copy the memo to a new object. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4094 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4095 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4096 | static PyObject * |
| 4097 | _pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4098 | /*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4099 | { |
| 4100 | Py_ssize_t i; |
| 4101 | PyMemoTable *memo; |
| 4102 | PyObject *new_memo = PyDict_New(); |
| 4103 | if (new_memo == NULL) |
| 4104 | return NULL; |
| 4105 | |
| 4106 | memo = self->pickler->memo; |
| 4107 | for (i = 0; i < memo->mt_allocated; ++i) { |
| 4108 | PyMemoEntry entry = memo->mt_table[i]; |
| 4109 | if (entry.me_key != NULL) { |
| 4110 | int status; |
| 4111 | PyObject *key, *value; |
| 4112 | |
| 4113 | key = PyLong_FromVoidPtr(entry.me_key); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4114 | value = Py_BuildValue("nO", entry.me_value, entry.me_key); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4115 | |
| 4116 | if (key == NULL || value == NULL) { |
| 4117 | Py_XDECREF(key); |
| 4118 | Py_XDECREF(value); |
| 4119 | goto error; |
| 4120 | } |
| 4121 | status = PyDict_SetItem(new_memo, key, value); |
| 4122 | Py_DECREF(key); |
| 4123 | Py_DECREF(value); |
| 4124 | if (status < 0) |
| 4125 | goto error; |
| 4126 | } |
| 4127 | } |
| 4128 | return new_memo; |
| 4129 | |
| 4130 | error: |
| 4131 | Py_XDECREF(new_memo); |
| 4132 | return NULL; |
| 4133 | } |
| 4134 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4135 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4136 | _pickle.PicklerMemoProxy.__reduce__ |
| 4137 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4138 | Implement pickle support. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4139 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4140 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4141 | static PyObject * |
| 4142 | _pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4143 | /*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4144 | { |
| 4145 | PyObject *reduce_value, *dict_args; |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4146 | PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4147 | if (contents == NULL) |
| 4148 | return NULL; |
| 4149 | |
| 4150 | reduce_value = PyTuple_New(2); |
| 4151 | if (reduce_value == NULL) { |
| 4152 | Py_DECREF(contents); |
| 4153 | return NULL; |
| 4154 | } |
| 4155 | dict_args = PyTuple_New(1); |
| 4156 | if (dict_args == NULL) { |
| 4157 | Py_DECREF(contents); |
| 4158 | Py_DECREF(reduce_value); |
| 4159 | return NULL; |
| 4160 | } |
| 4161 | PyTuple_SET_ITEM(dict_args, 0, contents); |
| 4162 | Py_INCREF((PyObject *)&PyDict_Type); |
| 4163 | PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type); |
| 4164 | PyTuple_SET_ITEM(reduce_value, 1, dict_args); |
| 4165 | return reduce_value; |
| 4166 | } |
| 4167 | |
| 4168 | static PyMethodDef picklerproxy_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4169 | _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF |
| 4170 | _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF |
| 4171 | _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4172 | {NULL, NULL} /* sentinel */ |
| 4173 | }; |
| 4174 | |
| 4175 | static void |
| 4176 | PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self) |
| 4177 | { |
| 4178 | PyObject_GC_UnTrack(self); |
| 4179 | Py_XDECREF(self->pickler); |
| 4180 | PyObject_GC_Del((PyObject *)self); |
| 4181 | } |
| 4182 | |
| 4183 | static int |
| 4184 | PicklerMemoProxy_traverse(PicklerMemoProxyObject *self, |
| 4185 | visitproc visit, void *arg) |
| 4186 | { |
| 4187 | Py_VISIT(self->pickler); |
| 4188 | return 0; |
| 4189 | } |
| 4190 | |
| 4191 | static int |
| 4192 | PicklerMemoProxy_clear(PicklerMemoProxyObject *self) |
| 4193 | { |
| 4194 | Py_CLEAR(self->pickler); |
| 4195 | return 0; |
| 4196 | } |
| 4197 | |
| 4198 | static PyTypeObject PicklerMemoProxyType = { |
| 4199 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4200 | "_pickle.PicklerMemoProxy", /*tp_name*/ |
| 4201 | sizeof(PicklerMemoProxyObject), /*tp_basicsize*/ |
| 4202 | 0, |
| 4203 | (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */ |
| 4204 | 0, /* tp_print */ |
| 4205 | 0, /* tp_getattr */ |
| 4206 | 0, /* tp_setattr */ |
| 4207 | 0, /* tp_compare */ |
| 4208 | 0, /* tp_repr */ |
| 4209 | 0, /* tp_as_number */ |
| 4210 | 0, /* tp_as_sequence */ |
| 4211 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 4212 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4213 | 0, /* tp_call */ |
| 4214 | 0, /* tp_str */ |
| 4215 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4216 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 4217 | 0, /* tp_as_buffer */ |
| 4218 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 4219 | 0, /* tp_doc */ |
| 4220 | (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */ |
| 4221 | (inquiry)PicklerMemoProxy_clear, /* tp_clear */ |
| 4222 | 0, /* tp_richcompare */ |
| 4223 | 0, /* tp_weaklistoffset */ |
| 4224 | 0, /* tp_iter */ |
| 4225 | 0, /* tp_iternext */ |
| 4226 | picklerproxy_methods, /* tp_methods */ |
| 4227 | }; |
| 4228 | |
| 4229 | static PyObject * |
| 4230 | PicklerMemoProxy_New(PicklerObject *pickler) |
| 4231 | { |
| 4232 | PicklerMemoProxyObject *self; |
| 4233 | |
| 4234 | self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType); |
| 4235 | if (self == NULL) |
| 4236 | return NULL; |
| 4237 | Py_INCREF(pickler); |
| 4238 | self->pickler = pickler; |
| 4239 | PyObject_GC_Track(self); |
| 4240 | return (PyObject *)self; |
| 4241 | } |
| 4242 | |
| 4243 | /*****************************************************************************/ |
| 4244 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4245 | static PyObject * |
| 4246 | Pickler_get_memo(PicklerObject *self) |
| 4247 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4248 | return PicklerMemoProxy_New(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4249 | } |
| 4250 | |
| 4251 | static int |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4252 | Pickler_set_memo(PicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4253 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4254 | PyMemoTable *new_memo = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4255 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4256 | if (obj == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4257 | PyErr_SetString(PyExc_TypeError, |
| 4258 | "attribute deletion is not supported"); |
| 4259 | return -1; |
| 4260 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4261 | |
| 4262 | if (Py_TYPE(obj) == &PicklerMemoProxyType) { |
| 4263 | PicklerObject *pickler = |
| 4264 | ((PicklerMemoProxyObject *)obj)->pickler; |
| 4265 | |
| 4266 | new_memo = PyMemoTable_Copy(pickler->memo); |
| 4267 | if (new_memo == NULL) |
| 4268 | return -1; |
| 4269 | } |
| 4270 | else if (PyDict_Check(obj)) { |
| 4271 | Py_ssize_t i = 0; |
| 4272 | PyObject *key, *value; |
| 4273 | |
| 4274 | new_memo = PyMemoTable_New(); |
| 4275 | if (new_memo == NULL) |
| 4276 | return -1; |
| 4277 | |
| 4278 | while (PyDict_Next(obj, &i, &key, &value)) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4279 | Py_ssize_t memo_id; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4280 | PyObject *memo_obj; |
| 4281 | |
| 4282 | if (!PyTuple_Check(value) || Py_SIZE(value) != 2) { |
| 4283 | PyErr_SetString(PyExc_TypeError, |
| 4284 | "'memo' values must be 2-item tuples"); |
| 4285 | goto error; |
| 4286 | } |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4287 | memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0)); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4288 | if (memo_id == -1 && PyErr_Occurred()) |
| 4289 | goto error; |
| 4290 | memo_obj = PyTuple_GET_ITEM(value, 1); |
| 4291 | if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0) |
| 4292 | goto error; |
| 4293 | } |
| 4294 | } |
| 4295 | else { |
| 4296 | PyErr_Format(PyExc_TypeError, |
| 4297 | "'memo' attribute must be an PicklerMemoProxy object" |
| 4298 | "or dict, not %.200s", Py_TYPE(obj)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4299 | return -1; |
| 4300 | } |
| 4301 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4302 | PyMemoTable_Del(self->memo); |
| 4303 | self->memo = new_memo; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4304 | |
| 4305 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4306 | |
| 4307 | error: |
| 4308 | if (new_memo) |
| 4309 | PyMemoTable_Del(new_memo); |
| 4310 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4311 | } |
| 4312 | |
| 4313 | static PyObject * |
| 4314 | Pickler_get_persid(PicklerObject *self) |
| 4315 | { |
| 4316 | if (self->pers_func == NULL) |
| 4317 | PyErr_SetString(PyExc_AttributeError, "persistent_id"); |
| 4318 | else |
| 4319 | Py_INCREF(self->pers_func); |
| 4320 | return self->pers_func; |
| 4321 | } |
| 4322 | |
| 4323 | static int |
| 4324 | Pickler_set_persid(PicklerObject *self, PyObject *value) |
| 4325 | { |
| 4326 | PyObject *tmp; |
| 4327 | |
| 4328 | if (value == NULL) { |
| 4329 | PyErr_SetString(PyExc_TypeError, |
| 4330 | "attribute deletion is not supported"); |
| 4331 | return -1; |
| 4332 | } |
| 4333 | if (!PyCallable_Check(value)) { |
| 4334 | PyErr_SetString(PyExc_TypeError, |
| 4335 | "persistent_id must be a callable taking one argument"); |
| 4336 | return -1; |
| 4337 | } |
| 4338 | |
| 4339 | tmp = self->pers_func; |
| 4340 | Py_INCREF(value); |
| 4341 | self->pers_func = value; |
| 4342 | Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */ |
| 4343 | |
| 4344 | return 0; |
| 4345 | } |
| 4346 | |
| 4347 | static PyMemberDef Pickler_members[] = { |
| 4348 | {"bin", T_INT, offsetof(PicklerObject, bin)}, |
| 4349 | {"fast", T_INT, offsetof(PicklerObject, fast)}, |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4350 | {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)}, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4351 | {NULL} |
| 4352 | }; |
| 4353 | |
| 4354 | static PyGetSetDef Pickler_getsets[] = { |
| 4355 | {"memo", (getter)Pickler_get_memo, |
| 4356 | (setter)Pickler_set_memo}, |
| 4357 | {"persistent_id", (getter)Pickler_get_persid, |
| 4358 | (setter)Pickler_set_persid}, |
| 4359 | {NULL} |
| 4360 | }; |
| 4361 | |
| 4362 | static PyTypeObject Pickler_Type = { |
| 4363 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4364 | "_pickle.Pickler" , /*tp_name*/ |
| 4365 | sizeof(PicklerObject), /*tp_basicsize*/ |
| 4366 | 0, /*tp_itemsize*/ |
| 4367 | (destructor)Pickler_dealloc, /*tp_dealloc*/ |
| 4368 | 0, /*tp_print*/ |
| 4369 | 0, /*tp_getattr*/ |
| 4370 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 4371 | 0, /*tp_reserved*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4372 | 0, /*tp_repr*/ |
| 4373 | 0, /*tp_as_number*/ |
| 4374 | 0, /*tp_as_sequence*/ |
| 4375 | 0, /*tp_as_mapping*/ |
| 4376 | 0, /*tp_hash*/ |
| 4377 | 0, /*tp_call*/ |
| 4378 | 0, /*tp_str*/ |
| 4379 | 0, /*tp_getattro*/ |
| 4380 | 0, /*tp_setattro*/ |
| 4381 | 0, /*tp_as_buffer*/ |
| 4382 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4383 | _pickle_Pickler___init____doc__, /*tp_doc*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4384 | (traverseproc)Pickler_traverse, /*tp_traverse*/ |
| 4385 | (inquiry)Pickler_clear, /*tp_clear*/ |
| 4386 | 0, /*tp_richcompare*/ |
| 4387 | 0, /*tp_weaklistoffset*/ |
| 4388 | 0, /*tp_iter*/ |
| 4389 | 0, /*tp_iternext*/ |
| 4390 | Pickler_methods, /*tp_methods*/ |
| 4391 | Pickler_members, /*tp_members*/ |
| 4392 | Pickler_getsets, /*tp_getset*/ |
| 4393 | 0, /*tp_base*/ |
| 4394 | 0, /*tp_dict*/ |
| 4395 | 0, /*tp_descr_get*/ |
| 4396 | 0, /*tp_descr_set*/ |
| 4397 | 0, /*tp_dictoffset*/ |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4398 | _pickle_Pickler___init__, /*tp_init*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4399 | PyType_GenericAlloc, /*tp_alloc*/ |
| 4400 | PyType_GenericNew, /*tp_new*/ |
| 4401 | PyObject_GC_Del, /*tp_free*/ |
| 4402 | 0, /*tp_is_gc*/ |
| 4403 | }; |
| 4404 | |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 4405 | /* Temporary helper for calling self.find_class(). |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4406 | |
| 4407 | XXX: It would be nice to able to avoid Python function call overhead, by |
| 4408 | using directly the C version of find_class(), when find_class() is not |
| 4409 | overridden by a subclass. Although, this could become rather hackish. A |
| 4410 | simpler optimization would be to call the C function when self is not a |
| 4411 | subclass instance. */ |
| 4412 | static PyObject * |
| 4413 | find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name) |
| 4414 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4415 | _Py_IDENTIFIER(find_class); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 4416 | |
| 4417 | return _PyObject_CallMethodId((PyObject *)self, &PyId_find_class, "OO", |
| 4418 | module_name, global_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4419 | } |
| 4420 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4421 | static Py_ssize_t |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4422 | marker(UnpicklerObject *self) |
| 4423 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4424 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4425 | if (self->num_marks < 1) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4426 | PyErr_SetString(st->UnpicklingError, "could not find MARK"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4427 | return -1; |
| 4428 | } |
| 4429 | |
| 4430 | return self->marks[--self->num_marks]; |
| 4431 | } |
| 4432 | |
| 4433 | static int |
| 4434 | load_none(UnpicklerObject *self) |
| 4435 | { |
| 4436 | PDATA_APPEND(self->stack, Py_None, -1); |
| 4437 | return 0; |
| 4438 | } |
| 4439 | |
| 4440 | static int |
| 4441 | bad_readline(void) |
| 4442 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4443 | PickleState *st = _Pickle_GetGlobalState(); |
| 4444 | PyErr_SetString(st->UnpicklingError, "pickle data was truncated"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4445 | return -1; |
| 4446 | } |
| 4447 | |
| 4448 | static int |
| 4449 | load_int(UnpicklerObject *self) |
| 4450 | { |
| 4451 | PyObject *value; |
| 4452 | char *endptr, *s; |
| 4453 | Py_ssize_t len; |
| 4454 | long x; |
| 4455 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4456 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4457 | return -1; |
| 4458 | if (len < 2) |
| 4459 | return bad_readline(); |
| 4460 | |
| 4461 | errno = 0; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 4462 | /* XXX: Should the base argument of strtol() be explicitly set to 10? |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4463 | XXX(avassalotti): Should this uses PyOS_strtol()? */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4464 | x = strtol(s, &endptr, 0); |
| 4465 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4466 | if (errno || (*endptr != '\n' && *endptr != '\0')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4467 | /* Hm, maybe we've got something long. Let's try reading |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4468 | * it as a Python int object. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4469 | errno = 0; |
| 4470 | /* XXX: Same thing about the base here. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4471 | value = PyLong_FromString(s, NULL, 0); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4472 | if (value == NULL) { |
| 4473 | PyErr_SetString(PyExc_ValueError, |
| 4474 | "could not convert string to int"); |
| 4475 | return -1; |
| 4476 | } |
| 4477 | } |
| 4478 | else { |
| 4479 | if (len == 3 && (x == 0 || x == 1)) { |
| 4480 | if ((value = PyBool_FromLong(x)) == NULL) |
| 4481 | return -1; |
| 4482 | } |
| 4483 | else { |
| 4484 | if ((value = PyLong_FromLong(x)) == NULL) |
| 4485 | return -1; |
| 4486 | } |
| 4487 | } |
| 4488 | |
| 4489 | PDATA_PUSH(self->stack, value, -1); |
| 4490 | return 0; |
| 4491 | } |
| 4492 | |
| 4493 | static int |
| 4494 | load_bool(UnpicklerObject *self, PyObject *boolean) |
| 4495 | { |
| 4496 | assert(boolean == Py_True || boolean == Py_False); |
| 4497 | PDATA_APPEND(self->stack, boolean, -1); |
| 4498 | return 0; |
| 4499 | } |
| 4500 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4501 | /* s contains x bytes of an unsigned little-endian integer. Return its value |
| 4502 | * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX. |
| 4503 | */ |
| 4504 | static Py_ssize_t |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4505 | calc_binsize(char *bytes, int nbytes) |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4506 | { |
| 4507 | unsigned char *s = (unsigned char *)bytes; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4508 | int i; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4509 | size_t x = 0; |
| 4510 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 4511 | for (i = 0; i < nbytes && i < sizeof(size_t); i++) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4512 | x |= (size_t) s[i] << (8 * i); |
| 4513 | } |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4514 | |
| 4515 | if (x > PY_SSIZE_T_MAX) |
| 4516 | return -1; |
| 4517 | else |
| 4518 | return (Py_ssize_t) x; |
| 4519 | } |
| 4520 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4521 | /* s contains x bytes of a little-endian integer. Return its value as a |
| 4522 | * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian |
| 4523 | * int, but when x is 4 it's a signed one. This is an historical source |
| 4524 | * of x-platform bugs. |
| 4525 | */ |
| 4526 | static long |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4527 | calc_binint(char *bytes, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4528 | { |
| 4529 | unsigned char *s = (unsigned char *)bytes; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4530 | int i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4531 | long x = 0; |
| 4532 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4533 | for (i = 0; i < nbytes; i++) { |
| 4534 | x |= (long)s[i] << (8 * i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4535 | } |
| 4536 | |
| 4537 | /* Unlike BININT1 and BININT2, BININT (more accurately BININT4) |
| 4538 | * is signed, so on a box with longs bigger than 4 bytes we need |
| 4539 | * to extend a BININT's sign bit to the full width. |
| 4540 | */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4541 | if (SIZEOF_LONG > 4 && nbytes == 4) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4542 | x |= -(x & (1L << 31)); |
| 4543 | } |
| 4544 | |
| 4545 | return x; |
| 4546 | } |
| 4547 | |
| 4548 | static int |
| 4549 | load_binintx(UnpicklerObject *self, char *s, int size) |
| 4550 | { |
| 4551 | PyObject *value; |
| 4552 | long x; |
| 4553 | |
| 4554 | x = calc_binint(s, size); |
| 4555 | |
| 4556 | if ((value = PyLong_FromLong(x)) == NULL) |
| 4557 | return -1; |
| 4558 | |
| 4559 | PDATA_PUSH(self->stack, value, -1); |
| 4560 | return 0; |
| 4561 | } |
| 4562 | |
| 4563 | static int |
| 4564 | load_binint(UnpicklerObject *self) |
| 4565 | { |
| 4566 | char *s; |
| 4567 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4568 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4569 | return -1; |
| 4570 | |
| 4571 | return load_binintx(self, s, 4); |
| 4572 | } |
| 4573 | |
| 4574 | static int |
| 4575 | load_binint1(UnpicklerObject *self) |
| 4576 | { |
| 4577 | char *s; |
| 4578 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4579 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4580 | return -1; |
| 4581 | |
| 4582 | return load_binintx(self, s, 1); |
| 4583 | } |
| 4584 | |
| 4585 | static int |
| 4586 | load_binint2(UnpicklerObject *self) |
| 4587 | { |
| 4588 | char *s; |
| 4589 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4590 | if (_Unpickler_Read(self, &s, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4591 | return -1; |
| 4592 | |
| 4593 | return load_binintx(self, s, 2); |
| 4594 | } |
| 4595 | |
| 4596 | static int |
| 4597 | load_long(UnpicklerObject *self) |
| 4598 | { |
| 4599 | PyObject *value; |
Alexandre Vassalotti | 446f7ff | 2009-01-23 04:43:46 +0000 | [diff] [blame] | 4600 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4601 | Py_ssize_t len; |
| 4602 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4603 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4604 | return -1; |
| 4605 | if (len < 2) |
| 4606 | return bad_readline(); |
| 4607 | |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 4608 | /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove |
| 4609 | the 'L' before calling PyLong_FromString. In order to maintain |
| 4610 | compatibility with Python 3.0.0, we don't actually *require* |
| 4611 | the 'L' to be present. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4612 | if (s[len-2] == 'L') |
Alexandre Vassalotti | 446f7ff | 2009-01-23 04:43:46 +0000 | [diff] [blame] | 4613 | s[len-2] = '\0'; |
Alexandre Vassalotti | e4bccb7 | 2009-01-24 01:47:57 +0000 | [diff] [blame] | 4614 | /* XXX: Should the base argument explicitly set to 10? */ |
| 4615 | value = PyLong_FromString(s, NULL, 0); |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 4616 | if (value == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4617 | return -1; |
| 4618 | |
| 4619 | PDATA_PUSH(self->stack, value, -1); |
| 4620 | return 0; |
| 4621 | } |
| 4622 | |
| 4623 | /* 'size' bytes contain the # of bytes of little-endian 256's-complement |
| 4624 | * data following. |
| 4625 | */ |
| 4626 | static int |
| 4627 | load_counted_long(UnpicklerObject *self, int size) |
| 4628 | { |
| 4629 | PyObject *value; |
| 4630 | char *nbytes; |
| 4631 | char *pdata; |
| 4632 | |
| 4633 | assert(size == 1 || size == 4); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4634 | if (_Unpickler_Read(self, &nbytes, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4635 | return -1; |
| 4636 | |
| 4637 | size = calc_binint(nbytes, size); |
| 4638 | if (size < 0) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4639 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4640 | /* Corrupt or hostile pickle -- we never write one like this */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4641 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4642 | "LONG pickle has negative byte count"); |
| 4643 | return -1; |
| 4644 | } |
| 4645 | |
| 4646 | if (size == 0) |
| 4647 | value = PyLong_FromLong(0L); |
| 4648 | else { |
| 4649 | /* Read the raw little-endian bytes and convert. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4650 | if (_Unpickler_Read(self, &pdata, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4651 | return -1; |
| 4652 | value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size, |
| 4653 | 1 /* little endian */ , 1 /* signed */ ); |
| 4654 | } |
| 4655 | if (value == NULL) |
| 4656 | return -1; |
| 4657 | PDATA_PUSH(self->stack, value, -1); |
| 4658 | return 0; |
| 4659 | } |
| 4660 | |
| 4661 | static int |
| 4662 | load_float(UnpicklerObject *self) |
| 4663 | { |
| 4664 | PyObject *value; |
| 4665 | char *endptr, *s; |
| 4666 | Py_ssize_t len; |
| 4667 | double d; |
| 4668 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4669 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4670 | return -1; |
| 4671 | if (len < 2) |
| 4672 | return bad_readline(); |
| 4673 | |
| 4674 | errno = 0; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4675 | d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError); |
| 4676 | if (d == -1.0 && PyErr_Occurred()) |
| 4677 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4678 | if ((endptr[0] != '\n') && (endptr[0] != '\0')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4679 | PyErr_SetString(PyExc_ValueError, "could not convert string to float"); |
| 4680 | return -1; |
| 4681 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4682 | value = PyFloat_FromDouble(d); |
| 4683 | if (value == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4684 | return -1; |
| 4685 | |
| 4686 | PDATA_PUSH(self->stack, value, -1); |
| 4687 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4688 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4689 | |
| 4690 | static int |
| 4691 | load_binfloat(UnpicklerObject *self) |
| 4692 | { |
| 4693 | PyObject *value; |
| 4694 | double x; |
| 4695 | char *s; |
| 4696 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4697 | if (_Unpickler_Read(self, &s, 8) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4698 | return -1; |
| 4699 | |
| 4700 | x = _PyFloat_Unpack8((unsigned char *)s, 0); |
| 4701 | if (x == -1.0 && PyErr_Occurred()) |
| 4702 | return -1; |
| 4703 | |
| 4704 | if ((value = PyFloat_FromDouble(x)) == NULL) |
| 4705 | return -1; |
| 4706 | |
| 4707 | PDATA_PUSH(self->stack, value, -1); |
| 4708 | return 0; |
| 4709 | } |
| 4710 | |
| 4711 | static int |
| 4712 | load_string(UnpicklerObject *self) |
| 4713 | { |
| 4714 | PyObject *bytes; |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4715 | PyObject *obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4716 | Py_ssize_t len; |
| 4717 | char *s, *p; |
| 4718 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4719 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4720 | return -1; |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4721 | /* Strip the newline */ |
| 4722 | len--; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4723 | /* Strip outermost quotes */ |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4724 | if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4725 | p = s + 1; |
| 4726 | len -= 2; |
| 4727 | } |
| 4728 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4729 | PickleState *st = _Pickle_GetGlobalState(); |
| 4730 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4731 | "the STRING opcode argument must be quoted"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4732 | return -1; |
| 4733 | } |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4734 | assert(len >= 0); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4735 | |
| 4736 | /* Use the PyBytes API to decode the string, since that is what is used |
| 4737 | to encode, and then coerce the result to Unicode. */ |
| 4738 | bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4739 | if (bytes == NULL) |
| 4740 | return -1; |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4741 | |
| 4742 | /* Leave the Python 2.x strings as bytes if the *encoding* given to the |
| 4743 | Unpickler was 'bytes'. Otherwise, convert them to unicode. */ |
| 4744 | if (strcmp(self->encoding, "bytes") == 0) { |
| 4745 | obj = bytes; |
| 4746 | } |
| 4747 | else { |
| 4748 | obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors); |
| 4749 | Py_DECREF(bytes); |
| 4750 | if (obj == NULL) { |
| 4751 | return -1; |
| 4752 | } |
| 4753 | } |
| 4754 | |
| 4755 | PDATA_PUSH(self->stack, obj, -1); |
| 4756 | return 0; |
| 4757 | } |
| 4758 | |
| 4759 | static int |
| 4760 | load_counted_binstring(UnpicklerObject *self, int nbytes) |
| 4761 | { |
| 4762 | PyObject *obj; |
| 4763 | Py_ssize_t size; |
| 4764 | char *s; |
| 4765 | |
| 4766 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4767 | return -1; |
| 4768 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4769 | size = calc_binsize(s, nbytes); |
| 4770 | if (size < 0) { |
| 4771 | PickleState *st = _Pickle_GetGlobalState(); |
| 4772 | PyErr_Format(st->UnpicklingError, |
| 4773 | "BINSTRING exceeds system's maximum size of %zd bytes", |
| 4774 | PY_SSIZE_T_MAX); |
| 4775 | return -1; |
| 4776 | } |
| 4777 | |
| 4778 | if (_Unpickler_Read(self, &s, size) < 0) |
| 4779 | return -1; |
| 4780 | |
| 4781 | /* Convert Python 2.x strings to bytes if the *encoding* given to the |
| 4782 | Unpickler was 'bytes'. Otherwise, convert them to unicode. */ |
| 4783 | if (strcmp(self->encoding, "bytes") == 0) { |
| 4784 | obj = PyBytes_FromStringAndSize(s, size); |
| 4785 | } |
| 4786 | else { |
| 4787 | obj = PyUnicode_Decode(s, size, self->encoding, self->errors); |
| 4788 | } |
| 4789 | if (obj == NULL) { |
| 4790 | return -1; |
| 4791 | } |
| 4792 | |
| 4793 | PDATA_PUSH(self->stack, obj, -1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4794 | return 0; |
| 4795 | } |
| 4796 | |
| 4797 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4798 | load_counted_binbytes(UnpicklerObject *self, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4799 | { |
| 4800 | PyObject *bytes; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4801 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4802 | char *s; |
| 4803 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4804 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4805 | return -1; |
| 4806 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4807 | size = calc_binsize(s, nbytes); |
| 4808 | if (size < 0) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4809 | PyErr_Format(PyExc_OverflowError, |
| 4810 | "BINBYTES exceeds system's maximum size of %zd bytes", |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 4811 | PY_SSIZE_T_MAX); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4812 | return -1; |
| 4813 | } |
| 4814 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4815 | if (_Unpickler_Read(self, &s, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4816 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4817 | |
| 4818 | bytes = PyBytes_FromStringAndSize(s, size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4819 | if (bytes == NULL) |
| 4820 | return -1; |
| 4821 | |
| 4822 | PDATA_PUSH(self->stack, bytes, -1); |
| 4823 | return 0; |
| 4824 | } |
| 4825 | |
| 4826 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4827 | load_unicode(UnpicklerObject *self) |
| 4828 | { |
| 4829 | PyObject *str; |
| 4830 | Py_ssize_t len; |
| 4831 | char *s; |
| 4832 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4833 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4834 | return -1; |
| 4835 | if (len < 1) |
| 4836 | return bad_readline(); |
| 4837 | |
| 4838 | str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL); |
| 4839 | if (str == NULL) |
| 4840 | return -1; |
| 4841 | |
| 4842 | PDATA_PUSH(self->stack, str, -1); |
| 4843 | return 0; |
| 4844 | } |
| 4845 | |
| 4846 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4847 | load_counted_binunicode(UnpicklerObject *self, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4848 | { |
| 4849 | PyObject *str; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4850 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4851 | char *s; |
| 4852 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4853 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4854 | return -1; |
| 4855 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4856 | size = calc_binsize(s, nbytes); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4857 | if (size < 0) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4858 | PyErr_Format(PyExc_OverflowError, |
| 4859 | "BINUNICODE exceeds system's maximum size of %zd bytes", |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 4860 | PY_SSIZE_T_MAX); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4861 | return -1; |
| 4862 | } |
| 4863 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4864 | if (_Unpickler_Read(self, &s, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4865 | return -1; |
| 4866 | |
Victor Stinner | 485fb56 | 2010-04-13 11:07:24 +0000 | [diff] [blame] | 4867 | str = PyUnicode_DecodeUTF8(s, size, "surrogatepass"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4868 | if (str == NULL) |
| 4869 | return -1; |
| 4870 | |
| 4871 | PDATA_PUSH(self->stack, str, -1); |
| 4872 | return 0; |
| 4873 | } |
| 4874 | |
| 4875 | static int |
| 4876 | load_tuple(UnpicklerObject *self) |
| 4877 | { |
| 4878 | PyObject *tuple; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4879 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4880 | |
| 4881 | if ((i = marker(self)) < 0) |
| 4882 | return -1; |
| 4883 | |
| 4884 | tuple = Pdata_poptuple(self->stack, i); |
| 4885 | if (tuple == NULL) |
| 4886 | return -1; |
| 4887 | PDATA_PUSH(self->stack, tuple, -1); |
| 4888 | return 0; |
| 4889 | } |
| 4890 | |
| 4891 | static int |
| 4892 | load_counted_tuple(UnpicklerObject *self, int len) |
| 4893 | { |
| 4894 | PyObject *tuple; |
| 4895 | |
| 4896 | tuple = PyTuple_New(len); |
| 4897 | if (tuple == NULL) |
| 4898 | return -1; |
| 4899 | |
| 4900 | while (--len >= 0) { |
| 4901 | PyObject *item; |
| 4902 | |
| 4903 | PDATA_POP(self->stack, item); |
| 4904 | if (item == NULL) |
| 4905 | return -1; |
| 4906 | PyTuple_SET_ITEM(tuple, len, item); |
| 4907 | } |
| 4908 | PDATA_PUSH(self->stack, tuple, -1); |
| 4909 | return 0; |
| 4910 | } |
| 4911 | |
| 4912 | static int |
| 4913 | load_empty_list(UnpicklerObject *self) |
| 4914 | { |
| 4915 | PyObject *list; |
| 4916 | |
| 4917 | if ((list = PyList_New(0)) == NULL) |
| 4918 | return -1; |
| 4919 | PDATA_PUSH(self->stack, list, -1); |
| 4920 | return 0; |
| 4921 | } |
| 4922 | |
| 4923 | static int |
| 4924 | load_empty_dict(UnpicklerObject *self) |
| 4925 | { |
| 4926 | PyObject *dict; |
| 4927 | |
| 4928 | if ((dict = PyDict_New()) == NULL) |
| 4929 | return -1; |
| 4930 | PDATA_PUSH(self->stack, dict, -1); |
| 4931 | return 0; |
| 4932 | } |
| 4933 | |
| 4934 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4935 | load_empty_set(UnpicklerObject *self) |
| 4936 | { |
| 4937 | PyObject *set; |
| 4938 | |
| 4939 | if ((set = PySet_New(NULL)) == NULL) |
| 4940 | return -1; |
| 4941 | PDATA_PUSH(self->stack, set, -1); |
| 4942 | return 0; |
| 4943 | } |
| 4944 | |
| 4945 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4946 | load_list(UnpicklerObject *self) |
| 4947 | { |
| 4948 | PyObject *list; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4949 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4950 | |
| 4951 | if ((i = marker(self)) < 0) |
| 4952 | return -1; |
| 4953 | |
| 4954 | list = Pdata_poplist(self->stack, i); |
| 4955 | if (list == NULL) |
| 4956 | return -1; |
| 4957 | PDATA_PUSH(self->stack, list, -1); |
| 4958 | return 0; |
| 4959 | } |
| 4960 | |
| 4961 | static int |
| 4962 | load_dict(UnpicklerObject *self) |
| 4963 | { |
| 4964 | PyObject *dict, *key, *value; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4965 | Py_ssize_t i, j, k; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4966 | |
| 4967 | if ((i = marker(self)) < 0) |
| 4968 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4969 | j = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4970 | |
| 4971 | if ((dict = PyDict_New()) == NULL) |
| 4972 | return -1; |
| 4973 | |
| 4974 | for (k = i + 1; k < j; k += 2) { |
| 4975 | key = self->stack->data[k - 1]; |
| 4976 | value = self->stack->data[k]; |
| 4977 | if (PyDict_SetItem(dict, key, value) < 0) { |
| 4978 | Py_DECREF(dict); |
| 4979 | return -1; |
| 4980 | } |
| 4981 | } |
| 4982 | Pdata_clear(self->stack, i); |
| 4983 | PDATA_PUSH(self->stack, dict, -1); |
| 4984 | return 0; |
| 4985 | } |
| 4986 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4987 | static int |
| 4988 | load_frozenset(UnpicklerObject *self) |
| 4989 | { |
| 4990 | PyObject *items; |
| 4991 | PyObject *frozenset; |
| 4992 | Py_ssize_t i; |
| 4993 | |
| 4994 | if ((i = marker(self)) < 0) |
| 4995 | return -1; |
| 4996 | |
| 4997 | items = Pdata_poptuple(self->stack, i); |
| 4998 | if (items == NULL) |
| 4999 | return -1; |
| 5000 | |
| 5001 | frozenset = PyFrozenSet_New(items); |
| 5002 | Py_DECREF(items); |
| 5003 | if (frozenset == NULL) |
| 5004 | return -1; |
| 5005 | |
| 5006 | PDATA_PUSH(self->stack, frozenset, -1); |
| 5007 | return 0; |
| 5008 | } |
| 5009 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5010 | static PyObject * |
| 5011 | instantiate(PyObject *cls, PyObject *args) |
| 5012 | { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5013 | PyObject *result = NULL; |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5014 | _Py_IDENTIFIER(__getinitargs__); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5015 | /* Caller must assure args are a tuple. Normally, args come from |
| 5016 | Pdata_poptuple which packs objects from the top of the stack |
| 5017 | into a newly created tuple. */ |
| 5018 | assert(PyTuple_Check(args)); |
| 5019 | if (Py_SIZE(args) > 0 || !PyType_Check(cls) || |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5020 | _PyObject_HasAttrId(cls, &PyId___getinitargs__)) { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5021 | result = PyObject_CallObject(cls, args); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5022 | } |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5023 | else { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5024 | _Py_IDENTIFIER(__new__); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 5025 | |
| 5026 | result = _PyObject_CallMethodId(cls, &PyId___new__, "O", cls); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5027 | } |
| 5028 | return result; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5029 | } |
| 5030 | |
| 5031 | static int |
| 5032 | load_obj(UnpicklerObject *self) |
| 5033 | { |
| 5034 | PyObject *cls, *args, *obj = NULL; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5035 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5036 | |
| 5037 | if ((i = marker(self)) < 0) |
| 5038 | return -1; |
| 5039 | |
| 5040 | args = Pdata_poptuple(self->stack, i + 1); |
| 5041 | if (args == NULL) |
| 5042 | return -1; |
| 5043 | |
| 5044 | PDATA_POP(self->stack, cls); |
| 5045 | if (cls) { |
| 5046 | obj = instantiate(cls, args); |
| 5047 | Py_DECREF(cls); |
| 5048 | } |
| 5049 | Py_DECREF(args); |
| 5050 | if (obj == NULL) |
| 5051 | return -1; |
| 5052 | |
| 5053 | PDATA_PUSH(self->stack, obj, -1); |
| 5054 | return 0; |
| 5055 | } |
| 5056 | |
| 5057 | static int |
| 5058 | load_inst(UnpicklerObject *self) |
| 5059 | { |
| 5060 | PyObject *cls = NULL; |
| 5061 | PyObject *args = NULL; |
| 5062 | PyObject *obj = NULL; |
| 5063 | PyObject *module_name; |
| 5064 | PyObject *class_name; |
| 5065 | Py_ssize_t len; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5066 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5067 | char *s; |
| 5068 | |
| 5069 | if ((i = marker(self)) < 0) |
| 5070 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5071 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5072 | return -1; |
| 5073 | if (len < 2) |
| 5074 | return bad_readline(); |
| 5075 | |
| 5076 | /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII |
| 5077 | identifiers are permitted in Python 3.0, since the INST opcode is only |
| 5078 | supported by older protocols on Python 2.x. */ |
| 5079 | module_name = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
| 5080 | if (module_name == NULL) |
| 5081 | return -1; |
| 5082 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5083 | if ((len = _Unpickler_Readline(self, &s)) >= 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5084 | if (len < 2) |
| 5085 | return bad_readline(); |
| 5086 | class_name = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5087 | if (class_name != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5088 | cls = find_class(self, module_name, class_name); |
| 5089 | Py_DECREF(class_name); |
| 5090 | } |
| 5091 | } |
| 5092 | Py_DECREF(module_name); |
| 5093 | |
| 5094 | if (cls == NULL) |
| 5095 | return -1; |
| 5096 | |
| 5097 | if ((args = Pdata_poptuple(self->stack, i)) != NULL) { |
| 5098 | obj = instantiate(cls, args); |
| 5099 | Py_DECREF(args); |
| 5100 | } |
| 5101 | Py_DECREF(cls); |
| 5102 | |
| 5103 | if (obj == NULL) |
| 5104 | return -1; |
| 5105 | |
| 5106 | PDATA_PUSH(self->stack, obj, -1); |
| 5107 | return 0; |
| 5108 | } |
| 5109 | |
| 5110 | static int |
| 5111 | load_newobj(UnpicklerObject *self) |
| 5112 | { |
| 5113 | PyObject *args = NULL; |
| 5114 | PyObject *clsraw = NULL; |
| 5115 | PyTypeObject *cls; /* clsraw cast to its true type */ |
| 5116 | PyObject *obj; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5117 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5118 | |
| 5119 | /* Stack is ... cls argtuple, and we want to call |
| 5120 | * cls.__new__(cls, *argtuple). |
| 5121 | */ |
| 5122 | PDATA_POP(self->stack, args); |
| 5123 | if (args == NULL) |
| 5124 | goto error; |
| 5125 | if (!PyTuple_Check(args)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5126 | PyErr_SetString(st->UnpicklingError, |
| 5127 | "NEWOBJ expected an arg " "tuple."); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5128 | goto error; |
| 5129 | } |
| 5130 | |
| 5131 | PDATA_POP(self->stack, clsraw); |
| 5132 | cls = (PyTypeObject *)clsraw; |
| 5133 | if (cls == NULL) |
| 5134 | goto error; |
| 5135 | if (!PyType_Check(cls)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5136 | PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5137 | "isn't a type object"); |
| 5138 | goto error; |
| 5139 | } |
| 5140 | if (cls->tp_new == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5141 | PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5142 | "has NULL tp_new"); |
| 5143 | goto error; |
| 5144 | } |
| 5145 | |
| 5146 | /* Call __new__. */ |
| 5147 | obj = cls->tp_new(cls, args, NULL); |
| 5148 | if (obj == NULL) |
| 5149 | goto error; |
| 5150 | |
| 5151 | Py_DECREF(args); |
| 5152 | Py_DECREF(clsraw); |
| 5153 | PDATA_PUSH(self->stack, obj, -1); |
| 5154 | return 0; |
| 5155 | |
| 5156 | error: |
| 5157 | Py_XDECREF(args); |
| 5158 | Py_XDECREF(clsraw); |
| 5159 | return -1; |
| 5160 | } |
| 5161 | |
| 5162 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5163 | load_newobj_ex(UnpicklerObject *self) |
| 5164 | { |
| 5165 | PyObject *cls, *args, *kwargs; |
| 5166 | PyObject *obj; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5167 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5168 | |
| 5169 | PDATA_POP(self->stack, kwargs); |
| 5170 | if (kwargs == NULL) { |
| 5171 | return -1; |
| 5172 | } |
| 5173 | PDATA_POP(self->stack, args); |
| 5174 | if (args == NULL) { |
| 5175 | Py_DECREF(kwargs); |
| 5176 | return -1; |
| 5177 | } |
| 5178 | PDATA_POP(self->stack, cls); |
| 5179 | if (cls == NULL) { |
| 5180 | Py_DECREF(kwargs); |
| 5181 | Py_DECREF(args); |
| 5182 | return -1; |
| 5183 | } |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 5184 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5185 | if (!PyType_Check(cls)) { |
| 5186 | Py_DECREF(kwargs); |
| 5187 | Py_DECREF(args); |
| 5188 | Py_DECREF(cls); |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 5189 | PyErr_Format(st->UnpicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5190 | "NEWOBJ_EX class argument must be a type, not %.200s", |
| 5191 | Py_TYPE(cls)->tp_name); |
| 5192 | return -1; |
| 5193 | } |
| 5194 | |
| 5195 | if (((PyTypeObject *)cls)->tp_new == NULL) { |
| 5196 | Py_DECREF(kwargs); |
| 5197 | Py_DECREF(args); |
| 5198 | Py_DECREF(cls); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5199 | PyErr_SetString(st->UnpicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5200 | "NEWOBJ_EX class argument doesn't have __new__"); |
| 5201 | return -1; |
| 5202 | } |
| 5203 | obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs); |
| 5204 | Py_DECREF(kwargs); |
| 5205 | Py_DECREF(args); |
| 5206 | Py_DECREF(cls); |
| 5207 | if (obj == NULL) { |
| 5208 | return -1; |
| 5209 | } |
| 5210 | PDATA_PUSH(self->stack, obj, -1); |
| 5211 | return 0; |
| 5212 | } |
| 5213 | |
| 5214 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5215 | load_global(UnpicklerObject *self) |
| 5216 | { |
| 5217 | PyObject *global = NULL; |
| 5218 | PyObject *module_name; |
| 5219 | PyObject *global_name; |
| 5220 | Py_ssize_t len; |
| 5221 | char *s; |
| 5222 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5223 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5224 | return -1; |
| 5225 | if (len < 2) |
| 5226 | return bad_readline(); |
| 5227 | module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict"); |
| 5228 | if (!module_name) |
| 5229 | return -1; |
| 5230 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5231 | if ((len = _Unpickler_Readline(self, &s)) >= 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5232 | if (len < 2) { |
| 5233 | Py_DECREF(module_name); |
| 5234 | return bad_readline(); |
| 5235 | } |
| 5236 | global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict"); |
| 5237 | if (global_name) { |
| 5238 | global = find_class(self, module_name, global_name); |
| 5239 | Py_DECREF(global_name); |
| 5240 | } |
| 5241 | } |
| 5242 | Py_DECREF(module_name); |
| 5243 | |
| 5244 | if (global == NULL) |
| 5245 | return -1; |
| 5246 | PDATA_PUSH(self->stack, global, -1); |
| 5247 | return 0; |
| 5248 | } |
| 5249 | |
| 5250 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5251 | load_stack_global(UnpicklerObject *self) |
| 5252 | { |
| 5253 | PyObject *global; |
| 5254 | PyObject *module_name; |
| 5255 | PyObject *global_name; |
| 5256 | |
| 5257 | PDATA_POP(self->stack, global_name); |
| 5258 | PDATA_POP(self->stack, module_name); |
| 5259 | if (module_name == NULL || !PyUnicode_CheckExact(module_name) || |
| 5260 | global_name == NULL || !PyUnicode_CheckExact(global_name)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5261 | PickleState *st = _Pickle_GetGlobalState(); |
| 5262 | PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str"); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5263 | Py_XDECREF(global_name); |
| 5264 | Py_XDECREF(module_name); |
| 5265 | return -1; |
| 5266 | } |
| 5267 | global = find_class(self, module_name, global_name); |
| 5268 | Py_DECREF(global_name); |
| 5269 | Py_DECREF(module_name); |
| 5270 | if (global == NULL) |
| 5271 | return -1; |
| 5272 | PDATA_PUSH(self->stack, global, -1); |
| 5273 | return 0; |
| 5274 | } |
| 5275 | |
| 5276 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5277 | load_persid(UnpicklerObject *self) |
| 5278 | { |
| 5279 | PyObject *pid; |
| 5280 | Py_ssize_t len; |
| 5281 | char *s; |
| 5282 | |
| 5283 | if (self->pers_func) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5284 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5285 | return -1; |
Alexandre Vassalotti | 896414f | 2013-11-30 13:52:35 -0800 | [diff] [blame] | 5286 | if (len < 1) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5287 | return bad_readline(); |
| 5288 | |
| 5289 | pid = PyBytes_FromStringAndSize(s, len - 1); |
| 5290 | if (pid == NULL) |
| 5291 | return -1; |
| 5292 | |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5293 | /* This does not leak since _Pickle_FastCall() steals the reference |
| 5294 | to pid first. */ |
| 5295 | pid = _Pickle_FastCall(self->pers_func, pid); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5296 | if (pid == NULL) |
| 5297 | return -1; |
| 5298 | |
| 5299 | PDATA_PUSH(self->stack, pid, -1); |
| 5300 | return 0; |
| 5301 | } |
| 5302 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5303 | PickleState *st = _Pickle_GetGlobalState(); |
| 5304 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5305 | "A load persistent id instruction was encountered,\n" |
| 5306 | "but no persistent_load function was specified."); |
| 5307 | return -1; |
| 5308 | } |
| 5309 | } |
| 5310 | |
| 5311 | static int |
| 5312 | load_binpersid(UnpicklerObject *self) |
| 5313 | { |
| 5314 | PyObject *pid; |
| 5315 | |
| 5316 | if (self->pers_func) { |
| 5317 | PDATA_POP(self->stack, pid); |
| 5318 | if (pid == NULL) |
| 5319 | return -1; |
| 5320 | |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5321 | /* This does not leak since _Pickle_FastCall() steals the |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5322 | reference to pid first. */ |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5323 | pid = _Pickle_FastCall(self->pers_func, pid); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5324 | if (pid == NULL) |
| 5325 | return -1; |
| 5326 | |
| 5327 | PDATA_PUSH(self->stack, pid, -1); |
| 5328 | return 0; |
| 5329 | } |
| 5330 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5331 | PickleState *st = _Pickle_GetGlobalState(); |
| 5332 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5333 | "A load persistent id instruction was encountered,\n" |
| 5334 | "but no persistent_load function was specified."); |
| 5335 | return -1; |
| 5336 | } |
| 5337 | } |
| 5338 | |
| 5339 | static int |
| 5340 | load_pop(UnpicklerObject *self) |
| 5341 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5342 | Py_ssize_t len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5343 | |
| 5344 | /* Note that we split the (pickle.py) stack into two stacks, |
| 5345 | * an object stack and a mark stack. We have to be clever and |
| 5346 | * 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] | 5347 | * mark stack first, and only signalling a stack underflow if |
| 5348 | * the object stack is empty and the mark stack doesn't match |
| 5349 | * our expectations. |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5350 | */ |
Collin Winter | 8ca69de | 2009-05-26 16:53:41 +0000 | [diff] [blame] | 5351 | if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5352 | self->num_marks--; |
Antoine Pitrou | 01a15ea | 2010-01-07 17:57:31 +0000 | [diff] [blame] | 5353 | } else if (len > 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5354 | len--; |
| 5355 | Py_DECREF(self->stack->data[len]); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5356 | Py_SIZE(self->stack) = len; |
Collin Winter | 8ca69de | 2009-05-26 16:53:41 +0000 | [diff] [blame] | 5357 | } else { |
| 5358 | return stack_underflow(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5359 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5360 | return 0; |
| 5361 | } |
| 5362 | |
| 5363 | static int |
| 5364 | load_pop_mark(UnpicklerObject *self) |
| 5365 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5366 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5367 | |
| 5368 | if ((i = marker(self)) < 0) |
| 5369 | return -1; |
| 5370 | |
| 5371 | Pdata_clear(self->stack, i); |
| 5372 | |
| 5373 | return 0; |
| 5374 | } |
| 5375 | |
| 5376 | static int |
| 5377 | load_dup(UnpicklerObject *self) |
| 5378 | { |
| 5379 | PyObject *last; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5380 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5381 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5382 | if ((len = Py_SIZE(self->stack)) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5383 | return stack_underflow(); |
| 5384 | last = self->stack->data[len - 1]; |
| 5385 | PDATA_APPEND(self->stack, last, -1); |
| 5386 | return 0; |
| 5387 | } |
| 5388 | |
| 5389 | static int |
| 5390 | load_get(UnpicklerObject *self) |
| 5391 | { |
| 5392 | PyObject *key, *value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5393 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5394 | Py_ssize_t len; |
| 5395 | char *s; |
| 5396 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5397 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5398 | return -1; |
| 5399 | if (len < 2) |
| 5400 | return bad_readline(); |
| 5401 | |
| 5402 | key = PyLong_FromString(s, NULL, 10); |
| 5403 | if (key == NULL) |
| 5404 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5405 | idx = PyLong_AsSsize_t(key); |
| 5406 | if (idx == -1 && PyErr_Occurred()) { |
| 5407 | Py_DECREF(key); |
| 5408 | return -1; |
| 5409 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5410 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5411 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5412 | if (value == NULL) { |
| 5413 | if (!PyErr_Occurred()) |
| 5414 | PyErr_SetObject(PyExc_KeyError, key); |
| 5415 | Py_DECREF(key); |
| 5416 | return -1; |
| 5417 | } |
| 5418 | Py_DECREF(key); |
| 5419 | |
| 5420 | PDATA_APPEND(self->stack, value, -1); |
| 5421 | return 0; |
| 5422 | } |
| 5423 | |
| 5424 | static int |
| 5425 | load_binget(UnpicklerObject *self) |
| 5426 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5427 | PyObject *value; |
| 5428 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5429 | char *s; |
| 5430 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5431 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5432 | return -1; |
| 5433 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5434 | idx = Py_CHARMASK(s[0]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5435 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5436 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5437 | if (value == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5438 | PyObject *key = PyLong_FromSsize_t(idx); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5439 | if (key != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5440 | PyErr_SetObject(PyExc_KeyError, key); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5441 | Py_DECREF(key); |
| 5442 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5443 | return -1; |
| 5444 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5445 | |
| 5446 | PDATA_APPEND(self->stack, value, -1); |
| 5447 | return 0; |
| 5448 | } |
| 5449 | |
| 5450 | static int |
| 5451 | load_long_binget(UnpicklerObject *self) |
| 5452 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5453 | PyObject *value; |
| 5454 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5455 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5456 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5457 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5458 | return -1; |
| 5459 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5460 | idx = calc_binsize(s, 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5461 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5462 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5463 | if (value == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5464 | PyObject *key = PyLong_FromSsize_t(idx); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5465 | if (key != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5466 | PyErr_SetObject(PyExc_KeyError, key); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5467 | Py_DECREF(key); |
| 5468 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5469 | return -1; |
| 5470 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5471 | |
| 5472 | PDATA_APPEND(self->stack, value, -1); |
| 5473 | return 0; |
| 5474 | } |
| 5475 | |
| 5476 | /* Push an object from the extension registry (EXT[124]). nbytes is |
| 5477 | * the number of bytes following the opcode, holding the index (code) value. |
| 5478 | */ |
| 5479 | static int |
| 5480 | load_extension(UnpicklerObject *self, int nbytes) |
| 5481 | { |
| 5482 | char *codebytes; /* the nbytes bytes after the opcode */ |
| 5483 | long code; /* calc_binint returns long */ |
| 5484 | PyObject *py_code; /* code as a Python int */ |
| 5485 | PyObject *obj; /* the object to push */ |
| 5486 | PyObject *pair; /* (module_name, class_name) */ |
| 5487 | PyObject *module_name, *class_name; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5488 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5489 | |
| 5490 | assert(nbytes == 1 || nbytes == 2 || nbytes == 4); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5491 | if (_Unpickler_Read(self, &codebytes, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5492 | return -1; |
| 5493 | code = calc_binint(codebytes, nbytes); |
| 5494 | if (code <= 0) { /* note that 0 is forbidden */ |
| 5495 | /* Corrupt or hostile pickle. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5496 | PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5497 | return -1; |
| 5498 | } |
| 5499 | |
| 5500 | /* Look for the code in the cache. */ |
| 5501 | py_code = PyLong_FromLong(code); |
| 5502 | if (py_code == NULL) |
| 5503 | return -1; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5504 | obj = PyDict_GetItemWithError(st->extension_cache, py_code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5505 | if (obj != NULL) { |
| 5506 | /* Bingo. */ |
| 5507 | Py_DECREF(py_code); |
| 5508 | PDATA_APPEND(self->stack, obj, -1); |
| 5509 | return 0; |
| 5510 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5511 | if (PyErr_Occurred()) { |
| 5512 | Py_DECREF(py_code); |
| 5513 | return -1; |
| 5514 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5515 | |
| 5516 | /* Look up the (module_name, class_name) pair. */ |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5517 | pair = PyDict_GetItemWithError(st->inverted_registry, py_code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5518 | if (pair == NULL) { |
| 5519 | Py_DECREF(py_code); |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5520 | if (!PyErr_Occurred()) { |
| 5521 | PyErr_Format(PyExc_ValueError, "unregistered extension " |
| 5522 | "code %ld", code); |
| 5523 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5524 | return -1; |
| 5525 | } |
| 5526 | /* Since the extension registry is manipulable via Python code, |
| 5527 | * confirm that pair is really a 2-tuple of strings. |
| 5528 | */ |
| 5529 | if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 || |
| 5530 | !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) || |
| 5531 | !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) { |
| 5532 | Py_DECREF(py_code); |
| 5533 | PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] " |
| 5534 | "isn't a 2-tuple of strings", code); |
| 5535 | return -1; |
| 5536 | } |
| 5537 | /* Load the object. */ |
| 5538 | obj = find_class(self, module_name, class_name); |
| 5539 | if (obj == NULL) { |
| 5540 | Py_DECREF(py_code); |
| 5541 | return -1; |
| 5542 | } |
| 5543 | /* Cache code -> obj. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5544 | code = PyDict_SetItem(st->extension_cache, py_code, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5545 | Py_DECREF(py_code); |
| 5546 | if (code < 0) { |
| 5547 | Py_DECREF(obj); |
| 5548 | return -1; |
| 5549 | } |
| 5550 | PDATA_PUSH(self->stack, obj, -1); |
| 5551 | return 0; |
| 5552 | } |
| 5553 | |
| 5554 | static int |
| 5555 | load_put(UnpicklerObject *self) |
| 5556 | { |
| 5557 | PyObject *key, *value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5558 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5559 | Py_ssize_t len; |
| 5560 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5561 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5562 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5563 | return -1; |
| 5564 | if (len < 2) |
| 5565 | return bad_readline(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5566 | if (Py_SIZE(self->stack) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5567 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5568 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5569 | |
| 5570 | key = PyLong_FromString(s, NULL, 10); |
| 5571 | if (key == NULL) |
| 5572 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5573 | idx = PyLong_AsSsize_t(key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5574 | Py_DECREF(key); |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5575 | if (idx < 0) { |
| 5576 | if (!PyErr_Occurred()) |
| 5577 | PyErr_SetString(PyExc_ValueError, |
| 5578 | "negative PUT argument"); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5579 | return -1; |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5580 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5581 | |
| 5582 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5583 | } |
| 5584 | |
| 5585 | static int |
| 5586 | load_binput(UnpicklerObject *self) |
| 5587 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5588 | PyObject *value; |
| 5589 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5590 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5591 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5592 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5593 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5594 | |
| 5595 | if (Py_SIZE(self->stack) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5596 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5597 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
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 | idx = Py_CHARMASK(s[0]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5600 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5601 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5602 | } |
| 5603 | |
| 5604 | static int |
| 5605 | load_long_binput(UnpicklerObject *self) |
| 5606 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5607 | PyObject *value; |
| 5608 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5609 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5610 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5611 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5612 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5613 | |
| 5614 | if (Py_SIZE(self->stack) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5615 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5616 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5617 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5618 | idx = calc_binsize(s, 4); |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5619 | if (idx < 0) { |
| 5620 | PyErr_SetString(PyExc_ValueError, |
| 5621 | "negative LONG_BINPUT argument"); |
| 5622 | return -1; |
| 5623 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5624 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5625 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5626 | } |
| 5627 | |
| 5628 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5629 | load_memoize(UnpicklerObject *self) |
| 5630 | { |
| 5631 | PyObject *value; |
| 5632 | |
| 5633 | if (Py_SIZE(self->stack) <= 0) |
| 5634 | return stack_underflow(); |
| 5635 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
| 5636 | |
| 5637 | return _Unpickler_MemoPut(self, self->memo_len, value); |
| 5638 | } |
| 5639 | |
| 5640 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5641 | do_append(UnpicklerObject *self, Py_ssize_t x) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5642 | { |
| 5643 | PyObject *value; |
| 5644 | PyObject *list; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5645 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5646 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5647 | len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5648 | if (x > len || x <= 0) |
| 5649 | return stack_underflow(); |
| 5650 | if (len == x) /* nothing to do */ |
| 5651 | return 0; |
| 5652 | |
| 5653 | list = self->stack->data[x - 1]; |
| 5654 | |
| 5655 | if (PyList_Check(list)) { |
| 5656 | PyObject *slice; |
| 5657 | Py_ssize_t list_len; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5658 | int ret; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5659 | |
| 5660 | slice = Pdata_poplist(self->stack, x); |
| 5661 | if (!slice) |
| 5662 | return -1; |
| 5663 | list_len = PyList_GET_SIZE(list); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5664 | ret = PyList_SetSlice(list, list_len, list_len, slice); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5665 | Py_DECREF(slice); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5666 | return ret; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5667 | } |
| 5668 | else { |
| 5669 | PyObject *append_func; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5670 | _Py_IDENTIFIER(append); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5671 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 5672 | append_func = _PyObject_GetAttrId(list, &PyId_append); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5673 | if (append_func == NULL) |
| 5674 | return -1; |
| 5675 | for (i = x; i < len; i++) { |
| 5676 | PyObject *result; |
| 5677 | |
| 5678 | value = self->stack->data[i]; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5679 | result = _Pickle_FastCall(append_func, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5680 | if (result == NULL) { |
| 5681 | Pdata_clear(self->stack, i + 1); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5682 | Py_SIZE(self->stack) = x; |
Alexandre Vassalotti | 637c7c4 | 2013-04-20 21:28:21 -0700 | [diff] [blame] | 5683 | Py_DECREF(append_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5684 | return -1; |
| 5685 | } |
| 5686 | Py_DECREF(result); |
| 5687 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5688 | Py_SIZE(self->stack) = x; |
Alexandre Vassalotti | 637c7c4 | 2013-04-20 21:28:21 -0700 | [diff] [blame] | 5689 | Py_DECREF(append_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5690 | } |
| 5691 | |
| 5692 | return 0; |
| 5693 | } |
| 5694 | |
| 5695 | static int |
| 5696 | load_append(UnpicklerObject *self) |
| 5697 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5698 | return do_append(self, Py_SIZE(self->stack) - 1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5699 | } |
| 5700 | |
| 5701 | static int |
| 5702 | load_appends(UnpicklerObject *self) |
| 5703 | { |
| 5704 | return do_append(self, marker(self)); |
| 5705 | } |
| 5706 | |
| 5707 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5708 | do_setitems(UnpicklerObject *self, Py_ssize_t x) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5709 | { |
| 5710 | PyObject *value, *key; |
| 5711 | PyObject *dict; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5712 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5713 | int status = 0; |
| 5714 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5715 | len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5716 | if (x > len || x <= 0) |
| 5717 | return stack_underflow(); |
| 5718 | if (len == x) /* nothing to do */ |
| 5719 | return 0; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 5720 | if ((len - x) % 2 != 0) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5721 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5722 | /* Currupt or hostile pickle -- we never write one like this. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5723 | PyErr_SetString(st->UnpicklingError, |
| 5724 | "odd number of items for SETITEMS"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5725 | return -1; |
| 5726 | } |
| 5727 | |
| 5728 | /* Here, dict does not actually need to be a PyDict; it could be anything |
| 5729 | that supports the __setitem__ attribute. */ |
| 5730 | dict = self->stack->data[x - 1]; |
| 5731 | |
| 5732 | for (i = x + 1; i < len; i += 2) { |
| 5733 | key = self->stack->data[i - 1]; |
| 5734 | value = self->stack->data[i]; |
| 5735 | if (PyObject_SetItem(dict, key, value) < 0) { |
| 5736 | status = -1; |
| 5737 | break; |
| 5738 | } |
| 5739 | } |
| 5740 | |
| 5741 | Pdata_clear(self->stack, x); |
| 5742 | return status; |
| 5743 | } |
| 5744 | |
| 5745 | static int |
| 5746 | load_setitem(UnpicklerObject *self) |
| 5747 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5748 | return do_setitems(self, Py_SIZE(self->stack) - 2); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5749 | } |
| 5750 | |
| 5751 | static int |
| 5752 | load_setitems(UnpicklerObject *self) |
| 5753 | { |
| 5754 | return do_setitems(self, marker(self)); |
| 5755 | } |
| 5756 | |
| 5757 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5758 | load_additems(UnpicklerObject *self) |
| 5759 | { |
| 5760 | PyObject *set; |
| 5761 | Py_ssize_t mark, len, i; |
| 5762 | |
| 5763 | mark = marker(self); |
| 5764 | len = Py_SIZE(self->stack); |
| 5765 | if (mark > len || mark <= 0) |
| 5766 | return stack_underflow(); |
| 5767 | if (len == mark) /* nothing to do */ |
| 5768 | return 0; |
| 5769 | |
| 5770 | set = self->stack->data[mark - 1]; |
| 5771 | |
| 5772 | if (PySet_Check(set)) { |
| 5773 | PyObject *items; |
| 5774 | int status; |
| 5775 | |
| 5776 | items = Pdata_poptuple(self->stack, mark); |
| 5777 | if (items == NULL) |
| 5778 | return -1; |
| 5779 | |
| 5780 | status = _PySet_Update(set, items); |
| 5781 | Py_DECREF(items); |
| 5782 | return status; |
| 5783 | } |
| 5784 | else { |
| 5785 | PyObject *add_func; |
| 5786 | _Py_IDENTIFIER(add); |
| 5787 | |
| 5788 | add_func = _PyObject_GetAttrId(set, &PyId_add); |
| 5789 | if (add_func == NULL) |
| 5790 | return -1; |
| 5791 | for (i = mark; i < len; i++) { |
| 5792 | PyObject *result; |
| 5793 | PyObject *item; |
| 5794 | |
| 5795 | item = self->stack->data[i]; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5796 | result = _Pickle_FastCall(add_func, item); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5797 | if (result == NULL) { |
| 5798 | Pdata_clear(self->stack, i + 1); |
| 5799 | Py_SIZE(self->stack) = mark; |
| 5800 | return -1; |
| 5801 | } |
| 5802 | Py_DECREF(result); |
| 5803 | } |
| 5804 | Py_SIZE(self->stack) = mark; |
| 5805 | } |
| 5806 | |
| 5807 | return 0; |
| 5808 | } |
| 5809 | |
| 5810 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5811 | load_build(UnpicklerObject *self) |
| 5812 | { |
| 5813 | PyObject *state, *inst, *slotstate; |
| 5814 | PyObject *setstate; |
| 5815 | int status = 0; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5816 | _Py_IDENTIFIER(__setstate__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5817 | |
| 5818 | /* Stack is ... instance, state. We want to leave instance at |
| 5819 | * the stack top, possibly mutated via instance.__setstate__(state). |
| 5820 | */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5821 | if (Py_SIZE(self->stack) < 2) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5822 | return stack_underflow(); |
| 5823 | |
| 5824 | PDATA_POP(self->stack, state); |
| 5825 | if (state == NULL) |
| 5826 | return -1; |
| 5827 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5828 | inst = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5829 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 5830 | setstate = _PyObject_GetAttrId(inst, &PyId___setstate__); |
Alexandre Vassalotti | 1f9d907 | 2008-08-15 03:07:47 +0000 | [diff] [blame] | 5831 | if (setstate == NULL) { |
| 5832 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 5833 | PyErr_Clear(); |
Antoine Pitrou | d79dc62 | 2008-09-05 00:03:33 +0000 | [diff] [blame] | 5834 | else { |
| 5835 | Py_DECREF(state); |
Alexandre Vassalotti | 1f9d907 | 2008-08-15 03:07:47 +0000 | [diff] [blame] | 5836 | return -1; |
Antoine Pitrou | d79dc62 | 2008-09-05 00:03:33 +0000 | [diff] [blame] | 5837 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5838 | } |
| 5839 | else { |
| 5840 | PyObject *result; |
| 5841 | |
| 5842 | /* The explicit __setstate__ is responsible for everything. */ |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5843 | result = _Pickle_FastCall(setstate, state); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5844 | Py_DECREF(setstate); |
| 5845 | if (result == NULL) |
| 5846 | return -1; |
| 5847 | Py_DECREF(result); |
| 5848 | return 0; |
| 5849 | } |
| 5850 | |
| 5851 | /* A default __setstate__. First see whether state embeds a |
| 5852 | * slot state dict too (a proto 2 addition). |
| 5853 | */ |
| 5854 | if (PyTuple_Check(state) && Py_SIZE(state) == 2) { |
| 5855 | PyObject *tmp = state; |
| 5856 | |
| 5857 | state = PyTuple_GET_ITEM(tmp, 0); |
| 5858 | slotstate = PyTuple_GET_ITEM(tmp, 1); |
| 5859 | Py_INCREF(state); |
| 5860 | Py_INCREF(slotstate); |
| 5861 | Py_DECREF(tmp); |
| 5862 | } |
| 5863 | else |
| 5864 | slotstate = NULL; |
| 5865 | |
| 5866 | /* Set inst.__dict__ from the state dict (if any). */ |
| 5867 | if (state != Py_None) { |
| 5868 | PyObject *dict; |
Antoine Pitrou | a9f48a0 | 2009-05-02 21:41:14 +0000 | [diff] [blame] | 5869 | PyObject *d_key, *d_value; |
| 5870 | Py_ssize_t i; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5871 | _Py_IDENTIFIER(__dict__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5872 | |
| 5873 | if (!PyDict_Check(state)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5874 | PickleState *st = _Pickle_GetGlobalState(); |
| 5875 | PyErr_SetString(st->UnpicklingError, "state is not a dictionary"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5876 | goto error; |
| 5877 | } |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 5878 | dict = _PyObject_GetAttrId(inst, &PyId___dict__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5879 | if (dict == NULL) |
| 5880 | goto error; |
| 5881 | |
Antoine Pitrou | a9f48a0 | 2009-05-02 21:41:14 +0000 | [diff] [blame] | 5882 | i = 0; |
| 5883 | while (PyDict_Next(state, &i, &d_key, &d_value)) { |
| 5884 | /* normally the keys for instance attributes are |
| 5885 | interned. we should try to do that here. */ |
| 5886 | Py_INCREF(d_key); |
| 5887 | if (PyUnicode_CheckExact(d_key)) |
| 5888 | PyUnicode_InternInPlace(&d_key); |
| 5889 | if (PyObject_SetItem(dict, d_key, d_value) < 0) { |
| 5890 | Py_DECREF(d_key); |
| 5891 | goto error; |
| 5892 | } |
| 5893 | Py_DECREF(d_key); |
| 5894 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5895 | Py_DECREF(dict); |
| 5896 | } |
| 5897 | |
| 5898 | /* Also set instance attributes from the slotstate dict (if any). */ |
| 5899 | if (slotstate != NULL) { |
| 5900 | PyObject *d_key, *d_value; |
| 5901 | Py_ssize_t i; |
| 5902 | |
| 5903 | if (!PyDict_Check(slotstate)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5904 | PickleState *st = _Pickle_GetGlobalState(); |
| 5905 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5906 | "slot state is not a dictionary"); |
| 5907 | goto error; |
| 5908 | } |
| 5909 | i = 0; |
| 5910 | while (PyDict_Next(slotstate, &i, &d_key, &d_value)) { |
| 5911 | if (PyObject_SetAttr(inst, d_key, d_value) < 0) |
| 5912 | goto error; |
| 5913 | } |
| 5914 | } |
| 5915 | |
| 5916 | if (0) { |
| 5917 | error: |
| 5918 | status = -1; |
| 5919 | } |
| 5920 | |
| 5921 | Py_DECREF(state); |
| 5922 | Py_XDECREF(slotstate); |
| 5923 | return status; |
| 5924 | } |
| 5925 | |
| 5926 | static int |
| 5927 | load_mark(UnpicklerObject *self) |
| 5928 | { |
| 5929 | |
| 5930 | /* Note that we split the (pickle.py) stack into two stacks, an |
| 5931 | * object stack and a mark stack. Here we push a mark onto the |
| 5932 | * mark stack. |
| 5933 | */ |
| 5934 | |
| 5935 | if ((self->num_marks + 1) >= self->marks_size) { |
| 5936 | size_t alloc; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5937 | Py_ssize_t *marks; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5938 | |
| 5939 | /* Use the size_t type to check for overflow. */ |
| 5940 | alloc = ((size_t)self->num_marks << 1) + 20; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5941 | if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) || |
Alexandre Vassalotti | 7634ff5 | 2008-06-13 02:16:06 +0000 | [diff] [blame] | 5942 | alloc <= ((size_t)self->num_marks + 1)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5943 | PyErr_NoMemory(); |
| 5944 | return -1; |
| 5945 | } |
| 5946 | |
| 5947 | if (self->marks == NULL) |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5948 | marks = (Py_ssize_t *) PyMem_Malloc(alloc * sizeof(Py_ssize_t)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5949 | else |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5950 | marks = (Py_ssize_t *) PyMem_Realloc(self->marks, |
| 5951 | alloc * sizeof(Py_ssize_t)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5952 | if (marks == NULL) { |
| 5953 | PyErr_NoMemory(); |
| 5954 | return -1; |
| 5955 | } |
| 5956 | self->marks = marks; |
| 5957 | self->marks_size = (Py_ssize_t)alloc; |
| 5958 | } |
| 5959 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5960 | self->marks[self->num_marks++] = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5961 | |
| 5962 | return 0; |
| 5963 | } |
| 5964 | |
| 5965 | static int |
| 5966 | load_reduce(UnpicklerObject *self) |
| 5967 | { |
| 5968 | PyObject *callable = NULL; |
| 5969 | PyObject *argtup = NULL; |
| 5970 | PyObject *obj = NULL; |
| 5971 | |
| 5972 | PDATA_POP(self->stack, argtup); |
| 5973 | if (argtup == NULL) |
| 5974 | return -1; |
| 5975 | PDATA_POP(self->stack, callable); |
| 5976 | if (callable) { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5977 | obj = PyObject_CallObject(callable, argtup); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5978 | Py_DECREF(callable); |
| 5979 | } |
| 5980 | Py_DECREF(argtup); |
| 5981 | |
| 5982 | if (obj == NULL) |
| 5983 | return -1; |
| 5984 | |
| 5985 | PDATA_PUSH(self->stack, obj, -1); |
| 5986 | return 0; |
| 5987 | } |
| 5988 | |
| 5989 | /* Just raises an error if we don't know the protocol specified. PROTO |
| 5990 | * is the first opcode for protocols >= 2. |
| 5991 | */ |
| 5992 | static int |
| 5993 | load_proto(UnpicklerObject *self) |
| 5994 | { |
| 5995 | char *s; |
| 5996 | int i; |
| 5997 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5998 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5999 | return -1; |
| 6000 | |
| 6001 | i = (unsigned char)s[0]; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6002 | if (i <= HIGHEST_PROTOCOL) { |
| 6003 | self->proto = i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6004 | return 0; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6005 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6006 | |
| 6007 | PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i); |
| 6008 | return -1; |
| 6009 | } |
| 6010 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6011 | static int |
| 6012 | load_frame(UnpicklerObject *self) |
| 6013 | { |
| 6014 | char *s; |
| 6015 | Py_ssize_t frame_len; |
| 6016 | |
| 6017 | if (_Unpickler_Read(self, &s, 8) < 0) |
| 6018 | return -1; |
| 6019 | |
| 6020 | frame_len = calc_binsize(s, 8); |
| 6021 | if (frame_len < 0) { |
| 6022 | PyErr_Format(PyExc_OverflowError, |
| 6023 | "FRAME length exceeds system's maximum of %zd bytes", |
| 6024 | PY_SSIZE_T_MAX); |
| 6025 | return -1; |
| 6026 | } |
| 6027 | |
| 6028 | if (_Unpickler_Read(self, &s, frame_len) < 0) |
| 6029 | return -1; |
| 6030 | |
| 6031 | /* Rewind to start of frame */ |
| 6032 | self->next_read_idx -= frame_len; |
| 6033 | return 0; |
| 6034 | } |
| 6035 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6036 | static PyObject * |
| 6037 | load(UnpicklerObject *self) |
| 6038 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6039 | PyObject *value = NULL; |
Christian Heimes | 27ea78b | 2014-01-27 01:03:53 +0100 | [diff] [blame] | 6040 | char *s = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6041 | |
| 6042 | self->num_marks = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6043 | self->proto = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6044 | if (Py_SIZE(self->stack)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6045 | Pdata_clear(self->stack, 0); |
| 6046 | |
| 6047 | /* Convenient macros for the dispatch while-switch loop just below. */ |
| 6048 | #define OP(opcode, load_func) \ |
| 6049 | case opcode: if (load_func(self) < 0) break; continue; |
| 6050 | |
| 6051 | #define OP_ARG(opcode, load_func, arg) \ |
| 6052 | case opcode: if (load_func(self, (arg)) < 0) break; continue; |
| 6053 | |
| 6054 | while (1) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6055 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6056 | break; |
| 6057 | |
| 6058 | switch ((enum opcode)s[0]) { |
| 6059 | OP(NONE, load_none) |
| 6060 | OP(BININT, load_binint) |
| 6061 | OP(BININT1, load_binint1) |
| 6062 | OP(BININT2, load_binint2) |
| 6063 | OP(INT, load_int) |
| 6064 | OP(LONG, load_long) |
| 6065 | OP_ARG(LONG1, load_counted_long, 1) |
| 6066 | OP_ARG(LONG4, load_counted_long, 4) |
| 6067 | OP(FLOAT, load_float) |
| 6068 | OP(BINFLOAT, load_binfloat) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6069 | OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1) |
| 6070 | OP_ARG(BINBYTES, load_counted_binbytes, 4) |
| 6071 | OP_ARG(BINBYTES8, load_counted_binbytes, 8) |
| 6072 | OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1) |
| 6073 | OP_ARG(BINSTRING, load_counted_binstring, 4) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6074 | OP(STRING, load_string) |
| 6075 | OP(UNICODE, load_unicode) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6076 | OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1) |
| 6077 | OP_ARG(BINUNICODE, load_counted_binunicode, 4) |
| 6078 | OP_ARG(BINUNICODE8, load_counted_binunicode, 8) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6079 | OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0) |
| 6080 | OP_ARG(TUPLE1, load_counted_tuple, 1) |
| 6081 | OP_ARG(TUPLE2, load_counted_tuple, 2) |
| 6082 | OP_ARG(TUPLE3, load_counted_tuple, 3) |
| 6083 | OP(TUPLE, load_tuple) |
| 6084 | OP(EMPTY_LIST, load_empty_list) |
| 6085 | OP(LIST, load_list) |
| 6086 | OP(EMPTY_DICT, load_empty_dict) |
| 6087 | OP(DICT, load_dict) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6088 | OP(EMPTY_SET, load_empty_set) |
| 6089 | OP(ADDITEMS, load_additems) |
| 6090 | OP(FROZENSET, load_frozenset) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6091 | OP(OBJ, load_obj) |
| 6092 | OP(INST, load_inst) |
| 6093 | OP(NEWOBJ, load_newobj) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6094 | OP(NEWOBJ_EX, load_newobj_ex) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6095 | OP(GLOBAL, load_global) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6096 | OP(STACK_GLOBAL, load_stack_global) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6097 | OP(APPEND, load_append) |
| 6098 | OP(APPENDS, load_appends) |
| 6099 | OP(BUILD, load_build) |
| 6100 | OP(DUP, load_dup) |
| 6101 | OP(BINGET, load_binget) |
| 6102 | OP(LONG_BINGET, load_long_binget) |
| 6103 | OP(GET, load_get) |
| 6104 | OP(MARK, load_mark) |
| 6105 | OP(BINPUT, load_binput) |
| 6106 | OP(LONG_BINPUT, load_long_binput) |
| 6107 | OP(PUT, load_put) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6108 | OP(MEMOIZE, load_memoize) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6109 | OP(POP, load_pop) |
| 6110 | OP(POP_MARK, load_pop_mark) |
| 6111 | OP(SETITEM, load_setitem) |
| 6112 | OP(SETITEMS, load_setitems) |
| 6113 | OP(PERSID, load_persid) |
| 6114 | OP(BINPERSID, load_binpersid) |
| 6115 | OP(REDUCE, load_reduce) |
| 6116 | OP(PROTO, load_proto) |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6117 | OP(FRAME, load_frame) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6118 | OP_ARG(EXT1, load_extension, 1) |
| 6119 | OP_ARG(EXT2, load_extension, 2) |
| 6120 | OP_ARG(EXT4, load_extension, 4) |
| 6121 | OP_ARG(NEWTRUE, load_bool, Py_True) |
| 6122 | OP_ARG(NEWFALSE, load_bool, Py_False) |
| 6123 | |
| 6124 | case STOP: |
| 6125 | break; |
| 6126 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6127 | default: |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6128 | if (s[0] == '\0') { |
Benjamin Peterson | adde86d | 2011-09-23 13:41:41 -0400 | [diff] [blame] | 6129 | PyErr_SetNone(PyExc_EOFError); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6130 | } |
| 6131 | else { |
| 6132 | PickleState *st = _Pickle_GetGlobalState(); |
| 6133 | PyErr_Format(st->UnpicklingError, |
Benjamin Peterson | adde86d | 2011-09-23 13:41:41 -0400 | [diff] [blame] | 6134 | "invalid load key, '%c'.", s[0]); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6135 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6136 | return NULL; |
| 6137 | } |
| 6138 | |
| 6139 | break; /* and we are done! */ |
| 6140 | } |
| 6141 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6142 | if (PyErr_Occurred()) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6143 | return NULL; |
| 6144 | } |
| 6145 | |
Victor Stinner | 2ae57e3 | 2013-10-31 13:39:23 +0100 | [diff] [blame] | 6146 | if (_Unpickler_SkipConsumed(self) < 0) |
| 6147 | return NULL; |
| 6148 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6149 | PDATA_POP(self->stack, value); |
| 6150 | return value; |
| 6151 | } |
| 6152 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6153 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6154 | |
| 6155 | _pickle.Unpickler.load |
| 6156 | |
| 6157 | Load a pickle. |
| 6158 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6159 | Read a pickled object representation from the open file object given |
| 6160 | in the constructor, and return the reconstituted object hierarchy |
| 6161 | specified therein. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6162 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6163 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6164 | static PyObject * |
Larry Hastings | c204726 | 2014-01-25 20:43:29 -0800 | [diff] [blame] | 6165 | _pickle_Unpickler_load_impl(UnpicklerObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6166 | /*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6167 | { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6168 | UnpicklerObject *unpickler = (UnpicklerObject*)self; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6169 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6170 | /* Check whether the Unpickler was initialized correctly. This prevents |
| 6171 | segfaulting if a subclass overridden __init__ with a function that does |
| 6172 | not call Unpickler.__init__(). Here, we simply ensure that self->read |
| 6173 | is not NULL. */ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6174 | if (unpickler->read == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6175 | PickleState *st = _Pickle_GetGlobalState(); |
| 6176 | PyErr_Format(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6177 | "Unpickler.__init__() was not called by %s.__init__()", |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6178 | Py_TYPE(unpickler)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6179 | return NULL; |
| 6180 | } |
| 6181 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6182 | return load(unpickler); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6183 | } |
| 6184 | |
| 6185 | /* The name of find_class() is misleading. In newer pickle protocols, this |
| 6186 | function is used for loading any global (i.e., functions), not just |
| 6187 | classes. The name is kept only for backward compatibility. */ |
| 6188 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6189 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6190 | |
| 6191 | _pickle.Unpickler.find_class |
| 6192 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6193 | module_name: object |
| 6194 | global_name: object |
| 6195 | / |
| 6196 | |
| 6197 | Return an object from a specified module. |
| 6198 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6199 | If necessary, the module will be imported. Subclasses may override |
| 6200 | this method (e.g. to restrict unpickling of arbitrary classes and |
| 6201 | functions). |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6202 | |
| 6203 | This method is called whenever a class or a function object is |
| 6204 | needed. Both arguments passed are str objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6205 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6206 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6207 | static PyObject * |
| 6208 | _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] | 6209 | /*[clinic end generated code: output=64c77437e088e188 input=e2e6a865de093ef4]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6210 | { |
| 6211 | PyObject *global; |
| 6212 | PyObject *modules_dict; |
| 6213 | PyObject *module; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6214 | _Py_IDENTIFIER(modules); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6215 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6216 | /* Try to map the old names used in Python 2.x to the new ones used in |
| 6217 | Python 3.x. We do this only with old pickle protocols and when the |
| 6218 | user has not disabled the feature. */ |
| 6219 | if (self->proto < 3 && self->fix_imports) { |
| 6220 | PyObject *key; |
| 6221 | PyObject *item; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6222 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6223 | |
| 6224 | /* Check if the global (i.e., a function or a class) was renamed |
| 6225 | or moved to another module. */ |
| 6226 | key = PyTuple_Pack(2, module_name, global_name); |
| 6227 | if (key == NULL) |
| 6228 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6229 | item = PyDict_GetItemWithError(st->name_mapping_2to3, key); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6230 | Py_DECREF(key); |
| 6231 | if (item) { |
| 6232 | if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
| 6233 | PyErr_Format(PyExc_RuntimeError, |
| 6234 | "_compat_pickle.NAME_MAPPING values should be " |
| 6235 | "2-tuples, not %.200s", Py_TYPE(item)->tp_name); |
| 6236 | return NULL; |
| 6237 | } |
| 6238 | module_name = PyTuple_GET_ITEM(item, 0); |
| 6239 | global_name = PyTuple_GET_ITEM(item, 1); |
| 6240 | if (!PyUnicode_Check(module_name) || |
| 6241 | !PyUnicode_Check(global_name)) { |
| 6242 | PyErr_Format(PyExc_RuntimeError, |
| 6243 | "_compat_pickle.NAME_MAPPING values should be " |
| 6244 | "pairs of str, not (%.200s, %.200s)", |
| 6245 | Py_TYPE(module_name)->tp_name, |
| 6246 | Py_TYPE(global_name)->tp_name); |
| 6247 | return NULL; |
| 6248 | } |
| 6249 | } |
| 6250 | else if (PyErr_Occurred()) { |
| 6251 | return NULL; |
| 6252 | } |
| 6253 | |
| 6254 | /* Check if the module was renamed. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6255 | item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6256 | if (item) { |
| 6257 | if (!PyUnicode_Check(item)) { |
| 6258 | PyErr_Format(PyExc_RuntimeError, |
| 6259 | "_compat_pickle.IMPORT_MAPPING values should be " |
| 6260 | "strings, not %.200s", Py_TYPE(item)->tp_name); |
| 6261 | return NULL; |
| 6262 | } |
| 6263 | module_name = item; |
| 6264 | } |
| 6265 | else if (PyErr_Occurred()) { |
| 6266 | return NULL; |
| 6267 | } |
| 6268 | } |
| 6269 | |
Victor Stinner | bb52020 | 2013-11-06 22:40:41 +0100 | [diff] [blame] | 6270 | modules_dict = _PySys_GetObjectId(&PyId_modules); |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 6271 | if (modules_dict == NULL) { |
| 6272 | PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6273 | return NULL; |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 6274 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6275 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6276 | module = PyDict_GetItemWithError(modules_dict, module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6277 | if (module == NULL) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6278 | if (PyErr_Occurred()) |
| 6279 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6280 | module = PyImport_Import(module_name); |
| 6281 | if (module == NULL) |
| 6282 | return NULL; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6283 | global = getattribute(module, global_name, self->proto >= 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6284 | Py_DECREF(module); |
| 6285 | } |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 6286 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6287 | global = getattribute(module, global_name, self->proto >= 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6288 | } |
| 6289 | return global; |
| 6290 | } |
| 6291 | |
| 6292 | static struct PyMethodDef Unpickler_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6293 | _PICKLE_UNPICKLER_LOAD_METHODDEF |
| 6294 | _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6295 | {NULL, NULL} /* sentinel */ |
| 6296 | }; |
| 6297 | |
| 6298 | static void |
| 6299 | Unpickler_dealloc(UnpicklerObject *self) |
| 6300 | { |
| 6301 | PyObject_GC_UnTrack((PyObject *)self); |
| 6302 | Py_XDECREF(self->readline); |
| 6303 | Py_XDECREF(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6304 | Py_XDECREF(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6305 | Py_XDECREF(self->stack); |
| 6306 | Py_XDECREF(self->pers_func); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6307 | if (self->buffer.buf != NULL) { |
| 6308 | PyBuffer_Release(&self->buffer); |
| 6309 | self->buffer.buf = NULL; |
| 6310 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6311 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6312 | _Unpickler_MemoCleanup(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6313 | PyMem_Free(self->marks); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6314 | PyMem_Free(self->input_line); |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6315 | PyMem_Free(self->encoding); |
| 6316 | PyMem_Free(self->errors); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6317 | |
| 6318 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 6319 | } |
| 6320 | |
| 6321 | static int |
| 6322 | Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg) |
| 6323 | { |
| 6324 | Py_VISIT(self->readline); |
| 6325 | Py_VISIT(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6326 | Py_VISIT(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6327 | Py_VISIT(self->stack); |
| 6328 | Py_VISIT(self->pers_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6329 | return 0; |
| 6330 | } |
| 6331 | |
| 6332 | static int |
| 6333 | Unpickler_clear(UnpicklerObject *self) |
| 6334 | { |
| 6335 | Py_CLEAR(self->readline); |
| 6336 | Py_CLEAR(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6337 | Py_CLEAR(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6338 | Py_CLEAR(self->stack); |
| 6339 | Py_CLEAR(self->pers_func); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6340 | if (self->buffer.buf != NULL) { |
| 6341 | PyBuffer_Release(&self->buffer); |
| 6342 | self->buffer.buf = NULL; |
| 6343 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6344 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6345 | _Unpickler_MemoCleanup(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6346 | PyMem_Free(self->marks); |
| 6347 | self->marks = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6348 | PyMem_Free(self->input_line); |
| 6349 | self->input_line = NULL; |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6350 | PyMem_Free(self->encoding); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6351 | self->encoding = NULL; |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6352 | PyMem_Free(self->errors); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6353 | self->errors = NULL; |
| 6354 | |
| 6355 | return 0; |
| 6356 | } |
| 6357 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6358 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6359 | |
| 6360 | _pickle.Unpickler.__init__ |
| 6361 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6362 | file: object |
| 6363 | * |
| 6364 | fix_imports: bool = True |
| 6365 | encoding: str = 'ASCII' |
| 6366 | errors: str = 'strict' |
| 6367 | |
| 6368 | This takes a binary file for reading a pickle data stream. |
| 6369 | |
| 6370 | The protocol version of the pickle is detected automatically, so no |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6371 | protocol argument is needed. Bytes past the pickled object's |
| 6372 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6373 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6374 | The argument *file* must have two methods, a read() method that takes |
| 6375 | an integer argument, and a readline() method that requires no |
| 6376 | arguments. Both methods should return bytes. Thus *file* can be a |
| 6377 | binary file object opened for reading, a io.BytesIO object, or any |
| 6378 | other custom object that meets this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6379 | |
| 6380 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
| 6381 | which are used to control compatiblity support for pickle stream |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6382 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 6383 | 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] | 6384 | *encoding* and *errors* tell pickle how to decode 8-bit string |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6385 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 6386 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 6387 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6388 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6389 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6390 | static int |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6391 | _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] | 6392 | /*[clinic end generated code: output=b9ed1d84d315f3b5 input=30b4dc9e976b890c]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6393 | { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 6394 | _Py_IDENTIFIER(persistent_load); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6395 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6396 | /* In case of multiple __init__() calls, clear previous content. */ |
| 6397 | if (self->read != NULL) |
| 6398 | (void)Unpickler_clear(self); |
| 6399 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6400 | if (_Unpickler_SetInputStream(self, file) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6401 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6402 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6403 | if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6404 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6405 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6406 | self->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6407 | if (self->fix_imports == -1) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6408 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6409 | |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 6410 | if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) { |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6411 | self->pers_func = _PyObject_GetAttrId((PyObject *)self, |
| 6412 | &PyId_persistent_load); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6413 | if (self->pers_func == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6414 | return 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6415 | } |
| 6416 | else { |
| 6417 | self->pers_func = NULL; |
| 6418 | } |
| 6419 | |
| 6420 | self->stack = (Pdata *)Pdata_New(); |
| 6421 | if (self->stack == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6422 | return 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6423 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6424 | self->memo_size = 32; |
| 6425 | self->memo = _Unpickler_NewMemo(self->memo_size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6426 | if (self->memo == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6427 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6428 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6429 | self->proto = 0; |
Alexandre Vassalotti | 0e7aa8c | 2009-04-03 04:17:41 +0000 | [diff] [blame] | 6430 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6431 | return 0; |
| 6432 | } |
| 6433 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6434 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6435 | /* Define a proxy object for the Unpickler's internal memo object. This is to |
| 6436 | * avoid breaking code like: |
| 6437 | * unpickler.memo.clear() |
| 6438 | * and |
| 6439 | * unpickler.memo = saved_memo |
| 6440 | * Is this a good idea? Not really, but we don't want to break code that uses |
| 6441 | * it. Note that we don't implement the entire mapping API here. This is |
| 6442 | * intentional, as these should be treated as black-box implementation details. |
| 6443 | * |
| 6444 | * We do, however, have to implement pickling/unpickling support because of |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 6445 | * real-world code like cvs2svn. |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6446 | */ |
| 6447 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6448 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6449 | _pickle.UnpicklerMemoProxy.clear |
| 6450 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6451 | Remove all items from memo. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6452 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6453 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6454 | static PyObject * |
| 6455 | _pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6456 | /*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6457 | { |
| 6458 | _Unpickler_MemoCleanup(self->unpickler); |
| 6459 | self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size); |
| 6460 | if (self->unpickler->memo == NULL) |
| 6461 | return NULL; |
| 6462 | Py_RETURN_NONE; |
| 6463 | } |
| 6464 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6465 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6466 | _pickle.UnpicklerMemoProxy.copy |
| 6467 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6468 | Copy the memo to a new object. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6469 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6470 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6471 | static PyObject * |
| 6472 | _pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6473 | /*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6474 | { |
| 6475 | Py_ssize_t i; |
| 6476 | PyObject *new_memo = PyDict_New(); |
| 6477 | if (new_memo == NULL) |
| 6478 | return NULL; |
| 6479 | |
| 6480 | for (i = 0; i < self->unpickler->memo_size; i++) { |
| 6481 | int status; |
| 6482 | PyObject *key, *value; |
| 6483 | |
| 6484 | value = self->unpickler->memo[i]; |
| 6485 | if (value == NULL) |
| 6486 | continue; |
| 6487 | |
| 6488 | key = PyLong_FromSsize_t(i); |
| 6489 | if (key == NULL) |
| 6490 | goto error; |
| 6491 | status = PyDict_SetItem(new_memo, key, value); |
| 6492 | Py_DECREF(key); |
| 6493 | if (status < 0) |
| 6494 | goto error; |
| 6495 | } |
| 6496 | return new_memo; |
| 6497 | |
| 6498 | error: |
| 6499 | Py_DECREF(new_memo); |
| 6500 | return NULL; |
| 6501 | } |
| 6502 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6503 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6504 | _pickle.UnpicklerMemoProxy.__reduce__ |
| 6505 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6506 | Implement pickling support. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6507 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6508 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6509 | static PyObject * |
| 6510 | _pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6511 | /*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6512 | { |
| 6513 | PyObject *reduce_value; |
| 6514 | PyObject *constructor_args; |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6515 | PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6516 | if (contents == NULL) |
| 6517 | return NULL; |
| 6518 | |
| 6519 | reduce_value = PyTuple_New(2); |
| 6520 | if (reduce_value == NULL) { |
| 6521 | Py_DECREF(contents); |
| 6522 | return NULL; |
| 6523 | } |
| 6524 | constructor_args = PyTuple_New(1); |
| 6525 | if (constructor_args == NULL) { |
| 6526 | Py_DECREF(contents); |
| 6527 | Py_DECREF(reduce_value); |
| 6528 | return NULL; |
| 6529 | } |
| 6530 | PyTuple_SET_ITEM(constructor_args, 0, contents); |
| 6531 | Py_INCREF((PyObject *)&PyDict_Type); |
| 6532 | PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type); |
| 6533 | PyTuple_SET_ITEM(reduce_value, 1, constructor_args); |
| 6534 | return reduce_value; |
| 6535 | } |
| 6536 | |
| 6537 | static PyMethodDef unpicklerproxy_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6538 | _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF |
| 6539 | _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF |
| 6540 | _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6541 | {NULL, NULL} /* sentinel */ |
| 6542 | }; |
| 6543 | |
| 6544 | static void |
| 6545 | UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self) |
| 6546 | { |
| 6547 | PyObject_GC_UnTrack(self); |
| 6548 | Py_XDECREF(self->unpickler); |
| 6549 | PyObject_GC_Del((PyObject *)self); |
| 6550 | } |
| 6551 | |
| 6552 | static int |
| 6553 | UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self, |
| 6554 | visitproc visit, void *arg) |
| 6555 | { |
| 6556 | Py_VISIT(self->unpickler); |
| 6557 | return 0; |
| 6558 | } |
| 6559 | |
| 6560 | static int |
| 6561 | UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self) |
| 6562 | { |
| 6563 | Py_CLEAR(self->unpickler); |
| 6564 | return 0; |
| 6565 | } |
| 6566 | |
| 6567 | static PyTypeObject UnpicklerMemoProxyType = { |
| 6568 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6569 | "_pickle.UnpicklerMemoProxy", /*tp_name*/ |
| 6570 | sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/ |
| 6571 | 0, |
| 6572 | (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */ |
| 6573 | 0, /* tp_print */ |
| 6574 | 0, /* tp_getattr */ |
| 6575 | 0, /* tp_setattr */ |
| 6576 | 0, /* tp_compare */ |
| 6577 | 0, /* tp_repr */ |
| 6578 | 0, /* tp_as_number */ |
| 6579 | 0, /* tp_as_sequence */ |
| 6580 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 6581 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6582 | 0, /* tp_call */ |
| 6583 | 0, /* tp_str */ |
| 6584 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 6585 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 6586 | 0, /* tp_as_buffer */ |
| 6587 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 6588 | 0, /* tp_doc */ |
| 6589 | (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */ |
| 6590 | (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */ |
| 6591 | 0, /* tp_richcompare */ |
| 6592 | 0, /* tp_weaklistoffset */ |
| 6593 | 0, /* tp_iter */ |
| 6594 | 0, /* tp_iternext */ |
| 6595 | unpicklerproxy_methods, /* tp_methods */ |
| 6596 | }; |
| 6597 | |
| 6598 | static PyObject * |
| 6599 | UnpicklerMemoProxy_New(UnpicklerObject *unpickler) |
| 6600 | { |
| 6601 | UnpicklerMemoProxyObject *self; |
| 6602 | |
| 6603 | self = PyObject_GC_New(UnpicklerMemoProxyObject, |
| 6604 | &UnpicklerMemoProxyType); |
| 6605 | if (self == NULL) |
| 6606 | return NULL; |
| 6607 | Py_INCREF(unpickler); |
| 6608 | self->unpickler = unpickler; |
| 6609 | PyObject_GC_Track(self); |
| 6610 | return (PyObject *)self; |
| 6611 | } |
| 6612 | |
| 6613 | /*****************************************************************************/ |
| 6614 | |
| 6615 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6616 | static PyObject * |
| 6617 | Unpickler_get_memo(UnpicklerObject *self) |
| 6618 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6619 | return UnpicklerMemoProxy_New(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6620 | } |
| 6621 | |
| 6622 | static int |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6623 | Unpickler_set_memo(UnpicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6624 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6625 | PyObject **new_memo; |
| 6626 | Py_ssize_t new_memo_size = 0; |
| 6627 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6628 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6629 | if (obj == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6630 | PyErr_SetString(PyExc_TypeError, |
| 6631 | "attribute deletion is not supported"); |
| 6632 | return -1; |
| 6633 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6634 | |
| 6635 | if (Py_TYPE(obj) == &UnpicklerMemoProxyType) { |
| 6636 | UnpicklerObject *unpickler = |
| 6637 | ((UnpicklerMemoProxyObject *)obj)->unpickler; |
| 6638 | |
| 6639 | new_memo_size = unpickler->memo_size; |
| 6640 | new_memo = _Unpickler_NewMemo(new_memo_size); |
| 6641 | if (new_memo == NULL) |
| 6642 | return -1; |
| 6643 | |
| 6644 | for (i = 0; i < new_memo_size; i++) { |
| 6645 | Py_XINCREF(unpickler->memo[i]); |
| 6646 | new_memo[i] = unpickler->memo[i]; |
| 6647 | } |
| 6648 | } |
| 6649 | else if (PyDict_Check(obj)) { |
| 6650 | Py_ssize_t i = 0; |
| 6651 | PyObject *key, *value; |
| 6652 | |
| 6653 | new_memo_size = PyDict_Size(obj); |
| 6654 | new_memo = _Unpickler_NewMemo(new_memo_size); |
| 6655 | if (new_memo == NULL) |
| 6656 | return -1; |
| 6657 | |
| 6658 | while (PyDict_Next(obj, &i, &key, &value)) { |
| 6659 | Py_ssize_t idx; |
| 6660 | if (!PyLong_Check(key)) { |
| 6661 | PyErr_SetString(PyExc_TypeError, |
| 6662 | "memo key must be integers"); |
| 6663 | goto error; |
| 6664 | } |
| 6665 | idx = PyLong_AsSsize_t(key); |
| 6666 | if (idx == -1 && PyErr_Occurred()) |
| 6667 | goto error; |
Christian Heimes | a24b4d2 | 2013-07-01 15:17:45 +0200 | [diff] [blame] | 6668 | if (idx < 0) { |
| 6669 | PyErr_SetString(PyExc_ValueError, |
Christian Heimes | 8087879 | 2013-07-01 15:23:39 +0200 | [diff] [blame] | 6670 | "memo key must be positive integers."); |
Christian Heimes | a24b4d2 | 2013-07-01 15:17:45 +0200 | [diff] [blame] | 6671 | goto error; |
| 6672 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6673 | if (_Unpickler_MemoPut(self, idx, value) < 0) |
| 6674 | goto error; |
| 6675 | } |
| 6676 | } |
| 6677 | else { |
| 6678 | PyErr_Format(PyExc_TypeError, |
| 6679 | "'memo' attribute must be an UnpicklerMemoProxy object" |
| 6680 | "or dict, not %.200s", Py_TYPE(obj)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6681 | return -1; |
| 6682 | } |
| 6683 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6684 | _Unpickler_MemoCleanup(self); |
| 6685 | self->memo_size = new_memo_size; |
| 6686 | self->memo = new_memo; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6687 | |
| 6688 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6689 | |
| 6690 | error: |
| 6691 | if (new_memo_size) { |
| 6692 | i = new_memo_size; |
| 6693 | while (--i >= 0) { |
| 6694 | Py_XDECREF(new_memo[i]); |
| 6695 | } |
| 6696 | PyMem_FREE(new_memo); |
| 6697 | } |
| 6698 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6699 | } |
| 6700 | |
| 6701 | static PyObject * |
| 6702 | Unpickler_get_persload(UnpicklerObject *self) |
| 6703 | { |
| 6704 | if (self->pers_func == NULL) |
| 6705 | PyErr_SetString(PyExc_AttributeError, "persistent_load"); |
| 6706 | else |
| 6707 | Py_INCREF(self->pers_func); |
| 6708 | return self->pers_func; |
| 6709 | } |
| 6710 | |
| 6711 | static int |
| 6712 | Unpickler_set_persload(UnpicklerObject *self, PyObject *value) |
| 6713 | { |
| 6714 | PyObject *tmp; |
| 6715 | |
| 6716 | if (value == NULL) { |
| 6717 | PyErr_SetString(PyExc_TypeError, |
| 6718 | "attribute deletion is not supported"); |
| 6719 | return -1; |
| 6720 | } |
| 6721 | if (!PyCallable_Check(value)) { |
| 6722 | PyErr_SetString(PyExc_TypeError, |
| 6723 | "persistent_load must be a callable taking " |
| 6724 | "one argument"); |
| 6725 | return -1; |
| 6726 | } |
| 6727 | |
| 6728 | tmp = self->pers_func; |
| 6729 | Py_INCREF(value); |
| 6730 | self->pers_func = value; |
| 6731 | Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */ |
| 6732 | |
| 6733 | return 0; |
| 6734 | } |
| 6735 | |
| 6736 | static PyGetSetDef Unpickler_getsets[] = { |
| 6737 | {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo}, |
| 6738 | {"persistent_load", (getter)Unpickler_get_persload, |
| 6739 | (setter)Unpickler_set_persload}, |
| 6740 | {NULL} |
| 6741 | }; |
| 6742 | |
| 6743 | static PyTypeObject Unpickler_Type = { |
| 6744 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6745 | "_pickle.Unpickler", /*tp_name*/ |
| 6746 | sizeof(UnpicklerObject), /*tp_basicsize*/ |
| 6747 | 0, /*tp_itemsize*/ |
| 6748 | (destructor)Unpickler_dealloc, /*tp_dealloc*/ |
| 6749 | 0, /*tp_print*/ |
| 6750 | 0, /*tp_getattr*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6751 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 6752 | 0, /*tp_reserved*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6753 | 0, /*tp_repr*/ |
| 6754 | 0, /*tp_as_number*/ |
| 6755 | 0, /*tp_as_sequence*/ |
| 6756 | 0, /*tp_as_mapping*/ |
| 6757 | 0, /*tp_hash*/ |
| 6758 | 0, /*tp_call*/ |
| 6759 | 0, /*tp_str*/ |
| 6760 | 0, /*tp_getattro*/ |
| 6761 | 0, /*tp_setattro*/ |
| 6762 | 0, /*tp_as_buffer*/ |
| 6763 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6764 | _pickle_Unpickler___init____doc__, /*tp_doc*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6765 | (traverseproc)Unpickler_traverse, /*tp_traverse*/ |
| 6766 | (inquiry)Unpickler_clear, /*tp_clear*/ |
| 6767 | 0, /*tp_richcompare*/ |
| 6768 | 0, /*tp_weaklistoffset*/ |
| 6769 | 0, /*tp_iter*/ |
| 6770 | 0, /*tp_iternext*/ |
| 6771 | Unpickler_methods, /*tp_methods*/ |
| 6772 | 0, /*tp_members*/ |
| 6773 | Unpickler_getsets, /*tp_getset*/ |
| 6774 | 0, /*tp_base*/ |
| 6775 | 0, /*tp_dict*/ |
| 6776 | 0, /*tp_descr_get*/ |
| 6777 | 0, /*tp_descr_set*/ |
| 6778 | 0, /*tp_dictoffset*/ |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6779 | _pickle_Unpickler___init__, /*tp_init*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6780 | PyType_GenericAlloc, /*tp_alloc*/ |
| 6781 | PyType_GenericNew, /*tp_new*/ |
| 6782 | PyObject_GC_Del, /*tp_free*/ |
| 6783 | 0, /*tp_is_gc*/ |
| 6784 | }; |
| 6785 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6786 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6787 | |
| 6788 | _pickle.dump |
| 6789 | |
| 6790 | obj: object |
| 6791 | file: object |
| 6792 | protocol: object = NULL |
| 6793 | * |
| 6794 | fix_imports: bool = True |
| 6795 | |
| 6796 | Write a pickled representation of obj to the open file object file. |
| 6797 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6798 | This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may |
| 6799 | be more efficient. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6800 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6801 | The optional *protocol* argument tells the pickler to use the given |
| 6802 | protocol supported protocols are 0, 1, 2, 3 and 4. The default |
| 6803 | protocol is 3; a backward-incompatible protocol designed for Python 3. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6804 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6805 | Specifying a negative protocol version selects the highest protocol |
| 6806 | version supported. The higher the protocol used, the more recent the |
| 6807 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6808 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6809 | The *file* argument must have a write() method that accepts a single |
| 6810 | bytes argument. It can thus be a file object opened for binary |
| 6811 | writing, a io.BytesIO instance, or any other custom object that meets |
| 6812 | this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6813 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6814 | If *fix_imports* is True and protocol is less than 3, pickle will try |
| 6815 | to map the new Python 3 names to the old module names used in Python |
| 6816 | 2, so that the pickle data stream is readable with Python 2. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6817 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6818 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6819 | static PyObject * |
| 6820 | _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] | 6821 | /*[clinic end generated code: output=a606e626d553850d input=e9e5fdd48de92eae]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6822 | { |
| 6823 | PicklerObject *pickler = _Pickler_New(); |
| 6824 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6825 | if (pickler == NULL) |
| 6826 | return NULL; |
| 6827 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6828 | if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6829 | goto error; |
| 6830 | |
| 6831 | if (_Pickler_SetOutputStream(pickler, file) < 0) |
| 6832 | goto error; |
| 6833 | |
| 6834 | if (dump(pickler, obj) < 0) |
| 6835 | goto error; |
| 6836 | |
| 6837 | if (_Pickler_FlushToFile(pickler) < 0) |
| 6838 | goto error; |
| 6839 | |
| 6840 | Py_DECREF(pickler); |
| 6841 | Py_RETURN_NONE; |
| 6842 | |
| 6843 | error: |
| 6844 | Py_XDECREF(pickler); |
| 6845 | return NULL; |
| 6846 | } |
| 6847 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6848 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6849 | |
| 6850 | _pickle.dumps |
| 6851 | |
| 6852 | obj: object |
| 6853 | protocol: object = NULL |
| 6854 | * |
| 6855 | fix_imports: bool = True |
| 6856 | |
| 6857 | Return the pickled representation of the object as a bytes object. |
| 6858 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6859 | The optional *protocol* argument tells the pickler to use the given |
| 6860 | protocol; supported protocols are 0, 1, 2, 3 and 4. The default |
| 6861 | protocol is 3; a backward-incompatible protocol designed for Python 3. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6862 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6863 | Specifying a negative protocol version selects the highest protocol |
| 6864 | version supported. The higher the protocol used, the more recent the |
| 6865 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6866 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6867 | If *fix_imports* is True and *protocol* is less than 3, pickle will |
| 6868 | try to map the new Python 3 names to the old module names used in |
| 6869 | 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] | 6870 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6871 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6872 | static PyObject * |
| 6873 | _pickle_dumps_impl(PyModuleDef *module, PyObject *obj, PyObject *protocol, int fix_imports) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6874 | /*[clinic end generated code: output=777f0deefe5b88ee input=293dbeda181580b7]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6875 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6876 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6877 | PicklerObject *pickler = _Pickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6878 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6879 | if (pickler == NULL) |
| 6880 | return NULL; |
| 6881 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6882 | if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6883 | goto error; |
| 6884 | |
| 6885 | if (dump(pickler, obj) < 0) |
| 6886 | goto error; |
| 6887 | |
| 6888 | result = _Pickler_GetString(pickler); |
| 6889 | Py_DECREF(pickler); |
| 6890 | return result; |
| 6891 | |
| 6892 | error: |
| 6893 | Py_XDECREF(pickler); |
| 6894 | return NULL; |
| 6895 | } |
| 6896 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6897 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6898 | |
| 6899 | _pickle.load |
| 6900 | |
| 6901 | file: object |
| 6902 | * |
| 6903 | fix_imports: bool = True |
| 6904 | encoding: str = 'ASCII' |
| 6905 | errors: str = 'strict' |
| 6906 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6907 | 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] | 6908 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6909 | This is equivalent to ``Unpickler(file).load()``, but may be more |
| 6910 | efficient. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6911 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6912 | The protocol version of the pickle is detected automatically, so no |
| 6913 | protocol argument is needed. Bytes past the pickled object's |
| 6914 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6915 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6916 | The argument *file* must have two methods, a read() method that takes |
| 6917 | an integer argument, and a readline() method that requires no |
| 6918 | arguments. Both methods should return bytes. Thus *file* can be a |
| 6919 | binary file object opened for reading, a io.BytesIO object, or any |
| 6920 | other custom object that meets this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6921 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6922 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
| 6923 | which are used to control compatiblity support for pickle stream |
| 6924 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 6925 | map the old Python 2 names to the new names used in Python 3. The |
| 6926 | *encoding* and *errors* tell pickle how to decode 8-bit string |
| 6927 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 6928 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 6929 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6930 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6931 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6932 | static PyObject * |
| 6933 | _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] | 6934 | /*[clinic end generated code: output=568c61356c172654 input=da97372e38e510a6]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6935 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6936 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6937 | UnpicklerObject *unpickler = _Unpickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6938 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6939 | if (unpickler == NULL) |
| 6940 | return NULL; |
| 6941 | |
| 6942 | if (_Unpickler_SetInputStream(unpickler, file) < 0) |
| 6943 | goto error; |
| 6944 | |
| 6945 | if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0) |
| 6946 | goto error; |
| 6947 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6948 | unpickler->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6949 | |
| 6950 | result = load(unpickler); |
| 6951 | Py_DECREF(unpickler); |
| 6952 | return result; |
| 6953 | |
| 6954 | error: |
| 6955 | Py_XDECREF(unpickler); |
| 6956 | return NULL; |
| 6957 | } |
| 6958 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6959 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6960 | |
| 6961 | _pickle.loads |
| 6962 | |
| 6963 | data: object |
| 6964 | * |
| 6965 | fix_imports: bool = True |
| 6966 | encoding: str = 'ASCII' |
| 6967 | errors: str = 'strict' |
| 6968 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6969 | Read and return an object from the given pickle data. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6970 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6971 | The protocol version of the pickle is detected automatically, so no |
| 6972 | protocol argument is needed. Bytes past the pickled object's |
| 6973 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6974 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6975 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
| 6976 | which are used to control compatiblity support for pickle stream |
| 6977 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 6978 | map the old Python 2 names to the new names used in Python 3. The |
| 6979 | *encoding* and *errors* tell pickle how to decode 8-bit string |
| 6980 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 6981 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 6982 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6983 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6984 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6985 | static PyObject * |
| 6986 | _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] | 6987 | /*[clinic end generated code: output=0b3845ad110b2522 input=f57f0fdaa2b4cb8b]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6988 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6989 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6990 | UnpicklerObject *unpickler = _Unpickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6991 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6992 | if (unpickler == NULL) |
| 6993 | return NULL; |
| 6994 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6995 | if (_Unpickler_SetStringInput(unpickler, data) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6996 | goto error; |
| 6997 | |
| 6998 | if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0) |
| 6999 | goto error; |
| 7000 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7001 | unpickler->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7002 | |
| 7003 | result = load(unpickler); |
| 7004 | Py_DECREF(unpickler); |
| 7005 | return result; |
| 7006 | |
| 7007 | error: |
| 7008 | Py_XDECREF(unpickler); |
| 7009 | return NULL; |
| 7010 | } |
| 7011 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7012 | static struct PyMethodDef pickle_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7013 | _PICKLE_DUMP_METHODDEF |
| 7014 | _PICKLE_DUMPS_METHODDEF |
| 7015 | _PICKLE_LOAD_METHODDEF |
| 7016 | _PICKLE_LOADS_METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7017 | {NULL, NULL} /* sentinel */ |
| 7018 | }; |
| 7019 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7020 | static int |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7021 | pickle_clear(PyObject *m) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7022 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7023 | _Pickle_ClearState(_Pickle_GetState(m)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7024 | return 0; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7025 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7026 | |
Stefan Krah | f483b0f | 2013-12-14 13:43:10 +0100 | [diff] [blame] | 7027 | static void |
| 7028 | pickle_free(PyObject *m) |
| 7029 | { |
| 7030 | _Pickle_ClearState(_Pickle_GetState(m)); |
| 7031 | } |
| 7032 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7033 | static int |
| 7034 | pickle_traverse(PyObject *m, visitproc visit, void *arg) |
| 7035 | { |
| 7036 | PickleState *st = _Pickle_GetState(m); |
| 7037 | Py_VISIT(st->PickleError); |
| 7038 | Py_VISIT(st->PicklingError); |
| 7039 | Py_VISIT(st->UnpicklingError); |
| 7040 | Py_VISIT(st->dispatch_table); |
| 7041 | Py_VISIT(st->extension_registry); |
| 7042 | Py_VISIT(st->extension_cache); |
| 7043 | Py_VISIT(st->inverted_registry); |
| 7044 | Py_VISIT(st->name_mapping_2to3); |
| 7045 | Py_VISIT(st->import_mapping_2to3); |
| 7046 | Py_VISIT(st->name_mapping_3to2); |
| 7047 | Py_VISIT(st->import_mapping_3to2); |
| 7048 | Py_VISIT(st->codecs_encode); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7049 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7050 | } |
| 7051 | |
| 7052 | static struct PyModuleDef _picklemodule = { |
| 7053 | PyModuleDef_HEAD_INIT, |
Stefan Krah | f483b0f | 2013-12-14 13:43:10 +0100 | [diff] [blame] | 7054 | "_pickle", /* m_name */ |
| 7055 | pickle_module_doc, /* m_doc */ |
| 7056 | sizeof(PickleState), /* m_size */ |
| 7057 | pickle_methods, /* m_methods */ |
| 7058 | NULL, /* m_reload */ |
| 7059 | pickle_traverse, /* m_traverse */ |
| 7060 | pickle_clear, /* m_clear */ |
| 7061 | (freefunc)pickle_free /* m_free */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7062 | }; |
| 7063 | |
| 7064 | PyMODINIT_FUNC |
| 7065 | PyInit__pickle(void) |
| 7066 | { |
| 7067 | PyObject *m; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7068 | PickleState *st; |
| 7069 | |
| 7070 | m = PyState_FindModule(&_picklemodule); |
| 7071 | if (m) { |
| 7072 | Py_INCREF(m); |
| 7073 | return m; |
| 7074 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7075 | |
| 7076 | if (PyType_Ready(&Unpickler_Type) < 0) |
| 7077 | return NULL; |
| 7078 | if (PyType_Ready(&Pickler_Type) < 0) |
| 7079 | return NULL; |
| 7080 | if (PyType_Ready(&Pdata_Type) < 0) |
| 7081 | return NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7082 | if (PyType_Ready(&PicklerMemoProxyType) < 0) |
| 7083 | return NULL; |
| 7084 | if (PyType_Ready(&UnpicklerMemoProxyType) < 0) |
| 7085 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7086 | |
| 7087 | /* Create the module and add the functions. */ |
| 7088 | m = PyModule_Create(&_picklemodule); |
| 7089 | if (m == NULL) |
| 7090 | return NULL; |
| 7091 | |
Antoine Pitrou | 8391cf4 | 2011-07-15 21:01:21 +0200 | [diff] [blame] | 7092 | Py_INCREF(&Pickler_Type); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7093 | if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0) |
| 7094 | return NULL; |
Antoine Pitrou | 8391cf4 | 2011-07-15 21:01:21 +0200 | [diff] [blame] | 7095 | Py_INCREF(&Unpickler_Type); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7096 | if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0) |
| 7097 | return NULL; |
| 7098 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7099 | st = _Pickle_GetState(m); |
| 7100 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7101 | /* Initialize the exceptions. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7102 | st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL); |
| 7103 | if (st->PickleError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7104 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7105 | st->PicklingError = \ |
| 7106 | PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL); |
| 7107 | if (st->PicklingError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7108 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7109 | st->UnpicklingError = \ |
| 7110 | PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL); |
| 7111 | if (st->UnpicklingError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7112 | return NULL; |
| 7113 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7114 | Py_INCREF(st->PickleError); |
| 7115 | if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7116 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7117 | Py_INCREF(st->PicklingError); |
| 7118 | if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7119 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7120 | Py_INCREF(st->UnpicklingError); |
| 7121 | if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7122 | return NULL; |
| 7123 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7124 | if (_Pickle_InitState(st) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7125 | return NULL; |
| 7126 | |
| 7127 | return m; |
| 7128 | } |