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 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7 | /*[clinic] |
| 8 | module _pickle |
| 9 | class _pickle.Pickler |
| 10 | class _pickle.PicklerMemoProxy |
| 11 | class _pickle.Unpickler |
| 12 | class _pickle.UnpicklerMemoProxy |
| 13 | [clinic]*/ |
| 14 | /*[clinic checksum: da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ |
| 15 | |
| 16 | /*[python] |
| 17 | class PicklerObject_converter(self_converter): |
| 18 | type = "PicklerObject *" |
| 19 | |
| 20 | class PicklerMemoProxyObject_converter(self_converter): |
| 21 | type = "PicklerMemoProxyObject *" |
| 22 | |
| 23 | class UnpicklerObject_converter(self_converter): |
| 24 | type = "UnpicklerObject *" |
| 25 | |
| 26 | class UnpicklerMemoProxyObject_converter(self_converter): |
| 27 | type = "UnpicklerMemoProxyObject *" |
| 28 | [python]*/ |
| 29 | /*[python checksum: da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ |
| 30 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 31 | /* Bump this when new opcodes are added to the pickle protocol. */ |
| 32 | enum { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 33 | HIGHEST_PROTOCOL = 4, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 34 | DEFAULT_PROTOCOL = 3 |
| 35 | }; |
| 36 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 37 | /* Pickle opcodes. These must be kept updated with pickle.py. |
| 38 | Extensive docs are in pickletools.py. */ |
| 39 | enum opcode { |
| 40 | MARK = '(', |
| 41 | STOP = '.', |
| 42 | POP = '0', |
| 43 | POP_MARK = '1', |
| 44 | DUP = '2', |
| 45 | FLOAT = 'F', |
| 46 | INT = 'I', |
| 47 | BININT = 'J', |
| 48 | BININT1 = 'K', |
| 49 | LONG = 'L', |
| 50 | BININT2 = 'M', |
| 51 | NONE = 'N', |
| 52 | PERSID = 'P', |
| 53 | BINPERSID = 'Q', |
| 54 | REDUCE = 'R', |
| 55 | STRING = 'S', |
| 56 | BINSTRING = 'T', |
| 57 | SHORT_BINSTRING = 'U', |
| 58 | UNICODE = 'V', |
| 59 | BINUNICODE = 'X', |
| 60 | APPEND = 'a', |
| 61 | BUILD = 'b', |
| 62 | GLOBAL = 'c', |
| 63 | DICT = 'd', |
| 64 | EMPTY_DICT = '}', |
| 65 | APPENDS = 'e', |
| 66 | GET = 'g', |
| 67 | BINGET = 'h', |
| 68 | INST = 'i', |
| 69 | LONG_BINGET = 'j', |
| 70 | LIST = 'l', |
| 71 | EMPTY_LIST = ']', |
| 72 | OBJ = 'o', |
| 73 | PUT = 'p', |
| 74 | BINPUT = 'q', |
| 75 | LONG_BINPUT = 'r', |
| 76 | SETITEM = 's', |
| 77 | TUPLE = 't', |
| 78 | EMPTY_TUPLE = ')', |
| 79 | SETITEMS = 'u', |
| 80 | BINFLOAT = 'G', |
| 81 | |
| 82 | /* Protocol 2. */ |
| 83 | PROTO = '\x80', |
| 84 | NEWOBJ = '\x81', |
| 85 | EXT1 = '\x82', |
| 86 | EXT2 = '\x83', |
| 87 | EXT4 = '\x84', |
| 88 | TUPLE1 = '\x85', |
| 89 | TUPLE2 = '\x86', |
| 90 | TUPLE3 = '\x87', |
| 91 | NEWTRUE = '\x88', |
| 92 | NEWFALSE = '\x89', |
| 93 | LONG1 = '\x8a', |
| 94 | LONG4 = '\x8b', |
| 95 | |
| 96 | /* Protocol 3 (Python 3.x) */ |
| 97 | BINBYTES = 'B', |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 98 | SHORT_BINBYTES = 'C', |
| 99 | |
| 100 | /* Protocol 4 */ |
| 101 | SHORT_BINUNICODE = '\x8c', |
| 102 | BINUNICODE8 = '\x8d', |
| 103 | BINBYTES8 = '\x8e', |
| 104 | EMPTY_SET = '\x8f', |
| 105 | ADDITEMS = '\x90', |
| 106 | FROZENSET = '\x91', |
| 107 | NEWOBJ_EX = '\x92', |
| 108 | STACK_GLOBAL = '\x93', |
| 109 | MEMOIZE = '\x94', |
| 110 | FRAME = '\x95' |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 113 | enum { |
| 114 | /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements |
| 115 | batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will |
| 116 | break if this gets out of synch with pickle.py, but it's unclear that would |
| 117 | help anything either. */ |
| 118 | BATCHSIZE = 1000, |
| 119 | |
| 120 | /* Nesting limit until Pickler, when running in "fast mode", starts |
| 121 | checking for self-referential data-structures. */ |
| 122 | FAST_NESTING_LIMIT = 50, |
| 123 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 124 | /* Initial size of the write buffer of Pickler. */ |
| 125 | WRITE_BUF_SIZE = 4096, |
| 126 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 127 | /* Prefetch size when unpickling (disabled on unpeekable streams) */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 128 | PREFETCH = 8192 * 16, |
| 129 | |
| 130 | FRAME_SIZE_TARGET = 64 * 1024, |
| 131 | |
| 132 | FRAME_HEADER_SIZE = 9 |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 133 | }; |
| 134 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 135 | /*************************************************************************/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 136 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 137 | /* State of the pickle module, per PEP 3121. */ |
| 138 | typedef struct { |
| 139 | /* Exception classes for pickle. */ |
| 140 | PyObject *PickleError; |
| 141 | PyObject *PicklingError; |
| 142 | PyObject *UnpicklingError; |
| 143 | |
| 144 | /* copyreg.dispatch_table, {type_object: pickling_function} */ |
| 145 | PyObject *dispatch_table; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 146 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 147 | /* For the extension opcodes EXT1, EXT2 and EXT4. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 148 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 149 | /* copyreg._extension_registry, {(module_name, function_name): code} */ |
| 150 | PyObject *extension_registry; |
| 151 | /* copyreg._extension_cache, {code: object} */ |
| 152 | PyObject *extension_cache; |
| 153 | /* copyreg._inverted_registry, {code: (module_name, function_name)} */ |
| 154 | PyObject *inverted_registry; |
| 155 | |
| 156 | /* Import mappings for compatibility with Python 2.x */ |
| 157 | |
| 158 | /* _compat_pickle.NAME_MAPPING, |
| 159 | {(oldmodule, oldname): (newmodule, newname)} */ |
| 160 | PyObject *name_mapping_2to3; |
| 161 | /* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */ |
| 162 | PyObject *import_mapping_2to3; |
| 163 | /* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */ |
| 164 | PyObject *name_mapping_3to2; |
| 165 | PyObject *import_mapping_3to2; |
| 166 | |
| 167 | /* codecs.encode, used for saving bytes in older protocols */ |
| 168 | PyObject *codecs_encode; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 169 | } PickleState; |
| 170 | |
| 171 | /* Forward declaration of the _pickle module definition. */ |
| 172 | static struct PyModuleDef _picklemodule; |
| 173 | |
| 174 | /* Given a module object, get its per-module state. */ |
| 175 | static PickleState * |
| 176 | _Pickle_GetState(PyObject *module) |
| 177 | { |
| 178 | return (PickleState *)PyModule_GetState(module); |
| 179 | } |
| 180 | |
| 181 | /* Find the module instance imported in the currently running sub-interpreter |
| 182 | and get its state. */ |
| 183 | static PickleState * |
| 184 | _Pickle_GetGlobalState(void) |
| 185 | { |
| 186 | return _Pickle_GetState(PyState_FindModule(&_picklemodule)); |
| 187 | } |
| 188 | |
| 189 | /* Clear the given pickle module state. */ |
| 190 | static void |
| 191 | _Pickle_ClearState(PickleState *st) |
| 192 | { |
| 193 | Py_CLEAR(st->PickleError); |
| 194 | Py_CLEAR(st->PicklingError); |
| 195 | Py_CLEAR(st->UnpicklingError); |
| 196 | Py_CLEAR(st->dispatch_table); |
| 197 | Py_CLEAR(st->extension_registry); |
| 198 | Py_CLEAR(st->extension_cache); |
| 199 | Py_CLEAR(st->inverted_registry); |
| 200 | Py_CLEAR(st->name_mapping_2to3); |
| 201 | Py_CLEAR(st->import_mapping_2to3); |
| 202 | Py_CLEAR(st->name_mapping_3to2); |
| 203 | Py_CLEAR(st->import_mapping_3to2); |
| 204 | Py_CLEAR(st->codecs_encode); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /* Initialize the given pickle module state. */ |
| 208 | static int |
| 209 | _Pickle_InitState(PickleState *st) |
| 210 | { |
| 211 | PyObject *copyreg = NULL; |
| 212 | PyObject *compat_pickle = NULL; |
| 213 | PyObject *codecs = NULL; |
| 214 | |
| 215 | copyreg = PyImport_ImportModule("copyreg"); |
| 216 | if (!copyreg) |
| 217 | goto error; |
| 218 | st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table"); |
| 219 | if (!st->dispatch_table) |
| 220 | goto error; |
| 221 | if (!PyDict_CheckExact(st->dispatch_table)) { |
| 222 | PyErr_Format(PyExc_RuntimeError, |
| 223 | "copyreg.dispatch_table should be a dict, not %.200s", |
| 224 | Py_TYPE(st->dispatch_table)->tp_name); |
| 225 | goto error; |
| 226 | } |
| 227 | st->extension_registry = \ |
| 228 | PyObject_GetAttrString(copyreg, "_extension_registry"); |
| 229 | if (!st->extension_registry) |
| 230 | goto error; |
| 231 | if (!PyDict_CheckExact(st->extension_registry)) { |
| 232 | PyErr_Format(PyExc_RuntimeError, |
| 233 | "copyreg._extension_registry should be a dict, " |
| 234 | "not %.200s", Py_TYPE(st->extension_registry)->tp_name); |
| 235 | goto error; |
| 236 | } |
| 237 | st->inverted_registry = \ |
| 238 | PyObject_GetAttrString(copyreg, "_inverted_registry"); |
| 239 | if (!st->inverted_registry) |
| 240 | goto error; |
| 241 | if (!PyDict_CheckExact(st->inverted_registry)) { |
| 242 | PyErr_Format(PyExc_RuntimeError, |
| 243 | "copyreg._inverted_registry should be a dict, " |
| 244 | "not %.200s", Py_TYPE(st->inverted_registry)->tp_name); |
| 245 | goto error; |
| 246 | } |
| 247 | st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache"); |
| 248 | if (!st->extension_cache) |
| 249 | goto error; |
| 250 | if (!PyDict_CheckExact(st->extension_cache)) { |
| 251 | PyErr_Format(PyExc_RuntimeError, |
| 252 | "copyreg._extension_cache should be a dict, " |
| 253 | "not %.200s", Py_TYPE(st->extension_cache)->tp_name); |
| 254 | goto error; |
| 255 | } |
| 256 | Py_CLEAR(copyreg); |
| 257 | |
| 258 | /* Load the 2.x -> 3.x stdlib module mapping tables */ |
| 259 | compat_pickle = PyImport_ImportModule("_compat_pickle"); |
| 260 | if (!compat_pickle) |
| 261 | goto error; |
| 262 | st->name_mapping_2to3 = \ |
| 263 | PyObject_GetAttrString(compat_pickle, "NAME_MAPPING"); |
| 264 | if (!st->name_mapping_2to3) |
| 265 | goto error; |
| 266 | if (!PyDict_CheckExact(st->name_mapping_2to3)) { |
| 267 | PyErr_Format(PyExc_RuntimeError, |
| 268 | "_compat_pickle.NAME_MAPPING should be a dict, not %.200s", |
| 269 | Py_TYPE(st->name_mapping_2to3)->tp_name); |
| 270 | goto error; |
| 271 | } |
| 272 | st->import_mapping_2to3 = \ |
| 273 | PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING"); |
| 274 | if (!st->import_mapping_2to3) |
| 275 | goto error; |
| 276 | if (!PyDict_CheckExact(st->import_mapping_2to3)) { |
| 277 | PyErr_Format(PyExc_RuntimeError, |
| 278 | "_compat_pickle.IMPORT_MAPPING should be a dict, " |
| 279 | "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name); |
| 280 | goto error; |
| 281 | } |
| 282 | /* ... and the 3.x -> 2.x mapping tables */ |
| 283 | st->name_mapping_3to2 = \ |
| 284 | PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING"); |
| 285 | if (!st->name_mapping_3to2) |
| 286 | goto error; |
| 287 | if (!PyDict_CheckExact(st->name_mapping_3to2)) { |
| 288 | PyErr_Format(PyExc_RuntimeError, |
| 289 | "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, " |
| 290 | "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name); |
| 291 | goto error; |
| 292 | } |
| 293 | st->import_mapping_3to2 = \ |
| 294 | PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING"); |
| 295 | if (!st->import_mapping_3to2) |
| 296 | goto error; |
| 297 | if (!PyDict_CheckExact(st->import_mapping_3to2)) { |
| 298 | PyErr_Format(PyExc_RuntimeError, |
| 299 | "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, " |
| 300 | "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name); |
| 301 | goto error; |
| 302 | } |
| 303 | Py_CLEAR(compat_pickle); |
| 304 | |
| 305 | codecs = PyImport_ImportModule("codecs"); |
| 306 | if (codecs == NULL) |
| 307 | goto error; |
| 308 | st->codecs_encode = PyObject_GetAttrString(codecs, "encode"); |
| 309 | if (st->codecs_encode == NULL) { |
| 310 | goto error; |
| 311 | } |
| 312 | if (!PyCallable_Check(st->codecs_encode)) { |
| 313 | PyErr_Format(PyExc_RuntimeError, |
| 314 | "codecs.encode should be a callable, not %.200s", |
| 315 | Py_TYPE(st->codecs_encode)->tp_name); |
| 316 | goto error; |
| 317 | } |
| 318 | Py_CLEAR(codecs); |
| 319 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 320 | return 0; |
| 321 | |
| 322 | error: |
| 323 | Py_CLEAR(copyreg); |
| 324 | Py_CLEAR(compat_pickle); |
| 325 | Py_CLEAR(codecs); |
| 326 | _Pickle_ClearState(st); |
| 327 | return -1; |
| 328 | } |
| 329 | |
| 330 | /* Helper for calling a function with a single argument quickly. |
| 331 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 332 | This function steals the reference of the given argument. */ |
| 333 | static PyObject * |
| 334 | _Pickle_FastCall(PyObject *func, PyObject *obj) |
| 335 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 336 | PyObject *result; |
Alexandre Vassalotti | b13e6bc | 2013-11-28 14:56:09 -0800 | [diff] [blame] | 337 | PyObject *arg_tuple = PyTuple_New(1); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 338 | |
Alexandre Vassalotti | b13e6bc | 2013-11-28 14:56:09 -0800 | [diff] [blame] | 339 | /* Note: this function used to reuse the argument tuple. This used to give |
| 340 | a slight performance boost with older pickle implementations where many |
| 341 | unbuffered reads occurred (thus needing many function calls). |
| 342 | |
| 343 | However, this optimization was removed because it was too complicated |
| 344 | to get right. It abused the C API for tuples to mutate them which led |
| 345 | to subtle reference counting and concurrency bugs. Furthermore, the |
| 346 | introduction of protocol 4 and the prefetching optimization via peek() |
| 347 | significantly reduced the number of function calls we do. Thus, the |
| 348 | benefits became marginal at best. */ |
| 349 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 350 | if (arg_tuple == NULL) { |
Alexandre Vassalotti | b13e6bc | 2013-11-28 14:56:09 -0800 | [diff] [blame] | 351 | Py_DECREF(obj); |
| 352 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 353 | } |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 354 | PyTuple_SET_ITEM(arg_tuple, 0, obj); |
| 355 | result = PyObject_Call(func, arg_tuple, NULL); |
Alexandre Vassalotti | b13e6bc | 2013-11-28 14:56:09 -0800 | [diff] [blame] | 356 | Py_CLEAR(arg_tuple); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 357 | return result; |
| 358 | } |
| 359 | |
| 360 | /*************************************************************************/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 361 | |
| 362 | static int |
| 363 | stack_underflow(void) |
| 364 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 365 | PickleState *st = _Pickle_GetGlobalState(); |
| 366 | PyErr_SetString(st->UnpicklingError, "unpickling stack underflow"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 367 | return -1; |
| 368 | } |
| 369 | |
| 370 | /* Internal data type used as the unpickling stack. */ |
| 371 | typedef struct { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 372 | PyObject_VAR_HEAD |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 373 | PyObject **data; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 374 | Py_ssize_t allocated; /* number of slots in data allocated */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 375 | } Pdata; |
| 376 | |
| 377 | static void |
| 378 | Pdata_dealloc(Pdata *self) |
| 379 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 380 | Py_ssize_t i = Py_SIZE(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 381 | while (--i >= 0) { |
| 382 | Py_DECREF(self->data[i]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 383 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 384 | PyMem_FREE(self->data); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 385 | PyObject_Del(self); |
| 386 | } |
| 387 | |
| 388 | static PyTypeObject Pdata_Type = { |
| 389 | PyVarObject_HEAD_INIT(NULL, 0) |
| 390 | "_pickle.Pdata", /*tp_name*/ |
| 391 | sizeof(Pdata), /*tp_basicsize*/ |
| 392 | 0, /*tp_itemsize*/ |
| 393 | (destructor)Pdata_dealloc, /*tp_dealloc*/ |
| 394 | }; |
| 395 | |
| 396 | static PyObject * |
| 397 | Pdata_New(void) |
| 398 | { |
| 399 | Pdata *self; |
| 400 | |
| 401 | if (!(self = PyObject_New(Pdata, &Pdata_Type))) |
| 402 | return NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 403 | Py_SIZE(self) = 0; |
| 404 | self->allocated = 8; |
| 405 | self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 406 | if (self->data) |
| 407 | return (PyObject *)self; |
| 408 | Py_DECREF(self); |
| 409 | return PyErr_NoMemory(); |
| 410 | } |
| 411 | |
| 412 | |
| 413 | /* Retain only the initial clearto items. If clearto >= the current |
| 414 | * number of items, this is a (non-erroneous) NOP. |
| 415 | */ |
| 416 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 417 | Pdata_clear(Pdata *self, Py_ssize_t clearto) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 418 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 419 | Py_ssize_t i = Py_SIZE(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 420 | |
| 421 | if (clearto < 0) |
| 422 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 423 | if (clearto >= i) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 424 | return 0; |
| 425 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 426 | while (--i >= clearto) { |
| 427 | Py_CLEAR(self->data[i]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 428 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 429 | Py_SIZE(self) = clearto; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | static int |
| 434 | Pdata_grow(Pdata *self) |
| 435 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 436 | PyObject **data = self->data; |
| 437 | Py_ssize_t allocated = self->allocated; |
| 438 | Py_ssize_t new_allocated; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 439 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 440 | new_allocated = (allocated >> 3) + 6; |
| 441 | /* check for integer overflow */ |
| 442 | if (new_allocated > PY_SSIZE_T_MAX - allocated) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 443 | goto nomemory; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 444 | new_allocated += allocated; |
| 445 | if (new_allocated > (PY_SSIZE_T_MAX / sizeof(PyObject *))) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 446 | goto nomemory; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 447 | data = PyMem_REALLOC(data, new_allocated * sizeof(PyObject *)); |
| 448 | if (data == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 449 | goto nomemory; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 450 | |
| 451 | self->data = data; |
| 452 | self->allocated = new_allocated; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 453 | return 0; |
| 454 | |
| 455 | nomemory: |
| 456 | PyErr_NoMemory(); |
| 457 | return -1; |
| 458 | } |
| 459 | |
| 460 | /* D is a Pdata*. Pop the topmost element and store it into V, which |
| 461 | * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError |
| 462 | * is raised and V is set to NULL. |
| 463 | */ |
| 464 | static PyObject * |
| 465 | Pdata_pop(Pdata *self) |
| 466 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 467 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 468 | if (Py_SIZE(self) == 0) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 469 | PyErr_SetString(st->UnpicklingError, "bad pickle data"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 470 | return NULL; |
| 471 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 472 | return self->data[--Py_SIZE(self)]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 473 | } |
| 474 | #define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0) |
| 475 | |
| 476 | static int |
| 477 | Pdata_push(Pdata *self, PyObject *obj) |
| 478 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 479 | if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 480 | return -1; |
| 481 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 482 | self->data[Py_SIZE(self)++] = obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | /* Push an object on stack, transferring its ownership to the stack. */ |
| 487 | #define PDATA_PUSH(D, O, ER) do { \ |
| 488 | if (Pdata_push((D), (O)) < 0) return (ER); } while(0) |
| 489 | |
| 490 | /* Push an object on stack, adding a new reference to the object. */ |
| 491 | #define PDATA_APPEND(D, O, ER) do { \ |
| 492 | Py_INCREF((O)); \ |
| 493 | if (Pdata_push((D), (O)) < 0) return (ER); } while(0) |
| 494 | |
| 495 | static PyObject * |
| 496 | Pdata_poptuple(Pdata *self, Py_ssize_t start) |
| 497 | { |
| 498 | PyObject *tuple; |
| 499 | Py_ssize_t len, i, j; |
| 500 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 501 | len = Py_SIZE(self) - start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 502 | tuple = PyTuple_New(len); |
| 503 | if (tuple == NULL) |
| 504 | return NULL; |
| 505 | for (i = start, j = 0; j < len; i++, j++) |
| 506 | PyTuple_SET_ITEM(tuple, j, self->data[i]); |
| 507 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 508 | Py_SIZE(self) = start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 509 | return tuple; |
| 510 | } |
| 511 | |
| 512 | static PyObject * |
| 513 | Pdata_poplist(Pdata *self, Py_ssize_t start) |
| 514 | { |
| 515 | PyObject *list; |
| 516 | Py_ssize_t len, i, j; |
| 517 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 518 | len = Py_SIZE(self) - start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 519 | list = PyList_New(len); |
| 520 | if (list == NULL) |
| 521 | return NULL; |
| 522 | for (i = start, j = 0; j < len; i++, j++) |
| 523 | PyList_SET_ITEM(list, j, self->data[i]); |
| 524 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 525 | Py_SIZE(self) = start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 526 | return list; |
| 527 | } |
| 528 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 529 | typedef struct { |
| 530 | PyObject *me_key; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 531 | Py_ssize_t me_value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 532 | } PyMemoEntry; |
| 533 | |
| 534 | typedef struct { |
| 535 | Py_ssize_t mt_mask; |
| 536 | Py_ssize_t mt_used; |
| 537 | Py_ssize_t mt_allocated; |
| 538 | PyMemoEntry *mt_table; |
| 539 | } PyMemoTable; |
| 540 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 541 | typedef struct PicklerObject { |
| 542 | PyObject_HEAD |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 543 | PyMemoTable *memo; /* Memo table, keep track of the seen |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 544 | objects to support self-referential objects |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 545 | pickling. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 546 | PyObject *pers_func; /* persistent_id() method, can be NULL */ |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 547 | PyObject *dispatch_table; /* private dispatch_table, can be NULL */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 548 | |
| 549 | PyObject *write; /* write() method of the output stream. */ |
| 550 | PyObject *output_buffer; /* Write into a local bytearray buffer before |
| 551 | flushing to the stream. */ |
| 552 | Py_ssize_t output_len; /* Length of output_buffer. */ |
| 553 | Py_ssize_t max_output_len; /* Allocation size of output_buffer. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 554 | int proto; /* Pickle protocol number, >= 0 */ |
| 555 | int bin; /* Boolean, true if proto > 0 */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 556 | int framing; /* True when framing is enabled, proto >= 4 */ |
| 557 | Py_ssize_t frame_start; /* Position in output_buffer where the |
| 558 | where the current frame begins. -1 if there |
| 559 | is no frame currently open. */ |
| 560 | |
| 561 | Py_ssize_t buf_size; /* Size of the current buffered pickle data */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 562 | int fast; /* Enable fast mode if set to a true value. |
| 563 | The fast mode disable the usage of memo, |
| 564 | therefore speeding the pickling process by |
| 565 | not generating superfluous PUT opcodes. It |
| 566 | should not be used if with self-referential |
| 567 | objects. */ |
| 568 | int fast_nesting; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 569 | int fix_imports; /* Indicate whether Pickler should fix |
| 570 | the name of globals for Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 571 | PyObject *fast_memo; |
| 572 | } PicklerObject; |
| 573 | |
| 574 | typedef struct UnpicklerObject { |
| 575 | PyObject_HEAD |
| 576 | Pdata *stack; /* Pickle data stack, store unpickled objects. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 577 | |
| 578 | /* The unpickler memo is just an array of PyObject *s. Using a dict |
| 579 | is unnecessary, since the keys are contiguous ints. */ |
| 580 | PyObject **memo; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 581 | Py_ssize_t memo_size; /* Capacity of the memo array */ |
| 582 | Py_ssize_t memo_len; /* Number of objects in the memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 583 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 584 | PyObject *pers_func; /* persistent_load() method, can be NULL. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 585 | |
| 586 | Py_buffer buffer; |
| 587 | char *input_buffer; |
| 588 | char *input_line; |
| 589 | Py_ssize_t input_len; |
| 590 | Py_ssize_t next_read_idx; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 591 | Py_ssize_t prefetched_idx; /* index of first prefetched byte */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 592 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 593 | PyObject *read; /* read() method of the input stream. */ |
| 594 | PyObject *readline; /* readline() method of the input stream. */ |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 595 | PyObject *peek; /* peek() method of the input stream, or NULL */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 596 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 597 | char *encoding; /* Name of the encoding to be used for |
| 598 | decoding strings pickled using Python |
| 599 | 2.x. The default value is "ASCII" */ |
| 600 | char *errors; /* Name of errors handling scheme to used when |
| 601 | decoding strings. The default value is |
| 602 | "strict". */ |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 603 | Py_ssize_t *marks; /* Mark stack, used for unpickling container |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 604 | objects. */ |
| 605 | Py_ssize_t num_marks; /* Number of marks in the mark stack. */ |
| 606 | Py_ssize_t marks_size; /* Current allocated size of the mark stack. */ |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 607 | int proto; /* Protocol of the pickle loaded. */ |
| 608 | int fix_imports; /* Indicate whether Unpickler should fix |
| 609 | the name of globals pickled by Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 610 | } UnpicklerObject; |
| 611 | |
| 612 | /* Forward declarations */ |
| 613 | static int save(PicklerObject *, PyObject *, int); |
| 614 | static int save_reduce(PicklerObject *, PyObject *, PyObject *); |
| 615 | static PyTypeObject Pickler_Type; |
| 616 | static PyTypeObject Unpickler_Type; |
| 617 | |
| 618 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 619 | /************************************************************************* |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 620 | A custom hashtable mapping void* to Python ints. This is used by the pickler |
| 621 | 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] | 622 | a bunch of unnecessary object creation. This makes a huge performance |
| 623 | difference. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 624 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 625 | #define MT_MINSIZE 8 |
| 626 | #define PERTURB_SHIFT 5 |
| 627 | |
| 628 | |
| 629 | static PyMemoTable * |
| 630 | PyMemoTable_New(void) |
| 631 | { |
| 632 | PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable)); |
| 633 | if (memo == NULL) { |
| 634 | PyErr_NoMemory(); |
| 635 | return NULL; |
| 636 | } |
| 637 | |
| 638 | memo->mt_used = 0; |
| 639 | memo->mt_allocated = MT_MINSIZE; |
| 640 | memo->mt_mask = MT_MINSIZE - 1; |
| 641 | memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry)); |
| 642 | if (memo->mt_table == NULL) { |
| 643 | PyMem_FREE(memo); |
| 644 | PyErr_NoMemory(); |
| 645 | return NULL; |
| 646 | } |
| 647 | memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry)); |
| 648 | |
| 649 | return memo; |
| 650 | } |
| 651 | |
| 652 | static PyMemoTable * |
| 653 | PyMemoTable_Copy(PyMemoTable *self) |
| 654 | { |
| 655 | Py_ssize_t i; |
| 656 | PyMemoTable *new = PyMemoTable_New(); |
| 657 | if (new == NULL) |
| 658 | return NULL; |
| 659 | |
| 660 | new->mt_used = self->mt_used; |
| 661 | new->mt_allocated = self->mt_allocated; |
| 662 | new->mt_mask = self->mt_mask; |
| 663 | /* The table we get from _New() is probably smaller than we wanted. |
| 664 | Free it and allocate one that's the right size. */ |
| 665 | PyMem_FREE(new->mt_table); |
| 666 | new->mt_table = PyMem_MALLOC(self->mt_allocated * sizeof(PyMemoEntry)); |
| 667 | if (new->mt_table == NULL) { |
| 668 | PyMem_FREE(new); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 669 | PyErr_NoMemory(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 670 | return NULL; |
| 671 | } |
| 672 | for (i = 0; i < self->mt_allocated; i++) { |
| 673 | Py_XINCREF(self->mt_table[i].me_key); |
| 674 | } |
| 675 | memcpy(new->mt_table, self->mt_table, |
| 676 | sizeof(PyMemoEntry) * self->mt_allocated); |
| 677 | |
| 678 | return new; |
| 679 | } |
| 680 | |
| 681 | static Py_ssize_t |
| 682 | PyMemoTable_Size(PyMemoTable *self) |
| 683 | { |
| 684 | return self->mt_used; |
| 685 | } |
| 686 | |
| 687 | static int |
| 688 | PyMemoTable_Clear(PyMemoTable *self) |
| 689 | { |
| 690 | Py_ssize_t i = self->mt_allocated; |
| 691 | |
| 692 | while (--i >= 0) { |
| 693 | Py_XDECREF(self->mt_table[i].me_key); |
| 694 | } |
| 695 | self->mt_used = 0; |
| 696 | memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry)); |
| 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | static void |
| 701 | PyMemoTable_Del(PyMemoTable *self) |
| 702 | { |
| 703 | if (self == NULL) |
| 704 | return; |
| 705 | PyMemoTable_Clear(self); |
| 706 | |
| 707 | PyMem_FREE(self->mt_table); |
| 708 | PyMem_FREE(self); |
| 709 | } |
| 710 | |
| 711 | /* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup() |
| 712 | can be considerably simpler than dictobject.c's lookdict(). */ |
| 713 | static PyMemoEntry * |
| 714 | _PyMemoTable_Lookup(PyMemoTable *self, PyObject *key) |
| 715 | { |
| 716 | size_t i; |
| 717 | size_t perturb; |
| 718 | size_t mask = (size_t)self->mt_mask; |
| 719 | PyMemoEntry *table = self->mt_table; |
| 720 | PyMemoEntry *entry; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 721 | Py_hash_t hash = (Py_hash_t)key >> 3; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 722 | |
| 723 | i = hash & mask; |
| 724 | entry = &table[i]; |
| 725 | if (entry->me_key == NULL || entry->me_key == key) |
| 726 | return entry; |
| 727 | |
| 728 | for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
| 729 | i = (i << 2) + i + perturb + 1; |
| 730 | entry = &table[i & mask]; |
| 731 | if (entry->me_key == NULL || entry->me_key == key) |
| 732 | return entry; |
| 733 | } |
| 734 | assert(0); /* Never reached */ |
| 735 | return NULL; |
| 736 | } |
| 737 | |
| 738 | /* Returns -1 on failure, 0 on success. */ |
| 739 | static int |
| 740 | _PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size) |
| 741 | { |
| 742 | PyMemoEntry *oldtable = NULL; |
| 743 | PyMemoEntry *oldentry, *newentry; |
| 744 | Py_ssize_t new_size = MT_MINSIZE; |
| 745 | Py_ssize_t to_process; |
| 746 | |
| 747 | assert(min_size > 0); |
| 748 | |
| 749 | /* Find the smallest valid table size >= min_size. */ |
| 750 | while (new_size < min_size && new_size > 0) |
| 751 | new_size <<= 1; |
| 752 | if (new_size <= 0) { |
| 753 | PyErr_NoMemory(); |
| 754 | return -1; |
| 755 | } |
| 756 | /* new_size needs to be a power of two. */ |
| 757 | assert((new_size & (new_size - 1)) == 0); |
| 758 | |
| 759 | /* Allocate new table. */ |
| 760 | oldtable = self->mt_table; |
| 761 | self->mt_table = PyMem_MALLOC(new_size * sizeof(PyMemoEntry)); |
| 762 | if (self->mt_table == NULL) { |
Victor Stinner | 8ca72e2 | 2013-07-12 00:53:26 +0200 | [diff] [blame] | 763 | self->mt_table = oldtable; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 764 | PyErr_NoMemory(); |
| 765 | return -1; |
| 766 | } |
| 767 | self->mt_allocated = new_size; |
| 768 | self->mt_mask = new_size - 1; |
| 769 | memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size); |
| 770 | |
| 771 | /* Copy entries from the old table. */ |
| 772 | to_process = self->mt_used; |
| 773 | for (oldentry = oldtable; to_process > 0; oldentry++) { |
| 774 | if (oldentry->me_key != NULL) { |
| 775 | to_process--; |
| 776 | /* newentry is a pointer to a chunk of the new |
| 777 | mt_table, so we're setting the key:value pair |
| 778 | in-place. */ |
| 779 | newentry = _PyMemoTable_Lookup(self, oldentry->me_key); |
| 780 | newentry->me_key = oldentry->me_key; |
| 781 | newentry->me_value = oldentry->me_value; |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | /* Deallocate the old table. */ |
| 786 | PyMem_FREE(oldtable); |
| 787 | return 0; |
| 788 | } |
| 789 | |
| 790 | /* Returns NULL on failure, a pointer to the value otherwise. */ |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 791 | static Py_ssize_t * |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 792 | PyMemoTable_Get(PyMemoTable *self, PyObject *key) |
| 793 | { |
| 794 | PyMemoEntry *entry = _PyMemoTable_Lookup(self, key); |
| 795 | if (entry->me_key == NULL) |
| 796 | return NULL; |
| 797 | return &entry->me_value; |
| 798 | } |
| 799 | |
| 800 | /* Returns -1 on failure, 0 on success. */ |
| 801 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 802 | PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 803 | { |
| 804 | PyMemoEntry *entry; |
| 805 | |
| 806 | assert(key != NULL); |
| 807 | |
| 808 | entry = _PyMemoTable_Lookup(self, key); |
| 809 | if (entry->me_key != NULL) { |
| 810 | entry->me_value = value; |
| 811 | return 0; |
| 812 | } |
| 813 | Py_INCREF(key); |
| 814 | entry->me_key = key; |
| 815 | entry->me_value = value; |
| 816 | self->mt_used++; |
| 817 | |
| 818 | /* If we added a key, we can safely resize. Otherwise just return! |
| 819 | * If used >= 2/3 size, adjust size. Normally, this quaduples the size. |
| 820 | * |
| 821 | * Quadrupling the size improves average table sparseness |
| 822 | * (reducing collisions) at the cost of some memory. It also halves |
| 823 | * the number of expensive resize operations in a growing memo table. |
| 824 | * |
| 825 | * Very large memo tables (over 50K items) use doubling instead. |
| 826 | * This may help applications with severe memory constraints. |
| 827 | */ |
| 828 | if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2)) |
| 829 | return 0; |
| 830 | return _PyMemoTable_ResizeTable(self, |
| 831 | (self->mt_used > 50000 ? 2 : 4) * self->mt_used); |
| 832 | } |
| 833 | |
| 834 | #undef MT_MINSIZE |
| 835 | #undef PERTURB_SHIFT |
| 836 | |
| 837 | /*************************************************************************/ |
| 838 | |
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 | static int |
| 841 | _Pickler_ClearBuffer(PicklerObject *self) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 842 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 843 | Py_CLEAR(self->output_buffer); |
| 844 | self->output_buffer = |
| 845 | PyBytes_FromStringAndSize(NULL, self->max_output_len); |
| 846 | if (self->output_buffer == NULL) |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 847 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 848 | self->output_len = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 849 | self->frame_start = -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 850 | return 0; |
| 851 | } |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 852 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 853 | static void |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 854 | _write_size64(char *out, size_t value) |
| 855 | { |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 856 | int i; |
| 857 | |
| 858 | assert(sizeof(size_t) <= 8); |
| 859 | |
| 860 | for (i = 0; i < sizeof(size_t); i++) { |
| 861 | out[i] = (unsigned char)((value >> (8 * i)) & 0xff); |
| 862 | } |
| 863 | for (i = sizeof(size_t); i < 8; i++) { |
| 864 | out[i] = 0; |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 865 | } |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | static void |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 869 | _Pickler_WriteFrameHeader(PicklerObject *self, char *qdata, size_t frame_len) |
| 870 | { |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 871 | qdata[0] = FRAME; |
| 872 | _write_size64(qdata + 1, frame_len); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | static int |
| 876 | _Pickler_CommitFrame(PicklerObject *self) |
| 877 | { |
| 878 | size_t frame_len; |
| 879 | char *qdata; |
| 880 | |
| 881 | if (!self->framing || self->frame_start == -1) |
| 882 | return 0; |
| 883 | frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE; |
| 884 | qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start; |
| 885 | _Pickler_WriteFrameHeader(self, qdata, frame_len); |
| 886 | self->frame_start = -1; |
| 887 | return 0; |
| 888 | } |
| 889 | |
| 890 | static int |
| 891 | _Pickler_OpcodeBoundary(PicklerObject *self) |
| 892 | { |
| 893 | Py_ssize_t frame_len; |
| 894 | |
| 895 | if (!self->framing || self->frame_start == -1) |
| 896 | return 0; |
| 897 | frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE; |
| 898 | if (frame_len >= FRAME_SIZE_TARGET) |
| 899 | return _Pickler_CommitFrame(self); |
| 900 | else |
| 901 | return 0; |
| 902 | } |
| 903 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 904 | static PyObject * |
| 905 | _Pickler_GetString(PicklerObject *self) |
| 906 | { |
| 907 | PyObject *output_buffer = self->output_buffer; |
| 908 | |
| 909 | assert(self->output_buffer != NULL); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 910 | |
| 911 | if (_Pickler_CommitFrame(self)) |
| 912 | return NULL; |
| 913 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 914 | self->output_buffer = NULL; |
| 915 | /* Resize down to exact size */ |
| 916 | if (_PyBytes_Resize(&output_buffer, self->output_len) < 0) |
| 917 | return NULL; |
| 918 | return output_buffer; |
| 919 | } |
| 920 | |
| 921 | static int |
| 922 | _Pickler_FlushToFile(PicklerObject *self) |
| 923 | { |
| 924 | PyObject *output, *result; |
| 925 | |
| 926 | assert(self->write != NULL); |
| 927 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 928 | /* This will commit the frame first */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 929 | output = _Pickler_GetString(self); |
| 930 | if (output == NULL) |
| 931 | return -1; |
| 932 | |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 933 | result = _Pickle_FastCall(self->write, output); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 934 | Py_XDECREF(result); |
| 935 | return (result == NULL) ? -1 : 0; |
| 936 | } |
| 937 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 938 | static Py_ssize_t |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 939 | _Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 940 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 941 | Py_ssize_t i, n, required; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 942 | char *buffer; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 943 | int need_new_frame; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 944 | |
| 945 | assert(s != NULL); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 946 | need_new_frame = (self->framing && self->frame_start == -1); |
| 947 | |
| 948 | if (need_new_frame) |
| 949 | n = data_len + FRAME_HEADER_SIZE; |
| 950 | else |
| 951 | n = data_len; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 952 | |
| 953 | required = self->output_len + n; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 954 | if (required > self->max_output_len) { |
| 955 | /* Make place in buffer for the pickle chunk */ |
| 956 | if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) { |
| 957 | PyErr_NoMemory(); |
| 958 | return -1; |
| 959 | } |
| 960 | self->max_output_len = (self->output_len + n) / 2 * 3; |
| 961 | if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0) |
| 962 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 963 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 964 | buffer = PyBytes_AS_STRING(self->output_buffer); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 965 | if (need_new_frame) { |
| 966 | /* Setup new frame */ |
| 967 | Py_ssize_t frame_start = self->output_len; |
| 968 | self->frame_start = frame_start; |
| 969 | for (i = 0; i < FRAME_HEADER_SIZE; i++) { |
| 970 | /* Write an invalid value, for debugging */ |
| 971 | buffer[frame_start + i] = 0xFE; |
| 972 | } |
| 973 | self->output_len += FRAME_HEADER_SIZE; |
| 974 | } |
| 975 | if (data_len < 8) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 976 | /* This is faster than memcpy when the string is short. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 977 | for (i = 0; i < data_len; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 978 | buffer[self->output_len + i] = s[i]; |
| 979 | } |
| 980 | } |
| 981 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 982 | memcpy(buffer + self->output_len, s, data_len); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 983 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 984 | self->output_len += data_len; |
| 985 | return data_len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 986 | } |
| 987 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 988 | static PicklerObject * |
| 989 | _Pickler_New(void) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 990 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 991 | PicklerObject *self; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 992 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 993 | self = PyObject_GC_New(PicklerObject, &Pickler_Type); |
| 994 | if (self == NULL) |
| 995 | return NULL; |
| 996 | |
| 997 | self->pers_func = NULL; |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 998 | self->dispatch_table = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 999 | self->write = NULL; |
| 1000 | self->proto = 0; |
| 1001 | self->bin = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1002 | self->framing = 0; |
| 1003 | self->frame_start = -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1004 | self->fast = 0; |
| 1005 | self->fast_nesting = 0; |
| 1006 | self->fix_imports = 0; |
| 1007 | self->fast_memo = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1008 | self->max_output_len = WRITE_BUF_SIZE; |
| 1009 | self->output_len = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1010 | |
| 1011 | self->memo = PyMemoTable_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1012 | self->output_buffer = PyBytes_FromStringAndSize(NULL, |
| 1013 | self->max_output_len); |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1014 | |
| 1015 | if (self->memo == NULL || self->output_buffer == NULL) { |
Victor Stinner | c31df04 | 2013-07-12 00:08:59 +0200 | [diff] [blame] | 1016 | Py_DECREF(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1017 | return NULL; |
| 1018 | } |
| 1019 | return self; |
| 1020 | } |
| 1021 | |
| 1022 | static int |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1023 | _Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1024 | { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1025 | long proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1026 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1027 | if (protocol == NULL || protocol == Py_None) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1028 | proto = DEFAULT_PROTOCOL; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1029 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1030 | else { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1031 | proto = PyLong_AsLong(protocol); |
| 1032 | if (proto < 0) { |
| 1033 | if (proto == -1 && PyErr_Occurred()) |
| 1034 | return -1; |
| 1035 | proto = HIGHEST_PROTOCOL; |
| 1036 | } |
| 1037 | else if (proto > HIGHEST_PROTOCOL) { |
| 1038 | PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d", |
| 1039 | HIGHEST_PROTOCOL); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1040 | return -1; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1041 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1042 | } |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1043 | self->proto = (int)proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1044 | self->bin = proto > 0; |
| 1045 | self->fix_imports = fix_imports && proto < 3; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1046 | return 0; |
| 1047 | } |
| 1048 | |
| 1049 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1050 | be called once on a freshly created Pickler. */ |
| 1051 | static int |
| 1052 | _Pickler_SetOutputStream(PicklerObject *self, PyObject *file) |
| 1053 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1054 | _Py_IDENTIFIER(write); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1055 | assert(file != NULL); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1056 | self->write = _PyObject_GetAttrId(file, &PyId_write); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1057 | if (self->write == NULL) { |
| 1058 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1059 | PyErr_SetString(PyExc_TypeError, |
| 1060 | "file must have a 'write' attribute"); |
| 1061 | return -1; |
| 1062 | } |
| 1063 | |
| 1064 | return 0; |
| 1065 | } |
| 1066 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1067 | /* Returns the size of the input on success, -1 on failure. This takes its |
| 1068 | own reference to `input`. */ |
| 1069 | static Py_ssize_t |
| 1070 | _Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input) |
| 1071 | { |
| 1072 | if (self->buffer.buf != NULL) |
| 1073 | PyBuffer_Release(&self->buffer); |
| 1074 | if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0) |
| 1075 | return -1; |
| 1076 | self->input_buffer = self->buffer.buf; |
| 1077 | self->input_len = self->buffer.len; |
| 1078 | self->next_read_idx = 0; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1079 | self->prefetched_idx = self->input_len; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1080 | return self->input_len; |
| 1081 | } |
| 1082 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1083 | static int |
| 1084 | _Unpickler_SkipConsumed(UnpicklerObject *self) |
| 1085 | { |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1086 | Py_ssize_t consumed; |
| 1087 | PyObject *r; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1088 | |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1089 | consumed = self->next_read_idx - self->prefetched_idx; |
| 1090 | if (consumed <= 0) |
| 1091 | return 0; |
| 1092 | |
| 1093 | assert(self->peek); /* otherwise we did something wrong */ |
| 1094 | /* This makes an useless copy... */ |
| 1095 | r = PyObject_CallFunction(self->read, "n", consumed); |
| 1096 | if (r == NULL) |
| 1097 | return -1; |
| 1098 | Py_DECREF(r); |
| 1099 | |
| 1100 | self->prefetched_idx = self->next_read_idx; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1101 | return 0; |
| 1102 | } |
| 1103 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1104 | static const Py_ssize_t READ_WHOLE_LINE = -1; |
| 1105 | |
| 1106 | /* If reading from a file, we need to only pull the bytes we need, since there |
| 1107 | may be multiple pickle objects arranged contiguously in the same input |
| 1108 | buffer. |
| 1109 | |
| 1110 | If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n` |
| 1111 | bytes from the input stream/buffer. |
| 1112 | |
| 1113 | Update the unpickler's input buffer with the newly-read data. Returns -1 on |
| 1114 | failure; on success, returns the number of bytes read from the file. |
| 1115 | |
| 1116 | On success, self->input_len will be 0; this is intentional so that when |
| 1117 | unpickling from a file, the "we've run out of data" code paths will trigger, |
| 1118 | causing the Unpickler to go back to the file for more data. Use the returned |
| 1119 | size to tell you how much data you can process. */ |
| 1120 | static Py_ssize_t |
| 1121 | _Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n) |
| 1122 | { |
| 1123 | PyObject *data; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1124 | Py_ssize_t read_size, prefetched_size = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1125 | |
| 1126 | assert(self->read != NULL); |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1127 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1128 | if (_Unpickler_SkipConsumed(self) < 0) |
| 1129 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1130 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1131 | if (n == READ_WHOLE_LINE) { |
Alexandre Vassalotti | 6bf41e5 | 2013-11-28 15:17:29 -0800 | [diff] [blame] | 1132 | PyObject *empty_tuple = PyTuple_New(0); |
| 1133 | data = PyObject_Call(self->readline, empty_tuple, NULL); |
| 1134 | Py_DECREF(empty_tuple); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1135 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1136 | else { |
| 1137 | PyObject *len = PyLong_FromSsize_t(n); |
| 1138 | if (len == NULL) |
| 1139 | return -1; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 1140 | data = _Pickle_FastCall(self->read, len); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1141 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1142 | if (data == NULL) |
| 1143 | return -1; |
| 1144 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1145 | /* Prefetch some data without advancing the file pointer, if possible */ |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 1146 | if (self->peek) { |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1147 | PyObject *len, *prefetched; |
| 1148 | len = PyLong_FromSsize_t(PREFETCH); |
| 1149 | if (len == NULL) { |
| 1150 | Py_DECREF(data); |
| 1151 | return -1; |
| 1152 | } |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 1153 | prefetched = _Pickle_FastCall(self->peek, len); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1154 | if (prefetched == NULL) { |
| 1155 | if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 1156 | /* peek() is probably not supported by the given file object */ |
| 1157 | PyErr_Clear(); |
| 1158 | Py_CLEAR(self->peek); |
| 1159 | } |
| 1160 | else { |
| 1161 | Py_DECREF(data); |
| 1162 | return -1; |
| 1163 | } |
| 1164 | } |
| 1165 | else { |
| 1166 | assert(PyBytes_Check(prefetched)); |
| 1167 | prefetched_size = PyBytes_GET_SIZE(prefetched); |
| 1168 | PyBytes_ConcatAndDel(&data, prefetched); |
| 1169 | if (data == NULL) |
| 1170 | return -1; |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | read_size = _Unpickler_SetStringInput(self, data) - prefetched_size; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1175 | Py_DECREF(data); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1176 | self->prefetched_idx = read_size; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1177 | return read_size; |
| 1178 | } |
| 1179 | |
| 1180 | /* Read `n` bytes from the unpickler's data source, storing the result in `*s`. |
| 1181 | |
| 1182 | This should be used for all data reads, rather than accessing the unpickler's |
| 1183 | input buffer directly. This method deals correctly with reading from input |
| 1184 | streams, which the input buffer doesn't deal with. |
| 1185 | |
| 1186 | Note that when reading from a file-like object, self->next_read_idx won't |
| 1187 | be updated (it should remain at 0 for the entire unpickling process). You |
| 1188 | should use this function's return value to know how many bytes you can |
| 1189 | consume. |
| 1190 | |
| 1191 | Returns -1 (with an exception set) on failure. On success, return the |
| 1192 | number of chars read. */ |
| 1193 | static Py_ssize_t |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 1194 | _Unpickler_Read(UnpicklerObject *self, char **s, Py_ssize_t n) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1195 | { |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1196 | Py_ssize_t num_read; |
| 1197 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1198 | if (self->next_read_idx + n <= self->input_len) { |
| 1199 | *s = self->input_buffer + self->next_read_idx; |
| 1200 | self->next_read_idx += n; |
| 1201 | return n; |
| 1202 | } |
| 1203 | if (!self->read) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1204 | PyErr_Format(PyExc_EOFError, "Ran out of input"); |
Amaury Forgeot d'Arc | 3e4e72f | 2008-11-11 20:05:06 +0000 | [diff] [blame] | 1205 | return -1; |
| 1206 | } |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1207 | num_read = _Unpickler_ReadFromFile(self, n); |
| 1208 | if (num_read < 0) |
| 1209 | return -1; |
| 1210 | if (num_read < n) { |
| 1211 | PyErr_Format(PyExc_EOFError, "Ran out of input"); |
| 1212 | return -1; |
| 1213 | } |
| 1214 | *s = self->input_buffer; |
| 1215 | self->next_read_idx = n; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1216 | return n; |
| 1217 | } |
| 1218 | |
| 1219 | static Py_ssize_t |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1220 | _Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len, |
| 1221 | char **result) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1222 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1223 | char *input_line = PyMem_Realloc(self->input_line, len + 1); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1224 | if (input_line == NULL) { |
| 1225 | PyErr_NoMemory(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1226 | return -1; |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1227 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1228 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1229 | memcpy(input_line, line, len); |
| 1230 | input_line[len] = '\0'; |
| 1231 | self->input_line = input_line; |
| 1232 | *result = self->input_line; |
| 1233 | return len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1236 | /* Read a line from the input stream/buffer. If we run off the end of the input |
| 1237 | before hitting \n, return the data we found. |
| 1238 | |
| 1239 | Returns the number of chars read, or -1 on failure. */ |
| 1240 | static Py_ssize_t |
| 1241 | _Unpickler_Readline(UnpicklerObject *self, char **result) |
| 1242 | { |
| 1243 | Py_ssize_t i, num_read; |
| 1244 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1245 | for (i = self->next_read_idx; i < self->input_len; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1246 | if (self->input_buffer[i] == '\n') { |
| 1247 | char *line_start = self->input_buffer + self->next_read_idx; |
| 1248 | num_read = i - self->next_read_idx + 1; |
| 1249 | self->next_read_idx = i + 1; |
| 1250 | return _Unpickler_CopyLine(self, line_start, num_read, result); |
| 1251 | } |
| 1252 | } |
| 1253 | if (self->read) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1254 | num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE); |
| 1255 | if (num_read < 0) |
| 1256 | return -1; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1257 | self->next_read_idx = num_read; |
Antoine Pitrou | f6c7a85 | 2011-08-11 21:04:02 +0200 | [diff] [blame] | 1258 | return _Unpickler_CopyLine(self, self->input_buffer, num_read, result); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1259 | } |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1260 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1261 | /* If we get here, we've run off the end of the input string. Return the |
| 1262 | remaining string and let the caller figure it out. */ |
| 1263 | *result = self->input_buffer + self->next_read_idx; |
| 1264 | num_read = i - self->next_read_idx; |
| 1265 | self->next_read_idx = i; |
| 1266 | return num_read; |
| 1267 | } |
| 1268 | |
| 1269 | /* Returns -1 (with an exception set) on failure, 0 on success. The memo array |
| 1270 | will be modified in place. */ |
| 1271 | static int |
| 1272 | _Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size) |
| 1273 | { |
| 1274 | Py_ssize_t i; |
| 1275 | PyObject **memo; |
| 1276 | |
| 1277 | assert(new_size > self->memo_size); |
| 1278 | |
| 1279 | memo = PyMem_REALLOC(self->memo, new_size * sizeof(PyObject *)); |
| 1280 | if (memo == NULL) { |
| 1281 | PyErr_NoMemory(); |
| 1282 | return -1; |
| 1283 | } |
| 1284 | self->memo = memo; |
| 1285 | for (i = self->memo_size; i < new_size; i++) |
| 1286 | self->memo[i] = NULL; |
| 1287 | self->memo_size = new_size; |
| 1288 | return 0; |
| 1289 | } |
| 1290 | |
| 1291 | /* Returns NULL if idx is out of bounds. */ |
| 1292 | static PyObject * |
| 1293 | _Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx) |
| 1294 | { |
| 1295 | if (idx < 0 || idx >= self->memo_size) |
| 1296 | return NULL; |
| 1297 | |
| 1298 | return self->memo[idx]; |
| 1299 | } |
| 1300 | |
| 1301 | /* Returns -1 (with an exception set) on failure, 0 on success. |
| 1302 | This takes its own reference to `value`. */ |
| 1303 | static int |
| 1304 | _Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value) |
| 1305 | { |
| 1306 | PyObject *old_item; |
| 1307 | |
| 1308 | if (idx >= self->memo_size) { |
| 1309 | if (_Unpickler_ResizeMemoList(self, idx * 2) < 0) |
| 1310 | return -1; |
| 1311 | assert(idx < self->memo_size); |
| 1312 | } |
| 1313 | Py_INCREF(value); |
| 1314 | old_item = self->memo[idx]; |
| 1315 | self->memo[idx] = value; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1316 | if (old_item != NULL) { |
| 1317 | Py_DECREF(old_item); |
| 1318 | } |
| 1319 | else { |
| 1320 | self->memo_len++; |
| 1321 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1322 | return 0; |
| 1323 | } |
| 1324 | |
| 1325 | static PyObject ** |
| 1326 | _Unpickler_NewMemo(Py_ssize_t new_size) |
| 1327 | { |
| 1328 | PyObject **memo = PyMem_MALLOC(new_size * sizeof(PyObject *)); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1329 | if (memo == NULL) { |
| 1330 | PyErr_NoMemory(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1331 | return NULL; |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1332 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1333 | memset(memo, 0, new_size * sizeof(PyObject *)); |
| 1334 | return memo; |
| 1335 | } |
| 1336 | |
| 1337 | /* Free the unpickler's memo, taking care to decref any items left in it. */ |
| 1338 | static void |
| 1339 | _Unpickler_MemoCleanup(UnpicklerObject *self) |
| 1340 | { |
| 1341 | Py_ssize_t i; |
| 1342 | PyObject **memo = self->memo; |
| 1343 | |
| 1344 | if (self->memo == NULL) |
| 1345 | return; |
| 1346 | self->memo = NULL; |
| 1347 | i = self->memo_size; |
| 1348 | while (--i >= 0) { |
| 1349 | Py_XDECREF(memo[i]); |
| 1350 | } |
| 1351 | PyMem_FREE(memo); |
| 1352 | } |
| 1353 | |
| 1354 | static UnpicklerObject * |
| 1355 | _Unpickler_New(void) |
| 1356 | { |
| 1357 | UnpicklerObject *self; |
| 1358 | |
| 1359 | self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type); |
| 1360 | if (self == NULL) |
| 1361 | return NULL; |
| 1362 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1363 | self->pers_func = NULL; |
| 1364 | self->input_buffer = NULL; |
| 1365 | self->input_line = NULL; |
| 1366 | self->input_len = 0; |
| 1367 | self->next_read_idx = 0; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1368 | self->prefetched_idx = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1369 | self->read = NULL; |
| 1370 | self->readline = NULL; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1371 | self->peek = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1372 | self->encoding = NULL; |
| 1373 | self->errors = NULL; |
| 1374 | self->marks = NULL; |
| 1375 | self->num_marks = 0; |
| 1376 | self->marks_size = 0; |
| 1377 | self->proto = 0; |
| 1378 | self->fix_imports = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1379 | memset(&self->buffer, 0, sizeof(Py_buffer)); |
| 1380 | self->memo_size = 32; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1381 | self->memo_len = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1382 | self->memo = _Unpickler_NewMemo(self->memo_size); |
| 1383 | self->stack = (Pdata *)Pdata_New(); |
| 1384 | |
| 1385 | if (self->memo == NULL || self->stack == NULL) { |
| 1386 | Py_DECREF(self); |
| 1387 | return NULL; |
| 1388 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1389 | |
| 1390 | return self; |
| 1391 | } |
| 1392 | |
| 1393 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1394 | be called once on a freshly created Pickler. */ |
| 1395 | static int |
| 1396 | _Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file) |
| 1397 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1398 | _Py_IDENTIFIER(peek); |
| 1399 | _Py_IDENTIFIER(read); |
| 1400 | _Py_IDENTIFIER(readline); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1401 | |
| 1402 | self->peek = _PyObject_GetAttrId(file, &PyId_peek); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1403 | if (self->peek == NULL) { |
| 1404 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1405 | PyErr_Clear(); |
| 1406 | else |
| 1407 | return -1; |
| 1408 | } |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1409 | self->read = _PyObject_GetAttrId(file, &PyId_read); |
| 1410 | self->readline = _PyObject_GetAttrId(file, &PyId_readline); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1411 | if (self->readline == NULL || self->read == NULL) { |
| 1412 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1413 | PyErr_SetString(PyExc_TypeError, |
| 1414 | "file must have 'read' and 'readline' attributes"); |
| 1415 | Py_CLEAR(self->read); |
| 1416 | Py_CLEAR(self->readline); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1417 | Py_CLEAR(self->peek); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1418 | return -1; |
| 1419 | } |
| 1420 | return 0; |
| 1421 | } |
| 1422 | |
| 1423 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1424 | be called once on a freshly created Pickler. */ |
| 1425 | static int |
| 1426 | _Unpickler_SetInputEncoding(UnpicklerObject *self, |
| 1427 | const char *encoding, |
| 1428 | const char *errors) |
| 1429 | { |
| 1430 | if (encoding == NULL) |
| 1431 | encoding = "ASCII"; |
| 1432 | if (errors == NULL) |
| 1433 | errors = "strict"; |
| 1434 | |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 1435 | self->encoding = _PyMem_Strdup(encoding); |
| 1436 | self->errors = _PyMem_Strdup(errors); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1437 | if (self->encoding == NULL || self->errors == NULL) { |
| 1438 | PyErr_NoMemory(); |
| 1439 | return -1; |
| 1440 | } |
| 1441 | return 0; |
| 1442 | } |
| 1443 | |
| 1444 | /* Generate a GET opcode for an object stored in the memo. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1445 | static int |
| 1446 | memo_get(PicklerObject *self, PyObject *key) |
| 1447 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1448 | Py_ssize_t *value; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1449 | char pdata[30]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1450 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1451 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1452 | value = PyMemoTable_Get(self->memo, key); |
| 1453 | if (value == NULL) { |
| 1454 | PyErr_SetObject(PyExc_KeyError, key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1455 | return -1; |
| 1456 | } |
| 1457 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1458 | if (!self->bin) { |
| 1459 | pdata[0] = GET; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1460 | PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, |
| 1461 | "%" PY_FORMAT_SIZE_T "d\n", *value); |
| 1462 | len = strlen(pdata); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1463 | } |
| 1464 | else { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1465 | if (*value < 256) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1466 | pdata[0] = BINGET; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1467 | pdata[1] = (unsigned char)(*value & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1468 | len = 2; |
| 1469 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1470 | else if (*value <= 0xffffffffL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1471 | pdata[0] = LONG_BINGET; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1472 | pdata[1] = (unsigned char)(*value & 0xff); |
| 1473 | pdata[2] = (unsigned char)((*value >> 8) & 0xff); |
| 1474 | pdata[3] = (unsigned char)((*value >> 16) & 0xff); |
| 1475 | pdata[4] = (unsigned char)((*value >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1476 | len = 5; |
| 1477 | } |
| 1478 | else { /* unlikely */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1479 | PickleState *st = _Pickle_GetGlobalState(); |
| 1480 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1481 | "memo id too large for LONG_BINGET"); |
| 1482 | return -1; |
| 1483 | } |
| 1484 | } |
| 1485 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1486 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1487 | return -1; |
| 1488 | |
| 1489 | return 0; |
| 1490 | } |
| 1491 | |
| 1492 | /* Store an object in the memo, assign it a new unique ID based on the number |
| 1493 | of objects currently stored in the memo and generate a PUT opcode. */ |
| 1494 | static int |
| 1495 | memo_put(PicklerObject *self, PyObject *obj) |
| 1496 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1497 | char pdata[30]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1498 | Py_ssize_t len; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1499 | Py_ssize_t idx; |
| 1500 | |
| 1501 | const char memoize_op = MEMOIZE; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1502 | |
| 1503 | if (self->fast) |
| 1504 | return 0; |
| 1505 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1506 | idx = PyMemoTable_Size(self->memo); |
| 1507 | if (PyMemoTable_Set(self->memo, obj, idx) < 0) |
| 1508 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1509 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1510 | if (self->proto >= 4) { |
| 1511 | if (_Pickler_Write(self, &memoize_op, 1) < 0) |
| 1512 | return -1; |
| 1513 | return 0; |
| 1514 | } |
| 1515 | else if (!self->bin) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1516 | pdata[0] = PUT; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1517 | PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1518 | "%" PY_FORMAT_SIZE_T "d\n", idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1519 | len = strlen(pdata); |
| 1520 | } |
| 1521 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1522 | if (idx < 256) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1523 | pdata[0] = BINPUT; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1524 | pdata[1] = (unsigned char)idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1525 | len = 2; |
| 1526 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1527 | else if (idx <= 0xffffffffL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1528 | pdata[0] = LONG_BINPUT; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1529 | pdata[1] = (unsigned char)(idx & 0xff); |
| 1530 | pdata[2] = (unsigned char)((idx >> 8) & 0xff); |
| 1531 | pdata[3] = (unsigned char)((idx >> 16) & 0xff); |
| 1532 | pdata[4] = (unsigned char)((idx >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1533 | len = 5; |
| 1534 | } |
| 1535 | else { /* unlikely */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1536 | PickleState *st = _Pickle_GetGlobalState(); |
| 1537 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1538 | "memo id too large for LONG_BINPUT"); |
| 1539 | return -1; |
| 1540 | } |
| 1541 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1542 | if (_Pickler_Write(self, pdata, len) < 0) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1543 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1544 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1545 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1546 | } |
| 1547 | |
| 1548 | static PyObject * |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1549 | getattribute(PyObject *obj, PyObject *name, int allow_qualname) { |
| 1550 | PyObject *dotted_path; |
| 1551 | Py_ssize_t i; |
| 1552 | _Py_static_string(PyId_dot, "."); |
| 1553 | _Py_static_string(PyId_locals, "<locals>"); |
| 1554 | |
| 1555 | dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1); |
| 1556 | if (dotted_path == NULL) { |
| 1557 | return NULL; |
| 1558 | } |
| 1559 | assert(Py_SIZE(dotted_path) >= 1); |
| 1560 | if (!allow_qualname && Py_SIZE(dotted_path) > 1) { |
| 1561 | PyErr_Format(PyExc_AttributeError, |
| 1562 | "Can't get qualified attribute %R on %R;" |
| 1563 | "use protocols >= 4 to enable support", |
| 1564 | name, obj); |
| 1565 | Py_DECREF(dotted_path); |
| 1566 | return NULL; |
| 1567 | } |
| 1568 | Py_INCREF(obj); |
| 1569 | for (i = 0; i < Py_SIZE(dotted_path); i++) { |
| 1570 | PyObject *subpath = PyList_GET_ITEM(dotted_path, i); |
| 1571 | PyObject *tmp; |
| 1572 | PyObject *result = PyUnicode_RichCompare( |
| 1573 | subpath, _PyUnicode_FromId(&PyId_locals), Py_EQ); |
| 1574 | int is_equal = (result == Py_True); |
| 1575 | assert(PyBool_Check(result)); |
| 1576 | Py_DECREF(result); |
| 1577 | if (is_equal) { |
| 1578 | PyErr_Format(PyExc_AttributeError, |
| 1579 | "Can't get local attribute %R on %R", name, obj); |
| 1580 | Py_DECREF(dotted_path); |
| 1581 | Py_DECREF(obj); |
| 1582 | return NULL; |
| 1583 | } |
| 1584 | tmp = PyObject_GetAttr(obj, subpath); |
| 1585 | Py_DECREF(obj); |
| 1586 | if (tmp == NULL) { |
| 1587 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 1588 | PyErr_Clear(); |
| 1589 | PyErr_Format(PyExc_AttributeError, |
| 1590 | "Can't get attribute %R on %R", name, obj); |
| 1591 | } |
| 1592 | Py_DECREF(dotted_path); |
| 1593 | return NULL; |
| 1594 | } |
| 1595 | obj = tmp; |
| 1596 | } |
| 1597 | Py_DECREF(dotted_path); |
| 1598 | return obj; |
| 1599 | } |
| 1600 | |
| 1601 | static PyObject * |
| 1602 | whichmodule(PyObject *global, PyObject *global_name, int allow_qualname) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1603 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1604 | PyObject *module_name; |
| 1605 | PyObject *modules_dict; |
| 1606 | PyObject *module; |
| 1607 | PyObject *obj; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1608 | Py_ssize_t i, j; |
| 1609 | _Py_IDENTIFIER(__module__); |
| 1610 | _Py_IDENTIFIER(modules); |
| 1611 | _Py_IDENTIFIER(__main__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1612 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1613 | module_name = _PyObject_GetAttrId(global, &PyId___module__); |
| 1614 | |
| 1615 | if (module_name == NULL) { |
| 1616 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1617 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1618 | PyErr_Clear(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1619 | } |
| 1620 | else { |
| 1621 | /* In some rare cases (e.g., bound methods of extension types), |
| 1622 | __module__ can be None. If it is so, then search sys.modules for |
| 1623 | the module of global. */ |
| 1624 | if (module_name != Py_None) |
| 1625 | return module_name; |
| 1626 | Py_CLEAR(module_name); |
| 1627 | } |
| 1628 | assert(module_name == NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1629 | |
Victor Stinner | bb52020 | 2013-11-06 22:40:41 +0100 | [diff] [blame] | 1630 | modules_dict = _PySys_GetObjectId(&PyId_modules); |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 1631 | if (modules_dict == NULL) { |
| 1632 | PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1633 | return NULL; |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 1634 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1635 | |
| 1636 | i = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1637 | while ((j = PyDict_Next(modules_dict, &i, &module_name, &module))) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1638 | PyObject *result = PyUnicode_RichCompare( |
| 1639 | module_name, _PyUnicode_FromId(&PyId___main__), Py_EQ); |
| 1640 | int is_equal = (result == Py_True); |
| 1641 | assert(PyBool_Check(result)); |
| 1642 | Py_DECREF(result); |
| 1643 | if (is_equal) |
| 1644 | continue; |
| 1645 | if (module == Py_None) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1646 | continue; |
| 1647 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1648 | obj = getattribute(module, global_name, allow_qualname); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1649 | if (obj == NULL) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1650 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1651 | return NULL; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1652 | PyErr_Clear(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1653 | continue; |
| 1654 | } |
| 1655 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1656 | if (obj == global) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1657 | Py_DECREF(obj); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1658 | Py_INCREF(module_name); |
| 1659 | return module_name; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1660 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1661 | Py_DECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
| 1664 | /* If no module is found, use __main__. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1665 | module_name = _PyUnicode_FromId(&PyId___main__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1666 | Py_INCREF(module_name); |
| 1667 | return module_name; |
| 1668 | } |
| 1669 | |
| 1670 | /* fast_save_enter() and fast_save_leave() are guards against recursive |
| 1671 | objects when Pickler is used with the "fast mode" (i.e., with object |
| 1672 | memoization disabled). If the nesting of a list or dict object exceed |
| 1673 | FAST_NESTING_LIMIT, these guards will start keeping an internal |
| 1674 | reference to the seen list or dict objects and check whether these objects |
| 1675 | are recursive. These are not strictly necessary, since save() has a |
| 1676 | hard-coded recursion limit, but they give a nicer error message than the |
| 1677 | typical RuntimeError. */ |
| 1678 | static int |
| 1679 | fast_save_enter(PicklerObject *self, PyObject *obj) |
| 1680 | { |
| 1681 | /* if fast_nesting < 0, we're doing an error exit. */ |
| 1682 | if (++self->fast_nesting >= FAST_NESTING_LIMIT) { |
| 1683 | PyObject *key = NULL; |
| 1684 | if (self->fast_memo == NULL) { |
| 1685 | self->fast_memo = PyDict_New(); |
| 1686 | if (self->fast_memo == NULL) { |
| 1687 | self->fast_nesting = -1; |
| 1688 | return 0; |
| 1689 | } |
| 1690 | } |
| 1691 | key = PyLong_FromVoidPtr(obj); |
| 1692 | if (key == NULL) |
| 1693 | return 0; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame^] | 1694 | if (PyDict_GetItemWithError(self->fast_memo, key)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1695 | Py_DECREF(key); |
| 1696 | PyErr_Format(PyExc_ValueError, |
| 1697 | "fast mode: can't pickle cyclic objects " |
| 1698 | "including object type %.200s at %p", |
| 1699 | obj->ob_type->tp_name, obj); |
| 1700 | self->fast_nesting = -1; |
| 1701 | return 0; |
| 1702 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame^] | 1703 | if (PyErr_Occurred()) { |
| 1704 | return 0; |
| 1705 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1706 | if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) { |
| 1707 | Py_DECREF(key); |
| 1708 | self->fast_nesting = -1; |
| 1709 | return 0; |
| 1710 | } |
| 1711 | Py_DECREF(key); |
| 1712 | } |
| 1713 | return 1; |
| 1714 | } |
| 1715 | |
| 1716 | static int |
| 1717 | fast_save_leave(PicklerObject *self, PyObject *obj) |
| 1718 | { |
| 1719 | if (self->fast_nesting-- >= FAST_NESTING_LIMIT) { |
| 1720 | PyObject *key = PyLong_FromVoidPtr(obj); |
| 1721 | if (key == NULL) |
| 1722 | return 0; |
| 1723 | if (PyDict_DelItem(self->fast_memo, key) < 0) { |
| 1724 | Py_DECREF(key); |
| 1725 | return 0; |
| 1726 | } |
| 1727 | Py_DECREF(key); |
| 1728 | } |
| 1729 | return 1; |
| 1730 | } |
| 1731 | |
| 1732 | static int |
| 1733 | save_none(PicklerObject *self, PyObject *obj) |
| 1734 | { |
| 1735 | const char none_op = NONE; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1736 | if (_Pickler_Write(self, &none_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1737 | return -1; |
| 1738 | |
| 1739 | return 0; |
| 1740 | } |
| 1741 | |
| 1742 | static int |
| 1743 | save_bool(PicklerObject *self, PyObject *obj) |
| 1744 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1745 | if (self->proto >= 2) { |
Alexandre Vassalotti | 8a67f52 | 2013-11-24 21:40:18 -0800 | [diff] [blame] | 1746 | const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1747 | if (_Pickler_Write(self, &bool_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1748 | return -1; |
| 1749 | } |
Alexandre Vassalotti | 8a67f52 | 2013-11-24 21:40:18 -0800 | [diff] [blame] | 1750 | else { |
| 1751 | /* These aren't opcodes -- they're ways to pickle bools before protocol 2 |
| 1752 | * so that unpicklers written before bools were introduced unpickle them |
| 1753 | * as ints, but unpicklers after can recognize that bools were intended. |
| 1754 | * Note that protocol 2 added direct ways to pickle bools. |
| 1755 | */ |
| 1756 | const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n"; |
| 1757 | if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0) |
| 1758 | return -1; |
| 1759 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1760 | return 0; |
| 1761 | } |
| 1762 | |
| 1763 | static int |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1764 | save_long(PicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1765 | { |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1766 | PyObject *repr = NULL; |
| 1767 | Py_ssize_t size; |
| 1768 | long val; |
| 1769 | int status = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1770 | |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1771 | const char long_op = LONG; |
| 1772 | |
| 1773 | val= PyLong_AsLong(obj); |
| 1774 | if (val == -1 && PyErr_Occurred()) { |
| 1775 | /* out of range for int pickling */ |
| 1776 | PyErr_Clear(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1777 | } |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1778 | else if (self->bin && |
| 1779 | (sizeof(long) <= 4 || |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 1780 | (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) { |
| 1781 | /* result fits in a signed 4-byte integer. |
| 1782 | |
| 1783 | Note: we can't use -0x80000000L in the above condition because some |
| 1784 | compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type |
| 1785 | before applying the unary minus when sizeof(long) <= 4. The |
| 1786 | resulting value stays unsigned which is commonly not what we want, |
| 1787 | so MSVC happily warns us about it. However, that result would have |
| 1788 | been fine because we guard for sizeof(long) <= 4 which turns the |
| 1789 | condition true in that particular case. */ |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1790 | char pdata[32]; |
| 1791 | Py_ssize_t len = 0; |
| 1792 | |
| 1793 | pdata[1] = (unsigned char)(val & 0xff); |
| 1794 | pdata[2] = (unsigned char)((val >> 8) & 0xff); |
| 1795 | pdata[3] = (unsigned char)((val >> 16) & 0xff); |
| 1796 | pdata[4] = (unsigned char)((val >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1797 | |
| 1798 | if ((pdata[4] == 0) && (pdata[3] == 0)) { |
| 1799 | if (pdata[2] == 0) { |
| 1800 | pdata[0] = BININT1; |
| 1801 | len = 2; |
| 1802 | } |
| 1803 | else { |
| 1804 | pdata[0] = BININT2; |
| 1805 | len = 3; |
| 1806 | } |
| 1807 | } |
| 1808 | else { |
| 1809 | pdata[0] = BININT; |
| 1810 | len = 5; |
| 1811 | } |
| 1812 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1813 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1814 | return -1; |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1815 | |
| 1816 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1819 | if (self->proto >= 2) { |
| 1820 | /* Linear-time pickling. */ |
| 1821 | size_t nbits; |
| 1822 | size_t nbytes; |
| 1823 | unsigned char *pdata; |
| 1824 | char header[5]; |
| 1825 | int i; |
| 1826 | int sign = _PyLong_Sign(obj); |
| 1827 | |
| 1828 | if (sign == 0) { |
| 1829 | header[0] = LONG1; |
| 1830 | header[1] = 0; /* It's 0 -- an empty bytestring. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1831 | if (_Pickler_Write(self, header, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1832 | goto error; |
| 1833 | return 0; |
| 1834 | } |
| 1835 | nbits = _PyLong_NumBits(obj); |
| 1836 | if (nbits == (size_t)-1 && PyErr_Occurred()) |
| 1837 | goto error; |
| 1838 | /* How many bytes do we need? There are nbits >> 3 full |
| 1839 | * bytes of data, and nbits & 7 leftover bits. If there |
| 1840 | * are any leftover bits, then we clearly need another |
| 1841 | * byte. Wnat's not so obvious is that we *probably* |
| 1842 | * need another byte even if there aren't any leftovers: |
| 1843 | * the most-significant bit of the most-significant byte |
| 1844 | * acts like a sign bit, and it's usually got a sense |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1845 | * opposite of the one we need. The exception is ints |
| 1846 | * 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] | 1847 | * its own 256's-complement, so has the right sign bit |
| 1848 | * even without the extra byte. That's a pain to check |
| 1849 | * for in advance, though, so we always grab an extra |
| 1850 | * byte at the start, and cut it back later if possible. |
| 1851 | */ |
| 1852 | nbytes = (nbits >> 3) + 1; |
Antoine Pitrou | bf6ecf9 | 2012-11-24 20:40:21 +0100 | [diff] [blame] | 1853 | if (nbytes > 0x7fffffffL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1854 | PyErr_SetString(PyExc_OverflowError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1855 | "int too large to pickle"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1856 | goto error; |
| 1857 | } |
Neal Norwitz | 6ae2eb2 | 2008-08-24 23:50:08 +0000 | [diff] [blame] | 1858 | repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1859 | if (repr == NULL) |
| 1860 | goto error; |
Neal Norwitz | 6ae2eb2 | 2008-08-24 23:50:08 +0000 | [diff] [blame] | 1861 | pdata = (unsigned char *)PyBytes_AS_STRING(repr); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1862 | i = _PyLong_AsByteArray((PyLongObject *)obj, |
| 1863 | pdata, nbytes, |
| 1864 | 1 /* little endian */ , 1 /* signed */ ); |
| 1865 | if (i < 0) |
| 1866 | goto error; |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1867 | /* If the int is negative, this may be a byte more than |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1868 | * needed. This is so iff the MSB is all redundant sign |
| 1869 | * bits. |
| 1870 | */ |
| 1871 | if (sign < 0 && |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1872 | nbytes > 1 && |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1873 | pdata[nbytes - 1] == 0xff && |
| 1874 | (pdata[nbytes - 2] & 0x80) != 0) { |
| 1875 | nbytes--; |
| 1876 | } |
| 1877 | |
| 1878 | if (nbytes < 256) { |
| 1879 | header[0] = LONG1; |
| 1880 | header[1] = (unsigned char)nbytes; |
| 1881 | size = 2; |
| 1882 | } |
| 1883 | else { |
| 1884 | header[0] = LONG4; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1885 | size = (Py_ssize_t) nbytes; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1886 | for (i = 1; i < 5; i++) { |
| 1887 | header[i] = (unsigned char)(size & 0xff); |
| 1888 | size >>= 8; |
| 1889 | } |
| 1890 | size = 5; |
| 1891 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1892 | if (_Pickler_Write(self, header, size) < 0 || |
| 1893 | _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1894 | goto error; |
| 1895 | } |
| 1896 | else { |
| 1897 | char *string; |
| 1898 | |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 1899 | /* proto < 2: write the repr and newline. This is quadratic-time (in |
| 1900 | the number of digits), in both directions. We add a trailing 'L' |
| 1901 | to the repr, for compatibility with Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1902 | |
| 1903 | repr = PyObject_Repr(obj); |
| 1904 | if (repr == NULL) |
| 1905 | goto error; |
| 1906 | |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1907 | string = _PyUnicode_AsStringAndSize(repr, &size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1908 | if (string == NULL) |
| 1909 | goto error; |
| 1910 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1911 | if (_Pickler_Write(self, &long_op, 1) < 0 || |
| 1912 | _Pickler_Write(self, string, size) < 0 || |
| 1913 | _Pickler_Write(self, "L\n", 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1914 | goto error; |
| 1915 | } |
| 1916 | |
| 1917 | if (0) { |
| 1918 | error: |
| 1919 | status = -1; |
| 1920 | } |
| 1921 | Py_XDECREF(repr); |
| 1922 | |
| 1923 | return status; |
| 1924 | } |
| 1925 | |
| 1926 | static int |
| 1927 | save_float(PicklerObject *self, PyObject *obj) |
| 1928 | { |
| 1929 | double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj); |
| 1930 | |
| 1931 | if (self->bin) { |
| 1932 | char pdata[9]; |
| 1933 | pdata[0] = BINFLOAT; |
| 1934 | if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0) |
| 1935 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1936 | if (_Pickler_Write(self, pdata, 9) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1937 | return -1; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1938 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1939 | else { |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1940 | int result = -1; |
| 1941 | char *buf = NULL; |
| 1942 | char op = FLOAT; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1943 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1944 | if (_Pickler_Write(self, &op, 1) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1945 | goto done; |
| 1946 | |
Mark Dickinson | 3e09f43 | 2009-04-17 08:41:23 +0000 | [diff] [blame] | 1947 | buf = PyOS_double_to_string(x, 'g', 17, 0, NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1948 | if (!buf) { |
| 1949 | PyErr_NoMemory(); |
| 1950 | goto done; |
| 1951 | } |
| 1952 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1953 | if (_Pickler_Write(self, buf, strlen(buf)) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1954 | goto done; |
| 1955 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1956 | if (_Pickler_Write(self, "\n", 1) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1957 | goto done; |
| 1958 | |
| 1959 | result = 0; |
| 1960 | done: |
| 1961 | PyMem_Free(buf); |
| 1962 | return result; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1963 | } |
| 1964 | |
| 1965 | return 0; |
| 1966 | } |
| 1967 | |
| 1968 | static int |
| 1969 | save_bytes(PicklerObject *self, PyObject *obj) |
| 1970 | { |
| 1971 | if (self->proto < 3) { |
| 1972 | /* Older pickle protocols do not have an opcode for pickling bytes |
| 1973 | objects. Therefore, we need to fake the copy protocol (i.e., |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 1974 | the __reduce__ method) to permit bytes object unpickling. |
| 1975 | |
| 1976 | Here we use a hack to be compatible with Python 2. Since in Python |
| 1977 | 2 'bytes' is just an alias for 'str' (which has different |
| 1978 | parameters than the actual bytes object), we use codecs.encode |
| 1979 | to create the appropriate 'str' object when unpickled using |
| 1980 | Python 2 *and* the appropriate 'bytes' object when unpickled |
| 1981 | using Python 3. Again this is a hack and we don't need to do this |
| 1982 | with newer protocols. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1983 | PyObject *reduce_value = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1984 | int status; |
| 1985 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 1986 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 1987 | reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type); |
| 1988 | } |
| 1989 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1990 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 1991 | PyObject *unicode_str = |
| 1992 | PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj), |
| 1993 | PyBytes_GET_SIZE(obj), |
| 1994 | "strict"); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1995 | _Py_IDENTIFIER(latin1); |
| 1996 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 1997 | if (unicode_str == NULL) |
| 1998 | return -1; |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 1999 | reduce_value = Py_BuildValue("(O(OO))", |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 2000 | st->codecs_encode, unicode_str, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2001 | _PyUnicode_FromId(&PyId_latin1)); |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2002 | Py_DECREF(unicode_str); |
| 2003 | } |
| 2004 | |
| 2005 | if (reduce_value == NULL) |
| 2006 | return -1; |
| 2007 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2008 | /* save_reduce() will memoize the object automatically. */ |
| 2009 | status = save_reduce(self, reduce_value, obj); |
| 2010 | Py_DECREF(reduce_value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2011 | return status; |
| 2012 | } |
| 2013 | else { |
| 2014 | Py_ssize_t size; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2015 | char header[9]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2016 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2017 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2018 | size = PyBytes_GET_SIZE(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2019 | if (size < 0) |
| 2020 | return -1; |
| 2021 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2022 | if (size <= 0xff) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2023 | header[0] = SHORT_BINBYTES; |
| 2024 | header[1] = (unsigned char)size; |
| 2025 | len = 2; |
| 2026 | } |
| 2027 | else if (size <= 0xffffffffL) { |
| 2028 | header[0] = BINBYTES; |
| 2029 | header[1] = (unsigned char)(size & 0xff); |
| 2030 | header[2] = (unsigned char)((size >> 8) & 0xff); |
| 2031 | header[3] = (unsigned char)((size >> 16) & 0xff); |
| 2032 | header[4] = (unsigned char)((size >> 24) & 0xff); |
| 2033 | len = 5; |
| 2034 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2035 | else if (self->proto >= 4) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2036 | header[0] = BINBYTES8; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 2037 | _write_size64(header + 1, size); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2038 | len = 8; |
| 2039 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2040 | else { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2041 | PyErr_SetString(PyExc_OverflowError, |
Serhiy Storchaka | f8def28 | 2013-02-16 17:29:56 +0200 | [diff] [blame] | 2042 | "cannot serialize a bytes object larger than 4 GiB"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2043 | return -1; /* string too large */ |
| 2044 | } |
| 2045 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2046 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2047 | return -1; |
| 2048 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2049 | if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2050 | return -1; |
| 2051 | |
| 2052 | if (memo_put(self, obj) < 0) |
| 2053 | return -1; |
| 2054 | |
| 2055 | return 0; |
| 2056 | } |
| 2057 | } |
| 2058 | |
| 2059 | /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates |
| 2060 | backslash and newline characters to \uXXXX escapes. */ |
| 2061 | static PyObject * |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2062 | raw_unicode_escape(PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2063 | { |
| 2064 | PyObject *repr, *result; |
| 2065 | char *p; |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2066 | Py_ssize_t i, size, expandsize; |
| 2067 | void *data; |
| 2068 | unsigned int kind; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2069 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2070 | if (PyUnicode_READY(obj)) |
| 2071 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2072 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2073 | size = PyUnicode_GET_LENGTH(obj); |
| 2074 | data = PyUnicode_DATA(obj); |
| 2075 | kind = PyUnicode_KIND(obj); |
| 2076 | if (kind == PyUnicode_4BYTE_KIND) |
| 2077 | expandsize = 10; |
| 2078 | else |
| 2079 | expandsize = 6; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 2080 | |
Alexandre Vassalotti | 554d878 | 2008-12-27 07:32:41 +0000 | [diff] [blame] | 2081 | if (size > PY_SSIZE_T_MAX / expandsize) |
| 2082 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 554d878 | 2008-12-27 07:32:41 +0000 | [diff] [blame] | 2083 | repr = PyByteArray_FromStringAndSize(NULL, expandsize * size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2084 | if (repr == NULL) |
| 2085 | return NULL; |
| 2086 | if (size == 0) |
| 2087 | goto done; |
| 2088 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2089 | p = PyByteArray_AS_STRING(repr); |
| 2090 | for (i=0; i < size; i++) { |
| 2091 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2092 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 2093 | if (ch >= 0x10000) { |
| 2094 | *p++ = '\\'; |
| 2095 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 2096 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 2097 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 2098 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 2099 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 2100 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 2101 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 2102 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 2103 | *p++ = Py_hexdigits[ch & 15]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2104 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2105 | /* Map 16-bit characters to '\uxxxx' */ |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2106 | else if (ch >= 256 || ch == '\\' || ch == '\n') { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2107 | *p++ = '\\'; |
| 2108 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 2109 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 2110 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 2111 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 2112 | *p++ = Py_hexdigits[ch & 15]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2113 | } |
Alexandre Vassalotti | 554d878 | 2008-12-27 07:32:41 +0000 | [diff] [blame] | 2114 | /* Copy everything else as-is */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2115 | else |
| 2116 | *p++ = (char) ch; |
| 2117 | } |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2118 | size = p - PyByteArray_AS_STRING(repr); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2119 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2120 | done: |
Alexandre Vassalotti | 554d878 | 2008-12-27 07:32:41 +0000 | [diff] [blame] | 2121 | result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(repr), size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2122 | Py_DECREF(repr); |
| 2123 | return result; |
| 2124 | } |
| 2125 | |
| 2126 | static int |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2127 | write_utf8(PicklerObject *self, char *data, Py_ssize_t size) |
| 2128 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2129 | char header[9]; |
| 2130 | Py_ssize_t len; |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2131 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2132 | if (size <= 0xff && self->proto >= 4) { |
| 2133 | header[0] = SHORT_BINUNICODE; |
| 2134 | header[1] = (unsigned char)(size & 0xff); |
| 2135 | len = 2; |
| 2136 | } |
| 2137 | else if (size <= 0xffffffffUL) { |
| 2138 | header[0] = BINUNICODE; |
| 2139 | header[1] = (unsigned char)(size & 0xff); |
| 2140 | header[2] = (unsigned char)((size >> 8) & 0xff); |
| 2141 | header[3] = (unsigned char)((size >> 16) & 0xff); |
| 2142 | header[4] = (unsigned char)((size >> 24) & 0xff); |
| 2143 | len = 5; |
| 2144 | } |
| 2145 | else if (self->proto >= 4) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2146 | header[0] = BINUNICODE8; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 2147 | _write_size64(header + 1, size); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2148 | len = 9; |
| 2149 | } |
| 2150 | else { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2151 | PyErr_SetString(PyExc_OverflowError, |
Antoine Pitrou | 4b7b0f0 | 2013-04-07 23:46:52 +0200 | [diff] [blame] | 2152 | "cannot serialize a string larger than 4GiB"); |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2153 | return -1; |
| 2154 | } |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2155 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2156 | if (_Pickler_Write(self, header, len) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2157 | return -1; |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2158 | if (_Pickler_Write(self, data, size) < 0) |
| 2159 | return -1; |
| 2160 | |
| 2161 | return 0; |
| 2162 | } |
| 2163 | |
| 2164 | static int |
| 2165 | write_unicode_binary(PicklerObject *self, PyObject *obj) |
| 2166 | { |
| 2167 | PyObject *encoded = NULL; |
| 2168 | Py_ssize_t size; |
| 2169 | char *data; |
| 2170 | int r; |
| 2171 | |
| 2172 | if (PyUnicode_READY(obj)) |
| 2173 | return -1; |
| 2174 | |
| 2175 | data = PyUnicode_AsUTF8AndSize(obj, &size); |
| 2176 | if (data != NULL) |
| 2177 | return write_utf8(self, data, size); |
| 2178 | |
| 2179 | /* Issue #8383: for strings with lone surrogates, fallback on the |
| 2180 | "surrogatepass" error handler. */ |
| 2181 | PyErr_Clear(); |
| 2182 | encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass"); |
| 2183 | if (encoded == NULL) |
| 2184 | return -1; |
| 2185 | |
| 2186 | r = write_utf8(self, PyBytes_AS_STRING(encoded), |
| 2187 | PyBytes_GET_SIZE(encoded)); |
| 2188 | Py_DECREF(encoded); |
| 2189 | return r; |
| 2190 | } |
| 2191 | |
| 2192 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2193 | save_unicode(PicklerObject *self, PyObject *obj) |
| 2194 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2195 | if (self->bin) { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2196 | if (write_unicode_binary(self, obj) < 0) |
| 2197 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2198 | } |
| 2199 | else { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2200 | PyObject *encoded; |
| 2201 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2202 | const char unicode_op = UNICODE; |
| 2203 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2204 | encoded = raw_unicode_escape(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2205 | if (encoded == NULL) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2206 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2207 | |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2208 | if (_Pickler_Write(self, &unicode_op, 1) < 0) { |
| 2209 | Py_DECREF(encoded); |
| 2210 | return -1; |
| 2211 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2212 | |
| 2213 | size = PyBytes_GET_SIZE(encoded); |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2214 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) { |
| 2215 | Py_DECREF(encoded); |
| 2216 | return -1; |
| 2217 | } |
| 2218 | Py_DECREF(encoded); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2219 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2220 | if (_Pickler_Write(self, "\n", 1) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2221 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2222 | } |
| 2223 | if (memo_put(self, obj) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2224 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2225 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2226 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2227 | } |
| 2228 | |
| 2229 | /* A helper for save_tuple. Push the len elements in tuple t on the stack. */ |
| 2230 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2231 | store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2232 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2233 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2234 | |
| 2235 | assert(PyTuple_Size(t) == len); |
| 2236 | |
| 2237 | for (i = 0; i < len; i++) { |
| 2238 | PyObject *element = PyTuple_GET_ITEM(t, i); |
| 2239 | |
| 2240 | if (element == NULL) |
| 2241 | return -1; |
| 2242 | if (save(self, element, 0) < 0) |
| 2243 | return -1; |
| 2244 | } |
| 2245 | |
| 2246 | return 0; |
| 2247 | } |
| 2248 | |
| 2249 | /* Tuples are ubiquitous in the pickle protocols, so many techniques are |
| 2250 | * used across protocols to minimize the space needed to pickle them. |
| 2251 | * Tuples are also the only builtin immutable type that can be recursive |
| 2252 | * (a tuple can be reached from itself), and that requires some subtle |
| 2253 | * magic so that it works in all cases. IOW, this is a long routine. |
| 2254 | */ |
| 2255 | static int |
| 2256 | save_tuple(PicklerObject *self, PyObject *obj) |
| 2257 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2258 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2259 | |
| 2260 | const char mark_op = MARK; |
| 2261 | const char tuple_op = TUPLE; |
| 2262 | const char pop_op = POP; |
| 2263 | const char pop_mark_op = POP_MARK; |
| 2264 | const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3}; |
| 2265 | |
| 2266 | if ((len = PyTuple_Size(obj)) < 0) |
| 2267 | return -1; |
| 2268 | |
| 2269 | if (len == 0) { |
| 2270 | char pdata[2]; |
| 2271 | |
| 2272 | if (self->proto) { |
| 2273 | pdata[0] = EMPTY_TUPLE; |
| 2274 | len = 1; |
| 2275 | } |
| 2276 | else { |
| 2277 | pdata[0] = MARK; |
| 2278 | pdata[1] = TUPLE; |
| 2279 | len = 2; |
| 2280 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2281 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2282 | return -1; |
| 2283 | return 0; |
| 2284 | } |
| 2285 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2286 | /* 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] | 2287 | * saving the tuple elements, the tuple must be recursive, in |
| 2288 | * which case we'll pop everything we put on the stack, and fetch |
| 2289 | * its value from the memo. |
| 2290 | */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2291 | if (len <= 3 && self->proto >= 2) { |
| 2292 | /* Use TUPLE{1,2,3} opcodes. */ |
| 2293 | if (store_tuple_elements(self, obj, len) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2294 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2295 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2296 | if (PyMemoTable_Get(self->memo, obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2297 | /* pop the len elements */ |
| 2298 | for (i = 0; i < len; i++) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2299 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 2300 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2301 | /* fetch from memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2302 | if (memo_get(self, obj) < 0) |
| 2303 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2304 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2305 | return 0; |
| 2306 | } |
| 2307 | else { /* Not recursive. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2308 | if (_Pickler_Write(self, len2opcode + len, 1) < 0) |
| 2309 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2310 | } |
| 2311 | goto memoize; |
| 2312 | } |
| 2313 | |
| 2314 | /* proto < 2 and len > 0, or proto >= 2 and len > 3. |
| 2315 | * Generate MARK e1 e2 ... TUPLE |
| 2316 | */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2317 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2318 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2319 | |
| 2320 | if (store_tuple_elements(self, obj, len) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2321 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2322 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2323 | if (PyMemoTable_Get(self->memo, obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2324 | /* pop the stack stuff we pushed */ |
| 2325 | if (self->bin) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2326 | if (_Pickler_Write(self, &pop_mark_op, 1) < 0) |
| 2327 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2328 | } |
| 2329 | else { |
| 2330 | /* Note that we pop one more than len, to remove |
| 2331 | * the MARK too. |
| 2332 | */ |
| 2333 | for (i = 0; i <= len; i++) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2334 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 2335 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2336 | } |
| 2337 | /* fetch from memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2338 | if (memo_get(self, obj) < 0) |
| 2339 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2340 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2341 | return 0; |
| 2342 | } |
| 2343 | else { /* Not recursive. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2344 | if (_Pickler_Write(self, &tuple_op, 1) < 0) |
| 2345 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2346 | } |
| 2347 | |
| 2348 | memoize: |
| 2349 | if (memo_put(self, obj) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2350 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2351 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2352 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2353 | } |
| 2354 | |
| 2355 | /* iter is an iterator giving items, and we batch up chunks of |
| 2356 | * MARK item item ... item APPENDS |
| 2357 | * opcode sequences. Calling code should have arranged to first create an |
| 2358 | * empty list, or list-like object, for the APPENDS to operate on. |
| 2359 | * Returns 0 on success, <0 on error. |
| 2360 | */ |
| 2361 | static int |
| 2362 | batch_list(PicklerObject *self, PyObject *iter) |
| 2363 | { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2364 | PyObject *obj = NULL; |
| 2365 | PyObject *firstitem = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2366 | int i, n; |
| 2367 | |
| 2368 | const char mark_op = MARK; |
| 2369 | const char append_op = APPEND; |
| 2370 | const char appends_op = APPENDS; |
| 2371 | |
| 2372 | assert(iter != NULL); |
| 2373 | |
| 2374 | /* XXX: I think this function could be made faster by avoiding the |
| 2375 | iterator interface and fetching objects directly from list using |
| 2376 | PyList_GET_ITEM. |
| 2377 | */ |
| 2378 | |
| 2379 | if (self->proto == 0) { |
| 2380 | /* APPENDS isn't available; do one at a time. */ |
| 2381 | for (;;) { |
| 2382 | obj = PyIter_Next(iter); |
| 2383 | if (obj == NULL) { |
| 2384 | if (PyErr_Occurred()) |
| 2385 | return -1; |
| 2386 | break; |
| 2387 | } |
| 2388 | i = save(self, obj, 0); |
| 2389 | Py_DECREF(obj); |
| 2390 | if (i < 0) |
| 2391 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2392 | if (_Pickler_Write(self, &append_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2393 | return -1; |
| 2394 | } |
| 2395 | return 0; |
| 2396 | } |
| 2397 | |
| 2398 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 2399 | do { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2400 | /* Get first item */ |
| 2401 | firstitem = PyIter_Next(iter); |
| 2402 | if (firstitem == NULL) { |
| 2403 | if (PyErr_Occurred()) |
| 2404 | goto error; |
| 2405 | |
| 2406 | /* nothing more to add */ |
| 2407 | break; |
| 2408 | } |
| 2409 | |
| 2410 | /* Try to get a second item */ |
| 2411 | obj = PyIter_Next(iter); |
| 2412 | if (obj == NULL) { |
| 2413 | if (PyErr_Occurred()) |
| 2414 | goto error; |
| 2415 | |
| 2416 | /* Only one item to write */ |
| 2417 | if (save(self, firstitem, 0) < 0) |
| 2418 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2419 | if (_Pickler_Write(self, &append_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2420 | goto error; |
| 2421 | Py_CLEAR(firstitem); |
| 2422 | break; |
| 2423 | } |
| 2424 | |
| 2425 | /* More than one item to write */ |
| 2426 | |
| 2427 | /* Pump out MARK, items, APPENDS. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2428 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2429 | goto error; |
| 2430 | |
| 2431 | if (save(self, firstitem, 0) < 0) |
| 2432 | goto error; |
| 2433 | Py_CLEAR(firstitem); |
| 2434 | n = 1; |
| 2435 | |
| 2436 | /* Fetch and save up to BATCHSIZE items */ |
| 2437 | while (obj) { |
| 2438 | if (save(self, obj, 0) < 0) |
| 2439 | goto error; |
| 2440 | Py_CLEAR(obj); |
| 2441 | n += 1; |
| 2442 | |
| 2443 | if (n == BATCHSIZE) |
| 2444 | break; |
| 2445 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2446 | obj = PyIter_Next(iter); |
| 2447 | if (obj == NULL) { |
| 2448 | if (PyErr_Occurred()) |
| 2449 | goto error; |
| 2450 | break; |
| 2451 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2452 | } |
| 2453 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2454 | if (_Pickler_Write(self, &appends_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2455 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2456 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2457 | } while (n == BATCHSIZE); |
| 2458 | return 0; |
| 2459 | |
| 2460 | error: |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2461 | Py_XDECREF(firstitem); |
| 2462 | Py_XDECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2463 | return -1; |
| 2464 | } |
| 2465 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2466 | /* This is a variant of batch_list() above, specialized for lists (with no |
| 2467 | * support for list subclasses). Like batch_list(), we batch up chunks of |
| 2468 | * MARK item item ... item APPENDS |
| 2469 | * opcode sequences. Calling code should have arranged to first create an |
| 2470 | * empty list, or list-like object, for the APPENDS to operate on. |
| 2471 | * Returns 0 on success, -1 on error. |
| 2472 | * |
| 2473 | * This version is considerably faster than batch_list(), if less general. |
| 2474 | * |
| 2475 | * Note that this only works for protocols > 0. |
| 2476 | */ |
| 2477 | static int |
| 2478 | batch_list_exact(PicklerObject *self, PyObject *obj) |
| 2479 | { |
| 2480 | PyObject *item = NULL; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2481 | Py_ssize_t this_batch, total; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2482 | |
| 2483 | const char append_op = APPEND; |
| 2484 | const char appends_op = APPENDS; |
| 2485 | const char mark_op = MARK; |
| 2486 | |
| 2487 | assert(obj != NULL); |
| 2488 | assert(self->proto > 0); |
| 2489 | assert(PyList_CheckExact(obj)); |
| 2490 | |
| 2491 | if (PyList_GET_SIZE(obj) == 1) { |
| 2492 | item = PyList_GET_ITEM(obj, 0); |
| 2493 | if (save(self, item, 0) < 0) |
| 2494 | return -1; |
| 2495 | if (_Pickler_Write(self, &append_op, 1) < 0) |
| 2496 | return -1; |
| 2497 | return 0; |
| 2498 | } |
| 2499 | |
| 2500 | /* Write in batches of BATCHSIZE. */ |
| 2501 | total = 0; |
| 2502 | do { |
| 2503 | this_batch = 0; |
| 2504 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2505 | return -1; |
| 2506 | while (total < PyList_GET_SIZE(obj)) { |
| 2507 | item = PyList_GET_ITEM(obj, total); |
| 2508 | if (save(self, item, 0) < 0) |
| 2509 | return -1; |
| 2510 | total++; |
| 2511 | if (++this_batch == BATCHSIZE) |
| 2512 | break; |
| 2513 | } |
| 2514 | if (_Pickler_Write(self, &appends_op, 1) < 0) |
| 2515 | return -1; |
| 2516 | |
| 2517 | } while (total < PyList_GET_SIZE(obj)); |
| 2518 | |
| 2519 | return 0; |
| 2520 | } |
| 2521 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2522 | static int |
| 2523 | save_list(PicklerObject *self, PyObject *obj) |
| 2524 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2525 | char header[3]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2526 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2527 | int status = 0; |
| 2528 | |
| 2529 | if (self->fast && !fast_save_enter(self, obj)) |
| 2530 | goto error; |
| 2531 | |
| 2532 | /* Create an empty list. */ |
| 2533 | if (self->bin) { |
| 2534 | header[0] = EMPTY_LIST; |
| 2535 | len = 1; |
| 2536 | } |
| 2537 | else { |
| 2538 | header[0] = MARK; |
| 2539 | header[1] = LIST; |
| 2540 | len = 2; |
| 2541 | } |
| 2542 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2543 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2544 | goto error; |
| 2545 | |
| 2546 | /* Get list length, and bow out early if empty. */ |
| 2547 | if ((len = PyList_Size(obj)) < 0) |
| 2548 | goto error; |
| 2549 | |
| 2550 | if (memo_put(self, obj) < 0) |
| 2551 | goto error; |
| 2552 | |
| 2553 | if (len != 0) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2554 | /* Materialize the list elements. */ |
| 2555 | if (PyList_CheckExact(obj) && self->proto > 0) { |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2556 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 2557 | goto error; |
| 2558 | status = batch_list_exact(self, obj); |
| 2559 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2560 | } else { |
| 2561 | PyObject *iter = PyObject_GetIter(obj); |
| 2562 | if (iter == NULL) |
| 2563 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2564 | |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2565 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 2566 | Py_DECREF(iter); |
| 2567 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2568 | } |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2569 | status = batch_list(self, iter); |
| 2570 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2571 | Py_DECREF(iter); |
| 2572 | } |
| 2573 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2574 | if (0) { |
| 2575 | error: |
| 2576 | status = -1; |
| 2577 | } |
| 2578 | |
| 2579 | if (self->fast && !fast_save_leave(self, obj)) |
| 2580 | status = -1; |
| 2581 | |
| 2582 | return status; |
| 2583 | } |
| 2584 | |
| 2585 | /* iter is an iterator giving (key, value) pairs, and we batch up chunks of |
| 2586 | * MARK key value ... key value SETITEMS |
| 2587 | * opcode sequences. Calling code should have arranged to first create an |
| 2588 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 2589 | * Returns 0 on success, <0 on error. |
| 2590 | * |
| 2591 | * This is very much like batch_list(). The difference between saving |
| 2592 | * elements directly, and picking apart two-tuples, is so long-winded at |
| 2593 | * the C level, though, that attempts to combine these routines were too |
| 2594 | * ugly to bear. |
| 2595 | */ |
| 2596 | static int |
| 2597 | batch_dict(PicklerObject *self, PyObject *iter) |
| 2598 | { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2599 | PyObject *obj = NULL; |
| 2600 | PyObject *firstitem = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2601 | int i, n; |
| 2602 | |
| 2603 | const char mark_op = MARK; |
| 2604 | const char setitem_op = SETITEM; |
| 2605 | const char setitems_op = SETITEMS; |
| 2606 | |
| 2607 | assert(iter != NULL); |
| 2608 | |
| 2609 | if (self->proto == 0) { |
| 2610 | /* SETITEMS isn't available; do one at a time. */ |
| 2611 | for (;;) { |
| 2612 | obj = PyIter_Next(iter); |
| 2613 | if (obj == NULL) { |
| 2614 | if (PyErr_Occurred()) |
| 2615 | return -1; |
| 2616 | break; |
| 2617 | } |
| 2618 | if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) { |
| 2619 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2620 | "iterator must return 2-tuples"); |
| 2621 | return -1; |
| 2622 | } |
| 2623 | i = save(self, PyTuple_GET_ITEM(obj, 0), 0); |
| 2624 | if (i >= 0) |
| 2625 | i = save(self, PyTuple_GET_ITEM(obj, 1), 0); |
| 2626 | Py_DECREF(obj); |
| 2627 | if (i < 0) |
| 2628 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2629 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2630 | return -1; |
| 2631 | } |
| 2632 | return 0; |
| 2633 | } |
| 2634 | |
| 2635 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 2636 | do { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2637 | /* Get first item */ |
| 2638 | firstitem = PyIter_Next(iter); |
| 2639 | if (firstitem == NULL) { |
| 2640 | if (PyErr_Occurred()) |
| 2641 | goto error; |
| 2642 | |
| 2643 | /* nothing more to add */ |
| 2644 | break; |
| 2645 | } |
| 2646 | if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) { |
| 2647 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2648 | "iterator must return 2-tuples"); |
| 2649 | goto error; |
| 2650 | } |
| 2651 | |
| 2652 | /* Try to get a second item */ |
| 2653 | obj = PyIter_Next(iter); |
| 2654 | if (obj == NULL) { |
| 2655 | if (PyErr_Occurred()) |
| 2656 | goto error; |
| 2657 | |
| 2658 | /* Only one item to write */ |
| 2659 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 2660 | goto error; |
| 2661 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 2662 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2663 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2664 | goto error; |
| 2665 | Py_CLEAR(firstitem); |
| 2666 | break; |
| 2667 | } |
| 2668 | |
| 2669 | /* More than one item to write */ |
| 2670 | |
| 2671 | /* Pump out MARK, items, SETITEMS. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2672 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2673 | goto error; |
| 2674 | |
| 2675 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 2676 | goto error; |
| 2677 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 2678 | goto error; |
| 2679 | Py_CLEAR(firstitem); |
| 2680 | n = 1; |
| 2681 | |
| 2682 | /* Fetch and save up to BATCHSIZE items */ |
| 2683 | while (obj) { |
| 2684 | if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) { |
| 2685 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2686 | "iterator must return 2-tuples"); |
| 2687 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2688 | } |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2689 | if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 || |
| 2690 | save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0) |
| 2691 | goto error; |
| 2692 | Py_CLEAR(obj); |
| 2693 | n += 1; |
| 2694 | |
| 2695 | if (n == BATCHSIZE) |
| 2696 | break; |
| 2697 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2698 | obj = PyIter_Next(iter); |
| 2699 | if (obj == NULL) { |
| 2700 | if (PyErr_Occurred()) |
| 2701 | goto error; |
| 2702 | break; |
| 2703 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2704 | } |
| 2705 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2706 | if (_Pickler_Write(self, &setitems_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2707 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2708 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2709 | } while (n == BATCHSIZE); |
| 2710 | return 0; |
| 2711 | |
| 2712 | error: |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2713 | Py_XDECREF(firstitem); |
| 2714 | Py_XDECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2715 | return -1; |
| 2716 | } |
| 2717 | |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2718 | /* This is a variant of batch_dict() above that specializes for dicts, with no |
| 2719 | * support for dict subclasses. Like batch_dict(), we batch up chunks of |
| 2720 | * MARK key value ... key value SETITEMS |
| 2721 | * opcode sequences. Calling code should have arranged to first create an |
| 2722 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 2723 | * Returns 0 on success, -1 on error. |
| 2724 | * |
| 2725 | * Note that this currently doesn't work for protocol 0. |
| 2726 | */ |
| 2727 | static int |
| 2728 | batch_dict_exact(PicklerObject *self, PyObject *obj) |
| 2729 | { |
| 2730 | PyObject *key = NULL, *value = NULL; |
| 2731 | int i; |
| 2732 | Py_ssize_t dict_size, ppos = 0; |
| 2733 | |
Alexandre Vassalotti | f70b129 | 2009-05-25 18:00:52 +0000 | [diff] [blame] | 2734 | const char mark_op = MARK; |
| 2735 | const char setitem_op = SETITEM; |
| 2736 | const char setitems_op = SETITEMS; |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2737 | |
| 2738 | assert(obj != NULL); |
| 2739 | assert(self->proto > 0); |
| 2740 | |
| 2741 | dict_size = PyDict_Size(obj); |
| 2742 | |
| 2743 | /* Special-case len(d) == 1 to save space. */ |
| 2744 | if (dict_size == 1) { |
| 2745 | PyDict_Next(obj, &ppos, &key, &value); |
| 2746 | if (save(self, key, 0) < 0) |
| 2747 | return -1; |
| 2748 | if (save(self, value, 0) < 0) |
| 2749 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2750 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2751 | return -1; |
| 2752 | return 0; |
| 2753 | } |
| 2754 | |
| 2755 | /* Write in batches of BATCHSIZE. */ |
| 2756 | do { |
| 2757 | i = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2758 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2759 | return -1; |
| 2760 | while (PyDict_Next(obj, &ppos, &key, &value)) { |
| 2761 | if (save(self, key, 0) < 0) |
| 2762 | return -1; |
| 2763 | if (save(self, value, 0) < 0) |
| 2764 | return -1; |
| 2765 | if (++i == BATCHSIZE) |
| 2766 | break; |
| 2767 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2768 | if (_Pickler_Write(self, &setitems_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2769 | return -1; |
| 2770 | if (PyDict_Size(obj) != dict_size) { |
| 2771 | PyErr_Format( |
| 2772 | PyExc_RuntimeError, |
| 2773 | "dictionary changed size during iteration"); |
| 2774 | return -1; |
| 2775 | } |
| 2776 | |
| 2777 | } while (i == BATCHSIZE); |
| 2778 | return 0; |
| 2779 | } |
| 2780 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2781 | static int |
| 2782 | save_dict(PicklerObject *self, PyObject *obj) |
| 2783 | { |
| 2784 | PyObject *items, *iter; |
| 2785 | char header[3]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2786 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2787 | int status = 0; |
| 2788 | |
| 2789 | if (self->fast && !fast_save_enter(self, obj)) |
| 2790 | goto error; |
| 2791 | |
| 2792 | /* Create an empty dict. */ |
| 2793 | if (self->bin) { |
| 2794 | header[0] = EMPTY_DICT; |
| 2795 | len = 1; |
| 2796 | } |
| 2797 | else { |
| 2798 | header[0] = MARK; |
| 2799 | header[1] = DICT; |
| 2800 | len = 2; |
| 2801 | } |
| 2802 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2803 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2804 | goto error; |
| 2805 | |
| 2806 | /* Get dict size, and bow out early if empty. */ |
| 2807 | if ((len = PyDict_Size(obj)) < 0) |
| 2808 | goto error; |
| 2809 | |
| 2810 | if (memo_put(self, obj) < 0) |
| 2811 | goto error; |
| 2812 | |
| 2813 | if (len != 0) { |
| 2814 | /* Save the dict items. */ |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2815 | if (PyDict_CheckExact(obj) && self->proto > 0) { |
| 2816 | /* We can take certain shortcuts if we know this is a dict and |
| 2817 | not a dict subclass. */ |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2818 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 2819 | goto error; |
| 2820 | status = batch_dict_exact(self, obj); |
| 2821 | Py_LeaveRecursiveCall(); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2822 | } else { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2823 | _Py_IDENTIFIER(items); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2824 | |
| 2825 | items = _PyObject_CallMethodId(obj, &PyId_items, "()"); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2826 | if (items == NULL) |
| 2827 | goto error; |
| 2828 | iter = PyObject_GetIter(items); |
| 2829 | Py_DECREF(items); |
| 2830 | if (iter == NULL) |
| 2831 | goto error; |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2832 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 2833 | Py_DECREF(iter); |
| 2834 | goto error; |
| 2835 | } |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2836 | status = batch_dict(self, iter); |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2837 | Py_LeaveRecursiveCall(); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2838 | Py_DECREF(iter); |
| 2839 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2840 | } |
| 2841 | |
| 2842 | if (0) { |
| 2843 | error: |
| 2844 | status = -1; |
| 2845 | } |
| 2846 | |
| 2847 | if (self->fast && !fast_save_leave(self, obj)) |
| 2848 | status = -1; |
| 2849 | |
| 2850 | return status; |
| 2851 | } |
| 2852 | |
| 2853 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2854 | save_set(PicklerObject *self, PyObject *obj) |
| 2855 | { |
| 2856 | PyObject *item; |
| 2857 | int i; |
| 2858 | Py_ssize_t set_size, ppos = 0; |
| 2859 | Py_hash_t hash; |
| 2860 | |
| 2861 | const char empty_set_op = EMPTY_SET; |
| 2862 | const char mark_op = MARK; |
| 2863 | const char additems_op = ADDITEMS; |
| 2864 | |
| 2865 | if (self->proto < 4) { |
| 2866 | PyObject *items; |
| 2867 | PyObject *reduce_value; |
| 2868 | int status; |
| 2869 | |
| 2870 | items = PySequence_List(obj); |
| 2871 | if (items == NULL) { |
| 2872 | return -1; |
| 2873 | } |
| 2874 | reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items); |
| 2875 | Py_DECREF(items); |
| 2876 | if (reduce_value == NULL) { |
| 2877 | return -1; |
| 2878 | } |
| 2879 | /* save_reduce() will memoize the object automatically. */ |
| 2880 | status = save_reduce(self, reduce_value, obj); |
| 2881 | Py_DECREF(reduce_value); |
| 2882 | return status; |
| 2883 | } |
| 2884 | |
| 2885 | if (_Pickler_Write(self, &empty_set_op, 1) < 0) |
| 2886 | return -1; |
| 2887 | |
| 2888 | if (memo_put(self, obj) < 0) |
| 2889 | return -1; |
| 2890 | |
| 2891 | set_size = PySet_GET_SIZE(obj); |
| 2892 | if (set_size == 0) |
| 2893 | return 0; /* nothing to do */ |
| 2894 | |
| 2895 | /* Write in batches of BATCHSIZE. */ |
| 2896 | do { |
| 2897 | i = 0; |
| 2898 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2899 | return -1; |
| 2900 | while (_PySet_NextEntry(obj, &ppos, &item, &hash)) { |
| 2901 | if (save(self, item, 0) < 0) |
| 2902 | return -1; |
| 2903 | if (++i == BATCHSIZE) |
| 2904 | break; |
| 2905 | } |
| 2906 | if (_Pickler_Write(self, &additems_op, 1) < 0) |
| 2907 | return -1; |
| 2908 | if (PySet_GET_SIZE(obj) != set_size) { |
| 2909 | PyErr_Format( |
| 2910 | PyExc_RuntimeError, |
| 2911 | "set changed size during iteration"); |
| 2912 | return -1; |
| 2913 | } |
| 2914 | } while (i == BATCHSIZE); |
| 2915 | |
| 2916 | return 0; |
| 2917 | } |
| 2918 | |
| 2919 | static int |
| 2920 | save_frozenset(PicklerObject *self, PyObject *obj) |
| 2921 | { |
| 2922 | PyObject *iter; |
| 2923 | |
| 2924 | const char mark_op = MARK; |
| 2925 | const char frozenset_op = FROZENSET; |
| 2926 | |
| 2927 | if (self->fast && !fast_save_enter(self, obj)) |
| 2928 | return -1; |
| 2929 | |
| 2930 | if (self->proto < 4) { |
| 2931 | PyObject *items; |
| 2932 | PyObject *reduce_value; |
| 2933 | int status; |
| 2934 | |
| 2935 | items = PySequence_List(obj); |
| 2936 | if (items == NULL) { |
| 2937 | return -1; |
| 2938 | } |
| 2939 | reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type, |
| 2940 | items); |
| 2941 | Py_DECREF(items); |
| 2942 | if (reduce_value == NULL) { |
| 2943 | return -1; |
| 2944 | } |
| 2945 | /* save_reduce() will memoize the object automatically. */ |
| 2946 | status = save_reduce(self, reduce_value, obj); |
| 2947 | Py_DECREF(reduce_value); |
| 2948 | return status; |
| 2949 | } |
| 2950 | |
| 2951 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2952 | return -1; |
| 2953 | |
| 2954 | iter = PyObject_GetIter(obj); |
Christian Heimes | b3d3ee4 | 2013-11-23 21:01:40 +0100 | [diff] [blame] | 2955 | if (iter == NULL) { |
Christian Heimes | 74d8d63 | 2013-11-23 21:05:31 +0100 | [diff] [blame] | 2956 | return -1; |
Christian Heimes | b3d3ee4 | 2013-11-23 21:01:40 +0100 | [diff] [blame] | 2957 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2958 | for (;;) { |
| 2959 | PyObject *item; |
| 2960 | |
| 2961 | item = PyIter_Next(iter); |
| 2962 | if (item == NULL) { |
| 2963 | if (PyErr_Occurred()) { |
| 2964 | Py_DECREF(iter); |
| 2965 | return -1; |
| 2966 | } |
| 2967 | break; |
| 2968 | } |
| 2969 | if (save(self, item, 0) < 0) { |
| 2970 | Py_DECREF(item); |
| 2971 | Py_DECREF(iter); |
| 2972 | return -1; |
| 2973 | } |
| 2974 | Py_DECREF(item); |
| 2975 | } |
| 2976 | Py_DECREF(iter); |
| 2977 | |
| 2978 | /* If the object is already in the memo, this means it is |
| 2979 | recursive. In this case, throw away everything we put on the |
| 2980 | stack, and fetch the object back from the memo. */ |
| 2981 | if (PyMemoTable_Get(self->memo, obj)) { |
| 2982 | const char pop_mark_op = POP_MARK; |
| 2983 | |
| 2984 | if (_Pickler_Write(self, &pop_mark_op, 1) < 0) |
| 2985 | return -1; |
| 2986 | if (memo_get(self, obj) < 0) |
| 2987 | return -1; |
| 2988 | return 0; |
| 2989 | } |
| 2990 | |
| 2991 | if (_Pickler_Write(self, &frozenset_op, 1) < 0) |
| 2992 | return -1; |
| 2993 | if (memo_put(self, obj) < 0) |
| 2994 | return -1; |
| 2995 | |
| 2996 | return 0; |
| 2997 | } |
| 2998 | |
| 2999 | static int |
| 3000 | fix_imports(PyObject **module_name, PyObject **global_name) |
| 3001 | { |
| 3002 | PyObject *key; |
| 3003 | PyObject *item; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3004 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3005 | |
| 3006 | key = PyTuple_Pack(2, *module_name, *global_name); |
| 3007 | if (key == NULL) |
| 3008 | return -1; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3009 | item = PyDict_GetItemWithError(st->name_mapping_3to2, key); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3010 | Py_DECREF(key); |
| 3011 | if (item) { |
| 3012 | PyObject *fixed_module_name; |
| 3013 | PyObject *fixed_global_name; |
| 3014 | |
| 3015 | if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
| 3016 | PyErr_Format(PyExc_RuntimeError, |
| 3017 | "_compat_pickle.REVERSE_NAME_MAPPING values " |
| 3018 | "should be 2-tuples, not %.200s", |
| 3019 | Py_TYPE(item)->tp_name); |
| 3020 | return -1; |
| 3021 | } |
| 3022 | fixed_module_name = PyTuple_GET_ITEM(item, 0); |
| 3023 | fixed_global_name = PyTuple_GET_ITEM(item, 1); |
| 3024 | if (!PyUnicode_Check(fixed_module_name) || |
| 3025 | !PyUnicode_Check(fixed_global_name)) { |
| 3026 | PyErr_Format(PyExc_RuntimeError, |
| 3027 | "_compat_pickle.REVERSE_NAME_MAPPING values " |
| 3028 | "should be pairs of str, not (%.200s, %.200s)", |
| 3029 | Py_TYPE(fixed_module_name)->tp_name, |
| 3030 | Py_TYPE(fixed_global_name)->tp_name); |
| 3031 | return -1; |
| 3032 | } |
| 3033 | |
| 3034 | Py_CLEAR(*module_name); |
| 3035 | Py_CLEAR(*global_name); |
| 3036 | Py_INCREF(fixed_module_name); |
| 3037 | Py_INCREF(fixed_global_name); |
| 3038 | *module_name = fixed_module_name; |
| 3039 | *global_name = fixed_global_name; |
| 3040 | } |
| 3041 | else if (PyErr_Occurred()) { |
| 3042 | return -1; |
| 3043 | } |
| 3044 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3045 | item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3046 | if (item) { |
| 3047 | if (!PyUnicode_Check(item)) { |
| 3048 | PyErr_Format(PyExc_RuntimeError, |
| 3049 | "_compat_pickle.REVERSE_IMPORT_MAPPING values " |
| 3050 | "should be strings, not %.200s", |
| 3051 | Py_TYPE(item)->tp_name); |
| 3052 | return -1; |
| 3053 | } |
| 3054 | Py_CLEAR(*module_name); |
| 3055 | Py_INCREF(item); |
| 3056 | *module_name = item; |
| 3057 | } |
| 3058 | else if (PyErr_Occurred()) { |
| 3059 | return -1; |
| 3060 | } |
| 3061 | |
| 3062 | return 0; |
| 3063 | } |
| 3064 | |
| 3065 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3066 | save_global(PicklerObject *self, PyObject *obj, PyObject *name) |
| 3067 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3068 | PyObject *global_name = NULL; |
| 3069 | PyObject *module_name = NULL; |
| 3070 | PyObject *module = NULL; |
| 3071 | PyObject *cls; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3072 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3073 | int status = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3074 | _Py_IDENTIFIER(__name__); |
| 3075 | _Py_IDENTIFIER(__qualname__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3076 | |
| 3077 | const char global_op = GLOBAL; |
| 3078 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3079 | if (name) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3080 | Py_INCREF(name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3081 | global_name = name; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3082 | } |
| 3083 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3084 | if (self->proto >= 4) { |
| 3085 | global_name = _PyObject_GetAttrId(obj, &PyId___qualname__); |
| 3086 | if (global_name == NULL) { |
| 3087 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 3088 | goto error; |
| 3089 | PyErr_Clear(); |
| 3090 | } |
| 3091 | } |
| 3092 | if (global_name == NULL) { |
| 3093 | global_name = _PyObject_GetAttrId(obj, &PyId___name__); |
| 3094 | if (global_name == NULL) |
| 3095 | goto error; |
| 3096 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3097 | } |
| 3098 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3099 | module_name = whichmodule(obj, global_name, self->proto >= 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3100 | if (module_name == NULL) |
| 3101 | goto error; |
| 3102 | |
| 3103 | /* XXX: Change to use the import C API directly with level=0 to disallow |
| 3104 | relative imports. |
| 3105 | |
| 3106 | XXX: PyImport_ImportModuleLevel could be used. However, this bypasses |
| 3107 | builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore |
| 3108 | custom import functions (IMHO, this would be a nice security |
| 3109 | feature). The import C API would need to be extended to support the |
| 3110 | extra parameters of __import__ to fix that. */ |
| 3111 | module = PyImport_Import(module_name); |
| 3112 | if (module == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3113 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3114 | "Can't pickle %R: import of module %R failed", |
| 3115 | obj, module_name); |
| 3116 | goto error; |
| 3117 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3118 | cls = getattribute(module, global_name, self->proto >= 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3119 | if (cls == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3120 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3121 | "Can't pickle %R: attribute lookup %S on %S failed", |
| 3122 | obj, global_name, module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3123 | goto error; |
| 3124 | } |
| 3125 | if (cls != obj) { |
| 3126 | Py_DECREF(cls); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3127 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3128 | "Can't pickle %R: it's not the same object as %S.%S", |
| 3129 | obj, module_name, global_name); |
| 3130 | goto error; |
| 3131 | } |
| 3132 | Py_DECREF(cls); |
| 3133 | |
| 3134 | if (self->proto >= 2) { |
| 3135 | /* See whether this is in the extension registry, and if |
| 3136 | * so generate an EXT opcode. |
| 3137 | */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3138 | PyObject *extension_key; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3139 | PyObject *code_obj; /* extension code as Python object */ |
| 3140 | long code; /* extension code as C value */ |
| 3141 | char pdata[5]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3142 | Py_ssize_t n; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3143 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3144 | extension_key = PyTuple_Pack(2, module_name, global_name); |
| 3145 | if (extension_key == NULL) { |
| 3146 | goto error; |
| 3147 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame^] | 3148 | code_obj = PyDict_GetItemWithError(st->extension_registry, |
| 3149 | extension_key); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3150 | Py_DECREF(extension_key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3151 | /* The object is not registered in the extension registry. |
| 3152 | This is the most likely code path. */ |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame^] | 3153 | if (code_obj == NULL) { |
| 3154 | if (PyErr_Occurred()) { |
| 3155 | goto error; |
| 3156 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3157 | goto gen_global; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame^] | 3158 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3159 | |
| 3160 | /* XXX: pickle.py doesn't check neither the type, nor the range |
| 3161 | of the value returned by the extension_registry. It should for |
| 3162 | consistency. */ |
| 3163 | |
| 3164 | /* Verify code_obj has the right type and value. */ |
| 3165 | if (!PyLong_Check(code_obj)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3166 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3167 | "Can't pickle %R: extension code %R isn't an integer", |
| 3168 | obj, code_obj); |
| 3169 | goto error; |
| 3170 | } |
| 3171 | code = PyLong_AS_LONG(code_obj); |
| 3172 | if (code <= 0 || code > 0x7fffffffL) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3173 | if (!PyErr_Occurred()) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3174 | PyErr_Format(st->PicklingError, "Can't pickle %R: extension " |
| 3175 | "code %ld is out of range", obj, code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3176 | goto error; |
| 3177 | } |
| 3178 | |
| 3179 | /* Generate an EXT opcode. */ |
| 3180 | if (code <= 0xff) { |
| 3181 | pdata[0] = EXT1; |
| 3182 | pdata[1] = (unsigned char)code; |
| 3183 | n = 2; |
| 3184 | } |
| 3185 | else if (code <= 0xffff) { |
| 3186 | pdata[0] = EXT2; |
| 3187 | pdata[1] = (unsigned char)(code & 0xff); |
| 3188 | pdata[2] = (unsigned char)((code >> 8) & 0xff); |
| 3189 | n = 3; |
| 3190 | } |
| 3191 | else { |
| 3192 | pdata[0] = EXT4; |
| 3193 | pdata[1] = (unsigned char)(code & 0xff); |
| 3194 | pdata[2] = (unsigned char)((code >> 8) & 0xff); |
| 3195 | pdata[3] = (unsigned char)((code >> 16) & 0xff); |
| 3196 | pdata[4] = (unsigned char)((code >> 24) & 0xff); |
| 3197 | n = 5; |
| 3198 | } |
| 3199 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3200 | if (_Pickler_Write(self, pdata, n) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3201 | goto error; |
| 3202 | } |
| 3203 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3204 | gen_global: |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3205 | if (self->proto >= 4) { |
| 3206 | const char stack_global_op = STACK_GLOBAL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3207 | |
Christian Heimes | e8b1ba1 | 2013-11-23 21:13:39 +0100 | [diff] [blame] | 3208 | if (save(self, module_name, 0) < 0) |
| 3209 | goto error; |
| 3210 | if (save(self, global_name, 0) < 0) |
| 3211 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3212 | |
| 3213 | if (_Pickler_Write(self, &stack_global_op, 1) < 0) |
| 3214 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3215 | } |
| 3216 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3217 | /* Generate a normal global opcode if we are using a pickle |
| 3218 | protocol < 4, or if the object is not registered in the |
| 3219 | extension registry. */ |
| 3220 | PyObject *encoded; |
| 3221 | PyObject *(*unicode_encoder)(PyObject *); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3222 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3223 | if (_Pickler_Write(self, &global_op, 1) < 0) |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3224 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3225 | |
| 3226 | /* For protocol < 3 and if the user didn't request against doing |
| 3227 | so, we convert module names to the old 2.x module names. */ |
| 3228 | if (self->proto < 3 && self->fix_imports) { |
| 3229 | if (fix_imports(&module_name, &global_name) < 0) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3230 | goto error; |
| 3231 | } |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3232 | } |
| 3233 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3234 | /* Since Python 3.0 now supports non-ASCII identifiers, we encode |
| 3235 | both the module name and the global name using UTF-8. We do so |
| 3236 | only when we are using the pickle protocol newer than version |
| 3237 | 3. This is to ensure compatibility with older Unpickler running |
| 3238 | on Python 2.x. */ |
| 3239 | if (self->proto == 3) { |
| 3240 | unicode_encoder = PyUnicode_AsUTF8String; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3241 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3242 | else { |
| 3243 | unicode_encoder = PyUnicode_AsASCIIString; |
| 3244 | } |
| 3245 | encoded = unicode_encoder(module_name); |
| 3246 | if (encoded == NULL) { |
| 3247 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3248 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3249 | "can't pickle module identifier '%S' using " |
| 3250 | "pickle protocol %i", |
| 3251 | module_name, self->proto); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3252 | goto error; |
| 3253 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3254 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), |
| 3255 | PyBytes_GET_SIZE(encoded)) < 0) { |
| 3256 | Py_DECREF(encoded); |
| 3257 | goto error; |
| 3258 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3259 | Py_DECREF(encoded); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3260 | if(_Pickler_Write(self, "\n", 1) < 0) |
| 3261 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3262 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3263 | /* Save the name of the module. */ |
| 3264 | encoded = unicode_encoder(global_name); |
| 3265 | if (encoded == NULL) { |
| 3266 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3267 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3268 | "can't pickle global identifier '%S' using " |
| 3269 | "pickle protocol %i", |
| 3270 | global_name, self->proto); |
| 3271 | goto error; |
| 3272 | } |
| 3273 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), |
| 3274 | PyBytes_GET_SIZE(encoded)) < 0) { |
| 3275 | Py_DECREF(encoded); |
| 3276 | goto error; |
| 3277 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3278 | Py_DECREF(encoded); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3279 | if (_Pickler_Write(self, "\n", 1) < 0) |
| 3280 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3281 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3282 | /* Memoize the object. */ |
| 3283 | if (memo_put(self, obj) < 0) |
| 3284 | goto error; |
| 3285 | } |
| 3286 | |
| 3287 | if (0) { |
| 3288 | error: |
| 3289 | status = -1; |
| 3290 | } |
| 3291 | Py_XDECREF(module_name); |
| 3292 | Py_XDECREF(global_name); |
| 3293 | Py_XDECREF(module); |
| 3294 | |
| 3295 | return status; |
| 3296 | } |
| 3297 | |
| 3298 | static int |
| 3299 | save_pers(PicklerObject *self, PyObject *obj, PyObject *func) |
| 3300 | { |
| 3301 | PyObject *pid = NULL; |
| 3302 | int status = 0; |
| 3303 | |
| 3304 | const char persid_op = PERSID; |
| 3305 | const char binpersid_op = BINPERSID; |
| 3306 | |
| 3307 | Py_INCREF(obj); |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 3308 | pid = _Pickle_FastCall(func, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3309 | if (pid == NULL) |
| 3310 | return -1; |
| 3311 | |
| 3312 | if (pid != Py_None) { |
| 3313 | if (self->bin) { |
| 3314 | if (save(self, pid, 1) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3315 | _Pickler_Write(self, &binpersid_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3316 | goto error; |
| 3317 | } |
| 3318 | else { |
| 3319 | PyObject *pid_str = NULL; |
| 3320 | char *pid_ascii_bytes; |
| 3321 | Py_ssize_t size; |
| 3322 | |
| 3323 | pid_str = PyObject_Str(pid); |
| 3324 | if (pid_str == NULL) |
| 3325 | goto error; |
| 3326 | |
| 3327 | /* XXX: Should it check whether the persistent id only contains |
| 3328 | ASCII characters? And what if the pid contains embedded |
| 3329 | newlines? */ |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 3330 | pid_ascii_bytes = _PyUnicode_AsStringAndSize(pid_str, &size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3331 | Py_DECREF(pid_str); |
| 3332 | if (pid_ascii_bytes == NULL) |
| 3333 | goto error; |
| 3334 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3335 | if (_Pickler_Write(self, &persid_op, 1) < 0 || |
| 3336 | _Pickler_Write(self, pid_ascii_bytes, size) < 0 || |
| 3337 | _Pickler_Write(self, "\n", 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3338 | goto error; |
| 3339 | } |
| 3340 | status = 1; |
| 3341 | } |
| 3342 | |
| 3343 | if (0) { |
| 3344 | error: |
| 3345 | status = -1; |
| 3346 | } |
| 3347 | Py_XDECREF(pid); |
| 3348 | |
| 3349 | return status; |
| 3350 | } |
| 3351 | |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3352 | static PyObject * |
| 3353 | get_class(PyObject *obj) |
| 3354 | { |
| 3355 | PyObject *cls; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3356 | _Py_IDENTIFIER(__class__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3357 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3358 | cls = _PyObject_GetAttrId(obj, &PyId___class__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3359 | if (cls == NULL) { |
| 3360 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 3361 | PyErr_Clear(); |
| 3362 | cls = (PyObject *) Py_TYPE(obj); |
| 3363 | Py_INCREF(cls); |
| 3364 | } |
| 3365 | } |
| 3366 | return cls; |
| 3367 | } |
| 3368 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3369 | /* We're saving obj, and args is the 2-thru-5 tuple returned by the |
| 3370 | * appropriate __reduce__ method for obj. |
| 3371 | */ |
| 3372 | static int |
| 3373 | save_reduce(PicklerObject *self, PyObject *args, PyObject *obj) |
| 3374 | { |
| 3375 | PyObject *callable; |
| 3376 | PyObject *argtup; |
| 3377 | PyObject *state = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3378 | PyObject *listitems = Py_None; |
| 3379 | PyObject *dictitems = Py_None; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3380 | PickleState *st = _Pickle_GetGlobalState(); |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3381 | Py_ssize_t size; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3382 | int use_newobj = 0, use_newobj_ex = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3383 | |
| 3384 | const char reduce_op = REDUCE; |
| 3385 | const char build_op = BUILD; |
| 3386 | const char newobj_op = NEWOBJ; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3387 | const char newobj_ex_op = NEWOBJ_EX; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3388 | |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3389 | size = PyTuple_Size(args); |
| 3390 | if (size < 2 || size > 5) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3391 | PyErr_SetString(st->PicklingError, "tuple returned by " |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3392 | "__reduce__ must contain 2 through 5 elements"); |
| 3393 | return -1; |
| 3394 | } |
| 3395 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3396 | if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5, |
| 3397 | &callable, &argtup, &state, &listitems, &dictitems)) |
| 3398 | return -1; |
| 3399 | |
| 3400 | if (!PyCallable_Check(callable)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3401 | PyErr_SetString(st->PicklingError, "first item of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3402 | "returned by __reduce__ must be callable"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3403 | return -1; |
| 3404 | } |
| 3405 | if (!PyTuple_Check(argtup)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3406 | PyErr_SetString(st->PicklingError, "second item of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3407 | "returned by __reduce__ must be a tuple"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3408 | return -1; |
| 3409 | } |
| 3410 | |
| 3411 | if (state == Py_None) |
| 3412 | state = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3413 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3414 | if (listitems == Py_None) |
| 3415 | listitems = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3416 | else if (!PyIter_Check(listitems)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3417 | PyErr_Format(st->PicklingError, "fourth element of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3418 | "returned by __reduce__ must be an iterator, not %s", |
| 3419 | Py_TYPE(listitems)->tp_name); |
| 3420 | return -1; |
| 3421 | } |
| 3422 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3423 | if (dictitems == Py_None) |
| 3424 | dictitems = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3425 | else if (!PyIter_Check(dictitems)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3426 | PyErr_Format(st->PicklingError, "fifth element of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3427 | "returned by __reduce__ must be an iterator, not %s", |
| 3428 | Py_TYPE(dictitems)->tp_name); |
| 3429 | return -1; |
| 3430 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3431 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3432 | if (self->proto >= 2) { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3433 | PyObject *name; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3434 | _Py_IDENTIFIER(__name__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3435 | |
Victor Stinner | 804e05e | 2013-11-14 01:26:17 +0100 | [diff] [blame] | 3436 | name = _PyObject_GetAttrId(callable, &PyId___name__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3437 | if (name == NULL) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3438 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3439 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3440 | } |
| 3441 | PyErr_Clear(); |
| 3442 | } |
| 3443 | else if (self->proto >= 4) { |
| 3444 | _Py_IDENTIFIER(__newobj_ex__); |
| 3445 | use_newobj_ex = PyUnicode_Check(name) && |
| 3446 | PyUnicode_Compare( |
| 3447 | name, _PyUnicode_FromId(&PyId___newobj_ex__)) == 0; |
| 3448 | Py_DECREF(name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3449 | } |
| 3450 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3451 | _Py_IDENTIFIER(__newobj__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3452 | use_newobj = PyUnicode_Check(name) && |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3453 | PyUnicode_Compare( |
| 3454 | name, _PyUnicode_FromId(&PyId___newobj__)) == 0; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3455 | Py_DECREF(name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3456 | } |
| 3457 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3458 | |
| 3459 | if (use_newobj_ex) { |
| 3460 | PyObject *cls; |
| 3461 | PyObject *args; |
| 3462 | PyObject *kwargs; |
| 3463 | |
| 3464 | if (Py_SIZE(argtup) != 3) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3465 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3466 | "length of the NEWOBJ_EX argument tuple must be " |
| 3467 | "exactly 3, not %zd", Py_SIZE(argtup)); |
| 3468 | return -1; |
| 3469 | } |
| 3470 | |
| 3471 | cls = PyTuple_GET_ITEM(argtup, 0); |
| 3472 | if (!PyType_Check(cls)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3473 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3474 | "first item from NEWOBJ_EX argument tuple must " |
| 3475 | "be a class, not %.200s", Py_TYPE(cls)->tp_name); |
| 3476 | return -1; |
| 3477 | } |
| 3478 | args = PyTuple_GET_ITEM(argtup, 1); |
| 3479 | if (!PyTuple_Check(args)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3480 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3481 | "second item from NEWOBJ_EX argument tuple must " |
| 3482 | "be a tuple, not %.200s", Py_TYPE(args)->tp_name); |
| 3483 | return -1; |
| 3484 | } |
| 3485 | kwargs = PyTuple_GET_ITEM(argtup, 2); |
| 3486 | if (!PyDict_Check(kwargs)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3487 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3488 | "third item from NEWOBJ_EX argument tuple must " |
| 3489 | "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name); |
| 3490 | return -1; |
| 3491 | } |
| 3492 | |
| 3493 | if (save(self, cls, 0) < 0 || |
| 3494 | save(self, args, 0) < 0 || |
| 3495 | save(self, kwargs, 0) < 0 || |
| 3496 | _Pickler_Write(self, &newobj_ex_op, 1) < 0) { |
| 3497 | return -1; |
| 3498 | } |
| 3499 | } |
| 3500 | else if (use_newobj) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3501 | PyObject *cls; |
| 3502 | PyObject *newargtup; |
| 3503 | PyObject *obj_class; |
| 3504 | int p; |
| 3505 | |
| 3506 | /* Sanity checks. */ |
| 3507 | if (Py_SIZE(argtup) < 1) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3508 | PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3509 | return -1; |
| 3510 | } |
| 3511 | |
| 3512 | cls = PyTuple_GET_ITEM(argtup, 0); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3513 | if (!PyType_Check(cls)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3514 | PyErr_SetString(st->PicklingError, "args[0] from " |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3515 | "__newobj__ args is not a type"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3516 | return -1; |
| 3517 | } |
| 3518 | |
| 3519 | if (obj != NULL) { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3520 | obj_class = get_class(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3521 | p = obj_class != cls; /* true iff a problem */ |
| 3522 | Py_DECREF(obj_class); |
| 3523 | if (p) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3524 | PyErr_SetString(st->PicklingError, "args[0] from " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3525 | "__newobj__ args has the wrong class"); |
| 3526 | return -1; |
| 3527 | } |
| 3528 | } |
| 3529 | /* XXX: These calls save() are prone to infinite recursion. Imagine |
| 3530 | what happen if the value returned by the __reduce__() method of |
| 3531 | some extension type contains another object of the same type. Ouch! |
| 3532 | |
| 3533 | Here is a quick example, that I ran into, to illustrate what I |
| 3534 | mean: |
| 3535 | |
| 3536 | >>> import pickle, copyreg |
| 3537 | >>> copyreg.dispatch_table.pop(complex) |
| 3538 | >>> pickle.dumps(1+2j) |
| 3539 | Traceback (most recent call last): |
| 3540 | ... |
| 3541 | RuntimeError: maximum recursion depth exceeded |
| 3542 | |
| 3543 | Removing the complex class from copyreg.dispatch_table made the |
| 3544 | __reduce_ex__() method emit another complex object: |
| 3545 | |
| 3546 | >>> (1+1j).__reduce_ex__(2) |
| 3547 | (<function __newobj__ at 0xb7b71c3c>, |
| 3548 | (<class 'complex'>, (1+1j)), None, None, None) |
| 3549 | |
| 3550 | Thus when save() was called on newargstup (the 2nd item) recursion |
| 3551 | ensued. Of course, the bug was in the complex class which had a |
| 3552 | broken __getnewargs__() that emitted another complex object. But, |
| 3553 | the point, here, is it is quite easy to end up with a broken reduce |
| 3554 | function. */ |
| 3555 | |
| 3556 | /* Save the class and its __new__ arguments. */ |
| 3557 | if (save(self, cls, 0) < 0) |
| 3558 | return -1; |
| 3559 | |
| 3560 | newargtup = PyTuple_GetSlice(argtup, 1, Py_SIZE(argtup)); |
| 3561 | if (newargtup == NULL) |
| 3562 | return -1; |
| 3563 | |
| 3564 | p = save(self, newargtup, 0); |
| 3565 | Py_DECREF(newargtup); |
| 3566 | if (p < 0) |
| 3567 | return -1; |
| 3568 | |
| 3569 | /* Add NEWOBJ opcode. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3570 | if (_Pickler_Write(self, &newobj_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3571 | return -1; |
| 3572 | } |
| 3573 | else { /* Not using NEWOBJ. */ |
| 3574 | if (save(self, callable, 0) < 0 || |
| 3575 | save(self, argtup, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3576 | _Pickler_Write(self, &reduce_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3577 | return -1; |
| 3578 | } |
| 3579 | |
| 3580 | /* obj can be NULL when save_reduce() is used directly. A NULL obj means |
| 3581 | the caller do not want to memoize the object. Not particularly useful, |
| 3582 | but that is to mimic the behavior save_reduce() in pickle.py when |
| 3583 | obj is None. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3584 | if (obj != NULL) { |
| 3585 | /* If the object is already in the memo, this means it is |
| 3586 | recursive. In this case, throw away everything we put on the |
| 3587 | stack, and fetch the object back from the memo. */ |
| 3588 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3589 | const char pop_op = POP; |
| 3590 | |
| 3591 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 3592 | return -1; |
| 3593 | if (memo_get(self, obj) < 0) |
| 3594 | return -1; |
| 3595 | |
| 3596 | return 0; |
| 3597 | } |
| 3598 | else if (memo_put(self, obj) < 0) |
| 3599 | return -1; |
| 3600 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3601 | |
| 3602 | if (listitems && batch_list(self, listitems) < 0) |
| 3603 | return -1; |
| 3604 | |
| 3605 | if (dictitems && batch_dict(self, dictitems) < 0) |
| 3606 | return -1; |
| 3607 | |
| 3608 | if (state) { |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 3609 | if (save(self, state, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3610 | _Pickler_Write(self, &build_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3611 | return -1; |
| 3612 | } |
| 3613 | |
| 3614 | return 0; |
| 3615 | } |
| 3616 | |
| 3617 | static int |
| 3618 | save(PicklerObject *self, PyObject *obj, int pers_save) |
| 3619 | { |
| 3620 | PyTypeObject *type; |
| 3621 | PyObject *reduce_func = NULL; |
| 3622 | PyObject *reduce_value = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3623 | int status = 0; |
| 3624 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 3625 | if (_Pickler_OpcodeBoundary(self) < 0) |
| 3626 | return -1; |
| 3627 | |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 3628 | if (Py_EnterRecursiveCall(" while pickling an object")) |
Alexandre Vassalotti | dff1834 | 2008-07-13 18:48:30 +0000 | [diff] [blame] | 3629 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3630 | |
| 3631 | /* The extra pers_save argument is necessary to avoid calling save_pers() |
| 3632 | on its returned object. */ |
| 3633 | if (!pers_save && self->pers_func) { |
| 3634 | /* save_pers() returns: |
| 3635 | -1 to signal an error; |
| 3636 | 0 if it did nothing successfully; |
| 3637 | 1 if a persistent id was saved. |
| 3638 | */ |
| 3639 | if ((status = save_pers(self, obj, self->pers_func)) != 0) |
| 3640 | goto done; |
| 3641 | } |
| 3642 | |
| 3643 | type = Py_TYPE(obj); |
| 3644 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3645 | /* The old cPickle had an optimization that used switch-case statement |
| 3646 | dispatching on the first letter of the type name. This has was removed |
| 3647 | since benchmarks shown that this optimization was actually slowing |
| 3648 | things down. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3649 | |
| 3650 | /* Atom types; these aren't memoized, so don't check the memo. */ |
| 3651 | |
| 3652 | if (obj == Py_None) { |
| 3653 | status = save_none(self, obj); |
| 3654 | goto done; |
| 3655 | } |
| 3656 | else if (obj == Py_False || obj == Py_True) { |
| 3657 | status = save_bool(self, obj); |
| 3658 | goto done; |
| 3659 | } |
| 3660 | else if (type == &PyLong_Type) { |
| 3661 | status = save_long(self, obj); |
| 3662 | goto done; |
| 3663 | } |
| 3664 | else if (type == &PyFloat_Type) { |
| 3665 | status = save_float(self, obj); |
| 3666 | goto done; |
| 3667 | } |
| 3668 | |
| 3669 | /* Check the memo to see if it has the object. If so, generate |
| 3670 | a GET (or BINGET) opcode, instead of pickling the object |
| 3671 | once again. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3672 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3673 | if (memo_get(self, obj) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3674 | goto error; |
| 3675 | goto done; |
| 3676 | } |
| 3677 | |
| 3678 | if (type == &PyBytes_Type) { |
| 3679 | status = save_bytes(self, obj); |
| 3680 | goto done; |
| 3681 | } |
| 3682 | else if (type == &PyUnicode_Type) { |
| 3683 | status = save_unicode(self, obj); |
| 3684 | goto done; |
| 3685 | } |
| 3686 | else if (type == &PyDict_Type) { |
| 3687 | status = save_dict(self, obj); |
| 3688 | goto done; |
| 3689 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3690 | else if (type == &PySet_Type) { |
| 3691 | status = save_set(self, obj); |
| 3692 | goto done; |
| 3693 | } |
| 3694 | else if (type == &PyFrozenSet_Type) { |
| 3695 | status = save_frozenset(self, obj); |
| 3696 | goto done; |
| 3697 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3698 | else if (type == &PyList_Type) { |
| 3699 | status = save_list(self, obj); |
| 3700 | goto done; |
| 3701 | } |
| 3702 | else if (type == &PyTuple_Type) { |
| 3703 | status = save_tuple(self, obj); |
| 3704 | goto done; |
| 3705 | } |
| 3706 | else if (type == &PyType_Type) { |
| 3707 | status = save_global(self, obj, NULL); |
| 3708 | goto done; |
| 3709 | } |
| 3710 | else if (type == &PyFunction_Type) { |
| 3711 | status = save_global(self, obj, NULL); |
Alexandre Vassalotti | fc91285 | 2013-11-24 03:07:35 -0800 | [diff] [blame] | 3712 | goto done; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3713 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3714 | |
| 3715 | /* XXX: This part needs some unit tests. */ |
| 3716 | |
| 3717 | /* Get a reduction callable, and call it. This may come from |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3718 | * self.dispatch_table, copyreg.dispatch_table, the object's |
| 3719 | * __reduce_ex__ method, or the object's __reduce__ method. |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3720 | */ |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3721 | if (self->dispatch_table == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3722 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame^] | 3723 | reduce_func = PyDict_GetItemWithError(st->dispatch_table, |
| 3724 | (PyObject *)type); |
| 3725 | if (reduce_func == NULL) { |
| 3726 | if (PyErr_Occurred()) { |
| 3727 | goto error; |
| 3728 | } |
| 3729 | } else { |
| 3730 | /* PyDict_GetItemWithError() returns a borrowed reference. |
| 3731 | Increase the reference count to be consistent with |
| 3732 | PyObject_GetItem and _PyObject_GetAttrId used below. */ |
| 3733 | Py_INCREF(reduce_func); |
| 3734 | } |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3735 | } else { |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame^] | 3736 | reduce_func = PyObject_GetItem(self->dispatch_table, |
| 3737 | (PyObject *)type); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3738 | if (reduce_func == NULL) { |
| 3739 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 3740 | PyErr_Clear(); |
| 3741 | else |
| 3742 | goto error; |
| 3743 | } |
| 3744 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3745 | if (reduce_func != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3746 | Py_INCREF(obj); |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 3747 | reduce_value = _Pickle_FastCall(reduce_func, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3748 | } |
Antoine Pitrou | ffd41d9 | 2011-10-04 09:23:04 +0200 | [diff] [blame] | 3749 | else if (PyType_IsSubtype(type, &PyType_Type)) { |
| 3750 | status = save_global(self, obj, NULL); |
| 3751 | goto done; |
| 3752 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3753 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3754 | _Py_IDENTIFIER(__reduce__); |
| 3755 | _Py_IDENTIFIER(__reduce_ex__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3756 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3757 | |
| 3758 | /* XXX: If the __reduce__ method is defined, __reduce_ex__ is |
| 3759 | automatically defined as __reduce__. While this is convenient, this |
| 3760 | make it impossible to know which method was actually called. Of |
| 3761 | course, this is not a big deal. But still, it would be nice to let |
| 3762 | the user know which method was called when something go |
| 3763 | wrong. Incidentally, this means if __reduce_ex__ is not defined, we |
| 3764 | don't actually have to check for a __reduce__ method. */ |
| 3765 | |
| 3766 | /* Check for a __reduce_ex__ method. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3767 | reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3768 | if (reduce_func != NULL) { |
| 3769 | PyObject *proto; |
| 3770 | proto = PyLong_FromLong(self->proto); |
| 3771 | if (proto != NULL) { |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 3772 | reduce_value = _Pickle_FastCall(reduce_func, proto); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3773 | } |
| 3774 | } |
| 3775 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3776 | PickleState *st = _Pickle_GetGlobalState(); |
| 3777 | |
| 3778 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3779 | PyErr_Clear(); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3780 | } |
| 3781 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3782 | goto error; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3783 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3784 | /* Check for a __reduce__ method. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3785 | reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3786 | if (reduce_func != NULL) { |
Alexandre Vassalotti | 6bf41e5 | 2013-11-28 15:17:29 -0800 | [diff] [blame] | 3787 | PyObject *empty_tuple = PyTuple_New(0); |
| 3788 | reduce_value = PyObject_Call(reduce_func, empty_tuple, |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3789 | NULL); |
Alexandre Vassalotti | 6bf41e5 | 2013-11-28 15:17:29 -0800 | [diff] [blame] | 3790 | Py_DECREF(empty_tuple); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3791 | } |
| 3792 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3793 | PyErr_Format(st->PicklingError, |
| 3794 | "can't pickle '%.200s' object: %R", |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3795 | type->tp_name, obj); |
| 3796 | goto error; |
| 3797 | } |
| 3798 | } |
| 3799 | } |
| 3800 | |
| 3801 | if (reduce_value == NULL) |
| 3802 | goto error; |
| 3803 | |
| 3804 | if (PyUnicode_Check(reduce_value)) { |
| 3805 | status = save_global(self, obj, reduce_value); |
| 3806 | goto done; |
| 3807 | } |
| 3808 | |
| 3809 | if (!PyTuple_Check(reduce_value)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3810 | PickleState *st = _Pickle_GetGlobalState(); |
| 3811 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3812 | "__reduce__ must return a string or tuple"); |
| 3813 | goto error; |
| 3814 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3815 | |
| 3816 | status = save_reduce(self, reduce_value, obj); |
| 3817 | |
| 3818 | if (0) { |
| 3819 | error: |
| 3820 | status = -1; |
| 3821 | } |
| 3822 | done: |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 3823 | |
Alexandre Vassalotti | dff1834 | 2008-07-13 18:48:30 +0000 | [diff] [blame] | 3824 | Py_LeaveRecursiveCall(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3825 | Py_XDECREF(reduce_func); |
| 3826 | Py_XDECREF(reduce_value); |
| 3827 | |
| 3828 | return status; |
| 3829 | } |
| 3830 | |
| 3831 | static int |
| 3832 | dump(PicklerObject *self, PyObject *obj) |
| 3833 | { |
| 3834 | const char stop_op = STOP; |
| 3835 | |
| 3836 | if (self->proto >= 2) { |
| 3837 | char header[2]; |
| 3838 | |
| 3839 | header[0] = PROTO; |
| 3840 | assert(self->proto >= 0 && self->proto < 256); |
| 3841 | header[1] = (unsigned char)self->proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3842 | if (_Pickler_Write(self, header, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3843 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3844 | if (self->proto >= 4) |
| 3845 | self->framing = 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3846 | } |
| 3847 | |
| 3848 | if (save(self, obj, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3849 | _Pickler_Write(self, &stop_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3850 | return -1; |
| 3851 | |
| 3852 | return 0; |
| 3853 | } |
| 3854 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3855 | /*[clinic] |
| 3856 | |
| 3857 | _pickle.Pickler.clear_memo |
| 3858 | |
| 3859 | self: PicklerObject |
| 3860 | |
| 3861 | Clears the pickler's "memo". |
| 3862 | |
| 3863 | The memo is the data structure that remembers which objects the |
| 3864 | pickler has already seen, so that shared or recursive objects are |
| 3865 | pickled by reference and not by value. This method is useful when |
| 3866 | re-using picklers. |
| 3867 | [clinic]*/ |
| 3868 | |
| 3869 | PyDoc_STRVAR(_pickle_Pickler_clear_memo__doc__, |
| 3870 | "clear_memo()\n" |
| 3871 | "Clears the pickler\'s \"memo\".\n" |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3872 | "\n" |
| 3873 | "The memo is the data structure that remembers which objects the\n" |
| 3874 | "pickler has already seen, so that shared or recursive objects are\n" |
| 3875 | "pickled by reference and not by value. This method is useful when\n" |
| 3876 | "re-using picklers."); |
| 3877 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3878 | #define _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF \ |
| 3879 | {"clear_memo", (PyCFunction)_pickle_Pickler_clear_memo, METH_NOARGS, _pickle_Pickler_clear_memo__doc__}, |
| 3880 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3881 | static PyObject * |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3882 | _pickle_Pickler_clear_memo(PicklerObject *self) |
| 3883 | /*[clinic checksum: 9c32be7e7a17ff82a81aae409d0d4f469033a5b2]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3884 | { |
| 3885 | if (self->memo) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3886 | PyMemoTable_Clear(self->memo); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3887 | |
| 3888 | Py_RETURN_NONE; |
| 3889 | } |
| 3890 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3891 | /*[clinic] |
| 3892 | |
| 3893 | _pickle.Pickler.dump |
| 3894 | |
| 3895 | self: PicklerObject |
| 3896 | obj: object |
| 3897 | / |
| 3898 | |
| 3899 | Write a pickled representation of the given object to the open file. |
| 3900 | [clinic]*/ |
| 3901 | |
| 3902 | PyDoc_STRVAR(_pickle_Pickler_dump__doc__, |
| 3903 | "dump(obj)\n" |
| 3904 | "Write a pickled representation of the given object to the open file."); |
| 3905 | |
| 3906 | #define _PICKLE_PICKLER_DUMP_METHODDEF \ |
| 3907 | {"dump", (PyCFunction)_pickle_Pickler_dump, METH_O, _pickle_Pickler_dump__doc__}, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3908 | |
| 3909 | static PyObject * |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3910 | _pickle_Pickler_dump(PicklerObject *self, PyObject *obj) |
| 3911 | /*[clinic checksum: b72a69ec98737fabf66dae7c5a3210178bdbd3e6]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3912 | { |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 3913 | /* Check whether the Pickler was initialized correctly (issue3664). |
| 3914 | Developers often forget to call __init__() in their subclasses, which |
| 3915 | would trigger a segfault without this check. */ |
| 3916 | if (self->write == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3917 | PickleState *st = _Pickle_GetGlobalState(); |
| 3918 | PyErr_Format(st->PicklingError, |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 3919 | "Pickler.__init__() was not called by %s.__init__()", |
| 3920 | Py_TYPE(self)->tp_name); |
| 3921 | return NULL; |
| 3922 | } |
| 3923 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3924 | if (_Pickler_ClearBuffer(self) < 0) |
| 3925 | return NULL; |
| 3926 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3927 | if (dump(self, obj) < 0) |
| 3928 | return NULL; |
| 3929 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3930 | if (_Pickler_FlushToFile(self) < 0) |
| 3931 | return NULL; |
| 3932 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3933 | Py_RETURN_NONE; |
| 3934 | } |
| 3935 | |
| 3936 | static struct PyMethodDef Pickler_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3937 | _PICKLE_PICKLER_DUMP_METHODDEF |
| 3938 | _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3939 | {NULL, NULL} /* sentinel */ |
| 3940 | }; |
| 3941 | |
| 3942 | static void |
| 3943 | Pickler_dealloc(PicklerObject *self) |
| 3944 | { |
| 3945 | PyObject_GC_UnTrack(self); |
| 3946 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3947 | Py_XDECREF(self->output_buffer); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3948 | Py_XDECREF(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3949 | Py_XDECREF(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3950 | Py_XDECREF(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3951 | Py_XDECREF(self->fast_memo); |
| 3952 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3953 | PyMemoTable_Del(self->memo); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3954 | |
| 3955 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 3956 | } |
| 3957 | |
| 3958 | static int |
| 3959 | Pickler_traverse(PicklerObject *self, visitproc visit, void *arg) |
| 3960 | { |
| 3961 | Py_VISIT(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3962 | Py_VISIT(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3963 | Py_VISIT(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3964 | Py_VISIT(self->fast_memo); |
| 3965 | return 0; |
| 3966 | } |
| 3967 | |
| 3968 | static int |
| 3969 | Pickler_clear(PicklerObject *self) |
| 3970 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3971 | Py_CLEAR(self->output_buffer); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3972 | Py_CLEAR(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3973 | Py_CLEAR(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3974 | Py_CLEAR(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3975 | Py_CLEAR(self->fast_memo); |
| 3976 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3977 | if (self->memo != NULL) { |
| 3978 | PyMemoTable *memo = self->memo; |
| 3979 | self->memo = NULL; |
| 3980 | PyMemoTable_Del(memo); |
| 3981 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3982 | return 0; |
| 3983 | } |
| 3984 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3985 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3986 | /*[clinic] |
| 3987 | |
| 3988 | _pickle.Pickler.__init__ |
| 3989 | |
| 3990 | self: PicklerObject |
| 3991 | file: object |
| 3992 | protocol: object = NULL |
| 3993 | fix_imports: bool = True |
| 3994 | |
| 3995 | This takes a binary file for writing a pickle data stream. |
| 3996 | |
| 3997 | The optional protocol argument tells the pickler to use the |
| 3998 | given protocol; supported protocols are 0, 1, 2, 3 and 4. The |
| 3999 | default protocol is 3; a backward-incompatible protocol designed for |
| 4000 | Python 3. |
| 4001 | |
| 4002 | Specifying a negative protocol version selects the highest |
| 4003 | protocol version supported. The higher the protocol used, the |
| 4004 | more recent the version of Python needed to read the pickle |
| 4005 | produced. |
| 4006 | |
| 4007 | The file argument must have a write() method that accepts a single |
| 4008 | bytes argument. It can thus be a file object opened for binary |
| 4009 | writing, a io.BytesIO instance, or any other custom object that |
| 4010 | meets this interface. |
| 4011 | |
| 4012 | If fix_imports is True and protocol is less than 3, pickle will try to |
| 4013 | map the new Python 3 names to the old module names used in Python 2, |
| 4014 | so that the pickle data stream is readable with Python 2. |
| 4015 | [clinic]*/ |
| 4016 | |
| 4017 | PyDoc_STRVAR(_pickle_Pickler___init____doc__, |
| 4018 | "__init__(file, protocol=None, fix_imports=True)\n" |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4019 | "This takes a binary file for writing a pickle data stream.\n" |
| 4020 | "\n" |
| 4021 | "The optional protocol argument tells the pickler to use the\n" |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4022 | "given protocol; supported protocols are 0, 1, 2, 3 and 4. The\n" |
| 4023 | "default protocol is 3; a backward-incompatible protocol designed for\n" |
| 4024 | "Python 3.\n" |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4025 | "\n" |
| 4026 | "Specifying a negative protocol version selects the highest\n" |
| 4027 | "protocol version supported. The higher the protocol used, the\n" |
| 4028 | "more recent the version of Python needed to read the pickle\n" |
| 4029 | "produced.\n" |
| 4030 | "\n" |
| 4031 | "The file argument must have a write() method that accepts a single\n" |
| 4032 | "bytes argument. It can thus be a file object opened for binary\n" |
| 4033 | "writing, a io.BytesIO instance, or any other custom object that\n" |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 4034 | "meets this interface.\n" |
| 4035 | "\n" |
| 4036 | "If fix_imports is True and protocol is less than 3, pickle will try to\n" |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4037 | "map the new Python 3 names to the old module names used in Python 2,\n" |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4038 | "so that the pickle data stream is readable with Python 2."); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4039 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4040 | #define _PICKLE_PICKLER___INIT___METHODDEF \ |
| 4041 | {"__init__", (PyCFunction)_pickle_Pickler___init__, METH_VARARGS|METH_KEYWORDS, _pickle_Pickler___init____doc__}, |
| 4042 | |
| 4043 | static PyObject * |
| 4044 | _pickle_Pickler___init___impl(PicklerObject *self, PyObject *file, PyObject *protocol, int fix_imports); |
| 4045 | |
| 4046 | static PyObject * |
| 4047 | _pickle_Pickler___init__(PyObject *self, PyObject *args, PyObject *kwargs) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4048 | { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4049 | PyObject *return_value = NULL; |
| 4050 | static char *_keywords[] = {"file", "protocol", "fix_imports", NULL}; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4051 | PyObject *file; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4052 | PyObject *protocol = NULL; |
| 4053 | int fix_imports = 1; |
| 4054 | |
| 4055 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 4056 | "O|Op:__init__", _keywords, |
| 4057 | &file, &protocol, &fix_imports)) |
| 4058 | goto exit; |
| 4059 | return_value = _pickle_Pickler___init___impl((PicklerObject *)self, file, protocol, fix_imports); |
| 4060 | |
| 4061 | exit: |
| 4062 | return return_value; |
| 4063 | } |
| 4064 | |
| 4065 | static PyObject * |
| 4066 | _pickle_Pickler___init___impl(PicklerObject *self, PyObject *file, PyObject *protocol, int fix_imports) |
| 4067 | /*[clinic checksum: c99ff417bd703a74affc4b708167e56e135e8969]*/ |
| 4068 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4069 | _Py_IDENTIFIER(persistent_id); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4070 | _Py_IDENTIFIER(dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4071 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4072 | /* In case of multiple __init__() calls, clear previous content. */ |
| 4073 | if (self->write != NULL) |
| 4074 | (void)Pickler_clear(self); |
| 4075 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4076 | if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0) |
| 4077 | return NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4078 | |
| 4079 | if (_Pickler_SetOutputStream(self, file) < 0) |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4080 | return NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4081 | |
| 4082 | /* memo and output_buffer may have already been created in _Pickler_New */ |
| 4083 | if (self->memo == NULL) { |
| 4084 | self->memo = PyMemoTable_New(); |
| 4085 | if (self->memo == NULL) |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4086 | return NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4087 | } |
| 4088 | self->output_len = 0; |
| 4089 | if (self->output_buffer == NULL) { |
| 4090 | self->max_output_len = WRITE_BUF_SIZE; |
| 4091 | self->output_buffer = PyBytes_FromStringAndSize(NULL, |
| 4092 | self->max_output_len); |
| 4093 | if (self->output_buffer == NULL) |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4094 | return NULL; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 4095 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4096 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 4097 | self->fast = 0; |
| 4098 | self->fast_nesting = 0; |
| 4099 | self->fast_memo = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4100 | self->pers_func = NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 4101 | if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) { |
| 4102 | self->pers_func = _PyObject_GetAttrId((PyObject *)self, |
| 4103 | &PyId_persistent_id); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4104 | if (self->pers_func == NULL) |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4105 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4106 | } |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4107 | self->dispatch_table = NULL; |
| 4108 | if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) { |
| 4109 | self->dispatch_table = _PyObject_GetAttrId((PyObject *)self, |
| 4110 | &PyId_dispatch_table); |
| 4111 | if (self->dispatch_table == NULL) |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4112 | return NULL; |
| 4113 | } |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4114 | |
| 4115 | Py_RETURN_NONE; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4116 | } |
| 4117 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4118 | /* Wrap the Clinic generated signature to slot it in tp_init. */ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4119 | static int |
| 4120 | Pickler_init(PyObject *self, PyObject *args, PyObject *kwargs) |
| 4121 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4122 | PyObject *result = _pickle_Pickler___init__(self, args, kwargs); |
| 4123 | if (result == NULL) { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4124 | return -1; |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4125 | } |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4126 | Py_DECREF(result); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4127 | return 0; |
| 4128 | } |
| 4129 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4130 | /* Define a proxy object for the Pickler's internal memo object. This is to |
| 4131 | * avoid breaking code like: |
| 4132 | * pickler.memo.clear() |
| 4133 | * and |
| 4134 | * pickler.memo = saved_memo |
| 4135 | * Is this a good idea? Not really, but we don't want to break code that uses |
| 4136 | * it. Note that we don't implement the entire mapping API here. This is |
| 4137 | * intentional, as these should be treated as black-box implementation details. |
| 4138 | */ |
| 4139 | |
| 4140 | typedef struct { |
| 4141 | PyObject_HEAD |
| 4142 | PicklerObject *pickler; /* Pickler whose memo table we're proxying. */ |
| 4143 | } PicklerMemoProxyObject; |
| 4144 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4145 | /*[clinic] |
| 4146 | _pickle.PicklerMemoProxy.clear |
| 4147 | |
| 4148 | self: PicklerMemoProxyObject |
| 4149 | |
| 4150 | Remove all items from memo. |
| 4151 | [clinic]*/ |
| 4152 | |
| 4153 | PyDoc_STRVAR(_pickle_PicklerMemoProxy_clear__doc__, |
| 4154 | "clear()\n" |
| 4155 | "Remove all items from memo."); |
| 4156 | |
| 4157 | #define _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF \ |
| 4158 | {"clear", (PyCFunction)_pickle_PicklerMemoProxy_clear, METH_NOARGS, _pickle_PicklerMemoProxy_clear__doc__}, |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4159 | |
| 4160 | static PyObject * |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4161 | _pickle_PicklerMemoProxy_clear(PicklerMemoProxyObject *self) |
| 4162 | /*[clinic checksum: 507f13938721992e175a3e58b5ad02620045a1cc]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4163 | { |
| 4164 | if (self->pickler->memo) |
| 4165 | PyMemoTable_Clear(self->pickler->memo); |
| 4166 | Py_RETURN_NONE; |
| 4167 | } |
| 4168 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4169 | /*[clinic] |
| 4170 | _pickle.PicklerMemoProxy.copy |
| 4171 | |
| 4172 | self: PicklerMemoProxyObject |
| 4173 | |
| 4174 | Copy the memo to a new object. |
| 4175 | [clinic]*/ |
| 4176 | |
| 4177 | PyDoc_STRVAR(_pickle_PicklerMemoProxy_copy__doc__, |
| 4178 | "copy()\n" |
| 4179 | "Copy the memo to a new object."); |
| 4180 | |
| 4181 | #define _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF \ |
| 4182 | {"copy", (PyCFunction)_pickle_PicklerMemoProxy_copy, METH_NOARGS, _pickle_PicklerMemoProxy_copy__doc__}, |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4183 | |
| 4184 | static PyObject * |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4185 | _pickle_PicklerMemoProxy_copy(PicklerMemoProxyObject *self) |
| 4186 | /*[clinic checksum: 73a5117ab354290ebdbe07bd0bf7232d0936a69d]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4187 | { |
| 4188 | Py_ssize_t i; |
| 4189 | PyMemoTable *memo; |
| 4190 | PyObject *new_memo = PyDict_New(); |
| 4191 | if (new_memo == NULL) |
| 4192 | return NULL; |
| 4193 | |
| 4194 | memo = self->pickler->memo; |
| 4195 | for (i = 0; i < memo->mt_allocated; ++i) { |
| 4196 | PyMemoEntry entry = memo->mt_table[i]; |
| 4197 | if (entry.me_key != NULL) { |
| 4198 | int status; |
| 4199 | PyObject *key, *value; |
| 4200 | |
| 4201 | key = PyLong_FromVoidPtr(entry.me_key); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4202 | value = Py_BuildValue("nO", entry.me_value, entry.me_key); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4203 | |
| 4204 | if (key == NULL || value == NULL) { |
| 4205 | Py_XDECREF(key); |
| 4206 | Py_XDECREF(value); |
| 4207 | goto error; |
| 4208 | } |
| 4209 | status = PyDict_SetItem(new_memo, key, value); |
| 4210 | Py_DECREF(key); |
| 4211 | Py_DECREF(value); |
| 4212 | if (status < 0) |
| 4213 | goto error; |
| 4214 | } |
| 4215 | } |
| 4216 | return new_memo; |
| 4217 | |
| 4218 | error: |
| 4219 | Py_XDECREF(new_memo); |
| 4220 | return NULL; |
| 4221 | } |
| 4222 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4223 | /*[clinic] |
| 4224 | _pickle.PicklerMemoProxy.__reduce__ |
| 4225 | |
| 4226 | self: PicklerMemoProxyObject |
| 4227 | |
| 4228 | Implement pickle support. |
| 4229 | [clinic]*/ |
| 4230 | |
| 4231 | PyDoc_STRVAR(_pickle_PicklerMemoProxy___reduce____doc__, |
| 4232 | "__reduce__()\n" |
| 4233 | "Implement pickle support."); |
| 4234 | |
| 4235 | #define _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF \ |
| 4236 | {"__reduce__", (PyCFunction)_pickle_PicklerMemoProxy___reduce__, METH_NOARGS, _pickle_PicklerMemoProxy___reduce____doc__}, |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4237 | |
| 4238 | static PyObject * |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4239 | _pickle_PicklerMemoProxy___reduce__(PicklerMemoProxyObject *self) |
| 4240 | /*[clinic checksum: 40f0bf7a9b161e77130674f0481bda0a0184dcce]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4241 | { |
| 4242 | PyObject *reduce_value, *dict_args; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4243 | PyObject *contents = _pickle_PicklerMemoProxy_copy(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4244 | if (contents == NULL) |
| 4245 | return NULL; |
| 4246 | |
| 4247 | reduce_value = PyTuple_New(2); |
| 4248 | if (reduce_value == NULL) { |
| 4249 | Py_DECREF(contents); |
| 4250 | return NULL; |
| 4251 | } |
| 4252 | dict_args = PyTuple_New(1); |
| 4253 | if (dict_args == NULL) { |
| 4254 | Py_DECREF(contents); |
| 4255 | Py_DECREF(reduce_value); |
| 4256 | return NULL; |
| 4257 | } |
| 4258 | PyTuple_SET_ITEM(dict_args, 0, contents); |
| 4259 | Py_INCREF((PyObject *)&PyDict_Type); |
| 4260 | PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type); |
| 4261 | PyTuple_SET_ITEM(reduce_value, 1, dict_args); |
| 4262 | return reduce_value; |
| 4263 | } |
| 4264 | |
| 4265 | static PyMethodDef picklerproxy_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4266 | _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF |
| 4267 | _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF |
| 4268 | _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4269 | {NULL, NULL} /* sentinel */ |
| 4270 | }; |
| 4271 | |
| 4272 | static void |
| 4273 | PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self) |
| 4274 | { |
| 4275 | PyObject_GC_UnTrack(self); |
| 4276 | Py_XDECREF(self->pickler); |
| 4277 | PyObject_GC_Del((PyObject *)self); |
| 4278 | } |
| 4279 | |
| 4280 | static int |
| 4281 | PicklerMemoProxy_traverse(PicklerMemoProxyObject *self, |
| 4282 | visitproc visit, void *arg) |
| 4283 | { |
| 4284 | Py_VISIT(self->pickler); |
| 4285 | return 0; |
| 4286 | } |
| 4287 | |
| 4288 | static int |
| 4289 | PicklerMemoProxy_clear(PicklerMemoProxyObject *self) |
| 4290 | { |
| 4291 | Py_CLEAR(self->pickler); |
| 4292 | return 0; |
| 4293 | } |
| 4294 | |
| 4295 | static PyTypeObject PicklerMemoProxyType = { |
| 4296 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4297 | "_pickle.PicklerMemoProxy", /*tp_name*/ |
| 4298 | sizeof(PicklerMemoProxyObject), /*tp_basicsize*/ |
| 4299 | 0, |
| 4300 | (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */ |
| 4301 | 0, /* tp_print */ |
| 4302 | 0, /* tp_getattr */ |
| 4303 | 0, /* tp_setattr */ |
| 4304 | 0, /* tp_compare */ |
| 4305 | 0, /* tp_repr */ |
| 4306 | 0, /* tp_as_number */ |
| 4307 | 0, /* tp_as_sequence */ |
| 4308 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 4309 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4310 | 0, /* tp_call */ |
| 4311 | 0, /* tp_str */ |
| 4312 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4313 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 4314 | 0, /* tp_as_buffer */ |
| 4315 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 4316 | 0, /* tp_doc */ |
| 4317 | (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */ |
| 4318 | (inquiry)PicklerMemoProxy_clear, /* tp_clear */ |
| 4319 | 0, /* tp_richcompare */ |
| 4320 | 0, /* tp_weaklistoffset */ |
| 4321 | 0, /* tp_iter */ |
| 4322 | 0, /* tp_iternext */ |
| 4323 | picklerproxy_methods, /* tp_methods */ |
| 4324 | }; |
| 4325 | |
| 4326 | static PyObject * |
| 4327 | PicklerMemoProxy_New(PicklerObject *pickler) |
| 4328 | { |
| 4329 | PicklerMemoProxyObject *self; |
| 4330 | |
| 4331 | self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType); |
| 4332 | if (self == NULL) |
| 4333 | return NULL; |
| 4334 | Py_INCREF(pickler); |
| 4335 | self->pickler = pickler; |
| 4336 | PyObject_GC_Track(self); |
| 4337 | return (PyObject *)self; |
| 4338 | } |
| 4339 | |
| 4340 | /*****************************************************************************/ |
| 4341 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4342 | static PyObject * |
| 4343 | Pickler_get_memo(PicklerObject *self) |
| 4344 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4345 | return PicklerMemoProxy_New(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4346 | } |
| 4347 | |
| 4348 | static int |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4349 | Pickler_set_memo(PicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4350 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4351 | PyMemoTable *new_memo = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4352 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4353 | if (obj == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4354 | PyErr_SetString(PyExc_TypeError, |
| 4355 | "attribute deletion is not supported"); |
| 4356 | return -1; |
| 4357 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4358 | |
| 4359 | if (Py_TYPE(obj) == &PicklerMemoProxyType) { |
| 4360 | PicklerObject *pickler = |
| 4361 | ((PicklerMemoProxyObject *)obj)->pickler; |
| 4362 | |
| 4363 | new_memo = PyMemoTable_Copy(pickler->memo); |
| 4364 | if (new_memo == NULL) |
| 4365 | return -1; |
| 4366 | } |
| 4367 | else if (PyDict_Check(obj)) { |
| 4368 | Py_ssize_t i = 0; |
| 4369 | PyObject *key, *value; |
| 4370 | |
| 4371 | new_memo = PyMemoTable_New(); |
| 4372 | if (new_memo == NULL) |
| 4373 | return -1; |
| 4374 | |
| 4375 | while (PyDict_Next(obj, &i, &key, &value)) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4376 | Py_ssize_t memo_id; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4377 | PyObject *memo_obj; |
| 4378 | |
| 4379 | if (!PyTuple_Check(value) || Py_SIZE(value) != 2) { |
| 4380 | PyErr_SetString(PyExc_TypeError, |
| 4381 | "'memo' values must be 2-item tuples"); |
| 4382 | goto error; |
| 4383 | } |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4384 | memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0)); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4385 | if (memo_id == -1 && PyErr_Occurred()) |
| 4386 | goto error; |
| 4387 | memo_obj = PyTuple_GET_ITEM(value, 1); |
| 4388 | if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0) |
| 4389 | goto error; |
| 4390 | } |
| 4391 | } |
| 4392 | else { |
| 4393 | PyErr_Format(PyExc_TypeError, |
| 4394 | "'memo' attribute must be an PicklerMemoProxy object" |
| 4395 | "or dict, not %.200s", Py_TYPE(obj)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4396 | return -1; |
| 4397 | } |
| 4398 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4399 | PyMemoTable_Del(self->memo); |
| 4400 | self->memo = new_memo; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4401 | |
| 4402 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4403 | |
| 4404 | error: |
| 4405 | if (new_memo) |
| 4406 | PyMemoTable_Del(new_memo); |
| 4407 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4408 | } |
| 4409 | |
| 4410 | static PyObject * |
| 4411 | Pickler_get_persid(PicklerObject *self) |
| 4412 | { |
| 4413 | if (self->pers_func == NULL) |
| 4414 | PyErr_SetString(PyExc_AttributeError, "persistent_id"); |
| 4415 | else |
| 4416 | Py_INCREF(self->pers_func); |
| 4417 | return self->pers_func; |
| 4418 | } |
| 4419 | |
| 4420 | static int |
| 4421 | Pickler_set_persid(PicklerObject *self, PyObject *value) |
| 4422 | { |
| 4423 | PyObject *tmp; |
| 4424 | |
| 4425 | if (value == NULL) { |
| 4426 | PyErr_SetString(PyExc_TypeError, |
| 4427 | "attribute deletion is not supported"); |
| 4428 | return -1; |
| 4429 | } |
| 4430 | if (!PyCallable_Check(value)) { |
| 4431 | PyErr_SetString(PyExc_TypeError, |
| 4432 | "persistent_id must be a callable taking one argument"); |
| 4433 | return -1; |
| 4434 | } |
| 4435 | |
| 4436 | tmp = self->pers_func; |
| 4437 | Py_INCREF(value); |
| 4438 | self->pers_func = value; |
| 4439 | Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */ |
| 4440 | |
| 4441 | return 0; |
| 4442 | } |
| 4443 | |
| 4444 | static PyMemberDef Pickler_members[] = { |
| 4445 | {"bin", T_INT, offsetof(PicklerObject, bin)}, |
| 4446 | {"fast", T_INT, offsetof(PicklerObject, fast)}, |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4447 | {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)}, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4448 | {NULL} |
| 4449 | }; |
| 4450 | |
| 4451 | static PyGetSetDef Pickler_getsets[] = { |
| 4452 | {"memo", (getter)Pickler_get_memo, |
| 4453 | (setter)Pickler_set_memo}, |
| 4454 | {"persistent_id", (getter)Pickler_get_persid, |
| 4455 | (setter)Pickler_set_persid}, |
| 4456 | {NULL} |
| 4457 | }; |
| 4458 | |
| 4459 | static PyTypeObject Pickler_Type = { |
| 4460 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4461 | "_pickle.Pickler" , /*tp_name*/ |
| 4462 | sizeof(PicklerObject), /*tp_basicsize*/ |
| 4463 | 0, /*tp_itemsize*/ |
| 4464 | (destructor)Pickler_dealloc, /*tp_dealloc*/ |
| 4465 | 0, /*tp_print*/ |
| 4466 | 0, /*tp_getattr*/ |
| 4467 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 4468 | 0, /*tp_reserved*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4469 | 0, /*tp_repr*/ |
| 4470 | 0, /*tp_as_number*/ |
| 4471 | 0, /*tp_as_sequence*/ |
| 4472 | 0, /*tp_as_mapping*/ |
| 4473 | 0, /*tp_hash*/ |
| 4474 | 0, /*tp_call*/ |
| 4475 | 0, /*tp_str*/ |
| 4476 | 0, /*tp_getattro*/ |
| 4477 | 0, /*tp_setattro*/ |
| 4478 | 0, /*tp_as_buffer*/ |
| 4479 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4480 | _pickle_Pickler___init____doc__, /*tp_doc*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4481 | (traverseproc)Pickler_traverse, /*tp_traverse*/ |
| 4482 | (inquiry)Pickler_clear, /*tp_clear*/ |
| 4483 | 0, /*tp_richcompare*/ |
| 4484 | 0, /*tp_weaklistoffset*/ |
| 4485 | 0, /*tp_iter*/ |
| 4486 | 0, /*tp_iternext*/ |
| 4487 | Pickler_methods, /*tp_methods*/ |
| 4488 | Pickler_members, /*tp_members*/ |
| 4489 | Pickler_getsets, /*tp_getset*/ |
| 4490 | 0, /*tp_base*/ |
| 4491 | 0, /*tp_dict*/ |
| 4492 | 0, /*tp_descr_get*/ |
| 4493 | 0, /*tp_descr_set*/ |
| 4494 | 0, /*tp_dictoffset*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4495 | Pickler_init, /*tp_init*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4496 | PyType_GenericAlloc, /*tp_alloc*/ |
| 4497 | PyType_GenericNew, /*tp_new*/ |
| 4498 | PyObject_GC_Del, /*tp_free*/ |
| 4499 | 0, /*tp_is_gc*/ |
| 4500 | }; |
| 4501 | |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 4502 | /* Temporary helper for calling self.find_class(). |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4503 | |
| 4504 | XXX: It would be nice to able to avoid Python function call overhead, by |
| 4505 | using directly the C version of find_class(), when find_class() is not |
| 4506 | overridden by a subclass. Although, this could become rather hackish. A |
| 4507 | simpler optimization would be to call the C function when self is not a |
| 4508 | subclass instance. */ |
| 4509 | static PyObject * |
| 4510 | find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name) |
| 4511 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4512 | _Py_IDENTIFIER(find_class); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 4513 | |
| 4514 | return _PyObject_CallMethodId((PyObject *)self, &PyId_find_class, "OO", |
| 4515 | module_name, global_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4516 | } |
| 4517 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4518 | static Py_ssize_t |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4519 | marker(UnpicklerObject *self) |
| 4520 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4521 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4522 | if (self->num_marks < 1) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4523 | PyErr_SetString(st->UnpicklingError, "could not find MARK"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4524 | return -1; |
| 4525 | } |
| 4526 | |
| 4527 | return self->marks[--self->num_marks]; |
| 4528 | } |
| 4529 | |
| 4530 | static int |
| 4531 | load_none(UnpicklerObject *self) |
| 4532 | { |
| 4533 | PDATA_APPEND(self->stack, Py_None, -1); |
| 4534 | return 0; |
| 4535 | } |
| 4536 | |
| 4537 | static int |
| 4538 | bad_readline(void) |
| 4539 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4540 | PickleState *st = _Pickle_GetGlobalState(); |
| 4541 | PyErr_SetString(st->UnpicklingError, "pickle data was truncated"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4542 | return -1; |
| 4543 | } |
| 4544 | |
| 4545 | static int |
| 4546 | load_int(UnpicklerObject *self) |
| 4547 | { |
| 4548 | PyObject *value; |
| 4549 | char *endptr, *s; |
| 4550 | Py_ssize_t len; |
| 4551 | long x; |
| 4552 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4553 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4554 | return -1; |
| 4555 | if (len < 2) |
| 4556 | return bad_readline(); |
| 4557 | |
| 4558 | errno = 0; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 4559 | /* XXX: Should the base argument of strtol() be explicitly set to 10? |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4560 | XXX(avassalotti): Should this uses PyOS_strtol()? */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4561 | x = strtol(s, &endptr, 0); |
| 4562 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4563 | if (errno || (*endptr != '\n' && *endptr != '\0')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4564 | /* Hm, maybe we've got something long. Let's try reading |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4565 | * it as a Python int object. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4566 | errno = 0; |
| 4567 | /* XXX: Same thing about the base here. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4568 | value = PyLong_FromString(s, NULL, 0); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4569 | if (value == NULL) { |
| 4570 | PyErr_SetString(PyExc_ValueError, |
| 4571 | "could not convert string to int"); |
| 4572 | return -1; |
| 4573 | } |
| 4574 | } |
| 4575 | else { |
| 4576 | if (len == 3 && (x == 0 || x == 1)) { |
| 4577 | if ((value = PyBool_FromLong(x)) == NULL) |
| 4578 | return -1; |
| 4579 | } |
| 4580 | else { |
| 4581 | if ((value = PyLong_FromLong(x)) == NULL) |
| 4582 | return -1; |
| 4583 | } |
| 4584 | } |
| 4585 | |
| 4586 | PDATA_PUSH(self->stack, value, -1); |
| 4587 | return 0; |
| 4588 | } |
| 4589 | |
| 4590 | static int |
| 4591 | load_bool(UnpicklerObject *self, PyObject *boolean) |
| 4592 | { |
| 4593 | assert(boolean == Py_True || boolean == Py_False); |
| 4594 | PDATA_APPEND(self->stack, boolean, -1); |
| 4595 | return 0; |
| 4596 | } |
| 4597 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4598 | /* s contains x bytes of an unsigned little-endian integer. Return its value |
| 4599 | * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX. |
| 4600 | */ |
| 4601 | static Py_ssize_t |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4602 | calc_binsize(char *bytes, int nbytes) |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4603 | { |
| 4604 | unsigned char *s = (unsigned char *)bytes; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4605 | int i; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4606 | size_t x = 0; |
| 4607 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 4608 | for (i = 0; i < nbytes && i < sizeof(size_t); i++) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4609 | x |= (size_t) s[i] << (8 * i); |
| 4610 | } |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4611 | |
| 4612 | if (x > PY_SSIZE_T_MAX) |
| 4613 | return -1; |
| 4614 | else |
| 4615 | return (Py_ssize_t) x; |
| 4616 | } |
| 4617 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4618 | /* s contains x bytes of a little-endian integer. Return its value as a |
| 4619 | * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian |
| 4620 | * int, but when x is 4 it's a signed one. This is an historical source |
| 4621 | * of x-platform bugs. |
| 4622 | */ |
| 4623 | static long |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4624 | calc_binint(char *bytes, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4625 | { |
| 4626 | unsigned char *s = (unsigned char *)bytes; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4627 | int i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4628 | long x = 0; |
| 4629 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4630 | for (i = 0; i < nbytes; i++) { |
| 4631 | x |= (long)s[i] << (8 * i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4632 | } |
| 4633 | |
| 4634 | /* Unlike BININT1 and BININT2, BININT (more accurately BININT4) |
| 4635 | * is signed, so on a box with longs bigger than 4 bytes we need |
| 4636 | * to extend a BININT's sign bit to the full width. |
| 4637 | */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4638 | if (SIZEOF_LONG > 4 && nbytes == 4) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4639 | x |= -(x & (1L << 31)); |
| 4640 | } |
| 4641 | |
| 4642 | return x; |
| 4643 | } |
| 4644 | |
| 4645 | static int |
| 4646 | load_binintx(UnpicklerObject *self, char *s, int size) |
| 4647 | { |
| 4648 | PyObject *value; |
| 4649 | long x; |
| 4650 | |
| 4651 | x = calc_binint(s, size); |
| 4652 | |
| 4653 | if ((value = PyLong_FromLong(x)) == NULL) |
| 4654 | return -1; |
| 4655 | |
| 4656 | PDATA_PUSH(self->stack, value, -1); |
| 4657 | return 0; |
| 4658 | } |
| 4659 | |
| 4660 | static int |
| 4661 | load_binint(UnpicklerObject *self) |
| 4662 | { |
| 4663 | char *s; |
| 4664 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4665 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4666 | return -1; |
| 4667 | |
| 4668 | return load_binintx(self, s, 4); |
| 4669 | } |
| 4670 | |
| 4671 | static int |
| 4672 | load_binint1(UnpicklerObject *self) |
| 4673 | { |
| 4674 | char *s; |
| 4675 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4676 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4677 | return -1; |
| 4678 | |
| 4679 | return load_binintx(self, s, 1); |
| 4680 | } |
| 4681 | |
| 4682 | static int |
| 4683 | load_binint2(UnpicklerObject *self) |
| 4684 | { |
| 4685 | char *s; |
| 4686 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4687 | if (_Unpickler_Read(self, &s, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4688 | return -1; |
| 4689 | |
| 4690 | return load_binintx(self, s, 2); |
| 4691 | } |
| 4692 | |
| 4693 | static int |
| 4694 | load_long(UnpicklerObject *self) |
| 4695 | { |
| 4696 | PyObject *value; |
Alexandre Vassalotti | 446f7ff | 2009-01-23 04:43:46 +0000 | [diff] [blame] | 4697 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4698 | Py_ssize_t len; |
| 4699 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4700 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4701 | return -1; |
| 4702 | if (len < 2) |
| 4703 | return bad_readline(); |
| 4704 | |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 4705 | /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove |
| 4706 | the 'L' before calling PyLong_FromString. In order to maintain |
| 4707 | compatibility with Python 3.0.0, we don't actually *require* |
| 4708 | the 'L' to be present. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4709 | if (s[len-2] == 'L') |
Alexandre Vassalotti | 446f7ff | 2009-01-23 04:43:46 +0000 | [diff] [blame] | 4710 | s[len-2] = '\0'; |
Alexandre Vassalotti | e4bccb7 | 2009-01-24 01:47:57 +0000 | [diff] [blame] | 4711 | /* XXX: Should the base argument explicitly set to 10? */ |
| 4712 | value = PyLong_FromString(s, NULL, 0); |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 4713 | if (value == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4714 | return -1; |
| 4715 | |
| 4716 | PDATA_PUSH(self->stack, value, -1); |
| 4717 | return 0; |
| 4718 | } |
| 4719 | |
| 4720 | /* 'size' bytes contain the # of bytes of little-endian 256's-complement |
| 4721 | * data following. |
| 4722 | */ |
| 4723 | static int |
| 4724 | load_counted_long(UnpicklerObject *self, int size) |
| 4725 | { |
| 4726 | PyObject *value; |
| 4727 | char *nbytes; |
| 4728 | char *pdata; |
| 4729 | |
| 4730 | assert(size == 1 || size == 4); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4731 | if (_Unpickler_Read(self, &nbytes, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4732 | return -1; |
| 4733 | |
| 4734 | size = calc_binint(nbytes, size); |
| 4735 | if (size < 0) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4736 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4737 | /* Corrupt or hostile pickle -- we never write one like this */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4738 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4739 | "LONG pickle has negative byte count"); |
| 4740 | return -1; |
| 4741 | } |
| 4742 | |
| 4743 | if (size == 0) |
| 4744 | value = PyLong_FromLong(0L); |
| 4745 | else { |
| 4746 | /* Read the raw little-endian bytes and convert. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4747 | if (_Unpickler_Read(self, &pdata, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4748 | return -1; |
| 4749 | value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size, |
| 4750 | 1 /* little endian */ , 1 /* signed */ ); |
| 4751 | } |
| 4752 | if (value == NULL) |
| 4753 | return -1; |
| 4754 | PDATA_PUSH(self->stack, value, -1); |
| 4755 | return 0; |
| 4756 | } |
| 4757 | |
| 4758 | static int |
| 4759 | load_float(UnpicklerObject *self) |
| 4760 | { |
| 4761 | PyObject *value; |
| 4762 | char *endptr, *s; |
| 4763 | Py_ssize_t len; |
| 4764 | double d; |
| 4765 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4766 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4767 | return -1; |
| 4768 | if (len < 2) |
| 4769 | return bad_readline(); |
| 4770 | |
| 4771 | errno = 0; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4772 | d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError); |
| 4773 | if (d == -1.0 && PyErr_Occurred()) |
| 4774 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4775 | if ((endptr[0] != '\n') && (endptr[0] != '\0')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4776 | PyErr_SetString(PyExc_ValueError, "could not convert string to float"); |
| 4777 | return -1; |
| 4778 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4779 | value = PyFloat_FromDouble(d); |
| 4780 | if (value == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4781 | return -1; |
| 4782 | |
| 4783 | PDATA_PUSH(self->stack, value, -1); |
| 4784 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4785 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4786 | |
| 4787 | static int |
| 4788 | load_binfloat(UnpicklerObject *self) |
| 4789 | { |
| 4790 | PyObject *value; |
| 4791 | double x; |
| 4792 | char *s; |
| 4793 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4794 | if (_Unpickler_Read(self, &s, 8) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4795 | return -1; |
| 4796 | |
| 4797 | x = _PyFloat_Unpack8((unsigned char *)s, 0); |
| 4798 | if (x == -1.0 && PyErr_Occurred()) |
| 4799 | return -1; |
| 4800 | |
| 4801 | if ((value = PyFloat_FromDouble(x)) == NULL) |
| 4802 | return -1; |
| 4803 | |
| 4804 | PDATA_PUSH(self->stack, value, -1); |
| 4805 | return 0; |
| 4806 | } |
| 4807 | |
| 4808 | static int |
| 4809 | load_string(UnpicklerObject *self) |
| 4810 | { |
| 4811 | PyObject *bytes; |
| 4812 | PyObject *str = NULL; |
| 4813 | Py_ssize_t len; |
| 4814 | char *s, *p; |
| 4815 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4816 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4817 | return -1; |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4818 | /* Strip the newline */ |
| 4819 | len--; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4820 | /* Strip outermost quotes */ |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4821 | if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4822 | p = s + 1; |
| 4823 | len -= 2; |
| 4824 | } |
| 4825 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4826 | PickleState *st = _Pickle_GetGlobalState(); |
| 4827 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4828 | "the STRING opcode argument must be quoted"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4829 | return -1; |
| 4830 | } |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4831 | assert(len >= 0); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4832 | |
| 4833 | /* Use the PyBytes API to decode the string, since that is what is used |
| 4834 | to encode, and then coerce the result to Unicode. */ |
| 4835 | bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4836 | if (bytes == NULL) |
| 4837 | return -1; |
| 4838 | str = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors); |
| 4839 | Py_DECREF(bytes); |
| 4840 | if (str == NULL) |
| 4841 | return -1; |
| 4842 | |
| 4843 | PDATA_PUSH(self->stack, str, -1); |
| 4844 | return 0; |
| 4845 | } |
| 4846 | |
| 4847 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4848 | load_counted_binbytes(UnpicklerObject *self, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4849 | { |
| 4850 | PyObject *bytes; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4851 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4852 | char *s; |
| 4853 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4854 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4855 | return -1; |
| 4856 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4857 | size = calc_binsize(s, nbytes); |
| 4858 | if (size < 0) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4859 | PyErr_Format(PyExc_OverflowError, |
| 4860 | "BINBYTES exceeds system's maximum size of %zd bytes", |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 4861 | PY_SSIZE_T_MAX); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4862 | return -1; |
| 4863 | } |
| 4864 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4865 | if (_Unpickler_Read(self, &s, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4866 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4867 | |
| 4868 | bytes = PyBytes_FromStringAndSize(s, size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4869 | if (bytes == NULL) |
| 4870 | return -1; |
| 4871 | |
| 4872 | PDATA_PUSH(self->stack, bytes, -1); |
| 4873 | return 0; |
| 4874 | } |
| 4875 | |
| 4876 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4877 | load_counted_binstring(UnpicklerObject *self, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4878 | { |
| 4879 | PyObject *str; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4880 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4881 | char *s; |
| 4882 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4883 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4884 | return -1; |
| 4885 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4886 | size = calc_binsize(s, nbytes); |
| 4887 | if (size < 0) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4888 | PickleState *st = _Pickle_GetGlobalState(); |
| 4889 | PyErr_Format(st->UnpicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4890 | "BINSTRING exceeds system's maximum size of %zd bytes", |
| 4891 | PY_SSIZE_T_MAX); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4892 | return -1; |
| 4893 | } |
| 4894 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4895 | if (_Unpickler_Read(self, &s, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4896 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4897 | /* Convert Python 2.x strings to unicode. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4898 | str = PyUnicode_Decode(s, size, self->encoding, self->errors); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4899 | if (str == NULL) |
| 4900 | return -1; |
| 4901 | |
| 4902 | PDATA_PUSH(self->stack, str, -1); |
| 4903 | return 0; |
| 4904 | } |
| 4905 | |
| 4906 | static int |
| 4907 | load_unicode(UnpicklerObject *self) |
| 4908 | { |
| 4909 | PyObject *str; |
| 4910 | Py_ssize_t len; |
| 4911 | char *s; |
| 4912 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4913 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4914 | return -1; |
| 4915 | if (len < 1) |
| 4916 | return bad_readline(); |
| 4917 | |
| 4918 | str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL); |
| 4919 | if (str == NULL) |
| 4920 | return -1; |
| 4921 | |
| 4922 | PDATA_PUSH(self->stack, str, -1); |
| 4923 | return 0; |
| 4924 | } |
| 4925 | |
| 4926 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4927 | load_counted_binunicode(UnpicklerObject *self, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4928 | { |
| 4929 | PyObject *str; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4930 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4931 | char *s; |
| 4932 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4933 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4934 | return -1; |
| 4935 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4936 | size = calc_binsize(s, nbytes); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4937 | if (size < 0) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4938 | PyErr_Format(PyExc_OverflowError, |
| 4939 | "BINUNICODE exceeds system's maximum size of %zd bytes", |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 4940 | PY_SSIZE_T_MAX); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4941 | return -1; |
| 4942 | } |
| 4943 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4944 | if (_Unpickler_Read(self, &s, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4945 | return -1; |
| 4946 | |
Victor Stinner | 485fb56 | 2010-04-13 11:07:24 +0000 | [diff] [blame] | 4947 | str = PyUnicode_DecodeUTF8(s, size, "surrogatepass"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4948 | if (str == NULL) |
| 4949 | return -1; |
| 4950 | |
| 4951 | PDATA_PUSH(self->stack, str, -1); |
| 4952 | return 0; |
| 4953 | } |
| 4954 | |
| 4955 | static int |
| 4956 | load_tuple(UnpicklerObject *self) |
| 4957 | { |
| 4958 | PyObject *tuple; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4959 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4960 | |
| 4961 | if ((i = marker(self)) < 0) |
| 4962 | return -1; |
| 4963 | |
| 4964 | tuple = Pdata_poptuple(self->stack, i); |
| 4965 | if (tuple == NULL) |
| 4966 | return -1; |
| 4967 | PDATA_PUSH(self->stack, tuple, -1); |
| 4968 | return 0; |
| 4969 | } |
| 4970 | |
| 4971 | static int |
| 4972 | load_counted_tuple(UnpicklerObject *self, int len) |
| 4973 | { |
| 4974 | PyObject *tuple; |
| 4975 | |
| 4976 | tuple = PyTuple_New(len); |
| 4977 | if (tuple == NULL) |
| 4978 | return -1; |
| 4979 | |
| 4980 | while (--len >= 0) { |
| 4981 | PyObject *item; |
| 4982 | |
| 4983 | PDATA_POP(self->stack, item); |
| 4984 | if (item == NULL) |
| 4985 | return -1; |
| 4986 | PyTuple_SET_ITEM(tuple, len, item); |
| 4987 | } |
| 4988 | PDATA_PUSH(self->stack, tuple, -1); |
| 4989 | return 0; |
| 4990 | } |
| 4991 | |
| 4992 | static int |
| 4993 | load_empty_list(UnpicklerObject *self) |
| 4994 | { |
| 4995 | PyObject *list; |
| 4996 | |
| 4997 | if ((list = PyList_New(0)) == NULL) |
| 4998 | return -1; |
| 4999 | PDATA_PUSH(self->stack, list, -1); |
| 5000 | return 0; |
| 5001 | } |
| 5002 | |
| 5003 | static int |
| 5004 | load_empty_dict(UnpicklerObject *self) |
| 5005 | { |
| 5006 | PyObject *dict; |
| 5007 | |
| 5008 | if ((dict = PyDict_New()) == NULL) |
| 5009 | return -1; |
| 5010 | PDATA_PUSH(self->stack, dict, -1); |
| 5011 | return 0; |
| 5012 | } |
| 5013 | |
| 5014 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5015 | load_empty_set(UnpicklerObject *self) |
| 5016 | { |
| 5017 | PyObject *set; |
| 5018 | |
| 5019 | if ((set = PySet_New(NULL)) == NULL) |
| 5020 | return -1; |
| 5021 | PDATA_PUSH(self->stack, set, -1); |
| 5022 | return 0; |
| 5023 | } |
| 5024 | |
| 5025 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5026 | load_list(UnpicklerObject *self) |
| 5027 | { |
| 5028 | PyObject *list; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5029 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5030 | |
| 5031 | if ((i = marker(self)) < 0) |
| 5032 | return -1; |
| 5033 | |
| 5034 | list = Pdata_poplist(self->stack, i); |
| 5035 | if (list == NULL) |
| 5036 | return -1; |
| 5037 | PDATA_PUSH(self->stack, list, -1); |
| 5038 | return 0; |
| 5039 | } |
| 5040 | |
| 5041 | static int |
| 5042 | load_dict(UnpicklerObject *self) |
| 5043 | { |
| 5044 | PyObject *dict, *key, *value; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5045 | Py_ssize_t i, j, k; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5046 | |
| 5047 | if ((i = marker(self)) < 0) |
| 5048 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5049 | j = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5050 | |
| 5051 | if ((dict = PyDict_New()) == NULL) |
| 5052 | return -1; |
| 5053 | |
| 5054 | for (k = i + 1; k < j; k += 2) { |
| 5055 | key = self->stack->data[k - 1]; |
| 5056 | value = self->stack->data[k]; |
| 5057 | if (PyDict_SetItem(dict, key, value) < 0) { |
| 5058 | Py_DECREF(dict); |
| 5059 | return -1; |
| 5060 | } |
| 5061 | } |
| 5062 | Pdata_clear(self->stack, i); |
| 5063 | PDATA_PUSH(self->stack, dict, -1); |
| 5064 | return 0; |
| 5065 | } |
| 5066 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5067 | static int |
| 5068 | load_frozenset(UnpicklerObject *self) |
| 5069 | { |
| 5070 | PyObject *items; |
| 5071 | PyObject *frozenset; |
| 5072 | Py_ssize_t i; |
| 5073 | |
| 5074 | if ((i = marker(self)) < 0) |
| 5075 | return -1; |
| 5076 | |
| 5077 | items = Pdata_poptuple(self->stack, i); |
| 5078 | if (items == NULL) |
| 5079 | return -1; |
| 5080 | |
| 5081 | frozenset = PyFrozenSet_New(items); |
| 5082 | Py_DECREF(items); |
| 5083 | if (frozenset == NULL) |
| 5084 | return -1; |
| 5085 | |
| 5086 | PDATA_PUSH(self->stack, frozenset, -1); |
| 5087 | return 0; |
| 5088 | } |
| 5089 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5090 | static PyObject * |
| 5091 | instantiate(PyObject *cls, PyObject *args) |
| 5092 | { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5093 | PyObject *result = NULL; |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5094 | _Py_IDENTIFIER(__getinitargs__); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5095 | /* Caller must assure args are a tuple. Normally, args come from |
| 5096 | Pdata_poptuple which packs objects from the top of the stack |
| 5097 | into a newly created tuple. */ |
| 5098 | assert(PyTuple_Check(args)); |
| 5099 | if (Py_SIZE(args) > 0 || !PyType_Check(cls) || |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5100 | _PyObject_HasAttrId(cls, &PyId___getinitargs__)) { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5101 | result = PyObject_CallObject(cls, args); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5102 | } |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5103 | else { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5104 | _Py_IDENTIFIER(__new__); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 5105 | |
| 5106 | result = _PyObject_CallMethodId(cls, &PyId___new__, "O", cls); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5107 | } |
| 5108 | return result; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5109 | } |
| 5110 | |
| 5111 | static int |
| 5112 | load_obj(UnpicklerObject *self) |
| 5113 | { |
| 5114 | PyObject *cls, *args, *obj = NULL; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5115 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5116 | |
| 5117 | if ((i = marker(self)) < 0) |
| 5118 | return -1; |
| 5119 | |
| 5120 | args = Pdata_poptuple(self->stack, i + 1); |
| 5121 | if (args == NULL) |
| 5122 | return -1; |
| 5123 | |
| 5124 | PDATA_POP(self->stack, cls); |
| 5125 | if (cls) { |
| 5126 | obj = instantiate(cls, args); |
| 5127 | Py_DECREF(cls); |
| 5128 | } |
| 5129 | Py_DECREF(args); |
| 5130 | if (obj == NULL) |
| 5131 | return -1; |
| 5132 | |
| 5133 | PDATA_PUSH(self->stack, obj, -1); |
| 5134 | return 0; |
| 5135 | } |
| 5136 | |
| 5137 | static int |
| 5138 | load_inst(UnpicklerObject *self) |
| 5139 | { |
| 5140 | PyObject *cls = NULL; |
| 5141 | PyObject *args = NULL; |
| 5142 | PyObject *obj = NULL; |
| 5143 | PyObject *module_name; |
| 5144 | PyObject *class_name; |
| 5145 | Py_ssize_t len; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5146 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5147 | char *s; |
| 5148 | |
| 5149 | if ((i = marker(self)) < 0) |
| 5150 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5151 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5152 | return -1; |
| 5153 | if (len < 2) |
| 5154 | return bad_readline(); |
| 5155 | |
| 5156 | /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII |
| 5157 | identifiers are permitted in Python 3.0, since the INST opcode is only |
| 5158 | supported by older protocols on Python 2.x. */ |
| 5159 | module_name = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
| 5160 | if (module_name == NULL) |
| 5161 | return -1; |
| 5162 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5163 | if ((len = _Unpickler_Readline(self, &s)) >= 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5164 | if (len < 2) |
| 5165 | return bad_readline(); |
| 5166 | class_name = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5167 | if (class_name != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5168 | cls = find_class(self, module_name, class_name); |
| 5169 | Py_DECREF(class_name); |
| 5170 | } |
| 5171 | } |
| 5172 | Py_DECREF(module_name); |
| 5173 | |
| 5174 | if (cls == NULL) |
| 5175 | return -1; |
| 5176 | |
| 5177 | if ((args = Pdata_poptuple(self->stack, i)) != NULL) { |
| 5178 | obj = instantiate(cls, args); |
| 5179 | Py_DECREF(args); |
| 5180 | } |
| 5181 | Py_DECREF(cls); |
| 5182 | |
| 5183 | if (obj == NULL) |
| 5184 | return -1; |
| 5185 | |
| 5186 | PDATA_PUSH(self->stack, obj, -1); |
| 5187 | return 0; |
| 5188 | } |
| 5189 | |
| 5190 | static int |
| 5191 | load_newobj(UnpicklerObject *self) |
| 5192 | { |
| 5193 | PyObject *args = NULL; |
| 5194 | PyObject *clsraw = NULL; |
| 5195 | PyTypeObject *cls; /* clsraw cast to its true type */ |
| 5196 | PyObject *obj; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5197 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5198 | |
| 5199 | /* Stack is ... cls argtuple, and we want to call |
| 5200 | * cls.__new__(cls, *argtuple). |
| 5201 | */ |
| 5202 | PDATA_POP(self->stack, args); |
| 5203 | if (args == NULL) |
| 5204 | goto error; |
| 5205 | if (!PyTuple_Check(args)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5206 | PyErr_SetString(st->UnpicklingError, |
| 5207 | "NEWOBJ expected an arg " "tuple."); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5208 | goto error; |
| 5209 | } |
| 5210 | |
| 5211 | PDATA_POP(self->stack, clsraw); |
| 5212 | cls = (PyTypeObject *)clsraw; |
| 5213 | if (cls == NULL) |
| 5214 | goto error; |
| 5215 | if (!PyType_Check(cls)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5216 | PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5217 | "isn't a type object"); |
| 5218 | goto error; |
| 5219 | } |
| 5220 | if (cls->tp_new == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5221 | PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5222 | "has NULL tp_new"); |
| 5223 | goto error; |
| 5224 | } |
| 5225 | |
| 5226 | /* Call __new__. */ |
| 5227 | obj = cls->tp_new(cls, args, NULL); |
| 5228 | if (obj == NULL) |
| 5229 | goto error; |
| 5230 | |
| 5231 | Py_DECREF(args); |
| 5232 | Py_DECREF(clsraw); |
| 5233 | PDATA_PUSH(self->stack, obj, -1); |
| 5234 | return 0; |
| 5235 | |
| 5236 | error: |
| 5237 | Py_XDECREF(args); |
| 5238 | Py_XDECREF(clsraw); |
| 5239 | return -1; |
| 5240 | } |
| 5241 | |
| 5242 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5243 | load_newobj_ex(UnpicklerObject *self) |
| 5244 | { |
| 5245 | PyObject *cls, *args, *kwargs; |
| 5246 | PyObject *obj; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5247 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5248 | |
| 5249 | PDATA_POP(self->stack, kwargs); |
| 5250 | if (kwargs == NULL) { |
| 5251 | return -1; |
| 5252 | } |
| 5253 | PDATA_POP(self->stack, args); |
| 5254 | if (args == NULL) { |
| 5255 | Py_DECREF(kwargs); |
| 5256 | return -1; |
| 5257 | } |
| 5258 | PDATA_POP(self->stack, cls); |
| 5259 | if (cls == NULL) { |
| 5260 | Py_DECREF(kwargs); |
| 5261 | Py_DECREF(args); |
| 5262 | return -1; |
| 5263 | } |
| 5264 | |
| 5265 | if (!PyType_Check(cls)) { |
| 5266 | Py_DECREF(kwargs); |
| 5267 | Py_DECREF(args); |
| 5268 | Py_DECREF(cls); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5269 | PyErr_Format(st->UnpicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5270 | "NEWOBJ_EX class argument must be a type, not %.200s", |
| 5271 | Py_TYPE(cls)->tp_name); |
| 5272 | return -1; |
| 5273 | } |
| 5274 | |
| 5275 | if (((PyTypeObject *)cls)->tp_new == NULL) { |
| 5276 | Py_DECREF(kwargs); |
| 5277 | Py_DECREF(args); |
| 5278 | Py_DECREF(cls); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5279 | PyErr_SetString(st->UnpicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5280 | "NEWOBJ_EX class argument doesn't have __new__"); |
| 5281 | return -1; |
| 5282 | } |
| 5283 | obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs); |
| 5284 | Py_DECREF(kwargs); |
| 5285 | Py_DECREF(args); |
| 5286 | Py_DECREF(cls); |
| 5287 | if (obj == NULL) { |
| 5288 | return -1; |
| 5289 | } |
| 5290 | PDATA_PUSH(self->stack, obj, -1); |
| 5291 | return 0; |
| 5292 | } |
| 5293 | |
| 5294 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5295 | load_global(UnpicklerObject *self) |
| 5296 | { |
| 5297 | PyObject *global = NULL; |
| 5298 | PyObject *module_name; |
| 5299 | PyObject *global_name; |
| 5300 | Py_ssize_t len; |
| 5301 | char *s; |
| 5302 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5303 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5304 | return -1; |
| 5305 | if (len < 2) |
| 5306 | return bad_readline(); |
| 5307 | module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict"); |
| 5308 | if (!module_name) |
| 5309 | return -1; |
| 5310 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5311 | if ((len = _Unpickler_Readline(self, &s)) >= 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5312 | if (len < 2) { |
| 5313 | Py_DECREF(module_name); |
| 5314 | return bad_readline(); |
| 5315 | } |
| 5316 | global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict"); |
| 5317 | if (global_name) { |
| 5318 | global = find_class(self, module_name, global_name); |
| 5319 | Py_DECREF(global_name); |
| 5320 | } |
| 5321 | } |
| 5322 | Py_DECREF(module_name); |
| 5323 | |
| 5324 | if (global == NULL) |
| 5325 | return -1; |
| 5326 | PDATA_PUSH(self->stack, global, -1); |
| 5327 | return 0; |
| 5328 | } |
| 5329 | |
| 5330 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5331 | load_stack_global(UnpicklerObject *self) |
| 5332 | { |
| 5333 | PyObject *global; |
| 5334 | PyObject *module_name; |
| 5335 | PyObject *global_name; |
| 5336 | |
| 5337 | PDATA_POP(self->stack, global_name); |
| 5338 | PDATA_POP(self->stack, module_name); |
| 5339 | if (module_name == NULL || !PyUnicode_CheckExact(module_name) || |
| 5340 | global_name == NULL || !PyUnicode_CheckExact(global_name)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5341 | PickleState *st = _Pickle_GetGlobalState(); |
| 5342 | PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str"); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5343 | Py_XDECREF(global_name); |
| 5344 | Py_XDECREF(module_name); |
| 5345 | return -1; |
| 5346 | } |
| 5347 | global = find_class(self, module_name, global_name); |
| 5348 | Py_DECREF(global_name); |
| 5349 | Py_DECREF(module_name); |
| 5350 | if (global == NULL) |
| 5351 | return -1; |
| 5352 | PDATA_PUSH(self->stack, global, -1); |
| 5353 | return 0; |
| 5354 | } |
| 5355 | |
| 5356 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5357 | load_persid(UnpicklerObject *self) |
| 5358 | { |
| 5359 | PyObject *pid; |
| 5360 | Py_ssize_t len; |
| 5361 | char *s; |
| 5362 | |
| 5363 | if (self->pers_func) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5364 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5365 | return -1; |
| 5366 | if (len < 2) |
| 5367 | return bad_readline(); |
| 5368 | |
| 5369 | pid = PyBytes_FromStringAndSize(s, len - 1); |
| 5370 | if (pid == NULL) |
| 5371 | return -1; |
| 5372 | |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5373 | /* This does not leak since _Pickle_FastCall() steals the reference |
| 5374 | to pid first. */ |
| 5375 | pid = _Pickle_FastCall(self->pers_func, pid); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5376 | if (pid == NULL) |
| 5377 | return -1; |
| 5378 | |
| 5379 | PDATA_PUSH(self->stack, pid, -1); |
| 5380 | return 0; |
| 5381 | } |
| 5382 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5383 | PickleState *st = _Pickle_GetGlobalState(); |
| 5384 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5385 | "A load persistent id instruction was encountered,\n" |
| 5386 | "but no persistent_load function was specified."); |
| 5387 | return -1; |
| 5388 | } |
| 5389 | } |
| 5390 | |
| 5391 | static int |
| 5392 | load_binpersid(UnpicklerObject *self) |
| 5393 | { |
| 5394 | PyObject *pid; |
| 5395 | |
| 5396 | if (self->pers_func) { |
| 5397 | PDATA_POP(self->stack, pid); |
| 5398 | if (pid == NULL) |
| 5399 | return -1; |
| 5400 | |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5401 | /* This does not leak since _Pickle_FastCall() steals the |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5402 | reference to pid first. */ |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5403 | pid = _Pickle_FastCall(self->pers_func, pid); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5404 | if (pid == NULL) |
| 5405 | return -1; |
| 5406 | |
| 5407 | PDATA_PUSH(self->stack, pid, -1); |
| 5408 | return 0; |
| 5409 | } |
| 5410 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5411 | PickleState *st = _Pickle_GetGlobalState(); |
| 5412 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5413 | "A load persistent id instruction was encountered,\n" |
| 5414 | "but no persistent_load function was specified."); |
| 5415 | return -1; |
| 5416 | } |
| 5417 | } |
| 5418 | |
| 5419 | static int |
| 5420 | load_pop(UnpicklerObject *self) |
| 5421 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5422 | Py_ssize_t len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5423 | |
| 5424 | /* Note that we split the (pickle.py) stack into two stacks, |
| 5425 | * an object stack and a mark stack. We have to be clever and |
| 5426 | * 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] | 5427 | * mark stack first, and only signalling a stack underflow if |
| 5428 | * the object stack is empty and the mark stack doesn't match |
| 5429 | * our expectations. |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5430 | */ |
Collin Winter | 8ca69de | 2009-05-26 16:53:41 +0000 | [diff] [blame] | 5431 | if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5432 | self->num_marks--; |
Antoine Pitrou | 01a15ea | 2010-01-07 17:57:31 +0000 | [diff] [blame] | 5433 | } else if (len > 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5434 | len--; |
| 5435 | Py_DECREF(self->stack->data[len]); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5436 | Py_SIZE(self->stack) = len; |
Collin Winter | 8ca69de | 2009-05-26 16:53:41 +0000 | [diff] [blame] | 5437 | } else { |
| 5438 | return stack_underflow(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5439 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5440 | return 0; |
| 5441 | } |
| 5442 | |
| 5443 | static int |
| 5444 | load_pop_mark(UnpicklerObject *self) |
| 5445 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5446 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5447 | |
| 5448 | if ((i = marker(self)) < 0) |
| 5449 | return -1; |
| 5450 | |
| 5451 | Pdata_clear(self->stack, i); |
| 5452 | |
| 5453 | return 0; |
| 5454 | } |
| 5455 | |
| 5456 | static int |
| 5457 | load_dup(UnpicklerObject *self) |
| 5458 | { |
| 5459 | PyObject *last; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5460 | Py_ssize_t len; |
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 | if ((len = Py_SIZE(self->stack)) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5463 | return stack_underflow(); |
| 5464 | last = self->stack->data[len - 1]; |
| 5465 | PDATA_APPEND(self->stack, last, -1); |
| 5466 | return 0; |
| 5467 | } |
| 5468 | |
| 5469 | static int |
| 5470 | load_get(UnpicklerObject *self) |
| 5471 | { |
| 5472 | PyObject *key, *value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5473 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5474 | Py_ssize_t len; |
| 5475 | char *s; |
| 5476 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5477 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5478 | return -1; |
| 5479 | if (len < 2) |
| 5480 | return bad_readline(); |
| 5481 | |
| 5482 | key = PyLong_FromString(s, NULL, 10); |
| 5483 | if (key == NULL) |
| 5484 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5485 | idx = PyLong_AsSsize_t(key); |
| 5486 | if (idx == -1 && PyErr_Occurred()) { |
| 5487 | Py_DECREF(key); |
| 5488 | return -1; |
| 5489 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5490 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5491 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5492 | if (value == NULL) { |
| 5493 | if (!PyErr_Occurred()) |
| 5494 | PyErr_SetObject(PyExc_KeyError, key); |
| 5495 | Py_DECREF(key); |
| 5496 | return -1; |
| 5497 | } |
| 5498 | Py_DECREF(key); |
| 5499 | |
| 5500 | PDATA_APPEND(self->stack, value, -1); |
| 5501 | return 0; |
| 5502 | } |
| 5503 | |
| 5504 | static int |
| 5505 | load_binget(UnpicklerObject *self) |
| 5506 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5507 | PyObject *value; |
| 5508 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5509 | char *s; |
| 5510 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5511 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5512 | return -1; |
| 5513 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5514 | idx = Py_CHARMASK(s[0]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5515 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5516 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5517 | if (value == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5518 | PyObject *key = PyLong_FromSsize_t(idx); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5519 | if (key != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5520 | PyErr_SetObject(PyExc_KeyError, key); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5521 | Py_DECREF(key); |
| 5522 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5523 | return -1; |
| 5524 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5525 | |
| 5526 | PDATA_APPEND(self->stack, value, -1); |
| 5527 | return 0; |
| 5528 | } |
| 5529 | |
| 5530 | static int |
| 5531 | load_long_binget(UnpicklerObject *self) |
| 5532 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5533 | PyObject *value; |
| 5534 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5535 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5536 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5537 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5538 | return -1; |
| 5539 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5540 | idx = calc_binsize(s, 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5541 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5542 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5543 | if (value == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5544 | PyObject *key = PyLong_FromSsize_t(idx); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5545 | if (key != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5546 | PyErr_SetObject(PyExc_KeyError, key); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5547 | Py_DECREF(key); |
| 5548 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5549 | return -1; |
| 5550 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5551 | |
| 5552 | PDATA_APPEND(self->stack, value, -1); |
| 5553 | return 0; |
| 5554 | } |
| 5555 | |
| 5556 | /* Push an object from the extension registry (EXT[124]). nbytes is |
| 5557 | * the number of bytes following the opcode, holding the index (code) value. |
| 5558 | */ |
| 5559 | static int |
| 5560 | load_extension(UnpicklerObject *self, int nbytes) |
| 5561 | { |
| 5562 | char *codebytes; /* the nbytes bytes after the opcode */ |
| 5563 | long code; /* calc_binint returns long */ |
| 5564 | PyObject *py_code; /* code as a Python int */ |
| 5565 | PyObject *obj; /* the object to push */ |
| 5566 | PyObject *pair; /* (module_name, class_name) */ |
| 5567 | PyObject *module_name, *class_name; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5568 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5569 | |
| 5570 | assert(nbytes == 1 || nbytes == 2 || nbytes == 4); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5571 | if (_Unpickler_Read(self, &codebytes, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5572 | return -1; |
| 5573 | code = calc_binint(codebytes, nbytes); |
| 5574 | if (code <= 0) { /* note that 0 is forbidden */ |
| 5575 | /* Corrupt or hostile pickle. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5576 | PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5577 | return -1; |
| 5578 | } |
| 5579 | |
| 5580 | /* Look for the code in the cache. */ |
| 5581 | py_code = PyLong_FromLong(code); |
| 5582 | if (py_code == NULL) |
| 5583 | return -1; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame^] | 5584 | obj = PyDict_GetItemWithError(st->extension_cache, py_code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5585 | if (obj != NULL) { |
| 5586 | /* Bingo. */ |
| 5587 | Py_DECREF(py_code); |
| 5588 | PDATA_APPEND(self->stack, obj, -1); |
| 5589 | return 0; |
| 5590 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame^] | 5591 | if (PyErr_Occurred()) { |
| 5592 | Py_DECREF(py_code); |
| 5593 | return -1; |
| 5594 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5595 | |
| 5596 | /* Look up the (module_name, class_name) pair. */ |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame^] | 5597 | pair = PyDict_GetItemWithError(st->inverted_registry, py_code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5598 | if (pair == NULL) { |
| 5599 | Py_DECREF(py_code); |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame^] | 5600 | if (!PyErr_Occurred()) { |
| 5601 | PyErr_Format(PyExc_ValueError, "unregistered extension " |
| 5602 | "code %ld", code); |
| 5603 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5604 | return -1; |
| 5605 | } |
| 5606 | /* Since the extension registry is manipulable via Python code, |
| 5607 | * confirm that pair is really a 2-tuple of strings. |
| 5608 | */ |
| 5609 | if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 || |
| 5610 | !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) || |
| 5611 | !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) { |
| 5612 | Py_DECREF(py_code); |
| 5613 | PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] " |
| 5614 | "isn't a 2-tuple of strings", code); |
| 5615 | return -1; |
| 5616 | } |
| 5617 | /* Load the object. */ |
| 5618 | obj = find_class(self, module_name, class_name); |
| 5619 | if (obj == NULL) { |
| 5620 | Py_DECREF(py_code); |
| 5621 | return -1; |
| 5622 | } |
| 5623 | /* Cache code -> obj. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5624 | code = PyDict_SetItem(st->extension_cache, py_code, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5625 | Py_DECREF(py_code); |
| 5626 | if (code < 0) { |
| 5627 | Py_DECREF(obj); |
| 5628 | return -1; |
| 5629 | } |
| 5630 | PDATA_PUSH(self->stack, obj, -1); |
| 5631 | return 0; |
| 5632 | } |
| 5633 | |
| 5634 | static int |
| 5635 | load_put(UnpicklerObject *self) |
| 5636 | { |
| 5637 | PyObject *key, *value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5638 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5639 | Py_ssize_t len; |
| 5640 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5641 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5642 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5643 | return -1; |
| 5644 | if (len < 2) |
| 5645 | return bad_readline(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5646 | if (Py_SIZE(self->stack) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5647 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5648 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5649 | |
| 5650 | key = PyLong_FromString(s, NULL, 10); |
| 5651 | if (key == NULL) |
| 5652 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5653 | idx = PyLong_AsSsize_t(key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5654 | Py_DECREF(key); |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5655 | if (idx < 0) { |
| 5656 | if (!PyErr_Occurred()) |
| 5657 | PyErr_SetString(PyExc_ValueError, |
| 5658 | "negative PUT argument"); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5659 | return -1; |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5660 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5661 | |
| 5662 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5663 | } |
| 5664 | |
| 5665 | static int |
| 5666 | load_binput(UnpicklerObject *self) |
| 5667 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5668 | PyObject *value; |
| 5669 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5670 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5671 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5672 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5673 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5674 | |
| 5675 | if (Py_SIZE(self->stack) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5676 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5677 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5678 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5679 | idx = Py_CHARMASK(s[0]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5680 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5681 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5682 | } |
| 5683 | |
| 5684 | static int |
| 5685 | load_long_binput(UnpicklerObject *self) |
| 5686 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5687 | PyObject *value; |
| 5688 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5689 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5690 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5691 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5692 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5693 | |
| 5694 | if (Py_SIZE(self->stack) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5695 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5696 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5697 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5698 | idx = calc_binsize(s, 4); |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5699 | if (idx < 0) { |
| 5700 | PyErr_SetString(PyExc_ValueError, |
| 5701 | "negative LONG_BINPUT argument"); |
| 5702 | return -1; |
| 5703 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5704 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5705 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5706 | } |
| 5707 | |
| 5708 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5709 | load_memoize(UnpicklerObject *self) |
| 5710 | { |
| 5711 | PyObject *value; |
| 5712 | |
| 5713 | if (Py_SIZE(self->stack) <= 0) |
| 5714 | return stack_underflow(); |
| 5715 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
| 5716 | |
| 5717 | return _Unpickler_MemoPut(self, self->memo_len, value); |
| 5718 | } |
| 5719 | |
| 5720 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5721 | do_append(UnpicklerObject *self, Py_ssize_t x) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5722 | { |
| 5723 | PyObject *value; |
| 5724 | PyObject *list; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5725 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5726 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5727 | len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5728 | if (x > len || x <= 0) |
| 5729 | return stack_underflow(); |
| 5730 | if (len == x) /* nothing to do */ |
| 5731 | return 0; |
| 5732 | |
| 5733 | list = self->stack->data[x - 1]; |
| 5734 | |
| 5735 | if (PyList_Check(list)) { |
| 5736 | PyObject *slice; |
| 5737 | Py_ssize_t list_len; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5738 | int ret; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5739 | |
| 5740 | slice = Pdata_poplist(self->stack, x); |
| 5741 | if (!slice) |
| 5742 | return -1; |
| 5743 | list_len = PyList_GET_SIZE(list); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5744 | ret = PyList_SetSlice(list, list_len, list_len, slice); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5745 | Py_DECREF(slice); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5746 | return ret; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5747 | } |
| 5748 | else { |
| 5749 | PyObject *append_func; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5750 | _Py_IDENTIFIER(append); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5751 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 5752 | append_func = _PyObject_GetAttrId(list, &PyId_append); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5753 | if (append_func == NULL) |
| 5754 | return -1; |
| 5755 | for (i = x; i < len; i++) { |
| 5756 | PyObject *result; |
| 5757 | |
| 5758 | value = self->stack->data[i]; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5759 | result = _Pickle_FastCall(append_func, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5760 | if (result == NULL) { |
| 5761 | Pdata_clear(self->stack, i + 1); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5762 | Py_SIZE(self->stack) = x; |
Alexandre Vassalotti | 637c7c4 | 2013-04-20 21:28:21 -0700 | [diff] [blame] | 5763 | Py_DECREF(append_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5764 | return -1; |
| 5765 | } |
| 5766 | Py_DECREF(result); |
| 5767 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5768 | Py_SIZE(self->stack) = x; |
Alexandre Vassalotti | 637c7c4 | 2013-04-20 21:28:21 -0700 | [diff] [blame] | 5769 | Py_DECREF(append_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5770 | } |
| 5771 | |
| 5772 | return 0; |
| 5773 | } |
| 5774 | |
| 5775 | static int |
| 5776 | load_append(UnpicklerObject *self) |
| 5777 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5778 | return do_append(self, Py_SIZE(self->stack) - 1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5779 | } |
| 5780 | |
| 5781 | static int |
| 5782 | load_appends(UnpicklerObject *self) |
| 5783 | { |
| 5784 | return do_append(self, marker(self)); |
| 5785 | } |
| 5786 | |
| 5787 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5788 | do_setitems(UnpicklerObject *self, Py_ssize_t x) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5789 | { |
| 5790 | PyObject *value, *key; |
| 5791 | PyObject *dict; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5792 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5793 | int status = 0; |
| 5794 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5795 | len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5796 | if (x > len || x <= 0) |
| 5797 | return stack_underflow(); |
| 5798 | if (len == x) /* nothing to do */ |
| 5799 | return 0; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 5800 | if ((len - x) % 2 != 0) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5801 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5802 | /* Currupt or hostile pickle -- we never write one like this. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5803 | PyErr_SetString(st->UnpicklingError, |
| 5804 | "odd number of items for SETITEMS"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5805 | return -1; |
| 5806 | } |
| 5807 | |
| 5808 | /* Here, dict does not actually need to be a PyDict; it could be anything |
| 5809 | that supports the __setitem__ attribute. */ |
| 5810 | dict = self->stack->data[x - 1]; |
| 5811 | |
| 5812 | for (i = x + 1; i < len; i += 2) { |
| 5813 | key = self->stack->data[i - 1]; |
| 5814 | value = self->stack->data[i]; |
| 5815 | if (PyObject_SetItem(dict, key, value) < 0) { |
| 5816 | status = -1; |
| 5817 | break; |
| 5818 | } |
| 5819 | } |
| 5820 | |
| 5821 | Pdata_clear(self->stack, x); |
| 5822 | return status; |
| 5823 | } |
| 5824 | |
| 5825 | static int |
| 5826 | load_setitem(UnpicklerObject *self) |
| 5827 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5828 | return do_setitems(self, Py_SIZE(self->stack) - 2); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5829 | } |
| 5830 | |
| 5831 | static int |
| 5832 | load_setitems(UnpicklerObject *self) |
| 5833 | { |
| 5834 | return do_setitems(self, marker(self)); |
| 5835 | } |
| 5836 | |
| 5837 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5838 | load_additems(UnpicklerObject *self) |
| 5839 | { |
| 5840 | PyObject *set; |
| 5841 | Py_ssize_t mark, len, i; |
| 5842 | |
| 5843 | mark = marker(self); |
| 5844 | len = Py_SIZE(self->stack); |
| 5845 | if (mark > len || mark <= 0) |
| 5846 | return stack_underflow(); |
| 5847 | if (len == mark) /* nothing to do */ |
| 5848 | return 0; |
| 5849 | |
| 5850 | set = self->stack->data[mark - 1]; |
| 5851 | |
| 5852 | if (PySet_Check(set)) { |
| 5853 | PyObject *items; |
| 5854 | int status; |
| 5855 | |
| 5856 | items = Pdata_poptuple(self->stack, mark); |
| 5857 | if (items == NULL) |
| 5858 | return -1; |
| 5859 | |
| 5860 | status = _PySet_Update(set, items); |
| 5861 | Py_DECREF(items); |
| 5862 | return status; |
| 5863 | } |
| 5864 | else { |
| 5865 | PyObject *add_func; |
| 5866 | _Py_IDENTIFIER(add); |
| 5867 | |
| 5868 | add_func = _PyObject_GetAttrId(set, &PyId_add); |
| 5869 | if (add_func == NULL) |
| 5870 | return -1; |
| 5871 | for (i = mark; i < len; i++) { |
| 5872 | PyObject *result; |
| 5873 | PyObject *item; |
| 5874 | |
| 5875 | item = self->stack->data[i]; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5876 | result = _Pickle_FastCall(add_func, item); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5877 | if (result == NULL) { |
| 5878 | Pdata_clear(self->stack, i + 1); |
| 5879 | Py_SIZE(self->stack) = mark; |
| 5880 | return -1; |
| 5881 | } |
| 5882 | Py_DECREF(result); |
| 5883 | } |
| 5884 | Py_SIZE(self->stack) = mark; |
| 5885 | } |
| 5886 | |
| 5887 | return 0; |
| 5888 | } |
| 5889 | |
| 5890 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5891 | load_build(UnpicklerObject *self) |
| 5892 | { |
| 5893 | PyObject *state, *inst, *slotstate; |
| 5894 | PyObject *setstate; |
| 5895 | int status = 0; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5896 | _Py_IDENTIFIER(__setstate__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5897 | |
| 5898 | /* Stack is ... instance, state. We want to leave instance at |
| 5899 | * the stack top, possibly mutated via instance.__setstate__(state). |
| 5900 | */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5901 | if (Py_SIZE(self->stack) < 2) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5902 | return stack_underflow(); |
| 5903 | |
| 5904 | PDATA_POP(self->stack, state); |
| 5905 | if (state == NULL) |
| 5906 | return -1; |
| 5907 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5908 | inst = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5909 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 5910 | setstate = _PyObject_GetAttrId(inst, &PyId___setstate__); |
Alexandre Vassalotti | 1f9d907 | 2008-08-15 03:07:47 +0000 | [diff] [blame] | 5911 | if (setstate == NULL) { |
| 5912 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 5913 | PyErr_Clear(); |
Antoine Pitrou | d79dc62 | 2008-09-05 00:03:33 +0000 | [diff] [blame] | 5914 | else { |
| 5915 | Py_DECREF(state); |
Alexandre Vassalotti | 1f9d907 | 2008-08-15 03:07:47 +0000 | [diff] [blame] | 5916 | return -1; |
Antoine Pitrou | d79dc62 | 2008-09-05 00:03:33 +0000 | [diff] [blame] | 5917 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5918 | } |
| 5919 | else { |
| 5920 | PyObject *result; |
| 5921 | |
| 5922 | /* The explicit __setstate__ is responsible for everything. */ |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5923 | result = _Pickle_FastCall(setstate, state); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5924 | Py_DECREF(setstate); |
| 5925 | if (result == NULL) |
| 5926 | return -1; |
| 5927 | Py_DECREF(result); |
| 5928 | return 0; |
| 5929 | } |
| 5930 | |
| 5931 | /* A default __setstate__. First see whether state embeds a |
| 5932 | * slot state dict too (a proto 2 addition). |
| 5933 | */ |
| 5934 | if (PyTuple_Check(state) && Py_SIZE(state) == 2) { |
| 5935 | PyObject *tmp = state; |
| 5936 | |
| 5937 | state = PyTuple_GET_ITEM(tmp, 0); |
| 5938 | slotstate = PyTuple_GET_ITEM(tmp, 1); |
| 5939 | Py_INCREF(state); |
| 5940 | Py_INCREF(slotstate); |
| 5941 | Py_DECREF(tmp); |
| 5942 | } |
| 5943 | else |
| 5944 | slotstate = NULL; |
| 5945 | |
| 5946 | /* Set inst.__dict__ from the state dict (if any). */ |
| 5947 | if (state != Py_None) { |
| 5948 | PyObject *dict; |
Antoine Pitrou | a9f48a0 | 2009-05-02 21:41:14 +0000 | [diff] [blame] | 5949 | PyObject *d_key, *d_value; |
| 5950 | Py_ssize_t i; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5951 | _Py_IDENTIFIER(__dict__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5952 | |
| 5953 | if (!PyDict_Check(state)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5954 | PickleState *st = _Pickle_GetGlobalState(); |
| 5955 | PyErr_SetString(st->UnpicklingError, "state is not a dictionary"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5956 | goto error; |
| 5957 | } |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 5958 | dict = _PyObject_GetAttrId(inst, &PyId___dict__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5959 | if (dict == NULL) |
| 5960 | goto error; |
| 5961 | |
Antoine Pitrou | a9f48a0 | 2009-05-02 21:41:14 +0000 | [diff] [blame] | 5962 | i = 0; |
| 5963 | while (PyDict_Next(state, &i, &d_key, &d_value)) { |
| 5964 | /* normally the keys for instance attributes are |
| 5965 | interned. we should try to do that here. */ |
| 5966 | Py_INCREF(d_key); |
| 5967 | if (PyUnicode_CheckExact(d_key)) |
| 5968 | PyUnicode_InternInPlace(&d_key); |
| 5969 | if (PyObject_SetItem(dict, d_key, d_value) < 0) { |
| 5970 | Py_DECREF(d_key); |
| 5971 | goto error; |
| 5972 | } |
| 5973 | Py_DECREF(d_key); |
| 5974 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5975 | Py_DECREF(dict); |
| 5976 | } |
| 5977 | |
| 5978 | /* Also set instance attributes from the slotstate dict (if any). */ |
| 5979 | if (slotstate != NULL) { |
| 5980 | PyObject *d_key, *d_value; |
| 5981 | Py_ssize_t i; |
| 5982 | |
| 5983 | if (!PyDict_Check(slotstate)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5984 | PickleState *st = _Pickle_GetGlobalState(); |
| 5985 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5986 | "slot state is not a dictionary"); |
| 5987 | goto error; |
| 5988 | } |
| 5989 | i = 0; |
| 5990 | while (PyDict_Next(slotstate, &i, &d_key, &d_value)) { |
| 5991 | if (PyObject_SetAttr(inst, d_key, d_value) < 0) |
| 5992 | goto error; |
| 5993 | } |
| 5994 | } |
| 5995 | |
| 5996 | if (0) { |
| 5997 | error: |
| 5998 | status = -1; |
| 5999 | } |
| 6000 | |
| 6001 | Py_DECREF(state); |
| 6002 | Py_XDECREF(slotstate); |
| 6003 | return status; |
| 6004 | } |
| 6005 | |
| 6006 | static int |
| 6007 | load_mark(UnpicklerObject *self) |
| 6008 | { |
| 6009 | |
| 6010 | /* Note that we split the (pickle.py) stack into two stacks, an |
| 6011 | * object stack and a mark stack. Here we push a mark onto the |
| 6012 | * mark stack. |
| 6013 | */ |
| 6014 | |
| 6015 | if ((self->num_marks + 1) >= self->marks_size) { |
| 6016 | size_t alloc; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6017 | Py_ssize_t *marks; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6018 | |
| 6019 | /* Use the size_t type to check for overflow. */ |
| 6020 | alloc = ((size_t)self->num_marks << 1) + 20; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6021 | if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) || |
Alexandre Vassalotti | 7634ff5 | 2008-06-13 02:16:06 +0000 | [diff] [blame] | 6022 | alloc <= ((size_t)self->num_marks + 1)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6023 | PyErr_NoMemory(); |
| 6024 | return -1; |
| 6025 | } |
| 6026 | |
| 6027 | if (self->marks == NULL) |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6028 | marks = (Py_ssize_t *) PyMem_Malloc(alloc * sizeof(Py_ssize_t)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6029 | else |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6030 | marks = (Py_ssize_t *) PyMem_Realloc(self->marks, |
| 6031 | alloc * sizeof(Py_ssize_t)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6032 | if (marks == NULL) { |
| 6033 | PyErr_NoMemory(); |
| 6034 | return -1; |
| 6035 | } |
| 6036 | self->marks = marks; |
| 6037 | self->marks_size = (Py_ssize_t)alloc; |
| 6038 | } |
| 6039 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6040 | self->marks[self->num_marks++] = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6041 | |
| 6042 | return 0; |
| 6043 | } |
| 6044 | |
| 6045 | static int |
| 6046 | load_reduce(UnpicklerObject *self) |
| 6047 | { |
| 6048 | PyObject *callable = NULL; |
| 6049 | PyObject *argtup = NULL; |
| 6050 | PyObject *obj = NULL; |
| 6051 | |
| 6052 | PDATA_POP(self->stack, argtup); |
| 6053 | if (argtup == NULL) |
| 6054 | return -1; |
| 6055 | PDATA_POP(self->stack, callable); |
| 6056 | if (callable) { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 6057 | obj = PyObject_CallObject(callable, argtup); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6058 | Py_DECREF(callable); |
| 6059 | } |
| 6060 | Py_DECREF(argtup); |
| 6061 | |
| 6062 | if (obj == NULL) |
| 6063 | return -1; |
| 6064 | |
| 6065 | PDATA_PUSH(self->stack, obj, -1); |
| 6066 | return 0; |
| 6067 | } |
| 6068 | |
| 6069 | /* Just raises an error if we don't know the protocol specified. PROTO |
| 6070 | * is the first opcode for protocols >= 2. |
| 6071 | */ |
| 6072 | static int |
| 6073 | load_proto(UnpicklerObject *self) |
| 6074 | { |
| 6075 | char *s; |
| 6076 | int i; |
| 6077 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6078 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6079 | return -1; |
| 6080 | |
| 6081 | i = (unsigned char)s[0]; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6082 | if (i <= HIGHEST_PROTOCOL) { |
| 6083 | self->proto = i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6084 | return 0; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6085 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6086 | |
| 6087 | PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i); |
| 6088 | return -1; |
| 6089 | } |
| 6090 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6091 | static int |
| 6092 | load_frame(UnpicklerObject *self) |
| 6093 | { |
| 6094 | char *s; |
| 6095 | Py_ssize_t frame_len; |
| 6096 | |
| 6097 | if (_Unpickler_Read(self, &s, 8) < 0) |
| 6098 | return -1; |
| 6099 | |
| 6100 | frame_len = calc_binsize(s, 8); |
| 6101 | if (frame_len < 0) { |
| 6102 | PyErr_Format(PyExc_OverflowError, |
| 6103 | "FRAME length exceeds system's maximum of %zd bytes", |
| 6104 | PY_SSIZE_T_MAX); |
| 6105 | return -1; |
| 6106 | } |
| 6107 | |
| 6108 | if (_Unpickler_Read(self, &s, frame_len) < 0) |
| 6109 | return -1; |
| 6110 | |
| 6111 | /* Rewind to start of frame */ |
| 6112 | self->next_read_idx -= frame_len; |
| 6113 | return 0; |
| 6114 | } |
| 6115 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6116 | static PyObject * |
| 6117 | load(UnpicklerObject *self) |
| 6118 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6119 | PyObject *value = NULL; |
| 6120 | char *s; |
| 6121 | |
| 6122 | self->num_marks = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6123 | self->proto = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6124 | if (Py_SIZE(self->stack)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6125 | Pdata_clear(self->stack, 0); |
| 6126 | |
| 6127 | /* Convenient macros for the dispatch while-switch loop just below. */ |
| 6128 | #define OP(opcode, load_func) \ |
| 6129 | case opcode: if (load_func(self) < 0) break; continue; |
| 6130 | |
| 6131 | #define OP_ARG(opcode, load_func, arg) \ |
| 6132 | case opcode: if (load_func(self, (arg)) < 0) break; continue; |
| 6133 | |
| 6134 | while (1) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6135 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6136 | break; |
| 6137 | |
| 6138 | switch ((enum opcode)s[0]) { |
| 6139 | OP(NONE, load_none) |
| 6140 | OP(BININT, load_binint) |
| 6141 | OP(BININT1, load_binint1) |
| 6142 | OP(BININT2, load_binint2) |
| 6143 | OP(INT, load_int) |
| 6144 | OP(LONG, load_long) |
| 6145 | OP_ARG(LONG1, load_counted_long, 1) |
| 6146 | OP_ARG(LONG4, load_counted_long, 4) |
| 6147 | OP(FLOAT, load_float) |
| 6148 | OP(BINFLOAT, load_binfloat) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6149 | OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1) |
| 6150 | OP_ARG(BINBYTES, load_counted_binbytes, 4) |
| 6151 | OP_ARG(BINBYTES8, load_counted_binbytes, 8) |
| 6152 | OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1) |
| 6153 | OP_ARG(BINSTRING, load_counted_binstring, 4) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6154 | OP(STRING, load_string) |
| 6155 | OP(UNICODE, load_unicode) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6156 | OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1) |
| 6157 | OP_ARG(BINUNICODE, load_counted_binunicode, 4) |
| 6158 | OP_ARG(BINUNICODE8, load_counted_binunicode, 8) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6159 | OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0) |
| 6160 | OP_ARG(TUPLE1, load_counted_tuple, 1) |
| 6161 | OP_ARG(TUPLE2, load_counted_tuple, 2) |
| 6162 | OP_ARG(TUPLE3, load_counted_tuple, 3) |
| 6163 | OP(TUPLE, load_tuple) |
| 6164 | OP(EMPTY_LIST, load_empty_list) |
| 6165 | OP(LIST, load_list) |
| 6166 | OP(EMPTY_DICT, load_empty_dict) |
| 6167 | OP(DICT, load_dict) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6168 | OP(EMPTY_SET, load_empty_set) |
| 6169 | OP(ADDITEMS, load_additems) |
| 6170 | OP(FROZENSET, load_frozenset) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6171 | OP(OBJ, load_obj) |
| 6172 | OP(INST, load_inst) |
| 6173 | OP(NEWOBJ, load_newobj) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6174 | OP(NEWOBJ_EX, load_newobj_ex) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6175 | OP(GLOBAL, load_global) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6176 | OP(STACK_GLOBAL, load_stack_global) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6177 | OP(APPEND, load_append) |
| 6178 | OP(APPENDS, load_appends) |
| 6179 | OP(BUILD, load_build) |
| 6180 | OP(DUP, load_dup) |
| 6181 | OP(BINGET, load_binget) |
| 6182 | OP(LONG_BINGET, load_long_binget) |
| 6183 | OP(GET, load_get) |
| 6184 | OP(MARK, load_mark) |
| 6185 | OP(BINPUT, load_binput) |
| 6186 | OP(LONG_BINPUT, load_long_binput) |
| 6187 | OP(PUT, load_put) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6188 | OP(MEMOIZE, load_memoize) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6189 | OP(POP, load_pop) |
| 6190 | OP(POP_MARK, load_pop_mark) |
| 6191 | OP(SETITEM, load_setitem) |
| 6192 | OP(SETITEMS, load_setitems) |
| 6193 | OP(PERSID, load_persid) |
| 6194 | OP(BINPERSID, load_binpersid) |
| 6195 | OP(REDUCE, load_reduce) |
| 6196 | OP(PROTO, load_proto) |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6197 | OP(FRAME, load_frame) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6198 | OP_ARG(EXT1, load_extension, 1) |
| 6199 | OP_ARG(EXT2, load_extension, 2) |
| 6200 | OP_ARG(EXT4, load_extension, 4) |
| 6201 | OP_ARG(NEWTRUE, load_bool, Py_True) |
| 6202 | OP_ARG(NEWFALSE, load_bool, Py_False) |
| 6203 | |
| 6204 | case STOP: |
| 6205 | break; |
| 6206 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6207 | default: |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6208 | if (s[0] == '\0') { |
Benjamin Peterson | adde86d | 2011-09-23 13:41:41 -0400 | [diff] [blame] | 6209 | PyErr_SetNone(PyExc_EOFError); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6210 | } |
| 6211 | else { |
| 6212 | PickleState *st = _Pickle_GetGlobalState(); |
| 6213 | PyErr_Format(st->UnpicklingError, |
Benjamin Peterson | adde86d | 2011-09-23 13:41:41 -0400 | [diff] [blame] | 6214 | "invalid load key, '%c'.", s[0]); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6215 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6216 | return NULL; |
| 6217 | } |
| 6218 | |
| 6219 | break; /* and we are done! */ |
| 6220 | } |
| 6221 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6222 | if (PyErr_Occurred()) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6223 | return NULL; |
| 6224 | } |
| 6225 | |
Victor Stinner | 2ae57e3 | 2013-10-31 13:39:23 +0100 | [diff] [blame] | 6226 | if (_Unpickler_SkipConsumed(self) < 0) |
| 6227 | return NULL; |
| 6228 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6229 | PDATA_POP(self->stack, value); |
| 6230 | return value; |
| 6231 | } |
| 6232 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6233 | /*[clinic] |
| 6234 | |
| 6235 | _pickle.Unpickler.load |
| 6236 | |
| 6237 | Load a pickle. |
| 6238 | |
| 6239 | Read a pickled object representation from the open file object given in |
| 6240 | the constructor, and return the reconstituted object hierarchy specified |
| 6241 | therein. |
| 6242 | [clinic]*/ |
| 6243 | |
| 6244 | PyDoc_STRVAR(_pickle_Unpickler_load__doc__, |
| 6245 | "load()\n" |
| 6246 | "Load a pickle.\n" |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6247 | "\n" |
| 6248 | "Read a pickled object representation from the open file object given in\n" |
| 6249 | "the constructor, and return the reconstituted object hierarchy specified\n" |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6250 | "therein."); |
| 6251 | |
| 6252 | #define _PICKLE_UNPICKLER_LOAD_METHODDEF \ |
| 6253 | {"load", (PyCFunction)_pickle_Unpickler_load, METH_NOARGS, _pickle_Unpickler_load__doc__}, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6254 | |
| 6255 | static PyObject * |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6256 | _pickle_Unpickler_load(PyObject *self) |
| 6257 | /*[clinic checksum: 9a30ba4e4d9221d4dcd705e1471ab11b2c9e3ac6]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6258 | { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6259 | UnpicklerObject *unpickler = (UnpicklerObject*)self; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6260 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6261 | /* Check whether the Unpickler was initialized correctly. This prevents |
| 6262 | segfaulting if a subclass overridden __init__ with a function that does |
| 6263 | not call Unpickler.__init__(). Here, we simply ensure that self->read |
| 6264 | is not NULL. */ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6265 | if (unpickler->read == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6266 | PickleState *st = _Pickle_GetGlobalState(); |
| 6267 | PyErr_Format(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6268 | "Unpickler.__init__() was not called by %s.__init__()", |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6269 | Py_TYPE(unpickler)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6270 | return NULL; |
| 6271 | } |
| 6272 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6273 | return load(unpickler); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6274 | } |
| 6275 | |
| 6276 | /* The name of find_class() is misleading. In newer pickle protocols, this |
| 6277 | function is used for loading any global (i.e., functions), not just |
| 6278 | classes. The name is kept only for backward compatibility. */ |
| 6279 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6280 | /*[clinic] |
| 6281 | |
| 6282 | _pickle.Unpickler.find_class |
| 6283 | |
| 6284 | self: UnpicklerObject |
| 6285 | module_name: object |
| 6286 | global_name: object |
| 6287 | / |
| 6288 | |
| 6289 | Return an object from a specified module. |
| 6290 | |
| 6291 | If necessary, the module will be imported. Subclasses may override this |
| 6292 | method (e.g. to restrict unpickling of arbitrary classes and functions). |
| 6293 | |
| 6294 | This method is called whenever a class or a function object is |
| 6295 | needed. Both arguments passed are str objects. |
| 6296 | [clinic]*/ |
| 6297 | |
| 6298 | PyDoc_STRVAR(_pickle_Unpickler_find_class__doc__, |
| 6299 | "find_class(module_name, global_name)\n" |
| 6300 | "Return an object from a specified module.\n" |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6301 | "\n" |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6302 | "If necessary, the module will be imported. Subclasses may override this\n" |
| 6303 | "method (e.g. to restrict unpickling of arbitrary classes and functions).\n" |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6304 | "\n" |
| 6305 | "This method is called whenever a class or a function object is\n" |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6306 | "needed. Both arguments passed are str objects."); |
| 6307 | |
| 6308 | #define _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF \ |
| 6309 | {"find_class", (PyCFunction)_pickle_Unpickler_find_class, METH_VARARGS, _pickle_Unpickler_find_class__doc__}, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6310 | |
| 6311 | static PyObject * |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6312 | _pickle_Unpickler_find_class_impl(UnpicklerObject *self, PyObject *module_name, PyObject *global_name); |
| 6313 | |
| 6314 | static PyObject * |
| 6315 | _pickle_Unpickler_find_class(PyObject *self, PyObject *args) |
| 6316 | { |
| 6317 | PyObject *return_value = NULL; |
| 6318 | PyObject *module_name; |
| 6319 | PyObject *global_name; |
| 6320 | |
| 6321 | if (!PyArg_ParseTuple(args, |
| 6322 | "OO:find_class", |
| 6323 | &module_name, &global_name)) |
| 6324 | goto exit; |
| 6325 | return_value = _pickle_Unpickler_find_class_impl((UnpicklerObject *)self, module_name, global_name); |
| 6326 | |
| 6327 | exit: |
| 6328 | return return_value; |
| 6329 | } |
| 6330 | |
| 6331 | static PyObject * |
| 6332 | _pickle_Unpickler_find_class_impl(UnpicklerObject *self, PyObject *module_name, PyObject *global_name) |
| 6333 | /*[clinic checksum: b7d05d4dd8adc698e5780c1ac2be0f5062d33915]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6334 | { |
| 6335 | PyObject *global; |
| 6336 | PyObject *modules_dict; |
| 6337 | PyObject *module; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6338 | _Py_IDENTIFIER(modules); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6339 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6340 | /* Try to map the old names used in Python 2.x to the new ones used in |
| 6341 | Python 3.x. We do this only with old pickle protocols and when the |
| 6342 | user has not disabled the feature. */ |
| 6343 | if (self->proto < 3 && self->fix_imports) { |
| 6344 | PyObject *key; |
| 6345 | PyObject *item; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6346 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6347 | |
| 6348 | /* Check if the global (i.e., a function or a class) was renamed |
| 6349 | or moved to another module. */ |
| 6350 | key = PyTuple_Pack(2, module_name, global_name); |
| 6351 | if (key == NULL) |
| 6352 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6353 | item = PyDict_GetItemWithError(st->name_mapping_2to3, key); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6354 | Py_DECREF(key); |
| 6355 | if (item) { |
| 6356 | if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
| 6357 | PyErr_Format(PyExc_RuntimeError, |
| 6358 | "_compat_pickle.NAME_MAPPING values should be " |
| 6359 | "2-tuples, not %.200s", Py_TYPE(item)->tp_name); |
| 6360 | return NULL; |
| 6361 | } |
| 6362 | module_name = PyTuple_GET_ITEM(item, 0); |
| 6363 | global_name = PyTuple_GET_ITEM(item, 1); |
| 6364 | if (!PyUnicode_Check(module_name) || |
| 6365 | !PyUnicode_Check(global_name)) { |
| 6366 | PyErr_Format(PyExc_RuntimeError, |
| 6367 | "_compat_pickle.NAME_MAPPING values should be " |
| 6368 | "pairs of str, not (%.200s, %.200s)", |
| 6369 | Py_TYPE(module_name)->tp_name, |
| 6370 | Py_TYPE(global_name)->tp_name); |
| 6371 | return NULL; |
| 6372 | } |
| 6373 | } |
| 6374 | else if (PyErr_Occurred()) { |
| 6375 | return NULL; |
| 6376 | } |
| 6377 | |
| 6378 | /* Check if the module was renamed. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6379 | item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6380 | if (item) { |
| 6381 | if (!PyUnicode_Check(item)) { |
| 6382 | PyErr_Format(PyExc_RuntimeError, |
| 6383 | "_compat_pickle.IMPORT_MAPPING values should be " |
| 6384 | "strings, not %.200s", Py_TYPE(item)->tp_name); |
| 6385 | return NULL; |
| 6386 | } |
| 6387 | module_name = item; |
| 6388 | } |
| 6389 | else if (PyErr_Occurred()) { |
| 6390 | return NULL; |
| 6391 | } |
| 6392 | } |
| 6393 | |
Victor Stinner | bb52020 | 2013-11-06 22:40:41 +0100 | [diff] [blame] | 6394 | modules_dict = _PySys_GetObjectId(&PyId_modules); |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 6395 | if (modules_dict == NULL) { |
| 6396 | PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6397 | return NULL; |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 6398 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6399 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6400 | module = PyDict_GetItemWithError(modules_dict, module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6401 | if (module == NULL) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6402 | if (PyErr_Occurred()) |
| 6403 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6404 | module = PyImport_Import(module_name); |
| 6405 | if (module == NULL) |
| 6406 | return NULL; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6407 | global = getattribute(module, global_name, self->proto >= 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6408 | Py_DECREF(module); |
| 6409 | } |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 6410 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6411 | global = getattribute(module, global_name, self->proto >= 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6412 | } |
| 6413 | return global; |
| 6414 | } |
| 6415 | |
| 6416 | static struct PyMethodDef Unpickler_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6417 | _PICKLE_UNPICKLER_LOAD_METHODDEF |
| 6418 | _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6419 | {NULL, NULL} /* sentinel */ |
| 6420 | }; |
| 6421 | |
| 6422 | static void |
| 6423 | Unpickler_dealloc(UnpicklerObject *self) |
| 6424 | { |
| 6425 | PyObject_GC_UnTrack((PyObject *)self); |
| 6426 | Py_XDECREF(self->readline); |
| 6427 | Py_XDECREF(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6428 | Py_XDECREF(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6429 | Py_XDECREF(self->stack); |
| 6430 | Py_XDECREF(self->pers_func); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6431 | if (self->buffer.buf != NULL) { |
| 6432 | PyBuffer_Release(&self->buffer); |
| 6433 | self->buffer.buf = NULL; |
| 6434 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6435 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6436 | _Unpickler_MemoCleanup(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6437 | PyMem_Free(self->marks); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6438 | PyMem_Free(self->input_line); |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6439 | PyMem_Free(self->encoding); |
| 6440 | PyMem_Free(self->errors); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6441 | |
| 6442 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 6443 | } |
| 6444 | |
| 6445 | static int |
| 6446 | Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg) |
| 6447 | { |
| 6448 | Py_VISIT(self->readline); |
| 6449 | Py_VISIT(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6450 | Py_VISIT(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6451 | Py_VISIT(self->stack); |
| 6452 | Py_VISIT(self->pers_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6453 | return 0; |
| 6454 | } |
| 6455 | |
| 6456 | static int |
| 6457 | Unpickler_clear(UnpicklerObject *self) |
| 6458 | { |
| 6459 | Py_CLEAR(self->readline); |
| 6460 | Py_CLEAR(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6461 | Py_CLEAR(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6462 | Py_CLEAR(self->stack); |
| 6463 | Py_CLEAR(self->pers_func); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6464 | if (self->buffer.buf != NULL) { |
| 6465 | PyBuffer_Release(&self->buffer); |
| 6466 | self->buffer.buf = NULL; |
| 6467 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6468 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6469 | _Unpickler_MemoCleanup(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6470 | PyMem_Free(self->marks); |
| 6471 | self->marks = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6472 | PyMem_Free(self->input_line); |
| 6473 | self->input_line = NULL; |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6474 | PyMem_Free(self->encoding); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6475 | self->encoding = NULL; |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6476 | PyMem_Free(self->errors); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6477 | self->errors = NULL; |
| 6478 | |
| 6479 | return 0; |
| 6480 | } |
| 6481 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6482 | /*[clinic] |
| 6483 | |
| 6484 | _pickle.Unpickler.__init__ |
| 6485 | |
| 6486 | self: UnpicklerObject |
| 6487 | file: object |
| 6488 | * |
| 6489 | fix_imports: bool = True |
| 6490 | encoding: str = 'ASCII' |
| 6491 | errors: str = 'strict' |
| 6492 | |
| 6493 | This takes a binary file for reading a pickle data stream. |
| 6494 | |
| 6495 | The protocol version of the pickle is detected automatically, so no |
| 6496 | proto argument is needed. |
| 6497 | |
| 6498 | The file-like object must have two methods, a read() method |
| 6499 | that takes an integer argument, and a readline() method that |
| 6500 | requires no arguments. Both methods should return bytes. |
| 6501 | Thus file-like object can be a binary file object opened for |
| 6502 | reading, a BytesIO object, or any other custom object that |
| 6503 | meets this interface. |
| 6504 | |
| 6505 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
| 6506 | which are used to control compatiblity support for pickle stream |
| 6507 | generated by Python 2.x. If *fix_imports* is True, pickle will try to |
| 6508 | map the old Python 2.x names to the new names used in Python 3.x. The |
| 6509 | *encoding* and *errors* tell pickle how to decode 8-bit string |
| 6510 | instances pickled by Python 2.x; these default to 'ASCII' and |
| 6511 | 'strict', respectively. |
| 6512 | |
| 6513 | [clinic]*/ |
| 6514 | |
| 6515 | PyDoc_STRVAR(_pickle_Unpickler___init____doc__, |
| 6516 | "__init__(file, *, fix_imports=True, encoding=\'ASCII\', errors=\'strict\')\n" |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6517 | "This takes a binary file for reading a pickle data stream.\n" |
| 6518 | "\n" |
| 6519 | "The protocol version of the pickle is detected automatically, so no\n" |
| 6520 | "proto argument is needed.\n" |
| 6521 | "\n" |
| 6522 | "The file-like object must have two methods, a read() method\n" |
| 6523 | "that takes an integer argument, and a readline() method that\n" |
| 6524 | "requires no arguments. Both methods should return bytes.\n" |
| 6525 | "Thus file-like object can be a binary file object opened for\n" |
| 6526 | "reading, a BytesIO object, or any other custom object that\n" |
| 6527 | "meets this interface.\n" |
| 6528 | "\n" |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6529 | "Optional keyword arguments are *fix_imports*, *encoding* and *errors*,\n" |
| 6530 | "which are used to control compatiblity support for pickle stream\n" |
| 6531 | "generated by Python 2.x. If *fix_imports* is True, pickle will try to\n" |
| 6532 | "map the old Python 2.x names to the new names used in Python 3.x. The\n" |
| 6533 | "*encoding* and *errors* tell pickle how to decode 8-bit string\n" |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6534 | "instances pickled by Python 2.x; these default to \'ASCII\' and\n" |
| 6535 | "\'strict\', respectively."); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6536 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6537 | #define _PICKLE_UNPICKLER___INIT___METHODDEF \ |
| 6538 | {"__init__", (PyCFunction)_pickle_Unpickler___init__, METH_VARARGS|METH_KEYWORDS, _pickle_Unpickler___init____doc__}, |
| 6539 | |
| 6540 | static PyObject * |
| 6541 | _pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file, int fix_imports, const char *encoding, const char *errors); |
| 6542 | |
| 6543 | static PyObject * |
| 6544 | _pickle_Unpickler___init__(PyObject *self, PyObject *args, PyObject *kwargs) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6545 | { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6546 | PyObject *return_value = NULL; |
| 6547 | static char *_keywords[] = {"file", "fix_imports", "encoding", "errors", NULL}; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6548 | PyObject *file; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6549 | int fix_imports = 1; |
| 6550 | const char *encoding = "ASCII"; |
| 6551 | const char *errors = "strict"; |
| 6552 | |
| 6553 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 6554 | "O|$pss:__init__", _keywords, |
| 6555 | &file, &fix_imports, &encoding, &errors)) |
| 6556 | goto exit; |
| 6557 | return_value = _pickle_Unpickler___init___impl((UnpicklerObject *)self, file, fix_imports, encoding, errors); |
| 6558 | |
| 6559 | exit: |
| 6560 | return return_value; |
| 6561 | } |
| 6562 | |
| 6563 | static PyObject * |
| 6564 | _pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file, int fix_imports, const char *encoding, const char *errors) |
| 6565 | /*[clinic checksum: bed0d8bbe1c647960ccc6f997b33bf33935fa56f]*/ |
| 6566 | { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 6567 | _Py_IDENTIFIER(persistent_load); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6568 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6569 | /* In case of multiple __init__() calls, clear previous content. */ |
| 6570 | if (self->read != NULL) |
| 6571 | (void)Unpickler_clear(self); |
| 6572 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6573 | if (_Unpickler_SetInputStream(self, file) < 0) |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6574 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6575 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6576 | if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0) |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6577 | return NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6578 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6579 | self->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6580 | if (self->fix_imports == -1) |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6581 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6582 | |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 6583 | if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) { |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6584 | self->pers_func = _PyObject_GetAttrId((PyObject *)self, |
| 6585 | &PyId_persistent_load); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6586 | if (self->pers_func == NULL) |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6587 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6588 | } |
| 6589 | else { |
| 6590 | self->pers_func = NULL; |
| 6591 | } |
| 6592 | |
| 6593 | self->stack = (Pdata *)Pdata_New(); |
| 6594 | if (self->stack == NULL) |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6595 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6596 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6597 | self->memo_size = 32; |
| 6598 | self->memo = _Unpickler_NewMemo(self->memo_size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6599 | if (self->memo == NULL) |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6600 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6601 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6602 | self->proto = 0; |
Alexandre Vassalotti | 0e7aa8c | 2009-04-03 04:17:41 +0000 | [diff] [blame] | 6603 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6604 | Py_RETURN_NONE; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6605 | } |
| 6606 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6607 | /* Wrap the Clinic generated signature to slot it in tp_init. */ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6608 | static int |
| 6609 | Unpickler_init(PyObject *self, PyObject *args, PyObject *kwargs) |
| 6610 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6611 | PyObject *result = _pickle_Unpickler___init__(self, args, kwargs); |
| 6612 | if (result == NULL) { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6613 | return -1; |
| 6614 | } |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6615 | Py_DECREF(result); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6616 | return 0; |
| 6617 | } |
| 6618 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6619 | /* Define a proxy object for the Unpickler's internal memo object. This is to |
| 6620 | * avoid breaking code like: |
| 6621 | * unpickler.memo.clear() |
| 6622 | * and |
| 6623 | * unpickler.memo = saved_memo |
| 6624 | * Is this a good idea? Not really, but we don't want to break code that uses |
| 6625 | * it. Note that we don't implement the entire mapping API here. This is |
| 6626 | * intentional, as these should be treated as black-box implementation details. |
| 6627 | * |
| 6628 | * We do, however, have to implement pickling/unpickling support because of |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 6629 | * real-world code like cvs2svn. |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6630 | */ |
| 6631 | |
| 6632 | typedef struct { |
| 6633 | PyObject_HEAD |
| 6634 | UnpicklerObject *unpickler; |
| 6635 | } UnpicklerMemoProxyObject; |
| 6636 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6637 | /*[clinic] |
| 6638 | _pickle.UnpicklerMemoProxy.clear |
| 6639 | |
| 6640 | self: UnpicklerMemoProxyObject |
| 6641 | |
| 6642 | Remove all items from memo. |
| 6643 | [clinic]*/ |
| 6644 | |
| 6645 | PyDoc_STRVAR(_pickle_UnpicklerMemoProxy_clear__doc__, |
| 6646 | "clear()\n" |
| 6647 | "Remove all items from memo."); |
| 6648 | |
| 6649 | #define _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF \ |
| 6650 | {"clear", (PyCFunction)_pickle_UnpicklerMemoProxy_clear, METH_NOARGS, _pickle_UnpicklerMemoProxy_clear__doc__}, |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6651 | |
| 6652 | static PyObject * |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6653 | _pickle_UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self) |
| 6654 | /*[clinic checksum: 46fecf4e33c0c873124f845edf6cc3a2e9864bd5]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6655 | { |
| 6656 | _Unpickler_MemoCleanup(self->unpickler); |
| 6657 | self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size); |
| 6658 | if (self->unpickler->memo == NULL) |
| 6659 | return NULL; |
| 6660 | Py_RETURN_NONE; |
| 6661 | } |
| 6662 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6663 | /*[clinic] |
| 6664 | _pickle.UnpicklerMemoProxy.copy |
| 6665 | |
| 6666 | self: UnpicklerMemoProxyObject |
| 6667 | |
| 6668 | Copy the memo to a new object. |
| 6669 | [clinic]*/ |
| 6670 | |
| 6671 | PyDoc_STRVAR(_pickle_UnpicklerMemoProxy_copy__doc__, |
| 6672 | "copy()\n" |
| 6673 | "Copy the memo to a new object."); |
| 6674 | |
| 6675 | #define _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF \ |
| 6676 | {"copy", (PyCFunction)_pickle_UnpicklerMemoProxy_copy, METH_NOARGS, _pickle_UnpicklerMemoProxy_copy__doc__}, |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6677 | |
| 6678 | static PyObject * |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6679 | _pickle_UnpicklerMemoProxy_copy(UnpicklerMemoProxyObject *self) |
| 6680 | /*[clinic checksum: f8856c4e8a33540886dfbb245f286af3008fa0ad]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6681 | { |
| 6682 | Py_ssize_t i; |
| 6683 | PyObject *new_memo = PyDict_New(); |
| 6684 | if (new_memo == NULL) |
| 6685 | return NULL; |
| 6686 | |
| 6687 | for (i = 0; i < self->unpickler->memo_size; i++) { |
| 6688 | int status; |
| 6689 | PyObject *key, *value; |
| 6690 | |
| 6691 | value = self->unpickler->memo[i]; |
| 6692 | if (value == NULL) |
| 6693 | continue; |
| 6694 | |
| 6695 | key = PyLong_FromSsize_t(i); |
| 6696 | if (key == NULL) |
| 6697 | goto error; |
| 6698 | status = PyDict_SetItem(new_memo, key, value); |
| 6699 | Py_DECREF(key); |
| 6700 | if (status < 0) |
| 6701 | goto error; |
| 6702 | } |
| 6703 | return new_memo; |
| 6704 | |
| 6705 | error: |
| 6706 | Py_DECREF(new_memo); |
| 6707 | return NULL; |
| 6708 | } |
| 6709 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6710 | /*[clinic] |
| 6711 | _pickle.UnpicklerMemoProxy.__reduce__ |
| 6712 | |
| 6713 | self: UnpicklerMemoProxyObject |
| 6714 | |
| 6715 | Implement pickling support. |
| 6716 | [clinic]*/ |
| 6717 | |
| 6718 | PyDoc_STRVAR(_pickle_UnpicklerMemoProxy___reduce____doc__, |
| 6719 | "__reduce__()\n" |
| 6720 | "Implement pickling support."); |
| 6721 | |
| 6722 | #define _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF \ |
| 6723 | {"__reduce__", (PyCFunction)_pickle_UnpicklerMemoProxy___reduce__, METH_NOARGS, _pickle_UnpicklerMemoProxy___reduce____doc__}, |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6724 | |
| 6725 | static PyObject * |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6726 | _pickle_UnpicklerMemoProxy___reduce__(UnpicklerMemoProxyObject *self) |
| 6727 | /*[clinic checksum: ab5516a77659144e1191c7dd70a0c6c7455660bc]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6728 | { |
| 6729 | PyObject *reduce_value; |
| 6730 | PyObject *constructor_args; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6731 | PyObject *contents = _pickle_UnpicklerMemoProxy_copy(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6732 | if (contents == NULL) |
| 6733 | return NULL; |
| 6734 | |
| 6735 | reduce_value = PyTuple_New(2); |
| 6736 | if (reduce_value == NULL) { |
| 6737 | Py_DECREF(contents); |
| 6738 | return NULL; |
| 6739 | } |
| 6740 | constructor_args = PyTuple_New(1); |
| 6741 | if (constructor_args == NULL) { |
| 6742 | Py_DECREF(contents); |
| 6743 | Py_DECREF(reduce_value); |
| 6744 | return NULL; |
| 6745 | } |
| 6746 | PyTuple_SET_ITEM(constructor_args, 0, contents); |
| 6747 | Py_INCREF((PyObject *)&PyDict_Type); |
| 6748 | PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type); |
| 6749 | PyTuple_SET_ITEM(reduce_value, 1, constructor_args); |
| 6750 | return reduce_value; |
| 6751 | } |
| 6752 | |
| 6753 | static PyMethodDef unpicklerproxy_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6754 | _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF |
| 6755 | _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF |
| 6756 | _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6757 | {NULL, NULL} /* sentinel */ |
| 6758 | }; |
| 6759 | |
| 6760 | static void |
| 6761 | UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self) |
| 6762 | { |
| 6763 | PyObject_GC_UnTrack(self); |
| 6764 | Py_XDECREF(self->unpickler); |
| 6765 | PyObject_GC_Del((PyObject *)self); |
| 6766 | } |
| 6767 | |
| 6768 | static int |
| 6769 | UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self, |
| 6770 | visitproc visit, void *arg) |
| 6771 | { |
| 6772 | Py_VISIT(self->unpickler); |
| 6773 | return 0; |
| 6774 | } |
| 6775 | |
| 6776 | static int |
| 6777 | UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self) |
| 6778 | { |
| 6779 | Py_CLEAR(self->unpickler); |
| 6780 | return 0; |
| 6781 | } |
| 6782 | |
| 6783 | static PyTypeObject UnpicklerMemoProxyType = { |
| 6784 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6785 | "_pickle.UnpicklerMemoProxy", /*tp_name*/ |
| 6786 | sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/ |
| 6787 | 0, |
| 6788 | (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */ |
| 6789 | 0, /* tp_print */ |
| 6790 | 0, /* tp_getattr */ |
| 6791 | 0, /* tp_setattr */ |
| 6792 | 0, /* tp_compare */ |
| 6793 | 0, /* tp_repr */ |
| 6794 | 0, /* tp_as_number */ |
| 6795 | 0, /* tp_as_sequence */ |
| 6796 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 6797 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6798 | 0, /* tp_call */ |
| 6799 | 0, /* tp_str */ |
| 6800 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 6801 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 6802 | 0, /* tp_as_buffer */ |
| 6803 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 6804 | 0, /* tp_doc */ |
| 6805 | (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */ |
| 6806 | (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */ |
| 6807 | 0, /* tp_richcompare */ |
| 6808 | 0, /* tp_weaklistoffset */ |
| 6809 | 0, /* tp_iter */ |
| 6810 | 0, /* tp_iternext */ |
| 6811 | unpicklerproxy_methods, /* tp_methods */ |
| 6812 | }; |
| 6813 | |
| 6814 | static PyObject * |
| 6815 | UnpicklerMemoProxy_New(UnpicklerObject *unpickler) |
| 6816 | { |
| 6817 | UnpicklerMemoProxyObject *self; |
| 6818 | |
| 6819 | self = PyObject_GC_New(UnpicklerMemoProxyObject, |
| 6820 | &UnpicklerMemoProxyType); |
| 6821 | if (self == NULL) |
| 6822 | return NULL; |
| 6823 | Py_INCREF(unpickler); |
| 6824 | self->unpickler = unpickler; |
| 6825 | PyObject_GC_Track(self); |
| 6826 | return (PyObject *)self; |
| 6827 | } |
| 6828 | |
| 6829 | /*****************************************************************************/ |
| 6830 | |
| 6831 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6832 | static PyObject * |
| 6833 | Unpickler_get_memo(UnpicklerObject *self) |
| 6834 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6835 | return UnpicklerMemoProxy_New(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6836 | } |
| 6837 | |
| 6838 | static int |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6839 | Unpickler_set_memo(UnpicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6840 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6841 | PyObject **new_memo; |
| 6842 | Py_ssize_t new_memo_size = 0; |
| 6843 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6844 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6845 | if (obj == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6846 | PyErr_SetString(PyExc_TypeError, |
| 6847 | "attribute deletion is not supported"); |
| 6848 | return -1; |
| 6849 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6850 | |
| 6851 | if (Py_TYPE(obj) == &UnpicklerMemoProxyType) { |
| 6852 | UnpicklerObject *unpickler = |
| 6853 | ((UnpicklerMemoProxyObject *)obj)->unpickler; |
| 6854 | |
| 6855 | new_memo_size = unpickler->memo_size; |
| 6856 | new_memo = _Unpickler_NewMemo(new_memo_size); |
| 6857 | if (new_memo == NULL) |
| 6858 | return -1; |
| 6859 | |
| 6860 | for (i = 0; i < new_memo_size; i++) { |
| 6861 | Py_XINCREF(unpickler->memo[i]); |
| 6862 | new_memo[i] = unpickler->memo[i]; |
| 6863 | } |
| 6864 | } |
| 6865 | else if (PyDict_Check(obj)) { |
| 6866 | Py_ssize_t i = 0; |
| 6867 | PyObject *key, *value; |
| 6868 | |
| 6869 | new_memo_size = PyDict_Size(obj); |
| 6870 | new_memo = _Unpickler_NewMemo(new_memo_size); |
| 6871 | if (new_memo == NULL) |
| 6872 | return -1; |
| 6873 | |
| 6874 | while (PyDict_Next(obj, &i, &key, &value)) { |
| 6875 | Py_ssize_t idx; |
| 6876 | if (!PyLong_Check(key)) { |
| 6877 | PyErr_SetString(PyExc_TypeError, |
| 6878 | "memo key must be integers"); |
| 6879 | goto error; |
| 6880 | } |
| 6881 | idx = PyLong_AsSsize_t(key); |
| 6882 | if (idx == -1 && PyErr_Occurred()) |
| 6883 | goto error; |
Christian Heimes | a24b4d2 | 2013-07-01 15:17:45 +0200 | [diff] [blame] | 6884 | if (idx < 0) { |
| 6885 | PyErr_SetString(PyExc_ValueError, |
Christian Heimes | 8087879 | 2013-07-01 15:23:39 +0200 | [diff] [blame] | 6886 | "memo key must be positive integers."); |
Christian Heimes | a24b4d2 | 2013-07-01 15:17:45 +0200 | [diff] [blame] | 6887 | goto error; |
| 6888 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6889 | if (_Unpickler_MemoPut(self, idx, value) < 0) |
| 6890 | goto error; |
| 6891 | } |
| 6892 | } |
| 6893 | else { |
| 6894 | PyErr_Format(PyExc_TypeError, |
| 6895 | "'memo' attribute must be an UnpicklerMemoProxy object" |
| 6896 | "or dict, not %.200s", Py_TYPE(obj)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6897 | return -1; |
| 6898 | } |
| 6899 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6900 | _Unpickler_MemoCleanup(self); |
| 6901 | self->memo_size = new_memo_size; |
| 6902 | self->memo = new_memo; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6903 | |
| 6904 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6905 | |
| 6906 | error: |
| 6907 | if (new_memo_size) { |
| 6908 | i = new_memo_size; |
| 6909 | while (--i >= 0) { |
| 6910 | Py_XDECREF(new_memo[i]); |
| 6911 | } |
| 6912 | PyMem_FREE(new_memo); |
| 6913 | } |
| 6914 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6915 | } |
| 6916 | |
| 6917 | static PyObject * |
| 6918 | Unpickler_get_persload(UnpicklerObject *self) |
| 6919 | { |
| 6920 | if (self->pers_func == NULL) |
| 6921 | PyErr_SetString(PyExc_AttributeError, "persistent_load"); |
| 6922 | else |
| 6923 | Py_INCREF(self->pers_func); |
| 6924 | return self->pers_func; |
| 6925 | } |
| 6926 | |
| 6927 | static int |
| 6928 | Unpickler_set_persload(UnpicklerObject *self, PyObject *value) |
| 6929 | { |
| 6930 | PyObject *tmp; |
| 6931 | |
| 6932 | if (value == NULL) { |
| 6933 | PyErr_SetString(PyExc_TypeError, |
| 6934 | "attribute deletion is not supported"); |
| 6935 | return -1; |
| 6936 | } |
| 6937 | if (!PyCallable_Check(value)) { |
| 6938 | PyErr_SetString(PyExc_TypeError, |
| 6939 | "persistent_load must be a callable taking " |
| 6940 | "one argument"); |
| 6941 | return -1; |
| 6942 | } |
| 6943 | |
| 6944 | tmp = self->pers_func; |
| 6945 | Py_INCREF(value); |
| 6946 | self->pers_func = value; |
| 6947 | Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */ |
| 6948 | |
| 6949 | return 0; |
| 6950 | } |
| 6951 | |
| 6952 | static PyGetSetDef Unpickler_getsets[] = { |
| 6953 | {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo}, |
| 6954 | {"persistent_load", (getter)Unpickler_get_persload, |
| 6955 | (setter)Unpickler_set_persload}, |
| 6956 | {NULL} |
| 6957 | }; |
| 6958 | |
| 6959 | static PyTypeObject Unpickler_Type = { |
| 6960 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6961 | "_pickle.Unpickler", /*tp_name*/ |
| 6962 | sizeof(UnpicklerObject), /*tp_basicsize*/ |
| 6963 | 0, /*tp_itemsize*/ |
| 6964 | (destructor)Unpickler_dealloc, /*tp_dealloc*/ |
| 6965 | 0, /*tp_print*/ |
| 6966 | 0, /*tp_getattr*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6967 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 6968 | 0, /*tp_reserved*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6969 | 0, /*tp_repr*/ |
| 6970 | 0, /*tp_as_number*/ |
| 6971 | 0, /*tp_as_sequence*/ |
| 6972 | 0, /*tp_as_mapping*/ |
| 6973 | 0, /*tp_hash*/ |
| 6974 | 0, /*tp_call*/ |
| 6975 | 0, /*tp_str*/ |
| 6976 | 0, /*tp_getattro*/ |
| 6977 | 0, /*tp_setattro*/ |
| 6978 | 0, /*tp_as_buffer*/ |
| 6979 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6980 | _pickle_Unpickler___init____doc__, /*tp_doc*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6981 | (traverseproc)Unpickler_traverse, /*tp_traverse*/ |
| 6982 | (inquiry)Unpickler_clear, /*tp_clear*/ |
| 6983 | 0, /*tp_richcompare*/ |
| 6984 | 0, /*tp_weaklistoffset*/ |
| 6985 | 0, /*tp_iter*/ |
| 6986 | 0, /*tp_iternext*/ |
| 6987 | Unpickler_methods, /*tp_methods*/ |
| 6988 | 0, /*tp_members*/ |
| 6989 | Unpickler_getsets, /*tp_getset*/ |
| 6990 | 0, /*tp_base*/ |
| 6991 | 0, /*tp_dict*/ |
| 6992 | 0, /*tp_descr_get*/ |
| 6993 | 0, /*tp_descr_set*/ |
| 6994 | 0, /*tp_dictoffset*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6995 | Unpickler_init, /*tp_init*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6996 | PyType_GenericAlloc, /*tp_alloc*/ |
| 6997 | PyType_GenericNew, /*tp_new*/ |
| 6998 | PyObject_GC_Del, /*tp_free*/ |
| 6999 | 0, /*tp_is_gc*/ |
| 7000 | }; |
| 7001 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7002 | /*[clinic] |
| 7003 | |
| 7004 | _pickle.dump |
| 7005 | |
| 7006 | obj: object |
| 7007 | file: object |
| 7008 | protocol: object = NULL |
| 7009 | * |
| 7010 | fix_imports: bool = True |
| 7011 | |
| 7012 | Write a pickled representation of obj to the open file object file. |
| 7013 | |
| 7014 | This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may be more |
| 7015 | efficient. |
| 7016 | |
| 7017 | The optional protocol argument tells the pickler to use the given protocol |
| 7018 | supported protocols are 0, 1, 2, 3. The default protocol is 3; a |
| 7019 | backward-incompatible protocol designed for Python 3.0. |
| 7020 | |
| 7021 | Specifying a negative protocol version selects the highest protocol version |
| 7022 | supported. The higher the protocol used, the more recent the version of |
| 7023 | Python needed to read the pickle produced. |
| 7024 | |
| 7025 | The file argument must have a write() method that accepts a single bytes |
| 7026 | argument. It can thus be a file object opened for binary writing, a |
| 7027 | io.BytesIO instance, or any other custom object that meets this interface. |
| 7028 | |
| 7029 | If fix_imports is True and protocol is less than 3, pickle will try to |
| 7030 | map the new Python 3.x names to the old module names used in Python 2.x, |
| 7031 | so that the pickle data stream is readable with Python 2.x. |
| 7032 | [clinic]*/ |
| 7033 | |
| 7034 | PyDoc_STRVAR(_pickle_dump__doc__, |
| 7035 | "dump(obj, file, protocol=None, *, fix_imports=True)\n" |
| 7036 | "Write a pickled representation of obj to the open file object file.\n" |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7037 | "\n" |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7038 | "This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may be more\n" |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7039 | "efficient.\n" |
| 7040 | "\n" |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7041 | "The optional protocol argument tells the pickler to use the given protocol\n" |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7042 | "supported protocols are 0, 1, 2, 3. The default protocol is 3; a\n" |
| 7043 | "backward-incompatible protocol designed for Python 3.0.\n" |
| 7044 | "\n" |
| 7045 | "Specifying a negative protocol version selects the highest protocol version\n" |
| 7046 | "supported. The higher the protocol used, the more recent the version of\n" |
| 7047 | "Python needed to read the pickle produced.\n" |
| 7048 | "\n" |
| 7049 | "The file argument must have a write() method that accepts a single bytes\n" |
| 7050 | "argument. It can thus be a file object opened for binary writing, a\n" |
| 7051 | "io.BytesIO instance, or any other custom object that meets this interface.\n" |
| 7052 | "\n" |
| 7053 | "If fix_imports is True and protocol is less than 3, pickle will try to\n" |
| 7054 | "map the new Python 3.x names to the old module names used in Python 2.x,\n" |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7055 | "so that the pickle data stream is readable with Python 2.x."); |
| 7056 | |
| 7057 | #define _PICKLE_DUMP_METHODDEF \ |
| 7058 | {"dump", (PyCFunction)_pickle_dump, METH_VARARGS|METH_KEYWORDS, _pickle_dump__doc__}, |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7059 | |
| 7060 | static PyObject * |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7061 | _pickle_dump_impl(PyModuleDef *module, PyObject *obj, PyObject *file, PyObject *protocol, int fix_imports); |
| 7062 | |
| 7063 | static PyObject * |
| 7064 | _pickle_dump(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7065 | { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7066 | PyObject *return_value = NULL; |
| 7067 | static char *_keywords[] = {"obj", "file", "protocol", "fix_imports", NULL}; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7068 | PyObject *obj; |
| 7069 | PyObject *file; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7070 | PyObject *protocol = NULL; |
| 7071 | int fix_imports = 1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7072 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7073 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 7074 | "OO|O$p:dump", _keywords, |
| 7075 | &obj, &file, &protocol, &fix_imports)) |
| 7076 | goto exit; |
| 7077 | return_value = _pickle_dump_impl(module, obj, file, protocol, fix_imports); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7078 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7079 | exit: |
| 7080 | return return_value; |
| 7081 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7082 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7083 | static PyObject * |
| 7084 | _pickle_dump_impl(PyModuleDef *module, PyObject *obj, PyObject *file, PyObject *protocol, int fix_imports) |
| 7085 | /*[clinic checksum: e442721b16052d921b5e3fbd146d0a62e94a459e]*/ |
| 7086 | { |
| 7087 | PicklerObject *pickler = _Pickler_New(); |
| 7088 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7089 | if (pickler == NULL) |
| 7090 | return NULL; |
| 7091 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7092 | if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7093 | goto error; |
| 7094 | |
| 7095 | if (_Pickler_SetOutputStream(pickler, file) < 0) |
| 7096 | goto error; |
| 7097 | |
| 7098 | if (dump(pickler, obj) < 0) |
| 7099 | goto error; |
| 7100 | |
| 7101 | if (_Pickler_FlushToFile(pickler) < 0) |
| 7102 | goto error; |
| 7103 | |
| 7104 | Py_DECREF(pickler); |
| 7105 | Py_RETURN_NONE; |
| 7106 | |
| 7107 | error: |
| 7108 | Py_XDECREF(pickler); |
| 7109 | return NULL; |
| 7110 | } |
| 7111 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7112 | /*[clinic] |
| 7113 | |
| 7114 | _pickle.dumps |
| 7115 | |
| 7116 | obj: object |
| 7117 | protocol: object = NULL |
| 7118 | * |
| 7119 | fix_imports: bool = True |
| 7120 | |
| 7121 | Return the pickled representation of the object as a bytes object. |
| 7122 | |
| 7123 | The optional protocol argument tells the pickler to use the given protocol; |
| 7124 | supported protocols are 0, 1, 2, 3. The default protocol is 3; a |
| 7125 | backward-incompatible protocol designed for Python 3.0. |
| 7126 | |
| 7127 | Specifying a negative protocol version selects the highest protocol version |
| 7128 | supported. The higher the protocol used, the more recent the version of |
| 7129 | Python needed to read the pickle produced. |
| 7130 | |
| 7131 | If fix_imports is True and *protocol* is less than 3, pickle will try to |
| 7132 | map the new Python 3.x names to the old module names used in Python 2.x, |
| 7133 | so that the pickle data stream is readable with Python 2.x. |
| 7134 | [clinic]*/ |
| 7135 | |
| 7136 | PyDoc_STRVAR(_pickle_dumps__doc__, |
| 7137 | "dumps(obj, protocol=None, *, fix_imports=True)\n" |
| 7138 | "Return the pickled representation of the object as a bytes object.\n" |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7139 | "\n" |
| 7140 | "The optional protocol argument tells the pickler to use the given protocol;\n" |
| 7141 | "supported protocols are 0, 1, 2, 3. The default protocol is 3; a\n" |
| 7142 | "backward-incompatible protocol designed for Python 3.0.\n" |
| 7143 | "\n" |
| 7144 | "Specifying a negative protocol version selects the highest protocol version\n" |
| 7145 | "supported. The higher the protocol used, the more recent the version of\n" |
| 7146 | "Python needed to read the pickle produced.\n" |
| 7147 | "\n" |
| 7148 | "If fix_imports is True and *protocol* is less than 3, pickle will try to\n" |
| 7149 | "map the new Python 3.x names to the old module names used in Python 2.x,\n" |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7150 | "so that the pickle data stream is readable with Python 2.x."); |
| 7151 | |
| 7152 | #define _PICKLE_DUMPS_METHODDEF \ |
| 7153 | {"dumps", (PyCFunction)_pickle_dumps, METH_VARARGS|METH_KEYWORDS, _pickle_dumps__doc__}, |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7154 | |
| 7155 | static PyObject * |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7156 | _pickle_dumps_impl(PyModuleDef *module, PyObject *obj, PyObject *protocol, int fix_imports); |
| 7157 | |
| 7158 | static PyObject * |
| 7159 | _pickle_dumps(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7160 | { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7161 | PyObject *return_value = NULL; |
| 7162 | static char *_keywords[] = {"obj", "protocol", "fix_imports", NULL}; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7163 | PyObject *obj; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7164 | PyObject *protocol = NULL; |
| 7165 | int fix_imports = 1; |
| 7166 | |
| 7167 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 7168 | "O|O$p:dumps", _keywords, |
| 7169 | &obj, &protocol, &fix_imports)) |
| 7170 | goto exit; |
| 7171 | return_value = _pickle_dumps_impl(module, obj, protocol, fix_imports); |
| 7172 | |
| 7173 | exit: |
| 7174 | return return_value; |
| 7175 | } |
| 7176 | |
| 7177 | static PyObject * |
| 7178 | _pickle_dumps_impl(PyModuleDef *module, PyObject *obj, PyObject *protocol, int fix_imports) |
| 7179 | /*[clinic checksum: df6262c4c487f537f47aec8a1709318204c1e174]*/ |
| 7180 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7181 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7182 | PicklerObject *pickler = _Pickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7183 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7184 | if (pickler == NULL) |
| 7185 | return NULL; |
| 7186 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7187 | if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7188 | goto error; |
| 7189 | |
| 7190 | if (dump(pickler, obj) < 0) |
| 7191 | goto error; |
| 7192 | |
| 7193 | result = _Pickler_GetString(pickler); |
| 7194 | Py_DECREF(pickler); |
| 7195 | return result; |
| 7196 | |
| 7197 | error: |
| 7198 | Py_XDECREF(pickler); |
| 7199 | return NULL; |
| 7200 | } |
| 7201 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7202 | /*[clinic] |
| 7203 | |
| 7204 | _pickle.load |
| 7205 | |
| 7206 | file: object |
| 7207 | * |
| 7208 | fix_imports: bool = True |
| 7209 | encoding: str = 'ASCII' |
| 7210 | errors: str = 'strict' |
| 7211 | |
| 7212 | Return a reconstituted object from the pickle data stored in a file. |
| 7213 | |
| 7214 | This is equivalent to ``Unpickler(file).load()``, but may be more efficient. |
| 7215 | |
| 7216 | The protocol version of the pickle is detected automatically, so no protocol |
| 7217 | argument is needed. Bytes past the pickled object's representation are |
| 7218 | ignored. |
| 7219 | |
| 7220 | The argument file must have two methods, a read() method that takes an |
| 7221 | integer argument, and a readline() method that requires no arguments. Both |
| 7222 | methods should return bytes. Thus *file* can be a binary file object opened |
| 7223 | for reading, a BytesIO object, or any other custom object that meets this |
| 7224 | interface. |
| 7225 | |
| 7226 | Optional keyword arguments are fix_imports, encoding and errors, |
| 7227 | which are used to control compatiblity support for pickle stream generated |
| 7228 | by Python 2.x. If fix_imports is True, pickle will try to map the old |
| 7229 | Python 2.x names to the new names used in Python 3.x. The encoding and |
| 7230 | errors tell pickle how to decode 8-bit string instances pickled by Python |
| 7231 | 2.x; these default to 'ASCII' and 'strict', respectively. |
| 7232 | [clinic]*/ |
| 7233 | |
| 7234 | PyDoc_STRVAR(_pickle_load__doc__, |
| 7235 | "load(file, *, fix_imports=True, encoding=\'ASCII\', errors=\'strict\')\n" |
| 7236 | "Return a reconstituted object from the pickle data stored in a file.\n" |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7237 | "\n" |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7238 | "This is equivalent to ``Unpickler(file).load()``, but may be more efficient.\n" |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7239 | "\n" |
| 7240 | "The protocol version of the pickle is detected automatically, so no protocol\n" |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7241 | "argument is needed. Bytes past the pickled object\'s representation are\n" |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7242 | "ignored.\n" |
| 7243 | "\n" |
| 7244 | "The argument file must have two methods, a read() method that takes an\n" |
| 7245 | "integer argument, and a readline() method that requires no arguments. Both\n" |
| 7246 | "methods should return bytes. Thus *file* can be a binary file object opened\n" |
| 7247 | "for reading, a BytesIO object, or any other custom object that meets this\n" |
| 7248 | "interface.\n" |
| 7249 | "\n" |
| 7250 | "Optional keyword arguments are fix_imports, encoding and errors,\n" |
| 7251 | "which are used to control compatiblity support for pickle stream generated\n" |
| 7252 | "by Python 2.x. If fix_imports is True, pickle will try to map the old\n" |
| 7253 | "Python 2.x names to the new names used in Python 3.x. The encoding and\n" |
| 7254 | "errors tell pickle how to decode 8-bit string instances pickled by Python\n" |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7255 | "2.x; these default to \'ASCII\' and \'strict\', respectively."); |
| 7256 | |
| 7257 | #define _PICKLE_LOAD_METHODDEF \ |
| 7258 | {"load", (PyCFunction)_pickle_load, METH_VARARGS|METH_KEYWORDS, _pickle_load__doc__}, |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7259 | |
| 7260 | static PyObject * |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7261 | _pickle_load_impl(PyModuleDef *module, PyObject *file, int fix_imports, const char *encoding, const char *errors); |
| 7262 | |
| 7263 | static PyObject * |
| 7264 | _pickle_load(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7265 | { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7266 | PyObject *return_value = NULL; |
| 7267 | static char *_keywords[] = {"file", "fix_imports", "encoding", "errors", NULL}; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7268 | PyObject *file; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7269 | int fix_imports = 1; |
| 7270 | const char *encoding = "ASCII"; |
| 7271 | const char *errors = "strict"; |
| 7272 | |
| 7273 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 7274 | "O|$pss:load", _keywords, |
| 7275 | &file, &fix_imports, &encoding, &errors)) |
| 7276 | goto exit; |
| 7277 | return_value = _pickle_load_impl(module, file, fix_imports, encoding, errors); |
| 7278 | |
| 7279 | exit: |
| 7280 | return return_value; |
| 7281 | } |
| 7282 | |
| 7283 | static PyObject * |
| 7284 | _pickle_load_impl(PyModuleDef *module, PyObject *file, int fix_imports, const char *encoding, const char *errors) |
| 7285 | /*[clinic checksum: e10796f6765b22ce48dca6940f11b3933853ca35]*/ |
| 7286 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7287 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7288 | UnpicklerObject *unpickler = _Unpickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7289 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7290 | if (unpickler == NULL) |
| 7291 | return NULL; |
| 7292 | |
| 7293 | if (_Unpickler_SetInputStream(unpickler, file) < 0) |
| 7294 | goto error; |
| 7295 | |
| 7296 | if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0) |
| 7297 | goto error; |
| 7298 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7299 | unpickler->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7300 | |
| 7301 | result = load(unpickler); |
| 7302 | Py_DECREF(unpickler); |
| 7303 | return result; |
| 7304 | |
| 7305 | error: |
| 7306 | Py_XDECREF(unpickler); |
| 7307 | return NULL; |
| 7308 | } |
| 7309 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7310 | /*[clinic] |
| 7311 | |
| 7312 | _pickle.loads |
| 7313 | |
| 7314 | data: object |
| 7315 | * |
| 7316 | fix_imports: bool = True |
| 7317 | encoding: str = 'ASCII' |
| 7318 | errors: str = 'strict' |
| 7319 | |
| 7320 | Return a reconstituted object from the given pickle data. |
| 7321 | |
| 7322 | The protocol version of the pickle is detected automatically, so no protocol |
| 7323 | argument is needed. Bytes past the pickled object's representation are |
| 7324 | ignored. |
| 7325 | |
| 7326 | Optional keyword arguments are fix_imports, encoding and errors, which |
| 7327 | are used to control compatiblity support for pickle stream generated |
| 7328 | by Python 2.x. If fix_imports is True, pickle will try to map the old |
| 7329 | Python 2.x names to the new names used in Python 3.x. The encoding and |
| 7330 | errors tell pickle how to decode 8-bit string instances pickled by Python |
| 7331 | 2.x; these default to 'ASCII' and 'strict', respectively. |
| 7332 | [clinic]*/ |
| 7333 | |
| 7334 | PyDoc_STRVAR(_pickle_loads__doc__, |
| 7335 | "loads(data, *, fix_imports=True, encoding=\'ASCII\', errors=\'strict\')\n" |
| 7336 | "Return a reconstituted object from the given pickle data.\n" |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7337 | "\n" |
| 7338 | "The protocol version of the pickle is detected automatically, so no protocol\n" |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7339 | "argument is needed. Bytes past the pickled object\'s representation are\n" |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7340 | "ignored.\n" |
| 7341 | "\n" |
| 7342 | "Optional keyword arguments are fix_imports, encoding and errors, which\n" |
| 7343 | "are used to control compatiblity support for pickle stream generated\n" |
| 7344 | "by Python 2.x. If fix_imports is True, pickle will try to map the old\n" |
| 7345 | "Python 2.x names to the new names used in Python 3.x. The encoding and\n" |
| 7346 | "errors tell pickle how to decode 8-bit string instances pickled by Python\n" |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7347 | "2.x; these default to \'ASCII\' and \'strict\', respectively."); |
| 7348 | |
| 7349 | #define _PICKLE_LOADS_METHODDEF \ |
| 7350 | {"loads", (PyCFunction)_pickle_loads, METH_VARARGS|METH_KEYWORDS, _pickle_loads__doc__}, |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7351 | |
| 7352 | static PyObject * |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7353 | _pickle_loads_impl(PyModuleDef *module, PyObject *data, int fix_imports, const char *encoding, const char *errors); |
| 7354 | |
| 7355 | static PyObject * |
| 7356 | _pickle_loads(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7357 | { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7358 | PyObject *return_value = NULL; |
| 7359 | static char *_keywords[] = {"data", "fix_imports", "encoding", "errors", NULL}; |
| 7360 | PyObject *data; |
| 7361 | int fix_imports = 1; |
| 7362 | const char *encoding = "ASCII"; |
| 7363 | const char *errors = "strict"; |
| 7364 | |
| 7365 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 7366 | "O|$pss:loads", _keywords, |
| 7367 | &data, &fix_imports, &encoding, &errors)) |
| 7368 | goto exit; |
| 7369 | return_value = _pickle_loads_impl(module, data, fix_imports, encoding, errors); |
| 7370 | |
| 7371 | exit: |
| 7372 | return return_value; |
| 7373 | } |
| 7374 | |
| 7375 | static PyObject * |
| 7376 | _pickle_loads_impl(PyModuleDef *module, PyObject *data, int fix_imports, const char *encoding, const char *errors) |
| 7377 | /*[clinic checksum: 29ee725efcbf51a3533c19cb8261a8e267b7080a]*/ |
| 7378 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7379 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7380 | UnpicklerObject *unpickler = _Unpickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7381 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7382 | if (unpickler == NULL) |
| 7383 | return NULL; |
| 7384 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7385 | if (_Unpickler_SetStringInput(unpickler, data) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7386 | goto error; |
| 7387 | |
| 7388 | if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0) |
| 7389 | goto error; |
| 7390 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7391 | unpickler->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7392 | |
| 7393 | result = load(unpickler); |
| 7394 | Py_DECREF(unpickler); |
| 7395 | return result; |
| 7396 | |
| 7397 | error: |
| 7398 | Py_XDECREF(unpickler); |
| 7399 | return NULL; |
| 7400 | } |
| 7401 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7402 | static struct PyMethodDef pickle_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7403 | _PICKLE_DUMP_METHODDEF |
| 7404 | _PICKLE_DUMPS_METHODDEF |
| 7405 | _PICKLE_LOAD_METHODDEF |
| 7406 | _PICKLE_LOADS_METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7407 | {NULL, NULL} /* sentinel */ |
| 7408 | }; |
| 7409 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7410 | static int |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7411 | pickle_clear(PyObject *m) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7412 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7413 | _Pickle_ClearState(_Pickle_GetState(m)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7414 | return 0; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7415 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7416 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7417 | static int |
| 7418 | pickle_traverse(PyObject *m, visitproc visit, void *arg) |
| 7419 | { |
| 7420 | PickleState *st = _Pickle_GetState(m); |
| 7421 | Py_VISIT(st->PickleError); |
| 7422 | Py_VISIT(st->PicklingError); |
| 7423 | Py_VISIT(st->UnpicklingError); |
| 7424 | Py_VISIT(st->dispatch_table); |
| 7425 | Py_VISIT(st->extension_registry); |
| 7426 | Py_VISIT(st->extension_cache); |
| 7427 | Py_VISIT(st->inverted_registry); |
| 7428 | Py_VISIT(st->name_mapping_2to3); |
| 7429 | Py_VISIT(st->import_mapping_2to3); |
| 7430 | Py_VISIT(st->name_mapping_3to2); |
| 7431 | Py_VISIT(st->import_mapping_3to2); |
| 7432 | Py_VISIT(st->codecs_encode); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7433 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7434 | } |
| 7435 | |
| 7436 | static struct PyModuleDef _picklemodule = { |
| 7437 | PyModuleDef_HEAD_INIT, |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7438 | "_pickle", /* m_name */ |
| 7439 | pickle_module_doc, /* m_doc */ |
| 7440 | sizeof(PickleState), /* m_size */ |
| 7441 | pickle_methods, /* m_methods */ |
| 7442 | NULL, /* m_reload */ |
| 7443 | pickle_traverse, /* m_traverse */ |
| 7444 | pickle_clear, /* m_clear */ |
| 7445 | NULL /* m_free */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7446 | }; |
| 7447 | |
| 7448 | PyMODINIT_FUNC |
| 7449 | PyInit__pickle(void) |
| 7450 | { |
| 7451 | PyObject *m; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7452 | PickleState *st; |
| 7453 | |
| 7454 | m = PyState_FindModule(&_picklemodule); |
| 7455 | if (m) { |
| 7456 | Py_INCREF(m); |
| 7457 | return m; |
| 7458 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7459 | |
| 7460 | if (PyType_Ready(&Unpickler_Type) < 0) |
| 7461 | return NULL; |
| 7462 | if (PyType_Ready(&Pickler_Type) < 0) |
| 7463 | return NULL; |
| 7464 | if (PyType_Ready(&Pdata_Type) < 0) |
| 7465 | return NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7466 | if (PyType_Ready(&PicklerMemoProxyType) < 0) |
| 7467 | return NULL; |
| 7468 | if (PyType_Ready(&UnpicklerMemoProxyType) < 0) |
| 7469 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7470 | |
| 7471 | /* Create the module and add the functions. */ |
| 7472 | m = PyModule_Create(&_picklemodule); |
| 7473 | if (m == NULL) |
| 7474 | return NULL; |
| 7475 | |
Antoine Pitrou | 8391cf4 | 2011-07-15 21:01:21 +0200 | [diff] [blame] | 7476 | Py_INCREF(&Pickler_Type); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7477 | if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0) |
| 7478 | return NULL; |
Antoine Pitrou | 8391cf4 | 2011-07-15 21:01:21 +0200 | [diff] [blame] | 7479 | Py_INCREF(&Unpickler_Type); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7480 | if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0) |
| 7481 | return NULL; |
| 7482 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7483 | st = _Pickle_GetState(m); |
| 7484 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7485 | /* Initialize the exceptions. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7486 | st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL); |
| 7487 | if (st->PickleError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7488 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7489 | st->PicklingError = \ |
| 7490 | PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL); |
| 7491 | if (st->PicklingError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7492 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7493 | st->UnpicklingError = \ |
| 7494 | PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL); |
| 7495 | if (st->UnpicklingError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7496 | return NULL; |
| 7497 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7498 | Py_INCREF(st->PickleError); |
| 7499 | if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7500 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7501 | Py_INCREF(st->PicklingError); |
| 7502 | if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7503 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7504 | Py_INCREF(st->UnpicklingError); |
| 7505 | if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7506 | return NULL; |
| 7507 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7508 | if (_Pickle_InitState(st) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7509 | return NULL; |
| 7510 | |
| 7511 | return m; |
| 7512 | } |