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